X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=policy-agent%2Fsrc%2Ftest%2Fjava%2Forg%2Foransc%2Fpolicyagent%2Ftasks%2FStartupServiceTest.java;h=5cfabd9cce56e156d1c4226596d14d9fe9caf5e5;hb=a76d95e9292f99dfb5cd5782ef6d7bb2ec293fd7;hp=f15a3ab3ee1452cd4872fc71e91bf3513d1fd4af;hpb=f88fc83885ab534a48f258dab9c65023281f22d1;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..5cfabd9c 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,9 +26,11 @@ 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.Vector; @@ -42,7 +44,6 @@ 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.PolicyTypes; import org.oransc.policyagent.repository.Ric; import org.oransc.policyagent.repository.Rics; @@ -73,7 +74,7 @@ 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)); @@ -85,7 +86,8 @@ public class StartupServiceTest { .thenReturn(fluxType1.concatWith(fluxType2)); Flux policies = Flux.just(new String[] {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(); @@ -107,7 +109,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); @@ -118,8 +120,8 @@ public class StartupServiceTest { assertEquals(1, firstRic.getManagedNodes().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(), @@ -134,6 +136,67 @@ public class StartupServiceTest { assertTrue(secondRic.isManaging(MANAGED_NODE_C), MANAGED_NODE_C + " not managed by ric " + SECOND_RIC_NAME); } + @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); + + Flux fluxType1 = Flux.just(POLICY_TYPE_1_NAME); + doReturn(Flux.error(new Exception("Unable to contact ric.")), fluxType1).when(a1ClientMock) + .getPolicyTypeIdentities(anyString()); + + Flux policies = Flux.just(new String[] {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); + + 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); + + 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)); + when(a1ClientMock.getPolicyType(anyString(), anyString())).thenReturn(Mono.just("Schema")); + Flux policies = Flux.just(new String[] {POLICY_ID_1, POLICY_ID_2}); + doReturn(Flux.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); + + 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); + } + private RicConfig getRicConfig(String name, String baseUrl, String... nodeNames) { Vector managedNodes = new Vector(1); for (String nodeName : nodeNames) {