Removing jackson to mitigate cve-2017-4995
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / test / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / vnfm / notification / TestLifecycleChangeNotificationManager.java
index 7121bea..922e92b 100644 (file)
 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.notification;
 
 import com.google.gson.*;
-import com.nokia.cbam.lcm.v32.ApiException;
 import com.nokia.cbam.lcm.v32.model.*;
-import org.joda.time.DateTime;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Optional;
+import java.util.concurrent.*;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.ArgumentCaptor;
@@ -28,11 +31,7 @@ import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.LifecycleManager;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
 import org.threeten.bp.OffsetDateTime;
 
-import java.util.ArrayList;
-import java.util.List;
-import java.util.NoSuchElementException;
-import java.util.Optional;
-import java.util.concurrent.*;
+import static java.util.Optional.empty;
 
 import static com.nokia.cbam.lcm.v32.model.OperationType.*;
 import static junit.framework.TestCase.*;
@@ -42,8 +41,6 @@ import static org.springframework.test.util.ReflectionTestUtils.setField;
 
 public class TestLifecycleChangeNotificationManager extends TestBase {
 
-    public static final String OPERATION_EXECUTION_ID = "myOperationExecutionId";
-
     @InjectMocks
     private LifecycleChangeNotificationManager lifecycleChangeNotificationManager;
     private VnfLifecycleChangeNotification recievedLcn = new VnfLifecycleChangeNotification();
@@ -76,7 +73,7 @@ public class TestLifecycleChangeNotificationManager extends TestBase {
         recievedLcn.setLifecycleOperationOccurrenceId("instantiationOperationExecutionId");
         healOperation.setStartTime(OffsetDateTime.now().plusDays(1));
         recievedLcn.setVnfInstanceId(VNF_ID);
-        when(vnfApi.vnfsVnfInstanceIdOperationExecutionsGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(operationExecutions);
+        when(vnfApi.vnfsVnfInstanceIdOperationExecutionsGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(buildObservable(operationExecutions));
         prepOperation(instantiationOperation);
         prepOperation(scaleOperation);
         prepOperation(healOperation);
@@ -86,8 +83,8 @@ public class TestLifecycleChangeNotificationManager extends TestBase {
         VimInfo vimInfo = new VimInfo();
         vimInfo.setId(VIM_ID);
         instantiateVnfRequest.getVims().add(vimInfo);
-        when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(instantiationOperation.getId(), NOKIA_LCM_API_VERSION)).thenReturn(new Gson().toJsonTree(instantiateVnfRequest));
-        when(vnfApi.vnfsGet(NOKIA_LCM_API_VERSION)).thenReturn(vnfs);
+        when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(instantiationOperation.getId(), NOKIA_LCM_API_VERSION)).thenReturn(buildObservable(new Gson().toJsonTree(instantiateVnfRequest)));
+        when(vnfApi.vnfsGet(NOKIA_LCM_API_VERSION)).thenReturn(buildObservable(vnfs));
         vnfs.add(vnf);
         vnf.setId(VNF_ID);
         VnfProperty prop = new VnfProperty();
@@ -95,18 +92,17 @@ public class TestLifecycleChangeNotificationManager extends TestBase {
         prop.setValue(VNFM_ID);
         vnf.setExtensions(new ArrayList<>());
         vnf.getExtensions().add(prop);
-        when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnf);
+        when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(buildObservable(vnf));
     }
 
-    private void prepOperation(OperationExecution operationExecution) throws ApiException {
-        addEmptyModifiedConnectionPoints(operationExecution);
+    private void prepOperation(OperationExecution operationExecution) {
         JsonElement root = new JsonParser().parse("{ \"additionalParams\" : { \"jobId\" : \"" + JOB_ID + "\"}}");
         operationExecution.setOperationParams(root);
         switch (operationExecution.getOperationType()) {
             case TERMINATE:
                 root.getAsJsonObject().addProperty("terminationType", "GRACEFULL");
         }
-        when(operationExecutionApi.operationExecutionsOperationExecutionIdGet(operationExecution.getId(), NOKIA_LCM_API_VERSION)).thenReturn(operationExecution);
+        when(operationExecutionApi.operationExecutionsOperationExecutionIdGet(operationExecution.getId(), NOKIA_LCM_API_VERSION)).thenReturn(buildObservable(operationExecution));
         operationExecutions.add(operationExecution);
     }
 
@@ -141,7 +137,7 @@ public class TestLifecycleChangeNotificationManager extends TestBase {
      */
     @Test
     public void testInstantiationSufficesTheLastInstantiation() {
-        DateTime baseTime = DateTime.now();
+        OffsetDateTime baseTime = OffsetDateTime.now();
         List<OperationExecution> operations = new ArrayList<>();
 
         OperationExecution operation = buildOperation(OffsetDateTime.now(), INSTANTIATE);
@@ -159,7 +155,7 @@ public class TestLifecycleChangeNotificationManager extends TestBase {
      */
     @Test
     public void testNoInstantiation() {
-        DateTime baseTime = DateTime.now();
+        OffsetDateTime baseTime = OffsetDateTime.now();
         List<OperationExecution> operations = new ArrayList<>();
 
         OperationExecution operation = buildOperation(OffsetDateTime.now(), TERMINATE);
@@ -195,7 +191,7 @@ public class TestLifecycleChangeNotificationManager extends TestBase {
      */
     @Test
     public void testUnableToListVnfs() throws Exception {
-        ApiException expectedException = new ApiException();
+        RuntimeException expectedException = new RuntimeException();
         when(vnfApi.vnfsGet(NOKIA_LCM_API_VERSION)).thenThrow(expectedException);
         //when
         try {
@@ -212,7 +208,7 @@ public class TestLifecycleChangeNotificationManager extends TestBase {
      */
     @Test
     public void testUnableToQueryVnf() throws Exception {
-        ApiException expectedException = new ApiException();
+        RuntimeException expectedException = new RuntimeException();
         when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenThrow(expectedException);
         //when
         try {
@@ -298,13 +294,13 @@ public class TestLifecycleChangeNotificationManager extends TestBase {
     }
 
     /**
-     * if unable to send LCN to VF-C the error is propagated
+     * if unable to query all operation executions from CBAM the error is propagated
      */
     @Test
-    public void testUnableToQueryCurrentOperation() throws Exception {
+    public void testUnableToQueryCurrentOperations() throws Exception {
         recievedLcn.setOperation(OperationType.TERMINATE);
         recievedLcn.setStatus(OperationStatus.FINISHED);
-        ApiException expectedException = new ApiException();
+        RuntimeException expectedException = new RuntimeException();
         when(vnfApi.vnfsVnfInstanceIdOperationExecutionsGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenThrow(expectedException);
         //when
         try {
@@ -313,7 +309,27 @@ public class TestLifecycleChangeNotificationManager extends TestBase {
         } catch (Exception e) {
             //verify
             assertEquals(expectedException, e.getCause());
-            verify(logger).error("Unable to retrieve the current VNF myVnfId", e.getCause());
+            verify(logger).error("Unable to retrieve the operation executions for the VNF myVnfId", e.getCause());
+        }
+    }
+
+    /**
+     * if unable to query the given operation execution from CBAM the error is propagated
+     */
+    @Test
+    public void testUnableToQueryCurrentOperation() throws Exception {
+        recievedLcn.setOperation(OperationType.TERMINATE);
+        recievedLcn.setStatus(OperationStatus.FINISHED);
+        RuntimeException expectedException = new RuntimeException();
+        when(operationExecutionApi.operationExecutionsOperationExecutionIdGet(recievedLcn.getLifecycleOperationOccurrenceId(), NOKIA_LCM_API_VERSION)).thenThrow(expectedException);
+        //when
+        try {
+            lifecycleChangeNotificationManager.handleLcn(recievedLcn);
+            fail();
+        } catch (Exception e) {
+            //verify
+            assertEquals(expectedException, e.getCause());
+            verify(logger).error("Unable to retrieve the operation execution with instantiationOperationExecutionId identifier", e.getCause());
         }
     }
 
@@ -333,6 +349,7 @@ public class TestLifecycleChangeNotificationManager extends TestBase {
         secondTerminationOperationExecution.setOperationParams(buildTerminationParams());
         nonProcessedEvent.setLifecycleOperationOccurrenceId(secondTerminationOperationExecution.getId());
         lifecycleChangeNotificationManager.handleLcn(nonProcessedEvent);
+        addEmptyModifiedConnectionPoints(terminationOperation);
         //add second termination
         recievedLcn.setOperation(OperationType.TERMINATE);
         recievedLcn.setStatus(OperationStatus.FINISHED);
@@ -385,6 +402,7 @@ public class TestLifecycleChangeNotificationManager extends TestBase {
         lifecycleChangeNotificationManager.handleLcn(recievedLcn);
         //verify
         assertTrue(waitExitedWithSuccess.get());
+        assertEquals(empty(), affectedConnectionPoints.getValue());
     }
 
     /**
@@ -427,6 +445,34 @@ public class TestLifecycleChangeNotificationManager extends TestBase {
         verify(logger).warn("The operation failed and the affected connection points were not reported");
     }
 
+
+    /**
+     * affected connection points are passed to the actual notification processor
+     */
+    @Test
+    public void testAffectedConnectionPointProcessing() throws Exception {
+        //given
+        recievedLcn.setOperation(OperationType.INSTANTIATE);
+        recievedLcn.setStatus(OperationStatus.FINISHED);
+        recievedLcn.setLifecycleOperationOccurrenceId(instantiationOperation.getId());
+        instantiationOperation.setStatus(OperationStatus.FAILED);
+        addEmptyModifiedConnectionPoints(instantiationOperation);
+        OperationResult operationResult = new OperationResult();
+        ReportedAffectedConnectionPoints affectedCp = new ReportedAffectedConnectionPoints();
+        ReportedAffectedCp cp = new ReportedAffectedCp();
+        cp.setCpId("cpId");
+        affectedCp.getPost().add(cp);
+        operationResult.operationResult = affectedCp;
+        instantiationOperation.setAdditionalData(new Gson().toJsonTree(operationResult));
+
+        //when
+        lifecycleChangeNotificationManager.handleLcn(recievedLcn);
+        //verify
+        assertTrue(affectedConnectionPoints.getValue().isPresent());
+        ReportedAffectedConnectionPoints actualCps = new Gson().fromJson(new Gson().toJsonTree(affectedConnectionPoints.getValue().get()), ReportedAffectedConnectionPoints.class);
+        assertEquals(1, actualCps.getPost().size());
+    }
+
     /**
      * Failures in affected connection point processing are tolerated for failed operation
      * (because the POST script was not able to run)
@@ -438,6 +484,7 @@ public class TestLifecycleChangeNotificationManager extends TestBase {
         recievedLcn.setStatus(OperationStatus.FAILED);
         recievedLcn.setLifecycleOperationOccurrenceId(instantiationOperation.getId());
         instantiationOperation.setStatus(OperationStatus.FAILED);
+        addEmptyModifiedConnectionPoints(instantiationOperation);
         JsonObject additionalData = (JsonObject) instantiationOperation.getAdditionalData();
         additionalData.remove("operationResult");
         //when
@@ -484,8 +531,10 @@ public class TestLifecycleChangeNotificationManager extends TestBase {
         request.setType(ScaleDirection.OUT);
         scaleOperation.setOperationParams(request);
         scaleOperation.setStatus(OperationStatus.FAILED);
+        addEmptyModifiedConnectionPoints(scaleOperation);
         ((JsonObject) scaleOperation.getAdditionalData()).get("operationResult").getAsJsonObject().remove("cbam_post");
         scaleOperation.setOperationType(OperationType.SCALE);
+
         //when
         lifecycleChangeNotificationManager.handleLcn(recievedLcn);
         assertFalse(affectedConnectionPoints.getValue().isPresent());
@@ -506,6 +555,7 @@ public class TestLifecycleChangeNotificationManager extends TestBase {
         request.setType(ScaleDirection.OUT);
         scaleOperation.setOperationParams(request);
         scaleOperation.setStatus(OperationStatus.FAILED);
+        addEmptyModifiedConnectionPoints(scaleOperation);
         JsonObject operationResult = ((JsonObject) scaleOperation.getAdditionalData()).get("operationResult").getAsJsonObject();
         operationResult.remove("cbam_post");
         operationResult.addProperty("cbam_post", "");