Skip to content

Commit

Permalink
Fixed checkstyle issues and intermittent test failures
Browse files Browse the repository at this point in the history
  • Loading branch information
HasiniSama committed Nov 6, 2024
1 parent 7f7b42c commit a89695a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public OAuthConsumerAppDTO getOAuthApplicationDataByAppName(String appName) thro
/**
* Get OAuth application data by the application name and tenant ID.
*
* @param appName OAuth application name.
* @param appName OAuth application name.
* @param tenantID Tenant ID associated with the OAuth application.
* @return <code>OAuthConsumerAppDTO</code> with application information.
* @throws IdentityOAuthAdminException Error when reading application information from persistence store.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,11 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.wso2.carbon.base.MultitenantConstants.SUPER_TENANT_ID;
import static org.testng.Assert.assertThrows;
import static org.wso2.carbon.base.MultitenantConstants.SUPER_TENANT_ID;
import static org.wso2.carbon.identity.oauth.common.OAuthConstants.ENABLE_CLAIMS_SEPARATION_FOR_ACCESS_TOKEN;
import static org.wso2.carbon.identity.oauth.common.OAuthConstants.OIDC_DIALECT;
import static org.wso2.carbon.utils.multitenancy.MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
import static org.wso2.carbon.utils.multitenancy.MultitenantConstants.SUPER_TENANT_ID;

public class OAuthAdminServiceImplTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

import javax.servlet.http.HttpServletRequest;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -82,13 +83,7 @@ public class PublicClientAuthenticatorTest {
@Mock
private ApplicationManagementService mockedApplicationManagementService;

@Mock
private OAuth2ServiceComponentHolder mockedInstance;

private MockedStatic<IdentityUtil> identityUtil;
private MockedStatic<OAuth2Util> oAuth2Util;
private MockedStatic<OAuth2ServiceComponentHolder> oAuth2ServiceComponentHolderMockedStatic;
private MockedStatic<OAuthServerConfiguration> oAuthServerConfigurationMockedStatic;

@BeforeMethod
public void setUp() {
Expand All @@ -100,26 +95,21 @@ public void setUp() {
Paths.get(System.getProperty("user.dir"), "src", "test", "resources").toString()
);
identityUtil = mockStatic(IdentityUtil.class);
oAuth2ServiceComponentHolderMockedStatic = mockStatic(OAuth2ServiceComponentHolder.class);
oAuthServerConfigurationMockedStatic = mockStatic(OAuthServerConfiguration.class);
identityUtil.when(IdentityUtil::getIdentityConfigDirPath)
.thenReturn(System.getProperty("user.dir")
+ File.separator + "src"
+ File.separator + "test"
+ File.separator + "resources"
+ File.separator + "conf");
oAuthServerConfigurationMockedStatic.when(OAuthServerConfiguration::getInstance).thenReturn(mockedServerConfig);
OAuth2ServiceComponentHolder.getInstance().setApplicationMgtService(mockedApplicationManagementService);
OAuth2ServiceComponentHolder.getInstance().setOAuthAdminService(mockedOAuthAdminService);
publicClientSupportedGrantTypes.add(GRANT_TYPE);
oAuth2Util = mockStatic(OAuth2Util.class);
}

@AfterMethod
public void tearDown() {

identityUtil.close();
oAuth2Util.close();
oAuth2ServiceComponentHolderMockedStatic.close();
oAuthServerConfigurationMockedStatic.close();
}

@Test
Expand All @@ -142,20 +132,24 @@ public void testGetPriority() {
@Test(dataProvider = "testCanAuthenticateData")
public void testCanAuthenticate(String headerName, String headerValue, HashMap<String, List> bodyContent,
boolean publicClient, boolean canHandle,
List<String> publicClientSupportedGrantTypes) throws Exception {
List<String> publicClientSupportedGrantTypes) {

OAuthAppDO appDO = new OAuthAppDO();
appDO.setBypassClientCredentials(publicClient);
appDO.setApplicationName(APPLICATION_NAME);
try (MockedStatic<OAuth2Util> oAuth2Util = mockStatic(OAuth2Util.class);
MockedStatic<OAuthServerConfiguration>
oAuthServerConfiguration = mockStatic(OAuthServerConfiguration.class)) {

when(mockedServerConfig.getPublicClientSupportedGrantTypesList())
.thenReturn(publicClientSupportedGrantTypes);
lenient().when(mockedHttpServletRequest.getHeader(headerName)).thenReturn(headerValue);
oAuth2Util.when(() -> OAuth2Util.getAppInformationByClientId(CLIENT_ID, SUPER_TENANT_DOMAIN_NAME))
.thenReturn(appDO);
oAuthServerConfiguration.when(OAuthServerConfiguration::getInstance).thenReturn(mockedServerConfig);
when(mockedServerConfig.getPublicClientSupportedGrantTypesList())
.thenReturn(publicClientSupportedGrantTypes);

assertEquals(publicClientAuthenticator.canAuthenticate(mockedHttpServletRequest, bodyContent, new
OAuthClientAuthnContext()), canHandle, "Expected can authenticate evaluation not received");
OAuthAppDO appDO = new OAuthAppDO();
appDO.setBypassClientCredentials(publicClient);
oAuth2Util.when(() -> OAuth2Util.getAppInformationByClientId(anyString(), anyString())).thenReturn(appDO);
lenient().when(mockedHttpServletRequest.getHeader(headerName)).thenReturn(headerValue);

assertEquals(publicClientAuthenticator.canAuthenticate(mockedHttpServletRequest, bodyContent,
new OAuthClientAuthnContext()), canHandle, "Expected can authenticate evaluation not received");
}
}

/**
Expand Down Expand Up @@ -274,25 +268,31 @@ public void testPublicClientSharedAppInAPIBasedAuthFlow(HashMap<String, List> bo
boolean isPublicClientParent, boolean canHandle)
throws Exception {

OAuthAppDO appDO = new OAuthAppDO();
appDO.setBypassClientCredentials(isPublicClient);
appDO.setApplicationName(APPLICATION_NAME);

OAuthConsumerAppDTO mainOAuthAppDO = new OAuthConsumerAppDTO();
mainOAuthAppDO.setBypassClientCredentials(isPublicClientParent);

oAuth2Util.when(() -> OAuth2Util.getAppInformationByClientId(CLIENT_ID, SUPER_TENANT_DOMAIN_NAME))
.thenReturn(appDO);
oAuth2Util.when(() -> OAuth2Util.isApiBasedAuthenticationFlow(mockedHttpServletRequest)).thenReturn(true);
when(mockedServerConfig.getPublicClientSupportedGrantTypesList()).thenReturn(publicClientSupportedGrantTypes);
when(OAuth2ServiceComponentHolder.getInstance()).thenReturn(mockedInstance);
when(OAuth2ServiceComponentHolder.getApplicationMgtService()).thenReturn(mockedApplicationManagementService);
lenient().when(mockedInstance.getOAuthAdminService()).thenReturn(mockedOAuthAdminService);
lenient().when(mockedOAuthAdminService.getOAuthApplicationDataByAppName(APPLICATION_NAME, 0))
.thenReturn(mainOAuthAppDO);
lenient().when(mockedHttpServletRequest.getHeader(SIMPLE_CASE_AUTHORIZATION_HEADER)).thenReturn(null);

assertEquals(publicClientAuthenticator.canAuthenticate(mockedHttpServletRequest, bodyContent,
new OAuthClientAuthnContext()), canHandle, "Expected authenticate evaluation not received");
try (MockedStatic<OAuth2Util> oAuth2Util = mockStatic(OAuth2Util.class);
MockedStatic<OAuthServerConfiguration>
oAuthServerConfiguration = mockStatic(OAuthServerConfiguration.class)) {

oAuthServerConfiguration.when(OAuthServerConfiguration::getInstance).thenReturn(mockedServerConfig);
when(mockedServerConfig.getPublicClientSupportedGrantTypesList())
.thenReturn(publicClientSupportedGrantTypes);

OAuthAppDO oAuthAppDO = new OAuthAppDO();
oAuthAppDO.setBypassClientCredentials(isPublicClient);
oAuthAppDO.setApplicationName(APPLICATION_NAME);

OAuthConsumerAppDTO mainOAuthAppDO = new OAuthConsumerAppDTO();
mainOAuthAppDO.setBypassClientCredentials(isPublicClientParent);

oAuth2Util.when(() -> OAuth2Util.getAppInformationByClientId(anyString(), anyString()))
.thenReturn(oAuthAppDO);
oAuth2Util.when(() -> OAuth2Util.isApiBasedAuthenticationFlow(mockedHttpServletRequest))
.thenReturn(true);
lenient().when(mockedOAuthAdminService.getOAuthApplicationDataByAppName(APPLICATION_NAME, 0))
.thenReturn(mainOAuthAppDO);
lenient().when(mockedHttpServletRequest.getHeader(SIMPLE_CASE_AUTHORIZATION_HEADER)).thenReturn(null);

assertEquals(publicClientAuthenticator.canAuthenticate(mockedHttpServletRequest, bodyContent,
new OAuthClientAuthnContext()), canHandle, "Expected authenticate evaluation not received");
}
}
}

0 comments on commit a89695a

Please sign in to comment.