replace all fixed wiremock ports
[so.git] / mso-api-handlers / mso-api-handler-infra / src / test / java / org / onap / so / apihandlerinfra / tenantisolation / CloudResourcesOrchestrationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T 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.so.apihandlerinfra.tenantisolation;
22
23 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
24 import static com.github.tomakehurst.wiremock.client.WireMock.get;
25 import static com.github.tomakehurst.wiremock.client.WireMock.post;
26 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertTrue;
29
30 import java.text.ParseException;
31
32 import javax.ws.rs.core.MediaType;
33
34 import org.apache.http.HttpStatus;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.onap.so.apihandlerinfra.BaseTest;
38 import org.onap.so.db.request.beans.InfraActiveRequests;
39 import org.springframework.http.HttpEntity;
40 import org.springframework.http.HttpHeaders;
41 import org.springframework.http.HttpMethod;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.web.util.UriComponentsBuilder;
44
45
46 public class CloudResourcesOrchestrationTest extends BaseTest{
47         
48         private String requestJSON = "{\"requestDetails\":{\"requestInfo\":{\"source\":\"VID\",\"requestorId\":\"xxxxxx\" } } }";
49         private static final String path = "/onap/so/infra/cloudResourcesRequests";
50
51         HttpHeaders headers = new HttpHeaders();
52         @Before
53         public void setupTestClass() throws Exception{
54                 wireMockServer.stubFor(post(urlPathEqualTo(getTestUrl(""))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).withStatus(HttpStatus.SC_CREATED)));
55         }
56         @Test
57         public void testUnlockFailObjectMapping() {
58                 
59                 headers.set("Accept", MediaType.APPLICATION_JSON);
60                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
61                 HttpEntity<String> entity = new HttpEntity<String>(null, headers);
62         
63                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/test/unlock");
64                        
65                 ResponseEntity<String> response = restTemplate.exchange(
66                                 builder.toUriString(),
67                                 HttpMethod.POST, entity, String.class);
68                 
69                 String body = response.getBody();
70                 assertTrue(body.contains("Mapping of request to JSON object failed."));
71                 assertEquals(400, response.getStatusCodeValue());
72         }
73         
74         @Test
75         public void testParseOrchestrationError1() {
76                 String requestJSON = "{\"requestDetails\": null }";
77                 headers.set("Accept", MediaType.APPLICATION_JSON);
78                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
79                 HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
80                 
81                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/test/unlock");
82                        
83                 ResponseEntity<String> response = restTemplate.exchange(
84                                 builder.toUriString(),
85                                 HttpMethod.POST, entity, String.class);
86                 String body = response.getBody();
87                 assertTrue(body.contains("No valid requestDetails is specified"));
88                 assertEquals(400, response.getStatusCodeValue());
89         }
90         
91         @Test
92         public void testParseOrchestrationError2() {
93                 String requestJSON = "{\"requestDetails\":{\"requestInfo\":{\"source\":\"\",\"requestorId\":\"xxxxxx\" } } }";
94                 headers.set("Accept", MediaType.APPLICATION_JSON);
95                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
96                 HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
97
98                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/test/unlock");
99                        
100                 ResponseEntity<String> response = restTemplate.exchange(
101                                 builder.toUriString(),
102                                 HttpMethod.POST, entity, String.class);
103                 String body = response.getBody();
104                 assertTrue(body.contains("No valid source is specified"));
105                 assertEquals(400, response.getStatusCodeValue());
106         }
107         
108         @Test
109         public void testParseOrchestrationError3() {
110                 String requestJSON = "{\"requestDetails\":{\"requestInfo\":{\"source\":\"VID\",\"requestorId\":\"\" } } }";
111                 headers.set("Accept", MediaType.APPLICATION_JSON);
112                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
113                 HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
114
115                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/test/unlock");
116                        
117                 ResponseEntity<String> response = restTemplate.exchange(
118                                 builder.toUriString(),
119                                 HttpMethod.POST, entity, String.class);
120                 String body = response.getBody();
121                 assertTrue(body.contains("No valid requestorId is specified"));
122                 assertEquals(400, response.getStatusCodeValue());
123         }
124         
125         @Test
126         public void testGetInfraActiveRequestNull() {
127                 wireMockServer.stubFor(get(urlPathEqualTo(getTestUrl("request-id-null-check"))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
128                                 .withStatus(HttpStatus.SC_OK)));
129                 headers.set("Accept", MediaType.APPLICATION_JSON);
130                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
131                 HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
132
133                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/request-id-null-check/unlock");
134                        
135                 ResponseEntity<String> response = restTemplate.exchange(
136                                 builder.toUriString(),
137                                 HttpMethod.POST, entity, String.class);
138                 String body = response.getBody();
139                 assertTrue(body.contains("Orchestration RequestId request-id-null-check is not found in DB"));
140                 assertEquals(400, response.getStatusCodeValue());
141
142         }
143
144         @Test
145         public void testUnlock() throws ParseException {
146                 wireMockServer.stubFor(get(urlPathEqualTo(getTestUrl("requestIdtestUnlock"))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
147                                 .withBody(String.format(getResponseTemplate, "requestIdtestUnlock", "IN_PROGRESS"))
148                                 .withStatus(HttpStatus.SC_OK)));
149                 headers.set("Accept", MediaType.APPLICATION_JSON);
150                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
151                 HttpEntity<String> entity = new HttpEntity<>(requestJSON, headers);
152         
153                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/requestIdtestUnlock/unlock");
154                        
155                 ResponseEntity<String> response = restTemplate.exchange(
156                                 builder.toUriString(),
157                                 HttpMethod.POST, entity, String.class);
158                 
159                 assertEquals(204, response.getStatusCodeValue());
160         }
161         
162         @Test
163         public void testUnlockComplete() throws ParseException {
164                 wireMockServer.stubFor(get(urlPathEqualTo(getTestUrl("requestIdtestUnlockComplete"))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
165                                 .withBody(String.format(getResponseTemplate, "requestIdtestUnlockComplete", "COMPLETE"))
166                                 .withStatus(HttpStatus.SC_OK)));
167
168                 headers.set("Accept", MediaType.APPLICATION_JSON);
169                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
170                 HttpEntity<String> entity = new HttpEntity<>(requestJSON, headers);
171         
172                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/requestIdtestUnlockComplete/unlock");
173                        
174                 ResponseEntity<String> response = restTemplate.exchange(
175                                 builder.toUriString(),
176                                 HttpMethod.POST, entity, String.class);
177                 String body = response.getBody().toString();
178                 assertTrue(body.contains("Orchestration RequestId requestIdtestUnlockComplete has a status of COMPLETE and can not be unlocked"));
179                 assertEquals(400, response.getStatusCodeValue());
180         }
181         
182         @Test
183         public void testGetOperationalEnvFilter() {
184                 wireMockServer.stubFor(get(urlPathEqualTo(getTestUrl("not-there"))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
185                                 .withStatus(HttpStatus.SC_OK)));
186                 headers.set("Accept", MediaType.APPLICATION_JSON);
187                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
188                 HttpEntity<String> entity = new HttpEntity<>(null, headers);
189         
190                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1");
191                 
192                 builder.queryParam("requestId", "not-there");
193                        
194                 ResponseEntity<String> response = restTemplate.exchange(
195                                 builder.toUriString(),
196                                 HttpMethod.GET, entity, String.class);
197                 
198                 //204s cannot have a body
199                 //assertTrue(response.getBody().contains("Orchestration RequestId not-there is not found in DB"));
200                 assertEquals(204, response.getStatusCodeValue());
201         }
202         
203         @Test
204         public void testGetOperationalEnvSuccess() throws ParseException {
205                 wireMockServer.stubFor(get(urlPathEqualTo(getTestUrl("90c56827-1c78-4827-bc4d-6afcdb37a51f"))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
206                                 .withBody(String.format(getResponseTemplateNoBody, "90c56827-1c78-4827-bc4d-6afcdb37a51f", "COMPLETE"))
207                                 .withStatus(HttpStatus.SC_OK)));
208                 headers.set("Accept", MediaType.APPLICATION_JSON);
209                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
210                 HttpEntity<String> entity = new HttpEntity<>("", headers);
211         
212                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1");
213                 
214                 builder.queryParam("requestId", "90c56827-1c78-4827-bc4d-6afcdb37a51f");
215                        
216                 ResponseEntity<String> response = restTemplate.exchange(
217                                 builder.toUriString(),
218                                 HttpMethod.GET, entity, String.class);
219                 
220                 assertEquals(200, response.getStatusCodeValue());
221         assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
222         assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
223         assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
224         assertEquals("1.0.0", response.getHeaders().get("X-LatestVersion").get(0));
225         assertEquals("90c56827-1c78-4827-bc4d-6afcdb37a51f", response.getHeaders().get("X-TransactionID").get(0));
226         }
227         
228         @Test
229         public void testGetOperationalEnvFilterSuccess() throws ParseException {
230                 wireMockServer.stubFor(get(urlPathEqualTo(getTestUrl("requestIdtestGetOperationalEnvFilterSuccess"))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
231                                 .withBody(String.format(getResponseTemplate, "requestIdtestGetOperationalEnvFilterSuccess", "COMPLETE"))
232                                 .withStatus(HttpStatus.SC_OK)));
233
234                 wireMockServer.stubFor(post(urlPathEqualTo(getTestUrl("getCloudOrchestrationFiltersFromInfraActive"))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
235                                 .withBody("{\"requestId\":\"getCloudOrchestrationFiltersFromInfraActive\", \"operationalEnvironmentName\":\"myVnfOpEnv\"}")
236                                 .withBody("["+String.format(getResponseTemplateNoBody, "requestIdtestGetOperationalEnvFilterSuccess", "COMPLETE")+"]")
237                                 .withStatus(HttpStatus.SC_OK)));
238
239                 headers.set("Accept", MediaType.APPLICATION_JSON);
240                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
241                 HttpEntity<String> entity = new HttpEntity<>(null, headers);
242                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1");
243                 
244                 builder.queryParam("requestId", "requestIdtestGetOperationalEnvFilterSuccess");
245                 builder.queryParam("operationalEnvironmentName", "myVnfOpEnv");
246                        
247                 ResponseEntity<String> response = restTemplate.exchange(
248                                 builder.toUriString(),
249                                 HttpMethod.GET, entity, String.class);
250                 
251                 assertEquals(200, response.getStatusCodeValue());
252         assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
253         assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
254         assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
255         assertEquals("1.0.0", response.getHeaders().get("X-LatestVersion").get(0));
256
257         }
258         
259         @Test
260         public void testGetOperationalEnvFilterException1() throws ParseException {
261                 InfraActiveRequests iar = new InfraActiveRequests();
262                 iar.setRequestId("requestId-getOpEnvFilterEx1");
263                 iar.setRequestScope("requestScope");
264                 iar.setRequestType("requestType");
265                 iar.setOperationalEnvId("operationalEnvironmentId");
266                 iar.setOperationalEnvName("operationalEnvName");
267                 iar.setRequestorId("xxxxxx");
268                 iar.setRequestBody("");
269                 iar.setRequestStatus("COMPLETE");
270                 iar.setRequestAction("TEST");
271                 
272                 headers.set("Accept", MediaType.APPLICATION_JSON);
273                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
274                 HttpEntity<String> entity = new HttpEntity<>("", headers);
275                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1");
276                 
277                 builder.queryParam("filter", "operationalEnvironmentName:EQUALS:myVnfOpEnv");
278                        
279                 ResponseEntity<String> response = restTemplate.exchange(
280                                 builder.toUriString(),
281                                 HttpMethod.GET, entity, String.class);
282                 assertEquals(500, response.getStatusCodeValue());
283         }
284         
285         @Test
286         public void testGetOperationalEnvFilterException2() throws ParseException {
287                 InfraActiveRequests iar = new InfraActiveRequests();
288                 iar.setRequestId("requestIdFilterException2");
289                 iar.setRequestScope("requestScope");
290                 iar.setRequestType("requestType");
291                 iar.setOperationalEnvId("operationalEnvId");
292                 iar.setOperationalEnvName("operationalEnvName");
293                 iar.setRequestorId("xxxxxx");
294                 iar.setRequestBody("");
295                 iar.setRequestStatus("COMPLETE");
296                 iar.setRequestAction("TEST");
297                 
298                 headers.set("Accept", MediaType.APPLICATION_JSON);
299                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
300                 HttpEntity<String> entity = new HttpEntity<>(null, headers);
301                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1");
302                 
303                 builder.queryParam("operationalEnvironmentName", "");
304                        
305                 ResponseEntity<String> response = restTemplate.exchange(
306                                 builder.toUriString(),
307                                 HttpMethod.GET, entity, String.class);
308                 
309                 
310                 assertEquals(500, response.getStatusCodeValue());
311                 assertTrue(response.getBody().toString().contains("No valid operationalEnvironmentName value is specified"));
312         }
313
314 }