40de5a9b9b96de4ee8a0d18128df114f6a16843f
[aai/traversal.git] / aai-traversal / src / test / java / org / onap / aai / interceptors / AAILogJAXRSInInterceptorTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.interceptors;
23
24 import org.apache.cxf.message.Exchange;
25 import org.apache.cxf.message.ExchangeImpl;
26 import org.apache.cxf.message.Message;
27 import org.junit.Before;
28 import org.junit.Test;
29
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.util.Arrays;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36
37 import static org.mockito.Mockito.*;
38
39 public class AAILogJAXRSInInterceptorTest {
40
41     private AAILogJAXRSInInterceptor aaiLogJAXRSInInterceptor;
42
43     private Message message;
44     private Exchange exchange;
45     private InputStream is;
46     private Map<String, List<String>> headers;
47
48
49     @Before
50     public void setup(){
51
52         aaiLogJAXRSInInterceptor = new AAILogJAXRSInInterceptor();
53
54         message  = mock(Message.class);
55         exchange = spy(new ExchangeImpl());
56
57         is = getClass().getClassLoader().getResourceAsStream("logback.xml");
58
59         headers = new HashMap<>();
60         headers.put("X-FromAppId", Arrays.asList("JUNIT"));
61         headers.put("X-TransactionId", Arrays.asList("JUNIT"));
62         headers.put("Content-Type", Arrays.asList("application/json"));
63         headers.put("Accept", Arrays.asList("application/json"));
64     }
65
66     @Test
67     public void testHandleMessageWhenNotCamelRequest() throws IOException {
68
69         when(message.getExchange()).thenReturn(exchange);
70         when(message.getContent(InputStream.class)).thenReturn(is);
71         when(message.get(Message.PROTOCOL_HEADERS)).thenReturn(headers);
72         when(message.get(Message.CONTENT_TYPE)).thenReturn("*/*");
73
74         when(message.get("CamelHttpUrl")).thenReturn("/somestring");
75         aaiLogJAXRSInInterceptor.handleMessage(message);
76     }
77
78     @Test
79     public void testHandleMessageWhenUUIDHasMultiple() throws IOException {
80
81         Map<String, List<String>> headers = new HashMap<>();
82
83         headers.put("X-FromAppId", Arrays.asList("JUNIT"));
84         headers.put("X-TransactionId", Arrays.asList("jfasodjf:fjaosjfidsaj:afsidjfaodfja"));
85         headers.put("Content-Type", Arrays.asList("application/json"));
86         headers.put("Accept", Arrays.asList("application/json"));
87
88         when(message.getExchange()).thenReturn(exchange);
89         when(message.getContent(InputStream.class)).thenReturn(is);
90         when(message.get(Message.PROTOCOL_HEADERS)).thenReturn(headers);
91         when(message.get(Message.CONTENT_TYPE)).thenReturn("*/*");
92
93         when(message.get("CamelHttpUrl")).thenReturn("/somestring");
94         aaiLogJAXRSInInterceptor.handleMessage(message);
95     }
96
97     @Test
98     public void testHandleMessageWhenMissingTransactionId() throws IOException {
99
100         Map<String, List<String>> headers = new HashMap<>();
101
102         headers.put("X-FromAppId", Arrays.asList("JUNIT"));
103         headers.put("Content-Type", Arrays.asList("application/json"));
104         headers.put("Accept", Arrays.asList("application/json"));
105
106         when(message.getExchange()).thenReturn(exchange);
107         when(message.getContent(InputStream.class)).thenReturn(is);
108         when(message.get(Message.PROTOCOL_HEADERS)).thenReturn(headers);
109         when(message.get(Message.CONTENT_TYPE)).thenReturn("*/*");
110
111         when(message.get("CamelHttpUrl")).thenReturn("/somestring");
112         aaiLogJAXRSInInterceptor.handleMessage(message);
113     }
114
115     @Test
116     public void testHandleMessageWhenMissingContentType() throws IOException {
117
118         Map<String, List<String>> headers = new HashMap<>();
119
120         headers.put("X-FromAppId", Arrays.asList("JUNIT"));
121         headers.put("X-TransactionId", Arrays.asList("jfasodjf:fjaosjfidsaj:afsidjfaodfja"));
122         headers.put("Accept", Arrays.asList("application/json"));
123
124         when(message.getExchange()).thenReturn(exchange);
125         when(message.getContent(InputStream.class)).thenReturn(is);
126         when(message.get(Message.PROTOCOL_HEADERS)).thenReturn(headers);
127         when(message.get(Message.CONTENT_TYPE)).thenReturn("*/*");
128
129         when(message.get("CamelHttpUrl")).thenReturn("/somestring");
130         aaiLogJAXRSInInterceptor.handleMessage(message);
131     }
132
133     @Test
134     public void testHandleMessageWhenQueryExistsAndUriEcho() throws IOException {
135
136         Map<String, List<String>> headers = new HashMap<>();
137
138         headers.put("X-FromAppId", Arrays.asList("JUNIT"));
139         headers.put("X-TransactionId", Arrays.asList("jfasodjf:fjaosjfidsaj:afsidjfaodfja"));
140         headers.put("Content-Type", Arrays.asList("application/json"));
141         headers.put("Accept", Arrays.asList("application/json"));
142
143         when(message.getExchange()).thenReturn(exchange);
144         when(message.getContent(InputStream.class)).thenReturn(is);
145         when(message.get(Message.PROTOCOL_HEADERS)).thenReturn(headers);
146         when(message.get(Message.CONTENT_TYPE)).thenReturn("*/*");
147         when(message.get(Message.QUERY_STRING)).thenReturn(null);
148         when(exchange.containsKey("AAI_LOGGING_HBASE_ENABLED")).thenReturn(true);
149         when(exchange.remove("AAI_LOGGING_HBASE_ENABLED")).thenReturn("");
150         when(exchange.containsKey("AAI_LOGGING_TRACE_ENABLED")).thenReturn(true);
151         when(exchange.remove("AAI_LOGGING_TRACE_ENABLED")).thenReturn("");
152         when(message.get("CamelHttpUrl")).thenReturn("/util/echo");
153         aaiLogJAXRSInInterceptor.handleMessage(message);
154     }
155
156     @Test
157     public void testHandleMessageWhenQueryExistsAndUriTranslog() throws IOException {
158
159         Map<String, List<String>> headers = new HashMap<>();
160
161         headers.put("X-FromAppId", Arrays.asList("JUNIT"));
162         headers.put("X-TransactionId", Arrays.asList("jfasodjf:fjaosjfidsaj:afsidjfaodfja"));
163         headers.put("Content-Type", Arrays.asList("application/json"));
164         headers.put("Accept", Arrays.asList("application/json"));
165
166         when(message.getExchange()).thenReturn(exchange);
167         when(message.getContent(InputStream.class)).thenReturn(is);
168         when(message.get(Message.PROTOCOL_HEADERS)).thenReturn(headers);
169         when(message.get(Message.CONTENT_TYPE)).thenReturn("*/*");
170         when(message.get(Message.QUERY_STRING)).thenReturn(null);
171         when(exchange.containsKey("AAI_LOGGING_HBASE_ENABLED")).thenReturn(true);
172         when(exchange.remove("AAI_LOGGING_HBASE_ENABLED")).thenReturn("");
173         when(exchange.containsKey("AAI_LOGGING_TRACE_ENABLED")).thenReturn(true);
174         when(exchange.remove("AAI_LOGGING_TRACE_ENABLED")).thenReturn("");
175         when(message.get("CamelHttpUrl")).thenReturn("/translog/");
176         aaiLogJAXRSInInterceptor.handleMessage(message);
177     }
178
179     @Test
180     public void testHandleMessageWhenPutMessageKeyReturnsException() throws IOException {
181
182         Map<String, List<String>> headers = new HashMap<>();
183
184         headers.put("X-FromAppId", Arrays.asList("JUNIT"));
185         headers.put("X-TransactionId", Arrays.asList("jfasodjf:fjaosjfidsaj:afsidjfaodfja"));
186         headers.put("Content-Type", Arrays.asList("application/json"));
187         headers.put("Accept", Arrays.asList("application/json"));
188
189         when(message.getExchange()).thenReturn(exchange);
190         when(message.getContent(InputStream.class)).thenReturn(is);
191         when(message.get(Message.PROTOCOL_HEADERS)).thenReturn(headers);
192         when(message.get(Message.CONTENT_TYPE)).thenReturn("*/*");
193         when(message.get(Message.QUERY_STRING)).thenReturn(null);
194         when(exchange.containsKey("AAI_LOGGING_HBASE_ENABLED")).thenReturn(true);
195         when(exchange.remove("AAI_LOGGING_HBASE_ENABLED")).thenReturn("");
196         when(exchange.containsKey("AAI_LOGGING_TRACE_ENABLED")).thenReturn(true);
197         when(exchange.remove("AAI_LOGGING_TRACE_ENABLED")).thenReturn("");
198         when(message.get("CamelHttpUrl")).thenReturn("/translog/");
199         when(message.get(Message.ENCODING)).thenReturn("http");
200         when(message.get(Message.RESPONSE_CODE)).thenReturn(200);
201
202         aaiLogJAXRSInInterceptor.handleMessage(message);
203     }
204 }