2 * ============LICENSE_START=======================================================
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
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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.interceptors;
24 import org.apache.cxf.io.CacheAndWriteOutputStream;
25 import org.apache.cxf.message.Exchange;
26 import org.apache.cxf.message.ExchangeImpl;
27 import org.apache.cxf.message.Message;
28 import org.junit.Before;
29 import org.junit.Test;
31 import java.io.IOException;
32 import java.io.OutputStream;
33 import java.util.Arrays;
34 import java.util.HashMap;
35 import java.util.List;
38 import static org.mockito.Mockito.*;
40 public class AAILogJAXRSOutInterceptorTest {
42 private AAILogJAXRSOutInterceptor aaiLogJAXRSOutInterceptor;
44 private Message message;
45 private Exchange exchange;
46 private OutputStream out;
47 private Map<String, List<String>> headers;
48 private Message outMessage;
49 private Message inMessage;
55 aaiLogJAXRSOutInterceptor = new AAILogJAXRSOutInterceptor();
57 message = mock(Message.class);
58 exchange = spy(new ExchangeImpl());
59 out = mock(OutputStream.class);
60 outMessage = mock(Message.class);
61 inMessage = mock(Message.class);
64 headers = new HashMap<>();
65 headers.put("X-FromAppId", Arrays.asList("JUNIT"));
66 headers.put("X-TransactionId", Arrays.asList("JUNIT"));
67 headers.put("Content-Type", Arrays.asList("application/json"));
68 headers.put("Accept", Arrays.asList("application/json"));
72 public void testHandleMessageWhenNotCamelRequest() throws IOException {
74 when(message.getExchange()).thenReturn(exchange);
75 when(message.getContent(OutputStream.class)).thenReturn(out);
76 when(message.get(Message.PROTOCOL_HEADERS)).thenReturn(headers);
77 when(message.get(Message.CONTENT_TYPE)).thenReturn("*/*");
78 when(exchange.getOutMessage()).thenReturn(outMessage);
79 when(outMessage.getContent(OutputStream.class)).thenReturn(out);
80 when(exchange.containsKey("AAI_LOGGING_HBASE_ENABLED")).thenReturn(true);
81 when(exchange.remove("AAI_LOGGING_HBASE_ENABLED")).thenReturn("");
83 when(message.get("CamelHttpUrl")).thenReturn("/somestring");
84 aaiLogJAXRSOutInterceptor.handleMessage(message);
88 public void testLogCallBack(){
90 when(message.getExchange()).thenReturn(exchange);
91 when(message.getContent(OutputStream.class)).thenReturn(out);
92 when(message.get(Message.PROTOCOL_HEADERS)).thenReturn(headers);
93 when(message.get(Message.CONTENT_TYPE)).thenReturn("*/*");
94 when(exchange.getOutMessage()).thenReturn(outMessage);
96 when(outMessage.getContent(OutputStream.class)).thenReturn(out);
97 when(exchange.containsKey("AAI_LOGGING_HBASE_ENABLED")).thenReturn(true);
98 when(exchange.remove("AAI_LOGGING_HBASE_ENABLED")).thenReturn("");
99 when(exchange.getInMessage()).thenReturn(inMessage);
101 when(inMessage.getExchange()).thenReturn(exchange);
102 when(inMessage.getContent(OutputStream.class)).thenReturn(out);
103 when(inMessage.get(Message.PROTOCOL_HEADERS)).thenReturn(headers);
104 when(inMessage.get(Message.CONTENT_TYPE)).thenReturn("*/*");
106 AAILogJAXRSOutInterceptor.LoggingCallback loggingCallback = new AAILogJAXRSOutInterceptor().new LoggingCallback(message, out);
107 final CacheAndWriteOutputStream newOut = new CacheAndWriteOutputStream(out);
108 loggingCallback.onClose(newOut);