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.getTenantId(), infraActiveRequestsResponse.getTenantId());
87 assertEquals(infraActiveRequests.getRequestScope(), infraActiveRequestsResponse.getRequestScope());
88 assertEquals(infraActiveRequests.getRequestorId(), infraActiveRequestsResponse.getRequestorId());
89 assertEquals(infraActiveRequests.getSource(), infraActiveRequestsResponse.getSource());
90 assertEquals(infraActiveRequests.getOperationalEnvId(), infraActiveRequestsResponse.getOperationalEnvId());
91 assertEquals(infraActiveRequests.getOperationalEnvName(), infraActiveRequestsResponse.getOperationalEnvName());
92 assertEquals(infraActiveRequests.getRequestStatus(), infraActiveRequestsResponse.getRequestStatus());
93 assertEquals(infraActiveRequests.getRequestUrl(), infraActiveRequestsResponse.getRequestUrl());
98 infraActiveRequests = new InfraActiveRequests();
100 infraActiveRequests.setRequestId(UUID.randomUUID().toString());
101 infraActiveRequests.setOperationalEnvId(UUID.randomUUID().toString());
102 infraActiveRequests.setServiceInstanceId(UUID.randomUUID().toString());
103 infraActiveRequests.setServiceInstanceName("serviceInstanceNameTest");
104 infraActiveRequests.setVnfId(UUID.randomUUID().toString());
105 infraActiveRequests.setVnfName("vnfInstanceNameTest");
106 infraActiveRequests.setVfModuleId(UUID.randomUUID().toString());
107 infraActiveRequests.setVfModuleName("vfModuleInstanceNameTest");
108 infraActiveRequests.setVolumeGroupId(UUID.randomUUID().toString());
109 infraActiveRequests.setVolumeGroupName("volumeGroupInstanceNameTest");
110 infraActiveRequests.setNetworkId(UUID.randomUUID().toString());
111 infraActiveRequests.setNetworkName("networkInstanceNameTest");
112 infraActiveRequests.setConfigurationId(UUID.randomUUID().toString());
113 infraActiveRequests.setConfigurationName("configurationInstanceNameTest");
114 infraActiveRequests.setCloudRegion("1");
115 infraActiveRequests.setTenantId(UUID.randomUUID().toString());
116 infraActiveRequests.setRequestScope("operationalEnvironment");
117 infraActiveRequests.setRequestorId(UUID.randomUUID().toString());
118 infraActiveRequests.setSource("sourceTest");
119 infraActiveRequests.setOperationalEnvName(UUID.randomUUID().toString());
120 infraActiveRequests.setRequestStatus("IN_PROGRESS");
122 .setRequestUrl("http://localhost:8080/onap/so/infra/serviceInstantiation/v7/serviceInstances");
124 saveInfraActiveRequest(infraActiveRequests);
127 public void saveInfraActiveRequest(InfraActiveRequests request) {
128 headers = new HttpHeaders();
129 restTemplate = new TestRestTemplate("test", "test");
131 headers.set("Accept", MediaType.APPLICATION_JSON);
132 headers.set("Content-Type", MediaType.APPLICATION_JSON);
133 headers.set("Authorization", msoAdaptersAuth);
135 HttpEntity<String> entity = new HttpEntity(request, headers);
137 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort("/infraActiveRequests"));
138 ResponseEntity<String> response =
139 restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
140 assertEquals(201, response.getStatusCodeValue());
145 public void getCloudOrchestrationFiltersFromInfraActiveTest() {
147 Map<String, String> requestMap = new HashMap<>();
148 requestMap.put("operationalEnvironmentId", infraActiveRequests.getOperationalEnvId());
149 requestMap.put("operationalEnvironmentName", infraActiveRequests.getOperationalEnvName());
150 requestMap.put("resourceType", "operationalEnvironment");
152 HttpEntity<Map<String, String>> entity = new HttpEntity<>(requestMap, headers);
154 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(
155 createURLWithPort("/infraActiveRequests") + "/getCloudOrchestrationFiltersFromInfraActive");
157 ResponseEntity<List<InfraActiveRequests>> response = restTemplate.exchange(builder.toUriString(),
158 HttpMethod.POST, entity, new ParameterizedTypeReference<List<InfraActiveRequests>>() {});
160 List<InfraActiveRequests> iarr = response.getBody();
161 assertEquals(200, response.getStatusCodeValue());
163 assertTrue(iarr.size() == 1);
164 infraActiveRequestsResponse = iarr.get(0);
166 verifyInfraActiveRequests();
171 public void getOrchestrationFiltersFromInfraActiveTest() {
173 Map<String, List<String>> requestMap = new HashMap<>();
174 List<String> values = new ArrayList<>();
175 values.add("EQUALS");
176 values.add(infraActiveRequests.getServiceInstanceId());
177 requestMap.put("serviceInstanceId", values);
179 values = new ArrayList<>();
180 values.add("EQUALS");
181 values.add(infraActiveRequests.getServiceInstanceName());
182 requestMap.put("serviceInstanceName", values);
184 HttpEntity<Map<String, List<String>>> entityList = new HttpEntity(requestMap, headers);
185 UriComponentsBuilder builder = UriComponentsBuilder
186 .fromHttpUrl(createURLWithPort("/infraActiveRequests") + "/getOrchestrationFiltersFromInfraActive");
188 ResponseEntity<List<InfraActiveRequests>> response = restTemplate.exchange(builder.toUriString(),
189 HttpMethod.POST, entityList, new ParameterizedTypeReference<List<InfraActiveRequests>>() {});
191 List<InfraActiveRequests> iarr = response.getBody();
193 assertEquals(200, response.getStatusCodeValue());
195 assertTrue(iarr.size() == 1);
196 infraActiveRequestsResponse = iarr.get(0);
198 verifyInfraActiveRequests();
202 public void checkVnfIdStatusTest() {
205 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(
206 "/infraActiveRequests" + "/checkVnfIdStatus/" + infraActiveRequests.getOperationalEnvId()));
207 HttpEntity<String> entity = new HttpEntity(HttpEntity.EMPTY, headers);
209 ResponseEntity<InfraActiveRequests> response =
210 restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, InfraActiveRequests.class);
212 infraActiveRequestsResponse = response.getBody();
214 assertEquals(200, response.getStatusCodeValue());
216 verifyInfraActiveRequests();
220 public void checkInstanceNameDuplicateTest() {
222 InstanceNameDuplicateCheckRequest instanceNameDuplicateCheckRequest =
223 new InstanceNameDuplicateCheckRequest((HashMap<String, String>) null,
224 infraActiveRequests.getOperationalEnvName(), infraActiveRequests.getRequestScope());
226 HttpEntity<InstanceNameDuplicateCheckRequest> entityList =
227 new HttpEntity(instanceNameDuplicateCheckRequest, headers);
228 UriComponentsBuilder builder = UriComponentsBuilder
229 .fromHttpUrl(createURLWithPort("/infraActiveRequests") + "/checkInstanceNameDuplicate");
231 ResponseEntity<InfraActiveRequests> response = restTemplate.exchange(builder.toUriString(), HttpMethod.POST,
232 entityList, new ParameterizedTypeReference<InfraActiveRequests>() {});
234 infraActiveRequestsResponse = response.getBody();
236 assertEquals(200, response.getStatusCodeValue());
238 verifyInfraActiveRequests();
242 public void checkInstanceNameDuplicateViaTest() {
244 Map<String, String> requestMap = new HashMap<>();
245 requestMap.put("operationalEnvironmentId", infraActiveRequests.getOperationalEnvId());
247 InstanceNameDuplicateCheckRequest instanceNameDuplicateCheckRequest = new InstanceNameDuplicateCheckRequest(
248 (HashMap<String, String>) requestMap, null, infraActiveRequests.getRequestScope());
250 HttpEntity<InstanceNameDuplicateCheckRequest> entityList =
251 new HttpEntity(instanceNameDuplicateCheckRequest, headers);
252 UriComponentsBuilder builder = UriComponentsBuilder
253 .fromHttpUrl(createURLWithPort("/infraActiveRequests") + "/checkInstanceNameDuplicate");
255 ResponseEntity<InfraActiveRequests> response = restTemplate.exchange(builder.toUriString(), HttpMethod.POST,
256 entityList, new ParameterizedTypeReference<InfraActiveRequests>() {});
258 infraActiveRequestsResponse = response.getBody();
260 assertEquals(200, response.getStatusCodeValue());
262 verifyInfraActiveRequests();
266 public void checkInstanceNameDuplicateTestNotFound() {
268 String instanceNameDuplicateCheckRequest =
269 "{\r\n\t \"instanceName\":\"TestNotFoundInstanceName\",\r\n\t \"requestScope\":\"testasdfasdfasdf\"\r\n}";
271 HttpEntity<InstanceNameDuplicateCheckRequest> entityList =
272 new HttpEntity(instanceNameDuplicateCheckRequest, headers);
273 UriComponentsBuilder builder = UriComponentsBuilder
274 .fromHttpUrl(createURLWithPort("/infraActiveRequests") + "/checkInstanceNameDuplicate");
276 ResponseEntity<InfraActiveRequests> response = restTemplate.exchange(builder.toUriString(), HttpMethod.POST,
277 entityList, new ParameterizedTypeReference<InfraActiveRequests>() {});
279 assertEquals(200, response.getStatusCodeValue());
280 assertEquals(null, response.getBody());
284 public void checkInstanceNameDuplicateViaTestNotFound() {
286 Map<String, String> requestMap = new HashMap<>();
287 requestMap.put("operationalEnvironmentId", "NotFoundOperationalEnvId");
289 InstanceNameDuplicateCheckRequest instanceNameDuplicateCheckRequest = new InstanceNameDuplicateCheckRequest(
290 (HashMap<String, String>) requestMap, null, infraActiveRequests.getRequestScope());
292 HttpEntity<InstanceNameDuplicateCheckRequest> entityList =
293 new HttpEntity(instanceNameDuplicateCheckRequest, headers);
294 UriComponentsBuilder builder = UriComponentsBuilder
295 .fromHttpUrl(createURLWithPort("/infraActiveRequests") + "/checkInstanceNameDuplicate");
297 ResponseEntity<InfraActiveRequests> response = restTemplate.exchange(builder.toUriString(), HttpMethod.POST,
298 entityList, new ParameterizedTypeReference<InfraActiveRequests>() {});
300 infraActiveRequestsResponse = response.getBody();
302 assertEquals(200, response.getStatusCodeValue());
303 assertEquals(null, response.getBody());
307 public void getInProgressVolumeGroupsAndVfModulesTest() {
308 boolean expectedReturned = false;
309 InfraActiveRequests request = new InfraActiveRequests();
310 request.setRequestId(UUID.randomUUID().toString());
311 request.setVfModuleId(UUID.randomUUID().toString());
312 request.setRequestStatus("IN_PROGRESS");
313 request.setRequestScope(ModelType.vfModule.toString());
314 Instant startInstant = Instant.now().minus(3, ChronoUnit.MINUTES);
315 request.setStartTime(Timestamp.from(startInstant));
316 request.setRequestAction("create");
317 saveInfraActiveRequest(request);
319 UriComponentsBuilder builder = UriComponentsBuilder
320 .fromHttpUrl(createURLWithPort("/infraActiveRequests/getInProgressVolumeGroupsAndVfModules"));
322 HttpEntity<String> entity = new HttpEntity<String>(headers);
324 ResponseEntity<List<InfraActiveRequests>> response = restTemplate.exchange(builder.toUriString(),
325 HttpMethod.GET, entity, new ParameterizedTypeReference<List<InfraActiveRequests>>() {});
327 List<InfraActiveRequests> responseList = response.getBody();
329 assertEquals(200, response.getStatusCodeValue());
331 for (InfraActiveRequests result : responseList) {
332 if (result.getRequestId().equals(request.getRequestId())) {
333 assertThat(request, sameBeanAs(result).ignoring("modifyTime"));
334 expectedReturned = true;
337 assertTrue(expectedReturned);