Skip to content

Commit

Permalink
ES 802 (#76)
Browse files Browse the repository at this point in the history
* Signup service - Changes required in Async notification calls

Signed-off-by: mengleang-ngoun <[email protected]>

* throw exception and add test case

Signed-off-by: mengleang-ngoun <[email protected]>

---------

Signed-off-by: mengleang-ngoun <[email protected]>
  • Loading branch information
mengleang-0090 authored Mar 2, 2024
1 parent c168148 commit 99fbf1b
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 13 deletions.
28 changes: 28 additions & 0 deletions signup-service/src/main/java/io/mosip/signup/config/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.mosip.signup.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Configuration
public class Config {

@Value("${mosip.signup.task.core.pool.size:20}")
private int taskCorePoolSize;

@Value("${mosip.signup.task.max.pool.size:20}")
private int taskMaxPoolSize;

@Bean
public Executor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(taskCorePoolSize);
executor.setMaxPoolSize(taskMaxPoolSize);
executor.setThreadNamePrefix("MOSIP-SIGNUP-Async-Thread-");
executor.initialize();
return executor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,16 @@ public class NotificationHelper {
@Value("${mosip.signup.send-notification.endpoint}")
private String sendNotificationEndpoint;


@Async
public CompletableFuture<RestResponseWrapper<NotificationResponse>> sendSMSNotificationAsync
public RestResponseWrapper<NotificationResponse> sendSMSNotification
(String number, String locale, String templateKey, Map<String, String> params){

locale = locale != null ? locale : "khm";
String message = locale.equalsIgnoreCase("eng") ?
environment.getProperty(templateKey + "." + locale) :
new String(Base64.getDecoder().decode(environment.getProperty(templateKey + "." + locale)));

if(params != null && message != null){
for (Map.Entry<String, String> entry: params.entrySet()){
if (params != null && message != null) {
for (Map.Entry<String, String> entry : params.entrySet()) {
message = message.replace(entry.getKey(), entry.getValue());
}
}
Expand All @@ -55,10 +53,15 @@ public class NotificationHelper {
restRequestWrapper.setRequesttime(IdentityProviderUtil.getUTCDateTime());
restRequestWrapper.setRequest(notificationRequest);

return CompletableFuture.supplyAsync(() -> selfTokenRestTemplate
.exchange(sendNotificationEndpoint,
return selfTokenRestTemplate.exchange(sendNotificationEndpoint,
HttpMethod.POST,
new HttpEntity<>(restRequestWrapper),
new ParameterizedTypeReference<RestResponseWrapper<NotificationResponse>>(){}).getBody());
new ParameterizedTypeReference<RestResponseWrapper<NotificationResponse>>(){}).getBody();
}

@Async
public CompletableFuture<RestResponseWrapper<NotificationResponse>> sendSMSNotificationAsync
(String number, String locale, String templateKey, Map<String, String> params){
return CompletableFuture.supplyAsync(() -> sendSMSNotification(number, locale, templateKey, params));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.Cookie;
Expand Down Expand Up @@ -166,11 +167,14 @@ public GenerateChallengeResponse generateChallenge(GenerateChallengeRequest gene

HashMap<String, String> hashMap = new LinkedHashMap<>();
hashMap.put("{challenge}", challenge);
notificationHelper.sendSMSNotificationAsync(generateChallengeRequest.getIdentifier(), transaction.getLocale(),
SEND_OTP_SMS_NOTIFICATION_TEMPLATE_KEY, hashMap)
.thenAccept(notificationResponseRestResponseWrapper ->
log.debug(notificationLogging, notificationResponseRestResponseWrapper)
);
RestResponseWrapper<NotificationResponse> notificationResponseRest;
try{
notificationResponseRest = notificationHelper.sendSMSNotification(generateChallengeRequest.getIdentifier(), transaction.getLocale(),
SEND_OTP_SMS_NOTIFICATION_TEMPLATE_KEY, hashMap);
log.debug(notificationLogging, notificationResponseRest);
}catch (RestClientException e){
throw new SignUpException(ErrorConstants.OTP_NOTIFICATION_FAILED);
}
return new GenerateChallengeResponse(ActionStatus.SUCCESS);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ mosip.signup.status-check.txn.timeout=200
mosip.signup.status.request.delay=20
mosip.signup.status.request.limit=10

mosip.signup.task.core.pool.size=2
mosip.signup.task.max.pool.size=4

## ------------------------------------- challenge configuration -------------------------------------------------------
mosip.signup.supported.generate-challenge-type=OTP
mosip.signup.supported.challenge-format-types={'alpha-numeric', 'base64url-encoded-json'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down

0 comments on commit 99fbf1b

Please sign in to comment.