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