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.aai.simulator.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;
27 import java.io.IOException;
28 import org.junit.After;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.onap.aai.domain.yang.Customer;
32 import org.onap.aai.domain.yang.ServiceSubscription;
33 import org.onap.so.aai.simulator.service.providers.CustomerServiceProvider;
34 import org.onap.so.aai.simulator.utils.Constant;
35 import org.onap.so.aai.simulator.utils.RequestError;
36 import org.onap.so.aai.simulator.utils.ServiceException;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.boot.test.context.SpringBootTest;
39 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
40 import org.springframework.boot.test.web.client.TestRestTemplate;
41 import org.springframework.boot.web.server.LocalServerPort;
42 import org.springframework.context.annotation.Configuration;
43 import org.springframework.core.io.ClassPathResource;
44 import org.springframework.http.HttpEntity;
45 import org.springframework.http.HttpMethod;
46 import org.springframework.http.HttpStatus;
47 import org.springframework.http.ResponseEntity;
48 import org.springframework.test.context.ActiveProfiles;
49 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
50 import com.fasterxml.jackson.databind.ObjectMapper;
51 import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
54 * @author waqas.ikram@ericsson.com
57 @RunWith(SpringJUnit4ClassRunner.class)
58 @ActiveProfiles("test")
59 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
61 public class BusinessControllerTest {
63 private static final String SERVICE_TYPE = "vCPE";
65 private static final String GLOBAL_CUSTOMER_ID = "DemoCustomer";
71 private TestRestTemplate restTemplate;
74 private CustomerServiceProvider customerServiceProvider;
78 customerServiceProvider.clearAll();
82 public void test_putCustomer_successfullyAddedToCache() throws Exception {
83 final Customer customer = getCustomer(getFile("test-data/business-customer.json"));
84 final String url = getBaseUrl() + "/customers/customer/" + GLOBAL_CUSTOMER_ID;
85 final ResponseEntity<Void> actual = invokeHttpPut(url, customer);
87 assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
88 assertTrue(customerServiceProvider.getCustomer(GLOBAL_CUSTOMER_ID).isPresent());
92 public void test_getCustomer_ableToRetrieveCustomer() throws Exception {
93 final Customer customer = getCustomer(getFile("test-data/business-customer.json"));
94 final String url = getBaseUrl() + "/customers/customer/" + GLOBAL_CUSTOMER_ID;
96 invokeHttpPut(url, customer);
98 final ResponseEntity<Customer> actual = restTemplate.getForEntity(url, Customer.class);
100 assertEquals(HttpStatus.OK, actual.getStatusCode());
101 assertTrue(actual.hasBody());
103 final Customer actualCustomer = actual.getBody();
104 assertEquals(GLOBAL_CUSTOMER_ID, actualCustomer.getGlobalCustomerId());
105 assertNotNull(actualCustomer.getResourceVersion());
106 assertFalse(actualCustomer.getResourceVersion().isEmpty());
110 public void test_getCustomer_returnRequestError_ifCustomerNotInCache() throws Exception {
111 final String url = getBaseUrl() + "/customers/customer/" + GLOBAL_CUSTOMER_ID;
113 final ResponseEntity<RequestError> actual = restTemplate.getForEntity(url, RequestError.class);
115 assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
117 final RequestError actualError = actual.getBody();
118 final ServiceException serviceException = actualError.getServiceException();
120 assertNotNull(serviceException);
121 assertEquals(Constant.ERROR_MESSAGE_ID, serviceException.getMessageId());
122 assertEquals(Constant.ERROR_MESSAGE, serviceException.getText());
123 assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
128 public void test_getServiceSubscription_ableToRetrieveServiceSubscriptionFromCache() throws Exception {
129 final Customer customer = getCustomer(getFile("test-data/business-customer.json"));
130 final String customerUrl = getBaseUrl() + "/customers/customer/" + GLOBAL_CUSTOMER_ID;
131 final String url = customerUrl + "/service-subscriptions/service-subscription/" + SERVICE_TYPE;
133 invokeHttpPut(customerUrl, customer);
135 final ResponseEntity<ServiceSubscription> actual = restTemplate.getForEntity(url, ServiceSubscription.class);
137 assertEquals(HttpStatus.OK, actual.getStatusCode());
138 assertTrue(actual.hasBody());
140 final ServiceSubscription actualServiceSubscription = actual.getBody();
141 assertEquals(SERVICE_TYPE, actualServiceSubscription.getServiceType());
142 assertNotNull(actualServiceSubscription.getRelationshipList());
143 assertFalse(actualServiceSubscription.getRelationshipList().getRelationship().isEmpty());
146 private ResponseEntity<Void> invokeHttpPut(final String url, final Object obj) {
147 return restTemplate.exchange(url, HttpMethod.PUT, new HttpEntity<>(obj), Void.class);
150 private File getFile(final String file) throws IOException {
151 return new ClassPathResource(file).getFile();
154 private Customer getCustomer(final File file) throws Exception {
155 final ObjectMapper mapper = new ObjectMapper();
156 mapper.registerModule(new JaxbAnnotationModule());
158 return mapper.readValue(file, Customer.class);
161 private String getBaseUrl() {
162 return "http://localhost:" + port + Constant.BUSINESS_URL;