Implant vid-app-common org.onap.vid.job (main and test)
[vid.git] / vid-app-common / src / test / java / org / onap / vid / job / command / ServiceInProgressStatusCommandTest.java
index 31dbc9f..787ff60 100644 (file)
@@ -7,9 +7,9 @@
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
 package org.onap.vid.job.command;
 
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import org.mockito.ArgumentCaptor;
-import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
-import org.onap.portalsdk.core.util.SystemProperties;
 import org.onap.vid.job.*;
 import org.onap.vid.job.impl.JobSharedData;
+import org.onap.vid.model.Action;
 import org.onap.vid.model.serviceInstantiation.Network;
 import org.onap.vid.model.serviceInstantiation.ServiceInstantiation;
 import org.onap.vid.model.serviceInstantiation.Vnf;
-import org.onap.vid.mso.model.ModelInfo;
+import org.onap.vid.mso.RestMsoImplementation;
 import org.onap.vid.properties.Features;
 import org.onap.vid.properties.VidProperties;
 import org.onap.vid.services.AsyncInstantiationBusinessLogic;
-import org.springframework.core.env.Environment;
+import org.onap.vid.services.AuditService;
 import org.testng.Assert;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.DataProvider;
@@ -47,10 +47,9 @@ import java.time.ZoneOffset;
 import java.time.ZonedDateTime;
 import java.time.temporal.ChronoUnit;
 import java.util.Arrays;
-import java.util.Map;
-import java.util.TreeMap;
 import java.util.UUID;
 
+import static java.util.Collections.emptyList;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.containsInAnyOrder;
 import static org.hamcrest.core.Is.is;
@@ -58,7 +57,11 @@ import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.*;
 import static org.onap.vid.job.Job.JobStatus.*;
+import static org.onap.vid.job.command.ResourceCommandKt.*;
+import static org.onap.vid.job.command.ResourceCommandTest.FakeResourceCreator.*;
+import static org.onap.vid.model.Action.Create;
 import static org.onap.vid.testUtils.TestUtils.testWithSystemProperty;
