af80b193b706c5ad4388b1317cc6a0743a886cda
[logging-analytics.git] / reference / logging-filter / logging-filter-base / 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.ArgumentMatchers.any;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.when;
27 import javax.ws.rs.container.ContainerRequestContext;
28 import javax.ws.rs.container.ContainerResponseContext;
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.filter.base.AuditLogContainerFilter;
38 import org.onap.logging.filter.base.MDCSetup;
39 import org.onap.logging.filter.base.SimpleMap;
40 import org.onap.logging.ref.slf4j.ONAPLogConstants;
41 import org.slf4j.MDC;
42
43 @RunWith(MockitoJUnitRunner.class)
44 public class AuditLogContainerFilterTest {
45
46     @Mock
47     private ContainerRequestContext containerRequest;
48
49     @Mock
50     private ContainerResponseContext containerResponse;
51
52     @Mock
53     private MDCSetup mdcSetup;
54
55     @Mock
56     private UriInfo uriInfo;
57
58     @Spy
59     @InjectMocks
60     private AuditLogContainerFilter auditLogContainerFilter;
61
62     @After
63     public void tearDown() {
64         MDC.clear();
65     }
66
67     @Test
68     public void filterTest() {
69         when(mdcSetup.getRequestId(any(SimpleMap.class))).thenReturn("e3b08fa3-535f-4c1b-8228-91318d2bb4ee");
70         when(uriInfo.getPath()).thenReturn("onap/so/serviceInstances");
71         doReturn(uriInfo).when(containerRequest).getUriInfo();
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 }