2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.aaisimulator.controller;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.onap.aaisimulator.utils.Constants.BI_DIRECTIONAL_RELATIONSHIP_LIST_URL;
27 import static org.onap.aaisimulator.utils.Constants.RELATIONSHIP_LIST_RELATIONSHIP_URL;
28 import static org.onap.aaisimulator.utils.Constants.VSERVER;
29 import static org.onap.aaisimulator.utils.TestConstants.CLOUD_OWNER_NAME;
30 import static org.onap.aaisimulator.utils.TestConstants.CLOUD_REGION_NAME;
31 import static org.onap.aaisimulator.utils.TestConstants.CUSTOMERS_URL;
32 import static org.onap.aaisimulator.utils.TestConstants.ESR_PASSWORD;
33 import static org.onap.aaisimulator.utils.TestConstants.ESR_SERVICE_URL;
34 import static org.onap.aaisimulator.utils.TestConstants.ESR_SYSTEM_INFO_ID;
35 import static org.onap.aaisimulator.utils.TestConstants.ESR_SYSTEM_INFO_LIST_URL;
36 import static org.onap.aaisimulator.utils.TestConstants.ESR_SYSTEM_TYPE;
37 import static org.onap.aaisimulator.utils.TestConstants.ESR_TYEP;
38 import static org.onap.aaisimulator.utils.TestConstants.ESR_USERNAME;
39 import static org.onap.aaisimulator.utils.TestConstants.ESR_VENDOR;
40 import static org.onap.aaisimulator.utils.TestConstants.GENERIC_VNF_NAME;
41 import static org.onap.aaisimulator.utils.TestConstants.GENERIC_VNF_URL;
42 import static org.onap.aaisimulator.utils.TestConstants.SERVICE_INSTANCE_URL;
43 import static org.onap.aaisimulator.utils.TestConstants.SERVICE_SUBSCRIPTIONS_URL;
44 import static org.onap.aaisimulator.utils.TestConstants.SYSTEM_NAME;
45 import static org.onap.aaisimulator.utils.TestConstants.TENANTS_TENANT;
46 import static org.onap.aaisimulator.utils.TestConstants.TENANT_ID;
47 import static org.onap.aaisimulator.utils.TestConstants.VNF_ID;
48 import static org.onap.aaisimulator.utils.TestConstants.VSERVER_ID;
49 import static org.onap.aaisimulator.utils.TestConstants.VSERVER_NAME;
50 import static org.onap.aaisimulator.utils.TestConstants.VSERVER_URL;
51 import java.io.IOException;
52 import java.util.List;
53 import java.util.Optional;
54 import org.junit.After;
55 import org.junit.Test;
56 import org.onap.aai.domain.yang.CloudRegion;
57 import org.onap.aai.domain.yang.EsrSystemInfo;
58 import org.onap.aai.domain.yang.EsrSystemInfoList;
59 import org.onap.aai.domain.yang.GenericVnf;
60 import org.onap.aai.domain.yang.RelatedToProperty;
61 import org.onap.aai.domain.yang.Relationship;
62 import org.onap.aai.domain.yang.RelationshipData;
63 import org.onap.aai.domain.yang.RelationshipList;
64 import org.onap.aai.domain.yang.Tenant;
65 import org.onap.aai.domain.yang.Vserver;
66 import org.onap.aaisimulator.models.CloudRegionKey;
67 import org.onap.aaisimulator.service.providers.CloudRegionCacheServiceProvider;
68 import org.onap.aaisimulator.service.providers.CustomerCacheServiceProvider;
69 import org.onap.aaisimulator.service.providers.GenericVnfCacheServiceProvider;
70 import org.onap.aaisimulator.utils.Constants;
71 import org.onap.aaisimulator.utils.TestConstants;
72 import org.onap.aaisimulator.utils.TestUtils;
73 import org.springframework.beans.factory.annotation.Autowired;
74 import org.springframework.http.HttpStatus;
75 import org.springframework.http.ResponseEntity;
78 * @author Waqas Ikram (waqas.ikram@est.tech)
81 public class CloudRegionsControllerTest extends AbstractSpringBootTest {
83 private static final CloudRegionKey CLOUD_REGION_KEY = new CloudRegionKey(CLOUD_OWNER_NAME, CLOUD_REGION_NAME);
86 private CloudRegionCacheServiceProvider cloudRegionCacheServiceProvider;
89 private CustomerCacheServiceProvider customerCacheServiceProvider;
92 private GenericVnfCacheServiceProvider genericVnfCacheServiceProvider;
96 cloudRegionCacheServiceProvider.clearAll();
97 customerCacheServiceProvider.clearAll();
98 genericVnfCacheServiceProvider.clearAll();
102 public void test_putCloudRegion_successfullyAddedToCache() throws Exception {
103 final String url = getUrl(TestConstants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
105 invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
107 final ResponseEntity<CloudRegion> response = testRestTemplateService.invokeHttpGet(url, CloudRegion.class);
108 assertEquals(HttpStatus.OK, response.getStatusCode());
110 assertTrue(response.hasBody());
112 final CloudRegion cloudRegion = response.getBody();
113 assertEquals(CLOUD_OWNER_NAME, cloudRegion.getCloudOwner());
114 assertEquals(CLOUD_REGION_NAME, cloudRegion.getCloudRegionId());
116 assertNotNull("ResourceVersion should not be null", cloudRegion.getResourceVersion());
121 public void test_getCloudRegionWithDepthValue_shouldReturnMatchedCloudRegion() throws Exception {
122 final String url = getUrl(TestConstants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
124 invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
126 final ResponseEntity<CloudRegion> response =
127 testRestTemplateService.invokeHttpGet(url + "?depth=2", CloudRegion.class);
128 assertEquals(HttpStatus.OK, response.getStatusCode());
130 assertTrue(response.hasBody());
132 final CloudRegion cloudRegion = response.getBody();
133 assertEquals(CLOUD_OWNER_NAME, cloudRegion.getCloudOwner());
134 assertEquals(CLOUD_REGION_NAME, cloudRegion.getCloudRegionId());
136 assertNotNull("ResourceVersion should not be null", cloudRegion.getResourceVersion());
141 public void test_putGenericVnfRelationShipToPlatform_successfullyAddedToCache() throws Exception {
143 final String url = getUrl(TestConstants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
145 invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
147 final String relationShipUrl = getUrl(TestConstants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME,
148 BI_DIRECTIONAL_RELATIONSHIP_LIST_URL);
150 final ResponseEntity<Relationship> responseEntity = testRestTemplateService.invokeHttpPut(relationShipUrl,
151 TestUtils.getGenericVnfRelationShip(), Relationship.class);
152 assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
154 final Optional<CloudRegion> optional = cloudRegionCacheServiceProvider.getCloudRegion(CLOUD_REGION_KEY);
155 assertTrue(optional.isPresent());
157 final CloudRegion actual = optional.get();
159 assertNotNull(actual.getRelationshipList());
160 final List<Relationship> relationshipList = actual.getRelationshipList().getRelationship();
161 assertFalse("Relationship list should not be empty", relationshipList.isEmpty());
162 final Relationship relationship = relationshipList.get(0);
164 assertEquals(GENERIC_VNF_URL + VNF_ID, relationship.getRelatedLink());
166 assertFalse("RelationshipData list should not be empty", relationship.getRelationshipData().isEmpty());
167 assertFalse("RelatedToProperty list should not be empty", relationship.getRelatedToProperty().isEmpty());
169 final RelationshipData relationshipData = relationship.getRelationshipData().get(0);
170 assertEquals(Constants.GENERIC_VNF_VNF_ID, relationshipData.getRelationshipKey());
171 assertEquals(TestConstants.VNF_ID, relationshipData.getRelationshipValue());
173 final RelatedToProperty relatedToProperty = relationship.getRelatedToProperty().get(0);
174 assertEquals(Constants.GENERIC_VNF_VNF_NAME, relatedToProperty.getPropertyKey());
175 assertEquals(TestConstants.GENERIC_VNF_NAME, relatedToProperty.getPropertyValue());
180 public void test_putTenant_successfullyAddedToCache() throws Exception {
181 final String cloudRegionUrl = getUrl(TestConstants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
183 invokeCloudRegionHttpPutEndPointAndAssertResponse(cloudRegionUrl);
185 final String tenantUrl = getUrl(TestConstants.CLOUD_REGIONS, CLOUD_OWNER_NAME,
186 "/" + CLOUD_REGION_NAME + TENANTS_TENANT + TENANT_ID);
187 addTenantAndAssertResponse(tenantUrl);
189 final ResponseEntity<Tenant> response = testRestTemplateService.invokeHttpGet(tenantUrl, Tenant.class);
190 assertEquals(HttpStatus.OK, response.getStatusCode());
192 assertTrue(response.hasBody());
194 final Tenant tenant = response.getBody();
195 assertEquals(TENANT_ID, tenant.getTenantId());
196 assertEquals("admin", tenant.getTenantName());
198 assertNotNull("ResourceVersion should not be null", tenant.getResourceVersion());
203 public void test_putTenantRelationToGenericVnf_successfullyAddedToCache() throws Exception {
205 addCustomerServiceAndGenericVnf();
207 final String cloudRegionUrl = getUrl(TestConstants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
208 invokeCloudRegionHttpPutEndPointAndAssertResponse(cloudRegionUrl);
210 final String tenantUrl = getUrl(TestConstants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME,
211 TENANTS_TENANT + TENANT_ID);
212 addTenantAndAssertResponse(tenantUrl);
214 final String tenantRelationShipUrl = getUrl(TestConstants.CLOUD_REGIONS, CLOUD_OWNER_NAME,
215 "/" + CLOUD_REGION_NAME, TENANTS_TENANT + TENANT_ID, RELATIONSHIP_LIST_RELATIONSHIP_URL);
217 final ResponseEntity<Void> tenantRelationShipResponse = testRestTemplateService
218 .invokeHttpPut(tenantRelationShipUrl, TestUtils.getGenericVnfRelatedLink(), Void.class);
219 assertEquals(HttpStatus.ACCEPTED, tenantRelationShipResponse.getStatusCode());
221 final Optional<Tenant> optional = cloudRegionCacheServiceProvider.getTenant(CLOUD_REGION_KEY, TENANT_ID);
223 assertTrue(optional.isPresent());
224 final Tenant actualTenant = optional.get();
225 final RelationshipList relationshipList = actualTenant.getRelationshipList();
226 assertNotNull(relationshipList);
227 assertFalse(relationshipList.getRelationship().isEmpty());
229 final Relationship relationship = relationshipList.getRelationship().get(0);
231 assertEquals(Constants.BELONGS_TO, relationship.getRelationshipLabel());
232 assertFalse(relationship.getRelationshipData().isEmpty());
233 assertEquals(1, relationship.getRelationshipData().size());
235 final List<RelationshipData> relationshipDataList = relationship.getRelationshipData();
237 final RelationshipData relationshipData =
238 getRelationshipData(relationshipDataList, Constants.GENERIC_VNF_VNF_ID);
239 assertNotNull(relationshipData);
240 assertEquals(VNF_ID, relationshipData.getRelationshipValue());
242 final List<RelatedToProperty> relatedToPropertyList = relationship.getRelatedToProperty();
244 final RelatedToProperty property = getRelatedToProperty(relatedToPropertyList, Constants.GENERIC_VNF_VNF_NAME);
245 assertNotNull(property);
246 assertEquals(GENERIC_VNF_NAME, property.getPropertyValue());
248 final Optional<GenericVnf> genericVnfOptional = genericVnfCacheServiceProvider.getGenericVnf(VNF_ID);
249 assertTrue(genericVnfOptional.isPresent());
250 final GenericVnf actualGenericVnf = genericVnfOptional.get();
251 final RelationshipList relationshipListGenericVnf = actualGenericVnf.getRelationshipList();
252 assertNotNull(relationshipListGenericVnf);
253 assertFalse(relationshipListGenericVnf.getRelationship().isEmpty());
255 final Relationship relationshipGenericVnf = relationshipListGenericVnf.getRelationship().get(0);
257 assertEquals(Constants.BELONGS_TO, relationshipGenericVnf.getRelationshipLabel());
258 assertFalse(relationshipGenericVnf.getRelationshipData().isEmpty());
259 assertEquals(3, relationshipGenericVnf.getRelationshipData().size());
264 public void test_putEsrSystemInfo_successfullyAddedToCache() throws Exception {
265 final String url = getUrl(TestConstants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
267 invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
269 final String esrSystemInfoListUrl = getUrl(TestConstants.CLOUD_REGIONS, CLOUD_OWNER_NAME,
270 "/" + CLOUD_REGION_NAME, ESR_SYSTEM_INFO_LIST_URL);
272 final String esrSystemInfoUrl = esrSystemInfoListUrl + "/esr-system-info/" + ESR_SYSTEM_INFO_ID;
273 final ResponseEntity<Void> esrSystemInfoResponse =
274 testRestTemplateService.invokeHttpPut(esrSystemInfoUrl, TestUtils.getEsrSystemInfo(), Void.class);
275 assertEquals(HttpStatus.ACCEPTED, esrSystemInfoResponse.getStatusCode());
277 final ResponseEntity<EsrSystemInfoList> response =
278 testRestTemplateService.invokeHttpGet(esrSystemInfoListUrl, EsrSystemInfoList.class);
279 assertEquals(HttpStatus.OK, response.getStatusCode());
281 assertTrue(response.hasBody());
282 final EsrSystemInfoList actualEsrSystemInfoList = response.getBody();
284 final List<EsrSystemInfo> esrSystemInfoList = actualEsrSystemInfoList.getEsrSystemInfo();
285 assertNotNull(esrSystemInfoList);
286 assertEquals(1, esrSystemInfoList.size());
288 final EsrSystemInfo esrSystemInfo = esrSystemInfoList.get(0);
289 assertEquals(ESR_SYSTEM_INFO_ID, esrSystemInfo.getEsrSystemInfoId());
290 assertEquals(SYSTEM_NAME, esrSystemInfo.getSystemName());
291 assertEquals(ESR_TYEP, esrSystemInfo.getType());
292 assertEquals(ESR_VENDOR, esrSystemInfo.getVendor());
293 assertEquals(ESR_SERVICE_URL, esrSystemInfo.getServiceUrl());
294 assertEquals(ESR_USERNAME, esrSystemInfo.getUserName());
295 assertEquals(ESR_PASSWORD, esrSystemInfo.getPassword());
296 assertEquals(ESR_SYSTEM_TYPE, esrSystemInfo.getSystemType());
300 public void test_putVServer_successfullyAddedToCache() throws Exception {
301 final String url = getUrl(TestConstants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
303 invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
304 addCustomerServiceAndGenericVnf();
306 final String tenantUrl = url + TENANTS_TENANT + TENANT_ID;
307 addTenantAndAssertResponse(tenantUrl);
309 final String vServerUrl = tenantUrl + VSERVER_URL + VSERVER_ID;
311 final ResponseEntity<Void> vServerResponse =
312 testRestTemplateService.invokeHttpPut(vServerUrl, TestUtils.getVserver(), Void.class);
313 assertEquals(HttpStatus.ACCEPTED, vServerResponse.getStatusCode());
315 final ResponseEntity<Vserver> response = testRestTemplateService.invokeHttpGet(vServerUrl, Vserver.class);
316 assertEquals(HttpStatus.OK, response.getStatusCode());
318 assertTrue(response.hasBody());
319 final Vserver actualVserver = response.getBody();
320 assertEquals(VSERVER_NAME, actualVserver.getVserverName());
321 assertEquals(VSERVER_ID, actualVserver.getVserverId());
322 assertEquals("active", actualVserver.getProvStatus());
323 assertNotNull(actualVserver.getRelationshipList());
324 assertFalse(actualVserver.getRelationshipList().getRelationship().isEmpty());
326 final Optional<GenericVnf> optional = genericVnfCacheServiceProvider.getGenericVnf(VNF_ID);
327 assertTrue(optional.isPresent());
328 final GenericVnf genericVnf = optional.get();
329 assertNotNull(genericVnf.getRelationshipList());
330 assertFalse(genericVnf.getRelationshipList().getRelationship().isEmpty());
332 final Relationship expectedRelationShip = genericVnf.getRelationshipList().getRelationship().get(0);
333 assertEquals(VSERVER, expectedRelationShip.getRelatedTo());
334 assertNotNull(expectedRelationShip.getRelationshipData());
335 assertEquals(4, expectedRelationShip.getRelationshipData().size());
337 final List<RelationshipData> relationshipDataList = expectedRelationShip.getRelationshipData();
338 final RelationshipData vServerrelationshipData =
339 getRelationshipData(relationshipDataList, Constants.VSERVER_VSERVER_ID);
340 assertNotNull(vServerrelationshipData);
341 assertEquals(VSERVER_ID, vServerrelationshipData.getRelationshipValue());
343 final RelationshipData cloudOwnerRelationshipData =
344 getRelationshipData(relationshipDataList, Constants.CLOUD_REGION_CLOUD_OWNER);
345 assertNotNull(cloudOwnerRelationshipData);
346 assertEquals(CLOUD_OWNER_NAME, cloudOwnerRelationshipData.getRelationshipValue());
348 final RelationshipData cloudRegionIdRelationshipData =
349 getRelationshipData(relationshipDataList, Constants.CLOUD_REGION_CLOUD_REGION_ID);
350 assertNotNull(cloudRegionIdRelationshipData);
351 assertEquals(CLOUD_REGION_NAME, cloudRegionIdRelationshipData.getRelationshipValue());
353 final RelationshipData tenantRelationshipData =
354 getRelationshipData(relationshipDataList, Constants.TENANT_TENANT_ID);
355 assertNotNull(tenantRelationshipData);
356 assertEquals(TENANT_ID, tenantRelationshipData.getRelationshipValue());
361 public void test_deleteVServer_successfullyRemoveFromCache() throws Exception {
362 final String url = getUrl(TestConstants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
364 invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
365 addCustomerServiceAndGenericVnf();
367 final String tenantUrl = url + TENANTS_TENANT + TENANT_ID;
368 addTenantAndAssertResponse(tenantUrl);
370 final String vServerAddUrl = tenantUrl + VSERVER_URL + VSERVER_ID;
372 final ResponseEntity<Void> vServerAddResponse =
373 testRestTemplateService.invokeHttpPut(vServerAddUrl, TestUtils.getVserver(), Void.class);
374 assertEquals(HttpStatus.ACCEPTED, vServerAddResponse.getStatusCode());
376 final Optional<Vserver> optional =
377 cloudRegionCacheServiceProvider.getVserver(CLOUD_REGION_KEY, TENANT_ID, VSERVER_ID);
378 assertTrue(optional.isPresent());
379 final Vserver vserver = optional.get();
381 final String vServerRemoveUrl = vServerAddUrl + "?resource-version=" + vserver.getResourceVersion();
383 final ResponseEntity<Void> responseEntity =
384 testRestTemplateService.invokeHttpDelete(vServerRemoveUrl, Void.class);
385 assertEquals(HttpStatus.NO_CONTENT, responseEntity.getStatusCode());
386 assertFalse(cloudRegionCacheServiceProvider.getVserver(CLOUD_REGION_KEY, TENANT_ID, VSERVER_ID).isPresent());
391 private void addTenantAndAssertResponse(final String tenantUrl) throws IOException {
392 final ResponseEntity<Void> responseEntity =
393 testRestTemplateService.invokeHttpPut(tenantUrl, TestUtils.getTenant(), Void.class);
394 assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
397 private void addCustomerServiceAndGenericVnf() throws Exception, IOException {
398 final ResponseEntity<Void> customerResponse =
399 testRestTemplateService.invokeHttpPut(getUrl(CUSTOMERS_URL), TestUtils.getCustomer(), Void.class);
400 assertEquals(HttpStatus.ACCEPTED, customerResponse.getStatusCode());
402 final String serviceInstanceUrl = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL);
403 final ResponseEntity<Void> serviceInstanceResponse =
404 testRestTemplateService.invokeHttpPut(serviceInstanceUrl, TestUtils.getServiceInstance(), Void.class);
405 assertEquals(HttpStatus.ACCEPTED, serviceInstanceResponse.getStatusCode());
407 final String genericVnfUrl = getUrl(GENERIC_VNF_URL, VNF_ID);
408 final ResponseEntity<Void> genericVnfResponse =
409 testRestTemplateService.invokeHttpPut(genericVnfUrl, TestUtils.getGenericVnf(), Void.class);
410 assertEquals(HttpStatus.ACCEPTED, genericVnfResponse.getStatusCode());
414 private void invokeCloudRegionHttpPutEndPointAndAssertResponse(final String url) throws IOException {
415 final ResponseEntity<Void> responseEntity =
416 testRestTemplateService.invokeHttpPut(url, TestUtils.getCloudRegion(), Void.class);
417 assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());