NIFI-11613 Refactored SanitizeContextPathFilterTest from Groovy to Java

- Moved Java tests from groovy directory to java directory and removed groovy directory from nifi-web-utils

This closes #7333

Signed-off-by: David Handermann <exceptionfactory@apache.org>
This commit is contained in:
dan-s1 2023-06-02 16:20:58 +00:00 committed by exceptionfactory
parent 852ec9eefc
commit 27b5a2f931
No known key found for this signature in database
GPG Key ID: 29B6A52D2AAE8DBA
4 changed files with 99 additions and 120 deletions

View File

@ -1,120 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.nifi.web.filter
import org.junit.jupiter.api.Test
import javax.servlet.FilterConfig
import javax.servlet.ServletContext
import javax.servlet.http.HttpServletRequest
import static org.junit.jupiter.api.Assertions.assertEquals
class SanitizeContextPathFilterTest {
private static String getValue(String parameterName, Map<String, String> params = [:]) {
params.containsKey(parameterName) ? params[parameterName] : ""
}
@Test
void testInitShouldExtractAllowedContextPaths() {
// Arrange
def EXPECTED_ALLOWED_CONTEXT_PATHS = ["/path1", "/path2"].join(", ")
def parameters = [allowedContextPaths: EXPECTED_ALLOWED_CONTEXT_PATHS]
FilterConfig mockFilterConfig = [
getInitParameter : { String parameterName -> getValue(parameterName, parameters) },
getServletContext: { ->
[getInitParameter: { String parameterName -> return getValue(parameterName, parameters) }] as ServletContext
}] as FilterConfig
SanitizeContextPathFilter scpf = new SanitizeContextPathFilter()
// Act
scpf.init(mockFilterConfig)
// Assert
assertEquals(EXPECTED_ALLOWED_CONTEXT_PATHS, scpf.getAllowedContextPaths())
}
@Test
void testInitShouldHandleBlankAllowedContextPaths() {
// Arrange
def EXPECTED_ALLOWED_CONTEXT_PATHS = ""
FilterConfig mockFilterConfig = [
getInitParameter : { String parameterName -> "" },
getServletContext: { ->
[getInitParameter: { String parameterName -> "" }] as ServletContext
}] as FilterConfig
SanitizeContextPathFilter scpf = new SanitizeContextPathFilter()
// Act
scpf.init(mockFilterConfig)
// Assert
assertEquals(EXPECTED_ALLOWED_CONTEXT_PATHS, scpf.getAllowedContextPaths())
}
@Test
void testShouldInjectContextPathAttribute() {
// Arrange
final String EXPECTED_ALLOWED_CONTEXT_PATHS = ["/path1", "/path2"].join(", ")
final String EXPECTED_FORWARD_PATH = "index.jsp"
final Map PARAMETERS = [
allowedContextPaths: EXPECTED_ALLOWED_CONTEXT_PATHS,
forwardPath : EXPECTED_FORWARD_PATH
]
final String EXPECTED_CONTEXT_PATH = "/path1"
// Mock collaborators
FilterConfig mockFilterConfig = [
getInitParameter : { String parameterName ->
return getValue(parameterName, PARAMETERS)
},
getServletContext: { ->
[getInitParameter: { String parameterName ->
return getValue(parameterName, PARAMETERS)
}] as ServletContext
}] as FilterConfig
// Local map to store request attributes
def requestAttributes = [:]
final Map HEADERS = [
"X-ProxyContextPath" : "path1",
"X-Forwarded-Context": "",
"X-Forwarded-Prefix" : ""]
HttpServletRequest mockRequest = [
getContextPath : { -> EXPECTED_CONTEXT_PATH },
getHeader : { String headerName -> getValue(headerName, HEADERS) },
setAttribute : { String attr, String value ->
requestAttributes[attr] = value
},
] as HttpServletRequest
SanitizeContextPathFilter scpf = new SanitizeContextPathFilter()
scpf.init(mockFilterConfig)
// Act
scpf.injectContextPathAttribute(mockRequest)
// Assert
assertEquals(EXPECTED_CONTEXT_PATH, requestAttributes["contextPath"])
}
}

View File

@ -0,0 +1,99 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.nifi.web.filter;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.stubbing.Answer;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
public class SanitizeContextPathFilterTest {
@Test
public void testInitShouldExtractAllowedContextPaths(@Mock ServletContext servletContext, @Mock FilterConfig filterConfig) throws ServletException {
final String expectedAllowedContextPaths = String.join(",", new String[]{"/path1", "/path2"});
final Map<String, String> parameters = Collections.singletonMap("allowedContextPaths", expectedAllowedContextPaths);
when(servletContext.getInitParameter(anyString())).thenAnswer(
(Answer<String>) invocation -> getValue(invocation.getArgument(0, String.class), parameters));
when(filterConfig.getServletContext()).thenReturn(servletContext);
SanitizeContextPathFilter sanitizeContextPathFilter = new SanitizeContextPathFilter();
sanitizeContextPathFilter.init(filterConfig);
assertEquals(expectedAllowedContextPaths, sanitizeContextPathFilter.getAllowedContextPaths());
}
@Test
public void testInitShouldHandleBlankAllowedContextPaths(@Mock ServletContext servletContext, @Mock FilterConfig filterConfig) throws ServletException {
when(servletContext.getInitParameter(anyString())).thenReturn("");
when(filterConfig.getServletContext()).thenReturn(servletContext);
SanitizeContextPathFilter sanitizeContextPathFilter = new SanitizeContextPathFilter();
sanitizeContextPathFilter.init(filterConfig);
assertEquals("", sanitizeContextPathFilter.getAllowedContextPaths());
}
@Test
public void testShouldInjectContextPathAttribute(@Mock ServletContext servletContext, @Mock FilterConfig filterConfig,
@Mock HttpServletRequest httpServletRequest) throws ServletException {
final String expectedAllowedContextPaths = String.join(",", new String[]{"/path1", "/path2"});
final String expectedForwardPath = "index.jsp";
final Map<String, String> parameters = new HashMap<>();
parameters.put("allowedContextPaths", expectedAllowedContextPaths);
parameters.put("forwardPath", expectedForwardPath);
final String expectedContextPath = "/path1";
final Map<String, String> headers = new HashMap<>();
headers.put("X-ProxyContextPath", "path1");
headers.put("X-Forwarded-Context", "");
headers.put("X-Forwarded-Prefix", "");
final Map<String, Object> requestAttributes = new HashMap<>();
when(servletContext.getInitParameter(anyString())).thenAnswer(
(Answer<String>) invocation -> getValue(invocation.getArgument(0, String.class), parameters));
when(filterConfig.getServletContext()).thenReturn(servletContext);
when(httpServletRequest.getHeader(anyString())).thenAnswer(
(Answer<String>) invocation -> getValue(invocation.getArgument(0, String.class), headers));
doAnswer((Answer<Void>) invocation -> {
requestAttributes.put(invocation.getArgument(0, String.class), invocation.getArgument(1, Object.class));
return null;
}).when(httpServletRequest).setAttribute(anyString(), any());
SanitizeContextPathFilter sanitizeContextPathFilter = new SanitizeContextPathFilter();
sanitizeContextPathFilter.init(filterConfig);
sanitizeContextPathFilter.injectContextPathAttribute(httpServletRequest);
assertEquals(expectedContextPath, requestAttributes.get("contextPath"));
}
private static String getValue(String parameterName, Map<String, String> params) {
return params != null && params.containsKey(parameterName) ? params.get(parameterName) : "";
}
}