2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.apihandlerinfra.tenantisolation;
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;
32 import java.io.IOException;
34 import javax.ws.rs.core.MediaType;
36 import org.apache.http.HttpStatus;
37 import org.junit.Before;
38 import org.junit.Ignore;
39 import org.junit.Test;
40 import org.onap.so.apihandlerinfra.BaseTest;
41 import org.onap.so.apihandlerinfra.Status;
42 import org.onap.so.apihandlerinfra.tenantisolationbeans.Action;
43 import org.onap.so.apihandlerinfra.tenantisolationbeans.TenantIsolationRequest;
44 import org.onap.so.db.request.beans.InfraActiveRequests;
45 import org.springframework.http.HttpEntity;
46 import org.springframework.http.HttpHeaders;
47 import org.springframework.http.HttpMethod;
48 import org.springframework.http.ResponseEntity;
49 import org.springframework.web.util.UriComponentsBuilder;
51 import com.fasterxml.jackson.databind.ObjectMapper;
54 public class CloudOrchestrationTest extends BaseTest {
56 private static final String path = "/onap/so/infra/cloudResources/v1";
58 private HttpHeaders headers = new HttpHeaders();
61 public void setupTestClass() throws Exception{
62 wireMockServer.stubFor(post(urlPathEqualTo(getTestUrl(""))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).withStatus(HttpStatus.SC_CREATED)));
66 public void testCreateOpEnvObjectMapperError() throws IOException {
68 headers.set("Accept", MediaType.APPLICATION_JSON);
69 headers.set("Content-Type", MediaType.APPLICATION_JSON);
70 HttpEntity<String> entity = new HttpEntity<String>(null, headers);
72 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
73 ResponseEntity<String> response = restTemplate.exchange(
74 builder.toUriString(),
75 HttpMethod.POST, entity, String.class);
77 String body = response.getBody().toString();
78 assertTrue(body.contains("Mapping of request to JSON object failed."));
79 assertEquals(400, response.getStatusCodeValue());
83 public void testCreateOpEnvError() throws IOException {
85 String request = "{\"requestDetails\":{\"requestInfo\":{\"resourceType\":\"operationalEnvironment\",\"instanceName\": \"myOpEnv\",\"source\": \"VID\",\"requestorId\": \"xxxxxx\"},"
86 + " \"requestParameters\": {\"tenantContext\": \"Test\",\"workloadContext\": \"ECOMP_E2E-IST\"}}}";
87 headers.set("Accept", MediaType.APPLICATION_JSON);
88 headers.set("Content-Type", MediaType.APPLICATION_JSON);
89 HttpEntity<String> entity = new HttpEntity<String>(request, headers);
91 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
93 ResponseEntity<String> response = restTemplate.exchange(
94 builder.toUriString(),
95 HttpMethod.POST, entity, String.class);
97 String body = response.getBody().toString();
98 assertTrue(body.contains("Mapping of request to JSON object failed"));
99 assertEquals(400, response.getStatusCodeValue());
103 public void testCreateOpEnvReqRecordDuplicateCheck() throws IOException {
104 wireMockServer.stubFor(post(urlPathEqualTo(getTestUrl("checkInstanceNameDuplicate"))).withRequestBody(equalTo("{\"instanceIdMap\":null,\"instanceName\":\"myOpEnv\",\"requestScope\":\"operationalEnvironment\"}")).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
105 .withBody(String.format(getResponseTemplate, "123", "PENDING"))
106 .withStatus(HttpStatus.SC_OK)));
107 ObjectMapper mapper = new ObjectMapper();
108 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/ECOMPOperationEnvironmentCreate.json"), TenantIsolationRequest.class);
109 headers.set("Accept", MediaType.APPLICATION_JSON);
110 headers.set("Content-Type", MediaType.APPLICATION_JSON);
111 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
114 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
116 ResponseEntity<String> response = restTemplate.exchange(
117 builder.toUriString(),
118 HttpMethod.POST, entity, String.class);
120 assertEquals(409, response.getStatusCodeValue());
124 public void testCreateOperationalEnvironment() throws IOException {
125 ObjectMapper mapper = new ObjectMapper();
126 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/ECOMPOperationEnvironmentCreate.json"), TenantIsolationRequest.class);
127 headers.set("Accept", MediaType.APPLICATION_JSON);
128 headers.set("Content-Type", MediaType.APPLICATION_JSON);
129 headers.set("X-TransactionID", "987654321");
130 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
132 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
133 wireMockServer.stubFor(post(urlPathEqualTo(getTestUrl("checkInstanceNameDuplicate"))).withRequestBody(equalTo("{\"instanceIdMap\":null,\"instanceName\":\"myOpEnv\",\"requestScope\":\"operationalEnvironment\"}")).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
134 .withStatus(HttpStatus.SC_NOT_FOUND)));
135 ResponseEntity<String> response = restTemplate.exchange(
136 builder.toUriString(),
137 HttpMethod.POST, entity, String.class);
138 assertEquals(200, response.getStatusCodeValue());
142 public void testCreateVNFDuplicateCheck() throws IOException {
143 wireMockServer.stubFor(post(urlPathEqualTo(getTestUrl("checkInstanceNameDuplicate"))).withRequestBody(equalTo("{\"instanceIdMap\":null,\"instanceName\":\"myVnfOpEnv\",\"requestScope\":\"operationalEnvironment\"}")).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
144 .withBody(String.format(getResponseTemplate, "requestId", Status.IN_PROGRESS.toString()))
145 .withStatus(HttpStatus.SC_OK)));
146 ObjectMapper mapper = new ObjectMapper();
147 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/VNFOperationEnvironmentCreate.json"), TenantIsolationRequest.class);
148 headers.set("Accept", MediaType.APPLICATION_JSON);
149 headers.set("Content-Type", MediaType.APPLICATION_JSON);
150 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
152 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
154 ResponseEntity<String> response = restTemplate.exchange(
155 builder.toUriString(),
156 HttpMethod.POST, entity, String.class);
157 assertEquals(409, response.getStatusCodeValue());
161 public void testCreateVNF() throws IOException {
162 ObjectMapper mapper = new ObjectMapper();
163 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/VNFOperationEnvironmentCreate.json"), TenantIsolationRequest.class);
164 headers.set("Accept", MediaType.APPLICATION_JSON);
165 headers.set("Content-Type", MediaType.APPLICATION_JSON);
166 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
168 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
169 wireMockServer.stubFor(post(urlPathEqualTo(getTestUrl("checkInstanceNameDuplicate"))).withRequestBody(equalTo("{\"instanceIdMap\":null,\"instanceName\":\"myVnfOpEnv\",\"requestScope\":\"operationalEnvironment\"}")).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
170 .withStatus(HttpStatus.SC_NOT_FOUND)));
172 ResponseEntity<String> response = restTemplate.exchange(
173 builder.toUriString(),
174 HttpMethod.POST, entity, String.class);
175 assertEquals(200, response.getStatusCodeValue());
179 public void testActivate() throws IOException {
180 ObjectMapper mapper = new ObjectMapper();
181 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/ActivateOperationEnvironment.json"), TenantIsolationRequest.class);
182 headers.set("Accept", MediaType.APPLICATION_JSON);
183 headers.set("Content-Type", MediaType.APPLICATION_JSON);
184 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
186 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments/ff3514e3-5a33-55df-13ab-12abad84e7ff/activate");
187 wireMockServer.stubFor(post(urlPathEqualTo(getTestUrl("checkInstanceNameDuplicate"))).withRequestBody(equalTo("{\"instanceIdMap\":{\"operationalEnvironmentId\":\"ff3514e3-5a33-55df-13ab-12abad84e7ff\"},\"instanceName\":\"myVnfOpEnv\",\"requestScope\":\"operationalEnvironment\"}")).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
188 .withStatus(HttpStatus.SC_NOT_FOUND)));
190 wireMockServer.stubFor(get(urlPathEqualTo(getTestUrl("checkVnfIdStatus/ff3514e3-5a33-55df-13ab-12abad84e7ff"))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
191 .withStatus(HttpStatus.SC_OK)));
193 ResponseEntity<String> response = restTemplate.exchange(
194 builder.toUriString(),
195 HttpMethod.POST, entity, String.class);
197 assertEquals(200, response.getStatusCodeValue());
201 public void testDeactivate() throws IOException {
202 ObjectMapper mapper = new ObjectMapper();
203 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/DeactivateOperationEnvironment.json"), TenantIsolationRequest.class);
205 headers.set("Accept", MediaType.APPLICATION_JSON);
206 headers.set("Content-Type", MediaType.APPLICATION_JSON);
207 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
210 // 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, MediaType.APPLICATION_JSON)
211 // .withBodyFile((String.format(getResponseTemplate, "ff3514e3-5a33-55df-13ab-12abad84e7fa", Status.COMPLETE.toString()))).withStatus(HttpStatus.SC_OK)));
213 wireMockServer.stubFor(get(urlPathEqualTo(getTestUrl("checkVnfIdStatus/ff3514e3-5a33-55df-13ab-12abad84e7fa"))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
214 .withStatus(HttpStatus.SC_OK)));
216 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments/ff3514e3-5a33-55df-13ab-12abad84e7fa/deactivate");
218 ResponseEntity<String> response = restTemplate.exchange(
219 builder.toUriString(),
220 HttpMethod.POST, entity, String.class);
222 assertEquals(200, response.getStatusCodeValue());
228 public void testDeactivateDupCheck() throws IOException {
230 InfraActiveRequests iar = new InfraActiveRequests();
231 iar.setRequestId("requestId");
232 iar.setOperationalEnvName("myVnfOpEnv");
233 iar.setRequestStatus(Status.IN_PROGRESS.toString());
234 iar.setAction(Action.create.toString());
235 iar.setRequestAction(Action.create.toString());
236 iar.setRequestScope("UNKNOWN");
237 //iarRepo.saveAndFlush(iar);
238 ObjectMapper mapper = new ObjectMapper();
239 String request = mapper.readValue(new File("src/test/resources/TenantIsolation/DeactivateOperationEnvironment.json"), String.class);
241 headers.set("Accept", MediaType.APPLICATION_JSON);
242 headers.set("Content-Type", MediaType.APPLICATION_JSON);
243 HttpEntity<String> entity = new HttpEntity<String>(request, headers);
245 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments/ff3514e3-5a33-55df-13ab-12abad84e7ff/deactivate");
247 ResponseEntity<String> response = restTemplate.exchange(
248 builder.toUriString(),
249 HttpMethod.POST, entity, String.class);
250 assertEquals(409, response.getStatusCodeValue());