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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.aaisimulator.controller;
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;
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;
45 public class BusinessControllerTest extends AbstractSpringBootTest {
48 private CustomerCacheServiceProvider cacheServiceProvider;
52 cacheServiceProvider.clearAll();
56 public void test_getSvcInstance_usingServiceInstanceId_fromCache() throws Exception {
57 final String url = getUrl(CUSTOMER_BASE_URL, SVC_SUBSCRIPTIONS_URL, SVC_INSTANCE_URL);
59 final ResponseEntity<Void> responseEntity = testRestTemplateService
60 .invokeHttpPut(url, getSvcInstance(), Void.class);
62 assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
64 final ResponseEntity<ServiceInstance> actual = testRestTemplateService
65 .invokeHttpGet(url, ServiceInstance.class);
67 assertEquals(HttpStatus.OK, actual.getStatusCode());
68 assertTrue(actual.hasBody());
70 final ServiceInstance actualServiceInstance = actual.getBody();
72 assertEquals(SVC_INSTANCE_CUSTOMER_NAME, actualServiceInstance.getServiceInstanceName());
73 assertEquals(SVC_INSTANCE_CUSTOMER_ID, actualServiceInstance.getServiceInstanceId());
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);
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);
88 assertEquals(HttpStatus.ACCEPTED, postServiceInstanceResponse.getStatusCode());
90 final ResponseEntity<ServiceInstance> response =
91 testRestTemplateService.invokeHttpGet(svcInstanceUrl, ServiceInstance.class);
92 assertEquals(HttpStatus.OK, response.getStatusCode());
94 assertTrue(response.hasBody());
96 final ServiceInstance actualServiceInstance = response.getBody();
98 assertEquals(SVC_INSTANCE_CUSTOMER_NAME, actualServiceInstance.getServiceInstanceName());
99 assertEquals(SVC_INSTANCE_CUSTOMER_ID, actualServiceInstance.getServiceInstanceId());
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());