Merge "Release 1.14.0 maven artifact"
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / restcore / RESTAPITest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.aai.restcore;
22
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
25
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.concurrent.Callable;
29
30 import javax.ws.rs.core.HttpHeaders;
31 import javax.ws.rs.core.MultivaluedHashMap;
32 import javax.ws.rs.core.MultivaluedMap;
33 import javax.ws.rs.core.Response;
34 import javax.ws.rs.core.UriInfo;
35
36 import org.junit.Assert;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.onap.aai.AAISetup;
40 import org.onap.aai.exceptions.AAIException;
41
42 public class RESTAPITest extends AAISetup {
43     private static RESTAPI restapi;
44     private static HttpHeaders httpHeaders;
45     private static Callable<Response> callable;
46     private static UriInfo info;
47     private static Response response;
48
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";
52
53     @BeforeClass
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);
60     }
61
62     @Test
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);
67
68         String fromAppId = restapi.getFromAppId(httpHeaders);
69         Assert.assertEquals("from-app-id-01", fromAppId);
70     }
71
72     @Test(expected = AAIException.class)
73     public void testGetFromAppId_throwAAIException() throws AAIException {
74         when(httpHeaders.getRequestHeader("X-FromAppId")).thenReturn(null);
75         restapi.getFromAppId(httpHeaders);
76     }
77
78     @Test
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);
83
84         String transId = restapi.getTransId(httpHeaders);
85         Assert.assertEquals("transaction-id-01", transId);
86     }
87
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);
92     }
93
94     @Test
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);
101
102         Response resp = restapi.runner(AAI_TIMEOUT_ENABLED, AAI_TIMEOUT_BY_APP, AAI_TIMEOUT_DEFAULT_LIMIT, httpHeaders,
103                 info, HttpMethod.GET, callable);
104         Assert.assertNotNull(resp);
105     }
106 }