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