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
|
/*-
* ============LICENSE_START=======================================================
* ONAP : APPC
* ================================================================================
* Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Copyright (C) 2017 Amdocs
* =============================================================================
* 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.appc.requesthandler.impl;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.onap.appc.adapter.message.MessageAdapterFactory;
import org.onap.appc.adapter.message.Producer;
import org.onap.appc.domainmodel.lcm.ActionLevel;
import org.onap.appc.domainmodel.lcm.RequestContext;
import org.onap.appc.domainmodel.lcm.RequestStatus;
import org.onap.appc.domainmodel.lcm.ResponseContext;
import org.onap.appc.domainmodel.lcm.RuntimeContext;
import org.onap.appc.domainmodel.lcm.VNFOperation;
import org.onap.appc.exceptions.APPCException;
import org.onap.appc.requesthandler.exceptions.MultipleRecordsRetrievedException;
import org.onap.appc.transactionrecorder.TransactionRecorder;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceReference;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyCollection;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
/**
* Test class for LocalRequestHandlerImpl
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({FrameworkUtil.class})
public class LocalRequestHandlerImplTest implements LocalRequestHanlderTestHelper {
private final String requestId = "requestId";
private final String vnfId = "vnfId";
private final String subRequestId = "subRequestId";
private final String originatorId = "originatorId";
private final String ACCEPTED_IN_PROGRESS_Response = "{\"status-reason\":\"ACCEPTED\"," +
"\"status\":\"IN_PROGRESS\"}";
private final String SUCCESSFUL_SUCCESSFUL_Response = "{\"status-reason\":\"SUCCESSFUL\"," +
"\"status\":\"SUCCESSFUL\"}";
private final String RECEIVED_IN_PROGRESS_Response = "{\"status-reason\":\"RECEIVED\"," +
"\"status\":\"IN_PROGRESS\"}";
private final String UNKNOWN_FAILED_Response = "{\"status-reason\":\"UNKNOWN\"," +
"\"status\":\"FAILED\"}";
private LocalRequestHandlerImpl requestHandler;
private TransactionRecorder recorder;
@Before
public void setUp() throws Exception {
mockStatic(FrameworkUtil.class);
Bundle myBundle = mock(Bundle.class);
PowerMockito.when(FrameworkUtil.getBundle(any())).thenReturn(myBundle);
BundleContext myBundleContext = mock(BundleContext.class);
Mockito.when(myBundle.getBundleContext()).thenReturn(myBundleContext);
ServiceReference svcRef = mock(ServiceReference.class);
Mockito.when(myBundleContext.getServiceReference(MessageAdapterFactory.class.getName())).thenReturn(svcRef);
Producer producer = mock(Producer.class);
MessageAdapterFactory factory = mock(MessageAdapterFactory.class);
Mockito.when(myBundleContext.getService(svcRef)).thenReturn(factory);
Mockito.when(factory.createProducer(anyCollection(), anyString(), anyString(), anyString())).thenReturn
(producer);
requestHandler = spy(new LocalRequestHandlerImpl());
recorder = mock(TransactionRecorder.class);
requestHandler.setTransactionRecorder(recorder);
List<RequestStatus> result = Arrays.asList(RequestStatus.ACCEPTED);
PowerMockito.when(recorder.getRecords(anyString(), anyString(), anyString(), anyString())).thenReturn(result);
}
/**
* Test response which contains status=Successful, status-reason=Successful
* <p>
* Search criteria one = vnf-id + request-id + sub-request-id + originator-id
*/
@Test
public void testHandleRequestSuccessfulWithSearchCriteriaOne() throws APPCException {
final String payload = "{\"request-id\":\"requestId\"," +
"\"sub-request-id\":\"subRequestId\"," +
"\"originator-id\":\"originatorId\"}";
List<RequestStatus> result = Arrays.asList(RequestStatus.SUCCESSFUL);
PowerMockito.when(recorder.getRecords(requestId, subRequestId, originatorId, vnfId)).thenReturn(result);
RuntimeContext runtimeContext = createRequestHandlerRuntimeContext(vnfId, payload);
requestHandler.handleRequest(runtimeContext);
Assert.assertTrue(SUCCESSFUL_SUCCESSFUL_Response.equals(runtimeContext.getResponseContext().getPayload()));
}
/**
* Test response which contains status=IN_PROGRESS, status-reason=RECEIVED
* <p>
* Search criteria one = vnf-id + request-id + sub-request-id + originator-id
*/
@Test
public void testHandleRequestReceivedWithSearchCriteriaOne() throws APPCException {
final String payload = "{\"request-id\":\"requestId\"," +
"\"sub-request-id\":\"subRequestId\"," +
"\"originator-id\":\"originatorId\"}";
List<RequestStatus> result = Arrays.asList(RequestStatus.RECEIVED);
PowerMockito.when(recorder.getRecords(requestId, subRequestId, originatorId, vnfId)).thenReturn(result);
RuntimeContext runtimeContext = createRequestHandlerRuntimeContext(vnfId, payload);
requestHandler.handleRequest(runtimeContext);
Assert.assertTrue(RECEIVED_IN_PROGRESS_Response.equals(runtimeContext.getResponseContext().getPayload()));
}
/**
* Test response which contains status=FAILED, status-reason=UNKNOWN
* <p>
* Search criteria two = vnf-id + request-id + sub-request-id
*/
@Test
public void testHandleRequestFailedWithSearchCriteriaTwo() throws APPCException {
final String payload = "{\"request-id\":\"requestId\"," +
"\"sub-request-id\":\"subRequestId\"}";
List<RequestStatus> result = Arrays.asList(RequestStatus.UNKNOWN);
PowerMockito.when(recorder.getRecords(requestId, subRequestId, null, vnfId)).thenReturn(result);
RuntimeContext runtimeContext = createRequestHandlerRuntimeContext(vnfId, payload);
requestHandler.handleRequest(runtimeContext);
Assert.assertTrue(UNKNOWN_FAILED_Response.equals(runtimeContext.getResponseContext().getPayload()));
}
@Test
public void testSuccessHandleRequest() throws Exception {
RuntimeContext runtimeContext = createRequestHandlerRuntimeContext("vnfId",
"{\"request-id\":\"request-id\"}");
requestHandler.handleRequest(runtimeContext);
Assert.assertTrue(ACCEPTED_IN_PROGRESS_Response.equals(runtimeContext.getResponseContext().getPayload()));
}
@Test(expected = IllegalArgumentException.class)
public void testHandleRequestVnfIdFailure() throws Exception {
RuntimeContext runtimeContext = createRequestHandlerRuntimeContext(null, "{\"request-id\":\"request-id\"}");
runtimeContext.getRequestContext().getActionIdentifiers().setVnfId(null);
requestHandler.handleRequest(runtimeContext);
}
@Test(expected = IllegalArgumentException.class)
public void testHandleRequestInvalidPayload() throws Exception {
String incorrectPayload = "{\"RequestId\":\"requestToCheck\"}";
RuntimeContext runtimeContext = createRequestHandlerRuntimeContext("vnfId", incorrectPayload);
requestHandler.handleRequest(runtimeContext);
}
@Test
public void testGetStatusOfRequestSuccess() throws Exception {
List<RequestStatus> result = Arrays.asList(RequestStatus.ACCEPTED);
PowerMockito.when(recorder.getRecords(requestId, null, null, vnfId)).thenReturn(result);
RequestStatus status = Whitebox.invokeMethod(requestHandler,
"getStatusOfRequest", requestId, null, null, vnfId);
Assert.assertTrue(status.name().equals("ACCEPTED"));
}
@Test(expected = MultipleRecordsRetrievedException.class)
public void testGetStatusOfRequest_MoreThanOne() throws Exception {
List<RequestStatus> result = Arrays.asList(RequestStatus.ACCEPTED, RequestStatus.FAILED);
PowerMockito.when(recorder.getRecords(requestId, null, null, vnfId)).thenReturn(result);
RequestStatus status = Whitebox.invokeMethod(requestHandler,
"getStatusOfRequest", requestId, null, null, vnfId);
Assert.assertNull(status);
}
@Test
public void testGetStatusOfRequestNotFound() throws Exception {
List<RequestStatus> result = new ArrayList<>();
PowerMockito.when(recorder.getRecords(requestId, null, null, vnfId)).thenReturn(result);
RequestStatus status = Whitebox.invokeMethod(requestHandler,
"getStatusOfRequest", requestId, null, null, vnfId);
Assert.assertTrue(status.name().equals("NOT_FOUND"));
}
}
|