283a2a20bdf2e22cdc3b47aaef10f4832e6a730c
[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 static org.onap.so.aai.simulator.utils.TestConstants.CUSTOMERS_URL;
27 import static org.onap.so.aai.simulator.utils.TestConstants.GLOBAL_CUSTOMER_ID;
28 import static org.onap.so.aai.simulator.utils.TestConstants.SERVICE_INSTANCES_URL;
29 import static org.onap.so.aai.simulator.utils.TestConstants.SERVICE_INSTANCE_ID;
30 import static org.onap.so.aai.simulator.utils.TestConstants.SERVICE_INSTANCE_URL;
31 import static org.onap.so.aai.simulator.utils.TestConstants.SERVICE_NAME;
32 import static org.onap.so.aai.simulator.utils.TestConstants.SERVICE_SUBSCRIPTIONS_URL;
33 import static org.onap.so.aai.simulator.utils.TestConstants.SERVICE_TYPE;
34 import static org.onap.so.aai.simulator.utils.TestUtils.getJsonString;
35 import java.io.IOException;
36 import java.util.Optional;
37 import java.util.UUID;
38 import org.junit.After;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.onap.aai.domain.yang.Customer;
42 import org.onap.aai.domain.yang.ServiceInstance;
43 import org.onap.aai.domain.yang.ServiceInstances;
44 import org.onap.aai.domain.yang.ServiceSubscription;
45 import org.onap.so.aai.simulator.service.providers.CustomerCacheServiceProvider;
46 import org.onap.so.aai.simulator.utils.Constants;
47 import org.onap.so.aai.simulator.utils.RequestError;
48 import org.onap.so.aai.simulator.utils.ServiceException;
49 import org.onap.so.aai.simulator.utils.TestUtils;
50 import org.springframework.beans.factory.annotation.Autowired;
51 import org.springframework.beans.factory.annotation.Value;
52 import org.springframework.boot.test.context.SpringBootTest;
53 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
54 import org.springframework.boot.test.web.client.TestRestTemplate;
55 import org.springframework.boot.web.server.LocalServerPort;
56 import org.springframework.context.annotation.Configuration;
57 import org.springframework.http.HttpEntity;
58 import org.springframework.http.HttpHeaders;
59 import org.springframework.http.HttpMethod;
60 import org.springframework.http.HttpStatus;
61 import org.springframework.http.ResponseEntity;
62 import org.springframework.test.context.ActiveProfiles;
63 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
64
65 /**
66  * @author waqas.ikram@ericsson.com
67  *
68  */
69 @RunWith(SpringJUnit4ClassRunner.class)
70 @ActiveProfiles("test")
71 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
72 @Configuration
73 public class BusinessControllerTest {
74
75     @LocalServerPort
76     private int port;
77
78     @Autowired
79     private TestRestTemplate restTemplate;
80
81     @Value("${spring.security.username}")
82     private String username;
83
84     @Autowired
85     private CustomerCacheServiceProvider cacheServiceProvider;
86
87     @After
88     public void after() {
89         cacheServiceProvider.clearAll();
90     }
91
92     @Test
93     public void test_putCustomer_successfullyAddedToCache() throws Exception {
94         final ResponseEntity<Void> actual = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
95
96         assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
97         assertTrue(cacheServiceProvider.getCustomer(GLOBAL_CUSTOMER_ID).isPresent());
98     }
99
100     @Test
101     public void test_getCustomer_ableToRetrieveCustomer() throws Exception {
102         final String url = getCustomerEndPointUrl();
103
104         invokeHttpPut(url, getCustomer());
105
106         final ResponseEntity<Customer> actual =
107                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), Customer.class);
108
109         assertEquals(HttpStatus.OK, actual.getStatusCode());
110         assertTrue(actual.hasBody());
111
112         final Customer actualCustomer = actual.getBody();
113         assertEquals(GLOBAL_CUSTOMER_ID, actualCustomer.getGlobalCustomerId());
114         assertNotNull(actualCustomer.getResourceVersion());
115         assertFalse(actualCustomer.getResourceVersion().isEmpty());
116     }
117
118     @Test
119     public void test_getCustomer_returnRequestError_ifCustomerNotInCache() throws Exception {
120         final String url = getCustomerEndPointUrl();
121
122         final ResponseEntity<RequestError> actual =
123                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), RequestError.class);
124
125         assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
126
127         final RequestError actualError = actual.getBody();
128         final ServiceException serviceException = actualError.getServiceException();
129
130         assertNotNull(serviceException);
131         assertEquals(Constants.ERROR_MESSAGE_ID, serviceException.getMessageId());
132         assertEquals(Constants.ERROR_MESSAGE, serviceException.getText());
133         assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
134
135     }
136
137     @Test
138     public void test_getServiceSubscription_ableToRetrieveServiceSubscriptionFromCache() throws Exception {
139         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL;
140
141         invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
142
143         final ResponseEntity<ServiceSubscription> actual = restTemplate.exchange(url, HttpMethod.GET,
144                 new HttpEntity<>(getHttpHeaders()), ServiceSubscription.class);
145
146         assertEquals(HttpStatus.OK, actual.getStatusCode());
147         assertTrue(actual.hasBody());
148
149         final ServiceSubscription actualServiceSubscription = actual.getBody();
150         assertEquals(SERVICE_TYPE, actualServiceSubscription.getServiceType());
151         assertNotNull(actualServiceSubscription.getRelationshipList());
152         assertFalse(actualServiceSubscription.getRelationshipList().getRelationship().isEmpty());
153     }
154
155     @Test
156     public void test_putSericeInstance_ableToRetrieveServiceInstanceFromCache() throws Exception {
157
158         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
159
160         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
161
162         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
163
164         invokeHttpPut(url, getServiceInstance());
165
166         final Optional<ServiceInstance> actual =
167                 cacheServiceProvider.getServiceInstance(GLOBAL_CUSTOMER_ID, SERVICE_TYPE, SERVICE_INSTANCE_ID);
168
169         assertTrue(actual.isPresent());
170         final ServiceInstance actualServiceInstance = actual.get();
171
172         assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
173         assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
174
175     }
176
177     @Test
178     public void test_getSericeInstance_usingServiceInstanceName_ableToRetrieveServiceInstanceFromCache()
179             throws Exception {
180
181         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
182
183         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
184
185         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
186
187         invokeHttpPut(url, getServiceInstance());
188
189         final String serviceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCES_URL
190                 + "?service-instance-name=" + SERVICE_NAME;
191
192         final ResponseEntity<ServiceInstances> actual = restTemplate.exchange(serviceInstanceUrl, HttpMethod.GET,
193                 new HttpEntity<>(getHttpHeaders()), ServiceInstances.class);
194
195         assertEquals(HttpStatus.OK, actual.getStatusCode());
196         assertTrue(actual.hasBody());
197
198         final ServiceInstances actualServiceInstances = actual.getBody();
199         assertFalse(actualServiceInstances.getServiceInstance().isEmpty());
200
201         assertEquals(SERVICE_NAME, actualServiceInstances.getServiceInstance().get(0).getServiceInstanceName());
202
203     }
204
205     @Test
206     public void test_getSericeInstance_usingServiceInstanceId_ableToRetrieveServiceInstanceFromCache()
207             throws Exception {
208
209         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
210
211         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
212
213         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
214
215         invokeHttpPut(url, getServiceInstance());
216
217         final ResponseEntity<ServiceInstance> actual =
218                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), ServiceInstance.class);
219
220         assertEquals(HttpStatus.OK, actual.getStatusCode());
221         assertTrue(actual.hasBody());
222
223         final ServiceInstance actualServiceInstance = actual.getBody();
224
225         assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
226         assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
227
228     }
229
230     @Test
231     public void test_getSericeInstance_usinginvalidServiceInstanceId_shouldReturnError() throws Exception {
232
233         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
234
235         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
236
237         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
238
239         invokeHttpPut(url, getServiceInstance());
240
241         final String invalidServiceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL
242                 + SERVICE_INSTANCES_URL + "/service-instance/" + UUID.randomUUID();
243
244         final ResponseEntity<RequestError> actual = restTemplate.exchange(invalidServiceInstanceUrl, HttpMethod.GET,
245                 new HttpEntity<>(getHttpHeaders()), RequestError.class);
246
247         assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
248
249         final RequestError actualError = actual.getBody();
250         final ServiceException serviceException = actualError.getServiceException();
251
252         assertNotNull(serviceException);
253         assertEquals(Constants.ERROR_MESSAGE_ID, serviceException.getMessageId());
254         assertEquals(Constants.ERROR_MESSAGE, serviceException.getText());
255         assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
256
257     }
258
259     @Test
260     public void test_getSericeInstance_usingInvalidServiceInstanceName_shouldReturnError() throws Exception {
261
262         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
263
264         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
265
266         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
267
268         final ResponseEntity<Void> putRequestReponse = invokeHttpPut(url, getServiceInstance());
269         assertEquals(HttpStatus.ACCEPTED, putRequestReponse.getStatusCode());
270
271
272         final String serviceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCES_URL
273                 + "?service-instance-name=Dummy&depth=2";
274
275         final ResponseEntity<RequestError> actual = restTemplate.exchange(serviceInstanceUrl, HttpMethod.GET,
276                 new HttpEntity<>(getHttpHeaders()), RequestError.class);
277
278         assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
279
280         final RequestError actualError = actual.getBody();
281         final ServiceException serviceException = actualError.getServiceException();
282
283         assertNotNull(serviceException);
284         assertEquals(Constants.ERROR_MESSAGE_ID, serviceException.getMessageId());
285         assertEquals(Constants.ERROR_MESSAGE, serviceException.getText());
286         assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
287
288     }
289
290     private String getCustomer() throws Exception, IOException {
291         return getJsonString("test-data/business-customer.json");
292     }
293
294     private String getCustomerEndPointUrl() {
295         return TestUtils.getBaseUrl(port) + CUSTOMERS_URL;
296     }
297
298     private ResponseEntity<Void> invokeHttpPut(final String url, final Object obj) {
299         final HttpEntity<?> httpEntity = getHttpEntity(obj);
300         return restTemplate.exchange(url, HttpMethod.PUT, httpEntity, Void.class);
301     }
302
303     private HttpEntity<?> getHttpEntity(final Object obj) {
304         return new HttpEntity<>(obj, getHttpHeaders());
305     }
306
307     private HttpHeaders getHttpHeaders() {
308         return TestUtils.getHttpHeaders(username);
309     }
310
311     private String getServiceInstance() throws Exception, IOException {
312         return getJsonString("test-data/service-instance.json");
313     }
314
315 }