+import static org.testng.AssertJUnit.assertEquals;
 
 public class ServiceInProgressStatusCommandTest {
 
@@ -78,45 +81,29 @@ public class ServiceInProgressStatusCommandTest {
     @Mock
     private JobSharedData sharedData;
 
-    @Mock
-    private Environment environment;
-
     @Mock
     private ServiceInstantiation request;
 
     @Mock
     private InProgressStatusService inProgressStatusService;
 
-    @InjectMocks
-    private ServiceInProgressStatusCommand command = new ServiceInProgressStatusCommand();
+    @Mock
+    private WatchChildrenJobsBL watchChildrenJobsBL;
 
-    @DataProvider
-    public static Object[][] isNeedToCreateChildJobsDataProvider() {
-        return new Object[][]{
-                {new TreeMap<String,Vnf>() ,                 true, true, false},
-                {null ,                                      true, true, false},
-                {ImmutableMap.of("a",mock(Vnf.class)),   false, true, false},
-                {ImmutableMap.of("a",mock(Vnf.class)),   true, false, false},
-                {ImmutableMap.of("a",mock(Vnf.class)),   true, true, true},
-        };
-    }
+    @Mock
+    private MsoResultHandlerService msoResultHandlerService;
+
+    @Mock
+    private MsoRequestBuilder msoRequestBuilder;
+
+    @Mock
+    private RestMsoImplementation restMsoImplementation;
+
+    @Mock
+    private AuditService auditService;
+
+    private ALaCarteServiceCommand command;
 
-    @DataProvider
-    public static Object[][] processJobStatusData() {
-        return new Object[][]{
-                /* {MSO jobStatus, jobStartTime, isNeedToCreateChildJobs(), property vid.job.max.hoursInProgress, expected nextCommand.getStatus() } */
-                {IN_PROGRESS,           false, IN_PROGRESS},
-                {FAILED,                false, FAILED},
-                {PAUSE,                 false, IN_PROGRESS},
-                {COMPLETED,             false, COMPLETED},
-                {COMPLETED,             true,  IN_PROGRESS},
-                {RESOURCE_IN_PROGRESS,  false, RESOURCE_IN_PROGRESS},
-                {PENDING,               false, PENDING},
-                {STOPPED,               false, STOPPED},
-                {COMPLETED_WITH_ERRORS, false, COMPLETED_WITH_ERRORS},
-                {CREATING,              false, CREATING}
-        };
-    }
 
     @DataProvider
     public static Object[][] isExpiredJobStatusData() {
@@ -138,32 +125,31 @@ public class ServiceInProgressStatusCommandTest {
     @BeforeMethod
     public void initMocks() {
         MockitoAnnotations.initMocks(this);
-    }
-
-    @Test(dataProvider = "isNeedToCreateChildJobsDataProvider" )
-    public void testIsNeedToCreateChildJobs(Map<String, Vnf> serviceVnfs, boolean isALaCarte,
-                                            boolean isFeatureEnabled, boolean expected) {
-        MockitoAnnotations.initMocks(this);
-        ServiceInstantiation serviceInstantiation = mock(ServiceInstantiation.class);
-        when(serviceInstantiation.getVnfs()).thenReturn(serviceVnfs);
-        when(serviceInstantiation.isALaCarte()).thenReturn(isALaCarte);
-        when(featureManager.isActive(Features.FLAG_ASYNC_ALACARTE_VNF)).thenReturn(isFeatureEnabled);
-        assertThat(command.isNeedToCreateChildJobs(serviceInstantiation), is(expected));
+        command = new ALaCarteServiceCommand(
+                inProgressStatusService,
+                watchChildrenJobsBL,
+                asyncInstantiationBL,
+                jobsBrokerService,
+                msoRequestBuilder,
+                msoResultHandlerService,
+                jobAdapter,
+                restMsoImplementation,
+                auditService
+        );
     }
 
     @Test
     public void whenGetFromMsoCompletedAndALaCarte_generateNewJobsForVnfs() {
         UUID uuid = UUID.randomUUID();
         String userId = "mockedUserID";
-        Vnf vnf1 = mock(Vnf.class);
-        Vnf vnf2 = mock(Vnf.class);
-        Network network1 = mock(Network.class);
-        ServiceInstantiation serviceInstantiation = mock(ServiceInstantiation.class);
-        when(serviceInstantiation.getVnfs()).thenReturn(ImmutableMap.of("a", vnf1, "b", vnf2));
-        when(serviceInstantiation.getNetworks()).thenReturn(ImmutableMap.of("c", network1));
-        when(serviceInstantiation.isALaCarte()).thenReturn(true);
-        when(serviceInstantiation.getModelInfo()).thenReturn(new ModelInfo());
-
+        String testApi = "VNF_API";
+        Vnf vnf1 = createVnf(emptyList(), Create);
+        Vnf vnf2 = createVnf(emptyList(), Create);
+        Network network = createNetwork(Create);
+        ServiceInstantiation serviceInstantiation = createService(
+                ImmutableList.of(vnf1, vnf2),
+                ImmutableList.of(network),
+                emptyList());
         when(featureManager.isActive(Features.FLAG_ASYNC_ALACARTE_VNF)).thenReturn(true);
 
         UUID uuid1 = UUID.fromString("12345678-1234-1234-1234-123456789012");
@@ -171,15 +157,25 @@ public class ServiceInProgressStatusCommandTest {
         UUID uuid3 = UUID.fromString("12345678-1234-1234-1234-123456789014");
         when(jobsBrokerService.add(any())).thenReturn(uuid1).thenReturn(uuid2).thenReturn(uuid3);
 
-        JobSharedData sharedData = new JobSharedData(uuid, userId, serviceInstantiation);
-        command.init(sharedData, "", "");
+        JobSharedData sharedData = new JobSharedData(uuid, userId, serviceInstantiation, testApi);
+        command.init(sharedData, ImmutableMap.of(
+                ACTION_PHASE, Action.Create.name(),
+                INTERNAL_STATE, InternalState.IN_PROGRESS.name()
+                ));
         when(inProgressStatusService.call(any(), eq(sharedData), any())).thenReturn(Job.JobStatus.COMPLETED);
+        when(watchChildrenJobsBL.cumulateJobStatus(Job.JobStatus.COMPLETED, COMPLETED_WITH_NO_ACTION)).thenReturn(COMPLETED);
+        when(watchChildrenJobsBL.cumulateJobStatus(Job.JobStatus.COMPLETED_WITH_NO_ACTION, COMPLETED)).thenReturn(COMPLETED);
+        when(msoResultHandlerService.getRequest(eq(sharedData))).thenReturn(serviceInstantiation);
         NextCommand nextCommand = command.call();
+        assertEquals(IN_PROGRESS,  nextCommand.getStatus());
+        nextCommand = nextCommand.getCommand().call();
 
         ArgumentCaptor<JobAdapter.AsyncJobRequest> argumentCaptor = ArgumentCaptor.forClass(JobAdapter.AsyncJobRequest.class);
-        verify(jobAdapter, times(2)).createChildJob(eq(JobType.VnfInstantiation), eq(Job.JobStatus.CREATING), argumentCaptor.capture(), eq(sharedData), any());
-        verify(jobAdapter, times(1)).createChildJob(eq(JobType.NetworkInstantiation), eq(Job.JobStatus.CREATING), argumentCaptor.capture(), eq(sharedData), any());
-        assertThat(argumentCaptor.getAllValues(), containsInAnyOrder(vnf1, vnf2, network1));
+        verify(jobAdapter, times(1)).createChildJob(eq(JobType.NetworkInstantiation), argumentCaptor.capture(), eq(sharedData), any(), eq(0));
+        verify(jobAdapter, times(1)).createChildJob(eq(JobType.VnfInstantiation), argumentCaptor.capture(), eq(sharedData), any(), eq(1));
+        verify(jobAdapter, times(1)).createChildJob(eq(JobType.VnfInstantiation), argumentCaptor.capture(), eq(sharedData), any(), eq(2));
+
+        assertThat(argumentCaptor.getAllValues(), containsInAnyOrder(vnf1, vnf2, network));
 
         verify(jobsBrokerService, times(3)).add(any());
 
@@ -187,41 +183,14 @@ public class ServiceInProgressStatusCommandTest {
         verify(asyncInstantiationBL, never()).updateServiceInfo(any(), any());
 
         assertThat(nextCommand.getStatus(), is(Job.JobStatus.IN_PROGRESS));
-        assertThat(nextCommand.getCommand().getType(), is(new WatchingCommand().getType()));
-        assertThat(nextCommand.getCommand().getData().get("childrenJobs"), is(Arrays.asList(uuid1.toString(), uuid2.toString(), uuid3.toString())));
-        assertThat(nextCommand.getCommand().getData().get("isService"), is(true));
-    }
-
-    @Test(dataProvider = "processJobStatusData")
-    public void processJobStatusTest(Job.JobStatus jobStatus, boolean isNeedToCreateChildJobs, Job.JobStatus expectedStatus) {
-
-        when(featureManager.isActive(Features.FLAG_ASYNC_ALACARTE_VNF)).thenReturn(true);
-        // All mocks under are used for isNeedToCreateChildJobs=true case
-        when(sharedData.getRequest()).thenReturn(request);
-        when(request.isALaCarte()).thenReturn(true);
-        Map vnfs = mock(Map.class);
-        ModelInfo modelInfo = mock(ModelInfo.class);
-      
-        // if vnfs.isEmpty -> isNeedToCreateChildJobs will return false
-        when(vnfs.isEmpty()).thenReturn(!isNeedToCreateChildJobs);
-      
-        when(request.getVnfs()).thenReturn(vnfs);
-        when(request.getModelInfo()).thenReturn(modelInfo);
-        command.instanceId = "MockInstId";
-
-        NextCommand nextCommand = command.processJobStatus(jobStatus);
-        Assert.assertEquals(nextCommand.getStatus(), expectedStatus);
-        if (isNeedToCreateChildJobs) {
-            Assert.assertEquals(nextCommand.getCommand().getClass(), WatchingCommand.class);
-        } else {
-            Assert.assertEquals(nextCommand.getCommand(), command);
-        }
+        assertThat(nextCommand.getCommand().getType(), is(JobType.ALaCarteService));
+        assertThat(nextCommand.getCommand().getData().get(CHILD_JOBS), is(Arrays.asList(uuid1.toString(), uuid2.toString(), uuid3.toString())));
     }
 
     @Test(dataProvider = "isExpiredJobStatusData")
     public void isExpiredJobStatusTest(ZonedDateTime jobStartTime, String configValue, boolean expectedResult) throws Exception {
         testWithSystemProperty(VidProperties.VID_JOB_MAX_HOURS_IN_PROGRESS, configValue, ()->
-            Assert.assertEquals(command.getExpiryChecker().isExpired(jobStartTime), expectedResult)
+                Assert.assertEquals(command.getExpiryChecker().isExpired(jobStartTime), expectedResult)
         );
     }
 }