Merge "Added STD sim 2.0.0 tests"
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / clients / A1ClientFactoryTest.java
index a7495b9..74cebb0 100644 (file)
@@ -36,14 +36,17 @@ import org.mockito.Mock;
 import org.mockito.junit.jupiter.MockitoExtension;
 import org.oransc.policyagent.clients.A1Client.A1ProtocolType;
 import org.oransc.policyagent.configuration.ApplicationConfig;
+import org.oransc.policyagent.configuration.ControllerConfig;
+import org.oransc.policyagent.configuration.ImmutableControllerConfig;
 import org.oransc.policyagent.configuration.ImmutableRicConfig;
+import org.oransc.policyagent.exceptions.ServiceException;
 import org.oransc.policyagent.repository.Ric;
 
 import reactor.core.publisher.Mono;
 import reactor.test.StepVerifier;
 
 @ExtendWith(MockitoExtension.class)
-public class A1ClientFactoryTest {
+class A1ClientFactoryTest {
     private static final String RIC_NAME = "Name";
     private static final String EXCEPTION_MESSAGE = "Error";
 
@@ -62,71 +65,83 @@ public class A1ClientFactoryTest {
     @Mock
     A1Client clientMock4;
 
-    @Mock
-    A1Client clientMock5;
-
-    private ImmutableRicConfig ricConfig =
-        ImmutableRicConfig.builder().name(RIC_NAME).baseUrl("baseUrl").managedElementIds(new Vector<>()).build();
-    private Ric ric = new Ric(ricConfig);
-
+    private Ric ric;
     private A1ClientFactory factoryUnderTest;
 
+    private static ImmutableRicConfig ricConfig(String controllerName) {
+        return ImmutableRicConfig.builder() //
+            .name(RIC_NAME) //
+            .baseUrl("baseUrl") //
+            .managedElementIds(new Vector<>()) //
+            .controllerName(controllerName) //
+            .build();
+    }
+
     @BeforeEach
-    public void createFactoryUnderTest() {
+    void createFactoryUnderTest() {
         factoryUnderTest = spy(new A1ClientFactory(applicationConfigMock));
+        this.ric = new Ric(ricConfig(""));
+
     }
 
     @Test
-    public void getProtocolVersion_ok() {
+    void getProtocolVersion_ok() throws ServiceException {
         whenGetProtocolVersionThrowException(clientMock1);
-        whenGetProtocolVersionReturn(clientMock2, A1ProtocolType.STD_V1);
+        whenGetProtocolVersionReturn(clientMock2, A1ProtocolType.STD_V1_1);
         doReturn(clientMock1, clientMock2).when(factoryUnderTest).createClient(any(), any());
 
         A1Client client = factoryUnderTest.createA1Client(ric).block();
 
         assertEquals(clientMock2, client, "Not correct client returned");
-        assertEquals(A1ProtocolType.STD_V1, ric.getProtocolVersion(), "Not correct protocol");
+        assertEquals(A1ProtocolType.STD_V1_1, ric.getProtocolVersion(), "Not correct protocol");
     }
 
     @Test
-    public void getProtocolVersion_ok_Last() {
-        whenGetProtocolVersionThrowException(clientMock1, clientMock2, clientMock3, clientMock4);
-        whenGetProtocolVersionReturn(clientMock5, A1ProtocolType.STD_V1_1);
-        doReturn(clientMock1, clientMock2, clientMock3, clientMock4, clientMock5).when(factoryUnderTest)
-            .createClient(any(), any());
+    void getProtocolVersion_ok_Last() throws ServiceException {
+        whenGetProtocolVersionThrowException(clientMock1, clientMock2, clientMock3);
+        whenGetProtocolVersionReturn(clientMock4, A1ProtocolType.STD_V1_1);
+        doReturn(clientMock1, clientMock2, clientMock3, clientMock4).when(factoryUnderTest).createClient(any(), any());
 
         A1Client client = factoryUnderTest.createA1Client(ric).block();
 
-        assertEquals(clientMock5, client, "Not correct client returned");
+        assertEquals(clientMock4, client, "Not correct client returned");
         assertEquals(A1ProtocolType.STD_V1_1, ric.getProtocolVersion(), "Not correct protocol");
     }
 
     @Test
-    public void getProtocolVersion_error() {
-        whenGetProtocolVersionThrowException(clientMock1, clientMock2, clientMock3, clientMock4, clientMock5);
-        doReturn(clientMock1, clientMock2, clientMock3, clientMock4, clientMock5).when(factoryUnderTest)
-            .createClient(any(), any());
+    void getProtocolVersion_error() throws ServiceException {
+        whenGetProtocolVersionThrowException(clientMock1, clientMock2, clientMock3, clientMock4);
+        doReturn(clientMock1, clientMock2, clientMock3, clientMock4).when(factoryUnderTest).createClient(any(), any());
 
         StepVerifier.create(factoryUnderTest.createA1Client(ric)) //
             .expectSubscription() //
-            .expectErrorMatches(throwable -> throwable.getMessage().equals(EXCEPTION_MESSAGE)) //
+            .expectError() //
             .verify();
 
-        assertEquals(A1ProtocolType.UNKNOWN, ric.getProtocolVersion(), "Not correct protocol");
+        assertEquals(A1ProtocolType.UNKNOWN, ric.getProtocolVersion(), "Protocol negotiation failed for " + ric.name());
     }
 
-    private A1Client createClient(A1ProtocolType version) {
+    private A1Client createClient(A1ProtocolType version) throws ServiceException {
         return factoryUnderTest.createClient(ric, version);
     }
 
     @Test
-    public void create_check_types() {
-        assertTrue(createClient(A1ProtocolType.STD_V1) instanceof StdA1ClientVersion1);
-        assertTrue(createClient(A1ProtocolType.STD_V1_1) instanceof StdA1ClientVersion2);
+    void create_check_types() throws ServiceException {
+        assertTrue(createClient(A1ProtocolType.STD_V1_1) instanceof StdA1ClientVersion1);
         assertTrue(createClient(A1ProtocolType.OSC_V1) instanceof OscA1Client);
+    }
+
+    @Test
+    void create_check_types_controllers() throws ServiceException {
+        this.ric = new Ric(ricConfig("anythingButEmpty"));
+        whenGetGetControllerConfigReturn();
         assertTrue(createClient(A1ProtocolType.SDNC_ONAP) instanceof SdncOnapA1Client);
-        assertTrue(createClient(A1ProtocolType.SDNC_OSC) instanceof SdncOscA1Client);
-        assertTrue(createClient(A1ProtocolType.UNKNOWN) == null);
+
+        whenGetGetControllerConfigReturn();
+        assertTrue(createClient(A1ProtocolType.SDNC_OSC_STD_V1_1) instanceof SdncOscA1Client);
+
+        whenGetGetControllerConfigReturn();
+        assertTrue(createClient(A1ProtocolType.SDNC_OSC_OSC_V1) instanceof SdncOscA1Client);
     }
 
     private void whenGetProtocolVersionThrowException(A1Client... clientMocks) {
@@ -139,4 +154,14 @@ public class A1ClientFactoryTest {
         when(clientMock.getProtocolVersion()).thenReturn(Mono.just(protocol));
     }
 
+    private void whenGetGetControllerConfigReturn() throws ServiceException {
+        ControllerConfig controllerCfg = ImmutableControllerConfig.builder() //
+            .name("name") //
+            .baseUrl("baseUrl") //
+            .password("pass") //
+            .userName("user") //
+            .build();
+        when(applicationConfigMock.getControllerConfig(any())).thenReturn(controllerCfg);
+    }
+
 }