Merge "Added STD sim 2.0.0 tests"
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / clients / A1ClientFactoryTest.java
index b42cebf..74cebb0 100644 (file)
@@ -2,7 +2,7 @@
  * ========================LICENSE_START=================================
  * O-RAN-SC
  * %%
- * Copyright (C) 2019 Nordix Foundation
+ * Copyright (C) 2020 Nordix Foundation
  * %%
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -25,13 +25,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.spy;
-import static org.mockito.Mockito.verifyNoMoreInteractions;
 import static org.mockito.Mockito.when;
 
-import ch.qos.logback.classic.Level;
-import ch.qos.logback.classic.spi.ILoggingEvent;
-import ch.qos.logback.core.read.ListAppender;
-
 import java.util.Vector;
 
 import org.junit.jupiter.api.BeforeEach;
@@ -41,15 +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 org.oransc.policyagent.utils.LoggingUtils;
 
 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";
 
@@ -57,159 +54,114 @@ public class A1ClientFactoryTest {
     private ApplicationConfig applicationConfigMock;
 
     @Mock
-    A1Client stdA1ClientMock;
+    A1Client clientMock1;
 
     @Mock
-    A1Client oscA1ClientMock;
+    A1Client clientMock2;
 
     @Mock
-    A1Client sdncOscA1ClientMock;
+    A1Client clientMock3;
 
     @Mock
-    A1Client sdnrOnapA1ClientMock;
-
-    private ImmutableRicConfig ricConfig =
-        ImmutableRicConfig.builder().name(RIC_NAME).baseUrl("baseUrl").managedElementIds(new Vector<>()).build();
-    private Ric ric = new Ric(ricConfig);
+    A1Client clientMock4;
 
+    private Ric ric;
     private A1ClientFactory factoryUnderTest;
 
-    @BeforeEach
-    public void createFactoryUnderTest() {
-        factoryUnderTest = spy(new A1ClientFactory(applicationConfigMock));
+    private static ImmutableRicConfig ricConfig(String controllerName) {
+        return ImmutableRicConfig.builder() //
+            .name(RIC_NAME) //
+            .baseUrl("baseUrl") //
+            .managedElementIds(new Vector<>()) //
+            .controllerName(controllerName) //
+            .build();
     }
 
-    @Test
-    public void createStd_ok() {
-        whenGetProtocolVersionSdnrOnapA1ClientThrowException();
-        whenGetProtocolVersionSdncOscA1ClientThrowException();
-        whenGetProtocolVersionOscA1ClientThrowException();
-        whenGetProtocolVersionStdA1ClientReturnCorrectProtocol();
-
-        StepVerifier.create(factoryUnderTest.createA1Client(ric)) //
-            .expectSubscription() //
-            .expectNext(stdA1ClientMock) //
-            .verifyComplete();
-
-        assertEquals(A1ProtocolType.STD_V1, ric.getProtocolVersion(), "Not correct protocol");
-    }
-
-    @Test
-    public void createOsc_ok() {
-        whenGetProtocolVersionSdnrOnapA1ClientThrowException();
-        whenGetProtocolVersionSdncOscA1ClientThrowException();
-        whenGetProtocolVersionOscA1ClientReturnCorrectProtocol();
-
-        StepVerifier.create(factoryUnderTest.createA1Client(ric)) //
-            .expectSubscription() //
-            .expectNext(oscA1ClientMock) //
-            .verifyComplete();
+    @BeforeEach
+    void createFactoryUnderTest() {
+        factoryUnderTest = spy(new A1ClientFactory(applicationConfigMock));
+        this.ric = new Ric(ricConfig(""));
 
-        assertEquals(A1ProtocolType.OSC_V1, ric.getProtocolVersion(), "Not correct protocol");
     }
 
     @Test
-    public void createSdncOsc_ok() {
-        whenGetProtocolVersionSdnrOnapA1ClientThrowException();
-        whenGetProtocolVersionSdncOscA1ClientReturnCorrectProtocol();
+    void getProtocolVersion_ok() throws ServiceException {
+        whenGetProtocolVersionThrowException(clientMock1);
+        whenGetProtocolVersionReturn(clientMock2, A1ProtocolType.STD_V1_1);
+        doReturn(clientMock1, clientMock2).when(factoryUnderTest).createClient(any(), any());
 
-        StepVerifier.create(factoryUnderTest.createA1Client(ric)) //
-            .expectSubscription() //
-            .expectNext(sdncOscA1ClientMock) //
-            .verifyComplete();
+        A1Client client = factoryUnderTest.createA1Client(ric).block();
 
-        assertEquals(A1ProtocolType.SDNC_OSC, ric.getProtocolVersion(), "Not correct protocol");
+        assertEquals(clientMock2, client, "Not correct client returned");
+        assertEquals(A1ProtocolType.STD_V1_1, ric.getProtocolVersion(), "Not correct protocol");
     }
 
     @Test
-    public void createSdnrOnap_ok() {
-        whenGetProtocolVersionSdnrOnapA1ClientReturnCorrectProtocol();
+    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());
 
-        StepVerifier.create(factoryUnderTest.createA1Client(ric)) //
-            .expectSubscription() //
-            .expectNext(sdnrOnapA1ClientMock) //
-            .verifyComplete();
+        A1Client client = factoryUnderTest.createA1Client(ric).block();
 
-        assertEquals(A1ProtocolType.SDNR_ONAP, ric.getProtocolVersion(), "Not correct protocol");
+        assertEquals(clientMock4, client, "Not correct client returned");
+        assertEquals(A1ProtocolType.STD_V1_1, ric.getProtocolVersion(), "Not correct protocol");
     }
 
     @Test
-    public void createWithNoProtocol_error() {
-        whenGetProtocolVersionSdnrOnapA1ClientThrowException();
-        whenGetProtocolVersionSdncOscA1ClientThrowException();
-        whenGetProtocolVersionOscA1ClientThrowException();
-        whenGetProtocolVersionStdA1ClientThrowException();
+    void getProtocolVersion_error() throws ServiceException {
+        whenGetProtocolVersionThrowException(clientMock1, clientMock2, clientMock3, clientMock4);
+        doReturn(clientMock1, clientMock2, clientMock3, clientMock4).when(factoryUnderTest).createClient(any(), any());
 
-        final ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(A1ClientFactory.class);
         StepVerifier.create(factoryUnderTest.createA1Client(ric)) //
             .expectSubscription() //
-            .expectErrorMatches(
-                throwable -> throwable instanceof Exception && throwable.getMessage().equals(EXCEPTION_MESSAGE))
+            .expectError() //
             .verify();
 
-        assertEquals(Level.WARN, logAppender.list.get(0).getLevel(), "Warning not logged");
-        assertTrue(logAppender.list.toString().contains("Could not get protocol version from RIC: " + RIC_NAME),
-            "Correct message not logged");
-
-        assertEquals(A1ProtocolType.UNKNOWN, ric.getProtocolVersion(), "Not correct protocol");
+        assertEquals(A1ProtocolType.UNKNOWN, ric.getProtocolVersion(), "Protocol negotiation failed for " + ric.name());
     }
 
-    @Test
-    public void createWithProtocolInRic_noTrialAndError() {
-        doReturn(stdA1ClientMock).when(factoryUnderTest).createStdA1ClientImpl(any(Ric.class));
-
-        ric.setProtocolVersion(A1ProtocolType.STD_V1);
-
-        StepVerifier.create(factoryUnderTest.createA1Client(ric)) //
-            .expectSubscription() //
-            .expectNext(stdA1ClientMock) //
-            .verifyComplete();
-
-        assertEquals(A1ProtocolType.STD_V1, ric.getProtocolVersion(), "Not correct protocol");
-
-        verifyNoMoreInteractions(sdnrOnapA1ClientMock);
-        verifyNoMoreInteractions(sdncOscA1ClientMock);
-        verifyNoMoreInteractions(oscA1ClientMock);
-        verifyNoMoreInteractions(stdA1ClientMock);
+    private A1Client createClient(A1ProtocolType version) throws ServiceException {
+        return factoryUnderTest.createClient(ric, version);
     }
 
-    private void whenGetProtocolVersionSdnrOnapA1ClientThrowException() {
-        doReturn(sdnrOnapA1ClientMock).when(factoryUnderTest).createSdnrOnapA1Client(ric);
-        when(sdnrOnapA1ClientMock.getProtocolVersion()).thenReturn(Mono.error(new Exception(EXCEPTION_MESSAGE)));
+    @Test
+    void create_check_types() throws ServiceException {
+        assertTrue(createClient(A1ProtocolType.STD_V1_1) instanceof StdA1ClientVersion1);
+        assertTrue(createClient(A1ProtocolType.OSC_V1) instanceof OscA1Client);
     }
 
-    private void whenGetProtocolVersionSdnrOnapA1ClientReturnCorrectProtocol() {
-        doReturn(sdnrOnapA1ClientMock).when(factoryUnderTest).createSdnrOnapA1Client(any(Ric.class));
-        when(sdnrOnapA1ClientMock.getProtocolVersion()).thenReturn(Mono.just(A1ProtocolType.SDNR_ONAP));
-    }
+    @Test
+    void create_check_types_controllers() throws ServiceException {
+        this.ric = new Ric(ricConfig("anythingButEmpty"));
+        whenGetGetControllerConfigReturn();
+        assertTrue(createClient(A1ProtocolType.SDNC_ONAP) instanceof SdncOnapA1Client);
 
-    private void whenGetProtocolVersionSdncOscA1ClientThrowException() {
-        doReturn(sdncOscA1ClientMock).when(factoryUnderTest).createSdncOscA1Client(any(Ric.class));
-        when(sdncOscA1ClientMock.getProtocolVersion()).thenReturn(Mono.error(new Exception(EXCEPTION_MESSAGE)));
-    }
+        whenGetGetControllerConfigReturn();
+        assertTrue(createClient(A1ProtocolType.SDNC_OSC_STD_V1_1) instanceof SdncOscA1Client);
 
-    private void whenGetProtocolVersionSdncOscA1ClientReturnCorrectProtocol() {
-        doReturn(sdncOscA1ClientMock).when(factoryUnderTest).createSdncOscA1Client(any(Ric.class));
-        when(sdncOscA1ClientMock.getProtocolVersion()).thenReturn(Mono.just(A1ProtocolType.SDNC_OSC));
+        whenGetGetControllerConfigReturn();
+        assertTrue(createClient(A1ProtocolType.SDNC_OSC_OSC_V1) instanceof SdncOscA1Client);
     }
 
-    private void whenGetProtocolVersionOscA1ClientThrowException() {
-        doReturn(oscA1ClientMock).when(factoryUnderTest).createOscA1Client(any(Ric.class));
-        when(oscA1ClientMock.getProtocolVersion()).thenReturn(Mono.error(new Exception(EXCEPTION_MESSAGE)));
+    private void whenGetProtocolVersionThrowException(A1Client... clientMocks) {
+        for (A1Client clientMock : clientMocks) {
+            when(clientMock.getProtocolVersion()).thenReturn(Mono.error(new Exception(EXCEPTION_MESSAGE)));
+        }
     }
 
-    private void whenGetProtocolVersionOscA1ClientReturnCorrectProtocol() {
-        doReturn(oscA1ClientMock).when(factoryUnderTest).createOscA1Client(any(Ric.class));
-        when(oscA1ClientMock.getProtocolVersion()).thenReturn(Mono.just(A1ProtocolType.OSC_V1));
+    private void whenGetProtocolVersionReturn(A1Client clientMock, A1ProtocolType protocol) {
+        when(clientMock.getProtocolVersion()).thenReturn(Mono.just(protocol));
     }
 
-    private void whenGetProtocolVersionStdA1ClientThrowException() {
-        doReturn(stdA1ClientMock).when(factoryUnderTest).createStdA1ClientImpl(any(Ric.class));
-        when(stdA1ClientMock.getProtocolVersion()).thenReturn(Mono.error(new Exception(EXCEPTION_MESSAGE)));
+    private void whenGetGetControllerConfigReturn() throws ServiceException {
+        ControllerConfig controllerCfg = ImmutableControllerConfig.builder() //
+            .name("name") //
+            .baseUrl("baseUrl") //
+            .password("pass") //
+            .userName("user") //
+            .build();
+        when(applicationConfigMock.getControllerConfig(any())).thenReturn(controllerCfg);
     }
 
-    private void whenGetProtocolVersionStdA1ClientReturnCorrectProtocol() {
-        doReturn(stdA1ClientMock).when(factoryUnderTest).createStdA1ClientImpl(any(Ric.class));
-        when(stdA1ClientMock.getProtocolVersion()).thenReturn(Mono.just(A1ProtocolType.STD_V1));
-    }
 }