Adding aai line of business endpoints
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / test / java / org / onap / so / aaisimulator / controller / NodesControllerTest.java
1 /*-
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
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.so.aaisimulator.controller;
21
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;
40 import java.util.Map;
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.TestRestTemplateService;
53 import org.onap.so.aaisimulator.utils.TestUtils;
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.web.server.LocalServerPort;
58 import org.springframework.context.annotation.Configuration;
59 import org.springframework.http.HttpStatus;
60 import org.springframework.http.ResponseEntity;
61 import org.springframework.test.context.ActiveProfiles;
62 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
63
64 /**
65  * @author waqas.ikram@ericsson.com
66  *
67  */
68 @RunWith(SpringJUnit4ClassRunner.class)
69 @ActiveProfiles("test")
70 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
71 @Configuration
72 public class NodesControllerTest {
73
74     @LocalServerPort
75     private int port;
76
77     @Autowired
78     private TestRestTemplateService testRestTemplateService;
79
80     @Autowired
81     private NodesCacheServiceProvider nodesCacheServiceProvider;
82
83     @Autowired
84     private CustomerCacheServiceProvider customerCacheServiceProvider;
85
86     @After
87     public void after() {
88         nodesCacheServiceProvider.clearAll();
89         customerCacheServiceProvider.clearAll();
90     }
91
92     @Test
93     public void test_getNodesSericeInstance_usingServiceInstanceId_ableToRetrieveServiceInstanceFromCache()
94             throws Exception {
95
96         invokeCustomerandServiceInstanceUrls();
97
98         final ResponseEntity<ServiceInstance> actual = testRestTemplateService
99                 .invokeHttpGet(getUrl(Constants.NODES_URL, SERVICE_INSTANCE_URL), ServiceInstance.class);
100
101         assertEquals(HttpStatus.OK, actual.getStatusCode());
102         assertTrue(actual.hasBody());
103
104         final ServiceInstance actualServiceInstance = actual.getBody();
105
106         assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
107         assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
108
109     }
110
111     @Test
112     public void test_getNodesSericeInstance_usingServiceInstanceIdAndFormatPathed_ableToRetrieveServiceInstanceFromCache()
113             throws Exception {
114
115         invokeCustomerandServiceInstanceUrls();
116
117         final ResponseEntity<Results> actual = testRestTemplateService.invokeHttpGet(
118                 getUrl(Constants.NODES_URL, SERVICE_INSTANCE_URL) + "?format=" + Format.PATHED.getValue(),
119                 Results.class);
120
121         assertEquals(HttpStatus.OK, actual.getStatusCode());
122         assertTrue(actual.hasBody());
123
124         final Results result = actual.getBody();
125
126         assertNotNull(result.getValues());
127         assertFalse(result.getValues().isEmpty());
128         final Map<String, Object> actualMap = result.getValues().get(0);
129
130         assertEquals(CUSTOMERS_URL + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL, actualMap.get(RESOURCE_LINK));
131         assertEquals(SERVICE_RESOURCE_TYPE, actualMap.get(RESOURCE_TYPE));
132
133     }
134
135     @Test
136     public void test_getNodesGenericVnfs_usingVnfName_ableToRetrieveItFromCache() throws Exception {
137         invokeCustomerandServiceInstanceUrls();
138
139         final String genericVnfUrl = getUrl(GENERIC_VNF_URL, VNF_ID);
140         final ResponseEntity<Void> genericVnfResponse =
141                 testRestTemplateService.invokeHttpPut(genericVnfUrl, TestUtils.getGenericVnf(), Void.class);
142         assertEquals(HttpStatus.ACCEPTED, genericVnfResponse.getStatusCode());
143
144         final String nodeGenericVnfsUrl = getUrl(NODES_URL, GENERIC_VNFS_URL) + "?vnf-name=" + GENERIC_VNF_NAME;
145
146         final ResponseEntity<GenericVnfs> actual =
147                 testRestTemplateService.invokeHttpGet(nodeGenericVnfsUrl, GenericVnfs.class);
148
149         assertEquals(HttpStatus.OK, actual.getStatusCode());
150         assertTrue(actual.hasBody());
151
152         final GenericVnfs genericVnfs = actual.getBody();
153         assertEquals(1, genericVnfs.getGenericVnf().size());
154
155         final GenericVnf genericVnf = genericVnfs.getGenericVnf().get(0);
156         assertEquals(GENERIC_VNF_NAME, genericVnf.getVnfName());
157         assertEquals(VNF_ID, genericVnf.getVnfId());
158
159     }
160
161     private void invokeCustomerandServiceInstanceUrls() throws Exception, IOException {
162         final String url = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL);
163
164         final ResponseEntity<Void> response =
165                 testRestTemplateService.invokeHttpPut(getUrl(CUSTOMERS_URL), TestUtils.getCustomer(), Void.class);
166
167         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
168
169         final ResponseEntity<Void> response2 =
170                 testRestTemplateService.invokeHttpPut(url, TestUtils.getServiceInstance(), Void.class);
171         assertEquals(HttpStatus.ACCEPTED, response2.getStatusCode());
172     }
173
174     private String getUrl(final String... urls) {
175         return TestUtils.getUrl(port, urls);
176     }
177
178 }