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/common/ApplicationProperties.java b/src/policy/src/main/java/org/etsi/tfs/policy/common/ApplicationProperties.java new file mode 100644 index 0000000000000000000000000000000000000000..f01fcd9cca39350bf103fd1e2fe894334c4a3b80 --- /dev/null +++ b/src/policy/src/main/java/org/etsi/tfs/policy/common/ApplicationProperties.java @@ -0,0 +1,59 @@ +/* +* Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/) +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +package org.etsi.tfs.policy.common; + +import org.etsi.tfs.policy.model.PolicyRuleState; +import org.etsi.tfs.policy.model.PolicyRuleStateEnum; + +public class ApplicationProperties { + + public static final String INVALID_MESSAGE = "%s is invalid."; + public static final String VALID_MESSAGE = "%s is valid."; + + public static final PolicyRuleState INSERTED_POLICYRULE_STATE = + new PolicyRuleState( + PolicyRuleStateEnum.POLICY_INSERTED, "Successfully entered to INSERTED state"); + public static final PolicyRuleState VALIDATED_POLICYRULE_STATE = + new PolicyRuleState( + PolicyRuleStateEnum.POLICY_VALIDATED, "Successfully transitioned to VALIDATED state"); + public static final PolicyRuleState PROVISIONED_POLICYRULE_STATE = + new PolicyRuleState( + PolicyRuleStateEnum.POLICY_PROVISIONED, + "Successfully transitioned from VALIDATED to PROVISIONED state"); + public static final PolicyRuleState ACTIVE_POLICYRULE_STATE = + new PolicyRuleState( + PolicyRuleStateEnum.POLICY_ACTIVE, + "Successfully transitioned from PROVISIONED to ACTIVE state"); + public static final PolicyRuleState ENFORCED_POLICYRULE_STATE = + new PolicyRuleState( + PolicyRuleStateEnum.POLICY_ENFORCED, + "Successfully transitioned from ACTIVE to ENFORCED state"); + public static final PolicyRuleState INEFFECTIVE_POLICYRULE_STATE = + new PolicyRuleState( + PolicyRuleStateEnum.POLICY_INEFFECTIVE, + "Transitioned from ENFORCED to INEFFECTIVE state"); + public static final PolicyRuleState EFFECTIVE_POLICYRULE_STATE = + new PolicyRuleState( + PolicyRuleStateEnum.POLICY_EFFECTIVE, + "Successfully transitioned from ENFORCED to EFFECTIVE state"); + public static final PolicyRuleState UPDATED_POLICYRULE_STATE = + new PolicyRuleState( + PolicyRuleStateEnum.POLICY_UPDATED, "Successfully entered to UPDATED state"); + public static final PolicyRuleState REMOVED_POLICYRULE_STATE = + new PolicyRuleState( + PolicyRuleStateEnum.POLICY_REMOVED, "Successfully entered to REMOVED state"); +} diff --git a/src/policy/src/main/java/org/etsi/tfs/policy/model/PolicyRuleBasic.java b/src/policy/src/main/java/org/etsi/tfs/policy/model/PolicyRuleBasic.java index ea00ea3fc2a2fc8492ef60a860df4e9baf220bfe..7df894abd91f23005e95a5cda8d5df6d196c61f0 100644 --- a/src/policy/src/main/java/org/etsi/tfs/policy/model/PolicyRuleBasic.java +++ b/src/policy/src/main/java/org/etsi/tfs/policy/model/PolicyRuleBasic.java @@ -64,7 +64,7 @@ public class PolicyRuleBasic { this.booleanOperator = BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_UNDEFINED; this.policyRuleActions = new ArrayList(); this.isValid = false; - this.exceptionMessage = e.toString(); + this.exceptionMessage = e.getMessage(); } } 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(); this.isValid = false; - this.exceptionMessage = e.toString(); + this.exceptionMessage = e.getMessage(); } } diff --git a/src/policy/src/main/java/org/etsi/tfs/policy/model/PolicyRuleService.java b/src/policy/src/main/java/org/etsi/tfs/policy/model/PolicyRuleService.java index 1f507ebc944ceab6f8018b52c8d534f5b9795930..db25dc9bfb15bc64212ac1f141b73a18eed7ff24 100644 --- a/src/policy/src/main/java/org/etsi/tfs/policy/model/PolicyRuleService.java +++ b/src/policy/src/main/java/org/etsi/tfs/policy/model/PolicyRuleService.java @@ -50,7 +50,7 @@ public class PolicyRuleService { this.serviceId = new ServiceId("", ""); this.deviceIds = new ArrayList(); 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..3c4a1577b550d56e853f6374594051c4e08838aa --- /dev/null +++ b/src/policy/src/test/java/org/etsi/tfs/policy/PolicyAddDeviceTest.java @@ -0,0 +1,202 @@ +/* +* Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/) +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +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 deviceIds = Arrays.asList("device1", "device2"); + + policyRuleDevice = new PolicyRuleDevice(policyRuleBasic, deviceIds); + } + + @Test + void deviceListMustNotBeEmpty() + throws ExecutionException, InterruptedException, TimeoutException { + CompletableFuture message = new CompletableFuture<>(); + + PolicyRuleDevice policyRuleDevice = new PolicyRuleDevice(policyRuleBasic, new ArrayList<>()); + + PolicyRuleState expectedResult = + new PolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED, "Device Ids must not be empty."); + + policyService + .addPolicyDevice(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 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 + .addPolicyDevice(policyRuleDevice) + .subscribe() + .with( + item -> { + message.complete(item); + }); + + assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage()) + .isEqualTo(expectedResult.getPolicyRuleStateMessage().toString()); + } + + @Test + void isPolicyRuleIdValid() throws ExecutionException, InterruptedException, TimeoutException { + CompletableFuture 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 + .addPolicyDevice(policyRuleDevice) + .subscribe() + .with( + item -> { + message.complete(item); + }); + + assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage()) + .isEqualTo(expectedResult.getPolicyRuleStateMessage().toString()); + } + + @Test + void successPolicyDevice() throws ExecutionException, InterruptedException, TimeoutException { + CompletableFuture 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/PolicyAddServiceTest.java b/src/policy/src/test/java/org/etsi/tfs/policy/PolicyAddServiceTest.java new file mode 100644 index 0000000000000000000000000000000000000000..773187f0748c6f2d0507e2f78b6ba42cd558e2ae --- /dev/null +++ b/src/policy/src/test/java/org/etsi/tfs/policy/PolicyAddServiceTest.java @@ -0,0 +1,251 @@ +/* +* Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/) +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +package org.etsi.tfs.policy; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.etsi.tfs.policy.common.ApplicationProperties.*; + +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.mockito.InjectMock; +import io.smallrye.mutiny.Uni; +import java.io.IOException; +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.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.monitoring.MonitoringService; +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 +public class PolicyAddServiceTest { + + @Inject PolicyServiceImpl policyService; + + @InjectMock PolicyRuleConditionValidator policyRuleConditionValidator; + + @InjectMock ContextService contextService; + + @InjectMock MonitoringService monitoringService; + + 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 deviceIds = Arrays.asList("device1", "device2"); + + policyRuleService = new PolicyRuleService(policyRuleBasic, serviceId, deviceIds); + } + + @Test + void contextOrServiceIdMustNotBeEmpty() + throws ExecutionException, InterruptedException, TimeoutException { + CompletableFuture message = new CompletableFuture<>(); + + ServiceId serviceId = new ServiceId("", ""); + List deviceIds = Arrays.asList("device1", "device2"); + + PolicyRuleService policyRuleService = + new PolicyRuleService(policyRuleBasic, serviceId, deviceIds); + + PolicyRuleState expectedResult = + new PolicyRuleState( + PolicyRuleStateEnum.POLICY_FAILED, "Context Id of Service Id must not be empty."); + + policyService + .addPolicyService(policyRuleService) + .subscribe() + .with( + item -> { + message.complete(item); + }); + + assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage()) + .isEqualTo(expectedResult.getPolicyRuleStateMessage().toString()); + } + + @Test + void serviceIdMustNotBeEmpty() throws ExecutionException, InterruptedException, TimeoutException { + CompletableFuture message = new CompletableFuture<>(); + + ServiceId serviceId = new ServiceId("sdf", ""); + List deviceIds = Arrays.asList("device1", "device2"); + + PolicyRuleService policyRuleService = + new PolicyRuleService(policyRuleBasic, serviceId, deviceIds); + + PolicyRuleState expectedResult = + new PolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED, "Service Id must not be empty."); + + policyService + .addPolicyService(policyRuleService) + .subscribe() + .with(item -> message.complete(item)); + + assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage()) + .isEqualTo(expectedResult.getPolicyRuleStateMessage().toString()); + } + + @Test + void policyRuleIdMustNotBeEmpty() + throws ExecutionException, InterruptedException, TimeoutException { + CompletableFuture 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."); + + policyService + .addPolicyService(policyRuleService) + .subscribe() + .with( + item -> { + message.complete(item); + }); + + assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage()) + .isEqualTo(expectedResult.getPolicyRuleStateMessage().toString()); + } + + @Test + void checkMessageIfServiceIsNotValid() + throws ExecutionException, InterruptedException, TimeoutException { + CompletableFuture message = new CompletableFuture<>(); + + ServiceId serviceId = new ServiceId("contextId", "serviceId"); + + PolicyRuleService policyRuleService = + new PolicyRuleService(policyRuleBasic, serviceId, new ArrayList<>()); + + PolicyRuleState expectedResult = + new PolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED, serviceId + " is invalid."); + + Mockito.when( + policyRuleConditionValidator.isServiceIdValid( + Mockito.any(ServiceId.class), Mockito.anyList())) + .thenReturn(Uni.createFrom().item(Boolean.FALSE)); + + policyService + .addPolicyService(policyRuleService) + .subscribe() + .with( + item -> { + message.complete(item); + }); + + assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage()) + .isEqualTo(expectedResult.getPolicyRuleStateMessage().toString()); + } + + @Test + void policyServiceSuccess() + throws ExecutionException, InterruptedException, TimeoutException, IOException { + CompletableFuture message = new CompletableFuture<>(); + + PolicyRuleState expectedResult = + new PolicyRuleState( + PolicyRuleStateEnum.POLICY_VALIDATED, + VALIDATED_POLICYRULE_STATE.getPolicyRuleStateMessage()); + + Mockito.when( + policyRuleConditionValidator.isServiceIdValid( + Mockito.any(ServiceId.class), Mockito.anyList())) + .thenReturn(Uni.createFrom().item(Boolean.TRUE)); + + Mockito.when(contextService.setPolicyRule(Mockito.any(PolicyRule.class))) + .thenReturn(Uni.createFrom().item("policyRuleId")); + + policyService + .addPolicyService(policyRuleService) + .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..a62c5dd3d6316b8a3a86a8f7745af08170404bd4 --- /dev/null +++ b/src/policy/src/test/java/org/etsi/tfs/policy/PolicyDeleteServiceTest.java @@ -0,0 +1,128 @@ +/* +* Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/) +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +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 deviceIds = Arrays.asList("device1", "device2"); + + policyRuleService = new PolicyRuleService(policyRuleBasic, serviceId, deviceIds); + } + + @Test + void contextOrServiceIdMustNotBeEmpty() + throws ExecutionException, InterruptedException, TimeoutException { + CompletableFuture 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/PolicyServiceTest.java b/src/policy/src/test/java/org/etsi/tfs/policy/PolicyGrpcServiceTest.java similarity index 99% rename from src/policy/src/test/java/org/etsi/tfs/policy/PolicyServiceTest.java rename to src/policy/src/test/java/org/etsi/tfs/policy/PolicyGrpcServiceTest.java index 2d1a425a8d4166779d7e18e5deaecb9ff9ec49ca..2461bcee61984656dc99ac75679dae680ab7b20b 100644 --- a/src/policy/src/test/java/org/etsi/tfs/policy/PolicyServiceTest.java +++ b/src/policy/src/test/java/org/etsi/tfs/policy/PolicyGrpcServiceTest.java @@ -46,14 +46,14 @@ import policy.PolicyCondition.PolicyRuleCondition; import policy.PolicyService; @QuarkusTest -class PolicyServiceTest { - private static final Logger LOGGER = Logger.getLogger(PolicyServiceTest.class); +class PolicyGrpcServiceTest { + private static final Logger LOGGER = Logger.getLogger(PolicyGrpcServiceTest.class); @GrpcClient PolicyService client; private final Serializer serializer; @Inject - PolicyServiceTest(Serializer serializer) { + PolicyGrpcServiceTest(Serializer serializer) { this.serializer = serializer; } 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..0cc2d5a70d3a5070eda718f906145de2b468cc6f --- /dev/null +++ b/src/policy/src/test/java/org/etsi/tfs/policy/PolicyUpdateDeviceTest.java @@ -0,0 +1,201 @@ +/* +* Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/) +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +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 deviceIds = Arrays.asList("device1", "device2"); + + policyRuleDevice = new PolicyRuleDevice(policyRuleBasic, deviceIds); + } + + @Test + void deviceListMustNotBeEmpty() + throws ExecutionException, InterruptedException, TimeoutException { + CompletableFuture 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 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 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 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/PolicyUpdateServiceTest.java b/src/policy/src/test/java/org/etsi/tfs/policy/PolicyUpdateServiceTest.java new file mode 100644 index 0000000000000000000000000000000000000000..e65f2d459557c25fe9ca1a61c97d9fd1accc1895 --- /dev/null +++ b/src/policy/src/test/java/org/etsi/tfs/policy/PolicyUpdateServiceTest.java @@ -0,0 +1,215 @@ +/* +* Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/) +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +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.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.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.monitoring.MonitoringService; +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 PolicyUpdateServiceTest { + + @Inject PolicyServiceImpl policyService; + + @InjectMock PolicyRuleConditionValidator policyRuleConditionValidator; + + @InjectMock ContextService contextService; + + @InjectMock MonitoringService monitoringService; + + 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 deviceIds = Arrays.asList("device1", "device2"); + + policyRuleService = new PolicyRuleService(policyRuleBasic, serviceId, deviceIds); + } + + @Test + void contextOrServiceIdMustNotBeEmpty() + throws ExecutionException, InterruptedException, TimeoutException { + CompletableFuture message = new CompletableFuture<>(); + + ServiceId serviceId = new ServiceId("", ""); + List deviceIds = Arrays.asList("device1", "device2"); + + PolicyRuleService policyRuleService = + new PolicyRuleService(policyRuleBasic, serviceId, deviceIds); + + PolicyRuleState expectedResult = + new PolicyRuleState( + PolicyRuleStateEnum.POLICY_FAILED, "Context Id of Service Id must not be empty."); + + policyService + .updatePolicyService(policyRuleService) + .subscribe() + .with( + item -> { + message.complete(item); + }); + + assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage()) + .isEqualTo(expectedResult.getPolicyRuleStateMessage().toString()); + } + + @Test + void serviceIdMustNotBeEmpty() throws ExecutionException, InterruptedException, TimeoutException { + CompletableFuture message = new CompletableFuture<>(); + + ServiceId serviceId = new ServiceId("sdf", ""); + List deviceIds = Arrays.asList("device1", "device2"); + + PolicyRuleService policyRuleService = + new PolicyRuleService(policyRuleBasic, serviceId, deviceIds); + + PolicyRuleState expectedResult = + new PolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED, "Service Id must not be empty."); + + policyService + .updatePolicyService(policyRuleService) + .subscribe() + .with(item -> message.complete(item)); + + assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage()) + .isEqualTo(expectedResult.getPolicyRuleStateMessage().toString()); + } + + @Test + void checkMessageIfServiceIsNotValid() + throws ExecutionException, InterruptedException, TimeoutException { + CompletableFuture message = new CompletableFuture<>(); + + ServiceId serviceId = new ServiceId("contextId", "serviceId"); + + PolicyRuleService policyRuleService = + new PolicyRuleService(policyRuleBasic, serviceId, new ArrayList<>()); + + PolicyRuleState expectedResult = + 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 + .updatePolicyService(policyRuleService) + .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 message = new CompletableFuture<>(); + + ServiceId serviceId = new ServiceId("contextId", "serviceId"); + + PolicyRuleService policyRuleService = + new PolicyRuleService(policyRuleBasic, serviceId, new ArrayList<>()); + + PolicyRuleState expectedResult = + new PolicyRuleState( + PolicyRuleStateEnum.POLICY_VALIDATED, + VALIDATED_POLICYRULE_STATE.getPolicyRuleStateMessage()); + + Mockito.when( + policyRuleConditionValidator.isPolicyRuleServiceValid( + Mockito.anyString(), Mockito.any(ServiceId.class))) + .thenReturn(Uni.createFrom().item(Boolean.TRUE)); + + policyService + .updatePolicyService(policyRuleService) + .subscribe() + .with( + item -> { + message.complete(item); + }); + + assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage()) + .isEqualTo(expectedResult.getPolicyRuleStateMessage().toString()); + } +} diff --git a/src/policy/target/kubernetes/kubernetes.yml b/src/policy/target/kubernetes/kubernetes.yml index 55847f89e7c031de854d1e54336342f7a24e320a..2737f8546570e5d483c62024018137d96fe32a4d 100644 --- a/src/policy/target/kubernetes/kubernetes.yml +++ b/src/policy/target/kubernetes/kubernetes.yml @@ -3,8 +3,8 @@ apiVersion: v1 kind: Service metadata: annotations: - app.quarkus.io/commit-id: 5f8866be9cb91871607627819258b0b375410467 - app.quarkus.io/build-timestamp: 2024-01-26 - 16:40:15 +0000 + app.quarkus.io/commit-id: 47e6691312515be37e2d9ffa85a1ee165a66c9db + app.quarkus.io/build-timestamp: 2024-02-09 - 14:52:23 +0000 prometheus.io/scrape: "true" prometheus.io/path: /q/metrics prometheus.io/port: "8080" @@ -29,8 +29,8 @@ apiVersion: apps/v1 kind: Deployment metadata: annotations: - app.quarkus.io/commit-id: 5f8866be9cb91871607627819258b0b375410467 - app.quarkus.io/build-timestamp: 2024-01-26 - 16:40:15 +0000 + app.quarkus.io/commit-id: 47e6691312515be37e2d9ffa85a1ee165a66c9db + app.quarkus.io/build-timestamp: 2024-02-09 - 14:52:23 +0000 prometheus.io/scrape: "true" prometheus.io/path: /q/metrics prometheus.io/port: "8080" @@ -47,8 +47,8 @@ spec: template: metadata: annotations: - app.quarkus.io/commit-id: 5f8866be9cb91871607627819258b0b375410467 - app.quarkus.io/build-timestamp: 2024-01-26 - 16:40:15 +0000 + app.quarkus.io/commit-id: 47e6691312515be37e2d9ffa85a1ee165a66c9db + app.quarkus.io/build-timestamp: 2024-02-09 - 14:52:23 +0000 prometheus.io/scrape: "true" prometheus.io/path: /q/metrics prometheus.io/port: "8080" @@ -63,12 +63,12 @@ spec: valueFrom: fieldRef: fieldPath: metadata.namespace + - name: CONTEXT_SERVICE_HOST + value: contextservice - name: SERVICE_SERVICE_HOST value: serviceservice - name: MONITORING_SERVICE_HOST value: monitoringservice - - name: CONTEXT_SERVICE_HOST - value: contextservice image: labs.etsi.org:5050/tfs/controller/policy:0.1.0 imagePullPolicy: Always livenessProbe: