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
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.vid.job.command;
22
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;
44
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;
51
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;
65
66 public class ServiceInProgressStatusCommandTest {
67
68
69     @Mock
70     private AsyncInstantiationBusinessLogic asyncInstantiationBL;
71
72     @Mock
73     private JobsBrokerService jobsBrokerService;
74
75     @Mock
76     private JobAdapter jobAdapter;
77
78     @Mock
79     private FeatureManager featureManager;
80
81     @Mock
82     private JobSharedData sharedData;
83
84     @Mock
85     private ServiceInstantiation request;
86
87     @Mock
88     private InProgressStatusService inProgressStatusService;
89
90     @Mock
91     private WatchChildrenJobsBL watchChildrenJobsBL;
92
93     @Mock
94     private MsoResultHandlerService msoResultHandlerService;
95
96     @Mock
97     private MsoRequestBuilder msoRequestBuilder;
98
99     @Mock
100     private RestMsoImplementation restMsoImplementation;
101
102     @Mock
103     private AuditService auditService;
104
105     private ALaCarteServiceCommand command;
106
107
108     @DataProvider
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}
118         };
119     }
120
121     private static ZonedDateTime getTimeNowMinus(int hoursAgo) {
122         return ZonedDateTime.ofInstant(Instant.now().minus(hoursAgo, ChronoUnit.HOURS), ZoneOffset.UTC);
123     }
124
125     @BeforeMethod
126     public void initMocks() {
127         MockitoAnnotations.initMocks(this);
128         command = new ALaCarteServiceCommand(
129                 inProgressStatusService,
130                 watchChildrenJobsBL,
131                 asyncInstantiationBL,
132                 jobsBrokerService,
133                 msoRequestBuilder,
134                 msoResultHandlerService,
135                 jobAdapter,
136                 restMsoImplementation,
137                 auditService
138         );
139     }
140
141     @Test
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),
152                 emptyList());
153         when(featureManager.isActive(Features.FLAG_ASYNC_ALACARTE_VNF)).thenReturn(true);
154
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);
159
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()
164                 ));
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();
172
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));
177
178         assertThat(argumentCaptor.getAllValues(), containsInAnyOrder(vnf1, vnf2, network));
179
180         verify(jobsBrokerService, times(3)).add(any());
181
182         //verify we don't update service info during this case, which shall stay in_progress
183         verify(asyncInstantiationBL, never()).updateServiceInfo(any(), any());
184
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())));
188     }
189
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)
194         );
195     }
196 }