Skip to content

Commit

Permalink
remove spring dependencies in dcr endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
lashinijay committed Nov 19, 2024
1 parent 643fdee commit a4f282c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
Expand Down Expand Up @@ -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);

Expand All @@ -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))).
Expand Down Expand Up @@ -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);

Expand All @@ -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);
// }
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand All @@ -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)
Expand All @@ -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);
Expand Down

0 comments on commit a4f282c

Please sign in to comment.