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.so.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.so.aaisimulator.utils.Constants.BI_DIRECTIONAL_RELATIONSHIP_LIST_URL;
27 import static org.onap.so.aaisimulator.utils.TestConstants.CLOUD_OWNER_NAME;
28 import static org.onap.so.aaisimulator.utils.TestConstants.CLOUD_REGION_NAME;
29 import static org.onap.so.aaisimulator.utils.TestConstants.TENANTS_TENANT;
30 import static org.onap.so.aaisimulator.utils.TestConstants.TENANT_ID;
31 import java.io.IOException;
32 import java.util.List;
33 import java.util.Optional;
34 import org.junit.After;
35 import org.junit.Test;
36 import org.onap.aai.domain.yang.CloudRegion;
37 import org.onap.aai.domain.yang.RelatedToProperty;
38 import org.onap.aai.domain.yang.Relationship;
39 import org.onap.aai.domain.yang.RelationshipData;
40 import org.onap.aai.domain.yang.Tenant;
41 import org.onap.so.aaisimulator.models.CloudRegionKey;
42 import org.onap.so.aaisimulator.service.providers.CloudRegionCacheServiceProvider;
43 import org.onap.so.aaisimulator.utils.Constants;
44 import org.onap.so.aaisimulator.utils.TestConstants;
45 import org.onap.so.aaisimulator.utils.TestUtils;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.http.HttpStatus;
48 import org.springframework.http.ResponseEntity;
51 * @author Waqas Ikram (waqas.ikram@est.tech)
54 public class CloudRegionsControllerTest extends AbstractSpringBootTest {
56 private static final CloudRegionKey CLOUD_REGION_KEY = new CloudRegionKey(CLOUD_OWNER_NAME, CLOUD_REGION_NAME);
59 private CloudRegionCacheServiceProvider cloudRegionCacheServiceProvider;
63 cloudRegionCacheServiceProvider.clearAll();
67 public void test_putCloudRegion_successfullyAddedToCache() throws Exception {
68 final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
70 invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
72 final ResponseEntity<CloudRegion> response = testRestTemplateService.invokeHttpGet(url, CloudRegion.class);
73 assertEquals(HttpStatus.OK, response.getStatusCode());
75 assertTrue(response.hasBody());
77 final CloudRegion cloudRegion = response.getBody();
78 assertEquals(CLOUD_OWNER_NAME, cloudRegion.getCloudOwner());
79 assertEquals(CLOUD_REGION_NAME, cloudRegion.getCloudRegionId());
81 assertNotNull("ResourceVersion should not be null", cloudRegion.getResourceVersion());
86 public void test_getCloudRegionWithDepthValue_shouldReturnMatchedCloudRegion() throws Exception {
87 final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
89 invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
91 final ResponseEntity<CloudRegion> response =
92 testRestTemplateService.invokeHttpGet(url + "?depth=2", CloudRegion.class);
93 assertEquals(HttpStatus.OK, response.getStatusCode());
95 assertTrue(response.hasBody());
97 final CloudRegion cloudRegion = response.getBody();
98 assertEquals(CLOUD_OWNER_NAME, cloudRegion.getCloudOwner());
99 assertEquals(CLOUD_REGION_NAME, cloudRegion.getCloudRegionId());
101 assertNotNull("ResourceVersion should not be null", cloudRegion.getResourceVersion());
106 public void test_putGenericVnfRelationShipToPlatform_successfullyAddedToCache() throws Exception {
108 final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
110 invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
112 final String relationShipUrl = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME,
113 BI_DIRECTIONAL_RELATIONSHIP_LIST_URL);
115 final ResponseEntity<Relationship> responseEntity = testRestTemplateService.invokeHttpPut(relationShipUrl,
116 TestUtils.getGenericVnfRelationShip(), Relationship.class);
117 assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
119 final Optional<CloudRegion> optional = cloudRegionCacheServiceProvider.getCloudRegion(CLOUD_REGION_KEY);
120 assertTrue(optional.isPresent());
122 final CloudRegion actual = optional.get();
124 assertNotNull(actual.getRelationshipList());
125 final List<Relationship> relationshipList = actual.getRelationshipList().getRelationship();
126 assertFalse("Relationship list should not be empty", relationshipList.isEmpty());
127 final Relationship relationship = relationshipList.get(0);
129 assertFalse("RelationshipData list should not be empty", relationship.getRelationshipData().isEmpty());
130 assertFalse("RelatedToProperty list should not be empty", relationship.getRelatedToProperty().isEmpty());
132 final RelationshipData relationshipData = relationship.getRelationshipData().get(0);
133 assertEquals(Constants.GENERIC_VNF_VNF_ID, relationshipData.getRelationshipKey());
134 assertEquals(TestConstants.VNF_ID, relationshipData.getRelationshipValue());
136 final RelatedToProperty relatedToProperty = relationship.getRelatedToProperty().get(0);
137 assertEquals(Constants.GENERIC_VNF_VNF_NAME, relatedToProperty.getPropertyKey());
138 assertEquals(TestConstants.GENERIC_VNF_NAME, relatedToProperty.getPropertyValue());
143 public void test_putTenant_successfullyAddedToCache() throws Exception {
144 final String cloudRegionUrl = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
146 invokeCloudRegionHttpPutEndPointAndAssertResponse(cloudRegionUrl);
148 final String tenantUrl =
149 getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME + TENANTS_TENANT + TENANT_ID);
150 final ResponseEntity<Void> responseEntity =
151 testRestTemplateService.invokeHttpPut(tenantUrl, TestUtils.getTenant(), Void.class);
152 assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
154 final ResponseEntity<Tenant> response = testRestTemplateService.invokeHttpGet(tenantUrl, Tenant.class);
155 assertEquals(HttpStatus.OK, response.getStatusCode());
157 assertTrue(response.hasBody());
159 final Tenant tenant = response.getBody();
160 assertEquals(TENANT_ID, tenant.getTenantId());
161 assertEquals("admin", tenant.getTenantName());
163 assertNotNull("ResourceVersion should not be null", tenant.getResourceVersion());
168 private void invokeCloudRegionHttpPutEndPointAndAssertResponse(final String url) throws IOException {
169 final ResponseEntity<Void> responseEntity =
170 testRestTemplateService.invokeHttpPut(url, TestUtils.getCloudRegion(), Void.class);
171 assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());