2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2018 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.adapters.requestsdb;
24 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertThat;
27 import static org.junit.Assert.assertTrue;
28 import java.sql.Timestamp;
29 import java.time.Instant;
30 import java.time.temporal.ChronoUnit;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
35 import java.util.UUID;
36 import javax.ws.rs.core.MediaType;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.onap.so.db.request.beans.InfraActiveRequests;
40 import org.onap.so.db.request.data.controller.InstanceNameDuplicateCheckRequest;
41 import org.onap.so.serviceinstancebeans.ModelType;
42 import org.springframework.beans.factory.annotation.Value;
43 import org.springframework.boot.test.web.client.TestRestTemplate;
44 import org.springframework.boot.web.server.LocalServerPort;
45 import org.springframework.core.ParameterizedTypeReference;
46 import org.springframework.http.HttpEntity;
47 import org.springframework.http.HttpHeaders;
48 import org.springframework.http.HttpMethod;
49 import org.springframework.http.ResponseEntity;
50 import org.springframework.transaction.annotation.Transactional;
51 import org.springframework.web.util.UriComponentsBuilder;
54 public class InfraActiveRequestsRepositoryCustomControllerTest extends RequestsAdapterBase {
59 @Value("${mso.adapters.requestDb.auth}")
60 private String msoAdaptersAuth;
62 private String createURLWithPort(String uri) {
63 return "http://localhost:" + port + uri;
66 private InfraActiveRequests infraActiveRequests;
67 private InfraActiveRequests infraActiveRequestsResponse;
68 private HttpHeaders headers;
69 private TestRestTemplate restTemplate;
71 private void verifyInfraActiveRequests() {
72 assertEquals(infraActiveRequests.getRequestId(), infraActiveRequestsResponse.getRequestId());
73 assertEquals(infraActiveRequests.getServiceInstanceId(), infraActiveRequestsResponse.getServiceInstanceId());
74 assertEquals(infraActiveRequests.getServiceInstanceName(),
75 infraActiveRequestsResponse.getServiceInstanceName());
76 assertEquals(infraActiveRequests.getVnfId(), infraActiveRequestsResponse.getVnfId());
77 assertEquals(infraActiveRequests.getVnfName(), infraActiveRequestsResponse.getVnfName());
78 assertEquals(infraActiveRequests.getVfModuleId(), infraActiveRequestsResponse.getVfModuleId());
79 assertEquals(infraActiveRequests.getVfModuleName(), infraActiveRequestsResponse.getVfModuleName());
80 assertEquals(infraActiveRequests.getVolumeGroupId(), infraActiveRequestsResponse.getVolumeGroupId());
81 assertEquals(infraActiveRequests.getVolumeGroupName(), infraActiveRequestsResponse.getVolumeGroupName());
82 assertEquals(infraActiveRequests.getNetworkId(), infraActiveRequestsResponse.getNetworkId());
83 assertEquals(infraActiveRequests.getNetworkName(), infraActiveRequestsResponse.getNetworkName());
84 assertEquals(infraActiveRequests.getConfigurationId(), infraActiveRequestsResponse.getConfigurationId());
85 assertEquals(infraActiveRequests.getConfigurationName(), infraActiveRequestsResponse.getConfigurationName());
86 assertEquals(infraActiveRequests.getAaiServiceId(), infraActiveRequestsResponse.getAaiServiceId());
87 assertEquals(infraActiveRequests.getTenantId(), infraActiveRequestsResponse.getTenantId());
88 assertEquals(infraActiveRequests.getRequestScope(), infraActiveRequestsResponse.getRequestScope());
89 assertEquals(infraActiveRequests.getRequestorId(), infraActiveRequestsResponse.getRequestorId());
90 assertEquals(infraActiveRequests.getSource(), infraActiveRequestsResponse.getSource());
91 assertEquals(infraActiveRequests.getOperationalEnvId(), infraActiveRequestsResponse.getOperationalEnvId());
92 assertEquals(infraActiveRequests.getOperationalEnvName(), infraActiveRequestsResponse.getOperationalEnvName());
93 assertEquals(infraActiveRequests.getRequestStatus(), infraActiveRequestsResponse.getRequestStatus());
94 assertEquals(infraActiveRequests.getAction(), infraActiveRequestsResponse.getAction());
95 assertEquals(infraActiveRequests.getRequestUrl(), infraActiveRequestsResponse.getRequestUrl());
100 infraActiveRequests = new InfraActiveRequests();
102 infraActiveRequests.setRequestId(UUID.randomUUID().toString());
103 infraActiveRequests.setOperationalEnvId(UUID.randomUUID().toString());
104 infraActiveRequests.setServiceInstanceId(UUID.randomUUID().toString());
105 infraActiveRequests.setServiceInstanceName("serviceInstanceNameTest");
106 infraActiveRequests.setVnfId(UUID.randomUUID().toString());
107 infraActiveRequests.setVnfName("vnfInstanceNameTest");
108 infraActiveRequests.setVfModuleId(UUID.randomUUID().toString());
109 infraActiveRequests.setVfModuleName("vfModuleInstanceNameTest");
110 infraActiveRequests.setVolumeGroupId(UUID.randomUUID().toString());
111 infraActiveRequests.setVolumeGroupName("volumeGroupInstanceNameTest");
112 infraActiveRequests.setNetworkId(UUID.randomUUID().toString());
113 infraActiveRequests.setNetworkName("networkInstanceNameTest");
114 infraActiveRequests.setConfigurationId(UUID.randomUUID().toString());
115 infraActiveRequests.setConfigurationName("configurationInstanceNameTest");
116 infraActiveRequests.setAicCloudRegion("1");
117 infraActiveRequests.setTenantId(UUID.randomUUID().toString());
118 infraActiveRequests.setRequestScope("operationalEnvironment");
119 infraActiveRequests.setRequestorId(UUID.randomUUID().toString());
120 infraActiveRequests.setSource("sourceTest");
121 infraActiveRequests.setOperationalEnvName(UUID.randomUUID().toString());
122 infraActiveRequests.setRequestStatus("IN_PROGRESS");
123 infraActiveRequests.setAction("create");
125 .setRequestUrl("http://localhost:8080/onap/so/infra/serviceInstantiation/v7/serviceInstances");
127 saveInfraActiveRequest(infraActiveRequests);
130 public void saveInfraActiveRequest(InfraActiveRequests request) {
131 headers = new HttpHeaders();
132 restTemplate = new TestRestTemplate("test", "test");
134 headers.set("Accept", MediaType.APPLICATION_JSON);
135 headers.set("Content-Type", MediaType.APPLICATION_JSON);
136 headers.set("Authorization", msoAdaptersAuth);
138 HttpEntity<String> entity = new HttpEntity(request, headers);
140 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort("/infraActiveRequests"));
141 ResponseEntity<String> response =
142 restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
143 assertEquals(201, response.getStatusCodeValue());
148 public void getCloudOrchestrationFiltersFromInfraActiveTest() {
150 Map<String, String> requestMap = new HashMap<>();
151 requestMap.put("operationalEnvironmentId", infraActiveRequests.getOperationalEnvId());
152 requestMap.put("operationalEnvironmentName", infraActiveRequests.getOperationalEnvName());
153 requestMap.put("resourceType", "operationalEnvironment");
155 HttpEntity<Map<String, String>> entity = new HttpEntity<>(requestMap, headers);
157 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(
158 createURLWithPort("/infraActiveRequests") + "/getCloudOrchestrationFiltersFromInfraActive");
160 ResponseEntity<List<InfraActiveRequests>> response = restTemplate.exchange(builder.toUriString(),
161 HttpMethod.POST, entity, new ParameterizedTypeReference<List<InfraActiveRequests>>() {});
163 List<InfraActiveRequests> iarr = response.getBody();
164 assertEquals(200, response.getStatusCodeValue());
166 assertTrue(iarr.size() == 1);
167 infraActiveRequestsResponse = iarr.get(0);
169 verifyInfraActiveRequests();
174 public void getOrchestrationFiltersFromInfraActiveTest() {
176 Map<String, List<String>> requestMap = new HashMap<>();
177 List<String> values = new ArrayList<>();
178 values.add("EQUALS");
179 values.add(infraActiveRequests.getServiceInstanceId());
180 requestMap.put("serviceInstanceId", values);
182 values = new ArrayList<>();
183 values.add("EQUALS");
184 values.add(infraActiveRequests.getServiceInstanceName());
185 requestMap.put("serviceInstanceName", values);
187 HttpEntity<Map<String, List<String>>> entityList = new HttpEntity(requestMap, headers);
188 UriComponentsBuilder builder = UriComponentsBuilder
189 .fromHttpUrl(createURLWithPort("/infraActiveRequests") + "/getOrchestrationFiltersFromInfraActive");
191 ResponseEntity<List<InfraActiveRequests>> response = restTemplate.exchange(builder.toUriString(),
192 HttpMethod.POST, entityList, new ParameterizedTypeReference<List<InfraActiveRequests>>() {});
194 List<InfraActiveRequests> iarr = response.getBody();
196 assertEquals(200, response.getStatusCodeValue());
198 assertTrue(iarr.size() == 1);
199 infraActiveRequestsResponse = iarr.get(0);
201 verifyInfraActiveRequests();
205 public void checkVnfIdStatusTest() {
208 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(
209 "/infraActiveRequests" + "/checkVnfIdStatus/" + infraActiveRequests.getOperationalEnvId()));
210 HttpEntity<String> entity = new HttpEntity(HttpEntity.EMPTY, headers);
212 ResponseEntity<InfraActiveRequests> response =
213 restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, InfraActiveRequests.class);
215 infraActiveRequestsResponse = response.getBody();
217 assertEquals(200, response.getStatusCodeValue());
219 verifyInfraActiveRequests();
223 public void checkInstanceNameDuplicateTest() {
225 InstanceNameDuplicateCheckRequest instanceNameDuplicateCheckRequest =
226 new InstanceNameDuplicateCheckRequest((HashMap<String, String>) null,
227 infraActiveRequests.getOperationalEnvName(), infraActiveRequests.getRequestScope());
229 HttpEntity<InstanceNameDuplicateCheckRequest> entityList =
230 new HttpEntity(instanceNameDuplicateCheckRequest, headers);
231 UriComponentsBuilder builder = UriComponentsBuilder
232 .fromHttpUrl(createURLWithPort("/infraActiveRequests") + "/checkInstanceNameDuplicate");
234 ResponseEntity<InfraActiveRequests> response = restTemplate.exchange(builder.toUriString(), HttpMethod.POST,
235 entityList, new ParameterizedTypeReference<InfraActiveRequests>() {});
237 infraActiveRequestsResponse = response.getBody();
239 assertEquals(200, response.getStatusCodeValue());
241 verifyInfraActiveRequests();
245 public void checkInstanceNameDuplicateViaTest() {
247 Map<String, String> requestMap = new HashMap<>();
248 requestMap.put("operationalEnvironmentId", infraActiveRequests.getOperationalEnvId());
250 InstanceNameDuplicateCheckRequest instanceNameDuplicateCheckRequest = new InstanceNameDuplicateCheckRequest(
251 (HashMap<String, String>) requestMap, null, infraActiveRequests.getRequestScope());
253 HttpEntity<InstanceNameDuplicateCheckRequest> entityList =
254 new HttpEntity(instanceNameDuplicateCheckRequest, headers);
255 UriComponentsBuilder builder = UriComponentsBuilder
256 .fromHttpUrl(createURLWithPort("/infraActiveRequests") + "/checkInstanceNameDuplicate");
258 ResponseEntity<InfraActiveRequests> response = restTemplate.exchange(builder.toUriString(), HttpMethod.POST,
259 entityList, new ParameterizedTypeReference<InfraActiveRequests>() {});
261 infraActiveRequestsResponse = response.getBody();
263 assertEquals(200, response.getStatusCodeValue());
265 verifyInfraActiveRequests();
269 public void checkInstanceNameDuplicateTestNotFound() {
271 String instanceNameDuplicateCheckRequest =
272 "{\r\n\t \"instanceName\":\"TestNotFoundInstanceName\",\r\n\t \"requestScope\":\"testasdfasdfasdf\"\r\n}";
274 HttpEntity<InstanceNameDuplicateCheckRequest> entityList =
275 new HttpEntity(instanceNameDuplicateCheckRequest, headers);
276 UriComponentsBuilder builder = UriComponentsBuilder
277 .fromHttpUrl(createURLWithPort("/infraActiveRequests") + "/checkInstanceNameDuplicate");
279 ResponseEntity<InfraActiveRequests> response = restTemplate.exchange(builder.toUriString(), HttpMethod.POST,
280 entityList, new ParameterizedTypeReference<InfraActiveRequests>() {});
282 assertEquals(200, response.getStatusCodeValue());
283 assertEquals(null, response.getBody());
287 public void checkInstanceNameDuplicateViaTestNotFound() {
289 Map<String, String> requestMap = new HashMap<>();
290 requestMap.put("operationalEnvironmentId", "NotFoundOperationalEnvId");
292 InstanceNameDuplicateCheckRequest instanceNameDuplicateCheckRequest = new InstanceNameDuplicateCheckRequest(
293 (HashMap<String, String>) requestMap, null, infraActiveRequests.getRequestScope());
295 HttpEntity<InstanceNameDuplicateCheckRequest> entityList =
296 new HttpEntity(instanceNameDuplicateCheckRequest, headers);
297 UriComponentsBuilder builder = UriComponentsBuilder
298 .fromHttpUrl(createURLWithPort("/infraActiveRequests") + "/checkInstanceNameDuplicate");
300 ResponseEntity<InfraActiveRequests> response = restTemplate.exchange(builder.toUriString(), HttpMethod.POST,
301 entityList, new ParameterizedTypeReference<InfraActiveRequests>() {});
303 infraActiveRequestsResponse = response.getBody();
305 assertEquals(200, response.getStatusCodeValue());
306 assertEquals(null, response.getBody());
310 public void getInProgressVolumeGroupsAndVfModulesTest() {
311 boolean expectedReturned = false;
312 InfraActiveRequests request = new InfraActiveRequests();
313 request.setRequestId(UUID.randomUUID().toString());
314 request.setVfModuleId(UUID.randomUUID().toString());
315 request.setRequestStatus("IN_PROGRESS");
316 request.setRequestScope(ModelType.vfModule.toString());
317 Instant startInstant = Instant.now().minus(3, ChronoUnit.MINUTES);
318 request.setStartTime(Timestamp.from(startInstant));
319 request.setRequestAction("create");
320 saveInfraActiveRequest(request);
322 UriComponentsBuilder builder = UriComponentsBuilder
323 .fromHttpUrl(createURLWithPort("/infraActiveRequests/getInProgressVolumeGroupsAndVfModules"));
325 HttpEntity<String> entity = new HttpEntity<String>(headers);
327 ResponseEntity<List<InfraActiveRequests>> response = restTemplate.exchange(builder.toUriString(),
328 HttpMethod.GET, entity, new ParameterizedTypeReference<List<InfraActiveRequests>>() {});
330 List<InfraActiveRequests> responseList = response.getBody();
332 assertEquals(200, response.getStatusCodeValue());
334 for (InfraActiveRequests result : responseList) {
335 if (result.getRequestId().equals(request.getRequestId())) {
336 assertThat(request, sameBeanAs(result).ignoring("modifyTime"));
337 expectedReturned = true;
340 assertTrue(expectedReturned);