Skip to content

Commit 86e1073

Browse files
KB-11624 | BE | DEV | API for Creating consent acknowledgement and reading the consent details. (#105)
* KB-11624 | BE | DEV | API for Creating consent acknowledgement and reading the consent details. 1. Added the check for 0 records found. 2. If record is not found will be returning 200 OK with message. * Removed the unused Consent Details Read API
1 parent 1ce33ac commit 86e1073

File tree

4 files changed

+63
-117
lines changed

4 files changed

+63
-117
lines changed

src/main/java/com/igot/cb/consentacknowledge/ConsentAcknowledgeController.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,6 @@ public ResponseEntity<ApiResponse> acknowledgeDeclaration(
2626
}
2727

2828

29-
@GetMapping("/details/{consentId}")
30-
public ResponseEntity<ApiResponse> getConsentDetails(@PathVariable String consentId,
31-
@RequestHeader(value = Constants.X_AUTH_TOKEN) String authToken) {
32-
ApiResponse response = acknowledgeService.getConsentDetails(consentId, authToken);
33-
return new ResponseEntity<>(response, response.getResponseCode());
34-
}
35-
36-
3729
@GetMapping("/acknowledge/read/{contentId}/{consentId}")
3830
public ResponseEntity<ApiResponse> getConsentAcknowledgementDetails(@PathVariable(Constants.CONSENT_ID) String consentId,
3931
@PathVariable(Constants.CONTENT_ID) String contentId,

src/main/java/com/igot/cb/consentacknowledge/ConsentAcknowledgeServiceImpl.java

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.igot.cb.util.AccessTokenValidator;
99
import com.igot.cb.util.Constants;
1010
import com.igot.cb.util.ProjectUtil;
11+
import org.apache.commons.collections.CollectionUtils;
1112
import org.apache.commons.collections.MapUtils;
1213
import org.apache.commons.lang3.StringUtils;
1314
import org.slf4j.Logger;
@@ -134,36 +135,6 @@ private String validateAcknowledgeDeclarationRequest(Map<String, Object> request
134135
}
135136

136137

137-
/**
138-
* Method to get consent details by consentId
139-
*/
140-
@Override
141-
public ApiResponse getConsentDetails(String consentId, String authToken) {
142-
ApiResponse response = ProjectUtil.createDefaultResponse("api.consent.read");
143-
String userId = accessTokenValidator.fetchUserIdFromAccessToken(authToken, response);
144-
if (StringUtils.isEmpty(userId)) {
145-
return response;
146-
}
147-
Map<String, Object> propertyMap = new HashMap<>();
148-
propertyMap.put(Constants.CONSENT_ID, consentId);
149-
List<Map<String, Object>> consentDetails;
150-
try {
151-
consentDetails = cassandraOperation
152-
.getRecordsByProperties(Constants.KEYSPACE_SUNBIRD, Constants.TABLE_CONSENT_DETAILS, propertyMap, Arrays.asList(Constants.CONSENT_ID, Constants.ADDITIONAL_DATA, Constants.DESCRIPTION), null);
153-
} catch (Exception e) {
154-
logger.error("Error while fetching consent details from DB", e);
155-
ProjectUtil.errorResponse(response, "Failed to fetch consent details. Please try again later.", HttpStatus.INTERNAL_SERVER_ERROR);
156-
return response;
157-
}
158-
Map<String, Object> consentDetailsMap = consentDetails.get(0);
159-
response.getParams().setStatus(Constants.OK);
160-
response.setResponseCode(HttpStatus.OK);
161-
Map<String, Object> result = new HashMap<>();
162-
result.put(Constants.RESPONSE, consentDetailsMap);
163-
response.getResult().putAll(result);
164-
return response;
165-
}
166-
167138

168139
/**
169140
* Method to get consent acknowledgement details by contentId and consentId
@@ -188,6 +159,16 @@ public ApiResponse getConsentAcknowledgementDetails(String contentId, String con
188159
ProjectUtil.errorResponse(response, "Failed to fetch consent Acknowledgement details. Please try again later.", HttpStatus.INTERNAL_SERVER_ERROR);
189160
return response;
190161
}
162+
Map<String, Object> result = new HashMap<>();
163+
if (CollectionUtils.isEmpty(consentAcknowledgementDetailsList)) {
164+
response.getParams().setStatus(Constants.OK);
165+
response.setResponseCode(HttpStatus.OK);
166+
Map<String, Object> responseMap = new HashMap<>();
167+
responseMap.put(Constants.MESSAGE, " No consent acknowledgement record found for the given contentId and consentId");
168+
result.put(Constants.RESPONSE, responseMap);
169+
response.getResult().putAll(result);
170+
return response;
171+
}
191172
Map<String, Object> consentAcknowledgementDetailsMap = consentAcknowledgementDetailsList.get(0);
192173
String additionAttributesStr = (String) consentAcknowledgementDetailsMap.get(Constants.ADDITIONAL_ATTRIBUTES);
193174
Map<String, Object> additionalAttributesMap = null;
@@ -202,7 +183,6 @@ public ApiResponse getConsentAcknowledgementDetails(String contentId, String con
202183
consentAcknowledgementDetailsMap.put(Constants.ADDITIONAL_ATTRIBUTES, additionalAttributesMap);
203184
response.getParams().setStatus(Constants.OK);
204185
response.setResponseCode(HttpStatus.OK);
205-
Map<String, Object> result = new HashMap<>();
206186
result.put(Constants.RESPONSE, consentAcknowledgementDetailsMap);
207187
response.getResult().putAll(result);
208188
return response;

src/main/java/com/igot/cb/consentacknowledge/IConsentAcknowledgeService.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,6 @@ public interface IConsentAcknowledgeService {
1515
*/
1616
ApiResponse acknowledgeDeclaration(Map<String, Object> request, String authToken);
1717

18-
/**
19-
* Retrieve consent details based on the provided consent ID and authentication token.
20-
*
21-
* @param consentId The ID of the consent to retrieve details for.
22-
* @param authToken The authentication token for validating the request.
23-
* @return An ApiResponse object containing the consent details.
24-
*/
25-
ApiResponse getConsentDetails(String consentId, String authToken);
26-
27-
2818
/**
2919
* Retrieve consent acknowledgement details based on the provided content ID, consent ID, and authentication token.
3020
*

src/test/java/com/igot/cb/consentacknowledge/ConsentAcknowledgeServiceImplTest.java

Lines changed: 52 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -152,47 +152,6 @@ void testAcknowledgeDeclaration_CassandraInsertFails() throws Exception {
152152
}
153153

154154

155-
@Test
156-
void testGetConsentDetails_Success() {
157-
when(accessTokenValidator.fetchUserIdFromAccessToken(anyString(), any())).thenReturn("user1");
158-
List<Map<String, Object>> fakeResult = List.of(Map.of(Constants.CONSENT_ID, "consent1", Constants.DESCRIPTION, "desc"));
159-
when(cassandraOperation.getRecordsByProperties(any(), any(), any(), any(), any())).thenReturn(fakeResult);
160-
ApiResponse response = service.getConsentDetails("consent1", "token");
161-
assertEquals(HttpStatus.OK, response.getResponseCode());
162-
assertTrue(response.getResult().containsKey(Constants.RESPONSE));
163-
}
164-
165-
@Test
166-
void testGetConsentDetails_Failure_DBError() {
167-
when(accessTokenValidator.fetchUserIdFromAccessToken(anyString(), any())).thenReturn("user1");
168-
when(cassandraOperation.getRecordsByProperties(any(), any(), any(), any(), any()))
169-
.thenThrow(new RuntimeException("DB error"));
170-
try (MockedStatic<ProjectUtil> mocked = mockStatic(ProjectUtil.class)) {
171-
mocked.when(() -> ProjectUtil.createDefaultResponse(anyString()))
172-
.thenCallRealMethod();
173-
mocked.when(() -> ProjectUtil.errorResponse(any(ApiResponse.class), anyString(), any()))
174-
.thenAnswer(invocation -> {
175-
ApiResponse resp = invocation.getArgument(0);
176-
resp.getParams().setErr(invocation.getArgument(1)); // force populate err
177-
resp.setResponseCode(invocation.getArgument(2));
178-
return null;
179-
});
180-
ApiResponse response = service.getConsentDetails("consent1", "token");
181-
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getResponseCode());
182-
assertEquals("Failed to fetch consent details. Please try again later.",
183-
response.getParams().getErr());
184-
}
185-
}
186-
187-
188-
@Test
189-
void testGetConsentDetails_InvalidUser() {
190-
when(accessTokenValidator.fetchUserIdFromAccessToken(anyString(), any())).thenReturn("");
191-
ApiResponse response = service.getConsentDetails("consent1", "token");
192-
assertNotEquals(Constants.OK, response.getParams().getStatus());
193-
}
194-
195-
196155
@Test
197156
void testAcknowledgeDeclaration_EmptyAdditionalData() {
198157
Map<String, Object> requestData = new HashMap<>();
@@ -294,33 +253,6 @@ void testAcknowledgeDeclaration_JsonProcessingException() throws Exception {
294253
assertNull(((Map<?, ?>) response.getResult()).get(Constants.ADDITIONAL_ATTRIBUTES));
295254
}
296255

297-
@Test
298-
void testGetConsentDetails_UserIdEmpty() {
299-
when(accessTokenValidator.fetchUserIdFromAccessToken(anyString(), any())).thenReturn("");
300-
try (MockedStatic<ProjectUtil> mocked = mockStatic(ProjectUtil.class)) {
301-
mocked.when(() -> ProjectUtil.createDefaultResponse(anyString()))
302-
.thenAnswer(inv -> {
303-
ApiResponse resp = new ApiResponse();
304-
resp.getParams().setStatus(Constants.FAILED);
305-
resp.setResponseCode(HttpStatus.BAD_REQUEST);
306-
return resp;
307-
});
308-
ApiResponse response = service.getConsentDetails("c1", "token");
309-
assertEquals(HttpStatus.BAD_REQUEST, response.getResponseCode());
310-
assertEquals(Constants.FAILED, response.getParams().getStatus());
311-
}
312-
}
313-
314-
315-
@Test
316-
void testGetConsentDetails_EmptyListThrows() {
317-
when(accessTokenValidator.fetchUserIdFromAccessToken(anyString(), any())).thenReturn("user1");
318-
when(cassandraOperation.getRecordsByProperties(any(), any(), any(), any(), any()))
319-
.thenReturn(List.of());
320-
assertThrows(IndexOutOfBoundsException.class,
321-
() -> service.getConsentDetails("c1", "token"));
322-
}
323-
324256
@Test
325257
void testAcknowledgeDeclaration_JsonProcessingFails() throws Exception {
326258
Map<String, Object> requestData = new HashMap<>();
@@ -422,4 +354,56 @@ void testGetConsentAcknowledgementDetails_JsonProcessingException() throws Excep
422354
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getResponseCode());
423355
assertEquals(Constants.FAILED, response.getParams().getStatus());
424356
}
357+
358+
@Test
359+
void testGetConsentAcknowledgementDetails_NoRecordsFound() {
360+
String contentId = "content1";
361+
String consentId = "consent1";
362+
String authToken = "token";
363+
when(accessTokenValidator.fetchUserIdFromAccessToken(eq(authToken), any()))
364+
.thenReturn("user1");
365+
when(cassandraOperation.getRecordsByProperties(any(), any(), any(), any(), isNull()))
366+
.thenReturn(Collections.emptyList());
367+
ApiResponse response = service.getConsentAcknowledgementDetails(contentId, consentId, authToken);
368+
assertEquals(HttpStatus.OK, response.getResponseCode());
369+
assertEquals(Constants.OK, response.getParams().getStatus());
370+
assertTrue(response.getResult().containsKey(Constants.RESPONSE));
371+
Map<String, Object> responseMap = (Map<String, Object>) response.getResult().get(Constants.RESPONSE);
372+
assertEquals(" No consent acknowledgement record found for the given contentId and consentId",
373+
responseMap.get(Constants.MESSAGE));
374+
}
375+
376+
@Test
377+
void testAcknowledgeDeclaration_InsertReturnsFailed() throws Exception {
378+
Map<String, Object> requestData = new HashMap<>();
379+
requestData.put(Constants.CONTENT_ID, "content1");
380+
requestData.put(Constants.CONSENT_ID, "consent1");
381+
requestData.put(Constants.ADDITIONAL_ATTRIBUTES, Map.of("k", "v"));
382+
Map<String, Object> body = Map.of(Constants.REQUEST, requestData);
383+
when(accessTokenValidator.fetchUserIdFromAccessToken(anyString(), any())).thenReturn("user1");
384+
when(objectMapper.writeValueAsString(any())).thenReturn("{\"k\":\"v\"}");
385+
ApiResponse failedResponse = new ApiResponse();
386+
failedResponse.put(Constants.RESPONSE, Constants.FAILED);
387+
failedResponse.put(Constants.ERROR_MESSAGE, "Simulated DB failure");
388+
when(cassandraOperation.insertRecord(any(), any(), any(), any(), any(), any()))
389+
.thenReturn(failedResponse);
390+
try (MockedStatic<ProjectUtil> mocked = mockStatic(ProjectUtil.class)) {
391+
mocked.when(() -> ProjectUtil.createDefaultResponse(anyString()))
392+
.thenCallRealMethod();
393+
mocked.when(() -> ProjectUtil.errorResponse(any(ApiResponse.class), anyString(), any()))
394+
.thenAnswer(invocation -> {
395+
ApiResponse resp = invocation.getArgument(0);
396+
resp.getParams().setErr(invocation.getArgument(1));
397+
resp.setResponseCode(invocation.getArgument(2));
398+
return null;
399+
});
400+
ApiResponse response = service.acknowledgeDeclaration(body, "token");
401+
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getResponseCode());
402+
assertEquals("Failed to acknowledge declaration. Please try again later.",
403+
response.getParams().getErr());
404+
assertEquals(Constants.FAILED, response.get(Constants.RESPONSE));
405+
verify(cassandraOperation, times(1))
406+
.insertRecord(any(), any(), any(), any(), any(), any());
407+
}
408+
}
425409
}

0 commit comments

Comments
 (0)