Replaced all tabs with spaces in java and pom.xml
[so.git] / mso-api-handlers / mso-api-handler-infra / src / test / java / org / onap / so / apihandlerinfra / tenantisolation / CloudOrchestrationTest.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.equalTo;
25 import static com.github.tomakehurst.wiremock.client.WireMock.get;
26 import static com.github.tomakehurst.wiremock.client.WireMock.post;
27 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertTrue;
30 import java.io.File;
31 import java.io.IOException;
32 import javax.ws.rs.core.MediaType;
33 import org.apache.http.HttpStatus;
34 import org.junit.Before;
35 import org.junit.Ignore;
36 import org.junit.Test;
37 import org.onap.so.apihandlerinfra.BaseTest;
38 import org.onap.so.apihandlerinfra.Status;
39 import org.onap.so.apihandlerinfra.tenantisolationbeans.Action;
40 import org.onap.so.apihandlerinfra.tenantisolationbeans.TenantIsolationRequest;
41 import org.onap.so.db.request.beans.InfraActiveRequests;
42 import org.springframework.http.HttpEntity;
43 import org.springframework.http.HttpHeaders;
44 import org.springframework.http.HttpMethod;
45 import org.springframework.http.ResponseEntity;
46 import org.springframework.web.util.UriComponentsBuilder;
47 import com.fasterxml.jackson.databind.ObjectMapper;
48
49
50 public class CloudOrchestrationTest extends BaseTest {
51
52     private static final String path = "/onap/so/infra/cloudResources/v1";
53
54     private HttpHeaders headers = new HttpHeaders();
55
56     @Before
57     public void setupTestClass() throws Exception {
58         wireMockServer.stubFor(post(urlPathEqualTo(getTestUrl(""))).willReturn(
59                 aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
60                         .withStatus(HttpStatus.SC_CREATED)));
61     }
62
63     @Test
64     public void testCreateOpEnvObjectMapperError() throws IOException {
65
66         headers.set("Accept", MediaType.APPLICATION_JSON);
67         headers.set("Content-Type", MediaType.APPLICATION_JSON);
68         HttpEntity<String> entity = new HttpEntity<String>(null, headers);
69
70         UriComponentsBuilder builder =
71                 UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
72         ResponseEntity<String> response =
73                 restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
74
75         String body = response.getBody().toString();
76         assertTrue(body.contains("Mapping of request to JSON object failed."));
77         assertEquals(400, response.getStatusCodeValue());
78     }
79
80     @Test
81     public void testCreateOpEnvError() throws IOException {
82
83         String request =
84                 "{\"requestDetails\":{\"requestInfo\":{\"resourceType\":\"operationalEnvironment\",\"instanceName\": \"myOpEnv\",\"source\": \"VID\",\"requestorId\": \"xxxxxx\"},"
85                         + "     \"requestParameters\": {\"tenantContext\": \"Test\",\"workloadContext\": \"ECOMP_E2E-IST\"}}}";
86         headers.set("Accept", MediaType.APPLICATION_JSON);
87         headers.set("Content-Type", MediaType.APPLICATION_JSON);
88         HttpEntity<String> entity = new HttpEntity<String>(request, headers);
89
90         UriComponentsBuilder builder =
91                 UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
92
93         ResponseEntity<String> response =
94                 restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
95
96         String body = response.getBody().toString();
97         assertTrue(body.contains("Mapping of request to JSON object failed"));
98         assertEquals(400, response.getStatusCodeValue());
99     }
100
101     @Test
102     public void testCreateOpEnvReqRecordDuplicateCheck() throws IOException {
103         wireMockServer.stubFor(post(urlPathEqualTo(getTestUrl("checkInstanceNameDuplicate"))).withRequestBody(equalTo(
104                 "{\"instanceIdMap\":null,\"instanceName\":\"myOpEnv\",\"requestScope\":\"operationalEnvironment\"}"))
105                 .willReturn(aResponse()
106                         .withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
107                         .withBody(String.format(getResponseTemplate, "123", "PENDING")).withStatus(HttpStatus.SC_OK)));
108         ObjectMapper mapper = new ObjectMapper();
109         TenantIsolationRequest request =
110                 mapper.readValue(new File("src/test/resources/TenantIsolation/ECOMPOperationEnvironmentCreate.json"),
111                         TenantIsolationRequest.class);
112         headers.set("Accept", MediaType.APPLICATION_JSON);
113         headers.set("Content-Type", MediaType.APPLICATION_JSON);
114         HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
115
116
117         UriComponentsBuilder builder =
118                 UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
119
120         ResponseEntity<String> response =
121                 restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
122
123         assertEquals(409, response.getStatusCodeValue());
124     }
125
126     @Test
127     public void testCreateOperationalEnvironment() throws IOException {
128         ObjectMapper mapper = new ObjectMapper();
129         TenantIsolationRequest request =
130                 mapper.readValue(new File("src/test/resources/TenantIsolation/ECOMPOperationEnvironmentCreate.json"),
131                         TenantIsolationRequest.class);
132         headers.set("Accept", MediaType.APPLICATION_JSON);
133         headers.set("Content-Type", MediaType.APPLICATION_JSON);
134         headers.set("X-TransactionID", "987654321");
135         HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
136
137         UriComponentsBuilder builder =
138                 UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
139         wireMockServer.stubFor(post(urlPathEqualTo(getTestUrl("checkInstanceNameDuplicate"))).withRequestBody(equalTo(
140                 "{\"instanceIdMap\":null,\"instanceName\":\"myOpEnv\",\"requestScope\":\"operationalEnvironment\"}"))
141                 .willReturn(
142                         aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
143                                 .withStatus(HttpStatus.SC_NOT_FOUND)));
144         ResponseEntity<String> response =
145                 restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
146         assertEquals(200, response.getStatusCodeValue());
147     }
148
149     @Test
150     public void testCreateVNFDuplicateCheck() throws IOException {
151         wireMockServer.stubFor(post(urlPathEqualTo(getTestUrl("checkInstanceNameDuplicate"))).withRequestBody(equalTo(
152                 "{\"instanceIdMap\":null,\"instanceName\":\"myVnfOpEnv\",\"requestScope\":\"operationalEnvironment\"}"))
153                 .willReturn(aResponse()
154                         .withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
155                         .withBody(String.format(getResponseTemplate, "requestId", Status.IN_PROGRESS.toString()))
156                         .withStatus(HttpStatus.SC_OK)));
157         ObjectMapper mapper = new ObjectMapper();
158         TenantIsolationRequest request =
159                 mapper.readValue(new File("src/test/resources/TenantIsolation/VNFOperationEnvironmentCreate.json"),
160                         TenantIsolationRequest.class);
161         headers.set("Accept", MediaType.APPLICATION_JSON);
162         headers.set("Content-Type", MediaType.APPLICATION_JSON);
163         HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
164
165         UriComponentsBuilder builder =
166                 UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
167
168         ResponseEntity<String> response =
169                 restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
170         assertEquals(409, response.getStatusCodeValue());
171     }
172
173     @Test
174     public void testCreateVNF() throws IOException {
175         ObjectMapper mapper = new ObjectMapper();
176         TenantIsolationRequest request =
177                 mapper.readValue(new File("src/test/resources/TenantIsolation/VNFOperationEnvironmentCreate.json"),
178                         TenantIsolationRequest.class);
179         headers.set("Accept", MediaType.APPLICATION_JSON);
180         headers.set("Content-Type", MediaType.APPLICATION_JSON);
181         HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
182
183         UriComponentsBuilder builder =
184                 UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
185         wireMockServer.stubFor(post(urlPathEqualTo(getTestUrl("checkInstanceNameDuplicate"))).withRequestBody(equalTo(
186                 "{\"instanceIdMap\":null,\"instanceName\":\"myVnfOpEnv\",\"requestScope\":\"operationalEnvironment\"}"))
187                 .willReturn(
188                         aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
189                                 .withStatus(HttpStatus.SC_NOT_FOUND)));
190
191         ResponseEntity<String> response =
192                 restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
193         assertEquals(200, response.getStatusCodeValue());
194     }
195
196     @Test
197     public void testActivate() throws IOException {
198         ObjectMapper mapper = new ObjectMapper();
199         TenantIsolationRequest request =
200                 mapper.readValue(new File("src/test/resources/TenantIsolation/ActivateOperationEnvironment.json"),
201                         TenantIsolationRequest.class);
202         headers.set("Accept", MediaType.APPLICATION_JSON);
203         headers.set("Content-Type", MediaType.APPLICATION_JSON);
204         HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
205
206         UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(
207                 createURLWithPort(path) + "/operationalEnvironments/ff3514e3-5a33-55df-13ab-12abad84e7ff/activate");
208         wireMockServer.stubFor(post(urlPathEqualTo(getTestUrl("checkInstanceNameDuplicate"))).withRequestBody(equalTo(
209                 "{\"instanceIdMap\":{\"operationalEnvironmentId\":\"ff3514e3-5a33-55df-13ab-12abad84e7ff\"},\"instanceName\":\"myVnfOpEnv\",\"requestScope\":\"operationalEnvironment\"}"))
210                 .willReturn(
211                         aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
212                                 .withStatus(HttpStatus.SC_NOT_FOUND)));
213
214         wireMockServer.stubFor(
215                 get(urlPathEqualTo(getTestUrl("checkVnfIdStatus/ff3514e3-5a33-55df-13ab-12abad84e7ff"))).willReturn(
216                         aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
217                                 .withStatus(HttpStatus.SC_OK)));
218
219         ResponseEntity<String> response =
220                 restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
221
222         assertEquals(200, response.getStatusCodeValue());
223     }
224
225     @Test
226     public void testDeactivate() throws IOException {
227         ObjectMapper mapper = new ObjectMapper();
228         TenantIsolationRequest request =
229                 mapper.readValue(new File("src/test/resources/TenantIsolation/DeactivateOperationEnvironment.json"),
230                         TenantIsolationRequest.class);
231
232         headers.set("Accept", MediaType.APPLICATION_JSON);
233         headers.set("Content-Type", MediaType.APPLICATION_JSON);
234         HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
235
236
237         // wireMockServer.stubFor(post(urlPathEqualTo(getTestUrl("checkInstanceNameDuplicate"))).withRequestBody(equalTo("{\"instanceIdMap\":{\"operationalEnvironmentId\":\"ff3514e3-5a33-55df-13ab-12abad84e7fa\"},\"instanceName\":null,\"requestScope\":\"operationalEnvironment\"}")).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE,
238         // MediaType.APPLICATION_JSON)
239         // .withBodyFile((String.format(getResponseTemplate, "ff3514e3-5a33-55df-13ab-12abad84e7fa",
240         // Status.COMPLETE.toString()))).withStatus(HttpStatus.SC_OK)));
241
242         wireMockServer.stubFor(
243                 get(urlPathEqualTo(getTestUrl("checkVnfIdStatus/ff3514e3-5a33-55df-13ab-12abad84e7fa"))).willReturn(
244                         aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
245                                 .withStatus(HttpStatus.SC_OK)));
246
247         UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(
248                 createURLWithPort(path) + "/operationalEnvironments/ff3514e3-5a33-55df-13ab-12abad84e7fa/deactivate");
249
250         ResponseEntity<String> response =
251                 restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
252
253         assertEquals(200, response.getStatusCodeValue());
254     }
255
256
257     @Test
258     @Ignore
259     public void testDeactivateDupCheck() throws IOException {
260
261         InfraActiveRequests iar = new InfraActiveRequests();
262         iar.setRequestId("requestId");
263         iar.setOperationalEnvName("myVnfOpEnv");
264         iar.setRequestStatus(Status.IN_PROGRESS.toString());
265         iar.setAction(Action.create.toString());
266         iar.setRequestAction(Action.create.toString());
267         iar.setRequestScope("UNKNOWN");
268         // iarRepo.saveAndFlush(iar);
269         ObjectMapper mapper = new ObjectMapper();
270         String request = mapper.readValue(
271                 new File("src/test/resources/TenantIsolation/DeactivateOperationEnvironment.json"), String.class);
272
273         headers.set("Accept", MediaType.APPLICATION_JSON);
274         headers.set("Content-Type", MediaType.APPLICATION_JSON);
275         HttpEntity<String> entity = new HttpEntity<String>(request, headers);
276
277         UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(
278                 createURLWithPort(path) + "/operationalEnvironments/ff3514e3-5a33-55df-13ab-12abad84e7ff/deactivate");
279
280         ResponseEntity<String> response =
281                 restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
282         assertEquals(409, response.getStatusCodeValue());
283     }
284
285 }