Merge "Logging improvements"
[vid.git] / vid-app-common / src / test / java / org / onap / vid / job / command / InProgressStatusCommandTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
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
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;
34
35 import javax.ws.rs.ProcessingException;
36 import java.util.UUID;
37
38
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;
43
44 public class InProgressStatusCommandTest {
45
46     @Mock
47     private AsyncInstantiationBusinessLogic asyncInstantiationBusinessLogic;
48
49     @Mock
50     private MsoInterface msoInterface;
51
52     @Mock
53     private AuditService auditService;
54
55     @Mock
56     private HttpResponse<AsyncRequestStatus> msoResponse;
57
58     @Mock
59     private AsyncRequestStatus asyncRequestStatus;
60
61     @Mock
62     private AsyncRequestStatus.Request request;
63
64     private UUID uuid = UUID.randomUUID();
65
66     private InProgressStatusCommand inProgressStatusCommand;
67
68     @BeforeMethod
69     public void setUp() {
70         initMocks(this);
71
72         inProgressStatusCommand = new InProgressStatusCommand(asyncInstantiationBusinessLogic, msoInterface, auditService, uuid, "sampleRequestId");
73
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);
77     }
78
79
80     @Test
81     public void whenSOReturnsErrorShouldSetProperFailureStateAndReturnRetryCommand() {
82         when(msoResponse.getStatus()).thenReturn(500);
83
84         NextCommand call = inProgressStatusCommand.call();
85
86         assertThat(call.getStatus()).isEqualTo(Job.JobStatus.IN_PROGRESS);
87         assertThat(call.getCommand()).isEqualTo(inProgressStatusCommand);
88
89         verify(auditService).setFailedAuditStatusFromMso(uuid, "sampleRequestId", 500, asyncRequestStatus.toString());
90     }
91
92     @Test
93     public void shouldProperlyHandleFailedInstantiation() {
94         when(msoResponse.getStatus()).thenReturn(200);
95         when(asyncInstantiationBusinessLogic.calcStatus(asyncRequestStatus)).thenReturn(Job.JobStatus.FAILED);
96         asyncRequestStatus.request = request;
97
98         NextCommand call = inProgressStatusCommand.call();
99
100         assertThat(call.getCommand()).isEqualTo(inProgressStatusCommand);
101         assertThat(call.getStatus()).isEqualTo(Job.JobStatus.FAILED);
102
103         verify(asyncInstantiationBusinessLogic).handleFailedInstantiation(uuid);
104         verify(asyncInstantiationBusinessLogic).auditMsoStatus(uuid, request);
105     }
106
107     @Test
108     public void shouldRetryCommandWithPausedState() {
109         when(msoResponse.getStatus()).thenReturn(200);
110         when(asyncInstantiationBusinessLogic.calcStatus(asyncRequestStatus)).thenReturn(Job.JobStatus.PAUSE);
111         asyncRequestStatus.request = request;
112
113         NextCommand call = inProgressStatusCommand.call();
114
115         assertThat(call.getCommand()).isEqualTo(inProgressStatusCommand);
116         assertThat(call.getStatus()).isEqualTo(Job.JobStatus.IN_PROGRESS);
117
118         verify(asyncInstantiationBusinessLogic).auditMsoStatus(uuid, request);
119         verify(asyncInstantiationBusinessLogic).updateServiceInfoAndAuditStatus(uuid, Job.JobStatus.PAUSE);
120     }
121
122     @Test
123     public void shouldRetryCommandExitedWithProcessingException() {
124         when(msoResponse.getStatus()).thenReturn(200);
125         when(asyncInstantiationBusinessLogic.calcStatus(asyncRequestStatus)).thenThrow(new ProcessingException(""));
126
127         NextCommand call = inProgressStatusCommand.call();
128
129         assertThat(call.getCommand()).isEqualTo(inProgressStatusCommand);
130         assertThat(call.getStatus()).isEqualTo(Job.JobStatus.IN_PROGRESS);
131     }
132
133     @Test
134     public void shouldSetStoppedStatusWhenRuntimeExceptionOccurs() {
135         when(msoResponse.getStatus()).thenReturn(200);
136         when(asyncInstantiationBusinessLogic.calcStatus(asyncRequestStatus)).thenThrow(new RuntimeException());
137
138         NextCommand call = inProgressStatusCommand.call();
139
140         assertThat(call.getCommand()).isEqualTo(inProgressStatusCommand);
141         assertThat(call.getStatus()).isEqualTo(Job.JobStatus.STOPPED);
142     }
143 }