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
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.so.aai.simulator.controller;
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.aai.simulator.utils.Constants.RESOURCE_LINK;
27 import static org.onap.so.aai.simulator.utils.Constants.RESOURCE_TYPE;
28 import static org.onap.so.aai.simulator.utils.Constants.SERVICE_RESOURCE_TYPE;
29 import static org.onap.so.aai.simulator.utils.TestConstants.CUSTOMERS_URL;
30 import static org.onap.so.aai.simulator.utils.TestConstants.SERVICE_INSTANCE_ID;
31 import static org.onap.so.aai.simulator.utils.TestConstants.SERVICE_INSTANCE_URL;
32 import static org.onap.so.aai.simulator.utils.TestConstants.SERVICE_NAME;
33 import static org.onap.so.aai.simulator.utils.TestConstants.SERVICE_SUBSCRIPTIONS_URL;
34 import static org.onap.so.aai.simulator.utils.TestUtils.getJsonString;
35 import java.io.IOException;
37 import org.junit.After;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.onap.aai.domain.yang.ServiceInstance;
41 import org.onap.so.aai.simulator.models.Format;
42 import org.onap.so.aai.simulator.models.Result;
43 import org.onap.so.aai.simulator.service.providers.CustomerCacheServiceProvider;
44 import org.onap.so.aai.simulator.service.providers.NodesCacheServiceProvider;
45 import org.onap.so.aai.simulator.utils.Constants;
46 import org.onap.so.aai.simulator.utils.TestUtils;
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.beans.factory.annotation.Value;
49 import org.springframework.boot.test.context.SpringBootTest;
50 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
51 import org.springframework.boot.test.web.client.TestRestTemplate;
52 import org.springframework.boot.web.server.LocalServerPort;
53 import org.springframework.context.annotation.Configuration;
54 import org.springframework.http.HttpEntity;
55 import org.springframework.http.HttpHeaders;
56 import org.springframework.http.HttpMethod;
57 import org.springframework.http.HttpStatus;
58 import org.springframework.http.ResponseEntity;
59 import org.springframework.test.context.ActiveProfiles;
60 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
63 * @author waqas.ikram@ericsson.com
66 @RunWith(SpringJUnit4ClassRunner.class)
67 @ActiveProfiles("test")
68 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
70 public class NodesControllerTest {
76 private TestRestTemplate restTemplate;
78 @Value("${spring.security.username}")
79 private String username;
82 private NodesCacheServiceProvider nodesCacheServiceProvider;
85 private CustomerCacheServiceProvider customerCacheServiceProvider;
89 nodesCacheServiceProvider.clearAll();
90 customerCacheServiceProvider.clearAll();
94 public void test_getNodesSericeInstance_usingServiceInstanceId_ableToRetrieveServiceInstanceFromCache()
97 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
99 final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
101 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
103 final ResponseEntity<Void> response2 = invokeHttpPut(url, getServiceInstance());
104 assertEquals(HttpStatus.ACCEPTED, response2.getStatusCode());
106 final ResponseEntity<ServiceInstance> actual =
107 restTemplate.exchange(getNodesEndPointUrl() + SERVICE_INSTANCE_URL, HttpMethod.GET,
108 new HttpEntity<>(getHttpHeaders()), ServiceInstance.class);
110 assertEquals(HttpStatus.OK, actual.getStatusCode());
111 assertTrue(actual.hasBody());
113 final ServiceInstance actualServiceInstance = actual.getBody();
115 assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
116 assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
121 public void test_getNodesSericeInstance_usingServiceInstanceIdAndFormatPathed_ableToRetrieveServiceInstanceFromCache()
124 final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
126 final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
128 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
130 final ResponseEntity<Void> response2 = invokeHttpPut(url, getServiceInstance());
131 assertEquals(HttpStatus.ACCEPTED, response2.getStatusCode());
133 final ResponseEntity<Result> actual = restTemplate.exchange(
134 getNodesEndPointUrl() + SERVICE_INSTANCE_URL + "?format=" + Format.PATHED.getValue(), HttpMethod.GET,
135 new HttpEntity<>(getHttpHeaders()), Result.class);
137 assertEquals(HttpStatus.OK, actual.getStatusCode());
138 assertTrue(actual.hasBody());
140 final Result result = actual.getBody();
142 assertNotNull(result.getValues());
143 assertFalse(result.getValues().isEmpty());
144 final Map<String, Object> actualMap = result.getValues().get(0);
146 assertEquals(CUSTOMERS_URL + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL, actualMap.get(RESOURCE_LINK));
147 assertEquals(SERVICE_RESOURCE_TYPE, actualMap.get(RESOURCE_TYPE));
151 private String getNodesEndPointUrl() {
152 return TestUtils.getBaseUrl(port) + Constants.NODES_URL;
156 private String getCustomerEndPointUrl() {
157 return TestUtils.getBaseUrl(port) + CUSTOMERS_URL;
160 private String getCustomer() throws Exception, IOException {
161 return getJsonString("test-data/business-customer.json");
164 private String getServiceInstance() throws Exception, IOException {
165 return getJsonString("test-data/service-instance.json");
168 private ResponseEntity<Void> invokeHttpPut(final String url, final Object obj) {
169 final HttpEntity<?> httpEntity = getHttpEntity(obj);
170 return restTemplate.exchange(url, HttpMethod.PUT, httpEntity, Void.class);
173 private HttpEntity<?> getHttpEntity(final Object obj) {
174 return new HttpEntity<>(obj, getHttpHeaders());
177 private HttpHeaders getHttpHeaders() {
178 return TestUtils.getHttpHeaders(username);