Adding aai platform 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.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;
67
68 /**
69  * @author waqas.ikram@ericsson.com
70  *
71  */
72 @RunWith(SpringJUnit4ClassRunner.class)
73 @ActiveProfiles("test")
74 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
75 @Configuration
76 public class NodesControllerTest {
77
78     @LocalServerPort
79     private int port;
80
81     @Autowired
82     private TestRestTemplate restTemplate;
83
84     @Autowired
85     private UserCredentials userCredentials;
86
87     @Autowired
88     private NodesCacheServiceProvider nodesCacheServiceProvider;
89
90     @Autowired
91     private CustomerCacheServiceProvider customerCacheServiceProvider;
92
93     @After
94     public void after() {
95         nodesCacheServiceProvider.clearAll();
96         customerCacheServiceProvider.clearAll();
97     }
98
99     @Test
100     public void test_getNodesSericeInstance_usingServiceInstanceId_ableToRetrieveServiceInstanceFromCache()
101             throws Exception {
102
103         invokeCustomerandServiceInstanceUrls();
104
105         final ResponseEntity<ServiceInstance> actual =
106                 restTemplate.exchange(getUrl(Constants.NODES_URL, SERVICE_INSTANCE_URL), HttpMethod.GET,
107                         new HttpEntity<>(getHttpHeaders()), ServiceInstance.class);
108
109         assertEquals(HttpStatus.OK, actual.getStatusCode());
110         assertTrue(actual.hasBody());
111
112         final ServiceInstance actualServiceInstance = actual.getBody();
113
114         assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
115         assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
116
117     }
118
119     @Test
120     public void test_getNodesSericeInstance_usingServiceInstanceIdAndFormatPathed_ableToRetrieveServiceInstanceFromCache()
121             throws Exception {
122
123         invokeCustomerandServiceInstanceUrls();
124
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);
128
129         assertEquals(HttpStatus.OK, actual.getStatusCode());
130         assertTrue(actual.hasBody());
131
132         final Results result = actual.getBody();
133
134         assertNotNull(result.getValues());
135         assertFalse(result.getValues().isEmpty());
136         final Map<String, Object> actualMap = result.getValues().get(0);
137
138         assertEquals(CUSTOMERS_URL + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL, actualMap.get(RESOURCE_LINK));
139         assertEquals(SERVICE_RESOURCE_TYPE, actualMap.get(RESOURCE_TYPE));
140
141     }
142
143     @Test
144     public void test_getNodesGenericVnfs_usingVnfName_ableToRetrieveItFromCache() throws Exception {
145         invokeCustomerandServiceInstanceUrls();
146
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());
150
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);
154
155         assertEquals(HttpStatus.OK, actual.getStatusCode());
156         assertTrue(actual.hasBody());
157
158         final GenericVnfs genericVnfs = actual.getBody();
159         assertEquals(1, genericVnfs.getGenericVnf().size());
160
161         final GenericVnf genericVnf = genericVnfs.getGenericVnf().get(0);
162         assertEquals(GENERIC_VNF_NAME, genericVnf.getVnfName());
163         assertEquals(VNF_ID, genericVnf.getVnfId());
164
165     }
166
167     private void invokeCustomerandServiceInstanceUrls() throws Exception, IOException {
168         final String url = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL);
169
170         final ResponseEntity<Void> response = invokeHttpPut(getUrl(CUSTOMERS_URL), TestUtils.getCustomer());
171
172         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
173
174         final ResponseEntity<Void> response2 = invokeHttpPut(url, TestUtils.getServiceInstance());
175         assertEquals(HttpStatus.ACCEPTED, response2.getStatusCode());
176     }
177
178     private String getUrl(final String... urls) {
179         return TestUtils.getUrl(port, urls);
180     }
181
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);
185     }
186
187     private HttpEntity<?> getHttpEntity(final Object obj) {
188         return new HttpEntity<>(obj, getHttpHeaders());
189     }
190
191     private HttpHeaders getHttpHeaders() {
192         return TestUtils.getHttpHeaders(userCredentials.getUsers().iterator().next().getUsername());
193     }
194 }