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