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.boot.context.embedded.LocalServerPort;
34 import org.springframework.http.HttpEntity;
35 import org.springframework.http.HttpHeaders;
36 import org.springframework.http.HttpMethod;
37 import org.springframework.http.ResponseEntity;
38 import org.springframework.web.util.UriComponentsBuilder;
40 import javax.ws.rs.core.MediaType;
42 import java.io.IOException;
44 import static com.github.tomakehurst.wiremock.client.WireMock.*;
45 import static org.junit.Assert.assertEquals;
46 import static org.junit.Assert.assertTrue;
49 public class CloudOrchestrationTest extends BaseTest {
51 private static final String path = "/onap/so/infra/cloudResources/v1";
53 private HttpHeaders headers = new HttpHeaders();
56 public void setupTestClass() throws Exception{
57 stubFor(post(urlPathEqualTo(getTestUrl(""))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).withStatus(HttpStatus.SC_CREATED)));
61 public void testCreateOpEnvObjectMapperError() throws IOException {
63 headers.set("Accept", MediaType.APPLICATION_JSON);
64 headers.set("Content-Type", MediaType.APPLICATION_JSON);
65 HttpEntity<String> entity = new HttpEntity<String>(null, headers);
67 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
68 ResponseEntity<String> response = restTemplate.exchange(
69 builder.toUriString(),
70 HttpMethod.POST, entity, String.class);
72 String body = response.getBody().toString();
73 assertTrue(body.contains("Mapping of request to JSON object failed."));
74 assertEquals(400, response.getStatusCodeValue());
78 public void testCreateOpEnvError() throws IOException {
80 String request = "{\"requestDetails\":{\"requestInfo\":{\"resourceType\":\"operationalEnvironment\",\"instanceName\": \"myOpEnv\",\"source\": \"VID\",\"requestorId\": \"xxxxxx\"},"
81 + " \"requestParameters\": {\"tenantContext\": \"Test\",\"workloadContext\": \"ECOMP_E2E-IST\"}}}";
82 headers.set("Accept", MediaType.APPLICATION_JSON);
83 headers.set("Content-Type", MediaType.APPLICATION_JSON);
84 HttpEntity<String> entity = new HttpEntity<String>(request, headers);
86 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
88 ResponseEntity<String> response = restTemplate.exchange(
89 builder.toUriString(),
90 HttpMethod.POST, entity, String.class);
92 String body = response.getBody().toString();
93 assertTrue(body.contains("Mapping of request to JSON object failed"));
94 assertEquals(400, response.getStatusCodeValue());
98 public void testCreateOpEnvReqRecordDuplicateCheck() throws IOException {
99 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)
100 .withBody(String.format(getResponseTemplate, "123", "PENDING"))
101 .withStatus(HttpStatus.SC_OK)));
102 ObjectMapper mapper = new ObjectMapper();
103 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/ECOMPOperationEnvironmentCreate.json"), TenantIsolationRequest.class);
104 headers.set("Accept", MediaType.APPLICATION_JSON);
105 headers.set("Content-Type", MediaType.APPLICATION_JSON);
106 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
109 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
111 ResponseEntity<String> response = restTemplate.exchange(
112 builder.toUriString(),
113 HttpMethod.POST, entity, String.class);
115 assertEquals(409, response.getStatusCodeValue());
119 public void testCreateOperationalEnvironment() throws IOException {
120 ObjectMapper mapper = new ObjectMapper();
121 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/ECOMPOperationEnvironmentCreate.json"), TenantIsolationRequest.class);
122 headers.set("Accept", MediaType.APPLICATION_JSON);
123 headers.set("Content-Type", MediaType.APPLICATION_JSON);
124 headers.set("X-TransactionID", "987654321");
125 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
127 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
128 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)
129 .withStatus(HttpStatus.SC_NOT_FOUND)));
130 ResponseEntity<String> response = restTemplate.exchange(
131 builder.toUriString(),
132 HttpMethod.POST, entity, String.class);
133 assertEquals(200, response.getStatusCodeValue());
137 public void testCreateVNFDuplicateCheck() throws IOException {
138 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)
139 .withBody(String.format(getResponseTemplate, "requestId", Status.IN_PROGRESS.toString()))
140 .withStatus(HttpStatus.SC_OK)));
141 ObjectMapper mapper = new ObjectMapper();
142 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/VNFOperationEnvironmentCreate.json"), TenantIsolationRequest.class);
143 headers.set("Accept", MediaType.APPLICATION_JSON);
144 headers.set("Content-Type", MediaType.APPLICATION_JSON);
145 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
147 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
149 ResponseEntity<String> response = restTemplate.exchange(
150 builder.toUriString(),
151 HttpMethod.POST, entity, String.class);
152 assertEquals(409, response.getStatusCodeValue());
156 public void testCreateVNF() throws IOException {
157 ObjectMapper mapper = new ObjectMapper();
158 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/VNFOperationEnvironmentCreate.json"), TenantIsolationRequest.class);
159 headers.set("Accept", MediaType.APPLICATION_JSON);
160 headers.set("Content-Type", MediaType.APPLICATION_JSON);
161 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
163 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
164 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)
165 .withStatus(HttpStatus.SC_NOT_FOUND)));
167 ResponseEntity<String> response = restTemplate.exchange(
168 builder.toUriString(),
169 HttpMethod.POST, entity, String.class);
170 assertEquals(200, response.getStatusCodeValue());
174 public void testActivate() throws IOException {
175 ObjectMapper mapper = new ObjectMapper();
176 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/ActivateOperationEnvironment.json"), TenantIsolationRequest.class);
177 headers.set("Accept", MediaType.APPLICATION_JSON);
178 headers.set("Content-Type", MediaType.APPLICATION_JSON);
179 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
181 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments/ff3514e3-5a33-55df-13ab-12abad84e7ff/activate");
182 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)
183 .withStatus(HttpStatus.SC_NOT_FOUND)));
185 stubFor(get(urlPathEqualTo(getTestUrl("checkVnfIdStatus/ff3514e3-5a33-55df-13ab-12abad84e7ff"))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
186 .withStatus(HttpStatus.SC_OK)));
188 ResponseEntity<String> response = restTemplate.exchange(
189 builder.toUriString(),
190 HttpMethod.POST, entity, String.class);
192 assertEquals(200, response.getStatusCodeValue());
196 public void testDeactivate() throws IOException {
197 ObjectMapper mapper = new ObjectMapper();
198 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/DeactivateOperationEnvironment.json"), TenantIsolationRequest.class);
200 headers.set("Accept", MediaType.APPLICATION_JSON);
201 headers.set("Content-Type", MediaType.APPLICATION_JSON);
202 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
205 // 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)
206 // .withBodyFile((String.format(getResponseTemplate, "ff3514e3-5a33-55df-13ab-12abad84e7fa", Status.COMPLETE.toString()))).withStatus(HttpStatus.SC_OK)));
208 stubFor(get(urlPathEqualTo(getTestUrl("checkVnfIdStatus/ff3514e3-5a33-55df-13ab-12abad84e7fa"))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
209 .withStatus(HttpStatus.SC_OK)));
211 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments/ff3514e3-5a33-55df-13ab-12abad84e7fa/deactivate");
213 ResponseEntity<String> response = restTemplate.exchange(
214 builder.toUriString(),
215 HttpMethod.POST, entity, String.class);
217 assertEquals(200, response.getStatusCodeValue());
223 public void testDeactivateDupCheck() throws IOException {
225 InfraActiveRequests iar = new InfraActiveRequests();
226 iar.setRequestId("requestId");
227 iar.setOperationalEnvName("myVnfOpEnv");
228 iar.setRequestStatus(Status.IN_PROGRESS.toString());
229 iar.setAction(Action.create.toString());
230 iar.setRequestAction(Action.create.toString());
231 iar.setRequestScope("UNKNOWN");
232 //iarRepo.saveAndFlush(iar);
233 ObjectMapper mapper = new ObjectMapper();
234 String request = mapper.readValue(new File("src/test/resources/TenantIsolation/DeactivateOperationEnvironment.json"), String.class);
236 headers.set("Accept", MediaType.APPLICATION_JSON);
237 headers.set("Content-Type", MediaType.APPLICATION_JSON);
238 HttpEntity<String> entity = new HttpEntity<String>(request, headers);
240 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments/ff3514e3-5a33-55df-13ab-12abad84e7ff/deactivate");
242 ResponseEntity<String> response = restTemplate.exchange(
243 builder.toUriString(),
244 HttpMethod.POST, entity, String.class);
245 assertEquals(409, response.getStatusCodeValue());