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
|
/*-
* ============LICENSE_START=======================================================
* ONAP - SO
* ================================================================================
* Copyright (C) 2017 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.tenantisolation;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.text.ParseException;
import javax.ws.rs.core.MediaType;
import org.apache.http.HttpStatus;
import org.junit.Before;
import org.junit.Test;
import org.onap.so.apihandlerinfra.BaseTest;
import org.onap.so.db.request.beans.InfraActiveRequests;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.util.UriComponentsBuilder;
public class CloudResourcesOrchestrationTest extends BaseTest{
private String requestJSON = "{\"requestDetails\":{\"requestInfo\":{\"source\":\"VID\",\"requestorId\":\"xxxxxx\" } } }";
private static final String path = "/onap/so/infra/cloudResourcesRequests";
HttpHeaders headers = new HttpHeaders();
@Before
public void setupTestClass() throws Exception{
wireMockServer.stubFor(post(urlPathEqualTo(getTestUrl(""))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).withStatus(HttpStatus.SC_CREATED)));
}
@Test
public void testUnlockFailObjectMapping() {
headers.set("Accept", MediaType.APPLICATION_JSON);
headers.set("Content-Type", MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<String>(null, headers);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/test/unlock");
ResponseEntity<String> response = restTemplate.exchange(
builder.toUriString(),
HttpMethod.POST, entity, String.class);
String body = response.getBody();
assertTrue(body.contains("Mapping of request to JSON object failed."));
assertEquals(400, response.getStatusCodeValue());
}
@Test
public void testParseOrchestrationError1() {
String requestJSON = "{\"requestDetails\": null }";
headers.set("Accept", MediaType.APPLICATION_JSON);
headers.set("Content-Type", MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/test/unlock");
ResponseEntity<String> response = restTemplate.exchange(
builder.toUriString(),
HttpMethod.POST, entity, String.class);
String body = response.getBody();
assertTrue(body.contains("No valid requestDetails is specified"));
assertEquals(400, response.getStatusCodeValue());
}
@Test
public void testParseOrchestrationError2() {
String requestJSON = "{\"requestDetails\":{\"requestInfo\":{\"source\":\"\",\"requestorId\":\"xxxxxx\" } } }";
headers.set("Accept", MediaType.APPLICATION_JSON);
headers.set("Content-Type", MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/test/unlock");
ResponseEntity<String> response = restTemplate.exchange(
builder.toUriString(),
HttpMethod.POST, entity, String.class);
String body = response.getBody();
assertTrue(body.contains("No valid source is specified"));
assertEquals(400, response.getStatusCodeValue());
}
@Test
public void testParseOrchestrationError3() {
String requestJSON = "{\"requestDetails\":{\"requestInfo\":{\"source\":\"VID\",\"requestorId\":\"\" } } }";
headers.set("Accept", MediaType.APPLICATION_JSON);
headers.set("Content-Type", MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/test/unlock");
ResponseEntity<String> response = restTemplate.exchange(
builder.toUriString(),
HttpMethod.POST, entity, String.class);
String body = response.getBody();
assertTrue(body.contains("No valid requestorId is specified"));
assertEquals(400, response.getStatusCodeValue());
}
@Test
public void testGetInfraActiveRequestNull() {
wireMockServer.stubFor(get(urlPathEqualTo(getTestUrl("request-id-null-check"))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
.withStatus(HttpStatus.SC_OK)));
headers.set("Accept", MediaType.APPLICATION_JSON);
headers.set("Content-Type", MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/request-id-null-check/unlock");
ResponseEntity<String> response = restTemplate.exchange(
builder.toUriString(),
HttpMethod.POST, entity, String.class);
String body = response.getBody();
assertTrue(body.contains("Orchestration RequestId request-id-null-check is not found in DB"));
assertEquals(400, response.getStatusCodeValue());
}
@Test
public void testUnlock() throws ParseException {
wireMockServer.stubFor(get(urlPathEqualTo(getTestUrl("requestIdtestUnlock"))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
.withBody(String.format(getResponseTemplate, "requestIdtestUnlock", "IN_PROGRESS"))
.withStatus(HttpStatus.SC_OK)));
headers.set("Accept", MediaType.APPLICATION_JSON);
headers.set("Content-Type", MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<>(requestJSON, headers);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/requestIdtestUnlock/unlock");
ResponseEntity<String> response = restTemplate.exchange(
builder.toUriString(),
HttpMethod.POST, entity, String.class);
assertEquals(204, response.getStatusCodeValue());
}
@Test
public void testUnlockComplete() throws ParseException {
wireMockServer.stubFor(get(urlPathEqualTo(getTestUrl("requestIdtestUnlockComplete"))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
.withBody(String.format(getResponseTemplate, "requestIdtestUnlockComplete", "COMPLETE"))
.withStatus(HttpStatus.SC_OK)));
headers.set("Accept", MediaType.APPLICATION_JSON);
headers.set("Content-Type", MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<>(requestJSON, headers);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/requestIdtestUnlockComplete/unlock");
ResponseEntity<String> response = restTemplate.exchange(
builder.toUriString(),
HttpMethod.POST, entity, String.class);
String body = response.getBody().toString();
assertTrue(body.contains("Orchestration RequestId requestIdtestUnlockComplete has a status of COMPLETE and can not be unlocked"));
assertEquals(400, response.getStatusCodeValue());
}
@Test
public void testGetOperationalEnvFilter() {
wireMockServer.stubFor(get(urlPathEqualTo(getTestUrl("not-there"))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
.withStatus(HttpStatus.SC_OK)));
headers.set("Accept", MediaType.APPLICATION_JSON);
headers.set("Content-Type", MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<>(null, headers);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1");
builder.queryParam("requestId", "not-there");
ResponseEntity<String> response = restTemplate.exchange(
builder.toUriString(),
HttpMethod.GET, entity, String.class);
//204s cannot have a body
//assertTrue(response.getBody().contains("Orchestration RequestId not-there is not found in DB"));
assertEquals(204, response.getStatusCodeValue());
}
@Test
public void testGetOperationalEnvSuccess() throws ParseException {
wireMockServer.stubFor(get(urlPathEqualTo(getTestUrl("90c56827-1c78-4827-bc4d-6afcdb37a51f"))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
.withBody(String.format(getResponseTemplateNoBody, "90c56827-1c78-4827-bc4d-6afcdb37a51f", "COMPLETE"))
.withStatus(HttpStatus.SC_OK)));
headers.set("Accept", MediaType.APPLICATION_JSON);
headers.set("Content-Type", MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<>("", headers);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1");
builder.queryParam("requestId", "90c56827-1c78-4827-bc4d-6afcdb37a51f");
ResponseEntity<String> response = restTemplate.exchange(
builder.toUriString(),
HttpMethod.GET, entity, String.class);
assertEquals(200, response.getStatusCodeValue());
assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
assertEquals("1.0.0", response.getHeaders().get("X-LatestVersion").get(0));
assertEquals("90c56827-1c78-4827-bc4d-6afcdb37a51f", response.getHeaders().get("X-TransactionID").get(0));
}
@Test
public void testGetOperationalEnvFilterSuccess() throws ParseException {
wireMockServer.stubFor(get(urlPathEqualTo(getTestUrl("requestIdtestGetOperationalEnvFilterSuccess"))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
.withBody(String.format(getResponseTemplate, "requestIdtestGetOperationalEnvFilterSuccess", "COMPLETE"))
.withStatus(HttpStatus.SC_OK)));
wireMockServer.stubFor(post(urlPathEqualTo(getTestUrl("getCloudOrchestrationFiltersFromInfraActive"))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
.withBody("{\"requestId\":\"getCloudOrchestrationFiltersFromInfraActive\", \"operationalEnvironmentName\":\"myVnfOpEnv\"}")
.withBody("["+String.format(getResponseTemplateNoBody, "requestIdtestGetOperationalEnvFilterSuccess", "COMPLETE")+"]")
.withStatus(HttpStatus.SC_OK)));
headers.set("Accept", MediaType.APPLICATION_JSON);
headers.set("Content-Type", MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<>(null, headers);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1");
builder.queryParam("requestId", "requestIdtestGetOperationalEnvFilterSuccess");
builder.queryParam("operationalEnvironmentName", "myVnfOpEnv");
ResponseEntity<String> response = restTemplate.exchange(
builder.toUriString(),
HttpMethod.GET, entity, String.class);
assertEquals(200, response.getStatusCodeValue());
assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
assertEquals("1.0.0", response.getHeaders().get("X-LatestVersion").get(0));
}
@Test
public void testGetOperationalEnvFilterException1() throws ParseException {
InfraActiveRequests iar = new InfraActiveRequests();
iar.setRequestId("requestId-getOpEnvFilterEx1");
iar.setRequestScope("requestScope");
iar.setRequestType("requestType");
iar.setOperationalEnvId("operationalEnvironmentId");
iar.setOperationalEnvName("operationalEnvName");
iar.setRequestorId("xxxxxx");
iar.setRequestBody("");
iar.setRequestStatus("COMPLETE");
iar.setRequestAction("TEST");
headers.set("Accept", MediaType.APPLICATION_JSON);
headers.set("Content-Type", MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<>("", headers);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1");
builder.queryParam("filter", "operationalEnvironmentName:EQUALS:myVnfOpEnv");
ResponseEntity<String> response = restTemplate.exchange(
builder.toUriString(),
HttpMethod.GET, entity, String.class);
assertEquals(500, response.getStatusCodeValue());
}
@Test
public void testGetOperationalEnvFilterException2() throws ParseException {
InfraActiveRequests iar = new InfraActiveRequests();
iar.setRequestId("requestIdFilterException2");
iar.setRequestScope("requestScope");
iar.setRequestType("requestType");
iar.setOperationalEnvId("operationalEnvId");
iar.setOperationalEnvName("operationalEnvName");
iar.setRequestorId("xxxxxx");
iar.setRequestBody("");
iar.setRequestStatus("COMPLETE");
iar.setRequestAction("TEST");
headers.set("Accept", MediaType.APPLICATION_JSON);
headers.set("Content-Type", MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<>(null, headers);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1");
builder.queryParam("operationalEnvironmentName", "");
ResponseEntity<String> response = restTemplate.exchange(
builder.toUriString(),
HttpMethod.GET, entity, String.class);
assertEquals(500, response.getStatusCodeValue());
assertTrue(response.getBody().toString().contains("No valid operationalEnvironmentName value is specified"));
}
}
|