Adding infrastructure for sdnc simulator
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / test / java / org / onap / so / aaisimulator / 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.aaisimulator.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.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.GLOBAL_CUSTOMER_ID;
29 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_INSTANCES_URL;
30 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_INSTANCE_ID;
31 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_INSTANCE_URL;
32 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_NAME;
33 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_SUBSCRIPTIONS_URL;
34 import static org.onap.so.aaisimulator.utils.TestConstants.SERVICE_TYPE;
35 import static org.onap.so.aaisimulator.utils.TestUtils.getJsonString;
36 import java.io.IOException;
37 import java.util.Optional;
38 import java.util.UUID;
39 import org.junit.After;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.onap.aai.domain.yang.Customer;
43 import org.onap.aai.domain.yang.ServiceInstance;
44 import org.onap.aai.domain.yang.ServiceInstances;
45 import org.onap.aai.domain.yang.ServiceSubscription;
46 import org.onap.so.aaisimulator.service.providers.CustomerCacheServiceProvider;
47 import org.onap.so.aaisimulator.utils.Constants;
48 import org.onap.so.aaisimulator.utils.RequestError;
49 import org.onap.so.aaisimulator.utils.ServiceException;
50 import org.onap.so.aaisimulator.utils.TestUtils;
51 import org.springframework.beans.factory.annotation.Autowired;
52 import org.springframework.beans.factory.annotation.Value;
53 import org.springframework.boot.test.context.SpringBootTest;
54 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
55 import org.springframework.boot.test.web.client.TestRestTemplate;
56 import org.springframework.boot.web.server.LocalServerPort;
57 import org.springframework.context.annotation.Configuration;
58 import org.springframework.http.HttpEntity;
59 import org.springframework.http.HttpHeaders;
60 import org.springframework.http.HttpMethod;
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;
65
66 /**
67  * @author waqas.ikram@ericsson.com
68  *
69  */
70 @RunWith(SpringJUnit4ClassRunner.class)
71 @ActiveProfiles("test")
72 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
73 @Configuration
74 public class BusinessControllerTest {
75
76     private static final String FIREWALL_SERVICE_TTYPE = "Firewall";
77
78     private static final String ORCHESTRATION_STATUS = "Active";
79
80     @LocalServerPort
81     private int port;
82
83     @Autowired
84     private TestRestTemplate restTemplate;
85
86     @Value("${spring.security.username}")
87     private String username;
88
89     @Autowired
90     private CustomerCacheServiceProvider cacheServiceProvider;
91
92     @After
93     public void after() {
94         cacheServiceProvider.clearAll();
95     }
96
97     @Test
98     public void test_putCustomer_successfullyAddedToCache() throws Exception {
99         final ResponseEntity<Void> actual = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
100
101         assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
102         assertTrue(cacheServiceProvider.getCustomer(GLOBAL_CUSTOMER_ID).isPresent());
103     }
104
105     @Test
106     public void test_getCustomer_ableToRetrieveCustomer() throws Exception {
107         final String url = getCustomerEndPointUrl();
108
109         invokeHttpPut(url, getCustomer());
110
111         final ResponseEntity<Customer> actual =
112                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), Customer.class);
113
114         assertEquals(HttpStatus.OK, actual.getStatusCode());
115         assertTrue(actual.hasBody());
116
117         final Customer actualCustomer = actual.getBody();
118         assertEquals(GLOBAL_CUSTOMER_ID, actualCustomer.getGlobalCustomerId());
119         assertNotNull(actualCustomer.getResourceVersion());
120         assertFalse(actualCustomer.getResourceVersion().isEmpty());
121     }
122
123     @Test
124     public void test_getCustomer_returnRequestError_ifCustomerNotInCache() throws Exception {
125         final String url = getCustomerEndPointUrl();
126
127         final ResponseEntity<RequestError> actual =
128                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), RequestError.class);
129
130         assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
131
132         final RequestError actualError = actual.getBody();
133         final ServiceException serviceException = actualError.getServiceException();
134
135         assertNotNull(serviceException);
136         assertEquals(Constants.ERROR_MESSAGE_ID, serviceException.getMessageId());
137         assertEquals(Constants.ERROR_MESSAGE, serviceException.getText());
138         assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
139
140     }
141
142     @Test
143     public void test_getServiceSubscription_ableToRetrieveServiceSubscriptionFromCache() throws Exception {
144         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL;
145
146         invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
147
148         final ResponseEntity<ServiceSubscription> actual = restTemplate.exchange(url, HttpMethod.GET,
149                 new HttpEntity<>(getHttpHeaders()), ServiceSubscription.class);
150
151         assertEquals(HttpStatus.OK, actual.getStatusCode());
152         assertTrue(actual.hasBody());
153
154         final ServiceSubscription actualServiceSubscription = actual.getBody();
155         assertEquals(SERVICE_TYPE, actualServiceSubscription.getServiceType());
156         assertNotNull(actualServiceSubscription.getRelationshipList());
157         assertFalse(actualServiceSubscription.getRelationshipList().getRelationship().isEmpty());
158     }
159
160     @Test
161     public void test_putSericeInstance_ableToRetrieveServiceInstanceFromCache() throws Exception {
162
163         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
164
165         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
166
167         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
168
169         invokeHttpPut(url, getServiceInstance());
170
171         final Optional<ServiceInstance> actual =
172                 cacheServiceProvider.getServiceInstance(GLOBAL_CUSTOMER_ID, SERVICE_TYPE, SERVICE_INSTANCE_ID);
173
174         assertTrue(actual.isPresent());
175         final ServiceInstance actualServiceInstance = actual.get();
176
177         assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
178         assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
179
180     }
181
182     @Test
183     public void test_getSericeInstance_usingServiceInstanceName_ableToRetrieveServiceInstanceFromCache()
184             throws Exception {
185
186         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
187
188         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
189
190         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
191
192         invokeHttpPut(url, getServiceInstance());
193
194         final String serviceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCES_URL
195                 + "?service-instance-name=" + SERVICE_NAME;
196
197         final ResponseEntity<ServiceInstances> actual = restTemplate.exchange(serviceInstanceUrl, HttpMethod.GET,
198                 new HttpEntity<>(getHttpHeaders()), ServiceInstances.class);
199
200         assertEquals(HttpStatus.OK, actual.getStatusCode());
201         assertTrue(actual.hasBody());
202
203         final ServiceInstances actualServiceInstances = actual.getBody();
204         assertFalse(actualServiceInstances.getServiceInstance().isEmpty());
205
206         assertEquals(SERVICE_NAME, actualServiceInstances.getServiceInstance().get(0).getServiceInstanceName());
207
208     }
209
210     @Test
211     public void test_getSericeInstance_usingServiceInstanceId_ableToRetrieveServiceInstanceFromCache()
212             throws Exception {
213
214         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
215
216         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
217
218         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
219
220         invokeHttpPut(url, getServiceInstance());
221
222         final ResponseEntity<ServiceInstance> actual =
223                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), ServiceInstance.class);
224
225         assertEquals(HttpStatus.OK, actual.getStatusCode());
226         assertTrue(actual.hasBody());
227
228         final ServiceInstance actualServiceInstance = actual.getBody();
229
230         assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
231         assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
232
233     }
234
235     @Test
236     public void test_getSericeInstance_usinginvalidServiceInstanceId_shouldReturnError() throws Exception {
237
238         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
239
240         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
241
242         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
243
244         invokeHttpPut(url, getServiceInstance());
245
246         final String invalidServiceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL
247                 + SERVICE_INSTANCES_URL + "/service-instance/" + UUID.randomUUID();
248
249         final ResponseEntity<RequestError> actual = restTemplate.exchange(invalidServiceInstanceUrl, HttpMethod.GET,
250                 new HttpEntity<>(getHttpHeaders()), RequestError.class);
251
252         assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
253
254         final RequestError actualError = actual.getBody();
255         final ServiceException serviceException = actualError.getServiceException();
256
257         assertNotNull(serviceException);
258         assertEquals(Constants.ERROR_MESSAGE_ID, serviceException.getMessageId());
259         assertEquals(Constants.ERROR_MESSAGE, serviceException.getText());
260         assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
261
262     }
263
264     @Test
265     public void test_getSericeInstance_usingInvalidServiceInstanceName_shouldReturnError() throws Exception {
266
267         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
268
269         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
270
271         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
272
273         final ResponseEntity<Void> putRequestReponse = invokeHttpPut(url, getServiceInstance());
274         assertEquals(HttpStatus.ACCEPTED, putRequestReponse.getStatusCode());
275
276
277         final String serviceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCES_URL
278                 + "?service-instance-name=Dummy&depth=2";
279
280         final ResponseEntity<RequestError> actual = restTemplate.exchange(serviceInstanceUrl, HttpMethod.GET,
281                 new HttpEntity<>(getHttpHeaders()), RequestError.class);
282
283         assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
284
285         final RequestError actualError = actual.getBody();
286         final ServiceException serviceException = actualError.getServiceException();
287
288         assertNotNull(serviceException);
289         assertEquals(Constants.ERROR_MESSAGE_ID, serviceException.getMessageId());
290         assertEquals(Constants.ERROR_MESSAGE, serviceException.getText());
291         assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
292
293     }
294
295     @Test
296     public void test_PathSericeInstance_usingServiceInstanceId_OrchStatusChangedInCache() throws Exception {
297
298         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
299
300         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
301
302         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
303
304         final ResponseEntity<Void> serviceInstancePutResponse = invokeHttpPut(url, getServiceInstance());
305         assertEquals(HttpStatus.ACCEPTED, serviceInstancePutResponse.getStatusCode());
306
307         final HttpHeaders httpHeaders = getHttpHeaders();
308         httpHeaders.add(X_HTTP_METHOD_OVERRIDE, HttpMethod.PATCH.toString());
309
310         final HttpEntity<?> orchStatuUpdateServiceInstance =
311                 getHttpEntity(getOrchStatuUpdateServiceInstance(), httpHeaders);
312
313         final ResponseEntity<Void> orchStatuUpdateServiceInstanceResponse =
314                 invokeHttpPost(orchStatuUpdateServiceInstance, url, getOrchStatuUpdateServiceInstance());
315
316         assertEquals(HttpStatus.ACCEPTED, orchStatuUpdateServiceInstanceResponse.getStatusCode());
317
318
319         final ResponseEntity<ServiceInstance> actual =
320                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), ServiceInstance.class);
321
322         assertEquals(HttpStatus.OK, actual.getStatusCode());
323         assertTrue(actual.hasBody());
324
325         final ServiceInstance actualServiceInstance = actual.getBody();
326
327         assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
328         assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
329         assertEquals(ORCHESTRATION_STATUS, actualServiceInstance.getOrchestrationStatus());
330
331     }
332
333     @Test
334     public void test_putServiceSubscription_successfullyAddedToCache() throws Exception {
335         final String serviceSubscriptionurl =
336                 getCustomerEndPointUrl() + "/service-subscriptions/service-subscription/" + FIREWALL_SERVICE_TTYPE;
337
338         final ResponseEntity<Void> customerPutResponse = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
339         assertEquals(HttpStatus.ACCEPTED, customerPutResponse.getStatusCode());
340
341         final ResponseEntity<Void> serviceSubscriptionPutResponse =
342                 invokeHttpPut(serviceSubscriptionurl, getServiceSubscription());
343         assertEquals(HttpStatus.ACCEPTED, serviceSubscriptionPutResponse.getStatusCode());
344
345         final ResponseEntity<ServiceSubscription> actual = restTemplate.exchange(serviceSubscriptionurl, HttpMethod.GET,
346                 new HttpEntity<>(getHttpHeaders()), ServiceSubscription.class);
347
348         assertEquals(HttpStatus.OK, actual.getStatusCode());
349         assertTrue(actual.hasBody());
350
351         final ServiceSubscription actualServiceSubscription = actual.getBody();
352         assertEquals(FIREWALL_SERVICE_TTYPE, actualServiceSubscription.getServiceType());
353
354     }
355
356     private String getCustomer() throws Exception, IOException {
357         return getJsonString("test-data/business-customer.json");
358     }
359
360     private String getServiceSubscription() throws Exception, IOException {
361         return getJsonString("test-data/service-subscription.json");
362     }
363
364
365     private String getCustomerEndPointUrl() {
366         return TestUtils.getBaseUrl(port) + CUSTOMERS_URL;
367     }
368
369     private ResponseEntity<Void> invokeHttpPut(final String url, final Object obj) {
370         final HttpEntity<?> httpEntity = getHttpEntity(obj);
371         return restTemplate.exchange(url, HttpMethod.PUT, httpEntity, Void.class);
372     }
373
374     private ResponseEntity<Void> invokeHttpPost(final HttpEntity<?> httpEntity, final String url, final Object obj) {
375         return restTemplate.exchange(url, HttpMethod.POST, httpEntity, Void.class);
376     }
377
378     private HttpEntity<?> getHttpEntity(final Object obj) {
379         return new HttpEntity<>(obj, getHttpHeaders());
380     }
381
382     private HttpEntity<?> getHttpEntity(final Object obj, final HttpHeaders headers) {
383         return new HttpEntity<>(obj, headers);
384     }
385
386     private HttpHeaders getHttpHeaders() {
387         return TestUtils.getHttpHeaders(username);
388     }
389
390     private String getServiceInstance() throws Exception, IOException {
391         return getJsonString("test-data/service-instance.json");
392     }
393
394     private String getOrchStatuUpdateServiceInstance() throws Exception, IOException {
395         return getJsonString("test-data/service-instance-orch-status-update.json");
396     }
397
398 }