Adding generic vnf and relationship endpoints
[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.getJsonString;
41 import java.io.IOException;
42 import java.util.Optional;
43 import java.util.UUID;
44 import org.junit.After;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
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.onap.so.simulator.model.UserCredentials;
60 import org.springframework.beans.factory.annotation.Autowired;
61 import org.springframework.boot.test.context.SpringBootTest;
62 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
63 import org.springframework.boot.test.web.client.TestRestTemplate;
64 import org.springframework.boot.web.server.LocalServerPort;
65 import org.springframework.context.annotation.Configuration;
66 import org.springframework.http.HttpEntity;
67 import org.springframework.http.HttpHeaders;
68 import org.springframework.http.HttpMethod;
69 import org.springframework.http.HttpStatus;
70 import org.springframework.http.ResponseEntity;
71 import org.springframework.test.context.ActiveProfiles;
72 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
73
74 /**
75  * @author waqas.ikram@ericsson.com
76  *
77  */
78 @RunWith(SpringJUnit4ClassRunner.class)
79 @ActiveProfiles("test")
80 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
81 @Configuration
82 public class BusinessControllerTest {
83
84     private static final String FIREWALL_SERVICE_TTYPE = "Firewall";
85
86     private static final String ORCHESTRATION_STATUS = "Active";
87
88     @LocalServerPort
89     private int port;
90
91     @Autowired
92     private TestRestTemplate restTemplate;
93
94     @Autowired
95     private UserCredentials userCredentials;
96
97     @Autowired
98     private CustomerCacheServiceProvider cacheServiceProvider;
99
100     @After
101     public void after() {
102         cacheServiceProvider.clearAll();
103     }
104
105     @Test
106     public void test_putCustomer_successfullyAddedToCache() throws Exception {
107         final ResponseEntity<Void> actual = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
108
109         assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
110         assertTrue(cacheServiceProvider.getCustomer(GLOBAL_CUSTOMER_ID).isPresent());
111     }
112
113     @Test
114     public void test_getCustomer_ableToRetrieveCustomer() throws Exception {
115         final String url = getCustomerEndPointUrl();
116
117         invokeHttpPut(url, getCustomer());
118
119         final ResponseEntity<Customer> actual =
120                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), Customer.class);
121
122         assertEquals(HttpStatus.OK, actual.getStatusCode());
123         assertTrue(actual.hasBody());
124
125         final Customer actualCustomer = actual.getBody();
126         assertEquals(GLOBAL_CUSTOMER_ID, actualCustomer.getGlobalCustomerId());
127         assertNotNull(actualCustomer.getResourceVersion());
128         assertFalse(actualCustomer.getResourceVersion().isEmpty());
129     }
130
131     @Test
132     public void test_getCustomer_returnRequestError_ifCustomerNotInCache() throws Exception {
133         final String url = getCustomerEndPointUrl();
134
135         final ResponseEntity<RequestError> actual =
136                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), RequestError.class);
137
138         assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
139
140         final RequestError actualError = actual.getBody();
141         final ServiceException serviceException = actualError.getServiceException();
142
143         assertNotNull(serviceException);
144         assertEquals(RequestErrorResponseUtils.ERROR_MESSAGE_ID, serviceException.getMessageId());
145         assertEquals(RequestErrorResponseUtils.ERROR_MESSAGE, serviceException.getText());
146         assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
147
148     }
149
150     @Test
151     public void test_getServiceSubscription_ableToRetrieveServiceSubscriptionFromCache() throws Exception {
152         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL;
153
154         invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
155
156         final ResponseEntity<ServiceSubscription> actual = restTemplate.exchange(url, HttpMethod.GET,
157                 new HttpEntity<>(getHttpHeaders()), ServiceSubscription.class);
158
159         assertEquals(HttpStatus.OK, actual.getStatusCode());
160         assertTrue(actual.hasBody());
161
162         final ServiceSubscription actualServiceSubscription = actual.getBody();
163         assertEquals(SERVICE_TYPE, actualServiceSubscription.getServiceType());
164         assertNotNull(actualServiceSubscription.getRelationshipList());
165         assertFalse(actualServiceSubscription.getRelationshipList().getRelationship().isEmpty());
166     }
167
168     @Test
169     public void test_putSericeInstance_ableToRetrieveServiceInstanceFromCache() throws Exception {
170
171         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
172
173         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
174
175         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
176
177         invokeHttpPut(url, getServiceInstance());
178
179         final Optional<ServiceInstance> actual =
180                 cacheServiceProvider.getServiceInstance(GLOBAL_CUSTOMER_ID, SERVICE_TYPE, SERVICE_INSTANCE_ID);
181
182         assertTrue(actual.isPresent());
183         final ServiceInstance actualServiceInstance = actual.get();
184
185         assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
186         assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
187
188     }
189
190     @Test
191     public void test_getSericeInstance_usingServiceInstanceName_ableToRetrieveServiceInstanceFromCache()
192             throws Exception {
193
194         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
195
196         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
197
198         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
199
200         invokeHttpPut(url, getServiceInstance());
201
202         final String serviceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCES_URL
203                 + "?depth=2&service-instance-name=" + SERVICE_NAME;
204
205         final ResponseEntity<ServiceInstances> actual = restTemplate.exchange(serviceInstanceUrl, HttpMethod.GET,
206                 new HttpEntity<>(getHttpHeaders()), ServiceInstances.class);
207
208         assertEquals(HttpStatus.OK, actual.getStatusCode());
209         assertTrue(actual.hasBody());
210
211         final ServiceInstances actualServiceInstances = actual.getBody();
212         assertFalse(actualServiceInstances.getServiceInstance().isEmpty());
213
214         assertEquals(SERVICE_NAME, actualServiceInstances.getServiceInstance().get(0).getServiceInstanceName());
215
216     }
217
218     @Test
219     public void test_getSericeInstance_usingServiceInstanceName_returnRequestErrorIfnoServiceInstanceFound()
220             throws Exception {
221
222         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
223
224         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
225
226         final String serviceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCES_URL
227                 + "?depth=2&service-instance-name=" + SERVICE_NAME;
228
229         final ResponseEntity<RequestError> actual = restTemplate.exchange(serviceInstanceUrl, HttpMethod.GET,
230                 new HttpEntity<>(getHttpHeaders()), RequestError.class);
231
232         assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
233         assertTrue(actual.hasBody());
234
235         assertNotNull(actual.getBody().getServiceException());
236
237     }
238
239     @Test
240     public void test_getSericeInstance_usingServiceInstanceId_ableToRetrieveServiceInstanceFromCache()
241             throws Exception {
242
243         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
244
245         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
246
247         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
248
249         invokeHttpPut(url, getServiceInstance());
250
251         final ResponseEntity<ServiceInstance> actual =
252                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), ServiceInstance.class);
253
254         assertEquals(HttpStatus.OK, actual.getStatusCode());
255         assertTrue(actual.hasBody());
256
257         final ServiceInstance actualServiceInstance = actual.getBody();
258
259         assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
260         assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
261
262     }
263
264     @Test
265     public void test_getSericeInstance_usinginvalidServiceInstanceId_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         invokeHttpPut(url, getServiceInstance());
274
275         final String invalidServiceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL
276                 + SERVICE_INSTANCES_URL + "/service-instance/" + UUID.randomUUID();
277
278         final ResponseEntity<RequestError> actual = restTemplate.exchange(invalidServiceInstanceUrl, HttpMethod.GET,
279                 new HttpEntity<>(getHttpHeaders()), RequestError.class);
280
281         assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
282
283         final RequestError actualError = actual.getBody();
284         final ServiceException serviceException = actualError.getServiceException();
285
286         assertNotNull(serviceException);
287         assertEquals(RequestErrorResponseUtils.ERROR_MESSAGE_ID, serviceException.getMessageId());
288         assertEquals(RequestErrorResponseUtils.ERROR_MESSAGE, serviceException.getText());
289         assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
290
291     }
292
293     @Test
294     public void test_getSericeInstance_usingInvalidServiceInstanceName_shouldReturnError() throws Exception {
295
296         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
297
298         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
299
300         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
301
302         final ResponseEntity<Void> putRequestReponse = invokeHttpPut(url, getServiceInstance());
303         assertEquals(HttpStatus.ACCEPTED, putRequestReponse.getStatusCode());
304
305
306         final String serviceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCES_URL
307                 + "?service-instance-name=Dummy&depth=2";
308
309         final ResponseEntity<RequestError> actual = restTemplate.exchange(serviceInstanceUrl, HttpMethod.GET,
310                 new HttpEntity<>(getHttpHeaders()), RequestError.class);
311
312         assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
313
314         final RequestError actualError = actual.getBody();
315         final ServiceException serviceException = actualError.getServiceException();
316
317         assertNotNull(serviceException);
318         assertEquals(RequestErrorResponseUtils.ERROR_MESSAGE_ID, serviceException.getMessageId());
319         assertEquals(RequestErrorResponseUtils.ERROR_MESSAGE, serviceException.getText());
320         assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
321
322     }
323
324     @Test
325     public void test_PathSericeInstance_usingServiceInstanceId_OrchStatusChangedInCache() throws Exception {
326
327         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
328
329         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
330
331         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
332
333         final ResponseEntity<Void> serviceInstancePutResponse = invokeHttpPut(url, getServiceInstance());
334         assertEquals(HttpStatus.ACCEPTED, serviceInstancePutResponse.getStatusCode());
335
336         final HttpHeaders httpHeaders = getHttpHeaders();
337         httpHeaders.add(X_HTTP_METHOD_OVERRIDE, HttpMethod.PATCH.toString());
338
339         final HttpEntity<?> orchStatuUpdateServiceInstance =
340                 getHttpEntity(getOrchStatuUpdateServiceInstance(), httpHeaders);
341
342         final ResponseEntity<Void> orchStatuUpdateServiceInstanceResponse =
343                 invokeHttpPost(orchStatuUpdateServiceInstance, url, getOrchStatuUpdateServiceInstance());
344
345         assertEquals(HttpStatus.ACCEPTED, orchStatuUpdateServiceInstanceResponse.getStatusCode());
346
347
348         final ResponseEntity<ServiceInstance> actual =
349                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), ServiceInstance.class);
350
351         assertEquals(HttpStatus.OK, actual.getStatusCode());
352         assertTrue(actual.hasBody());
353
354         final ServiceInstance actualServiceInstance = actual.getBody();
355
356         assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
357         assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
358         assertEquals(ORCHESTRATION_STATUS, actualServiceInstance.getOrchestrationStatus());
359
360     }
361
362     @Test
363     public void test_putServiceSubscription_successfullyAddedToCache() throws Exception {
364         final String serviceSubscriptionurl =
365                 getCustomerEndPointUrl() + "/service-subscriptions/service-subscription/" + FIREWALL_SERVICE_TTYPE;
366
367         final ResponseEntity<Void> customerPutResponse = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
368         assertEquals(HttpStatus.ACCEPTED, customerPutResponse.getStatusCode());
369
370         final ResponseEntity<Void> serviceSubscriptionPutResponse =
371                 invokeHttpPut(serviceSubscriptionurl, getServiceSubscription());
372         assertEquals(HttpStatus.ACCEPTED, serviceSubscriptionPutResponse.getStatusCode());
373
374         final ResponseEntity<ServiceSubscription> actual = restTemplate.exchange(serviceSubscriptionurl, HttpMethod.GET,
375                 new HttpEntity<>(getHttpHeaders()), ServiceSubscription.class);
376
377         assertEquals(HttpStatus.OK, actual.getStatusCode());
378         assertTrue(actual.hasBody());
379
380         final ServiceSubscription actualServiceSubscription = actual.getBody();
381         assertEquals(FIREWALL_SERVICE_TTYPE, actualServiceSubscription.getServiceType());
382
383     }
384
385     @Test
386     public void test_putSericeInstanceRelatedTo_ableToRetrieveServiceInstanceFromCache() throws Exception {
387
388         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
389
390         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
391
392         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
393
394         final ResponseEntity<Void> responseEntity = invokeHttpPut(url, getServiceInstance());
395         assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
396
397         final String relationShipUrl =
398                 getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL + RELATIONSHIP_URL;
399
400
401         final HttpEntity<?> httpEntity = getHttpEntity(getRelationShipJsonObject());
402         final ResponseEntity<Relationship> responseEntity2 =
403                 restTemplate.exchange(relationShipUrl, HttpMethod.PUT, httpEntity, Relationship.class);
404         assertEquals(HttpStatus.ACCEPTED, responseEntity2.getStatusCode());
405
406         final String genericVnfUrl = TestUtils.getBaseUrl(port) + GENERIC_VNF_URL + VNF_ID;
407         final ResponseEntity<Void> genericVnfResponse = invokeHttpPut(genericVnfUrl, getGenericVnf());
408         assertEquals(HttpStatus.ACCEPTED, genericVnfResponse.getStatusCode());
409
410
411         final ResponseEntity<GenericVnfs> actual =
412                 restTemplate.exchange(url + RELATED_TO_URL + "?vnf-name=" + GENERIC_VNF_NAME, HttpMethod.GET,
413                         new HttpEntity<>(getHttpHeaders()), GenericVnfs.class);
414
415         assertEquals(HttpStatus.OK, actual.getStatusCode());
416         
417         assertTrue(actual.hasBody());
418         final GenericVnfs genericVnfs = actual.getBody();
419         assertFalse(genericVnfs.getGenericVnf().isEmpty());
420         final GenericVnf genericVnf = genericVnfs.getGenericVnf().get(0);
421         assertEquals(GENERIC_VNF_NAME, genericVnf.getVnfName());
422
423
424     }
425
426     private String getCustomer() throws Exception, IOException {
427         return getJsonString("test-data/business-customer.json");
428     }
429
430     private String getServiceSubscription() throws Exception, IOException {
431         return getJsonString("test-data/service-subscription.json");
432     }
433
434
435     private String getCustomerEndPointUrl() {
436         return TestUtils.getBaseUrl(port) + CUSTOMERS_URL;
437     }
438
439     private ResponseEntity<Void> invokeHttpPut(final String url, final Object obj) {
440         final HttpEntity<?> httpEntity = getHttpEntity(obj);
441         return restTemplate.exchange(url, HttpMethod.PUT, httpEntity, Void.class);
442     }
443
444     private ResponseEntity<Void> invokeHttpPost(final HttpEntity<?> httpEntity, final String url, final Object obj) {
445         return restTemplate.exchange(url, HttpMethod.POST, httpEntity, Void.class);
446     }
447
448     private HttpEntity<?> getHttpEntity(final Object obj) {
449         return new HttpEntity<>(obj, getHttpHeaders());
450     }
451
452     private HttpEntity<?> getHttpEntity(final Object obj, final HttpHeaders headers) {
453         return new HttpEntity<>(obj, headers);
454     }
455
456     private HttpHeaders getHttpHeaders() {
457         return TestUtils.getHttpHeaders(userCredentials.getUsers().iterator().next().getUsername());
458     }
459
460     private String getServiceInstance() throws Exception, IOException {
461         return getJsonString("test-data/service-instance.json");
462     }
463
464     private String getOrchStatuUpdateServiceInstance() throws Exception, IOException {
465         return getJsonString("test-data/service-instance-orch-status-update.json");
466     }
467
468     private String getRelationShipJsonObject() throws IOException {
469         return getJsonString("test-data/service-Instance-relationShip.json");
470     }
471     
472     private String getGenericVnf() throws IOException {
473         return getJsonString("test-data/generic-vnf.json");
474     }
475
476 }