Update AAI-Simulator Support for Service Level Upgrade
[integration/csit.git] / plans / usecases-pnf-sw-upgrade / pnf-sw-upgrade / sorch / simulator / aai-simulator / src / test / java / org / onap / aaisimulator / controller / BusinessControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 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.aaisimulator.controller;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
24 import static org.onap.aaisimulator.utils.Constants.X_HTTP_METHOD_OVERRIDE;
25 import static org.onap.aaisimulator.utils.TestConstants.CUSTOMER_BASE_URL;
26 import static org.onap.aaisimulator.utils.TestConstants.SVC_INSTANCE_CUSTOMER_ID;
27 import static org.onap.aaisimulator.utils.TestConstants.SVC_INSTANCE_CUSTOMER_NAME;
28 import static org.onap.aaisimulator.utils.TestConstants.SVC_INSTANCE_URL;
29 import static org.onap.aaisimulator.utils.TestConstants.SVC_SUBSCRIPTIONS_URL;
30 import static org.onap.aaisimulator.utils.TestUtils.getSvcInstance;
31
32 import java.io.IOException;
33 import org.junit.After;
34 import org.junit.Test;
35 import org.onap.aai.domain.yang.ServiceInstance;
36 import org.onap.aaisimulator.service.providers.CustomerCacheServiceProvider;
37 import org.onap.aaisimulator.utils.Constants;
38 import org.onap.aaisimulator.utils.TestUtils;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.http.HttpHeaders;
41 import org.springframework.http.HttpMethod;
42 import org.springframework.http.HttpStatus;
43 import org.springframework.http.ResponseEntity;
44
45 public class BusinessControllerTest extends AbstractSpringBootTest {
46
47     @Autowired
48     private CustomerCacheServiceProvider cacheServiceProvider;
49
50     @After
51     public void after() {
52         cacheServiceProvider.clearAll();
53     }
54
55     @Test
56     public void test_getSvcInstance_usingServiceInstanceId_fromCache() throws Exception {
57         final String url = getUrl(CUSTOMER_BASE_URL, SVC_SUBSCRIPTIONS_URL, SVC_INSTANCE_URL);
58
59         final ResponseEntity<Void> responseEntity = testRestTemplateService
60             .invokeHttpPut(url, getSvcInstance(), Void.class);
61
62         assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
63
64         final ResponseEntity<ServiceInstance> actual = testRestTemplateService
65             .invokeHttpGet(url, ServiceInstance.class);
66
67         assertEquals(HttpStatus.OK, actual.getStatusCode());
68         assertTrue(actual.hasBody());
69
70         final ServiceInstance actualServiceInstance = actual.getBody();
71
72         assertEquals(SVC_INSTANCE_CUSTOMER_NAME, actualServiceInstance.getServiceInstanceName());
73         assertEquals(SVC_INSTANCE_CUSTOMER_ID, actualServiceInstance.getServiceInstanceId());
74     }
75
76     @Test
77     public void test_postForServiceInstanceId_fromCache() throws Exception {
78         addServiceInstnceToCache();
79         final HttpHeaders httpHeaders = testRestTemplateService.getHttpHeaders();
80         httpHeaders.add(X_HTTP_METHOD_OVERRIDE, HttpMethod.PATCH.toString());
81         httpHeaders.remove(HttpHeaders.CONTENT_TYPE);
82         httpHeaders.add(HttpHeaders.CONTENT_TYPE, Constants.APPLICATION_MERGE_PATCH_JSON);
83
84         final String svcInstanceUrl = getUrl(CUSTOMER_BASE_URL, SVC_SUBSCRIPTIONS_URL, SVC_INSTANCE_URL);
85         final ResponseEntity<Void> postServiceInstanceResponse = testRestTemplateService
86             .invokeHttpPost(httpHeaders, svcInstanceUrl, TestUtils.getSvcInstance(), Void.class);
87
88         assertEquals(HttpStatus.ACCEPTED, postServiceInstanceResponse.getStatusCode());
89
90         final ResponseEntity<ServiceInstance> response =
91             testRestTemplateService.invokeHttpGet(svcInstanceUrl, ServiceInstance.class);
92         assertEquals(HttpStatus.OK, response.getStatusCode());
93
94         assertTrue(response.hasBody());
95
96         final ServiceInstance actualServiceInstance = response.getBody();
97
98         assertEquals(SVC_INSTANCE_CUSTOMER_NAME, actualServiceInstance.getServiceInstanceName());
99         assertEquals(SVC_INSTANCE_CUSTOMER_ID, actualServiceInstance.getServiceInstanceId());
100
101     }
102
103     private void addServiceInstnceToCache() throws Exception, IOException {
104         final ResponseEntity<Void> serviceInstanceResponse =
105             testRestTemplateService.invokeHttpPut(getUrl(CUSTOMER_BASE_URL, SVC_SUBSCRIPTIONS_URL, SVC_INSTANCE_URL),
106                 TestUtils.getSvcInstance(), Void.class);
107         assertEquals(HttpStatus.ACCEPTED, serviceInstanceResponse.getStatusCode());
108     }
109 }