Fix sonar issues
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / test / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / vnfm / TestBase.java
index 7a1bf7f..a699ee9 100644 (file)
@@ -21,6 +21,22 @@ import com.nokia.cbam.catalog.v1.api.DefaultApi;
 import com.nokia.cbam.lcm.v32.api.OperationExecutionsApi;
 import com.nokia.cbam.lcm.v32.api.VnfsApi;
 import com.nokia.cbam.lcn.v32.api.SubscriptionsApi;
+import io.reactivex.Observable;
+import io.reactivex.internal.operators.observable.ObservableFromCallable;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.lang.reflect.Field;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.Callable;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+import javax.servlet.http.HttpServletResponse;
+import junit.framework.TestCase;
+import okhttp3.RequestBody;
+import okio.Buffer;
 import org.apache.commons.lang3.ArrayUtils;
 import org.apache.http.HttpEntity;
 import org.apache.http.client.methods.CloseableHttpResponse;
@@ -31,12 +47,14 @@ import org.junit.After;
 import org.junit.Before;
 import org.mockito.ArgumentCaptor;
 import org.mockito.Mock;
+import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
-import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;
+import org.onap.msb.api.ServiceResourceApi;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.INotificationSender;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.VnfmInfoProvider;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.MsbApiProvider;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager;
+import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.AaiSecurityProvider;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.vfc.VfcRestApiProvider;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions;
 import org.onap.vfccatalog.api.VnfpackageApi;
@@ -45,30 +63,25 @@ import org.slf4j.Logger;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.core.env.Environment;
 import org.springframework.test.util.ReflectionTestUtils;
-
-import javax.servlet.http.HttpServletResponse;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.lang.reflect.Field;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipInputStream;
+import retrofit2.Call;
+import retrofit2.Response;
 
 import static junit.framework.TestCase.assertEquals;
 import static junit.framework.TestCase.assertTrue;
 import static org.mockito.Mockito.when;
 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CatalogManager.getFileInZip;
 
+
 public class TestBase {
+
     public static final String VNF_ID = "myVnfId";
     public static final String VNFM_ID = "myVnfmId";
     public static final String ONAP_CSAR_ID = "myOnapCsarId";
     public static final String VIM_ID = "myCloudOwnerId_myRegionName";
     public static final String JOB_ID = "myJobId";
     public static final String CBAM_VNFD_ID = "cbamVnfdId";
+    protected static Call<Void> VOID_CALL = buildCall(null);
+    protected static VoidObservable VOID_OBSERVABLE = new VoidObservable();
     @Mock
     protected CbamRestApiProvider cbamRestApiProvider;
     @Mock
@@ -76,6 +89,8 @@ public class TestBase {
     @Mock
     protected MsbApiProvider msbApiProvider;
     @Mock
+    protected AaiSecurityProvider aaiSecurityProvider;
+    @Mock
     protected VnfmInfoProvider vnfmInfoProvider;
     @Mock
     protected VnfsApi vnfApi;
@@ -88,7 +103,7 @@ public class TestBase {
     @Mock
     protected SubscriptionsApi lcnApi;
     @Mock
-    protected MSBServiceClient msbClient;
+    protected ServiceResourceApi msbClient;
     @Mock
     protected DriverProperties driverProperties;
     @Mock
@@ -113,6 +128,20 @@ public class TestBase {
     @Mock
     protected Environment environment;
 
+    protected static <T> Call<T> buildCall(T response) {
+        Call<T> call = Mockito.mock(Call.class);
+        try {
+            when(call.execute()).thenReturn(Response.success(response));
+        } catch (Exception e) {
+            throw new RuntimeException();
+        }
+        return call;
+    }
+
+    protected static <T> Observable<T> buildObservable(T response) {
+        return Observable.just(response);
+    }
+
     @Before
     public void genericSetup() throws Exception {
         MockitoAnnotations.initMocks(this);
@@ -121,9 +150,9 @@ public class TestBase {
         when(cbamRestApiProvider.getCbamOperationExecutionApi(VNFM_ID)).thenReturn(operationExecutionApi);
         when(cbamRestApiProvider.getCbamLcnApi(VNFM_ID)).thenReturn(lcnApi);
         when(cbamRestApiProvider.getCbamCatalogApi(VNFM_ID)).thenReturn(cbamCatalogApi);
-        when(msbApiProvider.getMsbClient()).thenReturn(msbClient);
+        when(msbApiProvider.getMsbApi()).thenReturn(msbClient);
         when(vfcRestApiProvider.getNsLcmApi()).thenReturn(nsLcmApi);
-        when(vfcRestApiProvider.getOnapCatalogApi()).thenReturn(vfcCatalogApi);
+        when(vfcRestApiProvider.getVfcCatalogApi()).thenReturn(vfcCatalogApi);
         when(systemFunctions.getHttpClient()).thenReturn(httpClient);
         when(httpClient.execute(request.capture())).thenReturn(response);
         when(response.getEntity()).thenReturn(entity);
@@ -148,6 +177,18 @@ public class TestBase {
         assertEquals(build(expected), build(actual));
     }
 
+    byte[] getContent(RequestBody requestBody) {
+        try {
+            Buffer buffer = new Buffer();
+            requestBody.writeTo(buffer);
+            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+            buffer.copyTo(byteArrayOutputStream);
+            return byteArrayOutputStream.toByteArray();
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
     private Map<String, List<Byte>> build(byte[] zip) throws Exception {
         Map<String, List<Byte>> files = new HashMap<>();
         ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(zip));
@@ -161,7 +202,7 @@ public class TestBase {
         return files;
     }
 
-    protected void setFieldWithPropertyAnnotation(Object obj, String key, String value) {
+    protected void setFieldWithPropertyAnnotation(Object obj, String key, Object value) {
         for (Field field : obj.getClass().getDeclaredFields()) {
             for (Value fieldValue : field.getAnnotationsByType(Value.class)) {
                 if (fieldValue.value().equals(key)) {
@@ -175,4 +216,23 @@ public class TestBase {
             }
         }
     }
+
+    protected static class VoidObservable {
+        boolean called = false;
+        ObservableFromCallable<Void> s = new ObservableFromCallable(new Callable() {
+            @Override
+            public Object call() throws Exception {
+                called = true;
+                return "";
+            }
+        });
+
+        public void assertCalled() {
+            TestCase.assertTrue(called);
+        }
+
+        public Observable<Void> value() {
+            return s;
+        }
+    }
 }