Increase code coverage on aai-common: aai-els-onap-logging library
[aai/aai-common.git] / aai-els-onap-logging / src / test / java / org / onap / logging / filter / base / AuditLogContainerFilterTest.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 javax.ws.rs.container.ContainerRequestContext;
26 import javax.ws.rs.container.ContainerResponseContext;
27 import javax.ws.rs.core.MultivaluedHashMap;
28 import javax.ws.rs.core.MultivaluedMap;
29 import javax.ws.rs.core.UriInfo;
30 import org.junit.After;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.InjectMocks;
34 import org.mockito.Mock;
35 import org.mockito.Spy;
36 import org.mockito.junit.MockitoJUnitRunner;
37 import org.onap.logging.ref.slf4j.ONAPLogConstants;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.slf4j.MDC;
41
42 @RunWith(MockitoJUnitRunner.class)
43 public class AuditLogContainerFilterTest {
44     protected static final Logger logger = LoggerFactory.getLogger(AbstractMetricLogFilter.class);
45
46     @Mock
47     private ContainerRequestContext containerRequest;
48
49     @Mock
50     private ContainerResponseContext containerResponse;
51
52     @Mock
53     private UriInfo uriInfo;
54
55     @Spy
56     @InjectMocks
57     private AuditLogContainerFilter auditLogContainerFilter;
58
59     @After
60     public void tearDown() {
61         MDC.clear();
62     }
63
64
65     @Test
66     public void filterTest() {
67         MultivaluedMap<String, String> headerMap = new MultivaluedHashMap<>();
68         headerMap.putSingle(ONAPLogConstants.Headers.REQUEST_ID, "e3b08fa3-535f-4c1b-8228-91318d2bb4ee");
69         when(containerRequest.getHeaders()).thenReturn(headerMap);
70         when(uriInfo.getPath()).thenReturn("onap/so/serviceInstances");
71         when(containerRequest.getUriInfo()).thenReturn(uriInfo);
72         auditLogContainerFilter.filter(containerRequest);
73
74         assertEquals("e3b08fa3-535f-4c1b-8228-91318d2bb4ee", MDC.get(ONAPLogConstants.MDCs.REQUEST_ID));
75         assertEquals("onap/so/serviceInstances", MDC.get(ONAPLogConstants.MDCs.SERVICE_NAME));
76         assertEquals("INPROGRESS", MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
77     }
78
79     @Test
80     public void getResponseCodeTest() {
81         when(containerResponse.getStatus()).thenReturn(200);
82         int responseCode = auditLogContainerFilter.getResponseCode(containerResponse);
83
84         assertEquals(200, responseCode);
85     }
86 }