This is an automated email from the ASF dual-hosted git repository. exceptionfactory pushed a commit to branch support/nifi-1.x in repository https://gitbox.apache.org/repos/asf/nifi.git
commit 9df77971d11275e495ffdf8d52040e7922e005e0 Author: dan-s1 <dsti...@gmail.com> AuthorDate: Fri Jun 2 16:20:58 2023 +0000 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 <exceptionfact...@apache.org> (cherry picked from commit 27b5a2f931e10ce1f25351e8b1070924a3ece196) --- .../filter/SanitizeContextPathFilterTest.groovy | 120 --------------------- .../web/filter/SanitizeContextPathFilterTest.java | 99 +++++++++++++++++ .../nifi/web/util/RequestUriBuilderTest.java | 0 .../org/apache/nifi/web/util/WebUtilsTest.java | 0 4 files changed, 99 insertions(+), 120 deletions(-) diff --git a/nifi-commons/nifi-web-utils/src/test/groovy/org/apache/nifi/web/filter/SanitizeContextPathFilterTest.groovy b/nifi-commons/nifi-web-utils/src/test/groovy/org/apache/nifi/web/filter/SanitizeContextPathFilterTest.groovy deleted file mode 100644 index 5734d3cd76..0000000000 --- a/nifi-commons/nifi-web-utils/src/test/groovy/org/apache/nifi/web/filter/SanitizeContextPathFilterTest.groovy +++ /dev/null @@ -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"]) - } -} diff --git a/nifi-commons/nifi-web-utils/src/test/java/org/apache/nifi/web/filter/SanitizeContextPathFilterTest.java b/nifi-commons/nifi-web-utils/src/test/java/org/apache/nifi/web/filter/SanitizeContextPathFilterTest.java new file mode 100644 index 0000000000..3c53c28d30 --- /dev/null +++ b/nifi-commons/nifi-web-utils/src/test/java/org/apache/nifi/web/filter/SanitizeContextPathFilterTest.java @@ -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) : ""; + } +} diff --git a/nifi-commons/nifi-web-utils/src/test/groovy/org/apache/nifi/web/util/RequestUriBuilderTest.java b/nifi-commons/nifi-web-utils/src/test/java/org/apache/nifi/web/util/RequestUriBuilderTest.java similarity index 100% rename from nifi-commons/nifi-web-utils/src/test/groovy/org/apache/nifi/web/util/RequestUriBuilderTest.java rename to nifi-commons/nifi-web-utils/src/test/java/org/apache/nifi/web/util/RequestUriBuilderTest.java diff --git a/nifi-commons/nifi-web-utils/src/test/groovy/org/apache/nifi/web/util/WebUtilsTest.java b/nifi-commons/nifi-web-utils/src/test/java/org/apache/nifi/web/util/WebUtilsTest.java similarity index 100% rename from nifi-commons/nifi-web-utils/src/test/groovy/org/apache/nifi/web/util/WebUtilsTest.java rename to nifi-commons/nifi-web-utils/src/test/java/org/apache/nifi/web/util/WebUtilsTest.java