3c099c75bc14d28fd00f9b2b0e96bee79b220497
[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.*;
31
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;
37
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;
44
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";
48
49     @BeforeClass
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);
56     }
57
58     @Test
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);
63
64         String fromAppId = restapi.getFromAppId(httpHeaders);
65         Assert.assertEquals("from-app-id-01", fromAppId);
66     }
67
68     @Test(expected = AAIException.class)
69     public void testGetFromAppId_throwAAIException() throws AAIException {
70         when(httpHeaders.getRequestHeader("X-FromAppId")).thenReturn(null);
71         restapi.getFromAppId(httpHeaders);
72     }
73
74     @Test
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);
79
80         String transId = restapi.getTransId(httpHeaders);
81         Assert.assertEquals("transaction-id-01", transId);
82     }
83
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);
88     }
89
90     @Test
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);
97
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);
101     }
102 }