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.Constants.X_HTTP_METHOD_OVERRIDE;
27 import static org.onap.so.aaisimulator.utils.TestConstants.CUSTOMERS_URL;
28 import static org.onap.so.aaisimulator.utils.TestConstants.GENERIC_VNF_NAME;
29 import static org.onap.so.aaisimulator.utils.TestConstants.GENERIC_VNF_URL;
30 import static org.onap.so.aaisimulator.utils.TestConstants.GLOBAL_CUSTOMER_ID;
31 import static org.onap.so.aaisimulator.utils.TestConstants.RELATED_TO_URL;
32 import static org.onap.so.aaisimulator.utils.TestConstants.RELATIONSHIP_URL;
33 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_INSTANCES_URL;
34 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_INSTANCE_ID;
35 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_INSTANCE_URL;
36 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_NAME;
37 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_SUBSCRIPTIONS_URL;
38 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_TYPE;
39 import static org.onap.so.aaisimulator.utils.TestConstants.VNF_ID;
40 import static org.onap.so.aaisimulator.utils.TestUtils.getJsonString;
41 import java.io.IOException;
42 import java.util.Optional;
43 import java.util.UUID;
44 import org.junit.After;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.onap.aai.domain.yang.Customer;
48 import org.onap.aai.domain.yang.GenericVnf;
49 import org.onap.aai.domain.yang.GenericVnfs;
50 import org.onap.aai.domain.yang.Relationship;
51 import org.onap.aai.domain.yang.ServiceInstance;
52 import org.onap.aai.domain.yang.ServiceInstances;
53 import org.onap.aai.domain.yang.ServiceSubscription;
54 import org.onap.so.aaisimulator.service.providers.CustomerCacheServiceProvider;
55 import org.onap.so.aaisimulator.utils.RequestError;
56 import org.onap.so.aaisimulator.utils.RequestErrorResponseUtils;
57 import org.onap.so.aaisimulator.utils.ServiceException;
58 import org.onap.so.aaisimulator.utils.TestUtils;
59 import org.onap.so.simulator.model.UserCredentials;
60 import org.springframework.beans.factory.annotation.Autowired;
61 import org.springframework.boot.test.context.SpringBootTest;
62 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
63 import org.springframework.boot.test.web.client.TestRestTemplate;
64 import org.springframework.boot.web.server.LocalServerPort;
65 import org.springframework.context.annotation.Configuration;
66 import org.springframework.http.HttpEntity;
67 import org.springframework.http.HttpHeaders;
68 import org.springframework.http.HttpMethod;
69 import org.springframework.http.HttpStatus;
70 import org.springframework.http.ResponseEntity;
71 import org.springframework.test.context.ActiveProfiles;
72 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
75 * @author waqas.ikram@ericsson.com
78 @RunWith(SpringJUnit4ClassRunner.class)
79 @ActiveProfiles("test")
80 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
82 public class BusinessControllerTest {
84 private static final String FIREWALL_SERVICE_TTYPE = "Firewall";
86 private static final String ORCHESTRATION_STATUS = "Active";
92 private TestRestTemplate restTemplate;
95 private UserCredentials userCredentials;
98 private CustomerCacheServiceProvider cacheServiceProvider;
101 public void after() {
102 cacheServiceProvider.clearAll();
106 public void test_putCustomer_successfullyAddedToCache() throws Exception {
107 final ResponseEntity<Void> actual = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
109 assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
110 assertTrue(cacheServiceProvider.getCustomer(GLOBAL_CUSTOMER_ID).isPresent());
114 public void test_getCustomer_ableToRetrieveCustomer() throws Exception {
115 final String url = getCustomerEndPointUrl();
117 invokeHttpPut(url, getCustomer());
119 final ResponseEntity<Customer> actual =
120 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), Customer.class);
122 assertEquals(HttpStatus.OK, actual.getStatusCode());
123 assertTrue(actual.hasBody());
125 final Customer actualCustomer = actual.getBody();
126 assertEquals(GLOBAL_CUSTOMER_ID, actualCustomer.getGlobalCustomerId());
127 assertNotNull(actualCustomer.getResourceVersion());
128 assertFalse(actualCustomer.getResourceVersion().isEmpty());
132 public void test_getCustomer_returnRequestError_ifCustomerNotInCache() throws Exception {
133 final String url = getCustomerEndPointUrl();
135 final ResponseEntity<RequestError> actual =
136 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), RequestError.class);
138 assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
140 final RequestError actualError = actual.getBody();
141 final ServiceException serviceException = actualError.getServiceException();
143 assertNotNull(serviceException);
144 assertEquals(RequestErrorResponseUtils.ERROR_MESSAGE_ID, serviceException.getMessageId());
145 assertEquals(RequestErrorResponseUtils.ERROR_MESSAGE, serviceException.getText());
146 assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
151 public void test_getServiceSubscription_ableToRetrieveServiceSubscriptionFromCache() throws Exception {
152 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL;
154 invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
156 final ResponseEntity<ServiceSubscription> actual = restTemplate.exchange(url, HttpMethod.GET,
157 new HttpEntity<>(getHttpHeaders()), ServiceSubscription.class);
159 assertEquals(HttpStatus.OK, actual.getStatusCode());
160 assertTrue(actual.hasBody());
162 final ServiceSubscription actualServiceSubscription = actual.getBody();
163 assertEquals(SERVICE_TYPE, actualServiceSubscription.getServiceType());
164 assertNotNull(actualServiceSubscription.getRelationshipList());
165 assertFalse(actualServiceSubscription.getRelationshipList().getRelationship().isEmpty());
169 public void test_putSericeInstance_ableToRetrieveServiceInstanceFromCache() throws Exception {
171 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
173 final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
175 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
177 invokeHttpPut(url, getServiceInstance());
179 final Optional<ServiceInstance> actual =
180 cacheServiceProvider.getServiceInstance(GLOBAL_CUSTOMER_ID, SERVICE_TYPE, SERVICE_INSTANCE_ID);
182 assertTrue(actual.isPresent());
183 final ServiceInstance actualServiceInstance = actual.get();
185 assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
186 assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
191 public void test_getSericeInstance_usingServiceInstanceName_ableToRetrieveServiceInstanceFromCache()
194 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
196 final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
198 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
200 invokeHttpPut(url, getServiceInstance());
202 final String serviceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCES_URL
203 + "?depth=2&service-instance-name=" + SERVICE_NAME;
205 final ResponseEntity<ServiceInstances> actual = restTemplate.exchange(serviceInstanceUrl, HttpMethod.GET,
206 new HttpEntity<>(getHttpHeaders()), ServiceInstances.class);
208 assertEquals(HttpStatus.OK, actual.getStatusCode());
209 assertTrue(actual.hasBody());
211 final ServiceInstances actualServiceInstances = actual.getBody();
212 assertFalse(actualServiceInstances.getServiceInstance().isEmpty());
214 assertEquals(SERVICE_NAME, actualServiceInstances.getServiceInstance().get(0).getServiceInstanceName());
219 public void test_getSericeInstance_usingServiceInstanceName_returnRequestErrorIfnoServiceInstanceFound()
222 final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
224 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
226 final String serviceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCES_URL
227 + "?depth=2&service-instance-name=" + SERVICE_NAME;
229 final ResponseEntity<RequestError> actual = restTemplate.exchange(serviceInstanceUrl, HttpMethod.GET,
230 new HttpEntity<>(getHttpHeaders()), RequestError.class);
232 assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
233 assertTrue(actual.hasBody());
235 assertNotNull(actual.getBody().getServiceException());
240 public void test_getSericeInstance_usingServiceInstanceId_ableToRetrieveServiceInstanceFromCache()
243 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
245 final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
247 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
249 invokeHttpPut(url, getServiceInstance());
251 final ResponseEntity<ServiceInstance> actual =
252 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), ServiceInstance.class);
254 assertEquals(HttpStatus.OK, actual.getStatusCode());
255 assertTrue(actual.hasBody());
257 final ServiceInstance actualServiceInstance = actual.getBody();
259 assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
260 assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
265 public void test_getSericeInstance_usinginvalidServiceInstanceId_shouldReturnError() throws Exception {
267 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
269 final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
271 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
273 invokeHttpPut(url, getServiceInstance());
275 final String invalidServiceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL
276 + SERVICE_INSTANCES_URL + "/service-instance/" + UUID.randomUUID();
278 final ResponseEntity<RequestError> actual = restTemplate.exchange(invalidServiceInstanceUrl, HttpMethod.GET,
279 new HttpEntity<>(getHttpHeaders()), RequestError.class);
281 assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
283 final RequestError actualError = actual.getBody();
284 final ServiceException serviceException = actualError.getServiceException();
286 assertNotNull(serviceException);
287 assertEquals(RequestErrorResponseUtils.ERROR_MESSAGE_ID, serviceException.getMessageId());
288 assertEquals(RequestErrorResponseUtils.ERROR_MESSAGE, serviceException.getText());
289 assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
294 public void test_getSericeInstance_usingInvalidServiceInstanceName_shouldReturnError() throws Exception {
296 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
298 final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
300 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
302 final ResponseEntity<Void> putRequestReponse = invokeHttpPut(url, getServiceInstance());
303 assertEquals(HttpStatus.ACCEPTED, putRequestReponse.getStatusCode());
306 final String serviceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCES_URL
307 + "?service-instance-name=Dummy&depth=2";
309 final ResponseEntity<RequestError> actual = restTemplate.exchange(serviceInstanceUrl, HttpMethod.GET,
310 new HttpEntity<>(getHttpHeaders()), RequestError.class);
312 assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
314 final RequestError actualError = actual.getBody();
315 final ServiceException serviceException = actualError.getServiceException();
317 assertNotNull(serviceException);
318 assertEquals(RequestErrorResponseUtils.ERROR_MESSAGE_ID, serviceException.getMessageId());
319 assertEquals(RequestErrorResponseUtils.ERROR_MESSAGE, serviceException.getText());
320 assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
325 public void test_PathSericeInstance_usingServiceInstanceId_OrchStatusChangedInCache() throws Exception {
327 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
329 final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
331 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
333 final ResponseEntity<Void> serviceInstancePutResponse = invokeHttpPut(url, getServiceInstance());
334 assertEquals(HttpStatus.ACCEPTED, serviceInstancePutResponse.getStatusCode());
336 final HttpHeaders httpHeaders = getHttpHeaders();
337 httpHeaders.add(X_HTTP_METHOD_OVERRIDE, HttpMethod.PATCH.toString());
339 final HttpEntity<?> orchStatuUpdateServiceInstance =
340 getHttpEntity(getOrchStatuUpdateServiceInstance(), httpHeaders);
342 final ResponseEntity<Void> orchStatuUpdateServiceInstanceResponse =
343 invokeHttpPost(orchStatuUpdateServiceInstance, url, getOrchStatuUpdateServiceInstance());
345 assertEquals(HttpStatus.ACCEPTED, orchStatuUpdateServiceInstanceResponse.getStatusCode());
348 final ResponseEntity<ServiceInstance> actual =
349 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), ServiceInstance.class);
351 assertEquals(HttpStatus.OK, actual.getStatusCode());
352 assertTrue(actual.hasBody());
354 final ServiceInstance actualServiceInstance = actual.getBody();
356 assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
357 assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
358 assertEquals(ORCHESTRATION_STATUS, actualServiceInstance.getOrchestrationStatus());
363 public void test_putServiceSubscription_successfullyAddedToCache() throws Exception {
364 final String serviceSubscriptionurl =
365 getCustomerEndPointUrl() + "/service-subscriptions/service-subscription/" + FIREWALL_SERVICE_TTYPE;
367 final ResponseEntity<Void> customerPutResponse = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
368 assertEquals(HttpStatus.ACCEPTED, customerPutResponse.getStatusCode());
370 final ResponseEntity<Void> serviceSubscriptionPutResponse =
371 invokeHttpPut(serviceSubscriptionurl, getServiceSubscription());
372 assertEquals(HttpStatus.ACCEPTED, serviceSubscriptionPutResponse.getStatusCode());
374 final ResponseEntity<ServiceSubscription> actual = restTemplate.exchange(serviceSubscriptionurl, HttpMethod.GET,
375 new HttpEntity<>(getHttpHeaders()), ServiceSubscription.class);
377 assertEquals(HttpStatus.OK, actual.getStatusCode());
378 assertTrue(actual.hasBody());
380 final ServiceSubscription actualServiceSubscription = actual.getBody();
381 assertEquals(FIREWALL_SERVICE_TTYPE, actualServiceSubscription.getServiceType());
386 public void test_putSericeInstanceRelatedTo_ableToRetrieveServiceInstanceFromCache() throws Exception {
388 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
390 final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
392 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
394 final ResponseEntity<Void> responseEntity = invokeHttpPut(url, getServiceInstance());
395 assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
397 final String relationShipUrl =
398 getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL + RELATIONSHIP_URL;
401 final HttpEntity<?> httpEntity = getHttpEntity(getRelationShipJsonObject());
402 final ResponseEntity<Relationship> responseEntity2 =
403 restTemplate.exchange(relationShipUrl, HttpMethod.PUT, httpEntity, Relationship.class);
404 assertEquals(HttpStatus.ACCEPTED, responseEntity2.getStatusCode());
406 final String genericVnfUrl = TestUtils.getBaseUrl(port) + GENERIC_VNF_URL + VNF_ID;
407 final ResponseEntity<Void> genericVnfResponse = invokeHttpPut(genericVnfUrl, getGenericVnf());
408 assertEquals(HttpStatus.ACCEPTED, genericVnfResponse.getStatusCode());
411 final ResponseEntity<GenericVnfs> actual =
412 restTemplate.exchange(url + RELATED_TO_URL + "?vnf-name=" + GENERIC_VNF_NAME, HttpMethod.GET,
413 new HttpEntity<>(getHttpHeaders()), GenericVnfs.class);
415 assertEquals(HttpStatus.OK, actual.getStatusCode());
417 assertTrue(actual.hasBody());
418 final GenericVnfs genericVnfs = actual.getBody();
419 assertFalse(genericVnfs.getGenericVnf().isEmpty());
420 final GenericVnf genericVnf = genericVnfs.getGenericVnf().get(0);
421 assertEquals(GENERIC_VNF_NAME, genericVnf.getVnfName());
426 private String getCustomer() throws Exception, IOException {
427 return getJsonString("test-data/business-customer.json");
430 private String getServiceSubscription() throws Exception, IOException {
431 return getJsonString("test-data/service-subscription.json");
435 private String getCustomerEndPointUrl() {
436 return TestUtils.getBaseUrl(port) + CUSTOMERS_URL;
439 private ResponseEntity<Void> invokeHttpPut(final String url, final Object obj) {
440 final HttpEntity<?> httpEntity = getHttpEntity(obj);
441 return restTemplate.exchange(url, HttpMethod.PUT, httpEntity, Void.class);
444 private ResponseEntity<Void> invokeHttpPost(final HttpEntity<?> httpEntity, final String url, final Object obj) {
445 return restTemplate.exchange(url, HttpMethod.POST, httpEntity, Void.class);
448 private HttpEntity<?> getHttpEntity(final Object obj) {
449 return new HttpEntity<>(obj, getHttpHeaders());
452 private HttpEntity<?> getHttpEntity(final Object obj, final HttpHeaders headers) {
453 return new HttpEntity<>(obj, headers);
456 private HttpHeaders getHttpHeaders() {
457 return TestUtils.getHttpHeaders(userCredentials.getUsers().iterator().next().getUsername());
460 private String getServiceInstance() throws Exception, IOException {
461 return getJsonString("test-data/service-instance.json");
464 private String getOrchStatuUpdateServiceInstance() throws Exception, IOException {
465 return getJsonString("test-data/service-instance-orch-status-update.json");
468 private String getRelationShipJsonObject() throws IOException {
469 return getJsonString("test-data/service-Instance-relationShip.json");
472 private String getGenericVnf() throws IOException {
473 return getJsonString("test-data/generic-vnf.json");