Adding generic vnf and relationship 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.RELATIONSHIP_URL;
31 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_INSTANCE_ID;
32 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_INSTANCE_URL;
33 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_NAME;
34 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_SUBSCRIPTIONS_URL;
35 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_TYPE;
36 import static org.onap.so.aaisimulator.utils.TestConstants.VNF_ID;
37 import static org.onap.so.aaisimulator.utils.TestUtils.getJsonString;
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.utils.Constants;
53 import org.onap.so.aaisimulator.utils.TestUtils;
54 import org.onap.so.simulator.model.UserCredentials;
55 import org.springframework.beans.factory.annotation.Autowired;
56 import org.springframework.boot.test.context.SpringBootTest;
57 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
58 import org.springframework.boot.test.web.client.TestRestTemplate;
59 import org.springframework.boot.web.server.LocalServerPort;
60 import org.springframework.context.annotation.Configuration;
61 import org.springframework.http.HttpEntity;
62 import org.springframework.http.HttpHeaders;
63 import org.springframework.http.HttpMethod;
64 import org.springframework.http.HttpStatus;
65 import org.springframework.http.ResponseEntity;
66 import org.springframework.test.context.ActiveProfiles;
67 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
68 import org.springframework.web.util.UriComponentsBuilder;
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     @After
96     public void after() {
97         customerCacheServiceProvider.clearAll();
98         genericVnfCacheServiceProvider.clearAll();
99     }
100
101     @Test
102     public void test_putGenericVnf_successfullyAddedToCache() throws Exception {
103
104         final String genericVnfUrl = getUrl(GENERIC_VNF_URL, VNF_ID);
105         final ResponseEntity<Void> genericVnfResponse = invokeHttpPut(genericVnfUrl, getGenericVnf());
106         assertEquals(HttpStatus.ACCEPTED, genericVnfResponse.getStatusCode());
107
108         final ResponseEntity<GenericVnf> response = invokeHttpGet(genericVnfUrl, GenericVnf.class);
109         assertEquals(HttpStatus.OK, response.getStatusCode());
110
111         assertTrue(response.hasBody());
112
113         final GenericVnf actualGenericVnf = response.getBody();
114         assertEquals(GENERIC_VNF_NAME, actualGenericVnf.getVnfName());
115         assertEquals(VNF_ID, actualGenericVnf.getVnfId());
116
117     }
118
119     @Test
120     public void test_putGenericVnfRelation_successfullyAddedToCache() throws Exception {
121
122         final ResponseEntity<Void> customerResponse = invokeHttpPut(getUrl(CUSTOMERS_URL), getCustomer());
123         assertEquals(HttpStatus.ACCEPTED, customerResponse.getStatusCode());
124
125         final String serviceInstanceUrl = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL);
126         final ResponseEntity<Void> serviceInstanceResponse = invokeHttpPut(serviceInstanceUrl, getServiceInstance());
127         assertEquals(HttpStatus.ACCEPTED, serviceInstanceResponse.getStatusCode());
128
129         final String genericVnfUrl = getUrl(GENERIC_VNF_URL, VNF_ID);
130         final ResponseEntity<Void> genericVnfResponse = invokeHttpPut(genericVnfUrl, getGenericVnf());
131         assertEquals(HttpStatus.ACCEPTED, genericVnfResponse.getStatusCode());
132
133         final String genericVnfRelationShipUrl = getUrl(GENERIC_VNF_URL, VNF_ID, RELATIONSHIP_URL);
134         final ResponseEntity<Void> genericVnfRelationShipResponse =
135                 invokeHttpPut(genericVnfRelationShipUrl, getRelationShip());
136
137         assertEquals(HttpStatus.ACCEPTED, genericVnfRelationShipResponse.getStatusCode());
138
139
140         final Optional<ServiceInstance> optional =
141                 customerCacheServiceProvider.getServiceInstance(GLOBAL_CUSTOMER_ID, SERVICE_TYPE, SERVICE_INSTANCE_ID);
142
143         assertTrue(optional.isPresent());
144
145         final ServiceInstance actualServiceInstance = optional.get();
146         final RelationshipList actualRelationshipList = actualServiceInstance.getRelationshipList();
147         assertNotNull(actualRelationshipList);
148         assertFalse(actualRelationshipList.getRelationship().isEmpty());
149         final Relationship actualRelationShip = actualRelationshipList.getRelationship().get(0);
150
151         assertFalse(actualRelationShip.getRelatedToProperty().isEmpty());
152         assertFalse(actualRelationShip.getRelationshipData().isEmpty());
153         final RelatedToProperty actualRelatedToProperty = actualRelationShip.getRelatedToProperty().get(0);
154         final RelationshipData actualRelationshipData = actualRelationShip.getRelationshipData().get(0);
155
156         assertEquals(Constants.GENERIC_VNF_VNF_NAME, actualRelatedToProperty.getPropertyKey());
157         assertEquals(GENERIC_VNF_NAME, actualRelatedToProperty.getPropertyValue());
158         assertEquals(Constants.GENERIC_VNF_VNF_ID, actualRelationshipData.getRelationshipKey());
159         assertEquals(VNF_ID, actualRelationshipData.getRelationshipValue());
160
161         final Optional<GenericVnf> genericVnfOptional = genericVnfCacheServiceProvider.getGenericVnf(VNF_ID);
162         assertTrue(genericVnfOptional.isPresent());
163         final GenericVnf actualGenericVnf = genericVnfOptional.get();
164         final RelationshipList relationshipList = actualGenericVnf.getRelationshipList();
165         assertNotNull(relationshipList);
166         assertFalse(relationshipList.getRelationship().isEmpty());
167
168         final Relationship relationship = relationshipList.getRelationship().get(0);
169         assertFalse(relationship.getRelatedToProperty().isEmpty());
170         assertEquals(3, relationship.getRelationshipData().size());
171
172         final List<RelatedToProperty> relatedToProperty = relationship.getRelatedToProperty();
173         final RelatedToProperty firstRelatedToProperty = relatedToProperty.get(0);
174         assertEquals(Constants.SERVICE_INSTANCE_SERVICE_INSTANCE_NAME, firstRelatedToProperty.getPropertyKey());
175         assertEquals(SERVICE_NAME, firstRelatedToProperty.getPropertyValue());
176
177         final List<RelationshipData> relationshipData = relationship.getRelationshipData();
178
179         final RelationshipData globalRelationshipData =
180                 getRelationshipData(relationshipData, Constants.CUSTOMER_GLOBAL_CUSTOMER_ID);
181         assertNotNull(globalRelationshipData);
182         assertEquals(GLOBAL_CUSTOMER_ID, globalRelationshipData.getRelationshipValue());
183
184         final RelationshipData serviceSubscriptionRelationshipData =
185                 getRelationshipData(relationshipData, Constants.SERVICE_SUBSCRIPTION_SERVICE_TYPE);
186         assertNotNull(serviceSubscriptionRelationshipData);
187         assertEquals(SERVICE_TYPE, serviceSubscriptionRelationshipData.getRelationshipValue());
188
189         final RelationshipData serviceInstanceRelationshipData =
190                 getRelationshipData(relationshipData, Constants.SERVICE_INSTANCE_SERVICE_INSTANCE_ID);
191         assertNotNull(serviceInstanceRelationshipData);
192         assertEquals(SERVICE_INSTANCE_ID, serviceInstanceRelationshipData.getRelationshipValue());
193
194     }
195
196     private RelationshipData getRelationshipData(final List<RelationshipData> relationshipData, final String key) {
197         return relationshipData.stream().filter(data -> data.getRelationshipKey().equals(key)).findFirst().orElse(null);
198     }
199
200     private ResponseEntity<Void> invokeHttpPut(final String url, final Object obj) {
201         final HttpEntity<?> httpEntity = getHttpEntity(obj);
202         return restTemplate.exchange(url, HttpMethod.PUT, httpEntity, Void.class);
203     }
204
205     private <T> ResponseEntity<T> invokeHttpGet(final String url, final Class<T> clazz) {
206         return restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), clazz);
207     }
208
209     private HttpEntity<?> getHttpEntity(final Object obj) {
210         return new HttpEntity<>(obj, getHttpHeaders());
211     }
212
213     private HttpHeaders getHttpHeaders() {
214         return TestUtils.getHttpHeaders(userCredentials.getUsers().iterator().next().getUsername());
215     }
216
217     private String getUrl(final String... urls) {
218         final UriComponentsBuilder baseUri = UriComponentsBuilder.fromUriString("https://localhost:" + port);
219         for (final String url : urls) {
220             baseUri.path(url);
221
222         }
223         return baseUri.toUriString();
224     }
225
226     private String getCustomer() throws IOException {
227         return getJsonString("test-data/business-customer.json");
228     }
229
230     private String getServiceInstance() throws IOException {
231         return getJsonString("test-data/service-instance.json");
232     }
233
234     private String getGenericVnf() throws IOException {
235         return getJsonString("test-data/generic-vnf.json");
236     }
237
238     private String getRelationShip() throws IOException {
239         return getJsonString("test-data/relation-ship.json");
240     }
241
242 }