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.Constants.RELATIONSHIP_LIST_RELATIONSHIP_URL;
28 import static org.onap.so.aaisimulator.utils.TestConstants.CLOUD_OWNER_NAME;
29 import static org.onap.so.aaisimulator.utils.TestConstants.CLOUD_REGION_NAME;
30 import static org.onap.so.aaisimulator.utils.TestConstants.CUSTOMERS_URL;
31 import static org.onap.so.aaisimulator.utils.TestConstants.GENERIC_VNF_NAME;
32 import static org.onap.so.aaisimulator.utils.TestConstants.GENERIC_VNF_URL;
33 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_INSTANCE_URL;
34 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_SUBSCRIPTIONS_URL;
35 import static org.onap.so.aaisimulator.utils.TestConstants.TENANTS_TENANT;
36 import static org.onap.so.aaisimulator.utils.TestConstants.TENANT_ID;
37 import static org.onap.so.aaisimulator.utils.TestConstants.VNF_ID;
38 import java.io.IOException;
39 import java.util.List;
40 import java.util.Optional;
41 import org.junit.After;
42 import org.junit.Test;
43 import org.onap.aai.domain.yang.CloudRegion;
44 import org.onap.aai.domain.yang.GenericVnf;
45 import org.onap.aai.domain.yang.RelatedToProperty;
46 import org.onap.aai.domain.yang.Relationship;
47 import org.onap.aai.domain.yang.RelationshipData;
48 import org.onap.aai.domain.yang.RelationshipList;
49 import org.onap.aai.domain.yang.Tenant;
50 import org.onap.so.aaisimulator.models.CloudRegionKey;
51 import org.onap.so.aaisimulator.service.providers.CloudRegionCacheServiceProvider;
52 import org.onap.so.aaisimulator.service.providers.CustomerCacheServiceProvider;
53 import org.onap.so.aaisimulator.service.providers.GenericVnfCacheServiceProvider;
54 import org.onap.so.aaisimulator.utils.Constants;
55 import org.onap.so.aaisimulator.utils.TestConstants;
56 import org.onap.so.aaisimulator.utils.TestUtils;
57 import org.springframework.beans.factory.annotation.Autowired;
58 import org.springframework.http.HttpStatus;
59 import org.springframework.http.ResponseEntity;
62 * @author Waqas Ikram (waqas.ikram@est.tech)
65 public class CloudRegionsControllerTest extends AbstractSpringBootTest {
67 private static final CloudRegionKey CLOUD_REGION_KEY = new CloudRegionKey(CLOUD_OWNER_NAME, CLOUD_REGION_NAME);
70 private CloudRegionCacheServiceProvider cloudRegionCacheServiceProvider;
73 private CustomerCacheServiceProvider customerCacheServiceProvider;
76 private GenericVnfCacheServiceProvider genericVnfCacheServiceProvider;
80 cloudRegionCacheServiceProvider.clearAll();
81 customerCacheServiceProvider.clearAll();
82 genericVnfCacheServiceProvider.clearAll();
86 public void test_putCloudRegion_successfullyAddedToCache() throws Exception {
87 final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
89 invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
91 final ResponseEntity<CloudRegion> response = testRestTemplateService.invokeHttpGet(url, CloudRegion.class);
92 assertEquals(HttpStatus.OK, response.getStatusCode());
94 assertTrue(response.hasBody());
96 final CloudRegion cloudRegion = response.getBody();
97 assertEquals(CLOUD_OWNER_NAME, cloudRegion.getCloudOwner());
98 assertEquals(CLOUD_REGION_NAME, cloudRegion.getCloudRegionId());
100 assertNotNull("ResourceVersion should not be null", cloudRegion.getResourceVersion());
105 public void test_getCloudRegionWithDepthValue_shouldReturnMatchedCloudRegion() throws Exception {
106 final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
108 invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
110 final ResponseEntity<CloudRegion> response =
111 testRestTemplateService.invokeHttpGet(url + "?depth=2", CloudRegion.class);
112 assertEquals(HttpStatus.OK, response.getStatusCode());
114 assertTrue(response.hasBody());
116 final CloudRegion cloudRegion = response.getBody();
117 assertEquals(CLOUD_OWNER_NAME, cloudRegion.getCloudOwner());
118 assertEquals(CLOUD_REGION_NAME, cloudRegion.getCloudRegionId());
120 assertNotNull("ResourceVersion should not be null", cloudRegion.getResourceVersion());
125 public void test_putGenericVnfRelationShipToPlatform_successfullyAddedToCache() throws Exception {
127 final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
129 invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
131 final String relationShipUrl = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME,
132 BI_DIRECTIONAL_RELATIONSHIP_LIST_URL);
134 final ResponseEntity<Relationship> responseEntity = testRestTemplateService.invokeHttpPut(relationShipUrl,
135 TestUtils.getGenericVnfRelationShip(), Relationship.class);
136 assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
138 final Optional<CloudRegion> optional = cloudRegionCacheServiceProvider.getCloudRegion(CLOUD_REGION_KEY);
139 assertTrue(optional.isPresent());
141 final CloudRegion actual = optional.get();
143 assertNotNull(actual.getRelationshipList());
144 final List<Relationship> relationshipList = actual.getRelationshipList().getRelationship();
145 assertFalse("Relationship list should not be empty", relationshipList.isEmpty());
146 final Relationship relationship = relationshipList.get(0);
148 assertFalse("RelationshipData list should not be empty", relationship.getRelationshipData().isEmpty());
149 assertFalse("RelatedToProperty list should not be empty", relationship.getRelatedToProperty().isEmpty());
151 final RelationshipData relationshipData = relationship.getRelationshipData().get(0);
152 assertEquals(Constants.GENERIC_VNF_VNF_ID, relationshipData.getRelationshipKey());
153 assertEquals(TestConstants.VNF_ID, relationshipData.getRelationshipValue());
155 final RelatedToProperty relatedToProperty = relationship.getRelatedToProperty().get(0);
156 assertEquals(Constants.GENERIC_VNF_VNF_NAME, relatedToProperty.getPropertyKey());
157 assertEquals(TestConstants.GENERIC_VNF_NAME, relatedToProperty.getPropertyValue());
162 public void test_putTenant_successfullyAddedToCache() throws Exception {
163 final String cloudRegionUrl = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
165 invokeCloudRegionHttpPutEndPointAndAssertResponse(cloudRegionUrl);
167 final String tenantUrl =
168 getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME + TENANTS_TENANT + TENANT_ID);
169 addTenantAndAssertResponse(tenantUrl);
171 final ResponseEntity<Tenant> response = testRestTemplateService.invokeHttpGet(tenantUrl, Tenant.class);
172 assertEquals(HttpStatus.OK, response.getStatusCode());
174 assertTrue(response.hasBody());
176 final Tenant tenant = response.getBody();
177 assertEquals(TENANT_ID, tenant.getTenantId());
178 assertEquals("admin", tenant.getTenantName());
180 assertNotNull("ResourceVersion should not be null", tenant.getResourceVersion());
185 public void test_putTenantRelationToGenericVnf_successfullyAddedToCache() throws Exception {
187 addCustomerServiceAndGenericVnf();
189 final String cloudRegionUrl = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
190 invokeCloudRegionHttpPutEndPointAndAssertResponse(cloudRegionUrl);
192 final String tenantUrl =
193 getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME, TENANTS_TENANT + TENANT_ID);
194 addTenantAndAssertResponse(tenantUrl);
196 final String tenantRelationShipUrl = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME,
197 TENANTS_TENANT + TENANT_ID, RELATIONSHIP_LIST_RELATIONSHIP_URL);
199 final ResponseEntity<Void> tenantRelationShipResponse = testRestTemplateService
200 .invokeHttpPut(tenantRelationShipUrl, TestUtils.getGenericVnfRelatedLink(), Void.class);
201 assertEquals(HttpStatus.ACCEPTED, tenantRelationShipResponse.getStatusCode());
203 final Optional<Tenant> optional = cloudRegionCacheServiceProvider.getTenant(CLOUD_REGION_KEY, TENANT_ID);
205 assertTrue(optional.isPresent());
206 final Tenant actualTenant = optional.get();
207 final RelationshipList relationshipList = actualTenant.getRelationshipList();
208 assertNotNull(relationshipList);
209 assertFalse(relationshipList.getRelationship().isEmpty());
211 final Relationship relationship = relationshipList.getRelationship().get(0);
213 assertEquals(Constants.COMPOSED_OF, relationship.getRelationshipLabel());
214 assertFalse(relationship.getRelationshipData().isEmpty());
215 assertEquals(1, relationship.getRelationshipData().size());
217 final List<RelationshipData> relationshipDataList = relationship.getRelationshipData();
219 final RelationshipData relationshipData =
220 getRelationshipData(relationshipDataList, Constants.GENERIC_VNF_VNF_ID);
221 assertNotNull(relationshipData);
222 assertEquals(VNF_ID, relationshipData.getRelationshipValue());
224 final List<RelatedToProperty> relatedToPropertyList = relationship.getRelatedToProperty();
226 final RelatedToProperty property = getRelatedToProperty(relatedToPropertyList, Constants.GENERIC_VNF_VNF_NAME);
227 assertNotNull(property);
228 assertEquals(GENERIC_VNF_NAME, property.getPropertyValue());
230 final Optional<GenericVnf> genericVnfOptional = genericVnfCacheServiceProvider.getGenericVnf(VNF_ID);
231 assertTrue(genericVnfOptional.isPresent());
232 final GenericVnf actualGenericVnf = genericVnfOptional.get();
233 final RelationshipList relationshipListGenericVnf = actualGenericVnf.getRelationshipList();
234 assertNotNull(relationshipListGenericVnf);
235 assertFalse(relationshipListGenericVnf.getRelationship().isEmpty());
237 final Relationship relationshipGenericVnf = relationshipListGenericVnf.getRelationship().get(0);
239 assertEquals(Constants.BELONGS_TO, relationshipGenericVnf.getRelationshipLabel());
240 assertFalse(relationshipGenericVnf.getRelationshipData().isEmpty());
241 assertEquals(3, relationshipGenericVnf.getRelationshipData().size());
245 private void addTenantAndAssertResponse(final String tenantUrl) throws IOException {
246 final ResponseEntity<Void> responseEntity =
247 testRestTemplateService.invokeHttpPut(tenantUrl, TestUtils.getTenant(), Void.class);
248 assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
251 private void addCustomerServiceAndGenericVnf() throws Exception, IOException {
252 final ResponseEntity<Void> customerResponse =
253 testRestTemplateService.invokeHttpPut(getUrl(CUSTOMERS_URL), TestUtils.getCustomer(), Void.class);
254 assertEquals(HttpStatus.ACCEPTED, customerResponse.getStatusCode());
256 final String serviceInstanceUrl = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL);
257 final ResponseEntity<Void> serviceInstanceResponse =
258 testRestTemplateService.invokeHttpPut(serviceInstanceUrl, TestUtils.getServiceInstance(), Void.class);
259 assertEquals(HttpStatus.ACCEPTED, serviceInstanceResponse.getStatusCode());
261 final String genericVnfUrl = getUrl(GENERIC_VNF_URL, VNF_ID);
262 final ResponseEntity<Void> genericVnfResponse =
263 testRestTemplateService.invokeHttpPut(genericVnfUrl, TestUtils.getGenericVnf(), Void.class);
264 assertEquals(HttpStatus.ACCEPTED, genericVnfResponse.getStatusCode());
268 private RelationshipData getRelationshipData(final List<RelationshipData> relationshipData, final String key) {
269 return relationshipData.stream().filter(data -> data.getRelationshipKey().equals(key)).findFirst().orElse(null);
272 private RelatedToProperty getRelatedToProperty(final List<RelatedToProperty> relatedToPropertyList,
274 return relatedToPropertyList.stream().filter(data -> data.getPropertyKey().equals(key)).findFirst()
278 private void invokeCloudRegionHttpPutEndPointAndAssertResponse(final String url) throws IOException {
279 final ResponseEntity<Void> responseEntity =
280 testRestTemplateService.invokeHttpPut(url, TestUtils.getCloudRegion(), Void.class);
281 assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());