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.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;
67 * @author Waqas Ikram (waqas.ikram@est.tech)
70 @RunWith(SpringJUnit4ClassRunner.class)
71 @ActiveProfiles("test")
72 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
74 public class GenericVnfsControllerTest {
80 private TestRestTemplateService testRestTemplateService;
83 private CustomerCacheServiceProvider customerCacheServiceProvider;
86 private GenericVnfCacheServiceProvider genericVnfCacheServiceProvider;
89 private PlatformCacheServiceProvider platformVnfCacheServiceProvider;
93 customerCacheServiceProvider.clearAll();
94 genericVnfCacheServiceProvider.clearAll();
95 platformVnfCacheServiceProvider.clearAll();
99 public void test_putGenericVnf_successfullyAddedToCache() throws Exception {
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());
106 final ResponseEntity<GenericVnf> response =
107 testRestTemplateService.invokeHttpGet(genericVnfUrl, GenericVnf.class);
108 assertEquals(HttpStatus.OK, response.getStatusCode());
110 assertTrue(response.hasBody());
112 final GenericVnf actualGenericVnf = response.getBody();
113 assertEquals(GENERIC_VNF_NAME, actualGenericVnf.getVnfName());
114 assertEquals(VNF_ID, actualGenericVnf.getVnfId());
119 public void test_putGenericVnfRelation_successfullyAddedToCache() throws Exception {
121 addCustomerServiceAndGenericVnf();
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);
127 assertEquals(HttpStatus.ACCEPTED, genericVnfRelationShipResponse.getStatusCode());
130 final Optional<ServiceInstance> optional =
131 customerCacheServiceProvider.getServiceInstance(GLOBAL_CUSTOMER_ID, SERVICE_TYPE, SERVICE_INSTANCE_ID);
133 assertTrue(optional.isPresent());
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);
141 assertEquals(Constants.COMPOSED_OF, actualRelationShip.getRelationshipLabel());
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);
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());
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());
160 final Relationship relationship = relationshipList.getRelationship().get(0);
161 assertFalse(relationship.getRelatedToProperty().isEmpty());
162 assertEquals(3, relationship.getRelationshipData().size());
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());
169 final List<RelationshipData> relationshipData = relationship.getRelationshipData();
171 final RelationshipData globalRelationshipData =
172 getRelationshipData(relationshipData, Constants.CUSTOMER_GLOBAL_CUSTOMER_ID);
173 assertNotNull(globalRelationshipData);
174 assertEquals(GLOBAL_CUSTOMER_ID, globalRelationshipData.getRelationshipValue());
176 final RelationshipData serviceSubscriptionRelationshipData =
177 getRelationshipData(relationshipData, Constants.SERVICE_SUBSCRIPTION_SERVICE_TYPE);
178 assertNotNull(serviceSubscriptionRelationshipData);
179 assertEquals(SERVICE_TYPE, serviceSubscriptionRelationshipData.getRelationshipValue());
181 final RelationshipData serviceInstanceRelationshipData =
182 getRelationshipData(relationshipData, Constants.SERVICE_INSTANCE_SERVICE_INSTANCE_ID);
183 assertNotNull(serviceInstanceRelationshipData);
184 assertEquals(SERVICE_INSTANCE_ID, serviceInstanceRelationshipData.getRelationshipValue());
189 public void test_putGenericVnfRelationToPlatform_successfullyAddedToCache() throws Exception {
190 addCustomerServiceAndGenericVnf();
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());
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);
201 assertEquals(HttpStatus.ACCEPTED, genericVnfRelationShipResponse.getStatusCode());
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());
210 final Relationship relationship = relationshipList.getRelationship().get(0);
212 assertEquals(Constants.USES, relationship.getRelationshipLabel());
213 assertFalse(relationship.getRelationshipData().isEmpty());
214 assertEquals(1, relationship.getRelationshipData().size());
216 final List<RelationshipData> relationshipData = relationship.getRelationshipData();
218 final RelationshipData platformRelationshipData =
219 getRelationshipData(relationshipData, Constants.PLATFORM_PLATFORM_NAME);
220 assertNotNull(platformRelationshipData);
221 assertEquals(PLATFORM_NAME, platformRelationshipData.getRelationshipValue());
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());
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());
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());
242 private RelationshipData getRelationshipData(final List<RelationshipData> relationshipData, final String key) {
243 return relationshipData.stream().filter(data -> data.getRelationshipKey().equals(key)).findFirst().orElse(null);
247 private String getUrl(final String... urls) {
248 return TestUtils.getUrl(port, urls);