Skip to content

Commit c5c19d8

Browse files
authored
Merge pull request #916 from Ashi1993/consent-manage-put
[OB4] Adding internal Consent Manage PUT endpoint
2 parents 3980f4a + 2a9b4f9 commit c5c19d8

File tree

20 files changed

+1794
-187
lines changed

20 files changed

+1794
-187
lines changed

financial-services-accelerator/components/org.wso2.financial.services.accelerator.consent.mgt.dao/src/main/java/org/wso2/financial/services/accelerator/consent/mgt/dao/ConsentCoreDAO.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,16 @@ void updateAuthorizationUser(Connection connection, String authorizationID, Stri
197197
boolean updateAuthorizationResources(Connection connection, List<AuthorizationResource> authorizationResources)
198198
throws ConsentDataUpdationException;
199199

200+
/**
201+
* This method is used to delete the given list of authorization resources.
202+
*
203+
* @param connection connection object
204+
* @param authorizationResourceIds a list of authorization resource IDs that should be deleted
205+
* @throws ConsentDataDeletionException thrown if a database error occurs
206+
*/
207+
boolean deleteAuthorizationResources(Connection connection, List<String> authorizationResourceIds)
208+
throws ConsentDataDeletionException;
209+
200210
/**
201211
* This method is used to store the consent mapping resource in the database. The request consent mapping object
202212
* must contain all the data in it without the consent mapping ID. It will be generated and set to the response
@@ -259,6 +269,16 @@ void updateConsentMappingStatus(Connection connection, ArrayList<String> mapping
259269
public boolean updateConsentMappingResources(Connection connection, List<ConsentMappingResource>
260270
consentMappingResources) throws ConsentDataUpdationException;
261271

272+
/**
273+
* This method is used to delete the given list of consent mapping resource Ids.
274+
*
275+
* @param connection connection object
276+
* @param consentMappingResourceIds a list of consent mapping resource Ids that should be deleted
277+
* @throws ConsentDataDeletionException thrown if a database error occurs
278+
*/
279+
boolean deleteConsentMappingResources(Connection connection, List<String> consentMappingResourceIds)
280+
throws ConsentDataDeletionException;
281+
262282
/**
263283
* This method is used to store the consent attributes in the database. The request consent attributes object
264284
* must be set with a consent ID and consent attribute map.

financial-services-accelerator/components/org.wso2.financial.services.accelerator.consent.mgt.dao/src/main/java/org/wso2/financial/services/accelerator/consent/mgt/dao/constants/ConsentMgtDAOConstants.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ public class ConsentMgtDAOConstants {
126126
"authorization user in the database";
127127
public static final String CONSENT_AUTHORIZATION_RESOURCE_UPDATE_ERROR_MSG = "Error occurred while updating " +
128128
"authorization resource in the database";
129+
public static final String CONSENT_AUTHORIZATION_RESOURCE_DELETE_ERROR_MSG = "Error occurred while deleting " +
130+
"authorization resource in the database";
129131
public static final String CONSENT_MAPPING_RESOURCE_STORE_ERROR_MSG = "Error occurred while storing consent " +
130132
"mapping resource in the database";
131133
public static final String CONSENT_MAPPING_RETRIEVE_ERROR_MSG = "Error occurred while retrieving consent mapping " +
@@ -134,6 +136,8 @@ public class ConsentMgtDAOConstants {
134136
"mapping status in the database";
135137
public static final String CONSENT_MAPPING_RESOURCE_UPDATE_ERROR_MSG = "Error occurred while updating consent " +
136138
"mapping resource in the database";
139+
public static final String CONSENT_MAPPING_RESOURCE_DELETE_ERROR_MSG = "Error occurred while deleting consent " +
140+
"mapping resource in the database";
137141
public static final String CONSENT_ATTRIBUTES_STORE_ERROR_MSG = "Error occurred while storing consent attributes " +
138142
"in the database";
139143
public static final String CONSENT_ATTRIBUTES_RETRIEVE_ERROR_MSG = "Error occurred while retrieving consent " +

financial-services-accelerator/components/org.wso2.financial.services.accelerator.consent.mgt.dao/src/main/java/org/wso2/financial/services/accelerator/consent/mgt/dao/impl/ConsentCoreDAOImpl.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,41 @@ public boolean updateAuthorizationResources(Connection connection, List<Authoriz
546546
return true;
547547
}
548548

549+
@Override
550+
public boolean deleteAuthorizationResources(Connection connection, List<String> authorizationResourceIds)
551+
throws ConsentDataDeletionException {
552+
553+
log.info(String.format("Deleting authorization resources. Count: %s", authorizationResourceIds.size()));
554+
String deleteAuthorizationResourcePrepStatement =
555+
sqlStatements.getDeleteAuthorizationResourcePreparedStatement();
556+
557+
try (PreparedStatement deleteAuthorizationResourcePreparedStmt =
558+
connection.prepareStatement(deleteAuthorizationResourcePrepStatement)) {
559+
560+
log.debug("Setting parameters to prepared statement to batch delete authorization resources");
561+
562+
for (String authId : authorizationResourceIds) {
563+
deleteAuthorizationResourcePreparedStmt.setString(1, authId);
564+
deleteAuthorizationResourcePreparedStmt.addBatch();
565+
}
566+
567+
int[] results = deleteAuthorizationResourcePreparedStmt.executeBatch();
568+
boolean allDeleted = Arrays.stream(results)
569+
.allMatch(result -> result > 0 || result == java.sql.Statement.SUCCESS_NO_INFO);
570+
if (allDeleted) {
571+
log.debug("Batch delete for authorization resources completed successfully.");
572+
return true;
573+
} else {
574+
log.error("Some or all rows were not deleted in batch delete for authorization resources.");
575+
throw new ConsentDataDeletionException("Failed to delete one or more authorization resources.");
576+
}
577+
} catch (SQLException e) {
578+
log.error(ConsentMgtDAOConstants.CONSENT_AUTHORIZATION_RESOURCE_DELETE_ERROR_MSG, e);
579+
throw new ConsentDataDeletionException(
580+
ConsentMgtDAOConstants.CONSENT_AUTHORIZATION_RESOURCE_DELETE_ERROR_MSG, e);
581+
}
582+
}
583+
549584

550585
@Override
551586
public ConsentMappingResource storeConsentMappingResource(Connection connection,
@@ -733,6 +768,40 @@ public boolean updateConsentMappingResources(Connection connection, List<Consent
733768
return true;
734769
}
735770

771+
@Override
772+
public boolean deleteConsentMappingResources(Connection connection, List<String> consentMappingResourceIds)
773+
throws ConsentDataDeletionException {
774+
775+
log.info(String.format("Deleting consent mapping resources. Count: %s", consentMappingResourceIds.size()));
776+
String deleteConsentMappingResourcePrepStatement =
777+
sqlStatements.getDeleteConsentMappingResourcePreparedStatement();
778+
779+
try (PreparedStatement deleteConsentMappingResourcePreparedStmt =
780+
connection.prepareStatement(deleteConsentMappingResourcePrepStatement)) {
781+
782+
log.debug("Setting parameters to prepared statement to batch delete consent mapping resources");
783+
784+
for (String mappingId : consentMappingResourceIds) {
785+
deleteConsentMappingResourcePreparedStmt.setString(1, mappingId);
786+
deleteConsentMappingResourcePreparedStmt.addBatch();
787+
}
788+
789+
int[] results = deleteConsentMappingResourcePreparedStmt.executeBatch();
790+
boolean allDeleted = Arrays.stream(results)
791+
.allMatch(result -> result > 0 || result == java.sql.Statement.SUCCESS_NO_INFO);
792+
if (allDeleted) {
793+
log.debug("Batch delete for consent mapping resources completed successfully.");
794+
return true;
795+
} else {
796+
log.error("Some or all rows were not deleted in batch delete for consent mapping resources.");
797+
throw new ConsentDataDeletionException("Failed to delete one or more consent mapping resources.");
798+
}
799+
} catch (SQLException e) {
800+
log.error(ConsentMgtDAOConstants.CONSENT_MAPPING_RESOURCE_DELETE_ERROR_MSG, e);
801+
throw new ConsentDataDeletionException(ConsentMgtDAOConstants.CONSENT_MAPPING_RESOURCE_DELETE_ERROR_MSG, e);
802+
}
803+
}
804+
736805

737806
@Override
738807
public boolean storeConsentAttributes(Connection connection, ConsentAttributes consentAttributes)

financial-services-accelerator/components/org.wso2.financial.services.accelerator.consent.mgt.dao/src/main/java/org/wso2/financial/services/accelerator/consent/mgt/dao/queries/ConsentMgtCommonDBQueries.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public String getGetConsentWithConsentAttributesPreparedStatement() {
4141

4242
return "SELECT FS_CONSENT.CONSENT_ID, RECEIPT, CREATED_TIME, UPDATED_TIME, CLIENT_ID, CONSENT_TYPE, " +
4343
"CURRENT_STATUS, CONSENT_FREQUENCY, VALIDITY_TIME, RECURRING_INDICATOR, " +
44-
"FS_CONSENT_ATTRIBUTE.ATT_KEY, FS_CONSENT_ATTRIBUTE.ATT_VALUE FROM FS_CONSENT RIGHT JOIN " +
44+
"FS_CONSENT_ATTRIBUTE.ATT_KEY, FS_CONSENT_ATTRIBUTE.ATT_VALUE FROM FS_CONSENT LEFT JOIN " +
4545
"FS_CONSENT_ATTRIBUTE ON FS_CONSENT.CONSENT_ID = FS_CONSENT_ATTRIBUTE.CONSENT_ID WHERE FS_CONSENT" +
4646
".CONSENT_ID = ?";
4747
}
@@ -125,6 +125,11 @@ public String getUpdateAuthorizationResourcePreparedStatement() {
125125
"WHERE AUTH_ID = ?";
126126
}
127127

128+
public String getDeleteAuthorizationResourcePreparedStatement() {
129+
130+
return "DELETE FROM FS_CONSENT_AUTH_RESOURCE WHERE AUTH_ID = ?";
131+
}
132+
128133
public String getStoreConsentMappingPreparedStatement() {
129134

130135
return "INSERT INTO FS_CONSENT_MAPPING (MAPPING_ID, AUTH_ID, ACCOUNT_ID, PERMISSION, MAPPING_STATUS) VALUES " +
@@ -146,6 +151,12 @@ public String getUpdateConsentMappingResourcePreparedStatement() {
146151
return "UPDATE FS_CONSENT_MAPPING SET PERMISSION = ?, MAPPING_STATUS = ? WHERE MAPPING_ID = ?";
147152
}
148153

154+
public String getDeleteConsentMappingResourcePreparedStatement() {
155+
156+
return "DELETE FROM FS_CONSENT_MAPPING WHERE MAPPING_ID = ? ";
157+
}
158+
159+
149160
public String getStoreConsentAttributesPreparedStatement() {
150161

151162
return "INSERT INTO FS_CONSENT_ATTRIBUTE (CONSENT_ID, ATT_KEY, ATT_VALUE) VALUES (?, ?, ?)";

financial-services-accelerator/components/org.wso2.financial.services.accelerator.consent.mgt.dao/src/test/java/org/wso2/financial/services/accelerator/consent/mgt/dao/impl/ConsentCoreDAOTests.java

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import java.sql.SQLException;
4848
import java.util.ArrayList;
4949
import java.util.Arrays;
50+
import java.util.Collections;
5051
import java.util.List;
5152
import java.util.Map;
5253
import java.util.UUID;
@@ -675,6 +676,44 @@ public void testUpdateAuthorizationUserWithUnmatchedAuthID() throws Exception {
675676
}
676677
}
677678

679+
@Test
680+
public void testDeleteAuthorizationResource() throws Exception {
681+
682+
ConsentResource storedConsentResource;
683+
AuthorizationResource storedAuthorizationResource;
684+
ConsentResource consentResource = ConsentMgtDAOTestData.getSampleTestConsentResource();
685+
686+
try (Connection connection = DAOUtils.getConnection(DB_NAME)) {
687+
688+
storedConsentResource = consentCoreDAO.storeConsentResource(connection, consentResource);
689+
Assert.assertNotNull(storedConsentResource.getConsentID());
690+
691+
AuthorizationResource authorizationResource = new AuthorizationResource();
692+
authorizationResource.setConsentID(storedConsentResource.getConsentID());
693+
authorizationResource.setAuthorizationType(ConsentMgtDAOTestData.SAMPLE_AUTHORIZATION_TYPE);
694+
authorizationResource.setUserID(ConsentMgtDAOTestData.SAMPLE_USER_ID);
695+
authorizationResource.setAuthorizationStatus(ConsentMgtDAOTestData.SAMPLE_AUTHORIZATION_STATUS);
696+
697+
storedAuthorizationResource = consentCoreDAO.storeAuthorizationResource(connection,
698+
authorizationResource);
699+
700+
Assert.assertNotNull(storedAuthorizationResource.getConsentID());
701+
Assert.assertNotNull(storedAuthorizationResource.getAuthorizationID());
702+
703+
boolean result = consentCoreDAO.deleteAuthorizationResources(connection,
704+
Collections.singletonList(storedAuthorizationResource.getAuthorizationID()));
705+
Assert.assertTrue(result);
706+
}
707+
}
708+
709+
@Test(expectedExceptions = ConsentDataDeletionException.class)
710+
public void testDeleteAuthorizationResourceError() throws Exception {
711+
712+
Mockito.doThrow(SQLException.class).when(mockedConnection).prepareStatement(Mockito.anyString());
713+
714+
consentCoreDAO.deleteAuthorizationResources(mockedConnection, Collections.singletonList("1234"));
715+
}
716+
678717
@Test
679718
public void testStoreConsentMappingResource() throws Exception {
680719

@@ -850,6 +889,57 @@ public void testUpdateConsentMappingStatusSQLError() throws Exception {
850889
ConsentMgtDAOTestData.SAMPLE_MAPPING_STATUS);
851890
}
852891

892+
@Test
893+
public void testDeleteConsentMappingResource() throws Exception {
894+
895+
ConsentResource storedConsentResource;
896+
AuthorizationResource storedAuthorizationResource;
897+
ConsentMappingResource storedConsentMappingResource;
898+
ConsentResource consentResource = ConsentMgtDAOTestData.getSampleTestConsentResource();
899+
900+
try (Connection connection = DAOUtils.getConnection(DB_NAME)) {
901+
902+
storedConsentResource = consentCoreDAO.storeConsentResource(connection, consentResource);
903+
Assert.assertNotNull(storedConsentResource.getConsentID());
904+
905+
AuthorizationResource authorizationResource = new AuthorizationResource();
906+
authorizationResource.setConsentID(storedConsentResource.getConsentID());
907+
authorizationResource.setAuthorizationType(ConsentMgtDAOTestData.SAMPLE_AUTHORIZATION_TYPE);
908+
authorizationResource.setUserID(ConsentMgtDAOTestData.SAMPLE_USER_ID);
909+
authorizationResource.setAuthorizationStatus(ConsentMgtDAOTestData.SAMPLE_AUTHORIZATION_STATUS);
910+
911+
storedAuthorizationResource = consentCoreDAO.storeAuthorizationResource(connection,
912+
authorizationResource);
913+
914+
Assert.assertNotNull(storedAuthorizationResource.getConsentID());
915+
Assert.assertNotNull(storedAuthorizationResource.getAuthorizationID());
916+
917+
ConsentMappingResource consentMappingResource = new ConsentMappingResource();
918+
consentMappingResource.setAuthorizationID(storedAuthorizationResource.getAuthorizationID());
919+
consentMappingResource.setAccountID(ConsentMgtDAOTestData.SAMPLE_ACCOUNT_ID);
920+
consentMappingResource.setPermission(ConsentMgtDAOTestData.SAMPLE_PERMISSION);
921+
consentMappingResource.setMappingStatus(ConsentMgtDAOTestData.SAMPLE_MAPPING_STATUS);
922+
923+
storedConsentMappingResource = consentCoreDAO.storeConsentMappingResource(connection,
924+
consentMappingResource);
925+
926+
Assert.assertNotNull(storedConsentMappingResource.getMappingID());
927+
Assert.assertNotNull(storedConsentMappingResource.getAuthorizationID());
928+
929+
boolean result = consentCoreDAO.deleteConsentMappingResources(connection,
930+
Collections.singletonList(storedConsentMappingResource.getMappingID()));
931+
Assert.assertTrue(result);
932+
}
933+
}
934+
935+
@Test(expectedExceptions = ConsentDataDeletionException.class)
936+
public void testDeleteConsentMappingResourceError() throws Exception {
937+
938+
Mockito.doThrow(SQLException.class).when(mockedConnection).prepareStatement(Mockito.anyString());
939+
940+
consentCoreDAO.deleteConsentMappingResources(mockedConnection, Collections.singletonList("1234"));
941+
}
942+
853943
@Test
854944
public void testStoreConsentAttributes() throws Exception {
855945

financial-services-accelerator/components/org.wso2.financial.services.accelerator.consent.mgt.extensions/src/main/java/org/wso2/financial/services/accelerator/consent/mgt/extensions/common/ConsentExtensionConstants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ public class ConsentExtensionConstants {
2727
public static final String ACCOUNTS = "accounts";
2828
public static final String PAYMENTS = "payments";
2929
public static final String FUNDS_CONFIRMATIONS = "fundsconfirmations";
30+
public static final String INTERNAL_UPDATE = "internal-consent-update";
3031
public static final String DEFAULT = "default";
3132
public static final String ACCOUNT_CONSENT_PATH = "account-access-consents";
3233
public static final String COF_CONSENT_PATH = "funds-confirmation-consents";
3334
public static final String PAYMENT_CONSENT_PATH = "payment-consents";
35+
public static final String CONSENT_UPDATE_PATH = "^consent/([^/?]+)$";
3436
public static final String CONSENT_DATA = "consentData";
3537
public static final String CONSUMER_DATA = "consumerData";
3638
public static final String TITLE = "title";

0 commit comments

Comments
 (0)