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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.so.aaisimulator.controller;
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;
71 * @author Waqas Ikram (waqas.ikram@est.tech)
74 @RunWith(SpringJUnit4ClassRunner.class)
75 @ActiveProfiles("test")
76 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
78 public class GenericVnfsControllerTest {
84 private TestRestTemplate restTemplate;
87 private UserCredentials userCredentials;
90 private CustomerCacheServiceProvider customerCacheServiceProvider;
93 private GenericVnfCacheServiceProvider genericVnfCacheServiceProvider;
96 private PlatformCacheServiceProvider platformVnfCacheServiceProvider;
100 customerCacheServiceProvider.clearAll();
101 genericVnfCacheServiceProvider.clearAll();
102 platformVnfCacheServiceProvider.clearAll();
106 public void test_putGenericVnf_successfullyAddedToCache() throws Exception {
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());
112 final ResponseEntity<GenericVnf> response = invokeHttpGet(genericVnfUrl, GenericVnf.class);
113 assertEquals(HttpStatus.OK, response.getStatusCode());
115 assertTrue(response.hasBody());
117 final GenericVnf actualGenericVnf = response.getBody();
118 assertEquals(GENERIC_VNF_NAME, actualGenericVnf.getVnfName());
119 assertEquals(VNF_ID, actualGenericVnf.getVnfId());
124 public void test_putGenericVnfRelation_successfullyAddedToCache() throws Exception {
126 addCustomerServiceAndGenericVnf();
128 final String genericVnfRelationShipUrl = getUrl(GENERIC_VNF_URL, VNF_ID, RELATIONSHIP_URL);
129 final ResponseEntity<Void> genericVnfRelationShipResponse =
130 invokeHttpPut(genericVnfRelationShipUrl, TestUtils.getRelationShip());
132 assertEquals(HttpStatus.ACCEPTED, genericVnfRelationShipResponse.getStatusCode());
135 final Optional<ServiceInstance> optional =
136 customerCacheServiceProvider.getServiceInstance(GLOBAL_CUSTOMER_ID, SERVICE_TYPE, SERVICE_INSTANCE_ID);
138 assertTrue(optional.isPresent());
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);
146 assertEquals(Constants.COMPOSED_OF, actualRelationShip.getRelationshipLabel());
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);
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());
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());
165 final Relationship relationship = relationshipList.getRelationship().get(0);
166 assertFalse(relationship.getRelatedToProperty().isEmpty());
167 assertEquals(3, relationship.getRelationshipData().size());
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());
174 final List<RelationshipData> relationshipData = relationship.getRelationshipData();
176 final RelationshipData globalRelationshipData =
177 getRelationshipData(relationshipData, Constants.CUSTOMER_GLOBAL_CUSTOMER_ID);
178 assertNotNull(globalRelationshipData);
179 assertEquals(GLOBAL_CUSTOMER_ID, globalRelationshipData.getRelationshipValue());
181 final RelationshipData serviceSubscriptionRelationshipData =
182 getRelationshipData(relationshipData, Constants.SERVICE_SUBSCRIPTION_SERVICE_TYPE);
183 assertNotNull(serviceSubscriptionRelationshipData);
184 assertEquals(SERVICE_TYPE, serviceSubscriptionRelationshipData.getRelationshipValue());
186 final RelationshipData serviceInstanceRelationshipData =
187 getRelationshipData(relationshipData, Constants.SERVICE_INSTANCE_SERVICE_INSTANCE_ID);
188 assertNotNull(serviceInstanceRelationshipData);
189 assertEquals(SERVICE_INSTANCE_ID, serviceInstanceRelationshipData.getRelationshipValue());
194 public void test_putGenericVnfRelationToPlatform_successfullyAddedToCache() throws Exception {
195 addCustomerServiceAndGenericVnf();
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());
201 final String genericVnfRelationShipUrl = getUrl(GENERIC_VNF_URL, VNF_ID, RELATIONSHIP_URL);
202 final ResponseEntity<Void> genericVnfRelationShipResponse =
203 invokeHttpPut(genericVnfRelationShipUrl, TestUtils.getPlatformRelatedLink());
205 assertEquals(HttpStatus.ACCEPTED, genericVnfRelationShipResponse.getStatusCode());
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());
214 final Relationship relationship = relationshipList.getRelationship().get(0);
216 assertEquals(Constants.USES, relationship.getRelationshipLabel());
217 assertFalse(relationship.getRelationshipData().isEmpty());
218 assertEquals(1, relationship.getRelationshipData().size());
220 final List<RelationshipData> relationshipData = relationship.getRelationshipData();
222 final RelationshipData platformRelationshipData =
223 getRelationshipData(relationshipData, Constants.PLATFORM_PLATFORM_NAME);
224 assertNotNull(platformRelationshipData);
225 assertEquals(PLATFORM_NAME, platformRelationshipData.getRelationshipValue());
229 private void addCustomerServiceAndGenericVnf() throws Exception, IOException {
230 final ResponseEntity<Void> customerResponse = invokeHttpPut(getUrl(CUSTOMERS_URL), TestUtils.getCustomer());
231 assertEquals(HttpStatus.ACCEPTED, customerResponse.getStatusCode());
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());
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());
244 private RelationshipData getRelationshipData(final List<RelationshipData> relationshipData, final String key) {
245 return relationshipData.stream().filter(data -> data.getRelationshipKey().equals(key)).findFirst().orElse(null);
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);
253 private <T> ResponseEntity<T> invokeHttpGet(final String url, final Class<T> clazz) {
254 return restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), clazz);
257 private HttpEntity<?> getHttpEntity(final Object obj) {
258 return new HttpEntity<>(obj, getHttpHeaders());
261 private HttpHeaders getHttpHeaders() {
262 return TestUtils.getHttpHeaders(userCredentials.getUsers().iterator().next().getUsername());
265 private String getUrl(final String... urls) {
266 return TestUtils.getUrl(port, urls);