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 org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
27 import java.io.IOException;
29 import javax.ws.rs.core.MediaType;
31 import org.apache.commons.io.IOUtils;
32 import org.apache.commons.lang.CharEncoding;
33 import org.junit.Before;
34 import org.junit.Ignore;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.onap.so.apihandlerinfra.ApiHandlerApplication;
38 import org.onap.so.apihandlerinfra.BaseTest;
39 import org.onap.so.apihandlerinfra.Status;
40 import org.onap.so.apihandlerinfra.tenantisolationbeans.Action;
41 import org.onap.so.apihandlerinfra.tenantisolationbeans.TenantIsolationRequest;
42 import org.onap.so.db.request.beans.InfraActiveRequests;
43 import org.onap.so.db.request.data.repository.InfraActiveRequestsRepository;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.boot.context.embedded.LocalServerPort;
46 import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
47 import org.springframework.boot.test.context.SpringBootTest;
48 import org.springframework.boot.test.web.client.TestRestTemplate;
49 import org.springframework.http.HttpEntity;
50 import org.springframework.http.HttpHeaders;
51 import org.springframework.http.HttpMethod;
52 import org.springframework.http.ResponseEntity;
53 import org.springframework.test.context.ActiveProfiles;
54 import org.springframework.test.context.junit4.SpringRunner;
55 import org.springframework.web.util.UriComponentsBuilder;
57 import com.fasterxml.jackson.databind.ObjectMapper;
60 public class CloudOrchestrationTest extends BaseTest {
62 private static final String path = "/onap/so/infra/cloudResources/v1";
63 private HttpHeaders headers = new HttpHeaders();
70 private InfraActiveRequestsRepository iarRepo;
73 public void testCreateOpEnvObjectMapperError() throws IOException {
75 headers.set("Accept", MediaType.APPLICATION_JSON);
76 headers.set("Content-Type", MediaType.APPLICATION_JSON);
77 HttpEntity<String> entity = new HttpEntity<String>(null, headers);
79 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
81 ResponseEntity<String> response = restTemplate.exchange(
82 builder.toUriString(),
83 HttpMethod.POST, entity, String.class);
85 String body = response.getBody().toString();
86 assertTrue(body.contains("Mapping of request to JSON object failed."));
87 assertEquals(400, response.getStatusCodeValue());
91 public void testCreateOpEnvError() throws IOException {
93 String request = "{\"requestDetails\":{\"requestInfo\":{\"resourceType\":\"operationalEnvironment\",\"instanceName\": \"myOpEnv\",\"source\": \"VID\",\"requestorId\": \"xxxxxx\"},"
94 + " \"requestParameters\": {\"tenantContext\": \"Test\",\"workloadContext\": \"ECOMP_E2E-IST\"}}}";
95 headers.set("Accept", MediaType.APPLICATION_JSON);
96 headers.set("Content-Type", MediaType.APPLICATION_JSON);
97 HttpEntity<String> entity = new HttpEntity<String>(request, headers);
99 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
101 ResponseEntity<String> response = restTemplate.exchange(
102 builder.toUriString(),
103 HttpMethod.POST, entity, String.class);
105 String body = response.getBody().toString();
106 assertTrue(body.contains("Mapping of request to JSON object failed"));
107 assertEquals(400, response.getStatusCodeValue());
111 public void testCreateOpEnvReqRecord() throws IOException {
112 ObjectMapper mapper = new ObjectMapper();
113 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/ECOMPOperationEnvironmentCreate.json"), TenantIsolationRequest.class);
114 headers.set("Accept", MediaType.APPLICATION_JSON);
115 headers.set("Content-Type", MediaType.APPLICATION_JSON);
116 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
118 InfraActiveRequests iar = new InfraActiveRequests();
119 iar.setRequestId("123");
120 iar.setOperationalEnvName("myOpEnv");
121 iar.setRequestScope("create");
122 iar.setRequestStatus("PENDING");
123 iar.setRequestAction("UNKNOWN");
124 iarRepo.saveAndFlush(iar);
126 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
128 ResponseEntity<String> response = restTemplate.exchange(
129 builder.toUriString(),
130 HttpMethod.POST, entity, String.class);
132 assertEquals(409, response.getStatusCodeValue());
136 public void testCreateOperationalEnvironment() throws IOException {
137 ObjectMapper mapper = new ObjectMapper();
138 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/ECOMPOperationEnvironmentCreate.json"), TenantIsolationRequest.class);
139 headers.set("Accept", MediaType.APPLICATION_JSON);
140 headers.set("Content-Type", MediaType.APPLICATION_JSON);
141 headers.set("X-TransactionID", "987654321");
142 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
144 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
146 ResponseEntity<String> response = restTemplate.exchange(
147 builder.toUriString(),
148 HttpMethod.POST, entity, String.class);
149 InfraActiveRequests iar = iarRepo.findOneByRequestId("987654321");
150 assertEquals(iar.getRequestBody(), mapper.writeValueAsString(request.getRequestDetails()));
151 assertEquals(200, response.getStatusCodeValue());
155 public void testCreateVNFDuplicateCheck() throws IOException {
156 InfraActiveRequests iar = new InfraActiveRequests();
157 iar.setRequestId("requestId");
158 iar.setOperationalEnvName("myVnfOpEnv");
159 iar.setRequestStatus(Status.IN_PROGRESS.toString());
160 iar.setAction(Action.create.toString());
161 iar.setRequestAction(Action.create.toString());
162 iar.setRequestScope("UNKNOWN");
163 iarRepo.saveAndFlush(iar);
165 ObjectMapper mapper = new ObjectMapper();
166 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/VNFOperationEnvironmentCreate.json"), TenantIsolationRequest.class);
167 headers.set("Accept", MediaType.APPLICATION_JSON);
168 headers.set("Content-Type", MediaType.APPLICATION_JSON);
169 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
171 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
173 ResponseEntity<String> response = restTemplate.exchange(
174 builder.toUriString(),
175 HttpMethod.POST, entity, String.class);
176 assertEquals(409, response.getStatusCodeValue());
180 public void testCreateVNF() throws IOException {
181 ObjectMapper mapper = new ObjectMapper();
182 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/VNFOperationEnvironmentCreate.json"), TenantIsolationRequest.class);
183 headers.set("Accept", MediaType.APPLICATION_JSON);
184 headers.set("Content-Type", MediaType.APPLICATION_JSON);
185 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
187 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
189 ResponseEntity<String> response = restTemplate.exchange(
190 builder.toUriString(),
191 HttpMethod.POST, entity, String.class);
192 assertEquals(200, response.getStatusCodeValue());
196 public void testActivate() throws IOException {
197 ObjectMapper mapper = new ObjectMapper();
198 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/ActivateOperationEnvironment.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);
203 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments/ff3514e3-5a33-55df-13ab-12abad84e7ff/activate");
205 ResponseEntity<String> response = restTemplate.exchange(
206 builder.toUriString(),
207 HttpMethod.POST, entity, String.class);
209 assertEquals(200, response.getStatusCodeValue());
213 public void testDeactivate() throws IOException {
214 InfraActiveRequests iar = new InfraActiveRequests();
215 iar.setRequestId("ff3514e3-5a33-55df-13ab-12abad84e7fa");
216 iar.setRequestStatus(Status.COMPLETE.toString());
217 iar.setRequestAction("UNKNOWN");
218 iar.setRequestScope("UNKNOWN");
219 iarRepo.saveAndFlush(iar);
222 ObjectMapper mapper = new ObjectMapper();
223 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/DeactivateOperationEnvironment.json"), TenantIsolationRequest.class);
225 headers.set("Accept", MediaType.APPLICATION_JSON);
226 headers.set("Content-Type", MediaType.APPLICATION_JSON);
227 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
231 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments/ff3514e3-5a33-55df-13ab-12abad84e7fa/deactivate");
233 ResponseEntity<String> response = restTemplate.exchange(
234 builder.toUriString(),
235 HttpMethod.POST, entity, String.class);
237 assertEquals(200, response.getStatusCodeValue());
241 public void testDeactivateThreadException() throws IOException {
242 //need to simulate a 500 error
243 /*CloudOrchestration co = new CloudOrchestration();
244 TenantIsolationRequest tenantIsolationRequest = mock(TenantIsolationRequest.class);
245 RequestsDatabase reqDB = mock(RequestsDatabase.class);
246 TenantIsolationRunnable thread = mock(TenantIsolationRunnable.class);
247 Response res = Response.status(500).entity("Failed creating a Thread").build();
249 co.setRequestsDatabase(reqDB);
250 co.setThread(thread);
251 co.setTenantIsolationRequest(tenantIsolationRequest);
252 String request = IOUtils.toString(ClassLoader.class.getResourceAsStream ("/DeactivateOperationEnvironment.json"), CharEncoding.UTF_8);
253 when(reqDB.checkInstanceNameDuplicate(null, "myVnfOpEnv", "operationalEnvironment")).thenReturn(null);
254 doNothing().when(tenantIsolationRequest).createRequestRecord(Status.IN_PROGRESS, Action.deactivate);
255 doThrow(Exception.class).when(thread).run();
256 when(tenantIsolationRequest.buildServiceErrorResponse(any(Integer.class), any(MsoException.class), any(String.class), any(String.class), any(List.class))).thenReturn(res);
258 Response response = co.activateOperationEnvironment(request, null, "ff3514e3-5a33-55df-13ab-12abad84e7ff");
259 assertEquals(500, response.getStatus());*/
264 public void testDeactivateDupCheck() throws IOException {
266 InfraActiveRequests iar = new InfraActiveRequests();
267 iar.setRequestId("requestId");
268 iar.setOperationalEnvName("myVnfOpEnv");
269 iar.setRequestStatus(Status.IN_PROGRESS.toString());
270 iar.setAction(Action.create.toString());
271 iar.setRequestAction(Action.create.toString());
272 iar.setRequestScope("UNKNOWN");
273 iarRepo.saveAndFlush(iar);
274 ObjectMapper mapper = new ObjectMapper();
275 String request = mapper.readValue(new File("src/test/resources/TenantIsolation/DeactivateOperationEnvironment.json"), String.class);
277 headers.set("Accept", MediaType.APPLICATION_JSON);
278 headers.set("Content-Type", MediaType.APPLICATION_JSON);
279 HttpEntity<String> entity = new HttpEntity<String>(request, headers);
281 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments/ff3514e3-5a33-55df-13ab-12abad84e7ff/deactivate");
283 ResponseEntity<String> response = restTemplate.exchange(
284 builder.toUriString(),
285 HttpMethod.POST, entity, String.class);
286 assertEquals(409, response.getStatusCodeValue());