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 com.google.common.collect.HashMultimap;
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import org.onap.aai.AAISetup;
29 import org.onap.aai.exceptions.AAIException;
30 import org.onap.aai.parsers.exceptions.AmbiguousMapAAIException;
32 import javax.ws.rs.core.*;
34 import static org.mockito.Mockito.mock;
35 import static org.mockito.Mockito.when;
37 import java.util.ArrayList;
38 import java.util.List;
40 import java.util.concurrent.Callable;
42 public class RESTAPITest extends AAISetup {
43 private static RESTAPI restapi;
44 private static HttpHeaders httpHeaders;
45 private static Callable callable;
46 private static UriInfo info;
47 private static Response response;
49 public static final String AAI_TIMEOUT_ENABLED = "aai.timeout.enabled";
50 public static final String AAI_TIMEOUT_BY_APP = "aai.timeout.by.app";
51 public static final String AAI_TIMEOUT_DEFAULT_LIMIT = "aai.timeout.default.limit";
54 public static void setUp() {
55 restapi = new RESTAPI();
56 httpHeaders = mock(HttpHeaders.class);
57 callable = mock(Callable.class);
58 info = mock(UriInfo.class);
59 response = mock(Response.class);
63 public void testGetFromAppId() throws AAIException {
64 List<String> fromAppIdList = new ArrayList<>();
65 fromAppIdList.add("from-app-id-01");
66 when(httpHeaders.getRequestHeader("X-FromAppId")).thenReturn(fromAppIdList);
68 String fromAppId = restapi.getFromAppId(httpHeaders);
69 Assert.assertEquals("from-app-id-01", fromAppId);
72 @Test(expected = AAIException.class)
73 public void testGetFromAppId_throwAAIException() throws AAIException {
74 when(httpHeaders.getRequestHeader("X-FromAppId")).thenReturn(null);
75 restapi.getFromAppId(httpHeaders);
79 public void testGetTransId() throws AAIException {
80 List<String> transactionIdList = new ArrayList<>();
81 transactionIdList.add("transaction-id-01");
82 when(httpHeaders.getRequestHeader("X-TransactionId")).thenReturn(transactionIdList);
84 String transId = restapi.getTransId(httpHeaders);
85 Assert.assertEquals("transaction-id-01", transId);
88 @Test(expected = AAIException.class)
89 public void testGetTransId_throwAAIException() throws AAIException {
90 when(httpHeaders.getRequestHeader("X-TransactionId")).thenReturn(null);
91 String transId = restapi.getTransId(httpHeaders);
95 public void testRunner() throws AAIException, Exception {
96 MultivaluedMap<String, String> requestHeaders = new MultivaluedHashMap<String, String>();
97 requestHeaders.add("X-FromAppId", "from-app-id-01");
98 requestHeaders.add("X-TransactionId", "transaction-id-01");
99 when(httpHeaders.getRequestHeaders()).thenReturn(requestHeaders);
100 when(callable.call()).thenReturn(response);
102 Response resp = restapi.runner(AAI_TIMEOUT_ENABLED, AAI_TIMEOUT_BY_APP, AAI_TIMEOUT_DEFAULT_LIMIT, httpHeaders, info, HttpMethod.GET, callable);
103 Assert.assertNotNull(resp);