584e28cf3da962ad8200aaf2c63896869f06edc3
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / test / java / org / onap / so / aaisimulator / controller / CloudRegionsControllerTest.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.aaisimulator.controller;
21
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;
75
76 /**
77  * @author Waqas Ikram (waqas.ikram@est.tech)
78  *
79  */
80 public class CloudRegionsControllerTest extends AbstractSpringBootTest {
81
82     private static final CloudRegionKey CLOUD_REGION_KEY = new CloudRegionKey(CLOUD_OWNER_NAME, CLOUD_REGION_NAME);
83
84     @Autowired
85     private CloudRegionCacheServiceProvider cloudRegionCacheServiceProvider;
86
87     @Autowired
88     private CustomerCacheServiceProvider customerCacheServiceProvider;
89
90     @Autowired
91     private GenericVnfCacheServiceProvider genericVnfCacheServiceProvider;
92
93     @After
94     public void after() {
95         cloudRegionCacheServiceProvider.clearAll();
96         customerCacheServiceProvider.clearAll();
97         genericVnfCacheServiceProvider.clearAll();
98     }
99
100     @Test
101     public void test_putCloudRegion_successfullyAddedToCache() throws Exception {
102         final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
103
104         invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
105
106         final ResponseEntity<CloudRegion> response = testRestTemplateService.invokeHttpGet(url, CloudRegion.class);
107         assertEquals(HttpStatus.OK, response.getStatusCode());
108
109         assertTrue(response.hasBody());
110
111         final CloudRegion cloudRegion = response.getBody();
112         assertEquals(CLOUD_OWNER_NAME, cloudRegion.getCloudOwner());
113         assertEquals(CLOUD_REGION_NAME, cloudRegion.getCloudRegionId());
114
115         assertNotNull("ResourceVersion should not be null", cloudRegion.getResourceVersion());
116
117     }
118
119     @Test
120     public void test_getCloudRegionWithDepthValue_shouldReturnMatchedCloudRegion() throws Exception {
121         final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
122
123         invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
124
125         final ResponseEntity<CloudRegion> response =
126                 testRestTemplateService.invokeHttpGet(url + "?depth=2", CloudRegion.class);
127         assertEquals(HttpStatus.OK, response.getStatusCode());
128
129         assertTrue(response.hasBody());
130
131         final CloudRegion cloudRegion = response.getBody();
132         assertEquals(CLOUD_OWNER_NAME, cloudRegion.getCloudOwner());
133         assertEquals(CLOUD_REGION_NAME, cloudRegion.getCloudRegionId());
134
135         assertNotNull("ResourceVersion should not be null", cloudRegion.getResourceVersion());
136
137     }
138
139     @Test
140     public void test_putGenericVnfRelationShipToPlatform_successfullyAddedToCache() throws Exception {
141
142         final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
143
144         invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
145
146         final String relationShipUrl = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME,
147                 BI_DIRECTIONAL_RELATIONSHIP_LIST_URL);
148
149         final ResponseEntity<Relationship> responseEntity = testRestTemplateService.invokeHttpPut(relationShipUrl,
150                 TestUtils.getGenericVnfRelationShip(), Relationship.class);
151         assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
152
153         final Optional<CloudRegion> optional = cloudRegionCacheServiceProvider.getCloudRegion(CLOUD_REGION_KEY);
154         assertTrue(optional.isPresent());
155
156         final CloudRegion actual = optional.get();
157
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);
162
163         assertEquals(GENERIC_VNF_URL + VNF_ID, relationship.getRelatedLink());
164
165         assertFalse("RelationshipData list should not be empty", relationship.getRelationshipData().isEmpty());
166         assertFalse("RelatedToProperty list should not be empty", relationship.getRelatedToProperty().isEmpty());
167
168         final RelationshipData relationshipData = relationship.getRelationshipData().get(0);
169         assertEquals(Constants.GENERIC_VNF_VNF_ID, relationshipData.getRelationshipKey());
170         assertEquals(TestConstants.VNF_ID, relationshipData.getRelationshipValue());
171
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());
175
176     }
177
178     @Test
179     public void test_putTenant_successfullyAddedToCache() throws Exception {
180         final String cloudRegionUrl = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
181
182         invokeCloudRegionHttpPutEndPointAndAssertResponse(cloudRegionUrl);
183
184         final String tenantUrl =
185                 getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME + TENANTS_TENANT + TENANT_ID);
186         addTenantAndAssertResponse(tenantUrl);
187
188         final ResponseEntity<Tenant> response = testRestTemplateService.invokeHttpGet(tenantUrl, Tenant.class);
189         assertEquals(HttpStatus.OK, response.getStatusCode());
190
191         assertTrue(response.hasBody());
192
193         final Tenant tenant = response.getBody();
194         assertEquals(TENANT_ID, tenant.getTenantId());
195         assertEquals("admin", tenant.getTenantName());
196
197         assertNotNull("ResourceVersion should not be null", tenant.getResourceVersion());
198
199     }
200
201     @Test
202     public void test_putTenantRelationToGenericVnf_successfullyAddedToCache() throws Exception {
203
204         addCustomerServiceAndGenericVnf();
205
206         final String cloudRegionUrl = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
207         invokeCloudRegionHttpPutEndPointAndAssertResponse(cloudRegionUrl);
208
209         final String tenantUrl =
210                 getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME, TENANTS_TENANT + TENANT_ID);
211         addTenantAndAssertResponse(tenantUrl);
212
213         final String tenantRelationShipUrl = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME,
214                 TENANTS_TENANT + TENANT_ID, RELATIONSHIP_LIST_RELATIONSHIP_URL);
215
216         final ResponseEntity<Void> tenantRelationShipResponse = testRestTemplateService
217                 .invokeHttpPut(tenantRelationShipUrl, TestUtils.getGenericVnfRelatedLink(), Void.class);
218         assertEquals(HttpStatus.ACCEPTED, tenantRelationShipResponse.getStatusCode());
219
220         final Optional<Tenant> optional = cloudRegionCacheServiceProvider.getTenant(CLOUD_REGION_KEY, TENANT_ID);
221
222         assertTrue(optional.isPresent());
223         final Tenant actualTenant = optional.get();
224         final RelationshipList relationshipList = actualTenant.getRelationshipList();
225         assertNotNull(relationshipList);
226         assertFalse(relationshipList.getRelationship().isEmpty());
227
228         final Relationship relationship = relationshipList.getRelationship().get(0);
229
230         assertEquals(Constants.COMPOSED_OF, relationship.getRelationshipLabel());
231         assertFalse(relationship.getRelationshipData().isEmpty());
232         assertEquals(1, relationship.getRelationshipData().size());
233
234         final List<RelationshipData> relationshipDataList = relationship.getRelationshipData();
235
236         final RelationshipData relationshipData =
237                 getRelationshipData(relationshipDataList, Constants.GENERIC_VNF_VNF_ID);
238         assertNotNull(relationshipData);
239         assertEquals(VNF_ID, relationshipData.getRelationshipValue());
240
241         final List<RelatedToProperty> relatedToPropertyList = relationship.getRelatedToProperty();
242
243         final RelatedToProperty property = getRelatedToProperty(relatedToPropertyList, Constants.GENERIC_VNF_VNF_NAME);
244         assertNotNull(property);
245         assertEquals(GENERIC_VNF_NAME, property.getPropertyValue());
246
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());
253
254         final Relationship relationshipGenericVnf = relationshipListGenericVnf.getRelationship().get(0);
255
256         assertEquals(Constants.BELONGS_TO, relationshipGenericVnf.getRelationshipLabel());
257         assertFalse(relationshipGenericVnf.getRelationshipData().isEmpty());
258         assertEquals(3, relationshipGenericVnf.getRelationshipData().size());
259
260     }
261
262     @Test
263     public void test_putEsrSystemInfo_successfullyAddedToCache() throws Exception {
264         final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
265
266         invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
267
268         final String esrSystemInfoListUrl =
269                 getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME, ESR_SYSTEM_INFO_LIST_URL);
270
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());
275
276         final ResponseEntity<EsrSystemInfoList> response =
277                 testRestTemplateService.invokeHttpGet(esrSystemInfoListUrl, EsrSystemInfoList.class);
278         assertEquals(HttpStatus.OK, response.getStatusCode());
279
280         assertTrue(response.hasBody());
281         final EsrSystemInfoList actualEsrSystemInfoList = response.getBody();
282
283         final List<EsrSystemInfo> esrSystemInfoList = actualEsrSystemInfoList.getEsrSystemInfo();
284         assertNotNull(esrSystemInfoList);
285         assertEquals(1, esrSystemInfoList.size());
286
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());
296     }
297
298     @Test
299     public void test_putVServer_successfullyAddedToCache() throws Exception {
300         final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
301
302         invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
303
304         final String tenantUrl = url + TENANTS_TENANT + TENANT_ID;
305         addTenantAndAssertResponse(tenantUrl);
306
307         final String vServerUrl = tenantUrl + VSERVER_URL + VSERVER_ID;
308
309         final ResponseEntity<Void> vServerResponse =
310                 testRestTemplateService.invokeHttpPut(vServerUrl, TestUtils.getVserver(), Void.class);
311         assertEquals(HttpStatus.ACCEPTED, vServerResponse.getStatusCode());
312
313         final ResponseEntity<Vserver> response = testRestTemplateService.invokeHttpGet(vServerUrl, Vserver.class);
314         assertEquals(HttpStatus.OK, response.getStatusCode());
315
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());
321     }
322
323
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());
328     }
329
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());
334
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());
339
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());
344
345     }
346
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());
351     }
352
353 }