|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.hadoop.crypto.key.kms.server; |
| 19 | + |
| 20 | +import static org.junit.Assert.assertEquals; |
| 21 | +import static org.junit.Assert.assertNull; |
| 22 | +import static org.mockito.Mockito.when; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | + |
| 26 | +import javax.servlet.FilterChain; |
| 27 | +import javax.servlet.ServletException; |
| 28 | +import javax.servlet.ServletRequest; |
| 29 | +import javax.servlet.ServletResponse; |
| 30 | +import javax.servlet.http.HttpServletRequest; |
| 31 | +import javax.servlet.http.HttpServletResponse; |
| 32 | + |
| 33 | +import org.junit.Before; |
| 34 | +import org.junit.Test; |
| 35 | +import org.mockito.Mockito; |
| 36 | + |
| 37 | +/** |
| 38 | + * Test for {@link KMSMDCFilter}. |
| 39 | + * |
| 40 | + */ |
| 41 | +public class TestKMSMDCFilter { |
| 42 | + |
| 43 | + private static final String REMOTE_ADDRESS = "192.168.100.100"; |
| 44 | + private static final String URL = "/admin"; |
| 45 | + private static final String METHOD = "GET"; |
| 46 | + |
| 47 | + private KMSMDCFilter filter; |
| 48 | + private HttpServletRequest httpRequest; |
| 49 | + private HttpServletResponse httpResponse; |
| 50 | + |
| 51 | + @Before |
| 52 | + public void setUp() throws IOException { |
| 53 | + filter = new KMSMDCFilter(); |
| 54 | + httpRequest = Mockito.mock(HttpServletRequest.class); |
| 55 | + httpResponse = Mockito.mock(HttpServletResponse.class); |
| 56 | + KMSMDCFilter.setContext(null, null, null, null); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testFilter() throws IOException, ServletException { |
| 61 | + when(httpRequest.getMethod()).thenReturn(METHOD); |
| 62 | + when(httpRequest.getRequestURL()).thenReturn(new StringBuffer(URL)); |
| 63 | + when(httpRequest.getRemoteAddr()).thenReturn(REMOTE_ADDRESS); |
| 64 | + |
| 65 | + FilterChain filterChain = new FilterChain() { |
| 66 | + @Override |
| 67 | + public void doFilter(ServletRequest request, ServletResponse response) |
| 68 | + throws IOException, ServletException { |
| 69 | + assertEquals("filter.remoteClientAddress", REMOTE_ADDRESS, |
| 70 | + KMSMDCFilter.getRemoteClientAddress()); |
| 71 | + assertEquals("filter.method", METHOD, KMSMDCFilter.getMethod()); |
| 72 | + assertEquals("filter.url", URL, KMSMDCFilter.getURL()); |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + checkMDCValuesAreEmpty(); |
| 77 | + filter.doFilter(httpRequest, httpResponse, filterChain); |
| 78 | + checkMDCValuesAreEmpty(); |
| 79 | + } |
| 80 | + |
| 81 | + private void checkMDCValuesAreEmpty() { |
| 82 | + assertNull("getRemoteClientAddress", KMSMDCFilter.getRemoteClientAddress()); |
| 83 | + assertNull("getMethod", KMSMDCFilter.getMethod()); |
| 84 | + assertNull("getURL", KMSMDCFilter.getURL()); |
| 85 | + assertNull("getUgi", KMSMDCFilter.getUgi()); |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments