Adding tenant relationship and generic vnf post endpoints
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / test / java / org / onap / so / aaisimulator / controller / GenericVnfsControllerTest.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.X_HTTP_METHOD_OVERRIDE;
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.GENERIC_VNF_NAME;
33 import static org.onap.so.aaisimulator.utils.TestConstants.GENERIC_VNF_URL;
34 import static org.onap.so.aaisimulator.utils.TestConstants.GLOBAL_CUSTOMER_ID;
35 import static org.onap.so.aaisimulator.utils.TestConstants.LINE_OF_BUSINESS_NAME;
36 import static org.onap.so.aaisimulator.utils.TestConstants.PLATFORM_NAME;
37 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_INSTANCE_ID;
38 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_INSTANCE_URL;
39 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_NAME;
40 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_SUBSCRIPTIONS_URL;
41 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_TYPE;
42 import static org.onap.so.aaisimulator.utils.TestConstants.VNF_ID;
43 import java.io.IOException;
44 import java.util.List;
45 import java.util.Optional;
46 import org.junit.After;
47 import org.junit.Test;
48 import org.onap.aai.domain.yang.GenericVnf;
49 import org.onap.aai.domain.yang.RelatedToProperty;
50 import org.onap.aai.domain.yang.Relationship;
51 import org.onap.aai.domain.yang.RelationshipData;
52 import org.onap.aai.domain.yang.RelationshipList;
53 import org.onap.aai.domain.yang.ServiceInstance;
54 import org.onap.so.aaisimulator.service.providers.CustomerCacheServiceProvider;
55 import org.onap.so.aaisimulator.service.providers.GenericVnfCacheServiceProvider;
56 import org.onap.so.aaisimulator.service.providers.LinesOfBusinessCacheServiceProvider;
57 import org.onap.so.aaisimulator.service.providers.PlatformCacheServiceProvider;
58 import org.onap.so.aaisimulator.utils.Constants;
59 import org.onap.so.aaisimulator.utils.TestUtils;
60 import org.springframework.beans.factory.annotation.Autowired;
61 import org.springframework.http.HttpHeaders;
62 import org.springframework.http.HttpMethod;
63 import org.springframework.http.HttpStatus;
64 import org.springframework.http.ResponseEntity;
65
66 /**
67  * @author Waqas Ikram (waqas.ikram@est.tech)
68  *
69  */
70 public class GenericVnfsControllerTest extends AbstractSpringBootTest {
71
72     @Autowired
73     private CustomerCacheServiceProvider customerCacheServiceProvider;
74
75     @Autowired
76     private GenericVnfCacheServiceProvider genericVnfCacheServiceProvider;
77
78     @Autowired
79     private LinesOfBusinessCacheServiceProvider linesOfBusinessCacheServiceProvider;
80
81     @Autowired
82     private PlatformCacheServiceProvider platformVnfCacheServiceProvider;
83
84     @After
85     public void after() {
86         customerCacheServiceProvider.clearAll();
87         genericVnfCacheServiceProvider.clearAll();
88         platformVnfCacheServiceProvider.clearAll();
89         linesOfBusinessCacheServiceProvider.clearAll();
90     }
91
92     @Test
93     public void test_putGenericVnf_successfullyAddedToCache() throws Exception {
94
95         final String genericVnfUrl = getUrl(GENERIC_VNF_URL, VNF_ID);
96         final ResponseEntity<Void> genericVnfResponse =
97                 testRestTemplateService.invokeHttpPut(genericVnfUrl, TestUtils.getGenericVnf(), Void.class);
98         assertEquals(HttpStatus.ACCEPTED, genericVnfResponse.getStatusCode());
99
100         final ResponseEntity<GenericVnf> response =
101                 testRestTemplateService.invokeHttpGet(genericVnfUrl, GenericVnf.class);
102         assertEquals(HttpStatus.OK, response.getStatusCode());
103
104         assertTrue(response.hasBody());
105
106         final GenericVnf actualGenericVnf = response.getBody();
107         assertEquals(GENERIC_VNF_NAME, actualGenericVnf.getVnfName());
108         assertEquals(VNF_ID, actualGenericVnf.getVnfId());
109
110     }
111
112     @Test
113     public void test_putGenericVnfRelation_successfullyAddedToCache() throws Exception {
114
115         addCustomerServiceAndGenericVnf();
116
117         final String genericVnfRelationShipUrl = getUrl(GENERIC_VNF_URL, VNF_ID, RELATIONSHIP_LIST_RELATIONSHIP_URL);
118         final ResponseEntity<Void> genericVnfRelationShipResponse = testRestTemplateService
119                 .invokeHttpPut(genericVnfRelationShipUrl, TestUtils.getRelationShip(), Void.class);
120
121         assertEquals(HttpStatus.ACCEPTED, genericVnfRelationShipResponse.getStatusCode());
122
123
124         final Optional<ServiceInstance> optional =
125                 customerCacheServiceProvider.getServiceInstance(GLOBAL_CUSTOMER_ID, SERVICE_TYPE, SERVICE_INSTANCE_ID);
126
127         assertTrue(optional.isPresent());
128
129         final ServiceInstance actualServiceInstance = optional.get();
130         final RelationshipList actualRelationshipList = actualServiceInstance.getRelationshipList();
131         assertNotNull(actualRelationshipList);
132         assertFalse(actualRelationshipList.getRelationship().isEmpty());
133         final Relationship actualRelationShip = actualRelationshipList.getRelationship().get(0);
134
135         assertEquals(Constants.COMPOSED_OF, actualRelationShip.getRelationshipLabel());
136
137         assertFalse(actualRelationShip.getRelatedToProperty().isEmpty());
138         assertFalse(actualRelationShip.getRelationshipData().isEmpty());
139         final RelatedToProperty actualRelatedToProperty = actualRelationShip.getRelatedToProperty().get(0);
140         final RelationshipData actualRelationshipData = actualRelationShip.getRelationshipData().get(0);
141
142         assertEquals(Constants.GENERIC_VNF_VNF_NAME, actualRelatedToProperty.getPropertyKey());
143         assertEquals(GENERIC_VNF_NAME, actualRelatedToProperty.getPropertyValue());
144         assertEquals(Constants.GENERIC_VNF_VNF_ID, actualRelationshipData.getRelationshipKey());
145         assertEquals(VNF_ID, actualRelationshipData.getRelationshipValue());
146
147         final Optional<GenericVnf> genericVnfOptional = genericVnfCacheServiceProvider.getGenericVnf(VNF_ID);
148         assertTrue(genericVnfOptional.isPresent());
149         final GenericVnf actualGenericVnf = genericVnfOptional.get();
150         final RelationshipList relationshipList = actualGenericVnf.getRelationshipList();
151         assertNotNull(relationshipList);
152         assertFalse(relationshipList.getRelationship().isEmpty());
153
154         final Relationship relationship = relationshipList.getRelationship().get(0);
155         assertFalse(relationship.getRelatedToProperty().isEmpty());
156         assertEquals(3, relationship.getRelationshipData().size());
157
158         final List<RelatedToProperty> relatedToProperty = relationship.getRelatedToProperty();
159         final RelatedToProperty firstRelatedToProperty = relatedToProperty.get(0);
160         assertEquals(Constants.SERVICE_INSTANCE_SERVICE_INSTANCE_NAME, firstRelatedToProperty.getPropertyKey());
161         assertEquals(SERVICE_NAME, firstRelatedToProperty.getPropertyValue());
162
163         final List<RelationshipData> relationshipData = relationship.getRelationshipData();
164
165         final RelationshipData globalRelationshipData =
166                 getRelationshipData(relationshipData, Constants.CUSTOMER_GLOBAL_CUSTOMER_ID);
167         assertNotNull(globalRelationshipData);
168         assertEquals(GLOBAL_CUSTOMER_ID, globalRelationshipData.getRelationshipValue());
169
170         final RelationshipData serviceSubscriptionRelationshipData =
171                 getRelationshipData(relationshipData, Constants.SERVICE_SUBSCRIPTION_SERVICE_TYPE);
172         assertNotNull(serviceSubscriptionRelationshipData);
173         assertEquals(SERVICE_TYPE, serviceSubscriptionRelationshipData.getRelationshipValue());
174
175         final RelationshipData serviceInstanceRelationshipData =
176                 getRelationshipData(relationshipData, Constants.SERVICE_INSTANCE_SERVICE_INSTANCE_ID);
177         assertNotNull(serviceInstanceRelationshipData);
178         assertEquals(SERVICE_INSTANCE_ID, serviceInstanceRelationshipData.getRelationshipValue());
179
180     }
181
182     @Test
183     public void test_putGenericVnfRelationToPlatform_successfullyAddedToCache() throws Exception {
184         addCustomerServiceAndGenericVnf();
185
186         final String platformUrl = getUrl(Constants.PLATFORMS_URL, PLATFORM_NAME);
187         final ResponseEntity<Void> platformResponse =
188                 testRestTemplateService.invokeHttpPut(platformUrl, TestUtils.getPlatform(), Void.class);
189         assertEquals(HttpStatus.ACCEPTED, platformResponse.getStatusCode());
190
191         final String genericVnfRelationShipUrl = getUrl(GENERIC_VNF_URL, VNF_ID, RELATIONSHIP_LIST_RELATIONSHIP_URL);
192         final ResponseEntity<Void> genericVnfRelationShipResponse = testRestTemplateService
193                 .invokeHttpPut(genericVnfRelationShipUrl, TestUtils.getPlatformRelatedLink(), Void.class);
194
195         assertEquals(HttpStatus.ACCEPTED, genericVnfRelationShipResponse.getStatusCode());
196
197         final Optional<GenericVnf> genericVnfOptional = genericVnfCacheServiceProvider.getGenericVnf(VNF_ID);
198         assertTrue(genericVnfOptional.isPresent());
199         final GenericVnf actualGenericVnf = genericVnfOptional.get();
200         final RelationshipList relationshipList = actualGenericVnf.getRelationshipList();
201         assertNotNull(relationshipList);
202         assertFalse(relationshipList.getRelationship().isEmpty());
203
204         final Relationship relationship = relationshipList.getRelationship().get(0);
205
206         assertEquals(Constants.USES, relationship.getRelationshipLabel());
207         assertFalse(relationship.getRelationshipData().isEmpty());
208         assertEquals(1, relationship.getRelationshipData().size());
209
210         final List<RelationshipData> relationshipData = relationship.getRelationshipData();
211
212         final RelationshipData platformRelationshipData =
213                 getRelationshipData(relationshipData, Constants.PLATFORM_PLATFORM_NAME);
214         assertNotNull(platformRelationshipData);
215         assertEquals(PLATFORM_NAME, platformRelationshipData.getRelationshipValue());
216
217     }
218
219     @Test
220     public void test_putGenericVnfRelationToLineOfBusiness_successfullyAddedToCache() throws Exception {
221         addCustomerServiceAndGenericVnf();
222
223         final String url = getUrl(Constants.LINES_OF_BUSINESS_URL, LINE_OF_BUSINESS_NAME);
224         final ResponseEntity<Void> responseEntity =
225                 testRestTemplateService.invokeHttpPut(url, TestUtils.getLineOfBusiness(), Void.class);
226         assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
227
228         final String genericVnfRelationShipUrl = getUrl(GENERIC_VNF_URL, VNF_ID, RELATIONSHIP_LIST_RELATIONSHIP_URL);
229         final ResponseEntity<Void> genericVnfRelationShipResponse = testRestTemplateService
230                 .invokeHttpPut(genericVnfRelationShipUrl, TestUtils.getLineOfBusinessRelatedLink(), Void.class);
231
232         assertEquals(HttpStatus.ACCEPTED, genericVnfRelationShipResponse.getStatusCode());
233
234         final Optional<GenericVnf> genericVnfOptional = genericVnfCacheServiceProvider.getGenericVnf(VNF_ID);
235         assertTrue(genericVnfOptional.isPresent());
236         final GenericVnf actualGenericVnf = genericVnfOptional.get();
237         final RelationshipList relationshipList = actualGenericVnf.getRelationshipList();
238         assertNotNull(relationshipList);
239         assertFalse(relationshipList.getRelationship().isEmpty());
240
241         final Relationship relationship = relationshipList.getRelationship().get(0);
242
243         assertEquals(Constants.USES, relationship.getRelationshipLabel());
244         assertFalse(relationship.getRelationshipData().isEmpty());
245         assertEquals(1, relationship.getRelationshipData().size());
246
247         final List<RelationshipData> relationshipData = relationship.getRelationshipData();
248
249         final RelationshipData lineOfBusinessRelationshipData =
250                 getRelationshipData(relationshipData, Constants.LINE_OF_BUSINESS_LINE_OF_BUSINESS_NAME);
251         assertNotNull(lineOfBusinessRelationshipData);
252         assertEquals(LINE_OF_BUSINESS_NAME, lineOfBusinessRelationshipData.getRelationshipValue());
253
254     }
255
256     @Test
257     public void test_putGenericVnfRelationToCloudRegion_successfullyAddedToCache() throws Exception {
258         addCustomerServiceAndGenericVnf();
259
260         final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
261
262         final ResponseEntity<Void> responseEntity =
263                 testRestTemplateService.invokeHttpPut(url, TestUtils.getCloudRegion(), Void.class);
264         assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
265
266         final String genericVnfRelationShipUrl = getUrl(GENERIC_VNF_URL, VNF_ID, RELATIONSHIP_LIST_RELATIONSHIP_URL);
267         final ResponseEntity<Void> genericVnfRelationShipResponse = testRestTemplateService
268                 .invokeHttpPut(genericVnfRelationShipUrl, TestUtils.getCloudRegionRelatedLink(), Void.class);
269
270         assertEquals(HttpStatus.ACCEPTED, genericVnfRelationShipResponse.getStatusCode());
271
272         final Optional<GenericVnf> genericVnfOptional = genericVnfCacheServiceProvider.getGenericVnf(VNF_ID);
273         assertTrue(genericVnfOptional.isPresent());
274         final GenericVnf actualGenericVnf = genericVnfOptional.get();
275         final RelationshipList relationshipList = actualGenericVnf.getRelationshipList();
276         assertNotNull(relationshipList);
277         assertFalse(relationshipList.getRelationship().isEmpty());
278
279         final Relationship relationship = relationshipList.getRelationship().get(0);
280
281         assertEquals(Constants.LOCATED_IN, relationship.getRelationshipLabel());
282         assertFalse(relationship.getRelationshipData().isEmpty());
283         assertEquals(2, relationship.getRelationshipData().size());
284
285         final List<RelationshipData> relationshipDataList = relationship.getRelationshipData();
286
287         final RelationshipData cloudOwnerRelationshipData =
288                 getRelationshipData(relationshipDataList, Constants.CLOUD_REGION_CLOUD_OWNER);
289         assertNotNull(cloudOwnerRelationshipData);
290         assertEquals(CLOUD_OWNER_NAME, cloudOwnerRelationshipData.getRelationshipValue());
291
292         final RelationshipData cloudRegionIdRelationshipData =
293                 getRelationshipData(relationshipDataList, Constants.CLOUD_REGION_CLOUD_REGION_ID);
294         assertNotNull(cloudRegionIdRelationshipData);
295         assertEquals(CLOUD_REGION_NAME, cloudRegionIdRelationshipData.getRelationshipValue());
296
297         final List<RelatedToProperty> relatedToPropertyList = relationship.getRelatedToProperty();
298
299         final RelatedToProperty cloudRegionOwnerDefinedTypeProperty =
300                 getRelatedToProperty(relatedToPropertyList, Constants.CLOUD_REGION_OWNER_DEFINED_TYPE);
301         assertNotNull(cloudRegionOwnerDefinedTypeProperty);
302         assertEquals("OwnerType", cloudRegionOwnerDefinedTypeProperty.getPropertyValue());
303
304     }
305
306     @Test
307     public void test_putBiDirectionalRelationShip_successfullyAddedToCache() throws Exception {
308         addCustomerServiceAndGenericVnf();
309
310         final String relationShipUrl = getUrl(GENERIC_VNF_URL, VNF_ID, BI_DIRECTIONAL_RELATIONSHIP_LIST_URL);
311
312         final ResponseEntity<Relationship> responseEntity = testRestTemplateService.invokeHttpPut(relationShipUrl,
313                 TestUtils.getTenantRelationShip(), Relationship.class);
314         assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
315
316         final Optional<GenericVnf> optional = genericVnfCacheServiceProvider.getGenericVnf(VNF_ID);
317         assertTrue(optional.isPresent());
318
319         final GenericVnf actual = optional.get();
320
321         assertNotNull(actual.getRelationshipList());
322         final List<Relationship> relationshipList = actual.getRelationshipList().getRelationship();
323         assertFalse("Relationship list should not be empty", relationshipList.isEmpty());
324         final Relationship relationship = relationshipList.get(0);
325
326         assertFalse("RelationshipData list should not be empty", relationship.getRelationshipData().isEmpty());
327         assertFalse("RelatedToProperty list should not be empty", relationship.getRelatedToProperty().isEmpty());
328     }
329
330     @Test
331     public void test_patchGenericVnf_usingVnfId_OrchStatusChangedInCache() throws Exception {
332         addCustomerServiceAndGenericVnf();
333
334         final HttpHeaders httpHeaders = testRestTemplateService.getHttpHeaders();
335         httpHeaders.add(X_HTTP_METHOD_OVERRIDE, HttpMethod.PATCH.toString());
336
337         final String genericVnfUrl = getUrl(GENERIC_VNF_URL, VNF_ID);
338         final ResponseEntity<Void> orchStatuUpdateServiceInstanceResponse = testRestTemplateService
339                 .invokeHttpPost(httpHeaders, genericVnfUrl, TestUtils.getGenericVnfOrchStatuUpdate(), Void.class);
340
341         assertEquals(HttpStatus.ACCEPTED, orchStatuUpdateServiceInstanceResponse.getStatusCode());
342
343         final ResponseEntity<GenericVnf> response =
344                 testRestTemplateService.invokeHttpGet(genericVnfUrl, GenericVnf.class);
345         assertEquals(HttpStatus.OK, response.getStatusCode());
346
347         assertTrue(response.hasBody());
348
349         final GenericVnf actualGenericVnf = response.getBody();
350         assertEquals(GENERIC_VNF_NAME, actualGenericVnf.getVnfName());
351         assertEquals(VNF_ID, actualGenericVnf.getVnfId());
352         assertEquals("Assigned", actualGenericVnf.getOrchestrationStatus());
353
354     }
355
356     private void addCustomerServiceAndGenericVnf() throws Exception, IOException {
357         final ResponseEntity<Void> customerResponse =
358                 testRestTemplateService.invokeHttpPut(getUrl(CUSTOMERS_URL), TestUtils.getCustomer(), Void.class);
359         assertEquals(HttpStatus.ACCEPTED, customerResponse.getStatusCode());
360
361         final String serviceInstanceUrl = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL);
362         final ResponseEntity<Void> serviceInstanceResponse =
363                 testRestTemplateService.invokeHttpPut(serviceInstanceUrl, TestUtils.getServiceInstance(), Void.class);
364         assertEquals(HttpStatus.ACCEPTED, serviceInstanceResponse.getStatusCode());
365
366         final String genericVnfUrl = getUrl(GENERIC_VNF_URL, VNF_ID);
367         final ResponseEntity<Void> genericVnfResponse =
368                 testRestTemplateService.invokeHttpPut(genericVnfUrl, TestUtils.getGenericVnf(), Void.class);
369         assertEquals(HttpStatus.ACCEPTED, genericVnfResponse.getStatusCode());
370
371     }
372
373     private RelationshipData getRelationshipData(final List<RelationshipData> relationshipData, final String key) {
374         return relationshipData.stream().filter(data -> data.getRelationshipKey().equals(key)).findFirst().orElse(null);
375     }
376
377     private RelatedToProperty getRelatedToProperty(final List<RelatedToProperty> relatedToPropertyList,
378             final String key) {
379         return relatedToPropertyList.stream().filter(data -> data.getPropertyKey().equals(key)).findFirst()
380                 .orElse(null);
381     }
382
383 }