Skip to content

Commit

Permalink
Test fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
snoopdave committed Jan 20, 2025
1 parent 9a75569 commit 5e5835b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<UIBeanFactory> 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<UIBeanFactory> mockedUIBeanFactory = mockStatic(UIBeanFactory.class);
MockedStatic<SaltCache> mockedSaltCache = mockStatic(SaltCache.class)) {

mockedUIBeanFactory.when(() -> UIBeanFactory.getBean(RollerSession.class)).thenReturn(rollerSession);
try (MockedStatic<SaltCache> 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<UIBeanFactory> mockedUIBeanFactory = mockStatic(UIBeanFactory.class);
MockedStatic<SaltCache> mockedSaltCache = mockStatic(SaltCache.class)) {

mockedUIBeanFactory.when(() -> UIBeanFactory.getBean(RollerSession.class)).thenReturn(null);
mockedSaltCache.when(SaltCache::getInstance).thenReturn(saltCache);
try (MockedStatic<UIBeanFactory> 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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,26 +116,34 @@ public void testDoFilterWithPostMethodAndMismatchedUserId() throws Exception {

@Test
public void testDoFilterWithPostMethodAndNullRollerSession() throws Exception {
try (MockedStatic<SaltCache> mockedSaltCache = mockStatic(SaltCache.class)) {
try (MockedStatic<SaltCache> mockedSaltCache = mockStatic(SaltCache.class);
MockedStatic<UIBeanFactory> 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");
when(saltCache.get("validSalt")).thenReturn("");
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<WebloggerConfig> mockedWebloggerConfig = mockStatic(WebloggerConfig.class)) {
try (MockedStatic<WebloggerConfig> mockedWebloggerConfig = mockStatic(WebloggerConfig.class);
MockedStatic<UIBeanFactory> 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");
Expand Down

0 comments on commit 5e5835b

Please sign in to comment.