Fixing vServer relationship endpoint
[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.Constants.VSERVER;
29 import static org.onap.so.aaisimulator.utils.TestConstants.CLOUD_OWNER_NAME;
30 import static org.onap.so.aaisimulator.utils.TestConstants.CLOUD_REGION_NAME;
31 import static org.onap.so.aaisimulator.utils.TestConstants.CUSTOMERS_URL;
32 import static org.onap.so.aaisimulator.utils.TestConstants.ESR_PASSWORD;
33 import static org.onap.so.aaisimulator.utils.TestConstants.ESR_SERVICE_URL;
34 import static org.onap.so.aaisimulator.utils.TestConstants.ESR_SYSTEM_INFO_ID;
35 import static org.onap.so.aaisimulator.utils.TestConstants.ESR_SYSTEM_INFO_LIST_URL;
36 import static org.onap.so.aaisimulator.utils.TestConstants.ESR_SYSTEM_TYPE;
37 import static org.onap.so.aaisimulator.utils.TestConstants.ESR_TYEP;
38 import static org.onap.so.aaisimulator.utils.TestConstants.ESR_USERNAME;
39 import static org.onap.so.aaisimulator.utils.TestConstants.ESR_VENDOR;
40 import static org.onap.so.aaisimulator.utils.TestConstants.GENERIC_VNF_NAME;
41 import static org.onap.so.aaisimulator.utils.TestConstants.GENERIC_VNF_URL;
42 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_INSTANCE_URL;
43 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_SUBSCRIPTIONS_URL;
44 import static org.onap.so.aaisimulator.utils.TestConstants.SYSTEM_NAME;
45 import static org.onap.so.aaisimulator.utils.TestConstants.TENANTS_TENANT;
46 import static org.onap.so.aaisimulator.utils.TestConstants.TENANT_ID;
47 import static org.onap.so.aaisimulator.utils.TestConstants.VNF_ID;
48 import static org.onap.so.aaisimulator.utils.TestConstants.VSERVER_ID;
49 import static org.onap.so.aaisimulator.utils.TestConstants.VSERVER_NAME;
50 import static org.onap.so.aaisimulator.utils.TestConstants.VSERVER_URL;
51 import java.io.IOException;
52 import java.util.List;
53 import java.util.Optional;
54 import org.junit.After;
55 import org.junit.Test;
56 import org.onap.aai.domain.yang.CloudRegion;
57 import org.onap.aai.domain.yang.EsrSystemInfo;
58 import org.onap.aai.domain.yang.EsrSystemInfoList;
59 import org.onap.aai.domain.yang.GenericVnf;
60 import org.onap.aai.domain.yang.RelatedToProperty;
61 import org.onap.aai.domain.yang.Relationship;
62 import org.onap.aai.domain.yang.RelationshipData;
63 import org.onap.aai.domain.yang.RelationshipList;
64 import org.onap.aai.domain.yang.Tenant;
65 import org.onap.aai.domain.yang.Vserver;
66 import org.onap.so.aaisimulator.models.CloudRegionKey;
67 import org.onap.so.aaisimulator.service.providers.CloudRegionCacheServiceProvider;
68 import org.onap.so.aaisimulator.service.providers.CustomerCacheServiceProvider;
69 import org.onap.so.aaisimulator.service.providers.GenericVnfCacheServiceProvider;
70 import org.onap.so.aaisimulator.utils.Constants;
71 import org.onap.so.aaisimulator.utils.TestConstants;
72 import org.onap.so.aaisimulator.utils.TestUtils;
73 import org.springframework.beans.factory.annotation.Autowired;
74 import org.springframework.http.HttpStatus;
75 import org.springframework.http.ResponseEntity;
76
77 /**
78  * @author Waqas Ikram (waqas.ikram@est.tech)
79  *
80  */
81 public class CloudRegionsControllerTest extends AbstractSpringBootTest {
82
83     private static final CloudRegionKey CLOUD_REGION_KEY = new CloudRegionKey(CLOUD_OWNER_NAME, CLOUD_REGION_NAME);
84
85     @Autowired
86     private CloudRegionCacheServiceProvider cloudRegionCacheServiceProvider;
87
88     @Autowired
89     private CustomerCacheServiceProvider customerCacheServiceProvider;
90
91     @Autowired
92     private GenericVnfCacheServiceProvider genericVnfCacheServiceProvider;
93
94     @After
95     public void after() {
96         cloudRegionCacheServiceProvider.clearAll();
97         customerCacheServiceProvider.clearAll();
98         genericVnfCacheServiceProvider.clearAll();
99     }
100
101     @Test
102     public void test_putCloudRegion_successfullyAddedToCache() throws Exception {
103         final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
104
105         invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
106
107         final ResponseEntity<CloudRegion> response = testRestTemplateService.invokeHttpGet(url, CloudRegion.class);
108         assertEquals(HttpStatus.OK, response.getStatusCode());
109
110         assertTrue(response.hasBody());
111
112         final CloudRegion cloudRegion = response.getBody();
113         assertEquals(CLOUD_OWNER_NAME, cloudRegion.getCloudOwner());
114         assertEquals(CLOUD_REGION_NAME, cloudRegion.getCloudRegionId());
115
116         assertNotNull("ResourceVersion should not be null", cloudRegion.getResourceVersion());
117
118     }
119
120     @Test
121     public void test_getCloudRegionWithDepthValue_shouldReturnMatchedCloudRegion() throws Exception {
122         final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
123
124         invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
125
126         final ResponseEntity<CloudRegion> response =
127                 testRestTemplateService.invokeHttpGet(url + "?depth=2", CloudRegion.class);
128         assertEquals(HttpStatus.OK, response.getStatusCode());
129
130         assertTrue(response.hasBody());
131
132         final CloudRegion cloudRegion = response.getBody();
133         assertEquals(CLOUD_OWNER_NAME, cloudRegion.getCloudOwner());
134         assertEquals(CLOUD_REGION_NAME, cloudRegion.getCloudRegionId());
135
136         assertNotNull("ResourceVersion should not be null", cloudRegion.getResourceVersion());
137
138     }
139
140     @Test
141     public void test_putGenericVnfRelationShipToPlatform_successfullyAddedToCache() throws Exception {
142
143         final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
144
145         invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
146
147         final String relationShipUrl = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME,
148                 BI_DIRECTIONAL_RELATIONSHIP_LIST_URL);
149
150         final ResponseEntity<Relationship> responseEntity = testRestTemplateService.invokeHttpPut(relationShipUrl,
151                 TestUtils.getGenericVnfRelationShip(), Relationship.class);
152         assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
153
154         final Optional<CloudRegion> optional = cloudRegionCacheServiceProvider.getCloudRegion(CLOUD_REGION_KEY);
155         assertTrue(optional.isPresent());
156
157         final CloudRegion actual = optional.get();
158
159         assertNotNull(actual.getRelationshipList());
160         final List<Relationship> relationshipList = actual.getRelationshipList().getRelationship();
161         assertFalse("Relationship list should not be empty", relationshipList.isEmpty());
162         final Relationship relationship = relationshipList.get(0);
163
164         assertEquals(GENERIC_VNF_URL + VNF_ID, relationship.getRelatedLink());
165
166         assertFalse("RelationshipData list should not be empty", relationship.getRelationshipData().isEmpty());
167         assertFalse("RelatedToProperty list should not be empty", relationship.getRelatedToProperty().isEmpty());
168
169         final RelationshipData relationshipData = relationship.getRelationshipData().get(0);
170         assertEquals(Constants.GENERIC_VNF_VNF_ID, relationshipData.getRelationshipKey());
171         assertEquals(TestConstants.VNF_ID, relationshipData.getRelationshipValue());
172
173         final RelatedToProperty relatedToProperty = relationship.getRelatedToProperty().get(0);
174         assertEquals(Constants.GENERIC_VNF_VNF_NAME, relatedToProperty.getPropertyKey());
175         assertEquals(TestConstants.GENERIC_VNF_NAME, relatedToProperty.getPropertyValue());
176
177     }
178
179     @Test
180     public void test_putTenant_successfullyAddedToCache() throws Exception {
181         final String cloudRegionUrl = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
182
183         invokeCloudRegionHttpPutEndPointAndAssertResponse(cloudRegionUrl);
184
185         final String tenantUrl =
186                 getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME + TENANTS_TENANT + TENANT_ID);
187         addTenantAndAssertResponse(tenantUrl);
188
189         final ResponseEntity<Tenant> response = testRestTemplateService.invokeHttpGet(tenantUrl, Tenant.class);
190         assertEquals(HttpStatus.OK, response.getStatusCode());
191
192         assertTrue(response.hasBody());
193
194         final Tenant tenant = response.getBody();
195         assertEquals(TENANT_ID, tenant.getTenantId());
196         assertEquals("admin", tenant.getTenantName());
197
198         assertNotNull("ResourceVersion should not be null", tenant.getResourceVersion());
199
200     }
201
202     @Test
203     public void test_putTenantRelationToGenericVnf_successfullyAddedToCache() throws Exception {
204
205         addCustomerServiceAndGenericVnf();
206
207         final String cloudRegionUrl = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
208         invokeCloudRegionHttpPutEndPointAndAssertResponse(cloudRegionUrl);
209
210         final String tenantUrl =
211                 getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME, TENANTS_TENANT + TENANT_ID);
212         addTenantAndAssertResponse(tenantUrl);
213
214         final String tenantRelationShipUrl = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME,
215                 TENANTS_TENANT + TENANT_ID, RELATIONSHIP_LIST_RELATIONSHIP_URL);
216
217         final ResponseEntity<Void> tenantRelationShipResponse = testRestTemplateService
218                 .invokeHttpPut(tenantRelationShipUrl, TestUtils.getGenericVnfRelatedLink(), Void.class);
219         assertEquals(HttpStatus.ACCEPTED, tenantRelationShipResponse.getStatusCode());
220
221         final Optional<Tenant> optional = cloudRegionCacheServiceProvider.getTenant(CLOUD_REGION_KEY, TENANT_ID);
222
223         assertTrue(optional.isPresent());
224         final Tenant actualTenant = optional.get();
225         final RelationshipList relationshipList = actualTenant.getRelationshipList();
226         assertNotNull(relationshipList);
227         assertFalse(relationshipList.getRelationship().isEmpty());
228
229         final Relationship relationship = relationshipList.getRelationship().get(0);
230
231         assertEquals(Constants.COMPOSED_OF, relationship.getRelationshipLabel());
232         assertFalse(relationship.getRelationshipData().isEmpty());
233         assertEquals(1, relationship.getRelationshipData().size());
234
235         final List<RelationshipData> relationshipDataList = relationship.getRelationshipData();
236
237         final RelationshipData relationshipData =
238                 getRelationshipData(relationshipDataList, Constants.GENERIC_VNF_VNF_ID);
239         assertNotNull(relationshipData);
240         assertEquals(VNF_ID, relationshipData.getRelationshipValue());
241
242         final List<RelatedToProperty> relatedToPropertyList = relationship.getRelatedToProperty();
243
244         final RelatedToProperty property = getRelatedToProperty(relatedToPropertyList, Constants.GENERIC_VNF_VNF_NAME);
245         assertNotNull(property);
246         assertEquals(GENERIC_VNF_NAME, property.getPropertyValue());
247
248         final Optional<GenericVnf> genericVnfOptional = genericVnfCacheServiceProvider.getGenericVnf(VNF_ID);
249         assertTrue(genericVnfOptional.isPresent());
250         final GenericVnf actualGenericVnf = genericVnfOptional.get();
251         final RelationshipList relationshipListGenericVnf = actualGenericVnf.getRelationshipList();
252         assertNotNull(relationshipListGenericVnf);
253         assertFalse(relationshipListGenericVnf.getRelationship().isEmpty());
254
255         final Relationship relationshipGenericVnf = relationshipListGenericVnf.getRelationship().get(0);
256
257         assertEquals(Constants.BELONGS_TO, relationshipGenericVnf.getRelationshipLabel());
258         assertFalse(relationshipGenericVnf.getRelationshipData().isEmpty());
259         assertEquals(3, relationshipGenericVnf.getRelationshipData().size());
260
261     }
262
263     @Test
264     public void test_putEsrSystemInfo_successfullyAddedToCache() throws Exception {
265         final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
266
267         invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
268
269         final String esrSystemInfoListUrl =
270                 getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME, ESR_SYSTEM_INFO_LIST_URL);
271
272         final String esrSystemInfoUrl = esrSystemInfoListUrl + "/esr-system-info/" + ESR_SYSTEM_INFO_ID;
273         final ResponseEntity<Void> esrSystemInfoResponse =
274                 testRestTemplateService.invokeHttpPut(esrSystemInfoUrl, TestUtils.getEsrSystemInfo(), Void.class);
275         assertEquals(HttpStatus.ACCEPTED, esrSystemInfoResponse.getStatusCode());
276
277         final ResponseEntity<EsrSystemInfoList> response =
278                 testRestTemplateService.invokeHttpGet(esrSystemInfoListUrl, EsrSystemInfoList.class);
279         assertEquals(HttpStatus.OK, response.getStatusCode());
280
281         assertTrue(response.hasBody());
282         final EsrSystemInfoList actualEsrSystemInfoList = response.getBody();
283
284         final List<EsrSystemInfo> esrSystemInfoList = actualEsrSystemInfoList.getEsrSystemInfo();
285         assertNotNull(esrSystemInfoList);
286         assertEquals(1, esrSystemInfoList.size());
287
288         final EsrSystemInfo esrSystemInfo = esrSystemInfoList.get(0);
289         assertEquals(ESR_SYSTEM_INFO_ID, esrSystemInfo.getEsrSystemInfoId());
290         assertEquals(SYSTEM_NAME, esrSystemInfo.getSystemName());
291         assertEquals(ESR_TYEP, esrSystemInfo.getType());
292         assertEquals(ESR_VENDOR, esrSystemInfo.getVendor());
293         assertEquals(ESR_SERVICE_URL, esrSystemInfo.getServiceUrl());
294         assertEquals(ESR_USERNAME, esrSystemInfo.getUserName());
295         assertEquals(ESR_PASSWORD, esrSystemInfo.getPassword());
296         assertEquals(ESR_SYSTEM_TYPE, esrSystemInfo.getSystemType());
297     }
298
299     @Test
300     public void test_putVServer_successfullyAddedToCache() throws Exception {
301         final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
302
303         invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
304         addCustomerServiceAndGenericVnf();
305
306         final String tenantUrl = url + TENANTS_TENANT + TENANT_ID;
307         addTenantAndAssertResponse(tenantUrl);
308
309         final String vServerUrl = tenantUrl + VSERVER_URL + VSERVER_ID;
310
311         final ResponseEntity<Void> vServerResponse =
312                 testRestTemplateService.invokeHttpPut(vServerUrl, TestUtils.getVserver(), Void.class);
313         assertEquals(HttpStatus.ACCEPTED, vServerResponse.getStatusCode());
314
315         final ResponseEntity<Vserver> response = testRestTemplateService.invokeHttpGet(vServerUrl, Vserver.class);
316         assertEquals(HttpStatus.OK, response.getStatusCode());
317
318         assertTrue(response.hasBody());
319         final Vserver actualVserver = response.getBody();
320         assertEquals(VSERVER_NAME, actualVserver.getVserverName());
321         assertEquals(VSERVER_ID, actualVserver.getVserverId());
322         assertEquals("active", actualVserver.getProvStatus());
323         assertNotNull(actualVserver.getRelationshipList());
324         assertFalse(actualVserver.getRelationshipList().getRelationship().isEmpty());
325
326         final Optional<GenericVnf> optional = genericVnfCacheServiceProvider.getGenericVnf(VNF_ID);
327         assertTrue(optional.isPresent());
328         final GenericVnf genericVnf = optional.get();
329         assertNotNull(genericVnf.getRelationshipList());
330         assertFalse(genericVnf.getRelationshipList().getRelationship().isEmpty());
331
332         final Relationship expectedRelationShip = genericVnf.getRelationshipList().getRelationship().get(0);
333         assertEquals(VSERVER, expectedRelationShip.getRelatedTo());
334         assertNotNull(expectedRelationShip.getRelationshipData());
335         assertEquals(4, expectedRelationShip.getRelationshipData().size());
336
337         final List<RelationshipData> relationshipDataList = expectedRelationShip.getRelationshipData();
338         final RelationshipData vServerrelationshipData =
339                 getRelationshipData(relationshipDataList, Constants.VSERVER_VSERVER_ID);
340         assertNotNull(vServerrelationshipData);
341         assertEquals(VSERVER_ID, vServerrelationshipData.getRelationshipValue());
342
343         final RelationshipData cloudOwnerRelationshipData =
344                 getRelationshipData(relationshipDataList, Constants.CLOUD_REGION_CLOUD_OWNER);
345         assertNotNull(cloudOwnerRelationshipData);
346         assertEquals(CLOUD_OWNER_NAME, cloudOwnerRelationshipData.getRelationshipValue());
347
348         final RelationshipData cloudRegionIdRelationshipData =
349                 getRelationshipData(relationshipDataList, Constants.CLOUD_REGION_CLOUD_REGION_ID);
350         assertNotNull(cloudRegionIdRelationshipData);
351         assertEquals(CLOUD_REGION_NAME, cloudRegionIdRelationshipData.getRelationshipValue());
352
353         final RelationshipData tenantRelationshipData =
354                 getRelationshipData(relationshipDataList, Constants.TENANT_TENANT_ID);
355         assertNotNull(tenantRelationshipData);
356         assertEquals(TENANT_ID, tenantRelationshipData.getRelationshipValue());
357
358     }
359
360     @Test
361     public void test_deleteVServer_successfullyRemoveFromCache() throws Exception {
362         final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
363
364         invokeCloudRegionHttpPutEndPointAndAssertResponse(url);
365         addCustomerServiceAndGenericVnf();
366
367         final String tenantUrl = url + TENANTS_TENANT + TENANT_ID;
368         addTenantAndAssertResponse(tenantUrl);
369
370         final String vServerAddUrl = tenantUrl + VSERVER_URL + VSERVER_ID;
371
372         final ResponseEntity<Void> vServerAddResponse =
373                 testRestTemplateService.invokeHttpPut(vServerAddUrl, TestUtils.getVserver(), Void.class);
374         assertEquals(HttpStatus.ACCEPTED, vServerAddResponse.getStatusCode());
375
376         final Optional<Vserver> optional =
377                 cloudRegionCacheServiceProvider.getVserver(CLOUD_REGION_KEY, TENANT_ID, VSERVER_ID);
378         assertTrue(optional.isPresent());
379         final Vserver vserver = optional.get();
380
381         final String vServerRemoveUrl = vServerAddUrl + "?resource-version=" + vserver.getResourceVersion();
382
383         final ResponseEntity<Void> responseEntity =
384                 testRestTemplateService.invokeHttpDelete(vServerRemoveUrl, Void.class);
385         assertEquals(HttpStatus.NO_CONTENT, responseEntity.getStatusCode());
386         assertFalse(cloudRegionCacheServiceProvider.getVserver(CLOUD_REGION_KEY, TENANT_ID, VSERVER_ID).isPresent());
387
388
389     }
390
391     private void addTenantAndAssertResponse(final String tenantUrl) throws IOException {
392         final ResponseEntity<Void> responseEntity =
393                 testRestTemplateService.invokeHttpPut(tenantUrl, TestUtils.getTenant(), Void.class);
394         assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
395     }
396
397     private void addCustomerServiceAndGenericVnf() throws Exception, IOException {
398         final ResponseEntity<Void> customerResponse =
399                 testRestTemplateService.invokeHttpPut(getUrl(CUSTOMERS_URL), TestUtils.getCustomer(), Void.class);
400         assertEquals(HttpStatus.ACCEPTED, customerResponse.getStatusCode());
401
402         final String serviceInstanceUrl = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL);
403         final ResponseEntity<Void> serviceInstanceResponse =
404                 testRestTemplateService.invokeHttpPut(serviceInstanceUrl, TestUtils.getServiceInstance(), Void.class);
405         assertEquals(HttpStatus.ACCEPTED, serviceInstanceResponse.getStatusCode());
406
407         final String genericVnfUrl = getUrl(GENERIC_VNF_URL, VNF_ID);
408         final ResponseEntity<Void> genericVnfResponse =
409                 testRestTemplateService.invokeHttpPut(genericVnfUrl, TestUtils.getGenericVnf(), Void.class);
410         assertEquals(HttpStatus.ACCEPTED, genericVnfResponse.getStatusCode());
411
412     }
413
414     private void invokeCloudRegionHttpPutEndPointAndAssertResponse(final String url) throws IOException {
415         final ResponseEntity<Void> responseEntity =
416                 testRestTemplateService.invokeHttpPut(url, TestUtils.getCloudRegion(), Void.class);
417         assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
418     }
419
420 }