Updating image version and fixing tests
[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.BI_DIRECTIONAL_RELATIONSHIP_LIST_URL;
27 import static org.onap.so.aaisimulator.utils.Constants.X_HTTP_METHOD_OVERRIDE;
28 import static org.onap.so.aaisimulator.utils.TestConstants.CUSTOMERS_URL;
29 import static org.onap.so.aaisimulator.utils.TestConstants.GENERIC_VNF_NAME;
30 import static org.onap.so.aaisimulator.utils.TestConstants.GENERIC_VNF_URL;
31 import static org.onap.so.aaisimulator.utils.TestConstants.GLOBAL_CUSTOMER_ID;
32 import static org.onap.so.aaisimulator.utils.TestConstants.RELATED_TO_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.getCustomer;
41 import static org.onap.so.aaisimulator.utils.TestUtils.getServiceInstance;
42 import java.io.IOException;
43 import java.util.Optional;
44 import java.util.UUID;
45 import org.junit.After;
46 import org.junit.Test;
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.springframework.beans.factory.annotation.Autowired;
60 import org.springframework.http.HttpHeaders;
61 import org.springframework.http.HttpMethod;
62 import org.springframework.http.HttpStatus;
63 import org.springframework.http.ResponseEntity;
64
65 /**
66  * @author waqas.ikram@ericsson.com
67  *
68  */
69 public class BusinessControllerTest extends AbstractSpringBootTest {
70
71     private static final String FIREWALL_SERVICE_TYPE = "Firewall";
72
73     private static final String ORCHESTRATION_STATUS = "Active";
74
75     @Autowired
76     private CustomerCacheServiceProvider cacheServiceProvider;
77
78     @After
79     public void after() {
80         cacheServiceProvider.clearAll();
81     }
82
83     @Test
84     public void test_putCustomer_successfullyAddedToCache() throws Exception {
85         invokeCustomerEndPointAndAssertResponse();
86         assertTrue(cacheServiceProvider.getCustomer(GLOBAL_CUSTOMER_ID).isPresent());
87     }
88
89     @Test
90     public void test_getCustomer_ableToRetrieveCustomer() throws Exception {
91         final String url = getUrl(CUSTOMERS_URL);
92
93         final ResponseEntity<Void> response = testRestTemplateService.invokeHttpPut(url, getCustomer(), Void.class);
94         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
95
96         final ResponseEntity<Customer> actual = testRestTemplateService.invokeHttpGet(url, Customer.class);
97
98         assertEquals(HttpStatus.OK, actual.getStatusCode());
99         assertTrue(actual.hasBody());
100
101         final Customer actualCustomer = actual.getBody();
102         assertEquals(GLOBAL_CUSTOMER_ID, actualCustomer.getGlobalCustomerId());
103         assertNotNull(actualCustomer.getResourceVersion());
104         assertFalse(actualCustomer.getResourceVersion().isEmpty());
105     }
106
107     @Test
108     public void test_getCustomer_returnRequestError_ifCustomerNotInCache() throws Exception {
109         final String url = getUrl(CUSTOMERS_URL);
110
111         final ResponseEntity<RequestError> actual = testRestTemplateService.invokeHttpGet(url, RequestError.class);
112
113         assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
114
115         final RequestError actualError = actual.getBody();
116         final ServiceException serviceException = actualError.getServiceException();
117
118         assertNotNull(serviceException);
119         assertEquals(RequestErrorResponseUtils.ERROR_MESSAGE_ID, serviceException.getMessageId());
120         assertEquals(RequestErrorResponseUtils.ERROR_MESSAGE, serviceException.getText());
121         assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
122
123     }
124
125     @Test
126     public void test_getServiceSubscription_ableToRetrieveServiceSubscriptionFromCache() throws Exception {
127         final String url = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL);
128
129         invokeCustomerEndPointAndAssertResponse();
130
131         final ResponseEntity<ServiceSubscription> actual =
132                 testRestTemplateService.invokeHttpGet(url, ServiceSubscription.class);
133
134         assertEquals(HttpStatus.OK, actual.getStatusCode());
135         assertTrue(actual.hasBody());
136
137         final ServiceSubscription actualServiceSubscription = actual.getBody();
138         assertEquals(SERVICE_TYPE, actualServiceSubscription.getServiceType());
139         assertNotNull(actualServiceSubscription.getRelationshipList());
140         assertFalse(actualServiceSubscription.getRelationshipList().getRelationship().isEmpty());
141     }
142
143     @Test
144     public void test_putSericeInstance_ableToRetrieveServiceInstanceFromCache() throws Exception {
145
146         invokeCustomerEndPointAndAssertResponse();
147         invokeServiceInstanceEndPointAndAssertResponse();
148
149
150         final Optional<ServiceInstance> actual =
151                 cacheServiceProvider.getServiceInstance(GLOBAL_CUSTOMER_ID, SERVICE_TYPE, SERVICE_INSTANCE_ID);
152
153         assertTrue(actual.isPresent());
154         final ServiceInstance actualServiceInstance = actual.get();
155
156         assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
157         assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
158
159     }
160
161     @Test
162     public void test_getSericeInstance_usingServiceInstanceName_ableToRetrieveServiceInstanceFromCache()
163             throws Exception {
164
165         invokeCustomerEndPointAndAssertResponse();
166         invokeServiceInstanceEndPointAndAssertResponse();
167
168
169         final String serviceInstanceUrl = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCES_URL)
170                 + "?depth=2&service-instance-name=" + SERVICE_NAME;
171
172         final ResponseEntity<ServiceInstances> actual =
173                 testRestTemplateService.invokeHttpGet(serviceInstanceUrl, ServiceInstances.class);
174
175         assertEquals(HttpStatus.OK, actual.getStatusCode());
176         assertTrue(actual.hasBody());
177
178         final ServiceInstances actualServiceInstances = actual.getBody();
179         assertFalse(actualServiceInstances.getServiceInstance().isEmpty());
180
181         assertEquals(SERVICE_NAME, actualServiceInstances.getServiceInstance().get(0).getServiceInstanceName());
182
183     }
184
185     @Test
186     public void test_getSericeInstance_usingServiceInstanceName_returnRequestErrorIfnoServiceInstanceFound()
187             throws Exception {
188
189         invokeCustomerEndPointAndAssertResponse();
190
191         final String serviceInstanceUrl = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCES_URL)
192                 + "?depth=2&service-instance-name=" + SERVICE_NAME;
193
194         final ResponseEntity<RequestError> actual =
195                 testRestTemplateService.invokeHttpGet(serviceInstanceUrl, RequestError.class);
196
197         assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
198         assertTrue(actual.hasBody());
199
200         assertNotNull(actual.getBody().getServiceException());
201
202     }
203
204     @Test
205     public void test_getSericeInstance_usingServiceInstanceId_ableToRetrieveServiceInstanceFromCache()
206             throws Exception {
207
208         final String url = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL);
209
210         invokeCustomerEndPointAndAssertResponse();
211         invokeServiceInstanceEndPointAndAssertResponse();
212
213         final ResponseEntity<ServiceInstance> actual =
214                 testRestTemplateService.invokeHttpGet(url, ServiceInstance.class);
215
216         assertEquals(HttpStatus.OK, actual.getStatusCode());
217         assertTrue(actual.hasBody());
218
219         final ServiceInstance actualServiceInstance = actual.getBody();
220
221         assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
222         assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
223
224     }
225
226     @Test
227     public void test_getSericeInstance_usinginvalidServiceInstanceId_shouldReturnError() throws Exception {
228
229         invokeCustomerEndPointAndAssertResponse();
230
231         invokeServiceInstanceEndPointAndAssertResponse();
232
233
234         final String invalidServiceInstanceUrl = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL,
235                 SERVICE_INSTANCES_URL + "/service-instance/" + UUID.randomUUID());
236
237         final ResponseEntity<RequestError> actual =
238                 testRestTemplateService.invokeHttpGet(invalidServiceInstanceUrl, RequestError.class);
239
240         assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
241
242         final RequestError actualError = actual.getBody();
243         final ServiceException serviceException = actualError.getServiceException();
244
245         assertNotNull(serviceException);
246         assertEquals(RequestErrorResponseUtils.ERROR_MESSAGE_ID, serviceException.getMessageId());
247         assertEquals(RequestErrorResponseUtils.ERROR_MESSAGE, serviceException.getText());
248         assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
249
250     }
251
252     @Test
253     public void test_getSericeInstance_usingInvalidServiceInstanceName_shouldReturnError() throws Exception {
254
255         invokeCustomerEndPointAndAssertResponse();
256         invokeServiceInstanceEndPointAndAssertResponse();
257
258
259         final String serviceInstanceUrl = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCES_URL)
260                 + "?service-instance-name=Dummy&depth=2";
261
262         final ResponseEntity<RequestError> actual =
263                 testRestTemplateService.invokeHttpGet(serviceInstanceUrl, RequestError.class);
264
265         assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
266
267         final RequestError actualError = actual.getBody();
268         final ServiceException serviceException = actualError.getServiceException();
269
270         assertNotNull(serviceException);
271         assertEquals(RequestErrorResponseUtils.ERROR_MESSAGE_ID, serviceException.getMessageId());
272         assertEquals(RequestErrorResponseUtils.ERROR_MESSAGE, serviceException.getText());
273         assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
274
275     }
276
277     @Test
278     public void test_PathSericeInstance_usingServiceInstanceId_OrchStatusChangedInCache() throws Exception {
279
280         final String url = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL);
281
282         invokeCustomerEndPointAndAssertResponse();
283         invokeServiceInstanceEndPointAndAssertResponse();
284
285         final HttpHeaders httpHeaders = testRestTemplateService.getHttpHeaders();
286         httpHeaders.add(X_HTTP_METHOD_OVERRIDE, HttpMethod.PATCH.toString());
287
288         final ResponseEntity<Void> orchStatuUpdateServiceInstanceResponse = testRestTemplateService
289                 .invokeHttpPost(httpHeaders, url, TestUtils.getOrchStatuUpdateServiceInstance(), Void.class);
290
291         assertEquals(HttpStatus.ACCEPTED, orchStatuUpdateServiceInstanceResponse.getStatusCode());
292
293         final ResponseEntity<ServiceInstance> actual =
294                 testRestTemplateService.invokeHttpGet(url, ServiceInstance.class);
295
296         assertEquals(HttpStatus.OK, actual.getStatusCode());
297         assertTrue(actual.hasBody());
298
299         final ServiceInstance actualServiceInstance = actual.getBody();
300
301         assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
302         assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
303         assertEquals(ORCHESTRATION_STATUS, actualServiceInstance.getOrchestrationStatus());
304
305     }
306
307     @Test
308     public void test_putServiceSubscription_successfullyAddedToCache() throws Exception {
309         final String serviceSubscriptionurl =
310                 getUrl(CUSTOMERS_URL, "/service-subscriptions/service-subscription/", FIREWALL_SERVICE_TYPE);
311
312         invokeCustomerEndPointAndAssertResponse();
313
314         final ResponseEntity<Void> responseEntity = testRestTemplateService.invokeHttpPut(serviceSubscriptionurl,
315                 TestUtils.getServiceSubscription(), Void.class);
316         assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
317
318         final ResponseEntity<ServiceSubscription> actual =
319                 testRestTemplateService.invokeHttpGet(serviceSubscriptionurl, ServiceSubscription.class);
320
321         assertEquals(HttpStatus.OK, actual.getStatusCode());
322         assertTrue(actual.hasBody());
323
324         final ServiceSubscription actualServiceSubscription = actual.getBody();
325         assertEquals(FIREWALL_SERVICE_TYPE, actualServiceSubscription.getServiceType());
326
327     }
328
329     @Test
330     public void test_putSericeInstanceRelatedTo_ableToRetrieveServiceInstanceFromCache() throws Exception {
331
332         final String url = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL);
333
334         invokeCustomerEndPointAndAssertResponse();
335
336         invokeServiceInstanceEndPointAndAssertResponse();
337
338         final String relationShipUrl = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL,
339                 BI_DIRECTIONAL_RELATIONSHIP_LIST_URL);
340
341         final ResponseEntity<Relationship> responseEntity2 = testRestTemplateService.invokeHttpPut(relationShipUrl,
342                 TestUtils.getRelationShipJsonObject(), Relationship.class);
343
344         assertEquals(HttpStatus.ACCEPTED, responseEntity2.getStatusCode());
345
346         final String genericVnfUrl = getUrl(GENERIC_VNF_URL, VNF_ID);
347         final ResponseEntity<Void> genericVnfResponse =
348                 testRestTemplateService.invokeHttpPut(genericVnfUrl, TestUtils.getGenericVnf(), Void.class);
349         assertEquals(HttpStatus.ACCEPTED, genericVnfResponse.getStatusCode());
350
351         final ResponseEntity<GenericVnfs> actual = testRestTemplateService
352                 .invokeHttpGet(url + RELATED_TO_URL + "?vnf-name=" + GENERIC_VNF_NAME, GenericVnfs.class);
353
354         assertEquals(HttpStatus.OK, actual.getStatusCode());
355
356         assertTrue(actual.hasBody());
357         final GenericVnfs genericVnfs = actual.getBody();
358         assertFalse(genericVnfs.getGenericVnf().isEmpty());
359         final GenericVnf genericVnf = genericVnfs.getGenericVnf().get(0);
360         assertEquals(GENERIC_VNF_NAME, genericVnf.getVnfName());
361     }
362
363     @Test
364     public void test_putServiceInstanceRelatedTo_ableToRetrieveAllRelatedGenericVnfsFromCache() throws Exception {
365         final String url = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL);
366         
367         invokeCustomerEndPointAndAssertResponse();
368         
369         invokeServiceInstanceEndPointAndAssertResponse();
370         
371         final String relationShipUrl = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL,
372                 BI_DIRECTIONAL_RELATIONSHIP_LIST_URL);
373         final ResponseEntity<Relationship> responseEntity2 = testRestTemplateService.invokeHttpPut(relationShipUrl,
374                 TestUtils.getRelationShipJsonObject(), Relationship.class);
375         assertEquals(HttpStatus.ACCEPTED, responseEntity2.getStatusCode());
376         
377         final String genericVnfUrl = getUrl(GENERIC_VNF_URL, VNF_ID);
378         final ResponseEntity<Void> genericVnfResponse =
379                 testRestTemplateService.invokeHttpPut(genericVnfUrl, TestUtils.getGenericVnf(), Void.class);
380         assertEquals(HttpStatus.ACCEPTED, genericVnfResponse.getStatusCode());
381         
382         final ResponseEntity<GenericVnfs> actual =
383                 testRestTemplateService.invokeHttpGet(url + RELATED_TO_URL, GenericVnfs.class);
384         assertEquals(HttpStatus.OK, actual.getStatusCode());
385         assertTrue(actual.hasBody());
386         final GenericVnfs genericVnfs = actual.getBody();
387         assertFalse(genericVnfs.getGenericVnf().isEmpty());
388         final GenericVnf genericVnf = genericVnfs.getGenericVnf().get(0);
389         assertEquals(GENERIC_VNF_NAME, genericVnf.getVnfName());
390     }
391
392
393     @Test
394     public void test_DeleteSericeInstance_ServiceInstanceRemovedFromCache() throws Exception {
395         final String url = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL);
396
397         invokeCustomerEndPointAndAssertResponse();
398
399         invokeServiceInstanceEndPointAndAssertResponse();
400
401         final Optional<ServiceInstance> optional =
402                 cacheServiceProvider.getServiceInstance(GLOBAL_CUSTOMER_ID, SERVICE_TYPE, SERVICE_INSTANCE_ID);
403         assertTrue(optional.isPresent());
404         final ServiceInstance serviceInstance = optional.get();
405
406         final ResponseEntity<Void> responseEntity = testRestTemplateService
407                 .invokeHttpDelete(url + "?resource-version=" + serviceInstance.getResourceVersion(), Void.class);
408         assertEquals(HttpStatus.NO_CONTENT, responseEntity.getStatusCode());
409         assertFalse(cacheServiceProvider.getServiceInstance(GLOBAL_CUSTOMER_ID, SERVICE_TYPE, SERVICE_INSTANCE_ID)
410                 .isPresent());
411     }
412
413     private void invokeServiceInstanceEndPointAndAssertResponse() throws IOException {
414         final String url = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL);
415         final ResponseEntity<Void> responseEntity =
416                 testRestTemplateService.invokeHttpPut(url, getServiceInstance(), Void.class);
417         assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
418     }
419
420     private void invokeCustomerEndPointAndAssertResponse() throws Exception, IOException {
421         final ResponseEntity<Void> response =
422                 testRestTemplateService.invokeHttpPut(getUrl(CUSTOMERS_URL), getCustomer(), Void.class);
423
424         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
425     }
426
427 }