diff --git a/src/policy/src/main/java/org/etsi/tfs/policy/PolicyServiceImpl.java b/src/policy/src/main/java/org/etsi/tfs/policy/PolicyServiceImpl.java index b5f1d85eb4d55668e16c0bdf32212e6ef559b8c9..c2f98e31efb351166a5ffaee57bbf455e34e48c8 100644 --- a/src/policy/src/main/java/org/etsi/tfs/policy/PolicyServiceImpl.java +++ b/src/policy/src/main/java/org/etsi/tfs/policy/PolicyServiceImpl.java @@ -16,6 +16,8 @@ package org.etsi.tfs.policy; +import static org.etsi.tfs.policy.common.ApplicationProperties.*; + import io.smallrye.mutiny.Multi; import io.smallrye.mutiny.Uni; import io.smallrye.mutiny.groups.UniJoin; @@ -64,8 +66,7 @@ import org.jboss.logging.Logger; public class PolicyServiceImpl implements PolicyService { private static final Logger LOGGER = Logger.getLogger(PolicyServiceImpl.class); - private static final String INVALID_MESSAGE = "%s is invalid."; - private static final String VALID_MESSAGE = "%s is valid."; + private static final int POLICY_EVALUATION_TIMEOUT = 5; private static final int ACCEPTABLE_NUMBER_OF_ALARMS = 3; private static final int MONITORING_WINDOW_IN_SECONDS = 5; @@ -74,39 +75,6 @@ public class PolicyServiceImpl implements PolicyService { // Temporary solution for not calling the same rpc more than it's needed private static int noAlarms = 0; - private static final PolicyRuleState INSERTED_POLICYRULE_STATE = - new PolicyRuleState( - PolicyRuleStateEnum.POLICY_INSERTED, "Successfully entered to INSERTED state"); - private static final PolicyRuleState VALIDATED_POLICYRULE_STATE = - new PolicyRuleState( - PolicyRuleStateEnum.POLICY_VALIDATED, "Successfully transitioned to VALIDATED state"); - private static final PolicyRuleState PROVISIONED_POLICYRULE_STATE = - new PolicyRuleState( - PolicyRuleStateEnum.POLICY_PROVISIONED, - "Successfully transitioned from VALIDATED to PROVISIONED state"); - private static final PolicyRuleState ACTIVE_POLICYRULE_STATE = - new PolicyRuleState( - PolicyRuleStateEnum.POLICY_ACTIVE, - "Successfully transitioned from PROVISIONED to ACTIVE state"); - private static final PolicyRuleState ENFORCED_POLICYRULE_STATE = - new PolicyRuleState( - PolicyRuleStateEnum.POLICY_ENFORCED, - "Successfully transitioned from ACTIVE to ENFORCED state"); - private static final PolicyRuleState INEFFECTIVE_POLICYRULE_STATE = - new PolicyRuleState( - PolicyRuleStateEnum.POLICY_INEFFECTIVE, - "Transitioned from ENFORCED to INEFFECTIVE state"); - private static final PolicyRuleState EFFECTIVE_POLICYRULE_STATE = - new PolicyRuleState( - PolicyRuleStateEnum.POLICY_EFFECTIVE, - "Successfully transitioned from ENFORCED to EFFECTIVE state"); - private static final PolicyRuleState UPDATED_POLICYRULE_STATE = - new PolicyRuleState( - PolicyRuleStateEnum.POLICY_UPDATED, "Successfully entered to UPDATED state"); - private static final PolicyRuleState REMOVED_POLICYRULE_STATE = - new PolicyRuleState( - PolicyRuleStateEnum.POLICY_REMOVED, "Successfully entered to REMOVED state"); - private final ContextService contextService; private final MonitoringService monitoringService; private final ServiceService serviceService; @@ -457,7 +425,9 @@ public class PolicyServiceImpl implements PolicyService { policyRuleBasic.getPolicyRuleId())); contextService.removePolicyRule(policyId).subscribe().with(x -> {}); - subscriptionList.get(policyId).cancel(); + + // TODO: When the Map doesn't contains the policyId we should throw an exception? + if (subscriptionList.contains(policyId)) subscriptionList.get(policyId).cancel(); return policyRuleBasic.getPolicyRuleState(); }); diff --git a/src/policy/src/main/java/org/etsi/tfs/policy/model/PolicyRuleDevice.java b/src/policy/src/main/java/org/etsi/tfs/policy/model/PolicyRuleDevice.java index 9c23692a13827e7701a3877a64fa4d625e25f877..f46635e87bebbcf1dee44987a770b01a2dc3b712 100644 --- a/src/policy/src/main/java/org/etsi/tfs/policy/model/PolicyRuleDevice.java +++ b/src/policy/src/main/java/org/etsi/tfs/policy/model/PolicyRuleDevice.java @@ -40,7 +40,7 @@ public class PolicyRuleDevice { this.policyRuleBasic = policyRuleBasic; this.deviceIds = new ArrayList<String>(); this.isValid = false; - this.exceptionMessage = e.toString(); + this.exceptionMessage = e.getMessage(); } } diff --git a/src/policy/src/test/java/org/etsi/tfs/policy/PolicyAddDeviceTest.java b/src/policy/src/test/java/org/etsi/tfs/policy/PolicyAddDeviceTest.java new file mode 100644 index 0000000000000000000000000000000000000000..1b00248e1b142750e8b247b5d9965939fba0e340 --- /dev/null +++ b/src/policy/src/test/java/org/etsi/tfs/policy/PolicyAddDeviceTest.java @@ -0,0 +1,188 @@ +package org.etsi.tfs.policy; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.etsi.tfs.policy.common.ApplicationProperties.INVALID_MESSAGE; +import static org.etsi.tfs.policy.common.ApplicationProperties.VALIDATED_POLICYRULE_STATE; + +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.mockito.InjectMock; +import io.smallrye.mutiny.Uni; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import javax.inject.Inject; +import org.etsi.tfs.policy.context.ContextService; +import org.etsi.tfs.policy.model.BooleanOperator; +import org.etsi.tfs.policy.model.NumericalOperator; +import org.etsi.tfs.policy.model.PolicyRuleAction; +import org.etsi.tfs.policy.model.PolicyRuleActionConfig; +import org.etsi.tfs.policy.model.PolicyRuleActionEnum; +import org.etsi.tfs.policy.model.PolicyRuleBasic; +import org.etsi.tfs.policy.model.PolicyRuleCondition; +import org.etsi.tfs.policy.model.PolicyRuleDevice; +import org.etsi.tfs.policy.model.PolicyRuleState; +import org.etsi.tfs.policy.model.PolicyRuleStateEnum; +import org.etsi.tfs.policy.monitoring.model.IntegerKpiValue; +import org.etsi.tfs.policy.monitoring.model.KpiValue; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +@QuarkusTest +class PolicyAddDeviceTest { + + @Inject PolicyServiceImpl policyService; + + @InjectMock PolicyRuleConditionValidator policyRuleConditionValidator; + + @InjectMock ContextService contextService; + + static PolicyRuleBasic policyRuleBasic; + static PolicyRuleDevice policyRuleDevice; + + @BeforeAll + static void init() { + + String policyId = "policyRuleId"; + KpiValue kpiValue = new IntegerKpiValue(100); + + PolicyRuleCondition policyRuleCondition = + new PolicyRuleCondition( + "kpiId", NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_GREATER_THAN, kpiValue); + + PolicyRuleActionConfig policyRuleActionConfig = new PolicyRuleActionConfig("key", "value"); + + PolicyRuleAction policyRuleAction = + new PolicyRuleAction( + PolicyRuleActionEnum.POLICY_RULE_ACTION_NO_ACTION, + Arrays.asList(policyRuleActionConfig)); + + policyRuleBasic = + new PolicyRuleBasic( + policyId, + new PolicyRuleState(PolicyRuleStateEnum.POLICY_INSERTED, "Failed due to some errors"), + 1, + Arrays.asList(policyRuleCondition), + BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_OR, + Arrays.asList(policyRuleAction)); + + List<String> deviceIds = Arrays.asList("device1", "device2"); + + policyRuleDevice = new PolicyRuleDevice(policyRuleBasic, deviceIds); + } + + @Test + void deviceListMustNotBeEmpty() + throws ExecutionException, InterruptedException, TimeoutException { + CompletableFuture<PolicyRuleState> message = new CompletableFuture<>(); + + PolicyRuleDevice policyRuleDevice = new PolicyRuleDevice(policyRuleBasic, new ArrayList<>()); + + PolicyRuleState expectedResult = + new PolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED, "Device Ids must not be empty."); + + policyService + .updatePolicyDevice(policyRuleDevice) + .subscribe() + .with( + item -> { + message.complete(item); + }); + + assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage()) + .isEqualTo(expectedResult.getPolicyRuleStateMessage().toString()); + } + + @Test + void isPolicyRuleBasicValid() throws ExecutionException, InterruptedException, TimeoutException { + CompletableFuture<PolicyRuleState> message = new CompletableFuture<>(); + + PolicyRuleBasic policyRuleBasic = + new PolicyRuleBasic( + "policyId", + new PolicyRuleState(PolicyRuleStateEnum.POLICY_INSERTED, "Failed due to some errors"), + 0, + new ArrayList<>(), + BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_OR, + new ArrayList<>()); + + PolicyRuleDevice policyRuleDevice = + new PolicyRuleDevice(policyRuleBasic, Arrays.asList("device1", "device2")); + + PolicyRuleState expectedResult = + new PolicyRuleState( + PolicyRuleStateEnum.POLICY_FAILED, "Policy Rule conditions cannot be empty."); + + policyService + .updatePolicyDevice(policyRuleDevice) + .subscribe() + .with( + item -> { + message.complete(item); + }); + + assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage()) + .isEqualTo(expectedResult.getPolicyRuleStateMessage().toString()); + } + + @Test + void isUpdatedPolicyRuleIdValid() + throws ExecutionException, InterruptedException, TimeoutException { + CompletableFuture<PolicyRuleState> message = new CompletableFuture<>(); + + PolicyRuleDevice policyRuleDevice = + new PolicyRuleDevice(policyRuleBasic, Arrays.asList("device1", "device2")); + + PolicyRuleState expectedResult = + new PolicyRuleState( + PolicyRuleStateEnum.POLICY_FAILED, + String.format( + INVALID_MESSAGE, policyRuleDevice.getPolicyRuleBasic().getPolicyRuleId())); + + Mockito.when(policyRuleConditionValidator.isUpdatedPolicyRuleIdValid(Mockito.anyString())) + .thenReturn(Uni.createFrom().item(Boolean.FALSE)); + + policyService + .updatePolicyDevice(policyRuleDevice) + .subscribe() + .with( + item -> { + message.complete(item); + }); + + assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage()) + .isEqualTo(expectedResult.getPolicyRuleStateMessage().toString()); + } + + @Test + void successUpdatePolicyService() + throws ExecutionException, InterruptedException, TimeoutException { + CompletableFuture<PolicyRuleState> message = new CompletableFuture<>(); + + PolicyRuleDevice policyRuleDevice = + new PolicyRuleDevice(policyRuleBasic, Arrays.asList("device1", "device2")); + + PolicyRuleState expectedResult = + new PolicyRuleState( + PolicyRuleStateEnum.POLICY_VALIDATED, + VALIDATED_POLICYRULE_STATE.getPolicyRuleStateMessage()); + + Mockito.when(policyRuleConditionValidator.isDeviceIdValid(Mockito.anyString())) + .thenReturn(Uni.createFrom().item(Boolean.TRUE)); + + policyService + .addPolicyDevice(policyRuleDevice) + .subscribe() + .with( + item -> { + message.complete(item); + }); + + assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage()) + .isEqualTo(expectedResult.getPolicyRuleStateMessage().toString()); + } +} diff --git a/src/policy/src/test/java/org/etsi/tfs/policy/PolicyDeleteServiceTest.java b/src/policy/src/test/java/org/etsi/tfs/policy/PolicyDeleteServiceTest.java new file mode 100644 index 0000000000000000000000000000000000000000..9609bf11b28c67df135264109574f93ca3a7fef8 --- /dev/null +++ b/src/policy/src/test/java/org/etsi/tfs/policy/PolicyDeleteServiceTest.java @@ -0,0 +1,112 @@ +package org.etsi.tfs.policy; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.etsi.tfs.policy.common.ApplicationProperties.REMOVED_POLICYRULE_STATE; + +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.mockito.InjectMock; +import io.smallrye.mutiny.Uni; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import javax.inject.Inject; +import org.etsi.tfs.policy.context.ContextService; +import org.etsi.tfs.policy.context.model.Service; +import org.etsi.tfs.policy.context.model.ServiceId; +import org.etsi.tfs.policy.context.model.ServiceTypeEnum; +import org.etsi.tfs.policy.model.BooleanOperator; +import org.etsi.tfs.policy.model.NumericalOperator; +import org.etsi.tfs.policy.model.PolicyRule; +import org.etsi.tfs.policy.model.PolicyRuleAction; +import org.etsi.tfs.policy.model.PolicyRuleActionConfig; +import org.etsi.tfs.policy.model.PolicyRuleActionEnum; +import org.etsi.tfs.policy.model.PolicyRuleBasic; +import org.etsi.tfs.policy.model.PolicyRuleCondition; +import org.etsi.tfs.policy.model.PolicyRuleService; +import org.etsi.tfs.policy.model.PolicyRuleState; +import org.etsi.tfs.policy.model.PolicyRuleStateEnum; +import org.etsi.tfs.policy.model.PolicyRuleType; +import org.etsi.tfs.policy.model.PolicyRuleTypeService; +import org.etsi.tfs.policy.monitoring.model.IntegerKpiValue; +import org.etsi.tfs.policy.monitoring.model.KpiValue; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +@QuarkusTest +class PolicyDeleteServiceTest { + + @Inject PolicyServiceImpl policyService; + @InjectMock ContextService contextService; + + static PolicyRuleBasic policyRuleBasic; + static PolicyRuleService policyRuleService; + + @BeforeAll + static void init() { + + String policyId = "policyRuleId"; + KpiValue kpiValue = new IntegerKpiValue(100); + + PolicyRuleCondition policyRuleCondition = + new PolicyRuleCondition( + "kpiId", NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_GREATER_THAN, kpiValue); + + PolicyRuleActionConfig policyRuleActionConfig = new PolicyRuleActionConfig("key", "value"); + + PolicyRuleAction policyRuleAction = + new PolicyRuleAction( + PolicyRuleActionEnum.POLICY_RULE_ACTION_NO_ACTION, + Arrays.asList(policyRuleActionConfig)); + + policyRuleBasic = + new PolicyRuleBasic( + policyId, + new PolicyRuleState(PolicyRuleStateEnum.POLICY_INSERTED, "Failed due to some errors"), + 1, + Arrays.asList(policyRuleCondition), + BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_OR, + Arrays.asList(policyRuleAction)); + + ServiceId serviceId = new ServiceId("contextId", "serviceId"); + + Service service = new Service(serviceId, ServiceTypeEnum.UNKNOWN, null, null, null, null, 0.0); + + List<String> deviceIds = Arrays.asList("device1", "device2"); + + policyRuleService = new PolicyRuleService(policyRuleBasic, serviceId, deviceIds); + } + + @Test + void contextOrServiceIdMustNotBeEmpty() + throws ExecutionException, InterruptedException, TimeoutException { + CompletableFuture<PolicyRuleState> message = new CompletableFuture<>(); + + String policyRuleId = ""; + PolicyRuleState expectedResult = + new PolicyRuleState( + PolicyRuleStateEnum.POLICY_REMOVED, + REMOVED_POLICYRULE_STATE.getPolicyRuleStateMessage()); + + PolicyRuleType policyRuleType = new PolicyRuleTypeService(policyRuleService); + + PolicyRule policyRule = new PolicyRule(policyRuleType); + + Mockito.when(contextService.getPolicyRule(Mockito.anyString())) + .thenReturn(Uni.createFrom().item(policyRule)); + + policyService + .deletePolicy(policyRuleId) + .subscribe() + .with( + item -> { + message.complete(item); + }); + + assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage()) + .isEqualTo(expectedResult.getPolicyRuleStateMessage().toString()); + } +} diff --git a/src/policy/src/test/java/org/etsi/tfs/policy/PolicyUpdateDeviceTest.java b/src/policy/src/test/java/org/etsi/tfs/policy/PolicyUpdateDeviceTest.java new file mode 100644 index 0000000000000000000000000000000000000000..0e8e2e3d0978bb25da4e3f14ef16aa81f41f6d57 --- /dev/null +++ b/src/policy/src/test/java/org/etsi/tfs/policy/PolicyUpdateDeviceTest.java @@ -0,0 +1,185 @@ +package org.etsi.tfs.policy; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.etsi.tfs.policy.common.ApplicationProperties.INVALID_MESSAGE; +import static org.etsi.tfs.policy.common.ApplicationProperties.VALIDATED_POLICYRULE_STATE; + +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.mockito.InjectMock; +import io.smallrye.mutiny.Uni; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import javax.inject.Inject; +import org.etsi.tfs.policy.model.BooleanOperator; +import org.etsi.tfs.policy.model.NumericalOperator; +import org.etsi.tfs.policy.model.PolicyRuleAction; +import org.etsi.tfs.policy.model.PolicyRuleActionConfig; +import org.etsi.tfs.policy.model.PolicyRuleActionEnum; +import org.etsi.tfs.policy.model.PolicyRuleBasic; +import org.etsi.tfs.policy.model.PolicyRuleCondition; +import org.etsi.tfs.policy.model.PolicyRuleDevice; +import org.etsi.tfs.policy.model.PolicyRuleState; +import org.etsi.tfs.policy.model.PolicyRuleStateEnum; +import org.etsi.tfs.policy.monitoring.model.IntegerKpiValue; +import org.etsi.tfs.policy.monitoring.model.KpiValue; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +@QuarkusTest +class PolicyUpdateDeviceTest { + + @Inject PolicyServiceImpl policyService; + + @InjectMock PolicyRuleConditionValidator policyRuleConditionValidator; + + static PolicyRuleBasic policyRuleBasic; + static PolicyRuleDevice policyRuleDevice; + + @BeforeAll + static void init() { + + String policyId = "policyRuleId"; + KpiValue kpiValue = new IntegerKpiValue(100); + + PolicyRuleCondition policyRuleCondition = + new PolicyRuleCondition( + "kpiId", NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_GREATER_THAN, kpiValue); + + PolicyRuleActionConfig policyRuleActionConfig = new PolicyRuleActionConfig("key", "value"); + + PolicyRuleAction policyRuleAction = + new PolicyRuleAction( + PolicyRuleActionEnum.POLICY_RULE_ACTION_NO_ACTION, + Arrays.asList(policyRuleActionConfig)); + + policyRuleBasic = + new PolicyRuleBasic( + policyId, + new PolicyRuleState(PolicyRuleStateEnum.POLICY_INSERTED, "Failed due to some errors"), + 1, + Arrays.asList(policyRuleCondition), + BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_OR, + Arrays.asList(policyRuleAction)); + + List<String> deviceIds = Arrays.asList("device1", "device2"); + + policyRuleDevice = new PolicyRuleDevice(policyRuleBasic, deviceIds); + } + + @Test + void deviceListMustNotBeEmpty() + throws ExecutionException, InterruptedException, TimeoutException { + CompletableFuture<PolicyRuleState> message = new CompletableFuture<>(); + + PolicyRuleDevice policyRuleDevice = new PolicyRuleDevice(policyRuleBasic, new ArrayList<>()); + + PolicyRuleState expectedResult = + new PolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED, "Device Ids must not be empty."); + + policyService + .updatePolicyDevice(policyRuleDevice) + .subscribe() + .with( + item -> { + message.complete(item); + }); + + assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage()) + .isEqualTo(expectedResult.getPolicyRuleStateMessage().toString()); + } + + @Test + void isPolicyRuleBasicValid() throws ExecutionException, InterruptedException, TimeoutException { + CompletableFuture<PolicyRuleState> message = new CompletableFuture<>(); + + PolicyRuleBasic policyRuleBasic = + new PolicyRuleBasic( + "policyId", + new PolicyRuleState(PolicyRuleStateEnum.POLICY_INSERTED, "Failed due to some errors"), + 0, + new ArrayList<>(), + BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_OR, + new ArrayList<>()); + + PolicyRuleDevice policyRuleDevice = + new PolicyRuleDevice(policyRuleBasic, Arrays.asList("device1", "device2")); + + PolicyRuleState expectedResult = + new PolicyRuleState( + PolicyRuleStateEnum.POLICY_FAILED, "Policy Rule conditions cannot be empty."); + + policyService + .updatePolicyDevice(policyRuleDevice) + .subscribe() + .with( + item -> { + message.complete(item); + }); + + assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage()) + .isEqualTo(expectedResult.getPolicyRuleStateMessage().toString()); + } + + @Test + void isUpdatedPolicyRuleIdValid() + throws ExecutionException, InterruptedException, TimeoutException { + CompletableFuture<PolicyRuleState> message = new CompletableFuture<>(); + + PolicyRuleDevice policyRuleDevice = + new PolicyRuleDevice(policyRuleBasic, Arrays.asList("device1", "device2")); + + PolicyRuleState expectedResult = + new PolicyRuleState( + PolicyRuleStateEnum.POLICY_FAILED, + String.format( + INVALID_MESSAGE, policyRuleDevice.getPolicyRuleBasic().getPolicyRuleId())); + + Mockito.when(policyRuleConditionValidator.isUpdatedPolicyRuleIdValid(Mockito.anyString())) + .thenReturn(Uni.createFrom().item(Boolean.FALSE)); + + policyService + .updatePolicyDevice(policyRuleDevice) + .subscribe() + .with( + item -> { + message.complete(item); + }); + + assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage()) + .isEqualTo(expectedResult.getPolicyRuleStateMessage().toString()); + } + + @Test + void successUpdatePolicyService() + throws ExecutionException, InterruptedException, TimeoutException { + CompletableFuture<PolicyRuleState> message = new CompletableFuture<>(); + + PolicyRuleDevice policyRuleDevice = + new PolicyRuleDevice(policyRuleBasic, Arrays.asList("device1")); + + PolicyRuleState expectedResult = + new PolicyRuleState( + PolicyRuleStateEnum.POLICY_VALIDATED, + VALIDATED_POLICYRULE_STATE.getPolicyRuleStateMessage()); + + Mockito.when(policyRuleConditionValidator.isUpdatedPolicyRuleIdValid(Mockito.anyString())) + .thenReturn(Uni.createFrom().item(Boolean.TRUE)); + + policyService + .updatePolicyDevice(policyRuleDevice) + .subscribe() + .with( + item -> { + message.complete(item); + }); + + assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage()) + .isEqualTo(expectedResult.getPolicyRuleStateMessage().toString()); + } +} diff --git a/src/policy/src/test/java/org/etsi/tfs/policy/PolicyServiceTest.java b/src/policy/src/test/java/org/etsi/tfs/policy/PolicyUpdateServiceTest.java similarity index 81% rename from src/policy/src/test/java/org/etsi/tfs/policy/PolicyServiceTest.java rename to src/policy/src/test/java/org/etsi/tfs/policy/PolicyUpdateServiceTest.java index b09cdeea0170a26842f258cfe95c31d57413d61c..3acab688b447c60d22098c53928c677e9a368653 100644 --- a/src/policy/src/test/java/org/etsi/tfs/policy/PolicyServiceTest.java +++ b/src/policy/src/test/java/org/etsi/tfs/policy/PolicyUpdateServiceTest.java @@ -1,6 +1,8 @@ package org.etsi.tfs.policy; import static org.assertj.core.api.Assertions.assertThat; +import static org.etsi.tfs.policy.common.ApplicationProperties.INVALID_MESSAGE; +import static org.etsi.tfs.policy.common.ApplicationProperties.VALIDATED_POLICYRULE_STATE; import io.quarkus.test.junit.QuarkusTest; import io.quarkus.test.junit.mockito.InjectMock; @@ -19,7 +21,6 @@ import org.etsi.tfs.policy.context.model.ServiceId; import org.etsi.tfs.policy.context.model.ServiceTypeEnum; import org.etsi.tfs.policy.model.BooleanOperator; import org.etsi.tfs.policy.model.NumericalOperator; -import org.etsi.tfs.policy.model.PolicyRule; import org.etsi.tfs.policy.model.PolicyRuleAction; import org.etsi.tfs.policy.model.PolicyRuleActionConfig; import org.etsi.tfs.policy.model.PolicyRuleActionEnum; @@ -36,7 +37,7 @@ import org.junit.jupiter.api.Test; import org.mockito.Mockito; @QuarkusTest -public class PolicyServiceTest { +class PolicyUpdateServiceTest { @Inject PolicyServiceImpl policyService; @@ -100,7 +101,7 @@ public class PolicyServiceTest { PolicyRuleStateEnum.POLICY_FAILED, "Context Id of Service Id must not be empty."); policyService - .addPolicyService(policyRuleService) + .updatePolicyService(policyRuleService) .subscribe() .with( item -> { @@ -125,7 +126,7 @@ public class PolicyServiceTest { new PolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED, "Service Id must not be empty."); policyService - .addPolicyService(policyRuleService) + .updatePolicyService(policyRuleService) .subscribe() .with(item -> message.complete(item)); @@ -134,33 +135,26 @@ public class PolicyServiceTest { } @Test - void policyRuleIdMustNotBeEmpty() + void checkMessageIfServiceIsNotValid() throws ExecutionException, InterruptedException, TimeoutException { CompletableFuture<PolicyRuleState> message = new CompletableFuture<>(); - String policyId = ""; - - PolicyRuleBasic policyRuleBasic = - new PolicyRuleBasic( - policyId, - new PolicyRuleState(PolicyRuleStateEnum.POLICY_INSERTED, "Failed due to some errors"), - 1, - new ArrayList<>(), - null, - new ArrayList<>()); - ServiceId serviceId = new ServiceId("contextId", "serviceId"); - Service service = new Service(serviceId, ServiceTypeEnum.UNKNOWN, null, null, null, null, 0.0); - PolicyRuleService policyRuleService = new PolicyRuleService(policyRuleBasic, serviceId, new ArrayList<>()); PolicyRuleState expectedResult = - new PolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED, "Policy rule ID must not be empty."); + new PolicyRuleState( + PolicyRuleStateEnum.POLICY_FAILED, String.format(INVALID_MESSAGE, serviceId)); + + Mockito.when( + policyRuleConditionValidator.isPolicyRuleServiceValid( + Mockito.anyString(), Mockito.any(ServiceId.class))) + .thenReturn(Uni.createFrom().item(Boolean.FALSE)); policyService - .addPolicyService(policyRuleService) + .updatePolicyService(policyRuleService) .subscribe() .with( item -> { @@ -172,23 +166,27 @@ public class PolicyServiceTest { } @Test - void policyServiceSuccess() throws ExecutionException, InterruptedException, TimeoutException { + void successUpdatePolicyService() + throws ExecutionException, InterruptedException, TimeoutException { CompletableFuture<PolicyRuleState> message = new CompletableFuture<>(); + ServiceId serviceId = new ServiceId("contextId", "serviceId"); + + PolicyRuleService policyRuleService = + new PolicyRuleService(policyRuleBasic, serviceId, new ArrayList<>()); + PolicyRuleState expectedResult = new PolicyRuleState( - PolicyRuleStateEnum.POLICY_VALIDATED, "Successfully transitioned to VALIDATED state"); + PolicyRuleStateEnum.POLICY_VALIDATED, + VALIDATED_POLICYRULE_STATE.getPolicyRuleStateMessage()); Mockito.when( - policyRuleConditionValidator.isServiceIdValid( - Mockito.any(ServiceId.class), Mockito.anyList())) + policyRuleConditionValidator.isPolicyRuleServiceValid( + Mockito.anyString(), Mockito.any(ServiceId.class))) .thenReturn(Uni.createFrom().item(Boolean.TRUE)); - Mockito.when(contextService.setPolicyRule(Mockito.any(PolicyRule.class))) - .thenReturn(Uni.createFrom().item("policyRuleId")); - policyService - .addPolicyService(policyRuleService) + .updatePolicyService(policyRuleService) .subscribe() .with( item -> {