1 package org.onap.vid.job.command;
3 import com.google.common.collect.ImmutableMap;
4 import org.mockito.ArgumentCaptor;
5 import org.mockito.InjectMocks;
6 import org.mockito.Mock;
7 import org.mockito.MockitoAnnotations;
8 import org.onap.portalsdk.core.util.SystemProperties;
9 import org.onap.vid.job.*;
10 import org.onap.vid.job.impl.JobSharedData;
11 import org.onap.vid.model.serviceInstantiation.Network;
12 import org.onap.vid.model.serviceInstantiation.ServiceInstantiation;
13 import org.onap.vid.model.serviceInstantiation.Vnf;
14 import org.onap.vid.mso.model.ModelInfo;
15 import org.onap.vid.properties.Features;
16 import org.onap.vid.properties.VidProperties;
17 import org.onap.vid.services.AsyncInstantiationBusinessLogic;
18 import org.springframework.core.env.Environment;
19 import org.testng.Assert;
20 import org.testng.annotations.BeforeMethod;
21 import org.testng.annotations.DataProvider;
22 import org.testng.annotations.Test;
23 import org.togglz.core.manager.FeatureManager;
25 import java.time.Instant;
26 import java.time.ZoneOffset;
27 import java.time.ZonedDateTime;
28 import java.time.temporal.ChronoUnit;
29 import java.util.Arrays;
31 import java.util.TreeMap;
32 import java.util.UUID;
34 import static org.hamcrest.MatcherAssert.assertThat;
35 import static org.hamcrest.Matchers.containsInAnyOrder;
36 import static org.hamcrest.core.Is.is;
37 import static org.mockito.Matchers.any;
38 import static org.mockito.Matchers.eq;
39 import static org.mockito.Mockito.*;
40 import static org.onap.vid.job.Job.JobStatus.*;
42 public class ServiceInProgressStatusCommandTest {
46 private AsyncInstantiationBusinessLogic asyncInstantiationBL;
49 private JobsBrokerService jobsBrokerService;
52 private JobAdapter jobAdapter;
55 private FeatureManager featureManager;
58 private JobSharedData sharedData;
61 private Environment environment;
64 private ServiceInstantiation request;
67 private InProgressStatusService inProgressStatusService;
70 private ServiceInProgressStatusCommand command = new ServiceInProgressStatusCommand();
73 public static Object[][] isNeedToCreateChildJobsDataProvider() {
74 return new Object[][]{
75 {new TreeMap<String,Vnf>() , true, true, false},
76 {null , true, true, false},
77 {ImmutableMap.of("a",mock(Vnf.class)), false, true, false},
78 {ImmutableMap.of("a",mock(Vnf.class)), true, false, false},
79 {ImmutableMap.of("a",mock(Vnf.class)), true, true, true},
84 public static Object[][] processJobStatusData() {
85 return new Object[][]{
86 /* {MSO jobStatus, jobStartTime, isNeedToCreateChildJobs(), property vid.job.max.hoursInProgress, expected nextCommand.getStatus() } */
87 {IN_PROGRESS, false, IN_PROGRESS},
88 {FAILED, false, FAILED},
89 {PAUSE, false, IN_PROGRESS},
90 {COMPLETED, false, COMPLETED},
91 {COMPLETED, true, IN_PROGRESS},
92 {RESOURCE_IN_PROGRESS, false, RESOURCE_IN_PROGRESS},
93 {PENDING, false, PENDING},
94 {STOPPED, false, STOPPED},
95 {COMPLETED_WITH_ERRORS, false, COMPLETED_WITH_ERRORS},
96 {CREATING, false, CREATING}
101 public static Object[][] isExpiredJobStatusData() {
102 return new Object[][]{
103 {ZonedDateTime.now(), "24", false},
104 {getTimeNowMinus(2), "1", true},
105 {getTimeNowMinus(24), "24", true},
106 {getTimeNowMinus(2), "0", false},
107 {getTimeNowMinus(2), "-1", false},
108 {getTimeNowMinus(2), "", false},
109 {getTimeNowMinus(2), "a", false}
113 private static ZonedDateTime getTimeNowMinus(int hoursAgo) {
114 return ZonedDateTime.ofInstant(Instant.now().minus(hoursAgo, ChronoUnit.HOURS), ZoneOffset.UTC);
118 public void initMocks() {
119 MockitoAnnotations.initMocks(this);
122 @Test(dataProvider = "isNeedToCreateChildJobsDataProvider" )
123 public void testIsNeedToCreateChildJobs(Map<String, Vnf> serviceVnfs, boolean isALaCarte,
124 boolean isFeatureEnabled, boolean expected) {
125 MockitoAnnotations.initMocks(this);
126 ServiceInstantiation serviceInstantiation = mock(ServiceInstantiation.class);
127 when(serviceInstantiation.getVnfs()).thenReturn(serviceVnfs);
128 when(serviceInstantiation.isALaCarte()).thenReturn(isALaCarte);
129 when(featureManager.isActive(Features.FLAG_ASYNC_ALACARTE_VNF)).thenReturn(isFeatureEnabled);
130 assertThat(command.isNeedToCreateChildJobs(serviceInstantiation), is(expected));
134 public void whenGetFromMsoCompletedAndALaCarte_generateNewJobsForVnfs() {
135 UUID uuid = UUID.randomUUID();
136 String userId = "mockedUserID";
137 Vnf vnf1 = mock(Vnf.class);
138 Vnf vnf2 = mock(Vnf.class);
139 Network network1 = mock(Network.class);
140 ServiceInstantiation serviceInstantiation = mock(ServiceInstantiation.class);
141 when(serviceInstantiation.getVnfs()).thenReturn(ImmutableMap.of("a", vnf1, "b", vnf2));
142 when(serviceInstantiation.getNetworks()).thenReturn(ImmutableMap.of("c", network1));
143 when(serviceInstantiation.isALaCarte()).thenReturn(true);
144 when(serviceInstantiation.getModelInfo()).thenReturn(new ModelInfo());
146 when(featureManager.isActive(Features.FLAG_ASYNC_ALACARTE_VNF)).thenReturn(true);
148 UUID uuid1 = UUID.fromString("12345678-1234-1234-1234-123456789012");
149 UUID uuid2 = UUID.fromString("12345678-1234-1234-1234-123456789013");
150 UUID uuid3 = UUID.fromString("12345678-1234-1234-1234-123456789014");
151 when(jobsBrokerService.add(any())).thenReturn(uuid1).thenReturn(uuid2).thenReturn(uuid3);
153 JobSharedData sharedData = new JobSharedData(uuid, userId, serviceInstantiation);
154 command.init(sharedData, "", "");
155 when(inProgressStatusService.call(any(), eq(sharedData), any())).thenReturn(Job.JobStatus.COMPLETED);
156 NextCommand nextCommand = command.call();
158 ArgumentCaptor<JobAdapter.AsyncJobRequest> argumentCaptor = ArgumentCaptor.forClass(JobAdapter.AsyncJobRequest.class);
159 verify(jobAdapter, times(2)).createChildJob(eq(JobType.VnfInstantiation), eq(Job.JobStatus.CREATING), argumentCaptor.capture(), eq(sharedData), any());
160 verify(jobAdapter, times(1)).createChildJob(eq(JobType.NetworkInstantiation), eq(Job.JobStatus.CREATING), argumentCaptor.capture(), eq(sharedData), any());
161 assertThat(argumentCaptor.getAllValues(), containsInAnyOrder(vnf1, vnf2, network1));
163 verify(jobsBrokerService, times(3)).add(any());
165 //verify we don't update service info during this case, which shall stay in_progress
166 verify(asyncInstantiationBL, never()).updateServiceInfo(any(), any());
168 assertThat(nextCommand.getStatus(), is(Job.JobStatus.IN_PROGRESS));
169 assertThat(nextCommand.getCommand().getType(), is(new WatchingCommand().getType()));
170 assertThat(nextCommand.getCommand().getData().get("childrenJobs"), is(Arrays.asList(uuid1.toString(), uuid2.toString(), uuid3.toString())));
171 assertThat(nextCommand.getCommand().getData().get("isService"), is(true));
174 @Test(dataProvider = "processJobStatusData")
175 public void processJobStatusTest(Job.JobStatus jobStatus, boolean isNeedToCreateChildJobs, Job.JobStatus expectedStatus) {
177 when(featureManager.isActive(Features.FLAG_ASYNC_ALACARTE_VNF)).thenReturn(true);
178 // All mocks under are used for isNeedToCreateChildJobs=true case
179 when(sharedData.getRequest()).thenReturn(request);
180 when(request.isALaCarte()).thenReturn(true);
181 Map vnfs = mock(Map.class);
182 ModelInfo modelInfo = mock(ModelInfo.class);
184 // if vnfs.isEmpty -> isNeedToCreateChildJobs will return false
185 when(vnfs.isEmpty()).thenReturn(!isNeedToCreateChildJobs);
187 when(request.getVnfs()).thenReturn(vnfs);
188 when(request.getModelInfo()).thenReturn(modelInfo);
189 command.instanceId = "MockInstId";
191 NextCommand nextCommand = command.processJobStatus(jobStatus);
192 Assert.assertEquals(nextCommand.getStatus(), expectedStatus);
193 if (isNeedToCreateChildJobs) {
194 Assert.assertEquals(nextCommand.getCommand().getClass(), WatchingCommand.class);
196 Assert.assertEquals(nextCommand.getCommand(), command);
200 @Test(dataProvider = "isExpiredJobStatusData")
201 public void isExpiredJobStatusTest(ZonedDateTime jobStartTime, String configValue, boolean expectedResult) {
202 SystemProperties systemProperties = new SystemProperties();
203 systemProperties.setEnvironment(environment);
204 when(environment.getRequiredProperty(VidProperties.VID_JOB_MAX_HOURS_IN_PROGRESS)).thenReturn(configValue);
205 when(environment.containsProperty(VidProperties.VID_JOB_MAX_HOURS_IN_PROGRESS)).thenReturn(true);
206 Assert.assertEquals(command.getExpiryChecker().isExpired(jobStartTime), expectedResult);