From a4f282caa423055cfb8ee5a08157a839cd20994f Mon Sep 17 00:00:00 2001 From: lashinie <lashinisharikaj@gmail.com> Date: Tue, 19 Nov 2024 12:23:22 +0530 Subject: [PATCH] remove spring dependencies in dcr endpoint --- .../oauth2/dcr/endpoint/util/DCRMUtils.java | 10 +++++-- .../RegisterApiServiceImplExceptionTest.java | 30 +++++++++---------- .../impl/RegisterApiServiceImplTest.java | 15 +++++++--- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/components/org.wso2.carbon.identity.api.server.dcr/src/main/java/org/wso2/carbon/identity/oauth2/dcr/endpoint/util/DCRMUtils.java b/components/org.wso2.carbon.identity.api.server.dcr/src/main/java/org/wso2/carbon/identity/oauth2/dcr/endpoint/util/DCRMUtils.java index 777f367fee..ecc90ab31c 100644 --- a/components/org.wso2.carbon.identity.api.server.dcr/src/main/java/org/wso2/carbon/identity/oauth2/dcr/endpoint/util/DCRMUtils.java +++ b/components/org.wso2.carbon.identity.api.server.dcr/src/main/java/org/wso2/carbon/identity/oauth2/dcr/endpoint/util/DCRMUtils.java @@ -45,12 +45,16 @@ public class DCRMUtils { private static final String NOT_FOUND_STATUS = "NOT_FOUND_"; private static final String FORBIDDEN_STATUS = "FORBIDDEN_"; - private static final DCRMService oAuth2DCRMService = (DCRMService) PrivilegedCarbonContext - .getThreadLocalCarbonContext().getOSGiService(DCRMService.class, null); + + private static class OAuth2DCRMServiceHolder { + + static final DCRMService SERVICE = (DCRMService) PrivilegedCarbonContext + .getThreadLocalCarbonContext().getOSGiService(DCRMService.class, null); + } public static DCRMService getOAuth2DCRMService() { - return oAuth2DCRMService; + return OAuth2DCRMServiceHolder.SERVICE; } public static ApplicationRegistrationRequest getApplicationRegistrationRequest( diff --git a/components/org.wso2.carbon.identity.api.server.dcr/src/test/java/org/wso2/carbon/identity/oauth2/dcr/endpoint/impl/RegisterApiServiceImplExceptionTest.java b/components/org.wso2.carbon.identity.api.server.dcr/src/test/java/org/wso2/carbon/identity/oauth2/dcr/endpoint/impl/RegisterApiServiceImplExceptionTest.java index 19d171118c..47baeb42fe 100644 --- a/components/org.wso2.carbon.identity.api.server.dcr/src/test/java/org/wso2/carbon/identity/oauth2/dcr/endpoint/impl/RegisterApiServiceImplExceptionTest.java +++ b/components/org.wso2.carbon.identity.api.server.dcr/src/test/java/org/wso2/carbon/identity/oauth2/dcr/endpoint/impl/RegisterApiServiceImplExceptionTest.java @@ -106,7 +106,7 @@ public void tearDown() { public void testDeleteApplicationClientException() throws Exception { try { - mockPrivilegedCarbonContext(); +// mockPrivilegedCarbonContext(); registerApiService.deleteApplication(""); } catch (DCRMEndpointException e) { assertEquals(e.getResponse().getStatus(), Response.Status.BAD_REQUEST.getStatusCode()); @@ -128,7 +128,7 @@ public void testDeleteApplicationThrowableException() throws DCRMException { public void testGetApplicationClientException() throws Exception { try { - mockPrivilegedCarbonContext(); +// mockPrivilegedCarbonContext(); registerApiService.getApplication(""); } catch (DCRMEndpointException e) { assertEquals(e.getResponse().getStatus(), Response.Status.BAD_REQUEST.getStatusCode()); @@ -157,7 +157,7 @@ public void testRegisterApplicationClientException() throws DCRMException { registrationRequestDTO.setClientName("Test App"); registrationRequestDTO.setGrantTypes(granttypes); registrationRequestDTO.setRedirectUris(redirectUris); - mockPrivilegedCarbonContext(); +// mockPrivilegedCarbonContext(); dcrDataHolder.when(DCRDataHolder::getInstance).thenReturn(dataHolder); lenient().when(dataHolder.getApplicationManagementService()).thenReturn(applicationManagementService); @@ -182,7 +182,7 @@ public void testRegisterApplicationServerException() throws DCRMException, Ident registrationRequestDTO.setGrantTypes(granttypes); registrationRequestDTO.setRedirectUris(redirectUris); - mockPrivilegedCarbonContext(); +// mockPrivilegedCarbonContext(); dcrDataHolder.when(DCRDataHolder::getInstance).thenReturn(dataHolder); lenient().when(dataHolder.getApplicationManagementService()).thenReturn(applicationManagementService); lenient().when(applicationManagementService.getServiceProvider(any(String.class), any(String.class))). @@ -220,7 +220,7 @@ public void testUpdateApplicationClientException() throws DCRMException { updateRequestDTO.setClientName("Test App"); updateRequestDTO.setGrantTypes(granttypes); updateRequestDTO.setRedirectUris(redirectUris); - mockPrivilegedCarbonContext(); +// mockPrivilegedCarbonContext(); dcrDataHolder.when(DCRDataHolder::getInstance).thenReturn(dataHolder); lenient().when(dataHolder.getApplicationManagementService()).thenReturn(applicationManagementService); @@ -245,14 +245,14 @@ public void testUpdateApplicationThrowableException() throws DCRMException { } } - private void mockPrivilegedCarbonContext() { - - try (MockedStatic<PrivilegedCarbonContext> privilegedCarbonContext = - mockStatic(PrivilegedCarbonContext.class)) { - privilegedCarbonContext.when(PrivilegedCarbonContext::getThreadLocalCarbonContext) - .thenReturn(mockedPrivilegedCarbonContext); - lenient().when(mockedPrivilegedCarbonContext.getOSGiService(DCRMService.class, null)). - thenReturn(mockedDCRMService); - } - } +// private void mockPrivilegedCarbonContext() { +// +// try (MockedStatic<PrivilegedCarbonContext> privilegedCarbonContext = +// mockStatic(PrivilegedCarbonContext.class)) { +// privilegedCarbonContext.when(PrivilegedCarbonContext::getThreadLocalCarbonContext) +// .thenReturn(mockedPrivilegedCarbonContext); +// lenient().when(mockedPrivilegedCarbonContext.getOSGiService(DCRMService.class, null)). +// thenReturn(mockedDCRMService); +// } +// } } diff --git a/components/org.wso2.carbon.identity.api.server.dcr/src/test/java/org/wso2/carbon/identity/oauth2/dcr/endpoint/impl/RegisterApiServiceImplTest.java b/components/org.wso2.carbon.identity.api.server.dcr/src/test/java/org/wso2/carbon/identity/oauth2/dcr/endpoint/impl/RegisterApiServiceImplTest.java index 9011f66797..29408bb6fb 100644 --- a/components/org.wso2.carbon.identity.api.server.dcr/src/test/java/org/wso2/carbon/identity/oauth2/dcr/endpoint/impl/RegisterApiServiceImplTest.java +++ b/components/org.wso2.carbon.identity.api.server.dcr/src/test/java/org/wso2/carbon/identity/oauth2/dcr/endpoint/impl/RegisterApiServiceImplTest.java @@ -26,6 +26,7 @@ import org.osgi.util.tracker.ServiceTracker; import org.testng.Assert; import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Listeners; import org.testng.annotations.Test; @@ -75,6 +76,12 @@ public class RegisterApiServiceImplTest { MockedConstruction<ServiceTracker> mockedConstruction; + @BeforeClass + public void initTest() throws Exception { + + mockPrivilegedCarbonContext(); + } + @BeforeMethod public void setUp() throws Exception { //Initializing variables. @@ -110,7 +117,7 @@ public void tearDown() { @Test public void testDeleteApplication() throws Exception { - mockPrivilegedCarbonContext(); +// mockPrivilegedCarbonContext(); doNothing().when(dcrmService).deleteApplication(validclientId); Assert.assertEquals(registerApiService.deleteApplication(validclientId).getStatus(), Response.Status.NO_CONTENT.getStatusCode()); @@ -131,7 +138,7 @@ public void testDeleteApplicationServerException() throws Exception { @Test public void testGetApplication() throws Exception { - mockPrivilegedCarbonContext(); +// mockPrivilegedCarbonContext(); when(dcrmService.getApplication(validclientId)).thenReturn(application); Assert.assertEquals(registerApiService.getApplication(validclientId).getStatus(), Response.Status.OK.getStatusCode()); @@ -157,7 +164,7 @@ public void testRegisterApplication() throws Exception { RegistrationRequestDTO registrationRequestDTO = new RegistrationRequestDTO(); registrationRequestDTO.setClientName("app1"); - mockPrivilegedCarbonContext(); +// mockPrivilegedCarbonContext(); when(dcrmService.registerApplication(any(ApplicationRegistrationRequest.class))) .thenReturn(application); Assert.assertEquals(registerApiService.registerApplication(registrationRequestDTO) @@ -183,7 +190,7 @@ public void testUpdateApplication() throws Exception { UpdateRequestDTO updateRequestDTO1 = new UpdateRequestDTO(); updateRequestDTO1.setClientName("Client1"); String clientID = "clientID1"; - mockPrivilegedCarbonContext(); +// mockPrivilegedCarbonContext(); when(dcrmService.updateApplication (any(ApplicationUpdateRequest.class), anyString())) .thenReturn(application);