1212import jakarta .servlet .http .HttpServletRequest ;
1313import jakarta .servlet .http .HttpServletResponse ;
1414import jakarta .servlet .http .HttpSession ;
15+ import org .junit .jupiter .api .AfterEach ;
1516import org .junit .jupiter .api .BeforeEach ;
1617import org .junit .jupiter .api .DisplayName ;
1718import org .junit .jupiter .api .Test ;
1819import org .mockito .Mock ;
20+ import org .mockito .MockedStatic ;
21+ import org .mockito .Mockito ;
1922import org .mockito .MockitoAnnotations ;
2023
2124import java .io .IOException ;
25+ import java .net .URL ;
2226import java .security .Principal ;
27+ import java .util .Map ;
2328
24- import static com .kinde .constants .KindeConstants .CONNECTION_ID ;
29+ import static com .kinde .constants .KindeConstants .* ;
2530import static org .junit .jupiter .api .Assertions .assertTrue ;
2631import static org .mockito .ArgumentMatchers .any ;
2732import static org .mockito .ArgumentMatchers .anyString ;
33+ import static org .mockito .ArgumentMatchers .argThat ;
2834import static org .mockito .Mockito .*;
2935
3036public class ConnectionIdFilterTest {
@@ -57,19 +63,31 @@ public class ConnectionIdFilterTest {
5763 private AuthorizationUrl authorizationUrl ;
5864
5965 private KindeLoginFilter filter ;
66+ private MockedStatic <KindeSingleton > kindeSingletonStatic ;
6067
6168 @ BeforeEach
6269 public void setUp () {
6370 MockitoAnnotations .openMocks (this );
6471 filter = new KindeLoginFilter ();
6572
73+ // Static mocking for KindeSingleton
74+ kindeSingletonStatic = Mockito .mockStatic (KindeSingleton .class );
75+ kindeSingletonStatic .when (KindeSingleton ::getInstance ).thenReturn (kindeSingleton );
76+
6677 when (request .getSession ()).thenReturn (session );
6778 when (request .getRequestURL ()).thenReturn (new StringBuffer ("http://localhost:8080/test" ));
6879 when (session .getAttribute (anyString ())).thenReturn (null );
6980 when (request .getParameter ("code" )).thenReturn (null );
7081 when (request .getParameter ("error" )).thenReturn (null );
7182 }
7283
84+ @ AfterEach
85+ public void tearDown () {
86+ if (kindeSingletonStatic != null ) {
87+ kindeSingletonStatic .close ();
88+ }
89+ }
90+
7391 @ Test
7492 @ DisplayName ("Filter should include connection_id in authorization URL when provided as request parameter" )
7593 public void testFilterWithConnectionId () throws ServletException , IOException {
@@ -79,8 +97,7 @@ public void testFilterWithConnectionId() throws ServletException, IOException {
7997 when (request .getParameter ("org_code" )).thenReturn (null );
8098 when (request .getParameter ("lang" )).thenReturn (null );
8199
82- // Mock KindeSingleton
83- KindeSingleton .setInstance (kindeSingleton );
100+ // Mock KindeSingleton chain
84101 when (kindeSingleton .getKindeClientBuilder ()).thenReturn (kindeClientBuilder );
85102 when (kindeClientBuilder .redirectUri (anyString ())).thenReturn (kindeClientBuilder );
86103 when (kindeClientBuilder .grantType (any (AuthorizationType .class ))).thenReturn (kindeClientBuilder );
@@ -90,13 +107,13 @@ public void testFilterWithConnectionId() throws ServletException, IOException {
90107 when (kindeClientBuilder .build ()).thenReturn (kindeClient );
91108 when (kindeClient .clientSession ()).thenReturn (kindeClientSession );
92109 when (kindeClientSession .authorizationUrlWithParameters (any ())).thenReturn (authorizationUrl );
93- when (authorizationUrl .getUrl ()).thenReturn (new java . net . URL ("http://example.com/auth?connection_id=" + connectionId ));
110+ when (authorizationUrl .getUrl ()).thenReturn (new URL ("http://example.com/auth?connection_id=" + connectionId ));
94111
95112 // Execute
96113 filter .doFilter (request , response , filterChain );
97114
98115 // Verify
99- verify (kindeClientSession ).authorizationUrlWithParameters (argThat (params ->
116+ verify (kindeClientSession ).authorizationUrlWithParameters (argThat (( Map < String , String > params ) ->
100117 params .containsKey (CONNECTION_ID ) &&
101118 params .get (CONNECTION_ID ).equals (connectionId ) &&
102119 params .containsKey ("supports_reauth" )
@@ -112,8 +129,7 @@ public void testFilterWithoutConnectionId() throws ServletException, IOException
112129 when (request .getParameter ("org_code" )).thenReturn (null );
113130 when (request .getParameter ("lang" )).thenReturn (null );
114131
115- // Mock KindeSingleton
116- KindeSingleton .setInstance (kindeSingleton );
132+ // Mock KindeSingleton chain
117133 when (kindeSingleton .getKindeClientBuilder ()).thenReturn (kindeClientBuilder );
118134 when (kindeClientBuilder .redirectUri (anyString ())).thenReturn (kindeClientBuilder );
119135 when (kindeClientBuilder .grantType (any (AuthorizationType .class ))).thenReturn (kindeClientBuilder );
@@ -123,7 +139,7 @@ public void testFilterWithoutConnectionId() throws ServletException, IOException
123139 when (kindeClientBuilder .build ()).thenReturn (kindeClient );
124140 when (kindeClient .clientSession ()).thenReturn (kindeClientSession );
125141 when (kindeClientSession .login ()).thenReturn (authorizationUrl );
126- when (authorizationUrl .getUrl ()).thenReturn (new java . net . URL ("http://example.com/auth" ));
142+ when (authorizationUrl .getUrl ()).thenReturn (new URL ("http://example.com/auth" ));
127143
128144 // Execute
129145 filter .doFilter (request , response , filterChain );
0 commit comments