2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.aai.restcore;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.concurrent.Callable;
30 import javax.ws.rs.core.*;
32 import org.junit.Assert;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.onap.aai.AAISetup;
36 import org.onap.aai.exceptions.AAIException;
38 public class RESTAPITest extends AAISetup {
39 private static RESTAPI restapi;
40 private static HttpHeaders httpHeaders;
41 private static Callable callable;
42 private static UriInfo info;
43 private static Response response;
45 public static final String AAI_TIMEOUT_ENABLED = "aai.timeout.enabled";
46 public static final String AAI_TIMEOUT_BY_APP = "aai.timeout.by.app";
47 public static final String AAI_TIMEOUT_DEFAULT_LIMIT = "aai.timeout.default.limit";
50 public static void setUp() {
51 restapi = new RESTAPI();
52 httpHeaders = mock(HttpHeaders.class);
53 callable = mock(Callable.class);
54 info = mock(UriInfo.class);
55 response = mock(Response.class);
59 public void testGetFromAppId() throws AAIException {
60 List<String> fromAppIdList = new ArrayList<>();
61 fromAppIdList.add("from-app-id-01");
62 when(httpHeaders.getRequestHeader("X-FromAppId")).thenReturn(fromAppIdList);
64 String fromAppId = restapi.getFromAppId(httpHeaders);
65 Assert.assertEquals("from-app-id-01", fromAppId);
68 @Test(expected = AAIException.class)
69 public void testGetFromAppId_throwAAIException() throws AAIException {
70 when(httpHeaders.getRequestHeader("X-FromAppId")).thenReturn(null);
71 restapi.getFromAppId(httpHeaders);
75 public void testGetTransId() throws AAIException {
76 List<String> transactionIdList = new ArrayList<>();
77 transactionIdList.add("transaction-id-01");
78 when(httpHeaders.getRequestHeader("X-TransactionId")).thenReturn(transactionIdList);
80 String transId = restapi.getTransId(httpHeaders);
81 Assert.assertEquals("transaction-id-01", transId);
84 @Test(expected = AAIException.class)
85 public void testGetTransId_throwAAIException() throws AAIException {
86 when(httpHeaders.getRequestHeader("X-TransactionId")).thenReturn(null);
87 String transId = restapi.getTransId(httpHeaders);
91 public void testRunner() throws AAIException, Exception {
92 MultivaluedMap<String, String> requestHeaders = new MultivaluedHashMap<String, String>();
93 requestHeaders.add("X-FromAppId", "from-app-id-01");
94 requestHeaders.add("X-TransactionId", "transaction-id-01");
95 when(httpHeaders.getRequestHeaders()).thenReturn(requestHeaders);
96 when(callable.call()).thenReturn(response);
98 Response resp = restapi.runner(AAI_TIMEOUT_ENABLED, AAI_TIMEOUT_BY_APP, AAI_TIMEOUT_DEFAULT_LIMIT, httpHeaders,
99 info, HttpMethod.GET, callable);
100 Assert.assertNotNull(resp);