5156516d4eab22a2512c0996d382ffc313a38fff
[logging-analytics.git] / reference / logging-filter / logging-filter-base / src / test / java / org / onap / logging / filter / base / PayloadLoggingClientFilterTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - Logging
4  * ================================================================================
5  * Copyright (C) 2019 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.logging.filter.base;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.Mockito.when;
25 import java.io.IOException;
26 import java.net.URISyntaxException;
27 import javax.ws.rs.client.ClientRequestContext;
28 import javax.ws.rs.core.MultivaluedHashMap;
29 import javax.ws.rs.core.MultivaluedMap;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.InjectMocks;
33 import org.mockito.Mock;
34 import org.mockito.Spy;
35 import org.mockito.junit.MockitoJUnitRunner;
36 import org.onap.logging.filter.base.Constants;
37 import org.onap.logging.filter.base.PayloadLoggingClientFilter;
38
39 @RunWith(MockitoJUnitRunner.class)
40 public class PayloadLoggingClientFilterTest {
41
42     @Mock
43     private ClientRequestContext requestContext;
44
45     @Spy
46     @InjectMocks
47     private PayloadLoggingClientFilter payloadLoggingClientFilter;
48
49     @Test
50     public void formatMethodTest() throws IOException, URISyntaxException {
51         when(requestContext.getHeaderString("X-HTTP-Method-Override")).thenReturn("filter");
52         when(requestContext.getMethod()).thenReturn("filtered");
53         String method = payloadLoggingClientFilter.formatMethod(requestContext);
54
55         assertEquals("filtered (overridden to filter)", method);
56     }
57
58     @Test
59     public void formatMethodNullHeaderTest() throws IOException, URISyntaxException {
60         when(requestContext.getMethod()).thenReturn("filtered");
61         String method = payloadLoggingClientFilter.formatMethod(requestContext);
62
63         assertEquals("filtered", method);
64     }
65
66     @Test
67     public void getHeadersTest() {
68         MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
69         headers.add(Constants.HttpHeaders.ONAP_PARTNER_NAME, "SO");
70         headers.add("Authorization", "Test");
71
72         String printHeaders = payloadLoggingClientFilter.getHeaders(headers);
73
74         assertEquals("{X-ONAP-PartnerName=[SO]}", printHeaders);
75     }
76 }