@@ -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