Remove diffutils dependency
[aai/traversal.git] / aai-traversal / src / test / java / org / onap / aai / interceptors / AAILogJAXRSOutInterceptorTest.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.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;
30
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;
36 import java.util.Map;
37
38 import static org.mockito.Mockito.*;
39
40 public class AAILogJAXRSOutInterceptorTest {
41
42     private AAILogJAXRSOutInterceptor aaiLogJAXRSOutInterceptor;
43
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;
50
51
52     @Before
53     public void setup(){
54
55         aaiLogJAXRSOutInterceptor = new AAILogJAXRSOutInterceptor();
56
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);
62
63
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"));
69     }
70
71     @Test
72     public void testHandleMessageWhenNotCamelRequest() throws IOException {
73
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("");
82
83         when(message.get("CamelHttpUrl")).thenReturn("/somestring");
84         aaiLogJAXRSOutInterceptor.handleMessage(message);
85     }
86
87     @Test
88     public void testLogCallBack(){
89
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);
95
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);
100
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("*/*");
105
106         AAILogJAXRSOutInterceptor.LoggingCallback loggingCallback = new AAILogJAXRSOutInterceptor().new LoggingCallback(message, out);
107         final CacheAndWriteOutputStream newOut = new CacheAndWriteOutputStream(out);
108         loggingCallback.onClose(newOut);
109     }
110
111 }