Implant vid-app-common org.onap.vid.job (main and test)
[vid.git] / vid-app-common / src / test / java / org / onap / vid / job / command / InProgressStatusServiceTest.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 org.jetbrains.annotations.NotNull;
24 import org.mockito.InjectMocks;
25 import org.mockito.Mock;
26 import org.mockito.Mockito;
27 import org.mockito.MockitoAnnotations;
28 import org.onap.vid.job.Job;
29 import org.onap.vid.job.impl.JobSharedData;
30 import org.onap.vid.model.serviceInstantiation.ServiceInstantiation;
31 import org.onap.vid.mso.RestMsoImplementation;
32 import org.onap.vid.mso.RestObject;
33 import org.onap.vid.mso.rest.AsyncRequestStatus;
34 import org.onap.vid.properties.Features;
35 import org.onap.vid.services.AsyncInstantiationBusinessLogic;
36 import org.onap.vid.services.AuditService;
37 import org.onap.vid.services.AsyncInstantiationBaseTest;
38 import org.onap.vid.testUtils.TestUtils;
39 import org.testng.annotations.BeforeClass;
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.util.UUID;
46 import java.util.stream.Stream;
47
48 import static org.mockito.ArgumentMatchers.any;
49 import static org.mockito.ArgumentMatchers.eq;
50 import static org.mockito.Mockito.*;
51 import static org.testng.AssertJUnit.assertEquals;
52
53 public class InProgressStatusServiceTest {
54
55     @Mock
56     private RestMsoImplementation restMso;
57
58     @Mock
59     private AsyncInstantiationBusinessLogic asyncInstantiationBL;
60
61     @Mock
62     private AuditService auditService;
63
64     @Mock
65     private FeatureManager featureManager;
66
67     @InjectMocks
68     private InProgressStatusService inProgressStatusService;
69
70     @BeforeClass
71     public void initMocks() {
72         MockitoAnnotations.initMocks(this);
73     }
74
75     @BeforeMethod
76     public void resetMocks() {
77         Mockito.reset(restMso);
78         Mockito.reset(asyncInstantiationBL);
79     }
80
81     @DataProvider
82     public static Object[][] jobStatuses() {
83         return Stream.of(Job.JobStatus.values())
84                 .map(status -> new Object[] { status })
85                 .toArray(Object[][]::new);
86     }
87
88     @Test(dataProvider = "jobStatuses")
89     public void whenGetFromMsoRequestStatus_returnItToCaller(Job.JobStatus expectedJobStatus) {
90
91         UUID jobUuid = UUID.randomUUID();
92         String userId = "mockedUserID";
93         String testApi = "mockedTestApi";
94         String requestId = UUID.randomUUID().toString();
95         ServiceInstantiation serviceInstantiation = mock(ServiceInstantiation.class);
96
97         when(asyncInstantiationBL.getOrchestrationRequestsPath()).thenReturn("");
98
99         AsyncRequestStatus requestStatus = AsyncInstantiationBaseTest.asyncRequestStatusResponse("");
100         RestObject<AsyncRequestStatus> msoResponse = createMockedAsyncRequestStatus(requestStatus, 200);
101         when(restMso.GetForObject(contains(requestId), eq(AsyncRequestStatus.class))).thenReturn(msoResponse);
102
103         when(asyncInstantiationBL.calcStatus(any())).thenReturn(expectedJobStatus);
104
105         ExpiryChecker expiryChecker = mock(ExpiryChecker.class);
106         when(expiryChecker.isExpired(any())).thenReturn(false);
107
108         JobSharedData sharedData = new JobSharedData(jobUuid, userId, serviceInstantiation, testApi);
109         Job.JobStatus actualJobStatus = inProgressStatusService.call(expiryChecker, sharedData, requestId);
110         assertEquals(expectedJobStatus, actualJobStatus);
111
112         verify(auditService).auditMsoStatus(eq(jobUuid), same(requestStatus.request));
113         verify(asyncInstantiationBL).updateResourceInfo(eq(sharedData), eq(expectedJobStatus), eq(requestStatus));
114         //verify we don't update service info during this case, which shall stay in_progress
115         verify(asyncInstantiationBL, never()).updateServiceInfo(any(), any());
116     }
117
118     @NotNull
119     protected RestObject<AsyncRequestStatus> createMockedAsyncRequestStatus(AsyncRequestStatus requestStatus, int statusCode) {
120         RestObject<AsyncRequestStatus> msoResponse = mock(RestObject.class);
121         when(msoResponse.getStatusCode()).thenReturn(statusCode);
122         when(msoResponse.get()).thenReturn(requestStatus);
123         return msoResponse;
124     }
125
126     @Test(dataProvider = "trueAndFalse", dataProviderClass = TestUtils.class)
127     public void whenGetAsyncRequestStatus_thenRightResponseReturned(boolean isResumeFlagActive) {
128         String requestId = "abcRequest";
129         String baseMso = "/fakeBase/v15";
130
131         when(asyncInstantiationBL.getOrchestrationRequestsPath()).thenReturn(baseMso);
132         when(featureManager.isActive(Features.FLAG_1908_RESUME_MACRO_SERVICE)).thenReturn(isResumeFlagActive);
133
134         AsyncRequestStatus requestStatus = AsyncInstantiationBaseTest.asyncRequestStatusResponse("");
135         RestObject<AsyncRequestStatus> mockedResponse = createMockedAsyncRequestStatus(requestStatus, 399);
136         String path = baseMso + "/" + requestId + (isResumeFlagActive ? "?format=detail" : "");
137         when(restMso.GetForObject(eq(path), eq(AsyncRequestStatus.class))).thenReturn(mockedResponse);
138
139         assertEquals(mockedResponse, inProgressStatusService.getAsyncRequestStatus(requestId));
140     }
141
142     @DataProvider
143     public static Object[][] getAsyncReturnErrorDataProvider() {
144         return new Object[][]{
145                 {AsyncInstantiationBaseTest.asyncRequestStatusResponse("xyz"), 400},
146                 {AsyncInstantiationBaseTest.asyncRequestStatusResponse("xyz"), 401},
147                 {AsyncInstantiationBaseTest.asyncRequestStatusResponse("xyz"), 500},
148                 {null, 200},
149         };
150     }
151
152     @Test(dataProvider = "getAsyncReturnErrorDataProvider", expectedExceptions = InProgressStatusService.BadResponseFromMso.class)
153     public void whenGetAsyncReturnError_thenExceptionIsThrown(AsyncRequestStatus requestStatus, int statusCode) {
154         String requestId = "abcRequest";
155         String baseMso = "/fakeBase/v15";
156         when(asyncInstantiationBL.getOrchestrationRequestsPath()).thenReturn(baseMso);
157
158         RestObject<AsyncRequestStatus> mockedResponse = createMockedAsyncRequestStatus(requestStatus, statusCode);
159         when(restMso.GetForObject(eq(baseMso + "/" + requestId), eq(AsyncRequestStatus.class))).thenReturn(mockedResponse);
160
161         inProgressStatusService.getAsyncRequestStatus(requestId);
162     }
163
164 }