2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.vid.job.command;
23 import com.google.common.collect.ImmutableList;
24 import com.google.common.collect.ImmutableMap;
25 import org.mockito.ArgumentCaptor;
26 import org.mockito.Mock;
27 import org.mockito.MockitoAnnotations;
28 import org.onap.vid.job.*;
29 import org.onap.vid.job.impl.JobSharedData;
30 import org.onap.vid.model.Action;
31 import org.onap.vid.model.serviceInstantiation.Network;
32 import org.onap.vid.model.serviceInstantiation.ServiceInstantiation;
33 import org.onap.vid.model.serviceInstantiation.Vnf;
34 import org.onap.vid.mso.RestMsoImplementation;
35 import org.onap.vid.properties.Features;
36 import org.onap.vid.properties.VidProperties;
37 import org.onap.vid.services.AsyncInstantiationBusinessLogic;
38 import org.onap.vid.services.AuditService;
39 import org.testng.Assert;
40 import org.testng.annotations.BeforeMethod;
41 import org.testng.annotations.DataProvider;
42 import org.testng.annotations.Test;
43 import org.togglz.core.manager.FeatureManager;
45 import java.time.Instant;
46 import java.time.ZoneOffset;
47 import java.time.ZonedDateTime;
48 import java.time.temporal.ChronoUnit;
49 import java.util.Arrays;
50 import java.util.UUID;
52 import static java.util.Collections.emptyList;
53 import static org.hamcrest.MatcherAssert.assertThat;
54 import static org.hamcrest.Matchers.containsInAnyOrder;
55 import static org.hamcrest.core.Is.is;
56 import static org.mockito.Matchers.any;
57 import static org.mockito.Matchers.eq;
58 import static org.mockito.Mockito.*;
59 import static org.onap.vid.job.Job.JobStatus.*;
60 import static org.onap.vid.job.command.ResourceCommandKt.*;
61 import static org.onap.vid.job.command.ResourceCommandTest.FakeResourceCreator.*;
62 import static org.onap.vid.model.Action.Create;
63 import static org.onap.vid.testUtils.TestUtils.testWithSystemProperty;
64 import static org.testng.AssertJUnit.assertEquals;
66 public class ServiceInProgressStatusCommandTest {
70 private AsyncInstantiationBusinessLogic asyncInstantiationBL;
73 private JobsBrokerService jobsBrokerService;
76 private JobAdapter jobAdapter;
79 private FeatureManager featureManager;
82 private JobSharedData sharedData;
85 private ServiceInstantiation request;
88 private InProgressStatusService inProgressStatusService;
91 private WatchChildrenJobsBL watchChildrenJobsBL;
94 private MsoResultHandlerService msoResultHandlerService;
97 private MsoRequestBuilder msoRequestBuilder;
100 private RestMsoImplementation restMsoImplementation;
103 private AuditService auditService;
105 private ALaCarteServiceCommand command;
109 public static Object[][] isExpiredJobStatusData() {
110 return new Object[][]{
111 {ZonedDateTime.now(), "24", false},
112 {getTimeNowMinus(2), "1", true},
113 {getTimeNowMinus(24), "24", true},
114 {getTimeNowMinus(2), "0", false},
115 {getTimeNowMinus(2), "-1", false},
116 {getTimeNowMinus(2), "", false},
117 {getTimeNowMinus(2), "a", false}
121 private static ZonedDateTime getTimeNowMinus(int hoursAgo) {
122 return ZonedDateTime.ofInstant(Instant.now().minus(hoursAgo, ChronoUnit.HOURS), ZoneOffset.UTC);
126 public void initMocks() {
127 MockitoAnnotations.initMocks(this);
128 command = new ALaCarteServiceCommand(
129 inProgressStatusService,
131 asyncInstantiationBL,
134 msoResultHandlerService,
136 restMsoImplementation,
142 public void whenGetFromMsoCompletedAndALaCarte_generateNewJobsForVnfs() {
143 UUID uuid = UUID.randomUUID();
144 String userId = "mockedUserID";
145 String testApi = "VNF_API";
147 // Create components setPosition in order to verify on the creation order on createChildJob
148 Network network = createNetwork(Create);
149 network.setPosition(0);
150 Vnf vnf1 = createVnf(emptyList(), Create);
152 Vnf vnf2 = createVnf(emptyList(), Create);
154 ServiceInstantiation serviceInstantiation = createService(
155 ImmutableList.of(vnf1, vnf2),
156 ImmutableList.of(network),
158 when(featureManager.isActive(Features.FLAG_ASYNC_ALACARTE_VNF)).thenReturn(true);
160 UUID uuid1 = UUID.fromString("12345678-1234-1234-1234-123456789012");
161 UUID uuid2 = UUID.fromString("12345678-1234-1234-1234-123456789013");
162 UUID uuid3 = UUID.fromString("12345678-1234-1234-1234-123456789014");
163 when(jobsBrokerService.add(any())).thenReturn(uuid1).thenReturn(uuid2).thenReturn(uuid3);
165 JobSharedData sharedData = new JobSharedData(uuid, userId, serviceInstantiation, testApi);
166 command.init(sharedData, ImmutableMap.of(
167 ACTION_PHASE, Action.Create.name(),
168 INTERNAL_STATE, InternalState.IN_PROGRESS.name()
170 when(inProgressStatusService.call(any(), eq(sharedData), any())).thenReturn(Job.JobStatus.COMPLETED);
171 when(watchChildrenJobsBL.cumulateJobStatus(Job.JobStatus.COMPLETED, COMPLETED_WITH_NO_ACTION)).thenReturn(COMPLETED);
172 when(watchChildrenJobsBL.cumulateJobStatus(Job.JobStatus.COMPLETED_WITH_NO_ACTION, COMPLETED)).thenReturn(COMPLETED);
173 when(msoResultHandlerService.getRequest(eq(sharedData))).thenReturn(serviceInstantiation);
174 NextCommand nextCommand = command.call();
175 assertEquals(IN_PROGRESS, nextCommand.getStatus());
176 nextCommand = nextCommand.getCommand().call();
178 ArgumentCaptor<JobAdapter.AsyncJobRequest> argumentCaptor = ArgumentCaptor.forClass(JobAdapter.AsyncJobRequest.class);
179 verify(jobAdapter, times(1)).createChildJob(eq(JobType.NetworkInstantiation), argumentCaptor.capture(), eq(sharedData), any(), eq(0));
180 verify(jobAdapter, times(1)).createChildJob(eq(JobType.VnfInstantiation), argumentCaptor.capture(), eq(sharedData), any(), eq(1));
181 verify(jobAdapter, times(1)).createChildJob(eq(JobType.VnfInstantiation), argumentCaptor.capture(), eq(sharedData), any(), eq(2));
183 assertThat(argumentCaptor.getAllValues(), containsInAnyOrder(vnf1, vnf2, network));
185 verify(jobsBrokerService, times(3)).add(any());
187 //verify we don't update service info during this case, which shall stay in_progress
188 verify(asyncInstantiationBL, never()).updateServiceInfo(any(), any());
190 assertThat(nextCommand.getStatus(), is(Job.JobStatus.IN_PROGRESS));
191 assertThat(nextCommand.getCommand().getType(), is(JobType.ALaCarteService));
192 assertThat(nextCommand.getCommand().getData().get(CHILD_JOBS), is(Arrays.asList(uuid1.toString(), uuid2.toString(), uuid3.toString())));
195 @Test(dataProvider = "isExpiredJobStatusData")
196 public void isExpiredJobStatusTest(ZonedDateTime jobStartTime, String configValue, boolean expectedResult) throws Exception {
197 testWithSystemProperty(VidProperties.VID_JOB_MAX_HOURS_IN_PROGRESS, configValue, ()->
198 Assert.assertEquals(command.getExpiryChecker().isExpired(jobStartTime), expectedResult)