|
1 | 1 | package io.mosip.registration.processor.message.sender.test.service; |
2 | 2 |
|
3 | 3 | import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertNull; |
4 | 5 | import static org.mockito.ArgumentMatchers.*; |
5 | 6 | import static org.mockito.Mockito.*; |
6 | 7 |
|
@@ -554,38 +555,43 @@ public void testSendSmsNotificationWhenPreferredLanguagesFailShouldStillSendSmsS |
554 | 555 | } |
555 | 556 |
|
556 | 557 | @Test |
557 | | - public void testSendSmsNotificationShouldThrowJsonParseException() throws IOException, ApisResourceAccessException, |
| 558 | + public void testSendSmsNotificationShouldHandleJsonParseExceptionGracefully() throws IOException, ApisResourceAccessException, |
558 | 559 | PacketDecryptionFailureException, JSONException, PacketManagerException, JsonProcessingException { |
| 560 | + |
559 | 561 | SyncRegistrationEntity syncEntity = new SyncRegistrationEntity(); |
560 | 562 | syncEntity.setAdditionalInfoReqId(null); |
561 | 563 | syncEntity.setRegistrationType("NEW"); |
562 | 564 | syncEntity.setPacketId("2018701130000410092018110735"); |
563 | 565 | syncEntity.setWorkflowInstanceId("78fc3d34-03f5-11ec-9a03-0242ac130004"); |
564 | 566 | syncEntity.setOptionalValues(new byte[] {1, 2, 3, 4, 5}); |
565 | | - List<SyncRegistrationEntity> entities = new ArrayList<SyncRegistrationEntity>(); |
| 567 | + List<SyncRegistrationEntity> entities = new ArrayList<>(); |
566 | 568 | entities.add(syncEntity); |
567 | 569 | SyncRegistrationEntity entity = new SyncRegistrationEntity(); |
568 | 570 | entities.add(entity); |
569 | 571 |
|
570 | 572 | 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 | + } |
589 | 595 | } |
590 | 596 |
|
591 | 597 | } |
0 commit comments