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.aaisimulator.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.aaisimulator.utils.Constants.NODES_URL;
27 import static org.onap.so.aaisimulator.utils.Constants.RESOURCE_LINK;
28 import static org.onap.so.aaisimulator.utils.Constants.RESOURCE_TYPE;
29 import static org.onap.so.aaisimulator.utils.Constants.SERVICE_RESOURCE_TYPE;
30 import static org.onap.so.aaisimulator.utils.TestConstants.CUSTOMERS_URL;
31 import static org.onap.so.aaisimulator.utils.TestConstants.GENERIC_VNFS_URL;
32 import static org.onap.so.aaisimulator.utils.TestConstants.GENERIC_VNF_NAME;
33 import static org.onap.so.aaisimulator.utils.TestConstants.GENERIC_VNF_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.VNF_ID;
39 import java.io.IOException;
41 import org.junit.After;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.onap.aai.domain.yang.GenericVnf;
45 import org.onap.aai.domain.yang.GenericVnfs;
46 import org.onap.aai.domain.yang.ServiceInstance;
47 import org.onap.so.aaisimulator.models.Format;
48 import org.onap.so.aaisimulator.models.Results;
49 import org.onap.so.aaisimulator.service.providers.CustomerCacheServiceProvider;
50 import org.onap.so.aaisimulator.service.providers.NodesCacheServiceProvider;
51 import org.onap.so.aaisimulator.utils.Constants;
52 import org.onap.so.aaisimulator.utils.TestUtils;
53 import org.onap.so.simulator.model.UserCredentials;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.boot.test.context.SpringBootTest;
56 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
57 import org.springframework.boot.test.web.client.TestRestTemplate;
58 import org.springframework.boot.web.server.LocalServerPort;
59 import org.springframework.context.annotation.Configuration;
60 import org.springframework.http.HttpEntity;
61 import org.springframework.http.HttpHeaders;
62 import org.springframework.http.HttpMethod;
63 import org.springframework.http.HttpStatus;
64 import org.springframework.http.ResponseEntity;
65 import org.springframework.test.context.ActiveProfiles;
66 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
69 * @author waqas.ikram@ericsson.com
72 @RunWith(SpringJUnit4ClassRunner.class)
73 @ActiveProfiles("test")
74 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
76 public class NodesControllerTest {
82 private TestRestTemplate restTemplate;
85 private UserCredentials userCredentials;
88 private NodesCacheServiceProvider nodesCacheServiceProvider;
91 private CustomerCacheServiceProvider customerCacheServiceProvider;
95 nodesCacheServiceProvider.clearAll();
96 customerCacheServiceProvider.clearAll();
100 public void test_getNodesSericeInstance_usingServiceInstanceId_ableToRetrieveServiceInstanceFromCache()
103 invokeCustomerandServiceInstanceUrls();
105 final ResponseEntity<ServiceInstance> actual =
106 restTemplate.exchange(getUrl(Constants.NODES_URL, SERVICE_INSTANCE_URL), HttpMethod.GET,
107 new HttpEntity<>(getHttpHeaders()), ServiceInstance.class);
109 assertEquals(HttpStatus.OK, actual.getStatusCode());
110 assertTrue(actual.hasBody());
112 final ServiceInstance actualServiceInstance = actual.getBody();
114 assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
115 assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
120 public void test_getNodesSericeInstance_usingServiceInstanceIdAndFormatPathed_ableToRetrieveServiceInstanceFromCache()
123 invokeCustomerandServiceInstanceUrls();
125 final ResponseEntity<Results> actual = restTemplate.exchange(
126 getUrl(Constants.NODES_URL, SERVICE_INSTANCE_URL) + "?format=" + Format.PATHED.getValue(),
127 HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), Results.class);
129 assertEquals(HttpStatus.OK, actual.getStatusCode());
130 assertTrue(actual.hasBody());
132 final Results result = actual.getBody();
134 assertNotNull(result.getValues());
135 assertFalse(result.getValues().isEmpty());
136 final Map<String, Object> actualMap = result.getValues().get(0);
138 assertEquals(CUSTOMERS_URL + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL, actualMap.get(RESOURCE_LINK));
139 assertEquals(SERVICE_RESOURCE_TYPE, actualMap.get(RESOURCE_TYPE));
144 public void test_getNodesGenericVnfs_usingVnfName_ableToRetrieveItFromCache() throws Exception {
145 invokeCustomerandServiceInstanceUrls();
147 final String genericVnfUrl = getUrl(GENERIC_VNF_URL, VNF_ID);
148 final ResponseEntity<Void> genericVnfResponse = invokeHttpPut(genericVnfUrl, TestUtils.getGenericVnf());
149 assertEquals(HttpStatus.ACCEPTED, genericVnfResponse.getStatusCode());
151 final String nodeGenericVnfsUrl = getUrl(NODES_URL, GENERIC_VNFS_URL) + "?vnf-name=" + GENERIC_VNF_NAME;
152 final ResponseEntity<GenericVnfs> actual = restTemplate.exchange(nodeGenericVnfsUrl, HttpMethod.GET,
153 new HttpEntity<>(getHttpHeaders()), GenericVnfs.class);
155 assertEquals(HttpStatus.OK, actual.getStatusCode());
156 assertTrue(actual.hasBody());
158 final GenericVnfs genericVnfs = actual.getBody();
159 assertEquals(1, genericVnfs.getGenericVnf().size());
161 final GenericVnf genericVnf = genericVnfs.getGenericVnf().get(0);
162 assertEquals(GENERIC_VNF_NAME, genericVnf.getVnfName());
163 assertEquals(VNF_ID, genericVnf.getVnfId());
167 private void invokeCustomerandServiceInstanceUrls() throws Exception, IOException {
168 final String url = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL);
170 final ResponseEntity<Void> response = invokeHttpPut(getUrl(CUSTOMERS_URL), TestUtils.getCustomer());
172 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
174 final ResponseEntity<Void> response2 = invokeHttpPut(url, TestUtils.getServiceInstance());
175 assertEquals(HttpStatus.ACCEPTED, response2.getStatusCode());
178 private String getUrl(final String... urls) {
179 return TestUtils.getUrl(port, urls);
182 private ResponseEntity<Void> invokeHttpPut(final String url, final Object obj) {
183 final HttpEntity<?> httpEntity = getHttpEntity(obj);
184 return restTemplate.exchange(url, HttpMethod.PUT, httpEntity, Void.class);
187 private HttpEntity<?> getHttpEntity(final Object obj) {
188 return new HttpEntity<>(obj, getHttpHeaders());
191 private HttpHeaders getHttpHeaders() {
192 return TestUtils.getHttpHeaders(userCredentials.getUsers().iterator().next().getUsername());