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 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;
45 import java.util.UUID;
46 import java.util.stream.Stream;
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;
53 public class InProgressStatusServiceTest {
56 private RestMsoImplementation restMso;
59 private AsyncInstantiationBusinessLogic asyncInstantiationBL;
62 private AuditService auditService;
65 private FeatureManager featureManager;
68 private InProgressStatusService inProgressStatusService;
71 public void initMocks() {
72 MockitoAnnotations.initMocks(this);
76 public void resetMocks() {
77 Mockito.reset(restMso);
78 Mockito.reset(asyncInstantiationBL);
82 public static Object[][] jobStatuses() {
83 return Stream.of(Job.JobStatus.values())
84 .map(status -> new Object[] { status })
85 .toArray(Object[][]::new);
88 @Test(dataProvider = "jobStatuses")
89 public void whenGetFromMsoRequestStatus_returnItToCaller(Job.JobStatus expectedJobStatus) {
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);
97 when(asyncInstantiationBL.getOrchestrationRequestsPath()).thenReturn("");
99 AsyncRequestStatus requestStatus = AsyncInstantiationBaseTest.asyncRequestStatusResponse("");
100 RestObject<AsyncRequestStatus> msoResponse = createMockedAsyncRequestStatus(requestStatus, 200);
101 when(restMso.GetForObject(contains(requestId), eq(AsyncRequestStatus.class))).thenReturn(msoResponse);
103 when(asyncInstantiationBL.calcStatus(any())).thenReturn(expectedJobStatus);
105 ExpiryChecker expiryChecker = mock(ExpiryChecker.class);
106 when(expiryChecker.isExpired(any())).thenReturn(false);
108 JobSharedData sharedData = new JobSharedData(jobUuid, userId, serviceInstantiation, testApi);
109 Job.JobStatus actualJobStatus = inProgressStatusService.call(expiryChecker, sharedData, requestId);
110 assertEquals(expectedJobStatus, actualJobStatus);
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());
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);
126 @Test(dataProvider = "trueAndFalse", dataProviderClass = TestUtils.class)
127 public void whenGetAsyncRequestStatus_thenRightResponseReturned(boolean isResumeFlagActive) {
128 String requestId = "abcRequest";
129 String baseMso = "/fakeBase/v15";
131 when(asyncInstantiationBL.getOrchestrationRequestsPath()).thenReturn(baseMso);
132 when(featureManager.isActive(Features.FLAG_1908_RESUME_MACRO_SERVICE)).thenReturn(isResumeFlagActive);
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);
139 assertEquals(mockedResponse, inProgressStatusService.getAsyncRequestStatus(requestId));
143 public static Object[][] getAsyncReturnErrorDataProvider() {
144 return new Object[][]{
145 {AsyncInstantiationBaseTest.asyncRequestStatusResponse("xyz"), 400},
146 {AsyncInstantiationBaseTest.asyncRequestStatusResponse("xyz"), 401},
147 {AsyncInstantiationBaseTest.asyncRequestStatusResponse("xyz"), 500},
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);
158 RestObject<AsyncRequestStatus> mockedResponse = createMockedAsyncRequestStatus(requestStatus, statusCode);
159 when(restMso.GetForObject(eq(baseMso + "/" + requestId), eq(AsyncRequestStatus.class))).thenReturn(mockedResponse);
161 inProgressStatusService.getAsyncRequestStatus(requestId);