Increase code coverage on aai-common: aai-els-onap-logging library
[aai/aai-common.git] / aai-els-onap-logging / src / test / java / org / onap / aai / aailog / filter / AaiAuditLogContainerFilterTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 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 package org.onap.aai.aailog.filter;
21
22 import org.junit.After;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.InjectMocks;
26 import org.mockito.Mock;
27 import org.mockito.Spy;
28 import org.mockito.junit.MockitoJUnitRunner;
29 import org.onap.logging.filter.base.Constants;
30 import org.onap.logging.ref.slf4j.ONAPLogConstants;
31 import org.slf4j.MDC;
32
33 import javax.ws.rs.container.ContainerRequestContext;
34 import javax.ws.rs.container.ContainerResponseContext;
35 import javax.ws.rs.core.MultivaluedHashMap;
36 import javax.ws.rs.core.MultivaluedMap;
37 import javax.ws.rs.core.UriInfo;
38 import java.net.URI;
39
40 import static org.junit.Assert.assertEquals;
41 import static org.mockito.Mockito.when;
42
43 @RunWith(MockitoJUnitRunner.class)
44 public class AaiAuditLogContainerFilterTest {
45     @Mock
46     private ContainerRequestContext containerRequest;
47
48     @Mock
49     private ContainerResponseContext containerResponse;
50
51     @Mock
52     private UriInfo uriInfo;
53
54     @Spy
55     @InjectMocks
56     private AaiAuditLogContainerFilter aaiAuditFilter;
57
58     @After
59     public void tearDown() {
60         MDC.clear();
61     }
62     @Test
63     public void partnerAndServiceNameValueTest() throws java.net.URISyntaxException {
64
65         MultivaluedMap<String, String> headerMap = new MultivaluedHashMap<>();
66         headerMap.putSingle(Constants.HttpHeaders.HEADER_FROM_APP_ID, "FROM_APP_ID_TEST");
67         when(containerRequest.getHeaders()).thenReturn(headerMap);
68
69         URI uri = null;
70         try {
71             uri = new URI("https://localhost:9999/onap/aai/network/logical-link");
72         }
73         catch (java.net.URISyntaxException e) {
74             throw e;
75         }
76         when(uriInfo.getAbsolutePath()).thenReturn(uri);
77         when(containerRequest.getUriInfo()).thenReturn(uriInfo);
78
79         aaiAuditFilter.filter(containerRequest);
80         assertEquals("FROM_APP_ID_TEST", MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME));
81         assertEquals("/onap/aai/network/logical-link", MDC.get(ONAPLogConstants.MDCs.SERVICE_NAME));
82     }
83
84 }
85