X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=policy-agent%2Fsrc%2Ftest%2Fjava%2Forg%2Foransc%2Fpolicyagent%2Ftasks%2FStartupServiceTest.java;h=0c254d59f1841af083c33603493b89a2f3234c72;hb=b47a7130c10bef2bf812366ca971e4eaa938b152;hp=f15a3ab3ee1452cd4872fc71e91bf3513d1fd4af;hpb=ecd2e057eb3df580433df224060c9d755146f834;p=nonrtric.git diff --git a/policy-agent/src/test/java/org/oransc/policyagent/tasks/StartupServiceTest.java b/policy-agent/src/test/java/org/oransc/policyagent/tasks/StartupServiceTest.java index f15a3ab3..0c254d59 100644 --- a/policy-agent/src/test/java/org/oransc/policyagent/tasks/StartupServiceTest.java +++ b/policy-agent/src/test/java/org/oransc/policyagent/tasks/StartupServiceTest.java @@ -26,10 +26,14 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.oransc.policyagent.repository.Ric.RicState.ACTIVE; +import static org.oransc.policyagent.repository.Ric.RicState.NOT_REACHABLE; +import java.util.Arrays; +import java.util.Collection; import java.util.Vector; import org.junit.jupiter.api.Test; @@ -42,12 +46,10 @@ import org.oransc.policyagent.clients.A1Client; import org.oransc.policyagent.configuration.ApplicationConfig; import org.oransc.policyagent.configuration.ImmutableRicConfig; import org.oransc.policyagent.configuration.RicConfig; -import org.oransc.policyagent.exceptions.ServiceException; +import org.oransc.policyagent.repository.Policies; import org.oransc.policyagent.repository.PolicyTypes; import org.oransc.policyagent.repository.Ric; import org.oransc.policyagent.repository.Rics; - -import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @ExtendWith(MockitoExtension.class) @@ -73,23 +75,24 @@ public class StartupServiceTest { A1Client a1ClientMock; @Test - public void startup_allOk() throws ServiceException { + public void startup_allOk() { Vector ricConfigs = new Vector<>(2); ricConfigs.add(getRicConfig(FIRST_RIC_NAME, FIRST_RIC_URL, MANAGED_NODE_A)); ricConfigs.add(getRicConfig(SECOND_RIC_NAME, SECOND_RIC_URL, MANAGED_NODE_B, MANAGED_NODE_C)); when(appConfigMock.getRicConfigs()).thenReturn(ricConfigs); - Flux fluxType1 = Flux.just(POLICY_TYPE_1_NAME); - Flux fluxType2 = Flux.just(POLICY_TYPE_2_NAME); - when(a1ClientMock.getPolicyTypeIdentities(anyString())).thenReturn(fluxType1) - .thenReturn(fluxType1.concatWith(fluxType2)); - Flux policies = Flux.just(new String[] {POLICY_ID_1, POLICY_ID_2}); + Mono> policyTypes1 = Mono.just(Arrays.asList(POLICY_TYPE_1_NAME)); + Mono> policyTypes2 = Mono.just(Arrays.asList(POLICY_TYPE_1_NAME, POLICY_TYPE_2_NAME)); + when(a1ClientMock.getPolicyTypeIdentities(anyString())).thenReturn(policyTypes1).thenReturn(policyTypes2); + Mono> policies = Mono.just(Arrays.asList(POLICY_ID_1, POLICY_ID_2)); when(a1ClientMock.getPolicyIdentities(anyString())).thenReturn(policies); - when(a1ClientMock.deletePolicy(anyString(), anyString())).thenReturn(Mono.empty()); + when(a1ClientMock.getPolicyType(anyString(), anyString())).thenReturn(Mono.just("Schema")); + when(a1ClientMock.deletePolicy(anyString(), anyString())).thenReturn(Mono.just("OK")); Rics rics = new Rics(); PolicyTypes policyTypes = new PolicyTypes(); - StartupService serviceUnderTest = new StartupService(appConfigMock, rics, policyTypes, a1ClientMock); + StartupService serviceUnderTest = + new StartupService(appConfigMock, rics, policyTypes, a1ClientMock, new Policies()); serviceUnderTest.startup(); @@ -107,7 +110,7 @@ public class StartupServiceTest { assertTrue(policyTypes.contains(POLICY_TYPE_2_NAME), POLICY_TYPE_2_NAME + " not added to PolicyTypes."); assertEquals(2, rics.size(), "Correct number of Rics not added to Rics"); - Ric firstRic = rics.getRic(FIRST_RIC_NAME); + Ric firstRic = rics.get(FIRST_RIC_NAME); assertNotNull(firstRic, "Ric " + FIRST_RIC_NAME + " not added to repository"); assertEquals(FIRST_RIC_NAME, firstRic.name(), FIRST_RIC_NAME + " not added to Rics"); assertEquals(ACTIVE, firstRic.state(), "Not correct state for ric " + FIRST_RIC_NAME); @@ -115,11 +118,12 @@ public class StartupServiceTest { "Not correct no of types supported for ric " + FIRST_RIC_NAME); assertTrue(firstRic.isSupportingType(POLICY_TYPE_1_NAME), POLICY_TYPE_1_NAME + " not supported by ric " + FIRST_RIC_NAME); - assertEquals(1, firstRic.getManagedNodes().size(), "Not correct no of managed nodes for ric " + FIRST_RIC_NAME); + assertEquals(1, firstRic.getManagedElementIds().size(), + "Not correct no of managed nodes for ric " + FIRST_RIC_NAME); assertTrue(firstRic.isManaging(MANAGED_NODE_A), MANAGED_NODE_A + " not managed by ric " + FIRST_RIC_NAME); - Ric secondRic = rics.getRic(SECOND_RIC_NAME); - assertNotNull(secondRic, "Ric " + SECOND_RIC_NAME + " not added to repositpry"); + Ric secondRic = rics.get(SECOND_RIC_NAME); + assertNotNull(secondRic, "Ric " + SECOND_RIC_NAME + " not added to repository"); assertEquals(SECOND_RIC_NAME, secondRic.name(), SECOND_RIC_NAME + " not added to Rics"); assertEquals(ACTIVE, secondRic.state(), "Not correct state for " + SECOND_RIC_NAME); assertEquals(2, secondRic.getSupportedPolicyTypes().size(), @@ -128,20 +132,87 @@ public class StartupServiceTest { POLICY_TYPE_1_NAME + " not supported by ric " + SECOND_RIC_NAME); assertTrue(secondRic.isSupportingType(POLICY_TYPE_2_NAME), POLICY_TYPE_2_NAME + " not supported by ric " + SECOND_RIC_NAME); - assertEquals(2, secondRic.getManagedNodes().size(), + assertEquals(2, secondRic.getManagedElementIds().size(), "Not correct no of managed nodes for ric " + SECOND_RIC_NAME); assertTrue(secondRic.isManaging(MANAGED_NODE_B), MANAGED_NODE_B + " not managed by ric " + SECOND_RIC_NAME); assertTrue(secondRic.isManaging(MANAGED_NODE_C), MANAGED_NODE_C + " not managed by ric " + SECOND_RIC_NAME); } - private RicConfig getRicConfig(String name, String baseUrl, String... nodeNames) { - Vector managedNodes = new Vector(1); - for (String nodeName : nodeNames) { - managedNodes.add(nodeName); + @Test + public void startup_unableToConnectToGetTypes() { + Vector ricConfigs = new Vector<>(2); + ricConfigs.add(getRicConfig(FIRST_RIC_NAME, FIRST_RIC_URL, MANAGED_NODE_A)); + ricConfigs.add(getRicConfig(SECOND_RIC_NAME, SECOND_RIC_URL, MANAGED_NODE_B, MANAGED_NODE_C)); + when(appConfigMock.getRicConfigs()).thenReturn(ricConfigs); + + Mono> policyIdentities = Mono.just(Arrays.asList(POLICY_TYPE_1_NAME)); + Mono error = Mono.error(new Exception("Unable to contact ric.")); + doReturn(error, policyIdentities).when(a1ClientMock).getPolicyTypeIdentities(anyString()); + + Mono> policies = Mono.just(Arrays.asList(POLICY_ID_1, POLICY_ID_2)); + doReturn(policies).when(a1ClientMock).getPolicyIdentities(anyString()); + when(a1ClientMock.getPolicyType(anyString(), anyString())).thenReturn(Mono.just("Schema")); + when(a1ClientMock.deletePolicy(anyString(), anyString())).thenReturn(Mono.just("OK")); + + Rics rics = new Rics(); + PolicyTypes policyTypes = new PolicyTypes(); + StartupService serviceUnderTest = + new StartupService(appConfigMock, rics, policyTypes, a1ClientMock, new Policies()); + + serviceUnderTest.startup(); + + verify(a1ClientMock).deletePolicy(SECOND_RIC_URL, POLICY_ID_1); + verify(a1ClientMock).deletePolicy(SECOND_RIC_URL, POLICY_ID_2); + + assertEquals(NOT_REACHABLE, rics.get(FIRST_RIC_NAME).state(), "Not correct state for " + FIRST_RIC_NAME); + + assertEquals(ACTIVE, rics.get(SECOND_RIC_NAME).state(), "Not correct state for " + SECOND_RIC_NAME); + } + + @Test + public void startup_unableToConnectToGetPolicies() { + Vector ricConfigs = new Vector<>(2); + ricConfigs.add(getRicConfig(FIRST_RIC_NAME, FIRST_RIC_URL, MANAGED_NODE_A)); + ricConfigs.add(getRicConfig(SECOND_RIC_NAME, SECOND_RIC_URL, MANAGED_NODE_B, MANAGED_NODE_C)); + when(appConfigMock.getRicConfigs()).thenReturn(ricConfigs); + + Mono> policyTypes1 = Mono.just(Arrays.asList(POLICY_TYPE_1_NAME)); + Mono> policyTypes2 = Mono.just(Arrays.asList(POLICY_TYPE_1_NAME, POLICY_TYPE_2_NAME)); + when(a1ClientMock.getPolicyTypeIdentities(anyString())).thenReturn(policyTypes1).thenReturn(policyTypes2); + when(a1ClientMock.getPolicyType(anyString(), anyString())).thenReturn(Mono.just("Schema")); + Mono> policies = Mono.just(Arrays.asList(POLICY_ID_1, POLICY_ID_2)); + doReturn(Mono.error(new Exception("Unable to contact ric.")), policies).when(a1ClientMock) + .getPolicyIdentities(anyString()); + when(a1ClientMock.deletePolicy(anyString(), anyString())).thenReturn(Mono.just("OK")); + + Rics rics = new Rics(); + PolicyTypes policyTypes = new PolicyTypes(); + StartupService serviceUnderTest = + new StartupService(appConfigMock, rics, policyTypes, a1ClientMock, new Policies()); + + serviceUnderTest.startup(); + + verify(a1ClientMock).deletePolicy(SECOND_RIC_URL, POLICY_ID_1); + verify(a1ClientMock).deletePolicy(SECOND_RIC_URL, POLICY_ID_2); + + assertEquals(NOT_REACHABLE, rics.get(FIRST_RIC_NAME).state(), "Not correct state for " + FIRST_RIC_NAME); + + assertEquals(ACTIVE, rics.get(SECOND_RIC_NAME).state(), "Not correct state for " + SECOND_RIC_NAME); + } + + @SafeVarargs + private Vector toVector(T... objs) { + Vector result = new Vector<>(); + for (T o : objs) { + result.add(o); } + return result; + } + + private RicConfig getRicConfig(String name, String baseUrl, String... managedElementIds) { ImmutableRicConfig ricConfig = ImmutableRicConfig.builder() // .name(name) // - .managedElementIds(managedNodes) // + .managedElementIds(toVector(managedElementIds)) // .baseUrl(baseUrl) // .build(); return ricConfig;