Merge from ECOMP's repository
[vid.git] / vid-app-common / src / test / java / org / onap / vid / job / command / WatchingCommandTest.java
1 package org.onap.vid.job.command;
2
3 import org.mockito.InjectMocks;
4 import org.mockito.Mock;
5 import org.mockito.MockitoAnnotations;
6 import org.onap.portalsdk.core.service.DataAccessService;
7 import org.onap.vid.job.Job;
8 import org.onap.vid.job.NextCommand;
9 import org.onap.vid.job.impl.JobSharedData;
10 import org.onap.vid.model.serviceInstantiation.ServiceInstantiation;
11 import org.onap.vid.services.AsyncInstantiationBusinessLogic;
12 import org.testng.annotations.BeforeClass;
13 import org.testng.annotations.DataProvider;
14 import org.testng.annotations.Test;
15
16 import java.util.List;
17 import java.util.UUID;
18
19 import static org.hamcrest.MatcherAssert.assertThat;
20 import static org.hamcrest.core.Is.is;
21 import static org.mockito.Matchers.eq;
22 import static org.mockito.Mockito.*;
23
24 public class WatchingCommandTest {
25
26     @Mock
27     private AsyncInstantiationBusinessLogic asyncInstantiationBL;
28
29     @Mock
30     private DataAccessService dataAccessService;
31
32     @Mock
33     private WatchChildrenJobsBL watchChildrenJobsBL;
34
35     @InjectMocks
36     private WatchingCommand watchingCommand = new WatchingCommand();
37
38
39
40     @BeforeClass
41     public void initMocks() {
42         MockitoAnnotations.initMocks(this);
43     }
44
45     @DataProvider
46     public static Object[][] testWatchingDataProvider() {
47         return new Object[][]{
48                 {"all children final, no failed child, is service", Job.JobStatus.COMPLETED, true, Job.JobStatus.COMPLETED},
49                 {"all children final, there is failed child, is service", Job.JobStatus.COMPLETED_WITH_ERRORS, true, Job.JobStatus.COMPLETED_WITH_ERRORS},
50                 {"not all children final, is service", Job.JobStatus.IN_PROGRESS, true, Job.JobStatus.IN_PROGRESS},
51                 {"all children final, no failed child, not service", Job.JobStatus.COMPLETED, false, Job.JobStatus.COMPLETED},
52                 {"all children final, there is failed child, not service", Job.JobStatus.COMPLETED_WITH_ERRORS, false, Job.JobStatus.COMPLETED_WITH_ERRORS},
53                 {"not all children final, not service", Job.JobStatus.IN_PROGRESS, false, Job.JobStatus.RESOURCE_IN_PROGRESS},
54         };
55     }
56
57
58
59     @Test(dataProvider = "testWatchingDataProvider")
60     public void whenGetChildrenStatus_thenJobStatusAsExpected(String desc, Job.JobStatus childrenComulativeStatus, boolean isService, Job.JobStatus expectedCommandStatus) {
61         UUID jobUUID = UUID.randomUUID();
62         JobSharedData sharedData = new JobSharedData(jobUUID, "mockedUserID", mock(ServiceInstantiation.class));
63         List<String> uuids = mock(List.class);
64         watchingCommand.init(sharedData, uuids, isService);
65         when(watchChildrenJobsBL.retrieveChildrenJobsStatus(eq(uuids))).thenReturn(childrenComulativeStatus);
66         when(watchChildrenJobsBL.cumulateJobStatus(eq(childrenComulativeStatus),eq(Job.JobStatus.COMPLETED))).thenReturn(childrenComulativeStatus);
67
68         //execute command and verify
69         NextCommand nextCommand = watchingCommand.call();
70         assertThat(nextCommand.getStatus(), is(expectedCommandStatus));
71         if (!expectedCommandStatus.equals(Job.JobStatus.IN_PROGRESS) && isService) {
72             verify(asyncInstantiationBL).updateServiceInfoAndAuditStatus(jobUUID, expectedCommandStatus);
73         } else {
74             verify(asyncInstantiationBL, never()).updateServiceInfoAndAuditStatus(jobUUID, expectedCommandStatus);
75         }
76     }
77 }