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 assertEquals(GENERIC_VNF_URL + VNF_ID, relationship.getRelatedLink());
150 assertFalse("RelationshipData list should not be empty", relationship.getRelationshipData().isEmpty());
151 assertFalse("RelatedToProperty list should not be empty", relationship.getRelatedToProperty().isEmpty());
153 final RelationshipData relationshipData = relationship.getRelationshipData().get(0);
154 assertEquals(Constants.GENERIC_VNF_VNF_ID, relationshipData.getRelationshipKey());
155 assertEquals(TestConstants.VNF_ID, relationshipData.getRelationshipValue());
157 final RelatedToProperty relatedToProperty = relationship.getRelatedToProperty().get(0);
158 assertEquals(Constants.GENERIC_VNF_VNF_NAME, relatedToProperty.getPropertyKey());
159 assertEquals(TestConstants.GENERIC_VNF_NAME, relatedToProperty.getPropertyValue());
164 public void test_putTenant_successfullyAddedToCache() throws Exception {
165 final String cloudRegionUrl = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
167 invokeCloudRegionHttpPutEndPointAndAssertResponse(cloudRegionUrl);
169 final String tenantUrl =
170 getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME + TENANTS_TENANT + TENANT_ID);
171 addTenantAndAssertResponse(tenantUrl);
173 final ResponseEntity<Tenant> response = testRestTemplateService.invokeHttpGet(tenantUrl, Tenant.class);
174 assertEquals(HttpStatus.OK, response.getStatusCode());
176 assertTrue(response.hasBody());
178 final Tenant tenant = response.getBody();
179 assertEquals(TENANT_ID, tenant.getTenantId());
180 assertEquals("admin", tenant.getTenantName());
182 assertNotNull("ResourceVersion should not be null", tenant.getResourceVersion());
187 public void test_putTenantRelationToGenericVnf_successfullyAddedToCache() throws Exception {
189 addCustomerServiceAndGenericVnf();
191 final String cloudRegionUrl = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
192 invokeCloudRegionHttpPutEndPointAndAssertResponse(cloudRegionUrl);
194 final String tenantUrl =
195 getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME, TENANTS_TENANT + TENANT_ID);
196 addTenantAndAssertResponse(tenantUrl);
198 final String tenantRelationShipUrl = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME,
199 TENANTS_TENANT + TENANT_ID, RELATIONSHIP_LIST_RELATIONSHIP_URL);
201 final ResponseEntity<Void> tenantRelationShipResponse = testRestTemplateService
202 .invokeHttpPut(tenantRelationShipUrl, TestUtils.getGenericVnfRelatedLink(), Void.class);
203 assertEquals(HttpStatus.ACCEPTED, tenantRelationShipResponse.getStatusCode());
205 final Optional<Tenant> optional = cloudRegionCacheServiceProvider.getTenant(CLOUD_REGION_KEY, TENANT_ID);
207 assertTrue(optional.isPresent());
208 final Tenant actualTenant = optional.get();
209 final RelationshipList relationshipList = actualTenant.getRelationshipList();
210 assertNotNull(relationshipList);
211 assertFalse(relationshipList.getRelationship().isEmpty());
213 final Relationship relationship = relationshipList.getRelationship().get(0);
215 assertEquals(Constants.COMPOSED_OF, relationship.getRelationshipLabel());
216 assertFalse(relationship.getRelationshipData().isEmpty());
217 assertEquals(1, relationship.getRelationshipData().size());
219 final List<RelationshipData> relationshipDataList = relationship.getRelationshipData();
221 final RelationshipData relationshipData =
222 getRelationshipData(relationshipDataList, Constants.GENERIC_VNF_VNF_ID);
223 assertNotNull(relationshipData);
224 assertEquals(VNF_ID, relationshipData.getRelationshipValue());
226 final List<RelatedToProperty> relatedToPropertyList = relationship.getRelatedToProperty();
228 final RelatedToProperty property = getRelatedToProperty(relatedToPropertyList, Constants.GENERIC_VNF_VNF_NAME);
229 assertNotNull(property);
230 assertEquals(GENERIC_VNF_NAME, property.getPropertyValue());
232 final Optional<GenericVnf> genericVnfOptional = genericVnfCacheServiceProvider.getGenericVnf(VNF_ID);
233 assertTrue(genericVnfOptional.isPresent());
234 final GenericVnf actualGenericVnf = genericVnfOptional.get();
235 final RelationshipList relationshipListGenericVnf = actualGenericVnf.getRelationshipList();
236 assertNotNull(relationshipListGenericVnf);
237 assertFalse(relationshipListGenericVnf.getRelationship().isEmpty());
239 final Relationship relationshipGenericVnf = relationshipListGenericVnf.getRelationship().get(0);
241 assertEquals(Constants.BELONGS_TO, relationshipGenericVnf.getRelationshipLabel());
242 assertFalse(relationshipGenericVnf.getRelationshipData().isEmpty());
243 assertEquals(3, relationshipGenericVnf.getRelationshipData().size());
247 private void addTenantAndAssertResponse(final String tenantUrl) throws IOException {
248 final ResponseEntity<Void> responseEntity =
249 testRestTemplateService.invokeHttpPut(tenantUrl, TestUtils.getTenant(), Void.class);
250 assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
253 private void addCustomerServiceAndGenericVnf() throws Exception, IOException {
254 final ResponseEntity<Void> customerResponse =
255 testRestTemplateService.invokeHttpPut(getUrl(CUSTOMERS_URL), TestUtils.getCustomer(), Void.class);
256 assertEquals(HttpStatus.ACCEPTED, customerResponse.getStatusCode());
258 final String serviceInstanceUrl = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL);
259 final ResponseEntity<Void> serviceInstanceResponse =
260 testRestTemplateService.invokeHttpPut(serviceInstanceUrl, TestUtils.getServiceInstance(), Void.class);
261 assertEquals(HttpStatus.ACCEPTED, serviceInstanceResponse.getStatusCode());
263 final String genericVnfUrl = getUrl(GENERIC_VNF_URL, VNF_ID);
264 final ResponseEntity<Void> genericVnfResponse =
265 testRestTemplateService.invokeHttpPut(genericVnfUrl, TestUtils.getGenericVnf(), Void.class);
266 assertEquals(HttpStatus.ACCEPTED, genericVnfResponse.getStatusCode());
270 private void invokeCloudRegionHttpPutEndPointAndAssertResponse(final String url) throws IOException {
271 final ResponseEntity<Void> responseEntity =
272 testRestTemplateService.invokeHttpPut(url, TestUtils.getCloudRegion(), Void.class);
273 assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());