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.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.aaisimulator.utils.Constants.RESOURCE_LINK;
27 import static org.onap.aaisimulator.utils.Constants.RESOURCE_TYPE;
28 import static org.onap.aaisimulator.utils.Constants.SERVICE_RESOURCE_TYPE;
29 import static org.onap.aaisimulator.utils.TestConstants.CUSTOMERS_URL;
30 import static org.onap.aaisimulator.utils.TestConstants.GENERIC_VNFS_URL;
31 import static org.onap.aaisimulator.utils.TestConstants.GENERIC_VNF_NAME;
32 import static org.onap.aaisimulator.utils.TestConstants.GENERIC_VNF_URL;
33 import static org.onap.aaisimulator.utils.TestConstants.SERVICE_INSTANCE_ID;
34 import static org.onap.aaisimulator.utils.TestConstants.SERVICE_INSTANCE_URL;
35 import static org.onap.aaisimulator.utils.TestConstants.SERVICE_NAME;
36 import static org.onap.aaisimulator.utils.TestConstants.SERVICE_SUBSCRIPTIONS_URL;
37 import static org.onap.aaisimulator.utils.TestConstants.VNF_ID;
38 import java.io.IOException;
40 import org.junit.After;
41 import org.junit.Test;
42 import org.onap.aai.domain.yang.GenericVnf;
43 import org.onap.aai.domain.yang.GenericVnfs;
44 import org.onap.aai.domain.yang.ServiceInstance;
45 import org.onap.aaisimulator.models.Format;
46 import org.onap.aaisimulator.models.Results;
47 import org.onap.aaisimulator.service.providers.CustomerCacheServiceProvider;
48 import org.onap.aaisimulator.service.providers.NodesCacheServiceProvider;
49 import org.onap.aaisimulator.utils.TestConstants;
50 import org.onap.aaisimulator.utils.TestUtils;
51 import org.springframework.beans.factory.annotation.Autowired;
52 import org.springframework.http.HttpStatus;
53 import org.springframework.http.ResponseEntity;
56 * @author waqas.ikram@ericsson.com
59 public class NodesControllerTest extends AbstractSpringBootTest {
62 private NodesCacheServiceProvider nodesCacheServiceProvider;
65 private CustomerCacheServiceProvider customerCacheServiceProvider;
69 nodesCacheServiceProvider.clearAll();
70 customerCacheServiceProvider.clearAll();
74 public void test_getNodesSericeInstance_usingServiceInstanceId_ableToRetrieveServiceInstanceFromCache()
77 invokeCustomerandServiceInstanceUrls();
79 final ResponseEntity<ServiceInstance> actual = testRestTemplateService
80 .invokeHttpGet(getUrl(TestConstants.NODES_URL, SERVICE_INSTANCE_URL), ServiceInstance.class);
82 assertEquals(HttpStatus.OK, actual.getStatusCode());
83 assertTrue(actual.hasBody());
85 final ServiceInstance actualServiceInstance = actual.getBody();
87 assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
88 assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
93 public void test_getNodesSericeInstance_usingServiceInstanceIdAndFormatPathed_ableToRetrieveServiceInstanceFromCache()
96 invokeCustomerandServiceInstanceUrls();
98 final ResponseEntity<Results> actual = testRestTemplateService.invokeHttpGet(
99 getUrl(TestConstants.NODES_URL, SERVICE_INSTANCE_URL) + "?format=" + Format.PATHED.getValue(),
102 assertEquals(HttpStatus.OK, actual.getStatusCode());
103 assertTrue(actual.hasBody());
105 final Results result = actual.getBody();
107 assertNotNull(result.getValues());
108 assertFalse(result.getValues().isEmpty());
109 final Map<String, Object> actualMap = result.getValues().get(0);
111 assertEquals(CUSTOMERS_URL + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL, actualMap.get(RESOURCE_LINK));
112 assertEquals(SERVICE_RESOURCE_TYPE, actualMap.get(RESOURCE_TYPE));
117 public void test_getNodesGenericVnfs_usingVnfName_ableToRetrieveItFromCache() throws Exception {
118 invokeCustomerandServiceInstanceUrls();
120 final String genericVnfUrl = getUrl(GENERIC_VNF_URL, VNF_ID);
121 final ResponseEntity<Void> genericVnfResponse =
122 testRestTemplateService.invokeHttpPut(genericVnfUrl, TestUtils.getGenericVnf(), Void.class);
123 assertEquals(HttpStatus.ACCEPTED, genericVnfResponse.getStatusCode());
125 final String nodeGenericVnfsUrl =
126 getUrl(TestConstants.NODES_URL, GENERIC_VNFS_URL) + "?vnf-name=" + GENERIC_VNF_NAME;
128 final ResponseEntity<GenericVnfs> actual =
129 testRestTemplateService.invokeHttpGet(nodeGenericVnfsUrl, GenericVnfs.class);
131 assertEquals(HttpStatus.OK, actual.getStatusCode());
132 assertTrue(actual.hasBody());
134 final GenericVnfs genericVnfs = actual.getBody();
135 assertEquals(1, genericVnfs.getGenericVnf().size());
137 final GenericVnf genericVnf = genericVnfs.getGenericVnf().get(0);
138 assertEquals(GENERIC_VNF_NAME, genericVnf.getVnfName());
139 assertEquals(VNF_ID, genericVnf.getVnfId());
143 private void invokeCustomerandServiceInstanceUrls() throws Exception, IOException {
144 final String url = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL);
146 final ResponseEntity<Void> response =
147 testRestTemplateService.invokeHttpPut(getUrl(CUSTOMERS_URL), TestUtils.getCustomer(), Void.class);
149 assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
151 final ResponseEntity<Void> response2 =
152 testRestTemplateService.invokeHttpPut(url, TestUtils.getServiceInstance(), Void.class);
153 assertEquals(HttpStatus.ACCEPTED, response2.getStatusCode());