From b0418fe3b3129a39da135e15254b1492a9151912 Mon Sep 17 00:00:00 2001 From: mengleang-ngoun Date: Wed, 21 Feb 2024 11:04:21 +0700 Subject: [PATCH] throw exception and add test case Signed-off-by: mengleang-ngoun --- .../signup/services/RegistrationService.java | 2 +- .../io/mosip/signup/util/ErrorConstants.java | 1 + .../services/RegistrationServiceTest.java | 22 +++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/signup-service/src/main/java/io/mosip/signup/services/RegistrationService.java b/signup-service/src/main/java/io/mosip/signup/services/RegistrationService.java index 62879330..b49baa66 100644 --- a/signup-service/src/main/java/io/mosip/signup/services/RegistrationService.java +++ b/signup-service/src/main/java/io/mosip/signup/services/RegistrationService.java @@ -173,7 +173,7 @@ public GenerateChallengeResponse generateChallenge(GenerateChallengeRequest gene SEND_OTP_SMS_NOTIFICATION_TEMPLATE_KEY, hashMap); log.debug(notificationLogging, notificationResponseRest); }catch (RestClientException e){ - log.debug(notificationLogging, "send notification failed"); + throw new SignUpException(ErrorConstants.OTP_NOTIFICATION_FAILED); } return new GenerateChallengeResponse(ActionStatus.SUCCESS); } diff --git a/signup-service/src/main/java/io/mosip/signup/util/ErrorConstants.java b/signup-service/src/main/java/io/mosip/signup/util/ErrorConstants.java index 90167d5c..acd2df9b 100644 --- a/signup-service/src/main/java/io/mosip/signup/util/ErrorConstants.java +++ b/signup-service/src/main/java/io/mosip/signup/util/ErrorConstants.java @@ -41,4 +41,5 @@ public class ErrorConstants { public static final String CHALLENGE_FORMAT_AND_TYPE_MISMATCH = "challenge_format_and_type_mismatch"; public static final String KNOWLEDGEBASE_MISMATCH = "knowledgebase_mismatch"; public static final String IDENTIFIER_BLOCKED = "identifier_blocked"; + public static final String OTP_NOTIFICATION_FAILED = "otp_notification_failed"; } diff --git a/signup-service/src/test/java/io/mosip/signup/services/RegistrationServiceTest.java b/signup-service/src/test/java/io/mosip/signup/services/RegistrationServiceTest.java index c937e56f..280a38ad 100644 --- a/signup-service/src/test/java/io/mosip/signup/services/RegistrationServiceTest.java +++ b/signup-service/src/test/java/io/mosip/signup/services/RegistrationServiceTest.java @@ -27,6 +27,7 @@ import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.util.ReflectionTestUtils; +import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; import java.io.IOException; @@ -1632,6 +1633,27 @@ public void doGenerateChallenge_withoutTransactionId_thenPass() throws SignUpExc Assert.assertEquals("SUCCESS", generateChallengeResponse.getStatus()); } + @Test + public void doGenerateChallenge_withFailedSendNotification_thenFail() throws SignUpException, IOException { + String identifier = "+85577410541"; + GenerateChallengeRequest generateChallengeRequest = new GenerateChallengeRequest(); + generateChallengeRequest.setIdentifier(identifier); + generateChallengeRequest.setCaptchaToken("mock-captcha"); + generateChallengeRequest.setRegenerate(false); + when(challengeManagerService.generateChallenge(any())).thenReturn("1111"); + when(googleRecaptchaValidatorService.validateCaptcha( + generateChallengeRequest.getCaptchaToken())).thenReturn(true); + when(notificationHelper.sendSMSNotification(any(), any(), any(), any())) + .thenThrow(new RestClientException("failed to send notification")); + + try{ + registrationService.generateChallenge(generateChallengeRequest, ""); + Assert.fail(); + } catch (SignUpException ex) { + Assert.assertEquals("otp_notification_failed", ex.getErrorCode()); + } + } + @Test public void doGenerateChallenge_withRetryAttemptsOver3time_thenFail() throws SignUpException{ String identifier = "+85577410541";