diff --git a/app/src/test/java/org/apache/roller/weblogger/ui/core/filters/LoadSaltFilterTest.java b/app/src/test/java/org/apache/roller/weblogger/ui/core/filters/LoadSaltFilterTest.java index 6501e72b3..10079a82b 100644 --- a/app/src/test/java/org/apache/roller/weblogger/ui/core/filters/LoadSaltFilterTest.java +++ b/app/src/test/java/org/apache/roller/weblogger/ui/core/filters/LoadSaltFilterTest.java @@ -11,6 +11,8 @@ import org.mockito.MockitoAnnotations; import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -36,40 +38,39 @@ public class LoadSaltFilterTest { private SaltCache saltCache; @BeforeEach - public void setUp() { - MockitoAnnotations.initMocks(this); - filter = new LoadSaltFilter(); + public void setUp() throws ServletException { + MockitoAnnotations.openMocks(this); + + try (MockedStatic mockedFactory = mockStatic(UIBeanFactory.class)) { + mockedFactory.when(() -> UIBeanFactory.getBean(RollerSession.class)) + .thenReturn(rollerSession); + + filter = new LoadSaltFilter(); + filter.init(mock(FilterConfig.class)); + } } @Test public void testDoFilterGeneratesSalt() throws Exception { - try (MockedStatic mockedUIBeanFactory = mockStatic(UIBeanFactory.class); - MockedStatic mockedSaltCache = mockStatic(SaltCache.class)) { - - mockedUIBeanFactory.when(() -> UIBeanFactory.getBean(RollerSession.class)).thenReturn(rollerSession); + try (MockedStatic mockedSaltCache = mockStatic(SaltCache.class)) { mockedSaltCache.when(SaltCache::getInstance).thenReturn(saltCache); - when(rollerSession.getAuthenticatedUser()).thenReturn(new TestUser("userId")); - filter.doFilter(request, response, chain); - verify(request).setAttribute(eq("salt"), anyString()); - verify(saltCache).put(anyString(), eq("userId")); verify(chain).doFilter(request, response); } } + @Test public void testDoFilterWithNullRollerSession() throws Exception { - try (MockedStatic mockedUIBeanFactory = mockStatic(UIBeanFactory.class); - MockedStatic mockedSaltCache = mockStatic(SaltCache.class)) { - - mockedUIBeanFactory.when(() -> UIBeanFactory.getBean(RollerSession.class)).thenReturn(null); - mockedSaltCache.when(SaltCache::getInstance).thenReturn(saltCache); + try (MockedStatic mockedUIBeanFactory = mockStatic(UIBeanFactory.class)) { + mockedUIBeanFactory.when(() -> UIBeanFactory.getBean(RollerSession.class)) + .thenReturn(null); + filter.init(mock(FilterConfig.class)); filter.doFilter(request, response, chain); verify(request, never()).setAttribute(eq("salt"), anyString()); - verify(saltCache, never()).put(anyString(), anyString()); verify(chain).doFilter(request, response); } } diff --git a/app/src/test/java/org/apache/roller/weblogger/ui/core/filters/ValidateSaltFilterTest.java b/app/src/test/java/org/apache/roller/weblogger/ui/core/filters/ValidateSaltFilterTest.java index f1218f725..d8c04871b 100644 --- a/app/src/test/java/org/apache/roller/weblogger/ui/core/filters/ValidateSaltFilterTest.java +++ b/app/src/test/java/org/apache/roller/weblogger/ui/core/filters/ValidateSaltFilterTest.java @@ -116,8 +116,11 @@ public void testDoFilterWithPostMethodAndMismatchedUserId() throws Exception { @Test public void testDoFilterWithPostMethodAndNullRollerSession() throws Exception { - try (MockedStatic mockedSaltCache = mockStatic(SaltCache.class)) { + try (MockedStatic mockedSaltCache = mockStatic(SaltCache.class); + MockedStatic mockedUIBeanFactory = mockStatic(UIBeanFactory.class)) { + mockedSaltCache.when(SaltCache::getInstance).thenReturn(saltCache); + mockedUIBeanFactory.when(() -> UIBeanFactory.getBean(RollerSession.class)).thenReturn(null); when(request.getMethod()).thenReturn("POST"); when(request.getParameter("salt")).thenReturn("validSalt"); @@ -125,17 +128,22 @@ public void testDoFilterWithPostMethodAndNullRollerSession() throws Exception { StringBuffer requestURL = new StringBuffer("https://example.com/app/ignoredurl"); when(request.getRequestURL()).thenReturn(requestURL); + filter.init(mock(FilterConfig.class)); filter.doFilter(request, response, chain); - verify(saltCache, never()).remove("validSalt"); + verify(chain).doFilter(request, response); + verify(saltCache, never()).remove(anyString()); } } @Test public void testDoFilterWithIgnoredURL() throws Exception { - try (MockedStatic mockedWebloggerConfig = mockStatic(WebloggerConfig.class)) { + try (MockedStatic mockedWebloggerConfig = mockStatic(WebloggerConfig.class); + MockedStatic mockedUIBeanFactory = mockStatic(UIBeanFactory.class)) { + mockedWebloggerConfig.when(() -> WebloggerConfig.getProperty("salt.ignored.urls")) .thenReturn("https://example.com/app/ignoredurl?param1=value1&m2=value2"); + mockedUIBeanFactory.when(() -> UIBeanFactory.getBean(RollerSession.class)).thenReturn(rollerSession); when(request.getMethod()).thenReturn("POST"); StringBuffer requestURL = new StringBuffer("https://example.com/app/ignoredurl");