1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
/*-
* ============LICENSE_START=======================================================
* ONAP - SO
* ================================================================================
* Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package org.onap.so.apihandlerinfra;
import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.doReturn;
import javax.ws.rs.core.Response;
import org.apache.commons.lang.StringUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.onap.so.apihandler.common.ResponseBuilder;
import org.onap.so.apihandlerinfra.exceptions.ApiException;
import org.onap.so.apihandlerinfra.exceptions.ValidateException;
import org.onap.so.constants.OrchestrationRequestFormat;
import org.onap.so.constants.Status;
import org.onap.so.db.request.beans.InfraActiveRequests;
import org.onap.so.db.request.client.RequestsDbClient;
import org.onap.so.serviceinstancebeans.InstanceReferences;
import org.onap.so.serviceinstancebeans.Request;
import org.onap.so.serviceinstancebeans.RequestStatus;
@RunWith(MockitoJUnitRunner.class)
public class OrchestrationRequestsUnitTest {
@Mock
private RequestsDbClient requestDbClient;
@Mock
private MsoRequest msoRequest;
@Mock
private ResponseBuilder builder;
@Mock
private Response response;
@Mock
private CamundaRequestHandler camundaRequestHandler;
@Rule
public ExpectedException thrown = ExpectedException.none();
@InjectMocks
private OrchestrationRequests orchestrationRequests;
private static final String REQUEST_ID = "7cb9aa56-dd31-41e5-828e-d93027d4ebba";
private static final String SERVICE_INSTANCE_ID = "7cb9aa56-dd31-41e5-828e-d93027d4ebbb";
private static final String ORIGINAL_REQUEST_ID = "8f2d38a6-7c20-465a-bd7e-075645f1394b";
private static final String SERVICE = "service";
private static final String EXT_SYSTEM_ERROR_SOURCE = "external system error source";
private static final String FLOW_STATUS = "FlowStatus";
private static final String RETRY_STATUS_MESSAGE = "RetryStatusMessage";
private static final String ROLLBACK_STATUS_MESSAGE = "RollbackStatusMessage";
private static final String TASK_INFORMATION = " TASK INFORMATION: Last task executed: Call SDNC";
private InfraActiveRequests iar;
boolean includeCloudRequest = false;
private static final String ROLLBACK_EXT_SYSTEM_ERROR_SOURCE = "SDNC";
@Before
public void setup() {
iar = new InfraActiveRequests();
iar.setRequestScope(SERVICE);
iar.setRequestId(REQUEST_ID);
iar.setServiceInstanceId(SERVICE_INSTANCE_ID);
iar.setExtSystemErrorSource(EXT_SYSTEM_ERROR_SOURCE);
iar.setRollbackExtSystemErrorSource(ROLLBACK_EXT_SYSTEM_ERROR_SOURCE);
iar.setFlowStatus(FLOW_STATUS);
iar.setRollbackStatusMessage(ROLLBACK_STATUS_MESSAGE);
iar.setRetryStatusMessage(RETRY_STATUS_MESSAGE);
}
@Test
public void mapInfraActiveRequestToRequestWithOriginalRequestIdTest() throws ApiException {
doReturn("Last task executed: Call SDNC").when(camundaRequestHandler).getTaskName(REQUEST_ID);
InstanceReferences instanceReferences = new InstanceReferences();
instanceReferences.setServiceInstanceId(SERVICE_INSTANCE_ID);
RequestStatus requestStatus = new RequestStatus();
requestStatus.setRequestState(iar.getRequestStatus());
requestStatus.setStatusMessage(String.format("FLOW STATUS: %s RETRY STATUS: %s ROLLBACK STATUS: %s",
FLOW_STATUS + TASK_INFORMATION, RETRY_STATUS_MESSAGE, ROLLBACK_STATUS_MESSAGE));
Request expected = new Request();
expected.setRequestId(REQUEST_ID);
expected.setOriginalRequestId(ORIGINAL_REQUEST_ID);
expected.setInstanceReferences(instanceReferences);
expected.setRequestStatus(requestStatus);
expected.setRequestScope(SERVICE);
iar.setOriginalRequestId(ORIGINAL_REQUEST_ID);
Request result = orchestrationRequests.mapInfraActiveRequestToRequest(iar, includeCloudRequest,
OrchestrationRequestFormat.DETAIL.toString());
assertThat(result, sameBeanAs(expected));
}
@Test
public void mapInfraActiveRequestToRequestOriginalRequestIdNullTest() throws ApiException {
doReturn("Last task executed: Call SDNC").when(camundaRequestHandler).getTaskName(REQUEST_ID);
InstanceReferences instanceReferences = new InstanceReferences();
instanceReferences.setServiceInstanceId(SERVICE_INSTANCE_ID);
RequestStatus requestStatus = new RequestStatus();
requestStatus.setRequestState(iar.getRequestStatus());
requestStatus.setStatusMessage(String.format("FLOW STATUS: %s RETRY STATUS: %s ROLLBACK STATUS: %s",
FLOW_STATUS + TASK_INFORMATION, RETRY_STATUS_MESSAGE, ROLLBACK_STATUS_MESSAGE));
Request expected = new Request();
expected.setRequestId(REQUEST_ID);
expected.setInstanceReferences(instanceReferences);
expected.setRequestStatus(requestStatus);
expected.setRequestScope(SERVICE);
Request result = orchestrationRequests.mapInfraActiveRequestToRequest(iar, includeCloudRequest,
OrchestrationRequestFormat.DETAIL.toString());
assertThat(result, sameBeanAs(expected));
}
@Test
public void mapRequestStatusAndExtSysErrSrcToRequestFalseTest() throws ApiException {
doReturn("Last task executed: Call SDNC").when(camundaRequestHandler).getTaskName(REQUEST_ID);
InstanceReferences instanceReferences = new InstanceReferences();
instanceReferences.setServiceInstanceId(SERVICE_INSTANCE_ID);
RequestStatus requestStatus = new RequestStatus();
requestStatus.setRequestState(iar.getRequestStatus());
requestStatus.setStatusMessage(String.format("FLOW STATUS: %s RETRY STATUS: %s ROLLBACK STATUS: %s",
FLOW_STATUS + TASK_INFORMATION, RETRY_STATUS_MESSAGE, ROLLBACK_STATUS_MESSAGE));
Request expected = new Request();
expected.setRequestId(REQUEST_ID);
expected.setInstanceReferences(instanceReferences);
expected.setRequestStatus(requestStatus);
expected.setRequestScope(SERVICE);
includeCloudRequest = false;
Request actual = orchestrationRequests.mapInfraActiveRequestToRequest(iar, includeCloudRequest,
OrchestrationRequestFormat.DETAIL.toString());
assertThat(actual, sameBeanAs(expected));
}
@Test
public void mapRequestStatusAndExtSysErrSrcToRequestStatusDetailTest() throws ApiException {
doReturn(null).when(camundaRequestHandler).getTaskName(REQUEST_ID);
InstanceReferences instanceReferences = new InstanceReferences();
instanceReferences.setServiceInstanceId(SERVICE_INSTANCE_ID);
RequestStatus requestStatus = new RequestStatus();
requestStatus.setExtSystemErrorSource(EXT_SYSTEM_ERROR_SOURCE);
requestStatus.setRollbackExtSystemErrorSource(ROLLBACK_EXT_SYSTEM_ERROR_SOURCE);
requestStatus.setRequestState(iar.getRequestStatus());
requestStatus.setFlowStatus(FLOW_STATUS);
requestStatus.setRollbackStatusMessage(ROLLBACK_STATUS_MESSAGE);
requestStatus.setRetryStatusMessage(RETRY_STATUS_MESSAGE);
Request expected = new Request();
expected.setRequestId(REQUEST_ID);
expected.setInstanceReferences(instanceReferences);
expected.setRequestStatus(requestStatus);
expected.setRequestScope(SERVICE);
includeCloudRequest = false;
Request actual = orchestrationRequests.mapInfraActiveRequestToRequest(iar, includeCloudRequest,
OrchestrationRequestFormat.STATUSDETAIL.toString());
assertThat(actual, sameBeanAs(expected));
}
@Test
public void mapRequestStatusAndExtSysErrSrcToRequestDetailTest() throws ApiException {
doReturn("Last task executed: Call SDNC").when(camundaRequestHandler).getTaskName(REQUEST_ID);
InstanceReferences instanceReferences = new InstanceReferences();
instanceReferences.setServiceInstanceId(SERVICE_INSTANCE_ID);
RequestStatus requestStatus = new RequestStatus();
requestStatus.setRequestState(iar.getRequestStatus());
requestStatus.setStatusMessage(String.format("FLOW STATUS: %s RETRY STATUS: %s ROLLBACK STATUS: %s",
FLOW_STATUS + TASK_INFORMATION, RETRY_STATUS_MESSAGE, ROLLBACK_STATUS_MESSAGE));
Request expected = new Request();
expected.setRequestId(REQUEST_ID);
expected.setInstanceReferences(instanceReferences);
expected.setRequestStatus(requestStatus);
expected.setRequestScope(SERVICE);
includeCloudRequest = false;
Request actual = orchestrationRequests.mapInfraActiveRequestToRequest(iar, includeCloudRequest,
OrchestrationRequestFormat.DETAIL.toString());
assertThat(actual, sameBeanAs(expected));
}
@Test
public void mapRequestStatusAndExtSysErrSrcToRequestNoFlowStatusTest() throws ApiException {
InstanceReferences instanceReferences = new InstanceReferences();
instanceReferences.setServiceInstanceId(SERVICE_INSTANCE_ID);
RequestStatus requestStatus = new RequestStatus();
requestStatus.setRequestState(iar.getRequestStatus());
requestStatus.setStatusMessage(
String.format("RETRY STATUS: %s ROLLBACK STATUS: %s", RETRY_STATUS_MESSAGE, ROLLBACK_STATUS_MESSAGE));
Request expected = new Request();
expected.setRequestId(REQUEST_ID);
expected.setInstanceReferences(instanceReferences);
expected.setRequestStatus(requestStatus);
expected.setRequestScope(SERVICE);
includeCloudRequest = false;
iar.setFlowStatus(null);
Request actual = orchestrationRequests.mapInfraActiveRequestToRequest(iar, includeCloudRequest,
OrchestrationRequestFormat.DETAIL.toString());
assertThat(actual, sameBeanAs(expected));
}
@Test
public void mapRequestStatusAndExtSysErrSrcToRequestErrorMessageTest() throws ApiException {
InstanceReferences instanceReferences = new InstanceReferences();
instanceReferences.setServiceInstanceId(SERVICE_INSTANCE_ID);
iar.setExtSystemErrorSource(ROLLBACK_EXT_SYSTEM_ERROR_SOURCE);
iar.setFlowStatus(null);
iar.setStatusMessage("Error retrieving cloud region from AAI");
Request actual = orchestrationRequests.mapInfraActiveRequestToRequest(iar, includeCloudRequest,
OrchestrationRequestFormat.DETAIL.toString());
assertTrue(actual.getRequestStatus().getStatusMessage()
.contains("Error Source: " + ROLLBACK_EXT_SYSTEM_ERROR_SOURCE));
}
@Test
public void mapRequestStatusAndExtSysErrSrcToRequestFlowStatusSuccessfulCompletionTest() throws ApiException {
InstanceReferences instanceReferences = new InstanceReferences();
instanceReferences.setServiceInstanceId(SERVICE_INSTANCE_ID);
RequestStatus requestStatus = new RequestStatus();
requestStatus.setRequestState(iar.getRequestStatus());
requestStatus.setStatusMessage(String.format("FLOW STATUS: %s RETRY STATUS: %s ROLLBACK STATUS: %s",
"Successfully completed all Building Blocks", RETRY_STATUS_MESSAGE, ROLLBACK_STATUS_MESSAGE));
Request expected = new Request();
expected.setRequestId(REQUEST_ID);
expected.setInstanceReferences(instanceReferences);
expected.setRequestStatus(requestStatus);
expected.setRequestScope(SERVICE);
includeCloudRequest = false;
iar.setFlowStatus("Successfully completed all Building Blocks");
Request actual = orchestrationRequests.mapInfraActiveRequestToRequest(iar, includeCloudRequest,
OrchestrationRequestFormat.DETAIL.toString());
assertThat(actual, sameBeanAs(expected));
}
@Test
public void mapRequestStatusAndExtSysErrSrcToRequestFlowStatusSuccessfulRollbackTest() throws ApiException {
InstanceReferences instanceReferences = new InstanceReferences();
instanceReferences.setServiceInstanceId(SERVICE_INSTANCE_ID);
RequestStatus requestStatus = new RequestStatus();
requestStatus.setRequestState(iar.getRequestStatus());
requestStatus.setStatusMessage(String.format("FLOW STATUS: %s RETRY STATUS: %s ROLLBACK STATUS: %s",
"All Rollback flows have completed successfully", RETRY_STATUS_MESSAGE, ROLLBACK_STATUS_MESSAGE));
Request expected = new Request();
expected.setRequestId(REQUEST_ID);
expected.setInstanceReferences(instanceReferences);
expected.setRequestStatus(requestStatus);
expected.setRequestScope(SERVICE);
includeCloudRequest = false;
iar.setFlowStatus("All Rollback flows have completed successfully");
Request actual = orchestrationRequests.mapInfraActiveRequestToRequest(iar, includeCloudRequest,
OrchestrationRequestFormat.DETAIL.toString());
assertThat(actual, sameBeanAs(expected));
}
@Test
public void requestStatusExtSystemErrorSourceTest() {
RequestStatus requestStatus = new RequestStatus();
requestStatus.setExtSystemErrorSource(EXT_SYSTEM_ERROR_SOURCE);
assertThat(requestStatus.getExtSystemErrorSource(), is(equalTo(EXT_SYSTEM_ERROR_SOURCE)));
}
@Test
public void mapRequestStatusToRequestForFormatDetailTest() throws ApiException {
iar.setRequestStatus(Status.ABORTED.toString());
String result =
orchestrationRequests.mapRequestStatusToRequest(iar, OrchestrationRequestFormat.DETAIL.toString());
assertEquals(Status.ABORTED.toString(), result);
}
@Test
public void mapRequestStatusToRequestForFormatStatusDetailTest() throws ApiException {
iar.setRequestStatus(Status.ABORTED.toString());
String result = orchestrationRequests.mapRequestStatusToRequest(iar, "statusDetail");
assertEquals(Status.ABORTED.toString(), result);
}
@Test
public void mapRequestStatusToRequestForFormatEmptyStringTest() throws ApiException {
iar.setRequestStatus(Status.ABORTED.toString());
String result = orchestrationRequests.mapRequestStatusToRequest(iar, StringUtils.EMPTY);
assertEquals(Status.FAILED.toString(), result);
}
@Test
public void infraActiveRequestNullValidationExceptionTest() throws ApiException {
iar.setRequestId(REQUEST_ID);
thrown.expect(ValidateException.class);
thrown.expectMessage(containsString("Null response from RequestDB when searching by RequestId " + REQUEST_ID));
orchestrationRequests.infraActiveRequestLookup(iar.getRequestId());
}
}
|