Skip to content

Commit

Permalink
Merge pull request #2679 from lashinijay/master-revamp-dcr-endpoint
Browse files Browse the repository at this point in the history
[Spring Cleanup] Refactor dcr endpoint to remove Spring dependency
  • Loading branch information
lashinijay authored Jan 21, 2025
2 parents 7782ea7 + e3c1723 commit 8bdfb65
Show file tree
Hide file tree
Showing 12 changed files with 780 additions and 155 deletions.
9 changes: 4 additions & 5 deletions components/org.wso2.carbon.identity.api.server.dcr/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@
<artifactId>jackson-databind</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down Expand Up @@ -151,6 +146,10 @@
<artifactId>jaxp-ri</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.testutil</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

public class RegisterApiServiceFactory {

private final static RegisterApiService service = new RegisterApiServiceImpl();
private static final RegisterApiService SERVICE = new RegisterApiServiceImpl();

public static RegisterApiService getRegisterApi()
{
return service;
return SERVICE;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
* Copyright (c) 2017-2025, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 Inc. licenses this file to you under the Apache License,
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
Expand All @@ -20,6 +20,7 @@

import org.apache.commons.logging.Log;
import org.slf4j.MDC;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.identity.oauth.dcr.DCRMConstants;
import org.wso2.carbon.identity.oauth.dcr.bean.Application;
import org.wso2.carbon.identity.oauth.dcr.bean.ApplicationRegistrationRequest;
Expand All @@ -44,16 +45,15 @@ public class DCRMUtils {
private static final String NOT_FOUND_STATUS = "NOT_FOUND_";
private static final String FORBIDDEN_STATUS = "FORBIDDEN_";

private static DCRMService oAuth2DCRMService;
private static class OAuth2DCRMServiceHolder {

public static void setOAuth2DCRMService(DCRMService oAuth2DCRMService) {

DCRMUtils.oAuth2DCRMService = oAuth2DCRMService;
private 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 Expand Up @@ -307,5 +307,4 @@ private static DCRMEndpointException buildDCRMEndpointException(Response.Status
return new DCRMEndpointException(status, errorDTO);
}
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@
import org.wso2.carbon.context.internal.OSGiDataHolder;
import org.wso2.carbon.identity.application.common.IdentityApplicationManagementException;
import org.wso2.carbon.identity.application.mgt.ApplicationManagementService;
import org.wso2.carbon.identity.common.testng.WithCarbonHome;
import org.wso2.carbon.identity.oauth.dcr.exception.DCRMException;
import org.wso2.carbon.identity.oauth.dcr.internal.DCRDataHolder;
import org.wso2.carbon.identity.oauth.dcr.service.DCRMService;
import org.wso2.carbon.identity.oauth2.dcr.endpoint.TestUtil;
import org.wso2.carbon.identity.oauth2.dcr.endpoint.dto.RegistrationRequestDTO;
import org.wso2.carbon.identity.oauth2.dcr.endpoint.dto.UpdateRequestDTO;
import org.wso2.carbon.identity.oauth2.dcr.endpoint.exceptions.DCRMEndpointException;
import org.wso2.carbon.identity.oauth2.dcr.endpoint.util.DCRMUtils;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -49,11 +49,13 @@

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockConstruction;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;

@WithCarbonHome
@Listeners(MockitoTestNGListener.class)
public class RegisterApiServiceImplExceptionTest {

Expand All @@ -70,13 +72,22 @@ public class RegisterApiServiceImplExceptionTest {
ApplicationManagementService applicationManagementService;

@Mock
DCRMService mockedDCRMService;
PrivilegedCarbonContext privilegedCarbonContext;

MockedConstruction<ServiceTracker> mockedConstruction;
private MockedStatic<PrivilegedCarbonContext> mockedPrivilegedCarbonContext;

@BeforeMethod
public void setUp() throws Exception {

if (mockedPrivilegedCarbonContext != null) {
mockedPrivilegedCarbonContext.close();
}
mockedPrivilegedCarbonContext = mockStatic(PrivilegedCarbonContext.class);
when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedCarbonContext);
lenient().when(PrivilegedCarbonContext.getThreadLocalCarbonContext()
.getOSGiService(DCRMService.class, null)).thenReturn(mock(DCRMService.class));

// Initializing variables.
registerApiService = new RegisterApiServiceImpl();

Expand Down Expand Up @@ -104,7 +115,6 @@ public void tearDown() {
public void testDeleteApplicationClientException() throws Exception {

try {
DCRMUtils.setOAuth2DCRMService(mockedDCRMService);
registerApiService.deleteApplication("");
} catch (DCRMEndpointException e) {
assertEquals(e.getResponse().getStatus(), Response.Status.BAD_REQUEST.getStatusCode());
Expand All @@ -126,7 +136,6 @@ public void testDeleteApplicationThrowableException() throws DCRMException {
public void testGetApplicationClientException() throws Exception {

try {
DCRMUtils.setOAuth2DCRMService(mockedDCRMService);
registerApiService.getApplication("");
} catch (DCRMEndpointException e) {
assertEquals(e.getResponse().getStatus(), Response.Status.BAD_REQUEST.getStatusCode());
Expand Down Expand Up @@ -155,7 +164,6 @@ public void testRegisterApplicationClientException() throws DCRMException {
registrationRequestDTO.setClientName("Test App");
registrationRequestDTO.setGrantTypes(granttypes);
registrationRequestDTO.setRedirectUris(redirectUris);
DCRMUtils.setOAuth2DCRMService(mockedDCRMService);
dcrDataHolder.when(DCRDataHolder::getInstance).thenReturn(dataHolder);
lenient().when(dataHolder.getApplicationManagementService()).thenReturn(applicationManagementService);

Expand All @@ -180,7 +188,6 @@ public void testRegisterApplicationServerException() throws DCRMException, Ident
registrationRequestDTO.setGrantTypes(granttypes);
registrationRequestDTO.setRedirectUris(redirectUris);

DCRMUtils.setOAuth2DCRMService(mockedDCRMService);
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 @@ -218,7 +225,6 @@ public void testUpdateApplicationClientException() throws DCRMException {
updateRequestDTO.setClientName("Test App");
updateRequestDTO.setGrantTypes(granttypes);
updateRequestDTO.setRedirectUris(redirectUris);
DCRMUtils.setOAuth2DCRMService(mockedDCRMService);
dcrDataHolder.when(DCRDataHolder::getInstance).thenReturn(dataHolder);
lenient().when(dataHolder.getApplicationManagementService()).thenReturn(applicationManagementService);

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

import org.mockito.Mock;
import org.mockito.MockedConstruction;
import org.mockito.MockedStatic;
import org.mockito.testng.MockitoTestNGListener;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;
Expand All @@ -30,6 +31,7 @@
import org.testng.annotations.Test;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.context.internal.OSGiDataHolder;
import org.wso2.carbon.identity.common.testng.WithCarbonHome;
import org.wso2.carbon.identity.oauth.dcr.bean.Application;
import org.wso2.carbon.identity.oauth.dcr.bean.ApplicationRegistrationRequest;
import org.wso2.carbon.identity.oauth.dcr.bean.ApplicationUpdateRequest;
Expand All @@ -40,7 +42,6 @@
import org.wso2.carbon.identity.oauth2.dcr.endpoint.dto.RegistrationRequestDTO;
import org.wso2.carbon.identity.oauth2.dcr.endpoint.dto.UpdateRequestDTO;
import org.wso2.carbon.identity.oauth2.dcr.endpoint.exceptions.DCRMEndpointException;
import org.wso2.carbon.identity.oauth2.dcr.endpoint.util.DCRMUtils;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -49,11 +50,13 @@

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockConstruction;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;

@WithCarbonHome
@Listeners(MockitoTestNGListener.class)
public class RegisterApiServiceImplTest {

Expand All @@ -69,10 +72,20 @@ public class RegisterApiServiceImplTest {
@Mock
private DCRMService dcrmService;

@Mock
PrivilegedCarbonContext privilegedCarbonContext;

MockedConstruction<ServiceTracker> mockedConstruction;
private MockedStatic<PrivilegedCarbonContext> mockedPrivilegedCarbonContext;

@BeforeMethod
public void setUp() throws Exception {

mockedPrivilegedCarbonContext = mockStatic(PrivilegedCarbonContext.class);
lenient().when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedCarbonContext);
lenient().when(PrivilegedCarbonContext.getThreadLocalCarbonContext()
.getOSGiService(DCRMService.class, null)).thenReturn(mock(DCRMService.class));

//Initializing variables.
registerApiService = new RegisterApiServiceImpl();
validclientId = "N2QqQluzQuL5X6CtM3KZwqzLQhUa";
Expand Down Expand Up @@ -101,16 +114,15 @@ public void tearDown() {

mockedConstruction.close();
PrivilegedCarbonContext.endTenantFlow();
mockedPrivilegedCarbonContext.close();
}

@Test
public void testDeleteApplication() throws Exception {

DCRMUtils.setOAuth2DCRMService(dcrmService);
doNothing().when(dcrmService).deleteApplication(validclientId);
lenient().doNothing().when(dcrmService).deleteApplication(validclientId);
Assert.assertEquals(registerApiService.deleteApplication(validclientId).getStatus(),
Response.Status.NO_CONTENT.getStatusCode());

}

@Test
Expand All @@ -127,8 +139,7 @@ public void testDeleteApplicationServerException() throws Exception {
@Test
public void testGetApplication() throws Exception {

DCRMUtils.setOAuth2DCRMService(dcrmService);
when(dcrmService.getApplication(validclientId)).thenReturn(application);
lenient().when(dcrmService.getApplication(validclientId)).thenReturn(application);
Assert.assertEquals(registerApiService.getApplication(validclientId).getStatus(),
Response.Status.OK.getStatusCode());

Expand All @@ -153,8 +164,7 @@ public void testRegisterApplication() throws Exception {

RegistrationRequestDTO registrationRequestDTO = new RegistrationRequestDTO();
registrationRequestDTO.setClientName("app1");
DCRMUtils.setOAuth2DCRMService(dcrmService);
when(dcrmService.registerApplication(any(ApplicationRegistrationRequest.class)))
lenient().when(dcrmService.registerApplication(any(ApplicationRegistrationRequest.class)))
.thenReturn(application);
Assert.assertEquals(registerApiService.registerApplication(registrationRequestDTO)
.getStatus(), Response.Status.CREATED.getStatusCode());
Expand All @@ -179,8 +189,7 @@ public void testUpdateApplication() throws Exception {
UpdateRequestDTO updateRequestDTO1 = new UpdateRequestDTO();
updateRequestDTO1.setClientName("Client1");
String clientID = "clientID1";
DCRMUtils.setOAuth2DCRMService(dcrmService);
when(dcrmService.updateApplication
lenient().when(dcrmService.updateApplication
(any(ApplicationUpdateRequest.class), anyString()))
.thenReturn(application);
Assert.assertEquals(registerApiService.updateApplication(updateRequestDTO1, clientID)
Expand Down
Loading

0 comments on commit 8bdfb65

Please sign in to comment.