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.ESR_PASSWORD;
32 import static org.onap.so.aaisimulator.utils.TestConstants.ESR_SERVICE_URL;
33 import static org.onap.so.aaisimulator.utils.TestConstants.ESR_SYSTEM_INFO_ID;
34 import static org.onap.so.aaisimulator.utils.TestConstants.ESR_SYSTEM_INFO_LIST_URL;
35 import static org.onap.so.aaisimulator.utils.TestConstants.ESR_SYSTEM_TYPE;
36 import static org.onap.so.aaisimulator.utils.TestConstants.ESR_TYEP;
37 import static org.onap.so.aaisimulator.utils.TestConstants.ESR_USERNAME;
38 import static org.onap.so.aaisimulator.utils.TestConstants.ESR_VENDOR;
39 import static org.onap.so.aaisimulator.utils.TestConstants.GENERIC_VNF_NAME;
40 import static org.onap.so.aaisimulator.utils.TestConstants.GENERIC_VNF_URL;
41 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_INSTANCE_URL;
42 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_SUBSCRIPTIONS_URL;
43 import static org.onap.so.aaisimulator.utils.TestConstants.SYSTEM_NAME;
44 import static org.onap.so.aaisimulator.utils.TestConstants.TENANTS_TENANT;
45 import static org.onap.so.aaisimulator.utils.TestConstants.TENANT_ID;
46 import static org.onap.so.aaisimulator.utils.TestConstants.VNF_ID;
47 import static org.onap.so.aaisimulator.utils.TestConstants.VSERVER_ID;
48 import static org.onap.so.aaisimulator.utils.TestConstants.VSERVER_NAME;
49 import static org.onap.so.aaisimulator.utils.TestConstants.VSERVER_URL;
50 import java.io.IOException;
51 import java.util.List;
52 import java.util.Optional;
53 import org.junit.After;
54 import org.junit.Test;
55 import org.onap.aai.domain.yang.CloudRegion;
56 import org.onap.aai.domain.yang.EsrSystemInfo;
57 import org.onap.aai.domain.yang.EsrSystemInfoList;
58 import org.onap.aai.domain.yang.GenericVnf;
59 import org.onap.aai.domain.yang.RelatedToProperty;
60 import org.onap.aai.domain.yang.Relationship;
61 import org.onap.aai.domain.yang.RelationshipData;
62 import org.onap.aai.domain.yang.RelationshipList;
63 import org.onap.aai.domain.yang.Tenant;
64 import org.onap.aai.domain.yang.Vserver;
65 import org.onap.so.aaisimulator.models.CloudRegionKey;
66 import org.onap.so.aaisimulator.service.providers.CloudRegionCacheServiceProvider;
67 import org.onap.so.aaisimulator.service.providers.CustomerCacheServiceProvider;
68 import org.onap.so.aaisimulator.service.providers.GenericVnfCacheServiceProvider;
69 import org.onap.so.aaisimulator.utils.Constants;
70 import org.onap.so.aaisimulator.utils.TestConstants;
71 import org.onap.so.aaisimulator.utils.TestUtils;
72 import org.springframework.beans.factory.annotation.Autowired;
73 import org.springframework.http.HttpStatus;
74 import org.springframework.http.ResponseEntity;
77 * @author Waqas Ikram (waqas.ikram@est.tech)
80 public class CloudRegionsControllerTest extends AbstractSpringBootTest {
82 private static final CloudRegionKey CLOUD_REGION_KEY = new CloudRegionKey(CLOUD_OWNER_NAME, CLOUD_REGION_NAME);
85 private CloudRegionCacheServiceProvider cloudRegionCacheServiceProvider;
88 private CustomerCacheServiceProvider customerCacheServiceProvider;
91 private GenericVnfCacheServiceProvider genericVnfCacheServiceProvider;
95 cloudRegionCacheServiceProvider.clearAll();
96 customerCacheServiceProvider.clearAll();
97 genericVnfCacheServiceProvider.clearAll();
101 public void test_putCloudRegion_successfullyAddedToCache() throws Exception {
102 final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
104 invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
106 final ResponseEntity<CloudRegion> response = testRestTemplateService.invokeHttpGet(url, CloudRegion.class);
107 assertEquals(HttpStatus.OK, response.getStatusCode());
109 assertTrue(response.hasBody());
111 final CloudRegion cloudRegion = response.getBody();
112 assertEquals(CLOUD_OWNER_NAME, cloudRegion.getCloudOwner());
113 assertEquals(CLOUD_REGION_NAME, cloudRegion.getCloudRegionId());
115 assertNotNull("ResourceVersion should not be null", cloudRegion.getResourceVersion());
120 public void test_getCloudRegionWithDepthValue_shouldReturnMatchedCloudRegion() throws Exception {
121 final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
123 invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
125 final ResponseEntity<CloudRegion> response =
126 testRestTemplateService.invokeHttpGet(url + "?depth=2", CloudRegion.class);
127 assertEquals(HttpStatus.OK, response.getStatusCode());
129 assertTrue(response.hasBody());
131 final CloudRegion cloudRegion = response.getBody();
132 assertEquals(CLOUD_OWNER_NAME, cloudRegion.getCloudOwner());
133 assertEquals(CLOUD_REGION_NAME, cloudRegion.getCloudRegionId());
135 assertNotNull("ResourceVersion should not be null", cloudRegion.getResourceVersion());
140 public void test_putGenericVnfRelationShipToPlatform_successfullyAddedToCache() throws Exception {
142 final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
144 invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
146 final String relationShipUrl = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME,
147 BI_DIRECTIONAL_RELATIONSHIP_LIST_URL);
149 final ResponseEntity<Relationship> responseEntity = testRestTemplateService.invokeHttpPut(relationShipUrl,
150 TestUtils.getGenericVnfRelationShip(), Relationship.class);
151 assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
153 final Optional<CloudRegion> optional = cloudRegionCacheServiceProvider.getCloudRegion(CLOUD_REGION_KEY);
154 assertTrue(optional.isPresent());
156 final CloudRegion actual = optional.get();
158 assertNotNull(actual.getRelationshipList());
159 final List<Relationship> relationshipList = actual.getRelationshipList().getRelationship();
160 assertFalse("Relationship list should not be empty", relationshipList.isEmpty());
161 final Relationship relationship = relationshipList.get(0);
163 assertEquals(GENERIC_VNF_URL + VNF_ID, relationship.getRelatedLink());
165 assertFalse("RelationshipData list should not be empty", relationship.getRelationshipData().isEmpty());
166 assertFalse("RelatedToProperty list should not be empty", relationship.getRelatedToProperty().isEmpty());
168 final RelationshipData relationshipData = relationship.getRelationshipData().get(0);
169 assertEquals(Constants.GENERIC_VNF_VNF_ID, relationshipData.getRelationshipKey());
170 assertEquals(TestConstants.VNF_ID, relationshipData.getRelationshipValue());
172 final RelatedToProperty relatedToProperty = relationship.getRelatedToProperty().get(0);
173 assertEquals(Constants.GENERIC_VNF_VNF_NAME, relatedToProperty.getPropertyKey());
174 assertEquals(TestConstants.GENERIC_VNF_NAME, relatedToProperty.getPropertyValue());
179 public void test_putTenant_successfullyAddedToCache() throws Exception {
180 final String cloudRegionUrl = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
182 invokeCloudRegionHttpPutEndPointAndAssertResponse(cloudRegionUrl);
184 final String tenantUrl =
185 getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME + TENANTS_TENANT + TENANT_ID);
186 addTenantAndAssertResponse(tenantUrl);
188 final ResponseEntity<Tenant> response = testRestTemplateService.invokeHttpGet(tenantUrl, Tenant.class);
189 assertEquals(HttpStatus.OK, response.getStatusCode());
191 assertTrue(response.hasBody());
193 final Tenant tenant = response.getBody();
194 assertEquals(TENANT_ID, tenant.getTenantId());
195 assertEquals("admin", tenant.getTenantName());
197 assertNotNull("ResourceVersion should not be null", tenant.getResourceVersion());
202 public void test_putTenantRelationToGenericVnf_successfullyAddedToCache() throws Exception {
204 addCustomerServiceAndGenericVnf();
206 final String cloudRegionUrl = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
207 invokeCloudRegionHttpPutEndPointAndAssertResponse(cloudRegionUrl);
209 final String tenantUrl =
210 getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME, TENANTS_TENANT + TENANT_ID);
211 addTenantAndAssertResponse(tenantUrl);
213 final String tenantRelationShipUrl = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME,
214 TENANTS_TENANT + TENANT_ID, RELATIONSHIP_LIST_RELATIONSHIP_URL);
216 final ResponseEntity<Void> tenantRelationShipResponse = testRestTemplateService
217 .invokeHttpPut(tenantRelationShipUrl, TestUtils.getGenericVnfRelatedLink(), Void.class);
218 assertEquals(HttpStatus.ACCEPTED, tenantRelationShipResponse.getStatusCode());
220 final Optional<Tenant> optional = cloudRegionCacheServiceProvider.getTenant(CLOUD_REGION_KEY, TENANT_ID);
222 assertTrue(optional.isPresent());
223 final Tenant actualTenant = optional.get();
224 final RelationshipList relationshipList = actualTenant.getRelationshipList();
225 assertNotNull(relationshipList);
226 assertFalse(relationshipList.getRelationship().isEmpty());
228 final Relationship relationship = relationshipList.getRelationship().get(0);
230 assertEquals(Constants.COMPOSED_OF, relationship.getRelationshipLabel());
231 assertFalse(relationship.getRelationshipData().isEmpty());
232 assertEquals(1, relationship.getRelationshipData().size());
234 final List<RelationshipData> relationshipDataList = relationship.getRelationshipData();
236 final RelationshipData relationshipData =
237 getRelationshipData(relationshipDataList, Constants.GENERIC_VNF_VNF_ID);
238 assertNotNull(relationshipData);
239 assertEquals(VNF_ID, relationshipData.getRelationshipValue());
241 final List<RelatedToProperty> relatedToPropertyList = relationship.getRelatedToProperty();
243 final RelatedToProperty property = getRelatedToProperty(relatedToPropertyList, Constants.GENERIC_VNF_VNF_NAME);
244 assertNotNull(property);
245 assertEquals(GENERIC_VNF_NAME, property.getPropertyValue());
247 final Optional<GenericVnf> genericVnfOptional = genericVnfCacheServiceProvider.getGenericVnf(VNF_ID);
248 assertTrue(genericVnfOptional.isPresent());
249 final GenericVnf actualGenericVnf = genericVnfOptional.get();
250 final RelationshipList relationshipListGenericVnf = actualGenericVnf.getRelationshipList();
251 assertNotNull(relationshipListGenericVnf);
252 assertFalse(relationshipListGenericVnf.getRelationship().isEmpty());
254 final Relationship relationshipGenericVnf = relationshipListGenericVnf.getRelationship().get(0);
256 assertEquals(Constants.BELONGS_TO, relationshipGenericVnf.getRelationshipLabel());
257 assertFalse(relationshipGenericVnf.getRelationshipData().isEmpty());
258 assertEquals(3, relationshipGenericVnf.getRelationshipData().size());
263 public void test_putEsrSystemInfo_successfullyAddedToCache() throws Exception {
264 final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
266 invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
268 final String esrSystemInfoListUrl =
269 getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME, ESR_SYSTEM_INFO_LIST_URL);
271 final String esrSystemInfoUrl = esrSystemInfoListUrl + "/esr-system-info/" + ESR_SYSTEM_INFO_ID;
272 final ResponseEntity<Void> esrSystemInfoResponse =
273 testRestTemplateService.invokeHttpPut(esrSystemInfoUrl, TestUtils.getEsrSystemInfo(), Void.class);
274 assertEquals(HttpStatus.ACCEPTED, esrSystemInfoResponse.getStatusCode());
276 final ResponseEntity<EsrSystemInfoList> response =
277 testRestTemplateService.invokeHttpGet(esrSystemInfoListUrl, EsrSystemInfoList.class);
278 assertEquals(HttpStatus.OK, response.getStatusCode());
280 assertTrue(response.hasBody());
281 final EsrSystemInfoList actualEsrSystemInfoList = response.getBody();
283 final List<EsrSystemInfo> esrSystemInfoList = actualEsrSystemInfoList.getEsrSystemInfo();
284 assertNotNull(esrSystemInfoList);
285 assertEquals(1, esrSystemInfoList.size());
287 final EsrSystemInfo esrSystemInfo = esrSystemInfoList.get(0);
288 assertEquals(ESR_SYSTEM_INFO_ID, esrSystemInfo.getEsrSystemInfoId());
289 assertEquals(SYSTEM_NAME, esrSystemInfo.getSystemName());
290 assertEquals(ESR_TYEP, esrSystemInfo.getType());
291 assertEquals(ESR_VENDOR, esrSystemInfo.getVendor());
292 assertEquals(ESR_SERVICE_URL, esrSystemInfo.getServiceUrl());
293 assertEquals(ESR_USERNAME, esrSystemInfo.getUserName());
294 assertEquals(ESR_PASSWORD, esrSystemInfo.getPassword());
295 assertEquals(ESR_SYSTEM_TYPE, esrSystemInfo.getSystemType());
299 public void test_putVServer_successfullyAddedToCache() throws Exception {
300 final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
302 invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
304 final String tenantUrl = url + TENANTS_TENANT + TENANT_ID;
305 addTenantAndAssertResponse(tenantUrl);
307 final String vServerUrl = tenantUrl + VSERVER_URL + VSERVER_ID;
309 final ResponseEntity<Void> vServerResponse =
310 testRestTemplateService.invokeHttpPut(vServerUrl, TestUtils.getVserver(), Void.class);
311 assertEquals(HttpStatus.ACCEPTED, vServerResponse.getStatusCode());
313 final ResponseEntity<Vserver> response = testRestTemplateService.invokeHttpGet(vServerUrl, Vserver.class);
314 assertEquals(HttpStatus.OK, response.getStatusCode());
316 assertTrue(response.hasBody());
317 final Vserver actualVserver = response.getBody();
318 assertEquals(VSERVER_NAME, actualVserver.getVserverName());
319 assertEquals(VSERVER_ID, actualVserver.getVserverId());
320 assertEquals("active", actualVserver.getProvStatus());
324 private void addTenantAndAssertResponse(final String tenantUrl) throws IOException {
325 final ResponseEntity<Void> responseEntity =
326 testRestTemplateService.invokeHttpPut(tenantUrl, TestUtils.getTenant(), Void.class);
327 assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
330 private void addCustomerServiceAndGenericVnf() throws Exception, IOException {
331 final ResponseEntity<Void> customerResponse =
332 testRestTemplateService.invokeHttpPut(getUrl(CUSTOMERS_URL), TestUtils.getCustomer(), Void.class);
333 assertEquals(HttpStatus.ACCEPTED, customerResponse.getStatusCode());
335 final String serviceInstanceUrl = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL);
336 final ResponseEntity<Void> serviceInstanceResponse =
337 testRestTemplateService.invokeHttpPut(serviceInstanceUrl, TestUtils.getServiceInstance(), Void.class);
338 assertEquals(HttpStatus.ACCEPTED, serviceInstanceResponse.getStatusCode());
340 final String genericVnfUrl = getUrl(GENERIC_VNF_URL, VNF_ID);
341 final ResponseEntity<Void> genericVnfResponse =
342 testRestTemplateService.invokeHttpPut(genericVnfUrl, TestUtils.getGenericVnf(), Void.class);
343 assertEquals(HttpStatus.ACCEPTED, genericVnfResponse.getStatusCode());
347 private void invokeCloudRegionHttpPutEndPointAndAssertResponse(final String url) throws IOException {
348 final ResponseEntity<Void> responseEntity =
349 testRestTemplateService.invokeHttpPut(url, TestUtils.getCloudRegion(), Void.class);
350 assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());