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";
146 Vnf vnf1 = createVnf(emptyList(), Create);
147 Vnf vnf2 = createVnf(emptyList(), Create);
148 Network network = createNetwork(Create);
149 ServiceInstantiation serviceInstantiation = createService(
150 ImmutableList.of(vnf1, vnf2),
151 ImmutableList.of(network),
153 when(featureManager.isActive(Features.FLAG_ASYNC_ALACARTE_VNF)).thenReturn(true);
155 UUID uuid1 = UUID.fromString("12345678-1234-1234-1234-123456789012");
156 UUID uuid2 = UUID.fromString("12345678-1234-1234-1234-123456789013");
157 UUID uuid3 = UUID.fromString("12345678-1234-1234-1234-123456789014");
158 when(jobsBrokerService.add(any())).thenReturn(uuid1).thenReturn(uuid2).thenReturn(uuid3);
160 JobSharedData sharedData = new JobSharedData(uuid, userId, serviceInstantiation, testApi);
161 command.init(sharedData, ImmutableMap.of(
162 ACTION_PHASE, Action.Create.name(),
163 INTERNAL_STATE, InternalState.IN_PROGRESS.name()
165 when(inProgressStatusService.call(any(), eq(sharedData), any())).thenReturn(Job.JobStatus.COMPLETED);
166 when(watchChildrenJobsBL.cumulateJobStatus(Job.JobStatus.COMPLETED, COMPLETED_WITH_NO_ACTION)).thenReturn(COMPLETED);
167 when(watchChildrenJobsBL.cumulateJobStatus(Job.JobStatus.COMPLETED_WITH_NO_ACTION, COMPLETED)).thenReturn(COMPLETED);
168 when(msoResultHandlerService.getRequest(eq(sharedData))).thenReturn(serviceInstantiation);
169 NextCommand nextCommand = command.call();
170 assertEquals(IN_PROGRESS, nextCommand.getStatus());
171 nextCommand = nextCommand.getCommand().call();
173 ArgumentCaptor<JobAdapter.AsyncJobRequest> argumentCaptor = ArgumentCaptor.forClass(JobAdapter.AsyncJobRequest.class);
174 verify(jobAdapter, times(1)).createChildJob(eq(JobType.NetworkInstantiation), argumentCaptor.capture(), eq(sharedData), any(), eq(0));
175 verify(jobAdapter, times(1)).createChildJob(eq(JobType.VnfInstantiation), argumentCaptor.capture(), eq(sharedData), any(), eq(1));
176 verify(jobAdapter, times(1)).createChildJob(eq(JobType.VnfInstantiation), argumentCaptor.capture(), eq(sharedData), any(), eq(2));
178 assertThat(argumentCaptor.getAllValues(), containsInAnyOrder(vnf1, vnf2, network));
180 verify(jobsBrokerService, times(3)).add(any());
182 //verify we don't update service info during this case, which shall stay in_progress
183 verify(asyncInstantiationBL, never()).updateServiceInfo(any(), any());
185 assertThat(nextCommand.getStatus(), is(Job.JobStatus.IN_PROGRESS));
186 assertThat(nextCommand.getCommand().getType(), is(JobType.ALaCarteService));
187 assertThat(nextCommand.getCommand().getData().get(CHILD_JOBS), is(Arrays.asList(uuid1.toString(), uuid2.toString(), uuid3.toString())));
190 @Test(dataProvider = "isExpiredJobStatusData")
191 public void isExpiredJobStatusTest(ZonedDateTime jobStartTime, String configValue, boolean expectedResult) throws Exception {
192 testWithSystemProperty(VidProperties.VID_JOB_MAX_HOURS_IN_PROGRESS, configValue, ()->
193 Assert.assertEquals(command.getExpiryChecker().isExpired(jobStartTime), expectedResult)