1acd1f237186f52d6046337e14a494b7e9ffbf74
[integration/csit.git] /
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         assertEquals(GENERIC_VNF_URL + VNF_ID, actualRelationShip.getRelatedLink());
137
138
139         assertFalse(actualRelationShip.getRelatedToProperty().isEmpty());
140         assertFalse(actualRelationShip.getRelationshipData().isEmpty());
141         final RelatedToProperty actualRelatedToProperty = actualRelationShip.getRelatedToProperty().get(0);
142         final RelationshipData actualRelationshipData = actualRelationShip.getRelationshipData().get(0);
143
144         assertEquals(Constants.GENERIC_VNF_VNF_NAME, actualRelatedToProperty.getPropertyKey());
145         assertEquals(GENERIC_VNF_NAME, actualRelatedToProperty.getPropertyValue());
146         assertEquals(Constants.GENERIC_VNF_VNF_ID, actualRelationshipData.getRelationshipKey());
147         assertEquals(VNF_ID, actualRelationshipData.getRelationshipValue());
148
149         final Optional<GenericVnf> genericVnfOptional = genericVnfCacheServiceProvider.getGenericVnf(VNF_ID);
150         assertTrue(genericVnfOptional.isPresent());
151         final GenericVnf actualGenericVnf = genericVnfOptional.get();
152         final RelationshipList relationshipList = actualGenericVnf.getRelationshipList();
153         assertNotNull(relationshipList);
154         assertFalse(relationshipList.getRelationship().isEmpty());
155
156         final Relationship relationship = relationshipList.getRelationship().get(0);
157         assertFalse(relationship.getRelatedToProperty().isEmpty());
158         assertEquals(3, relationship.getRelationshipData().size());
159         assertEquals(CUSTOMERS_URL + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL, relationship.getRelatedLink());
160
161
162         final List<RelatedToProperty> relatedToProperty = relationship.getRelatedToProperty();
163         final RelatedToProperty firstRelatedToProperty = relatedToProperty.get(0);
164         assertEquals(Constants.SERVICE_INSTANCE_SERVICE_INSTANCE_NAME, firstRelatedToProperty.getPropertyKey());
165         assertEquals(SERVICE_NAME, firstRelatedToProperty.getPropertyValue());
166
167         final List<RelationshipData> relationshipData = relationship.getRelationshipData();
168
169         final RelationshipData globalRelationshipData =
170                 getRelationshipData(relationshipData, Constants.CUSTOMER_GLOBAL_CUSTOMER_ID);
171         assertNotNull(globalRelationshipData);
172         assertEquals(GLOBAL_CUSTOMER_ID, globalRelationshipData.getRelationshipValue());
173
174         final RelationshipData serviceSubscriptionRelationshipData =
175                 getRelationshipData(relationshipData, Constants.SERVICE_SUBSCRIPTION_SERVICE_TYPE);
176         assertNotNull(serviceSubscriptionRelationshipData);
177         assertEquals(SERVICE_TYPE, serviceSubscriptionRelationshipData.getRelationshipValue());
178
179         final RelationshipData serviceInstanceRelationshipData =
180                 getRelationshipData(relationshipData, Constants.SERVICE_INSTANCE_SERVICE_INSTANCE_ID);
181         assertNotNull(serviceInstanceRelationshipData);
182         assertEquals(SERVICE_INSTANCE_ID, serviceInstanceRelationshipData.getRelationshipValue());
183
184     }
185
186     @Test
187     public void test_putGenericVnfRelationToPlatform_successfullyAddedToCache() throws Exception {
188         addCustomerServiceAndGenericVnf();
189
190         final String platformUrl = getUrl(Constants.PLATFORMS_URL, PLATFORM_NAME);
191         final ResponseEntity<Void> platformResponse =
192                 testRestTemplateService.invokeHttpPut(platformUrl, TestUtils.getPlatform(), Void.class);
193         assertEquals(HttpStatus.ACCEPTED, platformResponse.getStatusCode());
194
195         final String genericVnfRelationShipUrl = getUrl(GENERIC_VNF_URL, VNF_ID, RELATIONSHIP_LIST_RELATIONSHIP_URL);
196         final ResponseEntity<Void> genericVnfRelationShipResponse = testRestTemplateService
197                 .invokeHttpPut(genericVnfRelationShipUrl, TestUtils.getPlatformRelatedLink(), Void.class);
198
199         assertEquals(HttpStatus.ACCEPTED, genericVnfRelationShipResponse.getStatusCode());
200
201         final Optional<GenericVnf> genericVnfOptional = genericVnfCacheServiceProvider.getGenericVnf(VNF_ID);
202         assertTrue(genericVnfOptional.isPresent());
203         final GenericVnf actualGenericVnf = genericVnfOptional.get();
204         final RelationshipList relationshipList = actualGenericVnf.getRelationshipList();
205         assertNotNull(relationshipList);
206         assertFalse(relationshipList.getRelationship().isEmpty());
207
208         final Relationship relationship = relationshipList.getRelationship().get(0);
209
210         assertEquals(Constants.USES, relationship.getRelationshipLabel());
211         assertFalse(relationship.getRelationshipData().isEmpty());
212         assertEquals(1, relationship.getRelationshipData().size());
213         assertEquals(Constants.PLATFORMS_URL + PLATFORM_NAME, relationship.getRelatedLink());
214
215
216         final List<RelationshipData> relationshipData = relationship.getRelationshipData();
217
218         final RelationshipData platformRelationshipData =
219                 getRelationshipData(relationshipData, Constants.PLATFORM_PLATFORM_NAME);
220         assertNotNull(platformRelationshipData);
221         assertEquals(PLATFORM_NAME, platformRelationshipData.getRelationshipValue());
222
223     }
224
225     @Test
226     public void test_putGenericVnfRelationToLineOfBusiness_successfullyAddedToCache() throws Exception {
227         addCustomerServiceAndGenericVnf();
228
229         final String url = getUrl(Constants.LINES_OF_BUSINESS_URL, LINE_OF_BUSINESS_NAME);
230         final ResponseEntity<Void> responseEntity =
231                 testRestTemplateService.invokeHttpPut(url, TestUtils.getLineOfBusiness(), Void.class);
232         assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
233
234         final String genericVnfRelationShipUrl = getUrl(GENERIC_VNF_URL, VNF_ID, RELATIONSHIP_LIST_RELATIONSHIP_URL);
235         final ResponseEntity<Void> genericVnfRelationShipResponse = testRestTemplateService
236                 .invokeHttpPut(genericVnfRelationShipUrl, TestUtils.getLineOfBusinessRelatedLink(), Void.class);
237
238         assertEquals(HttpStatus.ACCEPTED, genericVnfRelationShipResponse.getStatusCode());
239
240         final Optional<GenericVnf> genericVnfOptional = genericVnfCacheServiceProvider.getGenericVnf(VNF_ID);
241         assertTrue(genericVnfOptional.isPresent());
242         final GenericVnf actualGenericVnf = genericVnfOptional.get();
243         final RelationshipList relationshipList = actualGenericVnf.getRelationshipList();
244         assertNotNull(relationshipList);
245         assertFalse(relationshipList.getRelationship().isEmpty());
246
247         final Relationship relationship = relationshipList.getRelationship().get(0);
248
249         assertEquals(Constants.USES, relationship.getRelationshipLabel());
250         assertEquals(Constants.LINES_OF_BUSINESS_URL + LINE_OF_BUSINESS_NAME, relationship.getRelatedLink());
251
252         assertFalse(relationship.getRelationshipData().isEmpty());
253         assertEquals(1, relationship.getRelationshipData().size());
254
255         final List<RelationshipData> relationshipData = relationship.getRelationshipData();
256
257         final RelationshipData lineOfBusinessRelationshipData =
258                 getRelationshipData(relationshipData, Constants.LINE_OF_BUSINESS_LINE_OF_BUSINESS_NAME);
259         assertNotNull(lineOfBusinessRelationshipData);
260         assertEquals(LINE_OF_BUSINESS_NAME, lineOfBusinessRelationshipData.getRelationshipValue());
261
262     }
263
264     @Test
265     public void test_putGenericVnfRelationToCloudRegion_successfullyAddedToCache() throws Exception {
266         addCustomerServiceAndGenericVnf();
267
268         final String url = getUrl(Constants.CLOUD_REGIONS, CLOUD_OWNER_NAME, "/" + CLOUD_REGION_NAME);
269
270         final ResponseEntity<Void> responseEntity =
271                 testRestTemplateService.invokeHttpPut(url, TestUtils.getCloudRegion(), Void.class);
272         assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
273
274         final String genericVnfRelationShipUrl = getUrl(GENERIC_VNF_URL, VNF_ID, RELATIONSHIP_LIST_RELATIONSHIP_URL);
275         final ResponseEntity<Void> genericVnfRelationShipResponse = testRestTemplateService
276                 .invokeHttpPut(genericVnfRelationShipUrl, TestUtils.getCloudRegionRelatedLink(), Void.class);
277
278         assertEquals(HttpStatus.ACCEPTED, genericVnfRelationShipResponse.getStatusCode());
279
280         final Optional<GenericVnf> genericVnfOptional = genericVnfCacheServiceProvider.getGenericVnf(VNF_ID);
281         assertTrue(genericVnfOptional.isPresent());
282         final GenericVnf actualGenericVnf = genericVnfOptional.get();
283         final RelationshipList relationshipList = actualGenericVnf.getRelationshipList();
284         assertNotNull(relationshipList);
285         assertFalse(relationshipList.getRelationship().isEmpty());
286
287         final Relationship relationship = relationshipList.getRelationship().get(0);
288
289         assertEquals(Constants.LOCATED_IN, relationship.getRelationshipLabel());
290         assertEquals(Constants.CLOUD_REGIONS + CLOUD_OWNER_NAME + "/" + CLOUD_REGION_NAME,
291                 relationship.getRelatedLink());
292
293         assertFalse(relationship.getRelationshipData().isEmpty());
294         assertEquals(2, relationship.getRelationshipData().size());
295
296         final List<RelationshipData> relationshipDataList = relationship.getRelationshipData();
297
298         final RelationshipData cloudOwnerRelationshipData =
299                 getRelationshipData(relationshipDataList, Constants.CLOUD_REGION_CLOUD_OWNER);
300         assertNotNull(cloudOwnerRelationshipData);
301         assertEquals(CLOUD_OWNER_NAME, cloudOwnerRelationshipData.getRelationshipValue());
302
303         final RelationshipData cloudRegionIdRelationshipData =
304                 getRelationshipData(relationshipDataList, Constants.CLOUD_REGION_CLOUD_REGION_ID);
305         assertNotNull(cloudRegionIdRelationshipData);
306         assertEquals(CLOUD_REGION_NAME, cloudRegionIdRelationshipData.getRelationshipValue());
307
308         final List<RelatedToProperty> relatedToPropertyList = relationship.getRelatedToProperty();
309
310         final RelatedToProperty cloudRegionOwnerDefinedTypeProperty =
311                 getRelatedToProperty(relatedToPropertyList, Constants.CLOUD_REGION_OWNER_DEFINED_TYPE);
312         assertNotNull(cloudRegionOwnerDefinedTypeProperty);
313         assertEquals("OwnerType", cloudRegionOwnerDefinedTypeProperty.getPropertyValue());
314
315     }
316
317     @Test
318     public void test_putBiDirectionalRelationShip_successfullyAddedToCache() throws Exception {
319         addCustomerServiceAndGenericVnf();
320
321         final String relationShipUrl = getUrl(GENERIC_VNF_URL, VNF_ID, BI_DIRECTIONAL_RELATIONSHIP_LIST_URL);
322
323         final ResponseEntity<Relationship> responseEntity = testRestTemplateService.invokeHttpPut(relationShipUrl,
324                 TestUtils.getTenantRelationShip(), Relationship.class);
325         assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
326
327         final Optional<GenericVnf> optional = genericVnfCacheServiceProvider.getGenericVnf(VNF_ID);
328         assertTrue(optional.isPresent());
329
330         final GenericVnf actual = optional.get();
331
332         assertNotNull(actual.getRelationshipList());
333         final List<Relationship> relationshipList = actual.getRelationshipList().getRelationship();
334         assertFalse("Relationship list should not be empty", relationshipList.isEmpty());
335         final Relationship relationship = relationshipList.get(0);
336
337         assertFalse("RelationshipData list should not be empty", relationship.getRelationshipData().isEmpty());
338         assertFalse("RelatedToProperty list should not be empty", relationship.getRelatedToProperty().isEmpty());
339     }
340
341     @Test
342     public void test_patchGenericVnf_usingVnfId_OrchStatusChangedInCache() throws Exception {
343         addCustomerServiceAndGenericVnf();
344
345         final HttpHeaders httpHeaders = testRestTemplateService.getHttpHeaders();
346         httpHeaders.add(X_HTTP_METHOD_OVERRIDE, HttpMethod.PATCH.toString());
347
348         final String genericVnfUrl = getUrl(GENERIC_VNF_URL, VNF_ID);
349         final ResponseEntity<Void> orchStatuUpdateServiceInstanceResponse = testRestTemplateService
350                 .invokeHttpPost(httpHeaders, genericVnfUrl, TestUtils.getGenericVnfOrchStatuUpdate(), Void.class);
351
352         assertEquals(HttpStatus.ACCEPTED, orchStatuUpdateServiceInstanceResponse.getStatusCode());
353
354         final ResponseEntity<GenericVnf> response =
355                 testRestTemplateService.invokeHttpGet(genericVnfUrl, GenericVnf.class);
356         assertEquals(HttpStatus.OK, response.getStatusCode());
357
358         assertTrue(response.hasBody());
359
360         final GenericVnf actualGenericVnf = response.getBody();
361         assertEquals(GENERIC_VNF_NAME, actualGenericVnf.getVnfName());
362         assertEquals(VNF_ID, actualGenericVnf.getVnfId());
363         assertEquals("Assigned", actualGenericVnf.getOrchestrationStatus());
364
365     }
366
367     private void addCustomerServiceAndGenericVnf() throws Exception, IOException {
368         final ResponseEntity<Void> customerResponse =
369                 testRestTemplateService.invokeHttpPut(getUrl(CUSTOMERS_URL), TestUtils.getCustomer(), Void.class);
370         assertEquals(HttpStatus.ACCEPTED, customerResponse.getStatusCode());
371
372         final String serviceInstanceUrl = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL);
373         final ResponseEntity<Void> serviceInstanceResponse =
374                 testRestTemplateService.invokeHttpPut(serviceInstanceUrl, TestUtils.getServiceInstance(), Void.class);
375         assertEquals(HttpStatus.ACCEPTED, serviceInstanceResponse.getStatusCode());
376
377         final String genericVnfUrl = getUrl(GENERIC_VNF_URL, VNF_ID);
378         final ResponseEntity<Void> genericVnfResponse =
379                 testRestTemplateService.invokeHttpPut(genericVnfUrl, TestUtils.getGenericVnf(), Void.class);
380         assertEquals(HttpStatus.ACCEPTED, genericVnfResponse.getStatusCode());
381
382     }
383
384
385 }