Adding line of business relationship endpoint
[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.TestConstants.CUSTOMERS_URL;
27 import static org.onap.so.aaisimulator.utils.TestConstants.GENERIC_VNF_NAME;
28 import static org.onap.so.aaisimulator.utils.TestConstants.GENERIC_VNF_URL;
29 import static org.onap.so.aaisimulator.utils.TestConstants.GLOBAL_CUSTOMER_ID;
30 import static org.onap.so.aaisimulator.utils.TestConstants.LINE_OF_BUSINESS_NAME;
31 import static org.onap.so.aaisimulator.utils.TestConstants.PLATFORM_NAME;
32 import static org.onap.so.aaisimulator.utils.TestConstants.RELATIONSHIP_URL;
33 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_INSTANCE_ID;
34 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_INSTANCE_URL;
35 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_NAME;
36 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_SUBSCRIPTIONS_URL;
37 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_TYPE;
38 import static org.onap.so.aaisimulator.utils.TestConstants.VNF_ID;
39 import java.io.IOException;
40 import java.util.List;
41 import java.util.Optional;
42 import org.junit.After;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.onap.aai.domain.yang.GenericVnf;
46 import org.onap.aai.domain.yang.RelatedToProperty;
47 import org.onap.aai.domain.yang.Relationship;
48 import org.onap.aai.domain.yang.RelationshipData;
49 import org.onap.aai.domain.yang.RelationshipList;
50 import org.onap.aai.domain.yang.ServiceInstance;
51 import org.onap.so.aaisimulator.service.providers.CustomerCacheServiceProvider;
52 import org.onap.so.aaisimulator.service.providers.GenericVnfCacheServiceProvider;
53 import org.onap.so.aaisimulator.service.providers.LinesOfBusinessCacheServiceProvider;
54 import org.onap.so.aaisimulator.service.providers.PlatformCacheServiceProvider;
55 import org.onap.so.aaisimulator.utils.Constants;
56 import org.onap.so.aaisimulator.utils.TestRestTemplateService;
57 import org.onap.so.aaisimulator.utils.TestUtils;
58 import org.springframework.beans.factory.annotation.Autowired;
59 import org.springframework.boot.test.context.SpringBootTest;
60 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
61 import org.springframework.boot.web.server.LocalServerPort;
62 import org.springframework.context.annotation.Configuration;
63 import org.springframework.http.HttpStatus;
64 import org.springframework.http.ResponseEntity;
65 import org.springframework.test.context.ActiveProfiles;
66 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
67
68 /**
69  * @author Waqas Ikram (waqas.ikram@est.tech)
70  *
71  */
72 @RunWith(SpringJUnit4ClassRunner.class)
73 @ActiveProfiles("test")
74 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
75 @Configuration
76 public class GenericVnfsControllerTest {
77
78     @LocalServerPort
79     private int port;
80
81     @Autowired
82     private TestRestTemplateService testRestTemplateService;
83
84     @Autowired
85     private CustomerCacheServiceProvider customerCacheServiceProvider;
86
87     @Autowired
88     private GenericVnfCacheServiceProvider genericVnfCacheServiceProvider;
89
90     @Autowired
91     private LinesOfBusinessCacheServiceProvider linesOfBusinessCacheServiceProvider;
92
93     @Autowired
94     private PlatformCacheServiceProvider platformVnfCacheServiceProvider;
95
96     @After
97     public void after() {
98         customerCacheServiceProvider.clearAll();
99         genericVnfCacheServiceProvider.clearAll();
100         platformVnfCacheServiceProvider.clearAll();
101         linesOfBusinessCacheServiceProvider.clearAll();
102     }
103
104     @Test
105     public void test_putGenericVnf_successfullyAddedToCache() throws Exception {
106
107         final String genericVnfUrl = getUrl(GENERIC_VNF_URL, VNF_ID);
108         final ResponseEntity<Void> genericVnfResponse =
109                 testRestTemplateService.invokeHttpPut(genericVnfUrl, TestUtils.getGenericVnf(), Void.class);
110         assertEquals(HttpStatus.ACCEPTED, genericVnfResponse.getStatusCode());
111
112         final ResponseEntity<GenericVnf> response =
113                 testRestTemplateService.invokeHttpGet(genericVnfUrl, GenericVnf.class);
114         assertEquals(HttpStatus.OK, response.getStatusCode());
115
116         assertTrue(response.hasBody());
117
118         final GenericVnf actualGenericVnf = response.getBody();
119         assertEquals(GENERIC_VNF_NAME, actualGenericVnf.getVnfName());
120         assertEquals(VNF_ID, actualGenericVnf.getVnfId());
121
122     }
123
124     @Test
125     public void test_putGenericVnfRelation_successfullyAddedToCache() throws Exception {
126
127         addCustomerServiceAndGenericVnf();
128
129         final String genericVnfRelationShipUrl = getUrl(GENERIC_VNF_URL, VNF_ID, RELATIONSHIP_URL);
130         final ResponseEntity<Void> genericVnfRelationShipResponse = testRestTemplateService
131                 .invokeHttpPut(genericVnfRelationShipUrl, TestUtils.getRelationShip(), Void.class);
132
133         assertEquals(HttpStatus.ACCEPTED, genericVnfRelationShipResponse.getStatusCode());
134
135
136         final Optional<ServiceInstance> optional =
137                 customerCacheServiceProvider.getServiceInstance(GLOBAL_CUSTOMER_ID, SERVICE_TYPE, SERVICE_INSTANCE_ID);
138
139         assertTrue(optional.isPresent());
140
141         final ServiceInstance actualServiceInstance = optional.get();
142         final RelationshipList actualRelationshipList = actualServiceInstance.getRelationshipList();
143         assertNotNull(actualRelationshipList);
144         assertFalse(actualRelationshipList.getRelationship().isEmpty());
145         final Relationship actualRelationShip = actualRelationshipList.getRelationship().get(0);
146
147         assertEquals(Constants.COMPOSED_OF, actualRelationShip.getRelationshipLabel());
148
149         assertFalse(actualRelationShip.getRelatedToProperty().isEmpty());
150         assertFalse(actualRelationShip.getRelationshipData().isEmpty());
151         final RelatedToProperty actualRelatedToProperty = actualRelationShip.getRelatedToProperty().get(0);
152         final RelationshipData actualRelationshipData = actualRelationShip.getRelationshipData().get(0);
153
154         assertEquals(Constants.GENERIC_VNF_VNF_NAME, actualRelatedToProperty.getPropertyKey());
155         assertEquals(GENERIC_VNF_NAME, actualRelatedToProperty.getPropertyValue());
156         assertEquals(Constants.GENERIC_VNF_VNF_ID, actualRelationshipData.getRelationshipKey());
157         assertEquals(VNF_ID, actualRelationshipData.getRelationshipValue());
158
159         final Optional<GenericVnf> genericVnfOptional = genericVnfCacheServiceProvider.getGenericVnf(VNF_ID);
160         assertTrue(genericVnfOptional.isPresent());
161         final GenericVnf actualGenericVnf = genericVnfOptional.get();
162         final RelationshipList relationshipList = actualGenericVnf.getRelationshipList();
163         assertNotNull(relationshipList);
164         assertFalse(relationshipList.getRelationship().isEmpty());
165
166         final Relationship relationship = relationshipList.getRelationship().get(0);
167         assertFalse(relationship.getRelatedToProperty().isEmpty());
168         assertEquals(3, relationship.getRelationshipData().size());
169
170         final List<RelatedToProperty> relatedToProperty = relationship.getRelatedToProperty();
171         final RelatedToProperty firstRelatedToProperty = relatedToProperty.get(0);
172         assertEquals(Constants.SERVICE_INSTANCE_SERVICE_INSTANCE_NAME, firstRelatedToProperty.getPropertyKey());
173         assertEquals(SERVICE_NAME, firstRelatedToProperty.getPropertyValue());
174
175         final List<RelationshipData> relationshipData = relationship.getRelationshipData();
176
177         final RelationshipData globalRelationshipData =
178                 getRelationshipData(relationshipData, Constants.CUSTOMER_GLOBAL_CUSTOMER_ID);
179         assertNotNull(globalRelationshipData);
180         assertEquals(GLOBAL_CUSTOMER_ID, globalRelationshipData.getRelationshipValue());
181
182         final RelationshipData serviceSubscriptionRelationshipData =
183                 getRelationshipData(relationshipData, Constants.SERVICE_SUBSCRIPTION_SERVICE_TYPE);
184         assertNotNull(serviceSubscriptionRelationshipData);
185         assertEquals(SERVICE_TYPE, serviceSubscriptionRelationshipData.getRelationshipValue());
186
187         final RelationshipData serviceInstanceRelationshipData =
188                 getRelationshipData(relationshipData, Constants.SERVICE_INSTANCE_SERVICE_INSTANCE_ID);
189         assertNotNull(serviceInstanceRelationshipData);
190         assertEquals(SERVICE_INSTANCE_ID, serviceInstanceRelationshipData.getRelationshipValue());
191
192     }
193
194     @Test
195     public void test_putGenericVnfRelationToPlatform_successfullyAddedToCache() throws Exception {
196         addCustomerServiceAndGenericVnf();
197
198         final String platformUrl = getUrl(Constants.PLATFORMS_URL, PLATFORM_NAME);
199         final ResponseEntity<Void> platformResponse =
200                 testRestTemplateService.invokeHttpPut(platformUrl, TestUtils.getPlatform(), Void.class);
201         assertEquals(HttpStatus.ACCEPTED, platformResponse.getStatusCode());
202
203         final String genericVnfRelationShipUrl = getUrl(GENERIC_VNF_URL, VNF_ID, RELATIONSHIP_URL);
204         final ResponseEntity<Void> genericVnfRelationShipResponse = testRestTemplateService
205                 .invokeHttpPut(genericVnfRelationShipUrl, TestUtils.getPlatformRelatedLink(), Void.class);
206
207         assertEquals(HttpStatus.ACCEPTED, genericVnfRelationShipResponse.getStatusCode());
208
209         final Optional<GenericVnf> genericVnfOptional = genericVnfCacheServiceProvider.getGenericVnf(VNF_ID);
210         assertTrue(genericVnfOptional.isPresent());
211         final GenericVnf actualGenericVnf = genericVnfOptional.get();
212         final RelationshipList relationshipList = actualGenericVnf.getRelationshipList();
213         assertNotNull(relationshipList);
214         assertFalse(relationshipList.getRelationship().isEmpty());
215
216         final Relationship relationship = relationshipList.getRelationship().get(0);
217
218         assertEquals(Constants.USES, relationship.getRelationshipLabel());
219         assertFalse(relationship.getRelationshipData().isEmpty());
220         assertEquals(1, relationship.getRelationshipData().size());
221
222         final List<RelationshipData> relationshipData = relationship.getRelationshipData();
223
224         final RelationshipData platformRelationshipData =
225                 getRelationshipData(relationshipData, Constants.PLATFORM_PLATFORM_NAME);
226         assertNotNull(platformRelationshipData);
227         assertEquals(PLATFORM_NAME, platformRelationshipData.getRelationshipValue());
228
229     }
230
231     @Test
232     public void test_putGenericVnfRelationToLineOfBusiness_successfullyAddedToCache() throws Exception {
233         addCustomerServiceAndGenericVnf();
234
235         final String url = getUrl(Constants.LINES_OF_BUSINESS_URL, LINE_OF_BUSINESS_NAME);
236         final ResponseEntity<Void> responseEntity =
237                 testRestTemplateService.invokeHttpPut(url, TestUtils.getLineOfBusiness(), Void.class);
238         assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
239
240         final String genericVnfRelationShipUrl = getUrl(GENERIC_VNF_URL, VNF_ID, RELATIONSHIP_URL);
241         final ResponseEntity<Void> genericVnfRelationShipResponse = testRestTemplateService
242                 .invokeHttpPut(genericVnfRelationShipUrl, TestUtils.getLineOfBusinessRelatedLink(), Void.class);
243
244         assertEquals(HttpStatus.ACCEPTED, genericVnfRelationShipResponse.getStatusCode());
245
246         final Optional<GenericVnf> genericVnfOptional = genericVnfCacheServiceProvider.getGenericVnf(VNF_ID);
247         assertTrue(genericVnfOptional.isPresent());
248         final GenericVnf actualGenericVnf = genericVnfOptional.get();
249         final RelationshipList relationshipList = actualGenericVnf.getRelationshipList();
250         assertNotNull(relationshipList);
251         assertFalse(relationshipList.getRelationship().isEmpty());
252
253         final Relationship relationship = relationshipList.getRelationship().get(0);
254
255         assertEquals(Constants.USES, relationship.getRelationshipLabel());
256         assertFalse(relationship.getRelationshipData().isEmpty());
257         assertEquals(1, relationship.getRelationshipData().size());
258
259         final List<RelationshipData> relationshipData = relationship.getRelationshipData();
260
261         final RelationshipData lineOfBusinessRelationshipData =
262                 getRelationshipData(relationshipData, Constants.LINE_OF_BUSINESS_LINE_OF_BUSINESS_NAME);
263         assertNotNull(lineOfBusinessRelationshipData);
264         assertEquals(LINE_OF_BUSINESS_NAME, lineOfBusinessRelationshipData.getRelationshipValue());
265
266     }
267
268     private void addCustomerServiceAndGenericVnf() throws Exception, IOException {
269         final ResponseEntity<Void> customerResponse =
270                 testRestTemplateService.invokeHttpPut(getUrl(CUSTOMERS_URL), TestUtils.getCustomer(), Void.class);
271         assertEquals(HttpStatus.ACCEPTED, customerResponse.getStatusCode());
272
273         final String serviceInstanceUrl = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL);
274         final ResponseEntity<Void> serviceInstanceResponse =
275                 testRestTemplateService.invokeHttpPut(serviceInstanceUrl, TestUtils.getServiceInstance(), Void.class);
276         assertEquals(HttpStatus.ACCEPTED, serviceInstanceResponse.getStatusCode());
277
278         final String genericVnfUrl = getUrl(GENERIC_VNF_URL, VNF_ID);
279         final ResponseEntity<Void> genericVnfResponse =
280                 testRestTemplateService.invokeHttpPut(genericVnfUrl, TestUtils.getGenericVnf(), Void.class);
281         assertEquals(HttpStatus.ACCEPTED, genericVnfResponse.getStatusCode());
282
283     }
284
285     private RelationshipData getRelationshipData(final List<RelationshipData> relationshipData, final String key) {
286         return relationshipData.stream().filter(data -> data.getRelationshipKey().equals(key)).findFirst().orElse(null);
287     }
288
289     private String getUrl(final String... urls) {
290         return TestUtils.getUrl(port, urls);
291     }
292
293 }