Fix sonar issues
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / test / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / core / TestMsbApiProvider.java
index e6d8ebb..c313e31 100644 (file)
 
 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core;
 
+import io.reactivex.Observable;
+import java.util.ArrayList;
+import java.util.List;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mock;
-import org.onap.msb.sdk.discovery.common.RouteException;
-import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
-import org.onap.msb.sdk.discovery.entity.NodeInfo;
-import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;
+import org.mockito.Mockito;
+import org.onap.msb.ApiClient;
+import org.onap.msb.api.ServiceResourceApi;
+import org.onap.msb.model.MicroServiceFullInfo;
+import org.onap.msb.model.NodeInfo;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamTokenProvider;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
 import org.springframework.core.env.Environment;
 
-import java.util.HashSet;
-import java.util.Set;
-
 import static junit.framework.TestCase.assertEquals;
 import static junit.framework.TestCase.fail;
 import static org.mockito.Mockito.verify;
@@ -42,7 +43,7 @@ public class TestMsbApiProvider extends TestBase {
     @Mock
     private CbamTokenProvider cbamTokenProvider;
     private MicroServiceFullInfo microServiceInfo = new MicroServiceFullInfo();
-    private Set<NodeInfo> nodes = new HashSet<>();
+    private List<NodeInfo> nodes = new ArrayList<>();
     private MsbApiProvider msbApiProvider;
 
     @Before
@@ -60,9 +61,9 @@ public class TestMsbApiProvider extends TestBase {
         setFieldWithPropertyAnnotation(msbApiProvider, "${messageBusIp}", "mymessageBusIp");
         setFieldWithPropertyAnnotation(msbApiProvider, "${messageBusPort}", "123");
         //when
-        MSBServiceClient msbClient = msbApiProvider.getMsbClient();
+        ApiClient msbClient = msbApiProvider.buildApiClient();
         //verify
-        assertEquals("mymessageBusIp:123", msbClient.getMsbSvrAddress());
+        assertEquals("http://mymessagebusip:123/api/msdiscover/v1/", msbClient.getAdapterBuilder().build().baseUrl().toString());
     }
 
     /**
@@ -79,18 +80,34 @@ public class TestMsbApiProvider extends TestBase {
         nodes.add(dockerAccessPoint);
         msbApiProvider = new MsbApiProvider(environment) {
             @Override
-            public MSBServiceClient getMsbClient() {
+            public ServiceResourceApi getMsbApi() {
                 return msbClient;
             }
         };
-        when(msbClient.queryMicroServiceInfo("serviceName", "v1")).thenReturn(microServiceInfo);
+        when(msbClient.getMicroService_0("serviceName", "v1", null, null, null, null, null)).thenReturn(buildObservable(microServiceInfo));
         //when
         try {
             msbApiProvider.getMicroServiceUrl("serviceName", "v1");
             fail();
         } catch (Exception e) {
-            assertEquals("The serviceName service with v1 does not have any valid nodes[172.1.2.3:null  ttl:]", e.getMessage());
-            verify(logger).error("The serviceName service with v1 does not have any valid nodes[172.1.2.3:null  ttl:]");
+            String msg = "The serviceName service with v1 does not have any valid nodes[class NodeInfo {\n" +
+                    "    ip: 172.1.2.3\n" +
+                    "    port: null\n" +
+                    "    lbServerParams: null\n" +
+                    "    checkType: null\n" +
+                    "    checkUrl: null\n" +
+                    "    checkInterval: null\n" +
+                    "    checkTimeOut: null\n" +
+                    "    ttl: null\n" +
+                    "    haRole: null\n" +
+                    "    nodeId: null\n" +
+                    "    status: null\n" +
+                    "    expiration: null\n" +
+                    "    createdAt: null\n" +
+                    "    updatedAt: null\n" +
+                    "}]";
+            assertEquals(msg, e.getMessage());
+            verify(logger).error(msg);
         }
     }
 
@@ -109,16 +126,43 @@ public class TestMsbApiProvider extends TestBase {
         nodes.add(nonDocker);
         msbApiProvider = new MsbApiProvider(environment) {
             @Override
-            public MSBServiceClient getMsbClient() {
+            public ServiceResourceApi getMsbApi() {
                 return msbClient;
             }
         };
-        when(msbClient.queryMicroServiceInfo("serviceName", "v1")).thenReturn(microServiceInfo);
+        when(msbClient.getMicroService_0("serviceName", "v1", null, null, null, null, null)).thenReturn(buildObservable(microServiceInfo));
         msbApiProvider.afterPropertiesSet();
         //when
         assertEquals("http://1.2.3.4:234/lead/nslcm/v1", msbApiProvider.getMicroServiceUrl("serviceName", "v1"));
     }
 
+
+    /**
+     * use HTTPS for known ports
+     */
+    @Test
+    public void testMsbServiceOverssl() throws Exception {
+        NodeInfo nonDocker = new NodeInfo();
+        nonDocker.setIp("173.1.2.3");
+        nonDocker.setPort("123");
+        microServiceInfo.setServiceName("serviceName");
+        microServiceInfo.setVersion("v1");
+        microServiceInfo.setUrl("/lead/nslcm/v1");
+        microServiceInfo.setEnableSsl(true);
+        when(environment.getProperty(IpMappingProvider.IP_MAP, String.class, "")).thenReturn("173.1.2.3->1.2.3.4");
+        nodes.add(nonDocker);
+        msbApiProvider = new MsbApiProvider(environment) {
+            @Override
+            public ServiceResourceApi getMsbApi() {
+                return msbClient;
+            }
+        };
+        when(msbClient.getMicroService_0("serviceName", "v1", null, null, null, null, null)).thenReturn(buildObservable(microServiceInfo));
+        msbApiProvider.afterPropertiesSet();
+        //when
+        assertEquals("https://1.2.3.4:123/lead/nslcm/v1", msbApiProvider.getMicroServiceUrl("serviceName", "v1"));
+    }
+
     /**
      * if unable to get micro service info the error is propagated
      */
@@ -126,13 +170,12 @@ public class TestMsbApiProvider extends TestBase {
     public void testUnableQueryMicroserviInfo() throws Exception {
         msbApiProvider = new MsbApiProvider(environment) {
             @Override
-            public MSBServiceClient getMsbClient() {
+            public ServiceResourceApi getMsbApi() {
                 return msbClient;
             }
         };
-        RouteException expectedException = new RouteException();
-        when(msbClient.queryMicroServiceInfo("serviceName", "v1")).thenThrow(expectedException);
-
+        RuntimeException expectedException = new RuntimeException();
+        when(msbClient.getMicroService_0("serviceName", "v1", null, null, null, null, null)).thenReturn(Observable.error(expectedException));
         //when
         try {
             msbApiProvider.getMicroServiceUrl("serviceName", "v1");
@@ -143,4 +186,29 @@ public class TestMsbApiProvider extends TestBase {
         }
     }
 
+    /**
+     * Test API wrapping for LCM
+     * (questionable benefit [ this is more less ensured by Java type safety) ]
+     */
+    @Test
+    public void testOperationExecutionsApiAPiWrapping() {
+        ApiClient c = Mockito.mock(ApiClient.class);
+        class TestClasss extends MsbApiProvider {
+
+            TestClasss(Environment environment) {
+                super(environment);
+            }
+
+            @Override
+            ApiClient buildApiClient() {
+                return c;
+            }
+        }
+        ServiceResourceApi defaultApi = Mockito.mock(ServiceResourceApi.class);
+        when(c.createService(ServiceResourceApi.class)).thenReturn(defaultApi);
+        //verify
+        TestClasss testInstnace = new TestClasss(environment);
+        assertEquals(defaultApi, testInstnace.getMsbApi());
+    }
+
 }