Fix sonar issues
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / test / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / vnfm / TestSelfRegistrationManager.java
index 79ec1a8..640d29b 100644 (file)
 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm;
 
 import com.google.common.collect.Lists;
-import com.nokia.cbam.lcn.v32.ApiException;
 import com.nokia.cbam.lcn.v32.model.CreateSubscriptionRequest;
 import com.nokia.cbam.lcn.v32.model.Subscription;
 import com.nokia.cbam.lcn.v32.model.SubscriptionAuthentication;
 import com.nokia.cbam.lcn.v32.model.VnfNotificationType;
+import io.reactivex.Observable;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.UUID;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.*;
 import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
-import org.onap.msb.sdk.discovery.common.RouteException;
-import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
-import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;
-import org.onap.msb.sdk.discovery.entity.Node;
-import org.onap.msb.sdk.discovery.entity.RouteResult;
+import org.onap.msb.model.MicroServiceFullInfo;
+import org.onap.msb.model.MicroServiceInfo;
+import org.onap.msb.model.Node;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil;
 
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-import java.util.UUID;
-
 import static com.nokia.cbam.lcn.v32.model.OperationType.*;
 import static junit.framework.TestCase.*;
 import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.*;
+import static org.onap.msb.model.MicroServiceInfo.ProtocolEnum.REST;
+import static org.onap.msb.model.MicroServiceInfo.VisualRangeEnum._1;
+import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager.DRIVER_VERSION;
+import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager.SERVICE_NAME;
 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider.NOKIA_LCN_API_VERSION;
 import static org.springframework.test.util.ReflectionTestUtils.setField;
 
 public class TestSelfRegistrationManager extends TestBase {
     @Mock
     private JobManager jobManager;
-    private List<Subscription> subscriptions = new ArrayList<>();
+    private java.util.List<Subscription> subscriptions = new ArrayList<>();
     private ArgumentCaptor<MicroServiceInfo> registeredMicroservice = ArgumentCaptor.forClass(MicroServiceInfo.class);
     private ArgumentCaptor<CreateSubscriptionRequest> subscriptionToCreate = ArgumentCaptor.forClass(CreateSubscriptionRequest.class);
     @InjectMocks
@@ -58,7 +58,7 @@ public class TestSelfRegistrationManager extends TestBase {
     @Before
     public void initMocks() throws Exception {
         setField(SelfRegistrationManager.class, "logger", logger);
-        when(lcnApi.subscriptionsGet(NOKIA_LCN_API_VERSION)).thenReturn(subscriptions);
+        when(lcnApi.subscriptionsGet(NOKIA_LCN_API_VERSION)).thenReturn(buildObservable(subscriptions));
         when(driverProperties.getVnfmId()).thenReturn(VNFM_ID);
         setField(selfRegistrationManager, "driverMsbExternalIp", "1.2.3.4");
         setField(selfRegistrationManager, "driverVnfmExternalIp", "5.6.7.8");
@@ -78,15 +78,15 @@ public class TestSelfRegistrationManager extends TestBase {
     public void testRegistration() throws Exception {
         //given
         Subscription subscription = new Subscription();
-        when(lcnApi.subscriptionsPost(subscriptionToCreate.capture(), Mockito.eq(NOKIA_LCN_API_VERSION))).thenReturn(subscription);
+        when(lcnApi.subscriptionsPost(subscriptionToCreate.capture(), Mockito.eq(NOKIA_LCN_API_VERSION))).thenReturn(buildObservable(subscription));
         MicroServiceFullInfo returnedMicroService = new MicroServiceFullInfo();
-        when(msbClient.registerMicroServiceInfo(registeredMicroservice.capture())).thenReturn(returnedMicroService);
+        when(msbClient.addMicroService(registeredMicroservice.capture(), eq(true), eq(false))).thenReturn(buildObservable(returnedMicroService));
         //when
         selfRegistrationManager.register();
         //verify
         InOrder registrationOrder = Mockito.inOrder(lcnApi, msbClient);
         registrationOrder.verify(lcnApi).subscriptionsPost(any(), any());
-        registrationOrder.verify(msbClient).registerMicroServiceInfo(any());
+        registrationOrder.verify(msbClient).addMicroService(any(), any(), any());
 
         assertMicroserviceRegistered();
         assertNewLcnSubscription();
@@ -112,6 +112,7 @@ public class TestSelfRegistrationManager extends TestBase {
         assertTrue(subscriptionCreation.getFilter().getOperationTypes().contains(TERMINATE));
         assertTrue(subscriptionCreation.getFilter().getOperationTypes().contains(INSTANTIATE));
         assertEquals(4, subscriptionCreation.getFilter().getOperationTypes().size());
+        verify(logger).info("Subscribing to CBAM LCN {} with callback to {}", driverProperties.getCbamLcnUrl(), "http://5.6.7.8:12345/api/NokiaSVNFM/v1/lcn");
     }
 
     private void assertMicroserviceRegistered() {
@@ -121,15 +122,15 @@ public class TestSelfRegistrationManager extends TestBase {
         assertEquals("0", node.getTtl());
         assertEquals("1.2.3.4", node.getIp());
         assertEquals("12345", node.getPort());
-        assertEquals("REST", microserviceRequest.getProtocol());
+        assertEquals(REST, microserviceRequest.getProtocol());
         assertNull(microserviceRequest.getMetadata());
         //very strange, but it should be null for ONAP to work
-        assertEquals("", microserviceRequest.getPath());
-        assertEquals(SelfRegistrationManager.SERVICE_NAME, microserviceRequest.getServiceName());
+        assertEquals(SERVICE_NAME, microserviceRequest.getServiceName());
         assertEquals("/api/NokiaSVNFM/v1", microserviceRequest.getUrl());
         assertEquals("v1", microserviceRequest.getVersion());
+        assertEquals(Boolean.FALSE, microserviceRequest.isEnableSsl());
         //1 means internal service to ONAP
-        assertEquals("1", microserviceRequest.getVisualRange());
+        assertEquals(_1, microserviceRequest.getVisualRange());
     }
 
     /**
@@ -139,7 +140,7 @@ public class TestSelfRegistrationManager extends TestBase {
     public void testResubscription() throws Exception {
         //given
         MicroServiceFullInfo returnedMicroService = new MicroServiceFullInfo();
-        when(msbClient.registerMicroServiceInfo(registeredMicroservice.capture())).thenReturn(returnedMicroService);
+        when(msbClient.addMicroService(registeredMicroservice.capture(), eq(true), eq(false))).thenReturn(buildObservable(returnedMicroService));
         Subscription existingSubscription = new Subscription();
         existingSubscription.setId(UUID.randomUUID().toString());
         existingSubscription.setCallbackUrl("http://5.6.7.8:12345/api/NokiaSVNFM/v1/lcn");
@@ -158,7 +159,7 @@ public class TestSelfRegistrationManager extends TestBase {
     @Test
     public void testFailedLcnSubscription() throws Exception {
         //given
-        ApiException expectedException = new ApiException();
+        RuntimeException expectedException = new RuntimeException();
         when(lcnApi.subscriptionsPost(any(), any())).thenThrow(expectedException);
         //when
         try {
@@ -168,7 +169,7 @@ public class TestSelfRegistrationManager extends TestBase {
             assertEquals(expectedException, e.getCause());
         }
         //verify
-        verify(msbClient, never()).registerMicroServiceInfo(any());
+        verify(msbClient, never()).addMicroService(any(), any(), any());
         verify(logger).error("Unable to subscribe to CBAM LCN", expectedException);
         assertServiceDown();
     }
@@ -180,18 +181,16 @@ public class TestSelfRegistrationManager extends TestBase {
     public void testFailedMsbPublish() throws Exception {
         //given
         Subscription subscription = new Subscription();
-        when(lcnApi.subscriptionsPost(subscriptionToCreate.capture(), Mockito.eq(NOKIA_LCN_API_VERSION))).thenAnswer(new Answer<Subscription>() {
-            @Override
-            public Subscription answer(InvocationOnMock invocationOnMock) throws Throwable {
-                subscription.setCallbackUrl("http://5.6.7.8:12345/api/NokiaSVNFM/v1/lcn");
-                subscription.setId(UUID.randomUUID().toString());
-                subscriptions.add(subscription);
-                return subscription;
-            }
+        when(lcnApi.subscriptionsPost(subscriptionToCreate.capture(), Mockito.eq(NOKIA_LCN_API_VERSION))).thenAnswer(invocationOnMock -> {
+            subscription.setCallbackUrl("http://5.6.7.8:12345/api/NokiaSVNFM/v1/lcn");
+            subscription.setId(UUID.randomUUID().toString());
+            subscriptions.add(subscription);
+            when(lcnApi.subscriptionsSubscriptionIdDelete(subscription.getId(), NOKIA_LCN_API_VERSION)).thenReturn(VOID_OBSERVABLE.value());
+            return buildObservable(subscription);
         });
         MicroServiceFullInfo returnedMicroService = new MicroServiceFullInfo();
-        RouteException expectedException = new RouteException();
-        when(msbClient.registerMicroServiceInfo(registeredMicroservice.capture())).thenThrow(expectedException);
+        RuntimeException expectedException = new RuntimeException();
+        when(msbClient.addMicroService(registeredMicroservice.capture(), eq(true), eq(false))).thenThrow(expectedException);
         //when
         try {
             selfRegistrationManager.register();
@@ -219,14 +218,19 @@ public class TestSelfRegistrationManager extends TestBase {
         subscription.setId(UUID.randomUUID().toString());
         subscriptions.add(subscription);
         when(jobManager.hasOngoingJobs()).thenReturn(false);
-        MicroServiceFullInfo returnedMicroService = new MicroServiceFullInfo();
+        when(lcnApi.subscriptionsSubscriptionIdDelete(subscription.getId(), NOKIA_LCN_API_VERSION)).thenReturn(VOID_OBSERVABLE.value());
+        VoidObservable MSB_DELETE = new VoidObservable();
+        when(msbClient.deleteMicroService(SERVICE_NAME, DRIVER_VERSION, null, null)).thenReturn(MSB_DELETE.value());
         //when
         selfRegistrationManager.deRegister();
         //verify
         InOrder inOrder = Mockito.inOrder(jobManager, msbClient, lcnApi);
-        inOrder.verify(msbClient).cancelMicroServiceInfo(SelfRegistrationManager.SERVICE_NAME, SelfRegistrationManager.DRIVER_VERSION);
+        inOrder.verify(msbClient).deleteMicroService(SERVICE_NAME, DRIVER_VERSION, null, null);
         inOrder.verify(lcnApi).subscriptionsSubscriptionIdDelete(subscription.getId(), NOKIA_LCN_API_VERSION);
         assertServiceDown();
+        VOID_OBSERVABLE.assertCalled();
+        MSB_DELETE.assertCalled();
+        verify(logger).info("Deleting subscription with {} identifier", subscription.getId());
     }
 
     /**
@@ -241,22 +245,23 @@ public class TestSelfRegistrationManager extends TestBase {
         subscription.setId(UUID.randomUUID().toString());
         subscriptions.add(subscription);
         when(jobManager.hasOngoingJobs()).thenReturn(false);
-        when(msbClient.cancelMicroServiceInfo(SelfRegistrationManager.SERVICE_NAME, SelfRegistrationManager.DRIVER_VERSION)).then(new Answer<RouteResult>() {
+        when(msbClient.deleteMicroService(SERVICE_NAME, DRIVER_VERSION, null, null)).then(new Answer<Observable<Void>>() {
             @Override
-            public RouteResult answer(InvocationOnMock invocationOnMock) throws Throwable {
-                when(msbClient.queryMicroServiceInfo(SelfRegistrationManager.SERVICE_NAME, SelfRegistrationManager.DRIVER_VERSION)).thenThrow(new RouteException());
-                throw new RouteException();
+            public Observable<Void> answer(InvocationOnMock invocationOnMock) throws Throwable {
+                when(msbClient.getMicroService_0(SERVICE_NAME, DRIVER_VERSION, null, null, null, null, null)).thenThrow(new RuntimeException());
+                throw new RuntimeException();
             }
         });
-        MicroServiceFullInfo returnedMicroService = new MicroServiceFullInfo();
+        when(lcnApi.subscriptionsSubscriptionIdDelete(subscription.getId(), NOKIA_LCN_API_VERSION)).thenReturn(VOID_OBSERVABLE.value());
         //when
         selfRegistrationManager.deRegister();
         //verify
         InOrder inOrder = Mockito.inOrder(jobManager, msbClient, lcnApi);
-        inOrder.verify(msbClient).cancelMicroServiceInfo(SelfRegistrationManager.SERVICE_NAME, SelfRegistrationManager.DRIVER_VERSION);
-        inOrder.verify(msbClient).queryMicroServiceInfo(SelfRegistrationManager.SERVICE_NAME, SelfRegistrationManager.DRIVER_VERSION);
+        inOrder.verify(msbClient).deleteMicroService(SERVICE_NAME, DRIVER_VERSION, null, null);
+        inOrder.verify(msbClient).getMicroService_0(SERVICE_NAME, DRIVER_VERSION, null, null, null, null, null);
         inOrder.verify(lcnApi).subscriptionsSubscriptionIdDelete(subscription.getId(), NOKIA_LCN_API_VERSION);
         assertServiceDown();
+        VOID_OBSERVABLE.assertCalled();
     }
 
     /**
@@ -269,13 +274,12 @@ public class TestSelfRegistrationManager extends TestBase {
         subscription.setCallbackUrl("http://5.6.7.8:12345/api/NokiaSVNFM/v1/lcn");
         subscription.setId(UUID.randomUUID().toString());
         subscriptions.add(subscription);
-        when(msbClient.cancelMicroServiceInfo(SelfRegistrationManager.SERVICE_NAME, SelfRegistrationManager.DRIVER_VERSION)).then(new Answer<RouteResult>() {
+        when(msbClient.deleteMicroService(SERVICE_NAME, DRIVER_VERSION, null, null)).then(new Answer<Observable<Void>>() {
             @Override
-            public RouteResult answer(InvocationOnMock invocationOnMock) throws Throwable {
-                throw new RouteException();
+            public Observable<Void> answer(InvocationOnMock invocationOnMock) throws Throwable {
+                throw new RuntimeException();
             }
         });
-        MicroServiceFullInfo returnedMicroService = new MicroServiceFullInfo();
         //when
         try {
             selfRegistrationManager.deRegister();
@@ -285,8 +289,8 @@ public class TestSelfRegistrationManager extends TestBase {
         }
         //verify
         InOrder inOrder = Mockito.inOrder(jobManager, msbClient, lcnApi);
-        inOrder.verify(msbClient).cancelMicroServiceInfo(SelfRegistrationManager.SERVICE_NAME, SelfRegistrationManager.DRIVER_VERSION);
-        inOrder.verify(msbClient).queryMicroServiceInfo(SelfRegistrationManager.SERVICE_NAME, SelfRegistrationManager.DRIVER_VERSION);
+        inOrder.verify(msbClient).deleteMicroService(SERVICE_NAME, DRIVER_VERSION, null, null);
+        inOrder.verify(msbClient).getMicroService_0(SERVICE_NAME, DRIVER_VERSION, null, null, null, null, null);
         verify(lcnApi, Mockito.never()).subscriptionsSubscriptionIdDelete(subscription.getId(), NOKIA_LCN_API_VERSION);
         assertServiceDown();
     }
@@ -302,8 +306,9 @@ public class TestSelfRegistrationManager extends TestBase {
         subscription.setId(UUID.randomUUID().toString());
         subscriptions.add(subscription);
         when(jobManager.hasOngoingJobs()).thenReturn(false);
-        ApiException expectedException = new ApiException();
+        RuntimeException expectedException = new RuntimeException();
         doThrow(expectedException).when(lcnApi).subscriptionsSubscriptionIdDelete(subscription.getId(), NOKIA_LCN_API_VERSION);
+        when(msbClient.deleteMicroService(SERVICE_NAME, DRIVER_VERSION, null, null)).thenReturn(VOID_OBSERVABLE.value());
         //when
         try {
             selfRegistrationManager.deRegister();
@@ -313,7 +318,7 @@ public class TestSelfRegistrationManager extends TestBase {
         }
         //verify
         InOrder inOrder = Mockito.inOrder(jobManager, msbClient, lcnApi);
-        inOrder.verify(msbClient).cancelMicroServiceInfo(SelfRegistrationManager.SERVICE_NAME, SelfRegistrationManager.DRIVER_VERSION);
+        inOrder.verify(msbClient).deleteMicroService(SERVICE_NAME, DRIVER_VERSION, null, null);
         inOrder.verify(lcnApi).subscriptionsSubscriptionIdDelete(subscription.getId(), NOKIA_LCN_API_VERSION);
         assertServiceDown();
     }