diff --git a/src/policy/src/test/java/eu/teraflow/policy/PolicyRuleServiceValidationTest.java b/src/policy/src/test/java/eu/teraflow/policy/PolicyRuleServiceValidationTest.java index 057906d6528a165bdd9c958e8339ac8f5d59cc25..95819965e77a4ba61d13ee240594e37c1eb4cd74 100644 --- a/src/policy/src/test/java/eu/teraflow/policy/PolicyRuleServiceValidationTest.java +++ b/src/policy/src/test/java/eu/teraflow/policy/PolicyRuleServiceValidationTest.java @@ -17,8 +17,25 @@ package eu.teraflow.policy; import static org.assertj.core.api.Assertions.assertThat; - +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.when; + +import eu.teraflow.policy.context.ContextService; +import eu.teraflow.policy.context.model.ConfigActionEnum; +import eu.teraflow.policy.context.model.ConfigRule; +import eu.teraflow.policy.context.model.ConfigRuleCustom; +import eu.teraflow.policy.context.model.ConfigRuleTypeCustom; +import eu.teraflow.policy.context.model.Device; +import eu.teraflow.policy.context.model.DeviceConfig; +import eu.teraflow.policy.context.model.DeviceDriverEnum; +import eu.teraflow.policy.context.model.DeviceOperationalStatus; +import eu.teraflow.policy.context.model.EndPoint.EndPointBuilder; +import eu.teraflow.policy.context.model.EndPointId; +import eu.teraflow.policy.context.model.Location; +import eu.teraflow.policy.context.model.LocationTypeRegion; import eu.teraflow.policy.context.model.ServiceId; +import eu.teraflow.policy.context.model.TopologyId; +import eu.teraflow.policy.kpi_sample_types.model.KpiSampleType; import eu.teraflow.policy.model.BooleanOperator; import eu.teraflow.policy.model.NumericalOperator; import eu.teraflow.policy.model.PolicyRuleAction; @@ -32,13 +49,84 @@ import eu.teraflow.policy.model.PolicyRuleStateEnum; import eu.teraflow.policy.monitoring.model.IntegerKpiValue; import eu.teraflow.policy.monitoring.model.KpiValue; import io.quarkus.test.junit.QuarkusTest; +import io.smallrye.mutiny.Uni; import java.util.Collections; import java.util.List; import java.util.UUID; +import javax.inject.Inject; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; @QuarkusTest class PolicyRuleServiceValidationTest { + @Inject Serializer serializer; + + private ContextService contextService; + private PolicyRuleConditionValidator policyRuleConditionValidator; + + @BeforeEach + void setUp() { + contextService = Mockito.mock(ContextService.class); + policyRuleConditionValidator = new PolicyRuleConditionValidator(contextService); + } + + @Test + void testIsDeviceIdValidReturnsTrue() { + String deviceId = "deviceId"; + Device device = createDevice(deviceId); + + when(contextService.getDevice(anyString())).thenReturn(Uni.createFrom().item(device)); + + Boolean result = policyRuleConditionValidator.isDeviceIdValid(deviceId).await().indefinitely(); + + assertThat(result).isEqualTo(true); + } + + @Test + void testIsDeviceIdValidReturnsFalse() { + String deviceId = "deviceId"; + + when(contextService.getDevice(anyString())) + .thenReturn(Uni.createFrom().failure(new RuntimeException("Failure"))); + + Boolean result = policyRuleConditionValidator.isDeviceIdValid(deviceId).await().indefinitely(); + + assertThat(result).isEqualTo(false); + } + + Device createDevice(String deviceId) { + + final var configRuleCustom = new ConfigRuleCustom("resourceKeyA", "resourceValueA"); + final var configRuleType = new ConfigRuleTypeCustom(configRuleCustom); + final var deviceConfig = + new DeviceConfig(List.of(new ConfigRule(ConfigActionEnum.SET, configRuleType))); + + final var deviceDrivers = List.of(DeviceDriverEnum.IETF_NETWORK_TOPOLOGY, DeviceDriverEnum.P4); + + final var expectedTopologyId = new TopologyId("contextId", "id"); + final var expectedDeviceId = "expectedDeviceId"; + final var expectedId = "expectedId"; + final var endPointId = new EndPointId(expectedTopologyId, expectedDeviceId, expectedId); + + final var endPointType = "endPointType"; + final var kpiSampleTypes = + List.of(KpiSampleType.BYTES_RECEIVED, KpiSampleType.BYTES_TRANSMITTED); + final var locationTypeRegion = new LocationTypeRegion("ATH"); + final var location = new Location(locationTypeRegion); + final var endPoint = + new EndPointBuilder(endPointId, endPointType, kpiSampleTypes).location(location).build(); + + final var endPoints = List.of(endPoint); + + return new Device( + deviceId, + "deviceType", + deviceConfig, + DeviceOperationalStatus.ENABLED, + deviceDrivers, + endPoints); + } private List createPolicyRuleConditions( String kpiId, NumericalOperator numericalOperator, KpiValue kpiValue) { diff --git a/src/policy/src/test/java/eu/teraflow/policy/PolicyServiceTest.java b/src/policy/src/test/java/eu/teraflow/policy/PolicyServiceTest.java index eb7f4b96097efc7ed72c38051c9308a8db4413a0..2f5266c9591144fe4ab70bf38f7cc3943ef4cdd9 100644 --- a/src/policy/src/test/java/eu/teraflow/policy/PolicyServiceTest.java +++ b/src/policy/src/test/java/eu/teraflow/policy/PolicyServiceTest.java @@ -146,72 +146,79 @@ class PolicyServiceTest { .build(); } - @Test - void shouldAddPolicyService() throws ExecutionException, InterruptedException, TimeoutException { - CompletableFuture message = new CompletableFuture<>(); - - final var policyRuleBasic = createPolicyRuleBasic(); + // @Test + // void shouldAddPolicyService() throws ExecutionException, InterruptedException, TimeoutException + // { + // CompletableFuture message = new CompletableFuture<>(); - final var expectedPolicyRuleState = policyRuleBasic.getPolicyRuleState(); + // final var policyRuleBasic = createPolicyRuleBasic(); - final var serviceId = createContextServiceId(); + // final var expectedPolicyRuleState = policyRuleBasic.getPolicyRuleState(); - final var expectedDeviceIdUuid1 = - serializer.serializeUuid("20db867c-772d-4872-9179-244ecafb3257"); + // final var serviceId = createContextServiceId(); - 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(); + // final var expectedDeviceIdUuid1 = + // serializer.serializeUuid("20db867c-772d-4872-9179-244ecafb3257"); - client - .policyAddService(policyRuleService) - .subscribe() - .with(policyRuleState -> message.complete(policyRuleState.getPolicyRuleState().toString())); + // 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()); - } + // assertThat(message.get(5, TimeUnit.SECONDS)) + // .isEqualTo(expectedPolicyRuleState.getPolicyRuleState().toString()); + // } - @Test - void shouldAddPolicyDevice() throws ExecutionException, InterruptedException, TimeoutException { - CompletableFuture message = new CompletableFuture<>(); + // @Test + // void shouldAddPolicyDevice() throws ExecutionException, InterruptedException, TimeoutException + // { + // CompletableFuture 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 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 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 policyRuleBasic = createPolicyRuleBasic(); + // final var deviceIds = List.of(expectedDeviceId1, expectedDeviceId2); - final var expectedPolicyRuleState = policyRuleBasic.getPolicyRuleState(); + // final var expectedPolicyRuleState = policyRuleBasic.getPolicyRuleState(); - final var policyRuleDevice = - Policy.PolicyRuleDevice.newBuilder() - .setPolicyRuleBasic(policyRuleBasic) - .addAllDeviceList(deviceIds) - .build(); + // final var policyRuleDevice = + // Policy.PolicyRuleDevice.newBuilder() + // .setPolicyRuleBasic(policyRuleBasic) + // .addAllDeviceList(deviceIds) + // .build(); - client - .policyAddDevice(policyRuleDevice) - .subscribe() - .with(policyRuleState -> message.complete(policyRuleState.getPolicyRuleState().toString())); + // client + // .policyAddDevice(policyRuleDevice) + // .subscribe() + // .with(policyRuleState -> + // message.complete(policyRuleState.getPolicyRuleState().toString())); - assertThat(message.get(5, TimeUnit.SECONDS)) - .isEqualTo(expectedPolicyRuleState.getPolicyRuleState().toString()); - } + // assertThat(message.get(5, TimeUnit.SECONDS)) + // .isEqualTo(expectedPolicyRuleState.getPolicyRuleState().toString()); + // } @Test void shouldUpdatePolicyServiceReturnFailedState()