abff7499baac70d432f9aa0d5afdcc7b06b19e21
[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.TestUtils;
55 import org.onap.so.simulator.model.UserCredentials;
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.test.web.client.TestRestTemplate;
60 import org.springframework.boot.web.server.LocalServerPort;
61 import org.springframework.context.annotation.Configuration;
62 import org.springframework.http.HttpEntity;
63 import org.springframework.http.HttpHeaders;
64 import org.springframework.http.HttpMethod;
65 import org.springframework.http.HttpStatus;
66 import org.springframework.http.ResponseEntity;
67 import org.springframework.test.context.ActiveProfiles;
68 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
69
70 /**
71  * @author Waqas Ikram (waqas.ikram@est.tech)
72  *
73  */
74 @RunWith(SpringJUnit4ClassRunner.class)
75 @ActiveProfiles("test")
76 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
77 @Configuration
78 public class GenericVnfsControllerTest {
79
80     @LocalServerPort
81     private int port;
82
83     @Autowired
84     private TestRestTemplate restTemplate;
85
86     @Autowired
87     private UserCredentials userCredentials;
88
89     @Autowired
90     private CustomerCacheServiceProvider customerCacheServiceProvider;
91
92     @Autowired
93     private GenericVnfCacheServiceProvider genericVnfCacheServiceProvider;
94
95     @Autowired
96     private PlatformCacheServiceProvider platformVnfCacheServiceProvider;
97
98     @After
99     public void after() {
100         customerCacheServiceProvider.clearAll();
101         genericVnfCacheServiceProvider.clearAll();
102         platformVnfCacheServiceProvider.clearAll();
103     }
104
105     @Test
106     public void test_putGenericVnf_successfullyAddedToCache() throws Exception {
107
108         final String genericVnfUrl = getUrl(GENERIC_VNF_URL, VNF_ID);
109         final ResponseEntity<Void> genericVnfResponse = invokeHttpPut(genericVnfUrl, TestUtils.getGenericVnf());
110         assertEquals(HttpStatus.ACCEPTED, genericVnfResponse.getStatusCode());
111
112         final ResponseEntity<GenericVnf> response = invokeHttpGet(genericVnfUrl, GenericVnf.class);
113         assertEquals(HttpStatus.OK, response.getStatusCode());
114
115         assertTrue(response.hasBody());
116
117         final GenericVnf actualGenericVnf = response.getBody();
118         assertEquals(GENERIC_VNF_NAME, actualGenericVnf.getVnfName());
119         assertEquals(VNF_ID, actualGenericVnf.getVnfId());
120
121     }
122
123     @Test
124     public void test_putGenericVnfRelation_successfullyAddedToCache() throws Exception {
125
126         addCustomerServiceAndGenericVnf();
127
128         final String genericVnfRelationShipUrl = getUrl(GENERIC_VNF_URL, VNF_ID, RELATIONSHIP_URL);
129         final ResponseEntity<Void> genericVnfRelationShipResponse =
130                 invokeHttpPut(genericVnfRelationShipUrl, TestUtils.getRelationShip());
131
132         assertEquals(HttpStatus.ACCEPTED, genericVnfRelationShipResponse.getStatusCode());
133
134
135         final Optional<ServiceInstance> optional =
136                 customerCacheServiceProvider.getServiceInstance(GLOBAL_CUSTOMER_ID, SERVICE_TYPE, SERVICE_INSTANCE_ID);
137
138         assertTrue(optional.isPresent());
139
140         final ServiceInstance actualServiceInstance = optional.get();
141         final RelationshipList actualRelationshipList = actualServiceInstance.getRelationshipList();
142         assertNotNull(actualRelationshipList);
143         assertFalse(actualRelationshipList.getRelationship().isEmpty());
144         final Relationship actualRelationShip = actualRelationshipList.getRelationship().get(0);
145
146         assertEquals(Constants.COMPOSED_OF, actualRelationShip.getRelationshipLabel());
147
148         assertFalse(actualRelationShip.getRelatedToProperty().isEmpty());
149         assertFalse(actualRelationShip.getRelationshipData().isEmpty());
150         final RelatedToProperty actualRelatedToProperty = actualRelationShip.getRelatedToProperty().get(0);
151         final RelationshipData actualRelationshipData = actualRelationShip.getRelationshipData().get(0);
152
153         assertEquals(Constants.GENERIC_VNF_VNF_NAME, actualRelatedToProperty.getPropertyKey());
154         assertEquals(GENERIC_VNF_NAME, actualRelatedToProperty.getPropertyValue());
155         assertEquals(Constants.GENERIC_VNF_VNF_ID, actualRelationshipData.getRelationshipKey());
156         assertEquals(VNF_ID, actualRelationshipData.getRelationshipValue());
157
158         final Optional<GenericVnf> genericVnfOptional = genericVnfCacheServiceProvider.getGenericVnf(VNF_ID);
159         assertTrue(genericVnfOptional.isPresent());
160         final GenericVnf actualGenericVnf = genericVnfOptional.get();
161         final RelationshipList relationshipList = actualGenericVnf.getRelationshipList();
162         assertNotNull(relationshipList);
163         assertFalse(relationshipList.getRelationship().isEmpty());
164
165         final Relationship relationship = relationshipList.getRelationship().get(0);
166         assertFalse(relationship.getRelatedToProperty().isEmpty());
167         assertEquals(3, relationship.getRelationshipData().size());
168
169         final List<RelatedToProperty> relatedToProperty = relationship.getRelatedToProperty();
170         final RelatedToProperty firstRelatedToProperty = relatedToProperty.get(0);
171         assertEquals(Constants.SERVICE_INSTANCE_SERVICE_INSTANCE_NAME, firstRelatedToProperty.getPropertyKey());
172         assertEquals(SERVICE_NAME, firstRelatedToProperty.getPropertyValue());
173
174         final List<RelationshipData> relationshipData = relationship.getRelationshipData();
175
176         final RelationshipData globalRelationshipData =
177                 getRelationshipData(relationshipData, Constants.CUSTOMER_GLOBAL_CUSTOMER_ID);
178         assertNotNull(globalRelationshipData);
179         assertEquals(GLOBAL_CUSTOMER_ID, globalRelationshipData.getRelationshipValue());
180
181         final RelationshipData serviceSubscriptionRelationshipData =
182                 getRelationshipData(relationshipData, Constants.SERVICE_SUBSCRIPTION_SERVICE_TYPE);
183         assertNotNull(serviceSubscriptionRelationshipData);
184         assertEquals(SERVICE_TYPE, serviceSubscriptionRelationshipData.getRelationshipValue());
185
186         final RelationshipData serviceInstanceRelationshipData =
187                 getRelationshipData(relationshipData, Constants.SERVICE_INSTANCE_SERVICE_INSTANCE_ID);
188         assertNotNull(serviceInstanceRelationshipData);
189         assertEquals(SERVICE_INSTANCE_ID, serviceInstanceRelationshipData.getRelationshipValue());
190
191     }
192
193     @Test
194     public void test_putGenericVnfRelationToPlatform_successfullyAddedToCache() throws Exception {
195         addCustomerServiceAndGenericVnf();
196
197         final String platformUrl = getUrl(Constants.PLATFORMS_URL, PLATFORM_NAME);
198         final ResponseEntity<Void> platformResponse = invokeHttpPut(platformUrl, TestUtils.getPlatform());
199         assertEquals(HttpStatus.ACCEPTED, platformResponse.getStatusCode());
200
201         final String genericVnfRelationShipUrl = getUrl(GENERIC_VNF_URL, VNF_ID, RELATIONSHIP_URL);
202         final ResponseEntity<Void> genericVnfRelationShipResponse =
203                 invokeHttpPut(genericVnfRelationShipUrl, TestUtils.getPlatformRelatedLink());
204
205         assertEquals(HttpStatus.ACCEPTED, genericVnfRelationShipResponse.getStatusCode());
206
207         final Optional<GenericVnf> genericVnfOptional = genericVnfCacheServiceProvider.getGenericVnf(VNF_ID);
208         assertTrue(genericVnfOptional.isPresent());
209         final GenericVnf actualGenericVnf = genericVnfOptional.get();
210         final RelationshipList relationshipList = actualGenericVnf.getRelationshipList();
211         assertNotNull(relationshipList);
212         assertFalse(relationshipList.getRelationship().isEmpty());
213
214         final Relationship relationship = relationshipList.getRelationship().get(0);
215
216         assertEquals(Constants.USES, relationship.getRelationshipLabel());
217         assertFalse(relationship.getRelationshipData().isEmpty());
218         assertEquals(1, relationship.getRelationshipData().size());
219
220         final List<RelationshipData> relationshipData = relationship.getRelationshipData();
221
222         final RelationshipData platformRelationshipData =
223                 getRelationshipData(relationshipData, Constants.PLATFORM_PLATFORM_NAME);
224         assertNotNull(platformRelationshipData);
225         assertEquals(PLATFORM_NAME, platformRelationshipData.getRelationshipValue());
226
227     }
228
229     private void addCustomerServiceAndGenericVnf() throws Exception, IOException {
230         final ResponseEntity<Void> customerResponse = invokeHttpPut(getUrl(CUSTOMERS_URL), TestUtils.getCustomer());
231         assertEquals(HttpStatus.ACCEPTED, customerResponse.getStatusCode());
232
233         final String serviceInstanceUrl = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL);
234         final ResponseEntity<Void> serviceInstanceResponse =
235                 invokeHttpPut(serviceInstanceUrl, TestUtils.getServiceInstance());
236         assertEquals(HttpStatus.ACCEPTED, serviceInstanceResponse.getStatusCode());
237
238         final String genericVnfUrl = getUrl(GENERIC_VNF_URL, VNF_ID);
239         final ResponseEntity<Void> genericVnfResponse = invokeHttpPut(genericVnfUrl, TestUtils.getGenericVnf());
240         assertEquals(HttpStatus.ACCEPTED, genericVnfResponse.getStatusCode());
241
242     }
243
244     private RelationshipData getRelationshipData(final List<RelationshipData> relationshipData, final String key) {
245         return relationshipData.stream().filter(data -> data.getRelationshipKey().equals(key)).findFirst().orElse(null);
246     }
247
248     private ResponseEntity<Void> invokeHttpPut(final String url, final Object obj) {
249         final HttpEntity<?> httpEntity = getHttpEntity(obj);
250         return restTemplate.exchange(url, HttpMethod.PUT, httpEntity, Void.class);
251     }
252
253     private <T> ResponseEntity<T> invokeHttpGet(final String url, final Class<T> clazz) {
254         return restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), clazz);
255     }
256
257     private HttpEntity<?> getHttpEntity(final Object obj) {
258         return new HttpEntity<>(obj, getHttpHeaders());
259     }
260
261     private HttpHeaders getHttpHeaders() {
262         return TestUtils.getHttpHeaders(userCredentials.getUsers().iterator().next().getUsername());
263     }
264
265     private String getUrl(final String... urls) {
266         return TestUtils.getUrl(port, urls);
267     }
268
269 }