2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Nokia 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;
24 import io.joshworks.restclient.http.HttpResponse;
25 import org.mockito.Mock;
26 import org.onap.vid.job.Job;
27 import org.onap.vid.job.NextCommand;
28 import org.onap.vid.mso.MsoInterface;
29 import org.onap.vid.mso.rest.AsyncRequestStatus;
30 import org.onap.vid.services.AsyncInstantiationBusinessLogic;
31 import org.onap.vid.services.AuditService;
32 import org.testng.annotations.BeforeMethod;
33 import org.testng.annotations.Test;
35 import javax.ws.rs.ProcessingException;
36 import java.util.UUID;
39 import static org.assertj.core.api.Java6Assertions.assertThat;
40 import static org.mockito.Mockito.verify;
41 import static org.mockito.Mockito.when;
42 import static org.mockito.MockitoAnnotations.initMocks;
44 public class InProgressStatusCommandTest {
47 private AsyncInstantiationBusinessLogic asyncInstantiationBusinessLogic;
50 private MsoInterface msoInterface;
53 private AuditService auditService;
56 private HttpResponse<AsyncRequestStatus> msoResponse;
59 private AsyncRequestStatus asyncRequestStatus;
62 private AsyncRequestStatus.Request request;
64 private UUID uuid = UUID.randomUUID();
66 private InProgressStatusCommand inProgressStatusCommand;
72 inProgressStatusCommand = new InProgressStatusCommand(asyncInstantiationBusinessLogic, msoInterface, auditService, uuid, "sampleRequestId");
74 when(asyncInstantiationBusinessLogic.getOrchestrationRequestsPath()).thenReturn("http://localhost:8080/samplePath");
75 when(msoInterface.get("http://localhost:8080/samplePath/sampleRequestId", AsyncRequestStatus.class)).thenReturn(msoResponse);
76 when(msoResponse.getBody()).thenReturn(asyncRequestStatus);
81 public void whenSOReturnsErrorShouldSetProperFailureStateAndReturnRetryCommand() {
82 when(msoResponse.getStatus()).thenReturn(500);
84 NextCommand call = inProgressStatusCommand.call();
86 assertThat(call.getStatus()).isEqualTo(Job.JobStatus.IN_PROGRESS);
87 assertThat(call.getCommand()).isEqualTo(inProgressStatusCommand);
89 verify(auditService).setFailedAuditStatusFromMso(uuid, "sampleRequestId", 500, asyncRequestStatus.toString());
93 public void shouldProperlyHandleFailedInstantiation() {
94 when(msoResponse.getStatus()).thenReturn(200);
95 when(asyncInstantiationBusinessLogic.calcStatus(asyncRequestStatus)).thenReturn(Job.JobStatus.FAILED);
96 asyncRequestStatus.request = request;
98 NextCommand call = inProgressStatusCommand.call();
100 assertThat(call.getCommand()).isEqualTo(inProgressStatusCommand);
101 assertThat(call.getStatus()).isEqualTo(Job.JobStatus.FAILED);
103 verify(asyncInstantiationBusinessLogic).handleFailedInstantiation(uuid);
104 verify(asyncInstantiationBusinessLogic).auditMsoStatus(uuid, request);
108 public void shouldRetryCommandWithPausedState() {
109 when(msoResponse.getStatus()).thenReturn(200);
110 when(asyncInstantiationBusinessLogic.calcStatus(asyncRequestStatus)).thenReturn(Job.JobStatus.PAUSE);
111 asyncRequestStatus.request = request;
113 NextCommand call = inProgressStatusCommand.call();
115 assertThat(call.getCommand()).isEqualTo(inProgressStatusCommand);
116 assertThat(call.getStatus()).isEqualTo(Job.JobStatus.IN_PROGRESS);
118 verify(asyncInstantiationBusinessLogic).auditMsoStatus(uuid, request);
119 verify(asyncInstantiationBusinessLogic).updateServiceInfoAndAuditStatus(uuid, Job.JobStatus.PAUSE);
123 public void shouldRetryCommandExitedWithProcessingException() {
124 when(msoResponse.getStatus()).thenReturn(200);
125 when(asyncInstantiationBusinessLogic.calcStatus(asyncRequestStatus)).thenThrow(new ProcessingException(""));
127 NextCommand call = inProgressStatusCommand.call();
129 assertThat(call.getCommand()).isEqualTo(inProgressStatusCommand);
130 assertThat(call.getStatus()).isEqualTo(Job.JobStatus.IN_PROGRESS);
134 public void shouldSetStoppedStatusWhenRuntimeExceptionOccurs() {
135 when(msoResponse.getStatus()).thenReturn(200);
136 when(asyncInstantiationBusinessLogic.calcStatus(asyncRequestStatus)).thenThrow(new RuntimeException());
138 NextCommand call = inProgressStatusCommand.call();
140 assertThat(call.getCommand()).isEqualTo(inProgressStatusCommand);
141 assertThat(call.getStatus()).isEqualTo(Job.JobStatus.STOPPED);