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 com.fasterxml.jackson.databind.ObjectMapper;
24 import org.apache.http.HttpStatus;
25 import org.junit.Before;
26 import org.junit.Ignore;
27 import org.junit.Test;
28 import org.onap.so.apihandlerinfra.BaseTest;
29 import org.onap.so.apihandlerinfra.Status;
30 import org.onap.so.apihandlerinfra.tenantisolationbeans.Action;
31 import org.onap.so.apihandlerinfra.tenantisolationbeans.TenantIsolationRequest;
32 import org.onap.so.db.request.beans.InfraActiveRequests;
33 import org.springframework.http.HttpEntity;
34 import org.springframework.http.HttpHeaders;
35 import org.springframework.http.HttpMethod;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.web.util.UriComponentsBuilder;
39 import javax.ws.rs.core.MediaType;
41 import java.io.IOException;
43 import static com.github.tomakehurst.wiremock.client.WireMock.*;
44 import static org.junit.Assert.assertEquals;
45 import static org.junit.Assert.assertTrue;
48 public class CloudOrchestrationTest extends BaseTest {
50 private static final String path = "/onap/so/infra/cloudResources/v1";
52 private HttpHeaders headers = new HttpHeaders();
55 public void setupTestClass() throws Exception{
56 stubFor(post(urlPathEqualTo(getTestUrl(""))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).withStatus(HttpStatus.SC_CREATED)));
60 public void testCreateOpEnvObjectMapperError() throws IOException {
62 headers.set("Accept", MediaType.APPLICATION_JSON);
63 headers.set("Content-Type", MediaType.APPLICATION_JSON);
64 HttpEntity<String> entity = new HttpEntity<String>(null, headers);
66 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
67 ResponseEntity<String> response = restTemplate.exchange(
68 builder.toUriString(),
69 HttpMethod.POST, entity, String.class);
71 String body = response.getBody().toString();
72 assertTrue(body.contains("Mapping of request to JSON object failed."));
73 assertEquals(400, response.getStatusCodeValue());
77 public void testCreateOpEnvError() throws IOException {
79 String request = "{\"requestDetails\":{\"requestInfo\":{\"resourceType\":\"operationalEnvironment\",\"instanceName\": \"myOpEnv\",\"source\": \"VID\",\"requestorId\": \"xxxxxx\"},"
80 + " \"requestParameters\": {\"tenantContext\": \"Test\",\"workloadContext\": \"ECOMP_E2E-IST\"}}}";
81 headers.set("Accept", MediaType.APPLICATION_JSON);
82 headers.set("Content-Type", MediaType.APPLICATION_JSON);
83 HttpEntity<String> entity = new HttpEntity<String>(request, headers);
85 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
87 ResponseEntity<String> response = restTemplate.exchange(
88 builder.toUriString(),
89 HttpMethod.POST, entity, String.class);
91 String body = response.getBody().toString();
92 assertTrue(body.contains("Mapping of request to JSON object failed"));
93 assertEquals(400, response.getStatusCodeValue());
97 public void testCreateOpEnvReqRecordDuplicateCheck() throws IOException {
98 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)
99 .withBody(String.format(getResponseTemplate, "123", "PENDING"))
100 .withStatus(HttpStatus.SC_OK)));
101 ObjectMapper mapper = new ObjectMapper();
102 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/ECOMPOperationEnvironmentCreate.json"), TenantIsolationRequest.class);
103 headers.set("Accept", MediaType.APPLICATION_JSON);
104 headers.set("Content-Type", MediaType.APPLICATION_JSON);
105 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
108 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
110 ResponseEntity<String> response = restTemplate.exchange(
111 builder.toUriString(),
112 HttpMethod.POST, entity, String.class);
114 assertEquals(409, response.getStatusCodeValue());
118 public void testCreateOperationalEnvironment() throws IOException {
119 ObjectMapper mapper = new ObjectMapper();
120 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/ECOMPOperationEnvironmentCreate.json"), TenantIsolationRequest.class);
121 headers.set("Accept", MediaType.APPLICATION_JSON);
122 headers.set("Content-Type", MediaType.APPLICATION_JSON);
123 headers.set("X-TransactionID", "987654321");
124 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
126 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
127 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)
128 .withStatus(HttpStatus.SC_NOT_FOUND)));
129 ResponseEntity<String> response = restTemplate.exchange(
130 builder.toUriString(),
131 HttpMethod.POST, entity, String.class);
132 assertEquals(200, response.getStatusCodeValue());
136 public void testCreateVNFDuplicateCheck() throws IOException {
137 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)
138 .withBody(String.format(getResponseTemplate, "requestId", Status.IN_PROGRESS.toString()))
139 .withStatus(HttpStatus.SC_OK)));
140 ObjectMapper mapper = new ObjectMapper();
141 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/VNFOperationEnvironmentCreate.json"), TenantIsolationRequest.class);
142 headers.set("Accept", MediaType.APPLICATION_JSON);
143 headers.set("Content-Type", MediaType.APPLICATION_JSON);
144 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
146 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
148 ResponseEntity<String> response = restTemplate.exchange(
149 builder.toUriString(),
150 HttpMethod.POST, entity, String.class);
151 assertEquals(409, response.getStatusCodeValue());
155 public void testCreateVNF() throws IOException {
156 ObjectMapper mapper = new ObjectMapper();
157 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/VNFOperationEnvironmentCreate.json"), TenantIsolationRequest.class);
158 headers.set("Accept", MediaType.APPLICATION_JSON);
159 headers.set("Content-Type", MediaType.APPLICATION_JSON);
160 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
162 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
163 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)
164 .withStatus(HttpStatus.SC_NOT_FOUND)));
166 ResponseEntity<String> response = restTemplate.exchange(
167 builder.toUriString(),
168 HttpMethod.POST, entity, String.class);
169 assertEquals(200, response.getStatusCodeValue());
173 public void testActivate() throws IOException {
174 ObjectMapper mapper = new ObjectMapper();
175 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/ActivateOperationEnvironment.json"), TenantIsolationRequest.class);
176 headers.set("Accept", MediaType.APPLICATION_JSON);
177 headers.set("Content-Type", MediaType.APPLICATION_JSON);
178 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
180 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments/ff3514e3-5a33-55df-13ab-12abad84e7ff/activate");
181 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)
182 .withStatus(HttpStatus.SC_NOT_FOUND)));
184 stubFor(get(urlPathEqualTo(getTestUrl("checkVnfIdStatus/ff3514e3-5a33-55df-13ab-12abad84e7ff"))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
185 .withStatus(HttpStatus.SC_OK)));
187 ResponseEntity<String> response = restTemplate.exchange(
188 builder.toUriString(),
189 HttpMethod.POST, entity, String.class);
191 assertEquals(200, response.getStatusCodeValue());
195 public void testDeactivate() throws IOException {
196 ObjectMapper mapper = new ObjectMapper();
197 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/DeactivateOperationEnvironment.json"), TenantIsolationRequest.class);
199 headers.set("Accept", MediaType.APPLICATION_JSON);
200 headers.set("Content-Type", MediaType.APPLICATION_JSON);
201 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
204 // 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)
205 // .withBodyFile((String.format(getResponseTemplate, "ff3514e3-5a33-55df-13ab-12abad84e7fa", Status.COMPLETE.toString()))).withStatus(HttpStatus.SC_OK)));
207 stubFor(get(urlPathEqualTo(getTestUrl("checkVnfIdStatus/ff3514e3-5a33-55df-13ab-12abad84e7fa"))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
208 .withStatus(HttpStatus.SC_OK)));
210 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments/ff3514e3-5a33-55df-13ab-12abad84e7fa/deactivate");
212 ResponseEntity<String> response = restTemplate.exchange(
213 builder.toUriString(),
214 HttpMethod.POST, entity, String.class);
216 assertEquals(200, response.getStatusCodeValue());
222 public void testDeactivateDupCheck() throws IOException {
224 InfraActiveRequests iar = new InfraActiveRequests();
225 iar.setRequestId("requestId");
226 iar.setOperationalEnvName("myVnfOpEnv");
227 iar.setRequestStatus(Status.IN_PROGRESS.toString());
228 iar.setAction(Action.create.toString());
229 iar.setRequestAction(Action.create.toString());
230 iar.setRequestScope("UNKNOWN");
231 //iarRepo.saveAndFlush(iar);
232 ObjectMapper mapper = new ObjectMapper();
233 String request = mapper.readValue(new File("src/test/resources/TenantIsolation/DeactivateOperationEnvironment.json"), String.class);
235 headers.set("Accept", MediaType.APPLICATION_JSON);
236 headers.set("Content-Type", MediaType.APPLICATION_JSON);
237 HttpEntity<String> entity = new HttpEntity<String>(request, headers);
239 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments/ff3514e3-5a33-55df-13ab-12abad84e7ff/deactivate");
241 ResponseEntity<String> response = restTemplate.exchange(
242 builder.toUriString(),
243 HttpMethod.POST, entity, String.class);
244 assertEquals(409, response.getStatusCodeValue());