Skip to content

Commit 233148b

Browse files
[MOSIP-43903] - Refactor test method based on code rabbit review
Signed-off-by: Nidhi0201 <nidhi.k@cyberpwn.com>
1 parent a9d32f3 commit 233148b

1 file changed

Lines changed: 26 additions & 20 deletions

File tree

registration-processor/registration-processor-message-sender-impl/src/test/java/io/mosip/registration/processor/message/sender/test/service/MessageNotificationServiceImplTest.java

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.mosip.registration.processor.message.sender.test.service;
22

33
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNull;
45
import static org.mockito.ArgumentMatchers.*;
56
import static org.mockito.Mockito.*;
67

@@ -554,38 +555,43 @@ public void testSendSmsNotificationWhenPreferredLanguagesFailShouldStillSendSmsS
554555
}
555556

556557
@Test
557-
public void testSendSmsNotificationShouldThrowJsonParseException() throws IOException, ApisResourceAccessException,
558+
public void testSendSmsNotificationShouldHandleJsonParseExceptionGracefully() throws IOException, ApisResourceAccessException,
558559
PacketDecryptionFailureException, JSONException, PacketManagerException, JsonProcessingException {
560+
559561
SyncRegistrationEntity syncEntity = new SyncRegistrationEntity();
560562
syncEntity.setAdditionalInfoReqId(null);
561563
syncEntity.setRegistrationType("NEW");
562564
syncEntity.setPacketId("2018701130000410092018110735");
563565
syncEntity.setWorkflowInstanceId("78fc3d34-03f5-11ec-9a03-0242ac130004");
564566
syncEntity.setOptionalValues(new byte[] {1, 2, 3, 4, 5});
565-
List<SyncRegistrationEntity> entities = new ArrayList<SyncRegistrationEntity>();
567+
List<SyncRegistrationEntity> entities = new ArrayList<>();
566568
entities.add(syncEntity);
567569
SyncRegistrationEntity entity = new SyncRegistrationEntity();
568570
entities.add(entity);
569571

570572
Mockito.when(syncRegistrationService.findByRegistrationId(any())).thenReturn(entities);
571-
572-
when(packetManagerService.getFields(anyString(),anyList(),any(), any())).thenReturn(null);
573-
574-
file1 = new File("src/test/resources/1001.zip");
575-
is = new FileInputStream(file1);
576-
IOUtils.toByteArray(is);
577-
Mockito.when(decryptor.decrypt(Mockito.any(), Mockito.any(),Mockito.any())).thenReturn(is);
578-
579-
ResponseWrapper<SmsResponseDto> mockWrapper = new ResponseWrapper<>();
580-
SmsResponseDto smsResp = new SmsResponseDto();
581-
smsResp.setStatus("OK");
582-
mockWrapper.setResponse(smsResp);
583-
584-
Mockito.when(restClientService.postApi(any(), any(), any(), Mockito.any(RequestWrapper.class),
585-
Mockito.eq(ResponseWrapper.class))).thenReturn(mockWrapper);
586-
587-
messageNotificationServiceImpl.sendSmsNotification("RPR_UIN_GEN_SMS", "12345", "NEW", IdType.RID, attributes,
588-
RegistrationType.NEW.name());
573+
when(packetManagerService.getFields(anyString(), anyList(), any(), any())).thenReturn(null);
574+
575+
// Use try-with-resources to ensure FileInputStream is properly closed
576+
try (FileInputStream is = new FileInputStream("src/test/resources/1001.zip")) {
577+
IOUtils.toByteArray(is);
578+
Mockito.when(decryptor.decrypt(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(is);
579+
580+
ResponseWrapper<SmsResponseDto> mockWrapper = new ResponseWrapper<>();
581+
SmsResponseDto smsResp = new SmsResponseDto();
582+
smsResp.setStatus("OK");
583+
mockWrapper.setResponse(smsResp);
584+
585+
Mockito.when(restClientService.postApi(any(), any(), any(), Mockito.any(RequestWrapper.class),
586+
Mockito.eq(ResponseWrapper.class))).thenReturn(mockWrapper);
587+
588+
SmsResponseDto result = messageNotificationServiceImpl.sendSmsNotification(
589+
"RPR_UIN_GEN_SMS", "12345", "NEW", IdType.RID, attributes, RegistrationType.NEW.name());
590+
assertNull("OK", result.getStatus());
591+
} catch (IOException e) {
592+
// Handle exception if necessary
593+
e.printStackTrace();
594+
}
589595
}
590596

591597
}

0 commit comments

Comments
 (0)