Adding project endpoint and https support
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / test / java / org / onap / so / aai / simulator / controller / BusinessControllerTest.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.aai.simulator.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.aai.simulator.controller.TestUtils.getFile;
27 import static org.onap.so.aai.simulator.controller.TestUtils.getJsonString;
28 import static org.onap.so.aai.simulator.controller.TestUtils.getObjectFromFile;
29 import java.io.File;
30 import java.io.IOException;
31 import java.nio.file.Files;
32 import java.util.Optional;
33 import java.util.UUID;
34 import org.junit.After;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.onap.aai.domain.yang.Customer;
38 import org.onap.aai.domain.yang.ServiceInstance;
39 import org.onap.aai.domain.yang.ServiceInstances;
40 import org.onap.aai.domain.yang.ServiceSubscription;
41 import org.onap.so.aai.simulator.service.providers.CustomerCacheServiceProvider;
42 import org.onap.so.aai.simulator.utils.Constants;
43 import org.onap.so.aai.simulator.utils.RequestError;
44 import org.onap.so.aai.simulator.utils.ServiceException;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.beans.factory.annotation.Value;
47 import org.springframework.boot.test.context.SpringBootTest;
48 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
49 import org.springframework.boot.test.web.client.TestRestTemplate;
50 import org.springframework.boot.web.server.LocalServerPort;
51 import org.springframework.context.annotation.Configuration;
52 import org.springframework.http.HttpEntity;
53 import org.springframework.http.HttpHeaders;
54 import org.springframework.http.HttpMethod;
55 import org.springframework.http.HttpStatus;
56 import org.springframework.http.ResponseEntity;
57 import org.springframework.test.context.ActiveProfiles;
58 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
59
60 /**
61  * @author waqas.ikram@ericsson.com
62  *
63  */
64 @RunWith(SpringJUnit4ClassRunner.class)
65 @ActiveProfiles("test")
66 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
67 @Configuration
68 public class BusinessControllerTest {
69
70     private static final String SERVICE_INSTANCES_URL = "/service-instances";
71
72     private static final String SERVICE_NAME = "ServiceTest";
73
74     private static final String SERVICE_INSTANCE_ID = "ccece8fe-13da-456a-baf6-41b3a4a2bc2b";
75
76     private static final String SERVICE_INSTANCE_URL =
77             SERVICE_INSTANCES_URL + "/service-instance/" + SERVICE_INSTANCE_ID;
78
79     private static final String SERVICE_TYPE = "vCPE";
80
81     private static final String SERVICE_SUBSCRIPTIONS_URL =
82             "/service-subscriptions/service-subscription/" + SERVICE_TYPE;
83
84     private static final String GLOBAL_CUSTOMER_ID = "DemoCustomer";
85
86     private static final String CUSTOMERS_URL = Constants.CUSTOMER_URL + GLOBAL_CUSTOMER_ID;
87
88     @LocalServerPort
89     private int port;
90
91     @Autowired
92     private TestRestTemplate restTemplate;
93
94     @Value("${spring.security.username}")
95     private String username;
96
97     @Autowired
98     private CustomerCacheServiceProvider cacheServiceProvider;
99
100     @After
101     public void after() {
102         cacheServiceProvider.clearAll();
103     }
104
105     @Test
106     public void test_putCustomer_successfullyAddedToCache() throws Exception {
107         final ResponseEntity<Void> actual = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
108
109         assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode());
110         assertTrue(cacheServiceProvider.getCustomer(GLOBAL_CUSTOMER_ID).isPresent());
111     }
112
113     @Test
114     public void test_getCustomer_ableToRetrieveCustomer() throws Exception {
115         final String url = getCustomerEndPointUrl();
116
117         invokeHttpPut(url, getCustomer());
118
119         final ResponseEntity<Customer> actual =
120                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), Customer.class);
121
122         assertEquals(HttpStatus.OK, actual.getStatusCode());
123         assertTrue(actual.hasBody());
124
125         final Customer actualCustomer = actual.getBody();
126         assertEquals(GLOBAL_CUSTOMER_ID, actualCustomer.getGlobalCustomerId());
127         assertNotNull(actualCustomer.getResourceVersion());
128         assertFalse(actualCustomer.getResourceVersion().isEmpty());
129     }
130
131     @Test
132     public void test_getCustomer_returnRequestError_ifCustomerNotInCache() throws Exception {
133         final String url = getCustomerEndPointUrl();
134
135         final ResponseEntity<RequestError> actual =
136                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), RequestError.class);
137
138         assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
139
140         final RequestError actualError = actual.getBody();
141         final ServiceException serviceException = actualError.getServiceException();
142
143         assertNotNull(serviceException);
144         assertEquals(Constants.ERROR_MESSAGE_ID, serviceException.getMessageId());
145         assertEquals(Constants.ERROR_MESSAGE, serviceException.getText());
146         assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
147
148     }
149
150     @Test
151     public void test_getServiceSubscription_ableToRetrieveServiceSubscriptionFromCache() throws Exception {
152         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL;
153
154         invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
155
156         final ResponseEntity<ServiceSubscription> actual = restTemplate.exchange(url, HttpMethod.GET,
157                 new HttpEntity<>(getHttpHeaders()), ServiceSubscription.class);
158
159         assertEquals(HttpStatus.OK, actual.getStatusCode());
160         assertTrue(actual.hasBody());
161
162         final ServiceSubscription actualServiceSubscription = actual.getBody();
163         assertEquals(SERVICE_TYPE, actualServiceSubscription.getServiceType());
164         assertNotNull(actualServiceSubscription.getRelationshipList());
165         assertFalse(actualServiceSubscription.getRelationshipList().getRelationship().isEmpty());
166     }
167
168     @Test
169     public void test_putSericeInstance_ableToRetrieveServiceInstanceFromCache() throws Exception {
170
171         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
172
173         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
174
175         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
176
177         invokeHttpPut(url, getServiceInstance());
178
179         final Optional<ServiceInstance> actual =
180                 cacheServiceProvider.getServiceInstance(GLOBAL_CUSTOMER_ID, SERVICE_TYPE, SERVICE_INSTANCE_ID);
181
182         assertTrue(actual.isPresent());
183         final ServiceInstance actualServiceInstance = actual.get();
184
185         assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
186         assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
187
188     }
189
190     @Test
191     public void test_getSericeInstance_usingServiceInstanceName_ableToRetrieveServiceInstanceFromCache()
192             throws Exception {
193
194         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
195
196         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
197
198         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
199
200         invokeHttpPut(url, getServiceInstance());
201
202         final String serviceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCES_URL
203                 + "?service-instance-name=" + SERVICE_NAME;
204
205         final ResponseEntity<ServiceInstances> actual = restTemplate.exchange(serviceInstanceUrl, HttpMethod.GET,
206                 new HttpEntity<>(getHttpHeaders()), ServiceInstances.class);
207
208         assertEquals(HttpStatus.OK, actual.getStatusCode());
209         assertTrue(actual.hasBody());
210
211         final ServiceInstances actualServiceInstances = actual.getBody();
212         assertFalse(actualServiceInstances.getServiceInstance().isEmpty());
213
214         assertEquals(SERVICE_NAME, actualServiceInstances.getServiceInstance().get(0).getServiceInstanceName());
215
216     }
217
218     @Test
219     public void test_getSericeInstance_usingServiceInstanceId_ableToRetrieveServiceInstanceFromCache()
220             throws Exception {
221
222         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
223
224         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
225
226         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
227
228         invokeHttpPut(url, getServiceInstance());
229
230         final ResponseEntity<ServiceInstance> actual =
231                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), ServiceInstance.class);
232
233         assertEquals(HttpStatus.OK, actual.getStatusCode());
234         assertTrue(actual.hasBody());
235
236         final ServiceInstance actualServiceInstance = actual.getBody();
237
238         assertEquals(SERVICE_NAME, actualServiceInstance.getServiceInstanceName());
239         assertEquals(SERVICE_INSTANCE_ID, actualServiceInstance.getServiceInstanceId());
240
241     }
242
243     @Test
244     public void test_getSericeInstance_usinginvalidServiceInstanceId_shouldReturnError() throws Exception {
245
246         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
247
248         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
249
250         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
251
252         invokeHttpPut(url, getServiceInstance());
253
254         final String invalidServiceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL
255                 + SERVICE_INSTANCES_URL + "/service-instance/" + UUID.randomUUID();
256
257         final ResponseEntity<RequestError> actual = restTemplate.exchange(invalidServiceInstanceUrl, HttpMethod.GET,
258                 new HttpEntity<>(getHttpHeaders()), RequestError.class);
259
260         assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
261
262         final RequestError actualError = actual.getBody();
263         final ServiceException serviceException = actualError.getServiceException();
264
265         assertNotNull(serviceException);
266         assertEquals(Constants.ERROR_MESSAGE_ID, serviceException.getMessageId());
267         assertEquals(Constants.ERROR_MESSAGE, serviceException.getText());
268         assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
269
270     }
271
272     @Test
273     public void test_getSericeInstance_usingInvalidServiceInstanceName_shouldReturnError() throws Exception {
274
275         final String url = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCE_URL;
276
277         final ResponseEntity<Void> response = invokeHttpPut(getCustomerEndPointUrl(), getCustomer());
278
279         assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
280
281         final ResponseEntity<Void> putRequestReponse = invokeHttpPut(url, getServiceInstance());
282         assertEquals(HttpStatus.ACCEPTED, putRequestReponse.getStatusCode());
283
284
285         final String serviceInstanceUrl = getCustomerEndPointUrl() + SERVICE_SUBSCRIPTIONS_URL + SERVICE_INSTANCES_URL
286                 + "?service-instance-name=Dummy&depth=2";
287
288         final ResponseEntity<RequestError> actual = restTemplate.exchange(serviceInstanceUrl, HttpMethod.GET,
289                 new HttpEntity<>(getHttpHeaders()), RequestError.class);
290
291         assertEquals(HttpStatus.NOT_FOUND, actual.getStatusCode());
292
293         final RequestError actualError = actual.getBody();
294         final ServiceException serviceException = actualError.getServiceException();
295
296         assertNotNull(serviceException);
297         assertEquals(Constants.ERROR_MESSAGE_ID, serviceException.getMessageId());
298         assertEquals(Constants.ERROR_MESSAGE, serviceException.getText());
299         assertTrue(serviceException.getVariables().contains(HttpMethod.GET.toString()));
300
301     }
302
303     private String getCustomer() throws Exception, IOException {
304         return getJsonString("test-data/business-customer.json");
305     }
306
307     private String getCustomerEndPointUrl() {
308         return TestUtils.getBaseUrl(port) + CUSTOMERS_URL;
309     }
310
311     private ResponseEntity<Void> invokeHttpPut(final String url, final Object obj) {
312         final HttpEntity<?> httpEntity = getHttpEntity(obj);
313         return restTemplate.exchange(url, HttpMethod.PUT, httpEntity, Void.class);
314     }
315
316     private HttpEntity<?> getHttpEntity(final Object obj) {
317         return new HttpEntity<>(obj, getHttpHeaders());
318     }
319
320     private HttpHeaders getHttpHeaders() {
321         return TestUtils.getHttpHeaders(username);
322     }
323
324     private String getServiceInstance() throws Exception, IOException {
325         return getJsonString("test-data/service-instance.json");
326     }
327
328 }