3d856844751a31eb230a5c3a0123cd9b64cf211e
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / test / java / org / onap / so / aai / simulator / controller / BusinessControllerTest.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.aai.simulator.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 java.io.File;
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;
52
53 /**
54  * @author waqas.ikram@ericsson.com
55  *
56  */
57 @RunWith(SpringJUnit4ClassRunner.class)
58 @ActiveProfiles("test")
59 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
60 @Configuration
61 public class BusinessControllerTest {
62
63     private static final String SERVICE_TYPE = "vCPE";
64
65     private static final String GLOBAL_CUSTOMER_ID = "DemoCustomer";
66
67     @LocalServerPort
68     private int port;
69
70     @Autowired
71     private TestRestTemplate restTemplate;
72
73     @Autowired
74     private CustomerServiceProvider customerServiceProvider;
75
76     @After
77     public void after() {
78         customerServiceProvider.clearAll();
79     }
80
81     @Test
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);
86
87         assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
88         assertTrue(customerServiceProvider.getCustomer(GLOBAL_CUSTOMER_ID).isPresent());
89     }
90
91     @Test
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;
95
96         invokeHttpPut(url, customer);
97
98         final ResponseEntity<Customer> actual = restTemplate.getForEntity(url, Customer.class);
99
100         assertEquals(HttpStatus.OK, actual.getStatusCode());
101         assertTrue(actual.hasBody());
102
103         final Customer actualCustomer = actual.getBody();
104         assertEquals(GLOBAL_CUSTOMER_ID, actualCustomer.getGlobalCustomerId());
105         assertNotNull(actualCustomer.getResourceVersion());
106         assertFalse(actualCustomer.getResourceVersion().isEmpty());
107     }
108
109     @Test
110     public void test_getCustomer_returnRequestError_ifCustomerNotInCache() throws Exception {
111         final String url = getBaseUrl() + "/customers/customer/" + GLOBAL_CUSTOMER_ID;
112
113         final ResponseEntity<RequestError> actual = restTemplate.getForEntity(url, RequestError.class);
114
115         assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
116
117         final RequestError actualError = actual.getBody();
118         final ServiceException serviceException = actualError.getServiceException();
119
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()));
124
125     }
126
127     @Test
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;
132
133         invokeHttpPut(customerUrl, customer);
134
135         final ResponseEntity<ServiceSubscription> actual = restTemplate.getForEntity(url, ServiceSubscription.class);
136
137         assertEquals(HttpStatus.OK, actual.getStatusCode());
138         assertTrue(actual.hasBody());
139
140         final ServiceSubscription actualServiceSubscription = actual.getBody();
141         assertEquals(SERVICE_TYPE, actualServiceSubscription.getServiceType());
142         assertNotNull(actualServiceSubscription.getRelationshipList());
143         assertFalse(actualServiceSubscription.getRelationshipList().getRelationship().isEmpty());
144     }
145
146     private ResponseEntity<Void> invokeHttpPut(final String url, final Object obj) {
147         return restTemplate.exchange(url, HttpMethod.PUT, new HttpEntity<>(obj), Void.class);
148     }
149
150     private File getFile(final String file) throws IOException {
151         return new ClassPathResource(file).getFile();
152     }
153
154     private Customer getCustomer(final File file) throws Exception {
155         final ObjectMapper mapper = new ObjectMapper();
156         mapper.registerModule(new JaxbAnnotationModule());
157
158         return mapper.readValue(file, Customer.class);
159     }
160
161     private String getBaseUrl() {
162         return "http://localhost:" + port + Constant.BUSINESS_URL;
163     }
164
165 }