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<PolicyRuleAction>(); 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<String>(); this.isValid = false; - this.exceptionMessage = e.toString(); + this.exceptionMessage = e.getMessage(); } } diff --git a/src/policy/src/test/java/org/etsi/tfs/policy/PolicyGrpcServiceTest.java b/src/policy/src/test/java/org/etsi/tfs/policy/PolicyGrpcServiceTest.java new file mode 100644 index 0000000000000000000000000000000000000000..2461bcee61984656dc99ac75679dae680ab7b20b --- /dev/null +++ b/src/policy/src/test/java/org/etsi/tfs/policy/PolicyGrpcServiceTest.java @@ -0,0 +1,382 @@ +/* +* 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 context.ContextOuterClass; +import context.ContextOuterClass.Uuid; +import io.quarkus.grpc.GrpcClient; +import io.quarkus.test.junit.QuarkusTest; +import java.util.List; +import java.util.UUID; +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 monitoring.Monitoring.KpiId; +import org.etsi.tfs.policy.monitoring.model.FloatKpiValue; +import org.etsi.tfs.policy.monitoring.model.IntegerKpiValue; +import org.jboss.logging.Logger; +import org.junit.jupiter.api.Test; +import policy.Policy; +import policy.Policy.PolicyRuleBasic; +import policy.Policy.PolicyRuleStateEnum; +import policy.PolicyAction; +import policy.PolicyAction.PolicyRuleActionEnum; +import policy.PolicyCondition; +import policy.PolicyCondition.BooleanOperator; +import policy.PolicyCondition.NumericalOperator; +import policy.PolicyCondition.PolicyRuleCondition; +import policy.PolicyService; + +@QuarkusTest +class PolicyGrpcServiceTest { + private static final Logger LOGGER = Logger.getLogger(PolicyGrpcServiceTest.class); + + @GrpcClient PolicyService client; + private final Serializer serializer; + + @Inject + PolicyGrpcServiceTest(Serializer serializer) { + this.serializer = serializer; + } + + private context.ContextOuterClass.ServiceId createContextServiceId() { + final var contextIdUuid = serializer.serializeUuid("571eabc1-0f59-48da-b608-c45876c3fa8a"); + + final var serviceIdUuid = serializer.serializeUuid("123456789"); + + context.ContextOuterClass.ContextId contextId = + context.ContextOuterClass.ContextId.newBuilder().setContextUuid(contextIdUuid).build(); + + return context.ContextOuterClass.ServiceId.newBuilder() + .setContextId(contextId) + .setServiceUuid(serviceIdUuid) + .build(); + } + + private PolicyRuleBasic createPolicyRuleBasic() { + final var expectedPolicyRuleIdUuid = + serializer.serializeUuid("571eabc1-0f59-48da-b608-c45876c3fa8a"); + + final var expectedPolicyRuleId = + Policy.PolicyRuleId.newBuilder().setUuid(expectedPolicyRuleIdUuid).build(); + + final var expectedPolicyRuleState = + Policy.PolicyRuleState.newBuilder() + .setPolicyRuleState(PolicyRuleStateEnum.POLICY_INSERTED) + .build(); + + final var expectedFirstKpiValue = new IntegerKpiValue(22); + final var expectedSecondKpiValue = new FloatKpiValue(69.1f); + + final var serializedExpectedFirstKpiValue = serializer.serialize(expectedFirstKpiValue); + final var serializedExpectedSecondKpiValue = serializer.serialize(expectedSecondKpiValue); + + final var firstExpectedPolicyRuleCondition = + PolicyRuleCondition.newBuilder() + .setKpiId( + KpiId.newBuilder() + .setKpiId( + Uuid.newBuilder().setUuid("79e49ba3-a7b4-4b4b-8aaa-28b05c6f888e").build())) + .setNumericalOperator(NumericalOperator.POLICYRULE_CONDITION_NUMERICAL_EQUAL) + .setKpiValue(serializedExpectedFirstKpiValue) + .build(); + + final var secondExpectedPolicyRuleCondition = + PolicyCondition.PolicyRuleCondition.newBuilder() + .setKpiId( + KpiId.newBuilder() + .setKpiId( + Uuid.newBuilder().setUuid("eae900e5-2703-467d-82f2-97aae8b55c15").build())) + .setNumericalOperator(NumericalOperator.POLICYRULE_CONDITION_NUMERICAL_GREATER_THAN) + .setKpiValue(serializedExpectedSecondKpiValue) + .build(); + + final var expectedPolicyRuleConditions = + List.of(firstExpectedPolicyRuleCondition, secondExpectedPolicyRuleCondition); + + org.etsi.tfs.policy.model.PolicyRuleActionConfig policyRuleActionConfig_1 = + new org.etsi.tfs.policy.model.PolicyRuleActionConfig("paramater1", "parameter2"); + final var serializedPolicyRuleActionConfigList_1 = + serializer.serialize(policyRuleActionConfig_1); + + org.etsi.tfs.policy.model.PolicyRuleActionConfig policyRuleActionConfig_2 = + new org.etsi.tfs.policy.model.PolicyRuleActionConfig("paramater3", "parameter4"); + final var serializedPolicyRuleActionConfigList_2 = + serializer.serialize(policyRuleActionConfig_2); + + final var firstExpectedPolicyRuleAction = + PolicyAction.PolicyRuleAction.newBuilder() + .setAction(PolicyAction.PolicyRuleActionEnum.POLICYRULE_ACTION_ADD_SERVICE_CONFIGRULE) + .addActionConfig(serializedPolicyRuleActionConfigList_1) + .build(); + + final var secondExpectedPolicyRuleAction = + PolicyAction.PolicyRuleAction.newBuilder() + .setAction(PolicyRuleActionEnum.POLICYRULE_ACTION_ADD_SERVICE_CONSTRAINT) + .addActionConfig(serializedPolicyRuleActionConfigList_2) + .build(); + + final var expectedPolicyRuleActions = + List.of(firstExpectedPolicyRuleAction, secondExpectedPolicyRuleAction); + + return PolicyRuleBasic.newBuilder() + .setPolicyRuleId(expectedPolicyRuleId) + .setPolicyRuleState(expectedPolicyRuleState) + .addAllConditionList(expectedPolicyRuleConditions) + .setBooleanOperator(BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_OR) + .addAllActionList(expectedPolicyRuleActions) + .build(); + } + + // @Test + // void shouldAddPolicyService() throws ExecutionException, InterruptedException, TimeoutException + // { + // CompletableFuture<String> message = new CompletableFuture<>(); + + // final var policyRuleBasic = createPolicyRuleBasic(); + + // final var expectedPolicyRuleState = policyRuleBasic.getPolicyRuleState(); + + // final var serviceId = createContextServiceId(); + + // final var expectedDeviceIdUuid1 = + // serializer.serializeUuid("20db867c-772d-4872-9179-244ecafb3257"); + + // final var expectedDeviceId1 = + // + // ContextOuterClass.DeviceId.newBuilder().setDeviceUuid(expectedDeviceIdUuid1).build(); + + // final var deviceIds = List.of(expectedDeviceId1); + // final var policyRuleService = + // Policy.PolicyRuleService.newBuilder() + // .setPolicyRuleBasic(policyRuleBasic) + // .setServiceId(serviceId) + // .addAllDeviceList(deviceIds) + // .build(); + + // client + // .policyAddService(policyRuleService) + // .subscribe() + // .with(policyRuleState -> + // message.complete(policyRuleState.getPolicyRuleState().toString())); + + // assertThat(message.get(5, TimeUnit.SECONDS)) + // .isEqualTo(expectedPolicyRuleState.getPolicyRuleState().toString()); + // } + + // @Test + // void shouldAddPolicyDevice() throws ExecutionException, InterruptedException, TimeoutException + // { + // CompletableFuture<String> message = new CompletableFuture<>(); + + // final var expectedDeviceIdUuid1 = + // serializer.serializeUuid("20db867c-772d-4872-9179-244ecafb3257"); + // final var expectedDeviceIdUuid2 = + // serializer.serializeUuid("095974ac-d757-412d-b317-bcf355220aa9"); + + // final var expectedDeviceId1 = + // + // ContextOuterClass.DeviceId.newBuilder().setDeviceUuid(expectedDeviceIdUuid1).build(); + // final var expectedDeviceId2 = + // + // ContextOuterClass.DeviceId.newBuilder().setDeviceUuid(expectedDeviceIdUuid2).build(); + + // final var policyRuleBasic = createPolicyRuleBasic(); + // final var deviceIds = List.of(expectedDeviceId1, expectedDeviceId2); + + // final var expectedPolicyRuleState = policyRuleBasic.getPolicyRuleState(); + + // final var policyRuleDevice = + // Policy.PolicyRuleDevice.newBuilder() + // .setPolicyRuleBasic(policyRuleBasic) + // .addAllDeviceList(deviceIds) + // .build(); + + // client + // .policyAddDevice(policyRuleDevice) + // .subscribe() + // .with(policyRuleState -> + // message.complete(policyRuleState.getPolicyRuleState().toString())); + + // assertThat(message.get(5, TimeUnit.SECONDS)) + // .isEqualTo(expectedPolicyRuleState.getPolicyRuleState().toString()); + // } + + @Test + void shouldUpdatePolicyServiceReturnFailedState() + throws ExecutionException, InterruptedException, TimeoutException { + CompletableFuture<String> message = new CompletableFuture<>(); + + final var expectedPolicyRuleState = + Policy.PolicyRuleState.newBuilder() + .setPolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED) + .build(); + + final var policyRuleBasic = + PolicyRuleBasic.newBuilder().setPolicyRuleState(expectedPolicyRuleState).build(); + final var policyRuleService = + Policy.PolicyRuleService.newBuilder().setPolicyRuleBasic(policyRuleBasic).build(); + + client + .policyUpdateService(policyRuleService) + .subscribe() + .with(policyRuleState -> message.complete(policyRuleState.getPolicyRuleState().toString())); + + assertThat(message.get(5, TimeUnit.SECONDS)) + .isEqualTo(expectedPolicyRuleState.getPolicyRuleState().toString()); + } + + @Test + void shouldUpdatePolicyDeviceReturnFailedState() + throws ExecutionException, InterruptedException, TimeoutException { + CompletableFuture<String> message = new CompletableFuture<>(); + + final var expectedDeviceIdUuid = + serializer.serializeUuid("20db867c-772d-4872-9179-244ecafb3257"); + + final var expectedDeviceId = + ContextOuterClass.DeviceId.newBuilder().setDeviceUuid(expectedDeviceIdUuid).build(); + + final var expectedPolicyRuleState = + Policy.PolicyRuleState.newBuilder() + .setPolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED) + .build(); + + final var policyRuleBasic = + PolicyRuleBasic.newBuilder().setPolicyRuleState(expectedPolicyRuleState).build(); + final var deviceIds = List.of(expectedDeviceId); + final var policyRuleDevice = + Policy.PolicyRuleDevice.newBuilder() + .setPolicyRuleBasic(policyRuleBasic) + .addAllDeviceList(deviceIds) + .build(); + + client + .policyUpdateDevice(policyRuleDevice) + .subscribe() + .with(policyRuleState -> message.complete(policyRuleState.getPolicyRuleState().toString())); + + assertThat(message.get(5, TimeUnit.SECONDS)) + .isEqualTo(expectedPolicyRuleState.getPolicyRuleState().toString()); + } + + // TODO: Disable shouldDeletePolicy test until mock context service + // @Test + // void shouldDeletePolicy() throws ExecutionException, InterruptedException, TimeoutException + // { + // CompletableFuture<String> message = new CompletableFuture<>(); + + // final var uuid = + // ContextOuterClass.Uuid.newBuilder() + // + // .setUuid(UUID.fromString("0f14d0ab-9608-7862-a9e4-5ed26688389b").toString()) + // .build(); + // final var policyRuleId = Policy.PolicyRuleId.newBuilder().setUuid(uuid).build(); + + // final var expectedPolicyRuleState = + // Policy.PolicyRuleState.newBuilder() + // .setPolicyRuleState(PolicyRuleStateEnum.POLICY_REMOVED) + // .build(); + + // client + // .policyDelete(policyRuleId) + // .subscribe() + // .with(policyRuleState -> + // message.complete(policyRuleState.getPolicyRuleState().toString())); + + // assertThat(message.get(5, TimeUnit.SECONDS)) + // .isEqualTo(expectedPolicyRuleState.getPolicyRuleState().toString()); + // } + + @Test + void shouldGetPolicyService() throws ExecutionException, InterruptedException, TimeoutException { + CompletableFuture<String> message = new CompletableFuture<>(); + + final var uuid = + ContextOuterClass.Uuid.newBuilder() + .setUuid(UUID.fromString("0f14d0ab-9608-7862-a9e4-5ed26688389b").toString()) + .build(); + final var policyRuleId = Policy.PolicyRuleId.newBuilder().setUuid(uuid).build(); + + client + .getPolicyService(policyRuleId) + .subscribe() + .with( + policyRuleService -> { + LOGGER.infof( + "Getting policy with ID: %s", + policyRuleService.getPolicyRuleBasic().getPolicyRuleId().getUuid()); + message.complete( + policyRuleService.getPolicyRuleBasic().getPolicyRuleId().getUuid().getUuid()); + }); + + assertThat(message.get(5, TimeUnit.SECONDS)).isEqualTo(policyRuleId.getUuid().getUuid()); + } + + @Test + void shouldGetPolicyDevice() throws ExecutionException, InterruptedException, TimeoutException { + CompletableFuture<String> message = new CompletableFuture<>(); + + final var uuid = + ContextOuterClass.Uuid.newBuilder() + .setUuid(UUID.fromString("0f14d0ab-9608-7862-a9e4-5ed26688389b").toString()) + .build(); + final var policyRuleId = Policy.PolicyRuleId.newBuilder().setUuid(uuid).build(); + + client + .getPolicyDevice(policyRuleId) + .subscribe() + .with( + policyRuleService -> { + LOGGER.infof( + "Getting policy with ID: %s", + policyRuleService.getPolicyRuleBasic().getPolicyRuleId().getUuid()); + message.complete( + policyRuleService.getPolicyRuleBasic().getPolicyRuleId().getUuid().getUuid()); + }); + + assertThat(message.get(5, TimeUnit.SECONDS)).isEqualTo(policyRuleId.getUuid().getUuid()); + } + + @Test + void shouldGetPolicyByServiceId() + throws ExecutionException, InterruptedException, TimeoutException { + + CompletableFuture<String> message = new CompletableFuture<>(); + + final var uuid = + ContextOuterClass.Uuid.newBuilder() + .setUuid(UUID.fromString("0f14d0ab-9608-7862-a9e4-5ed26688389b").toString()) + .build(); + final var serviceId = ContextOuterClass.ServiceId.newBuilder().setServiceUuid(uuid).build(); + + client + .getPolicyByServiceId(serviceId) + .subscribe() + .with( + policyRuleList -> { + LOGGER.infof("Getting policyRuleList with ID: %s", policyRuleList); + message.complete(policyRuleList.toString()); + }); + + assertThat(message.get(5, TimeUnit.SECONDS)).isEmpty(); + } +} diff --git a/src/policy/src/test/java/org/etsi/tfs/policy/PolicyServiceTest.java b/src/policy/src/test/java/org/etsi/tfs/policy/PolicyServiceTest.java index 2d1a425a8d4166779d7e18e5deaecb9ff9ec49ca..b09cdeea0170a26842f258cfe95c31d57413d61c 100644 --- a/src/policy/src/test/java/org/etsi/tfs/policy/PolicyServiceTest.java +++ b/src/policy/src/test/java/org/etsi/tfs/policy/PolicyServiceTest.java @@ -1,382 +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 context.ContextOuterClass; -import context.ContextOuterClass.Uuid; -import io.quarkus.grpc.GrpcClient; 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.UUID; 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 monitoring.Monitoring.KpiId; -import org.etsi.tfs.policy.monitoring.model.FloatKpiValue; +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.jboss.logging.Logger; +import org.etsi.tfs.policy.monitoring.model.KpiValue; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import policy.Policy; -import policy.Policy.PolicyRuleBasic; -import policy.Policy.PolicyRuleStateEnum; -import policy.PolicyAction; -import policy.PolicyAction.PolicyRuleActionEnum; -import policy.PolicyCondition; -import policy.PolicyCondition.BooleanOperator; -import policy.PolicyCondition.NumericalOperator; -import policy.PolicyCondition.PolicyRuleCondition; -import policy.PolicyService; +import org.mockito.Mockito; @QuarkusTest -class PolicyServiceTest { - private static final Logger LOGGER = Logger.getLogger(PolicyServiceTest.class); - - @GrpcClient PolicyService client; - private final Serializer serializer; - - @Inject - PolicyServiceTest(Serializer serializer) { - this.serializer = serializer; - } +public class PolicyServiceTest { - private context.ContextOuterClass.ServiceId createContextServiceId() { - final var contextIdUuid = serializer.serializeUuid("571eabc1-0f59-48da-b608-c45876c3fa8a"); + @Inject PolicyServiceImpl policyService; - final var serviceIdUuid = serializer.serializeUuid("123456789"); + @InjectMock PolicyRuleConditionValidator policyRuleConditionValidator; - context.ContextOuterClass.ContextId contextId = - context.ContextOuterClass.ContextId.newBuilder().setContextUuid(contextIdUuid).build(); + @InjectMock ContextService contextService; - return context.ContextOuterClass.ServiceId.newBuilder() - .setContextId(contextId) - .setServiceUuid(serviceIdUuid) - .build(); - } - - private PolicyRuleBasic createPolicyRuleBasic() { - final var expectedPolicyRuleIdUuid = - serializer.serializeUuid("571eabc1-0f59-48da-b608-c45876c3fa8a"); - - final var expectedPolicyRuleId = - Policy.PolicyRuleId.newBuilder().setUuid(expectedPolicyRuleIdUuid).build(); - - final var expectedPolicyRuleState = - Policy.PolicyRuleState.newBuilder() - .setPolicyRuleState(PolicyRuleStateEnum.POLICY_INSERTED) - .build(); - - final var expectedFirstKpiValue = new IntegerKpiValue(22); - final var expectedSecondKpiValue = new FloatKpiValue(69.1f); - - final var serializedExpectedFirstKpiValue = serializer.serialize(expectedFirstKpiValue); - final var serializedExpectedSecondKpiValue = serializer.serialize(expectedSecondKpiValue); - - final var firstExpectedPolicyRuleCondition = - PolicyRuleCondition.newBuilder() - .setKpiId( - KpiId.newBuilder() - .setKpiId( - Uuid.newBuilder().setUuid("79e49ba3-a7b4-4b4b-8aaa-28b05c6f888e").build())) - .setNumericalOperator(NumericalOperator.POLICYRULE_CONDITION_NUMERICAL_EQUAL) - .setKpiValue(serializedExpectedFirstKpiValue) - .build(); - - final var secondExpectedPolicyRuleCondition = - PolicyCondition.PolicyRuleCondition.newBuilder() - .setKpiId( - KpiId.newBuilder() - .setKpiId( - Uuid.newBuilder().setUuid("eae900e5-2703-467d-82f2-97aae8b55c15").build())) - .setNumericalOperator(NumericalOperator.POLICYRULE_CONDITION_NUMERICAL_GREATER_THAN) - .setKpiValue(serializedExpectedSecondKpiValue) - .build(); - - final var expectedPolicyRuleConditions = - List.of(firstExpectedPolicyRuleCondition, secondExpectedPolicyRuleCondition); - - org.etsi.tfs.policy.model.PolicyRuleActionConfig policyRuleActionConfig_1 = - new org.etsi.tfs.policy.model.PolicyRuleActionConfig("paramater1", "parameter2"); - final var serializedPolicyRuleActionConfigList_1 = - serializer.serialize(policyRuleActionConfig_1); - - org.etsi.tfs.policy.model.PolicyRuleActionConfig policyRuleActionConfig_2 = - new org.etsi.tfs.policy.model.PolicyRuleActionConfig("paramater3", "parameter4"); - final var serializedPolicyRuleActionConfigList_2 = - serializer.serialize(policyRuleActionConfig_2); - - final var firstExpectedPolicyRuleAction = - PolicyAction.PolicyRuleAction.newBuilder() - .setAction(PolicyAction.PolicyRuleActionEnum.POLICYRULE_ACTION_ADD_SERVICE_CONFIGRULE) - .addActionConfig(serializedPolicyRuleActionConfigList_1) - .build(); - - final var secondExpectedPolicyRuleAction = - PolicyAction.PolicyRuleAction.newBuilder() - .setAction(PolicyRuleActionEnum.POLICYRULE_ACTION_ADD_SERVICE_CONSTRAINT) - .addActionConfig(serializedPolicyRuleActionConfigList_2) - .build(); - - final var expectedPolicyRuleActions = - List.of(firstExpectedPolicyRuleAction, secondExpectedPolicyRuleAction); - - return PolicyRuleBasic.newBuilder() - .setPolicyRuleId(expectedPolicyRuleId) - .setPolicyRuleState(expectedPolicyRuleState) - .addAllConditionList(expectedPolicyRuleConditions) - .setBooleanOperator(BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_OR) - .addAllActionList(expectedPolicyRuleActions) - .build(); - } + @InjectMock MonitoringService monitoringService; - // @Test - // void shouldAddPolicyService() throws ExecutionException, InterruptedException, TimeoutException - // { - // CompletableFuture<String> message = new CompletableFuture<>(); + static PolicyRuleBasic policyRuleBasic; + static PolicyRuleService policyRuleService; - // final var policyRuleBasic = createPolicyRuleBasic(); + @BeforeAll + static void init() { - // final var expectedPolicyRuleState = policyRuleBasic.getPolicyRuleState(); + String policyId = "policyRuleId"; + KpiValue kpiValue = new IntegerKpiValue(100); - // final var serviceId = createContextServiceId(); + PolicyRuleCondition policyRuleCondition = + new PolicyRuleCondition( + "kpiId", NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_GREATER_THAN, kpiValue); - // final var expectedDeviceIdUuid1 = - // serializer.serializeUuid("20db867c-772d-4872-9179-244ecafb3257"); + PolicyRuleActionConfig policyRuleActionConfig = new PolicyRuleActionConfig("key", "value"); - // final var expectedDeviceId1 = - // - // ContextOuterClass.DeviceId.newBuilder().setDeviceUuid(expectedDeviceIdUuid1).build(); + PolicyRuleAction policyRuleAction = + new PolicyRuleAction( + PolicyRuleActionEnum.POLICY_RULE_ACTION_NO_ACTION, + Arrays.asList(policyRuleActionConfig)); - // final var deviceIds = List.of(expectedDeviceId1); - // final var policyRuleService = - // Policy.PolicyRuleService.newBuilder() - // .setPolicyRuleBasic(policyRuleBasic) - // .setServiceId(serviceId) - // .addAllDeviceList(deviceIds) - // .build(); + 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)); - // client - // .policyAddService(policyRuleService) - // .subscribe() - // .with(policyRuleState -> - // message.complete(policyRuleState.getPolicyRuleState().toString())); + ServiceId serviceId = new ServiceId("contextId", "serviceId"); - // assertThat(message.get(5, TimeUnit.SECONDS)) - // .isEqualTo(expectedPolicyRuleState.getPolicyRuleState().toString()); - // } + Service service = new Service(serviceId, ServiceTypeEnum.UNKNOWN, null, null, null, null, 0.0); - // @Test - // void shouldAddPolicyDevice() throws ExecutionException, InterruptedException, TimeoutException - // { - // CompletableFuture<String> message = new CompletableFuture<>(); + List<String> deviceIds = Arrays.asList("device1", "device2"); - // final var expectedDeviceIdUuid1 = - // serializer.serializeUuid("20db867c-772d-4872-9179-244ecafb3257"); - // final var expectedDeviceIdUuid2 = - // serializer.serializeUuid("095974ac-d757-412d-b317-bcf355220aa9"); + policyRuleService = new PolicyRuleService(policyRuleBasic, serviceId, deviceIds); + } - // final var expectedDeviceId1 = - // - // ContextOuterClass.DeviceId.newBuilder().setDeviceUuid(expectedDeviceIdUuid1).build(); - // final var expectedDeviceId2 = - // - // ContextOuterClass.DeviceId.newBuilder().setDeviceUuid(expectedDeviceIdUuid2).build(); + @Test + void contextOrServiceIdMustNotBeEmpty() + throws ExecutionException, InterruptedException, TimeoutException { + CompletableFuture<PolicyRuleState> message = new CompletableFuture<>(); - // final var policyRuleBasic = createPolicyRuleBasic(); - // final var deviceIds = List.of(expectedDeviceId1, expectedDeviceId2); + ServiceId serviceId = new ServiceId("", ""); + List<String> deviceIds = Arrays.asList("device1", "device2"); - // final var expectedPolicyRuleState = policyRuleBasic.getPolicyRuleState(); + PolicyRuleService policyRuleService = + new PolicyRuleService(policyRuleBasic, serviceId, deviceIds); - // final var policyRuleDevice = - // Policy.PolicyRuleDevice.newBuilder() - // .setPolicyRuleBasic(policyRuleBasic) - // .addAllDeviceList(deviceIds) - // .build(); + PolicyRuleState expectedResult = + new PolicyRuleState( + PolicyRuleStateEnum.POLICY_FAILED, "Context Id of Service Id must not be empty."); - // client - // .policyAddDevice(policyRuleDevice) - // .subscribe() - // .with(policyRuleState -> - // message.complete(policyRuleState.getPolicyRuleState().toString())); + policyService + .addPolicyService(policyRuleService) + .subscribe() + .with( + item -> { + message.complete(item); + }); - // assertThat(message.get(5, TimeUnit.SECONDS)) - // .isEqualTo(expectedPolicyRuleState.getPolicyRuleState().toString()); - // } + assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage()) + .isEqualTo(expectedResult.getPolicyRuleStateMessage().toString()); + } @Test - void shouldUpdatePolicyServiceReturnFailedState() - throws ExecutionException, InterruptedException, TimeoutException { - CompletableFuture<String> message = new CompletableFuture<>(); + void serviceIdMustNotBeEmpty() throws ExecutionException, InterruptedException, TimeoutException { + CompletableFuture<PolicyRuleState> message = new CompletableFuture<>(); + + ServiceId serviceId = new ServiceId("sdf", ""); + List<String> deviceIds = Arrays.asList("device1", "device2"); - final var expectedPolicyRuleState = - Policy.PolicyRuleState.newBuilder() - .setPolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED) - .build(); + PolicyRuleService policyRuleService = + new PolicyRuleService(policyRuleBasic, serviceId, deviceIds); - final var policyRuleBasic = - PolicyRuleBasic.newBuilder().setPolicyRuleState(expectedPolicyRuleState).build(); - final var policyRuleService = - Policy.PolicyRuleService.newBuilder().setPolicyRuleBasic(policyRuleBasic).build(); + PolicyRuleState expectedResult = + new PolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED, "Service Id must not be empty."); - client - .policyUpdateService(policyRuleService) + policyService + .addPolicyService(policyRuleService) .subscribe() - .with(policyRuleState -> message.complete(policyRuleState.getPolicyRuleState().toString())); + .with(item -> message.complete(item)); - assertThat(message.get(5, TimeUnit.SECONDS)) - .isEqualTo(expectedPolicyRuleState.getPolicyRuleState().toString()); + assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage()) + .isEqualTo(expectedResult.getPolicyRuleStateMessage().toString()); } @Test - void shouldUpdatePolicyDeviceReturnFailedState() + void policyRuleIdMustNotBeEmpty() throws ExecutionException, InterruptedException, TimeoutException { - CompletableFuture<String> message = new CompletableFuture<>(); - - final var expectedDeviceIdUuid = - serializer.serializeUuid("20db867c-772d-4872-9179-244ecafb3257"); - - final var expectedDeviceId = - ContextOuterClass.DeviceId.newBuilder().setDeviceUuid(expectedDeviceIdUuid).build(); - - final var expectedPolicyRuleState = - Policy.PolicyRuleState.newBuilder() - .setPolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED) - .build(); - - final var policyRuleBasic = - PolicyRuleBasic.newBuilder().setPolicyRuleState(expectedPolicyRuleState).build(); - final var deviceIds = List.of(expectedDeviceId); - final var policyRuleDevice = - Policy.PolicyRuleDevice.newBuilder() - .setPolicyRuleBasic(policyRuleBasic) - .addAllDeviceList(deviceIds) - .build(); - - client - .policyUpdateDevice(policyRuleDevice) - .subscribe() - .with(policyRuleState -> message.complete(policyRuleState.getPolicyRuleState().toString())); + CompletableFuture<PolicyRuleState> message = new CompletableFuture<>(); - assertThat(message.get(5, TimeUnit.SECONDS)) - .isEqualTo(expectedPolicyRuleState.getPolicyRuleState().toString()); - } - - // TODO: Disable shouldDeletePolicy test until mock context service - // @Test - // void shouldDeletePolicy() throws ExecutionException, InterruptedException, TimeoutException - // { - // CompletableFuture<String> message = new CompletableFuture<>(); - - // final var uuid = - // ContextOuterClass.Uuid.newBuilder() - // - // .setUuid(UUID.fromString("0f14d0ab-9608-7862-a9e4-5ed26688389b").toString()) - // .build(); - // final var policyRuleId = Policy.PolicyRuleId.newBuilder().setUuid(uuid).build(); - - // final var expectedPolicyRuleState = - // Policy.PolicyRuleState.newBuilder() - // .setPolicyRuleState(PolicyRuleStateEnum.POLICY_REMOVED) - // .build(); - - // client - // .policyDelete(policyRuleId) - // .subscribe() - // .with(policyRuleState -> - // message.complete(policyRuleState.getPolicyRuleState().toString())); - - // assertThat(message.get(5, TimeUnit.SECONDS)) - // .isEqualTo(expectedPolicyRuleState.getPolicyRuleState().toString()); - // } - - @Test - void shouldGetPolicyService() throws ExecutionException, InterruptedException, TimeoutException { - CompletableFuture<String> message = new CompletableFuture<>(); + String policyId = ""; - final var uuid = - ContextOuterClass.Uuid.newBuilder() - .setUuid(UUID.fromString("0f14d0ab-9608-7862-a9e4-5ed26688389b").toString()) - .build(); - final var policyRuleId = Policy.PolicyRuleId.newBuilder().setUuid(uuid).build(); + PolicyRuleBasic policyRuleBasic = + new PolicyRuleBasic( + policyId, + new PolicyRuleState(PolicyRuleStateEnum.POLICY_INSERTED, "Failed due to some errors"), + 1, + new ArrayList<>(), + null, + new ArrayList<>()); - client - .getPolicyService(policyRuleId) - .subscribe() - .with( - policyRuleService -> { - LOGGER.infof( - "Getting policy with ID: %s", - policyRuleService.getPolicyRuleBasic().getPolicyRuleId().getUuid()); - message.complete( - policyRuleService.getPolicyRuleBasic().getPolicyRuleId().getUuid().getUuid()); - }); + ServiceId serviceId = new ServiceId("contextId", "serviceId"); - assertThat(message.get(5, TimeUnit.SECONDS)).isEqualTo(policyRuleId.getUuid().getUuid()); - } + Service service = new Service(serviceId, ServiceTypeEnum.UNKNOWN, null, null, null, null, 0.0); - @Test - void shouldGetPolicyDevice() throws ExecutionException, InterruptedException, TimeoutException { - CompletableFuture<String> message = new CompletableFuture<>(); + PolicyRuleService policyRuleService = + new PolicyRuleService(policyRuleBasic, serviceId, new ArrayList<>()); - final var uuid = - ContextOuterClass.Uuid.newBuilder() - .setUuid(UUID.fromString("0f14d0ab-9608-7862-a9e4-5ed26688389b").toString()) - .build(); - final var policyRuleId = Policy.PolicyRuleId.newBuilder().setUuid(uuid).build(); + PolicyRuleState expectedResult = + new PolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED, "Policy rule ID must not be empty."); - client - .getPolicyDevice(policyRuleId) + policyService + .addPolicyService(policyRuleService) .subscribe() .with( - policyRuleService -> { - LOGGER.infof( - "Getting policy with ID: %s", - policyRuleService.getPolicyRuleBasic().getPolicyRuleId().getUuid()); - message.complete( - policyRuleService.getPolicyRuleBasic().getPolicyRuleId().getUuid().getUuid()); + item -> { + message.complete(item); }); - assertThat(message.get(5, TimeUnit.SECONDS)).isEqualTo(policyRuleId.getUuid().getUuid()); + assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage()) + .isEqualTo(expectedResult.getPolicyRuleStateMessage().toString()); } @Test - void shouldGetPolicyByServiceId() - throws ExecutionException, InterruptedException, TimeoutException { + void policyServiceSuccess() throws ExecutionException, InterruptedException, TimeoutException { + CompletableFuture<PolicyRuleState> message = new CompletableFuture<>(); + + PolicyRuleState expectedResult = + new PolicyRuleState( + PolicyRuleStateEnum.POLICY_VALIDATED, "Successfully transitioned to VALIDATED state"); - CompletableFuture<String> message = new CompletableFuture<>(); + Mockito.when( + policyRuleConditionValidator.isServiceIdValid( + Mockito.any(ServiceId.class), Mockito.anyList())) + .thenReturn(Uni.createFrom().item(Boolean.TRUE)); - final var uuid = - ContextOuterClass.Uuid.newBuilder() - .setUuid(UUID.fromString("0f14d0ab-9608-7862-a9e4-5ed26688389b").toString()) - .build(); - final var serviceId = ContextOuterClass.ServiceId.newBuilder().setServiceUuid(uuid).build(); + Mockito.when(contextService.setPolicyRule(Mockito.any(PolicyRule.class))) + .thenReturn(Uni.createFrom().item("policyRuleId")); - client - .getPolicyByServiceId(serviceId) + policyService + .addPolicyService(policyRuleService) .subscribe() .with( - policyRuleList -> { - LOGGER.infof("Getting policyRuleList with ID: %s", policyRuleList); - message.complete(policyRuleList.toString()); + item -> { + message.complete(item); }); - assertThat(message.get(5, TimeUnit.SECONDS)).isEmpty(); + 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 deleted file mode 100644 index 55847f89e7c031de854d1e54336342f7a24e320a..0000000000000000000000000000000000000000 --- a/src/policy/target/kubernetes/kubernetes.yml +++ /dev/null @@ -1,108 +0,0 @@ ---- -apiVersion: v1 -kind: Service -metadata: - annotations: - app.quarkus.io/commit-id: 5f8866be9cb91871607627819258b0b375410467 - app.quarkus.io/build-timestamp: 2024-01-26 - 16:40:15 +0000 - prometheus.io/scrape: "true" - prometheus.io/path: /q/metrics - prometheus.io/port: "8080" - prometheus.io/scheme: http - labels: - app.kubernetes.io/name: policyservice - app: policyservice - name: policyservice -spec: - ports: - - name: http - port: 9192 - targetPort: 8080 - - name: grpc-server - port: 6060 - targetPort: 6060 - selector: - app.kubernetes.io/name: policyservice - type: ClusterIP ---- -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 - prometheus.io/scrape: "true" - prometheus.io/path: /q/metrics - prometheus.io/port: "8080" - prometheus.io/scheme: http - labels: - app: policyservice - app.kubernetes.io/name: policyservice - name: policyservice -spec: - replicas: 1 - selector: - matchLabels: - app.kubernetes.io/name: policyservice - template: - metadata: - annotations: - app.quarkus.io/commit-id: 5f8866be9cb91871607627819258b0b375410467 - app.quarkus.io/build-timestamp: 2024-01-26 - 16:40:15 +0000 - prometheus.io/scrape: "true" - prometheus.io/path: /q/metrics - prometheus.io/port: "8080" - prometheus.io/scheme: http - labels: - app: policyservice - app.kubernetes.io/name: policyservice - spec: - containers: - - env: - - name: KUBERNETES_NAMESPACE - valueFrom: - fieldRef: - fieldPath: metadata.namespace - - 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: - failureThreshold: 3 - httpGet: - path: /q/health/live - port: 8080 - scheme: HTTP - initialDelaySeconds: 2 - periodSeconds: 10 - successThreshold: 1 - timeoutSeconds: 10 - name: policyservice - ports: - - containerPort: 8080 - name: http - protocol: TCP - - containerPort: 6060 - name: grpc-server - protocol: TCP - readinessProbe: - failureThreshold: 3 - httpGet: - path: /q/health/ready - port: 8080 - scheme: HTTP - initialDelaySeconds: 2 - periodSeconds: 10 - successThreshold: 1 - timeoutSeconds: 10 - resources: - limits: - cpu: 500m - memory: 2048Mi - requests: - cpu: 50m - memory: 512Mi