From 83a427b9ddf080d3cdf4c29d5e9b491b604b4690 Mon Sep 17 00:00:00 2001 From: AMIT KUMAR <amit.kumar@tarento.com> Date: Fri, 29 Oct 2021 14:08:54 +0530 Subject: [PATCH] SB-27415 feat:fixing email subject issue (#980) * Issue #SB-27415 feat:fixing email subject issue --- .../otp/validator/OtpRequestValidator.java | 10 +- controller/app/modules/OnRequestHandler.java | 14 +- .../validator/OtpRequestValidatorTest.java | 19 ++ .../main/java/org/sunbird/keys/JsonKey.java | 8 +- .../java/org/sunbird/actor/otp/OTPActor.java | 1 - .../org/sunbird/actor/otp/SendOTPActor.java | 20 +- .../java/org/sunbird/util/otp/OTPUtil.java | 51 +++-- .../org/sunbird/util/otp/OTPUtilTest.java | 181 ++++++++++++++---- 8 files changed, 204 insertions(+), 100 deletions(-) create mode 100644 controller/test/controllers/otp/validator/OtpRequestValidatorTest.java diff --git a/controller/app/controllers/otp/validator/OtpRequestValidator.java b/controller/app/controllers/otp/validator/OtpRequestValidator.java index a610e98e9..9e08a1f90 100644 --- a/controller/app/controllers/otp/validator/OtpRequestValidator.java +++ b/controller/app/controllers/otp/validator/OtpRequestValidator.java @@ -16,6 +16,12 @@ import org.sunbird.validator.BaseRequestValidator; public class OtpRequestValidator extends BaseRequestValidator { + private final List<String> allowedTemplate = + Arrays.asList( + JsonKey.RESET_PASSWORD_TEMPLATE_ID, + JsonKey.WARD_LOGIN_OTP_TEMPLATE_ID, + JsonKey.CONTACT_UPDATE_TEMPLATE_ID); + public void validateGenerateOtpRequest(Request otpRequest) { commonValidation(otpRequest, false); validateTemplateId(otpRequest); @@ -23,9 +29,7 @@ public class OtpRequestValidator extends BaseRequestValidator { private void validateTemplateId(Request otpRequest) { String templateId = (String) otpRequest.getRequest().get(JsonKey.TEMPLATE_ID); - if (StringUtils.isNotBlank(templateId) - && !templateId.equalsIgnoreCase(JsonKey.TEMPLATE_ID_VALUE) - && !templateId.equalsIgnoreCase(JsonKey.WARD_LOGIN_OTP_TEMPLATE_ID)) { + if (StringUtils.isNotBlank(templateId) && !allowedTemplate.contains(templateId)) { throw new ProjectCommonException( ResponseCode.invalidIdentifier.getErrorCode(), ProjectUtil.formatMessage( diff --git a/controller/app/modules/OnRequestHandler.java b/controller/app/modules/OnRequestHandler.java index 9ea8acd85..6d2988948 100644 --- a/controller/app/modules/OnRequestHandler.java +++ b/controller/app/modules/OnRequestHandler.java @@ -27,7 +27,6 @@ import play.mvc.Http; import play.mvc.Result; import play.mvc.Results; import util.Attrs; -import util.Common; import util.RequestInterceptor; public class OnRequestHandler implements ActionCreator { @@ -83,8 +82,7 @@ public class OnRequestHandler implements ActionCreator { } result = delegate.call(request); } else if (JsonKey.UNAUTHORIZED.equals(message)) { - result = - onDataValidationError(request, message, ResponseCode.UNAUTHORIZED.getResponseCode()); + result = onDataValidationError(request, ResponseCode.UNAUTHORIZED.getResponseCode()); } else { result = delegate.call(request); } @@ -144,17 +142,9 @@ public class OnRequestHandler implements ActionCreator { * send some key in header. * * @param request Request - * @param errorMessage String * @return CompletionStage<Result> */ - public CompletionStage<Result> onDataValidationError( - Http.Request request, String errorMessage, int responseCode) { - String context = Common.getFromRequest(request, Attrs.CONTEXT); - logger.info( - "onDataValidationError: Data error found with context info : " - + context - + " , Error Msg: " - + errorMessage); + public CompletionStage<Result> onDataValidationError(Http.Request request, int responseCode) { Response resp = BaseController.createFailureResponse( request, ResponseCode.unAuthorized, ResponseCode.UNAUTHORIZED); diff --git a/controller/test/controllers/otp/validator/OtpRequestValidatorTest.java b/controller/test/controllers/otp/validator/OtpRequestValidatorTest.java new file mode 100644 index 000000000..507dc8f01 --- /dev/null +++ b/controller/test/controllers/otp/validator/OtpRequestValidatorTest.java @@ -0,0 +1,19 @@ +package controllers.otp.validator; + +import org.junit.Test; +import org.sunbird.exception.ProjectCommonException; +import org.sunbird.keys.JsonKey; +import org.sunbird.request.Request; + +public class OtpRequestValidatorTest { + + @Test(expected = ProjectCommonException.class) + public void testValidateGenerateOtpRequest() { + Request otpRequest = new Request(); + otpRequest.getRequest().put(JsonKey.KEY, "xyz@xyz.com"); + otpRequest.getRequest().put(JsonKey.TYPE, "email"); + otpRequest.getRequest().put(JsonKey.TEMPLATE_ID, "invalidTemplateID"); + OtpRequestValidator otpRequestValidator = new OtpRequestValidator(); + otpRequestValidator.validateGenerateOtpRequest(otpRequest); + } +} diff --git a/core/platform-common/src/main/java/org/sunbird/keys/JsonKey.java b/core/platform-common/src/main/java/org/sunbird/keys/JsonKey.java index 63f2d48d3..0ec114bad 100644 --- a/core/platform-common/src/main/java/org/sunbird/keys/JsonKey.java +++ b/core/platform-common/src/main/java/org/sunbird/keys/JsonKey.java @@ -472,7 +472,7 @@ public final class JsonKey { public static final String CATEGORY = "category"; public static final String TEMPLATE_ID = "templateId"; public static final String TEMPLATE_OPTIONS = "templateOptions"; - public static final String TEMPLATE_ID_VALUE = "resetPasswordWithOtp"; + public static final String RESET_PASSWORD_TEMPLATE_ID = "resetPasswordWithOtp"; public static final String VERSION_3 = "v3"; public static final String VERSION_4 = "v4"; public static final String WARD_LOGIN_OTP_TEMPLATE_ID = "wardLoginOTP"; @@ -599,6 +599,12 @@ public final class JsonKey { public static final String IDS = "ids"; public static final String NOTIFICATIONS = "notifications"; public static final String FEEDS = "feeds"; + public static final String EMAIL_VERIFICATION_SUBJECT = "OTP to verify Email"; + public static final String CONTACT_UPDATE_TEMPLATE_ID = "otpContactUpdateTemplate"; + public static final String OTP_CONTACT_UPDATE_TEMPLATE_EMAIL = "otpContactUpdateTemplateEmail"; + public static final String OTP_CONTACT_UPDATE_TEMPLATE_SMS = "otpContactUpdateTemplateSms"; + public static final String CONTACT_DETAILS_UPDATE_VERIFICATION_SUBJECT = + "OTP to edit Diksha Profile"; private JsonKey() {} } diff --git a/service/src/main/java/org/sunbird/actor/otp/OTPActor.java b/service/src/main/java/org/sunbird/actor/otp/OTPActor.java index 2758ef4ba..06cbd7576 100644 --- a/service/src/main/java/org/sunbird/actor/otp/OTPActor.java +++ b/service/src/main/java/org/sunbird/actor/otp/OTPActor.java @@ -52,7 +52,6 @@ public class OTPActor extends BaseActor { logger.debug(request.getRequestContext(), "OTPActor:generateOTP method call start."); String type = (String) request.getRequest().get(JsonKey.TYPE); String key = (String) request.getRequest().get(JsonKey.KEY); - String userId = (String) request.getRequest().get(JsonKey.USER_ID); if (StringUtils.isNotBlank(userId)) { key = otpService.getEmailPhoneByUserId(userId, type, request.getRequestContext()); diff --git a/service/src/main/java/org/sunbird/actor/otp/SendOTPActor.java b/service/src/main/java/org/sunbird/actor/otp/SendOTPActor.java index ecf6bd1fd..6d53218a7 100644 --- a/service/src/main/java/org/sunbird/actor/otp/SendOTPActor.java +++ b/service/src/main/java/org/sunbird/actor/otp/SendOTPActor.java @@ -5,7 +5,6 @@ import java.util.HashMap; import java.util.Map; import javax.inject.Inject; import javax.inject.Named; -import org.apache.commons.lang3.StringUtils; import org.sunbird.actor.core.BaseActor; import org.sunbird.datasecurity.impl.LogMaskServiceImpl; import org.sunbird.keys.JsonKey; @@ -35,7 +34,7 @@ public class SendOTPActor extends BaseActor { String type = (String) request.getRequest().get(JsonKey.TYPE); String key = (String) request.getRequest().get(JsonKey.KEY); String otp = (String) request.getRequest().get(JsonKey.OTP); - String template = (String) request.getRequest().get(JsonKey.TEMPLATE_ID); + String templateId = (String) request.getRequest().get(JsonKey.TEMPLATE_ID); if (JsonKey.EMAIL.equalsIgnoreCase(type) || JsonKey.PREV_USED_EMAIL.equalsIgnoreCase(type) || JsonKey.RECOVERY_EMAIL.equalsIgnoreCase(type)) { @@ -46,14 +45,14 @@ public class SendOTPActor extends BaseActor { + logMaskService.maskEmail(key) + " or userId " + userId); - sendOTPViaEmail(key, otp, userId, template, request.getRequestContext()); + sendOTPViaEmail(key, otp, templateId, request.getRequestContext()); } else if (JsonKey.PHONE.equalsIgnoreCase(type) || JsonKey.PREV_USED_PHONE.equalsIgnoreCase(type) || JsonKey.RECOVERY_PHONE.equalsIgnoreCase(type)) { logger.info( request.getRequestContext(), "SendOTPActor:sendOTP : Sending OTP via sms for Key = " + logMaskService.maskPhone(key)); - sendOTPViaSMS(key, otp, template, request.getRequestContext()); + sendOTPViaSMS(key, otp, templateId, request.getRequestContext()); } else { logger.info(request.getRequestContext(), "SendOTPActor:sendOTP : No Email/Phone provided."); } @@ -62,20 +61,13 @@ public class SendOTPActor extends BaseActor { sender().tell(response, self()); } - private void sendOTPViaEmail( - String key, String otp, String otpType, String template, RequestContext context) { + private void sendOTPViaEmail(String key, String otp, String templateId, RequestContext context) { Map<String, Object> emailTemplateMap = new HashMap<>(); emailTemplateMap.put(JsonKey.EMAIL, key); emailTemplateMap.put(JsonKey.OTP, otp); emailTemplateMap.put(JsonKey.OTP_EXPIRATION_IN_MINUTES, OTPUtil.getOTPExpirationInMinutes()); - emailTemplateMap.put(JsonKey.TEMPLATE_ID, template); - Request emailRequest; - if (StringUtils.isBlank(otpType)) { - emailRequest = OTPUtil.getRequestToSendOTPViaEmail(emailTemplateMap, context); - } else { - emailRequest = - OTPUtil.getRequestToSendOTPViaEmail(emailTemplateMap, JsonKey.RESET_PASSWORD, context); - } + emailTemplateMap.put(JsonKey.TEMPLATE_ID, templateId); + Request emailRequest = OTPUtil.getRequestToSendOTPViaEmail(emailTemplateMap, context); emailRequest.setRequestContext(context); logger.info( context, diff --git a/service/src/main/java/org/sunbird/util/otp/OTPUtil.java b/service/src/main/java/org/sunbird/util/otp/OTPUtil.java index 0b5c1e46a..138fb1834 100644 --- a/service/src/main/java/org/sunbird/util/otp/OTPUtil.java +++ b/service/src/main/java/org/sunbird/util/otp/OTPUtil.java @@ -82,21 +82,22 @@ public final class OTPUtil { return false; } Map<String, String> smsTemplate = new HashMap<>(); - String template = (String) otpMap.get(JsonKey.TEMPLATE_ID); + String templateId = (String) otpMap.get(JsonKey.TEMPLATE_ID); smsTemplate.put(JsonKey.OTP, (String) otpMap.get(JsonKey.OTP)); smsTemplate.put( JsonKey.OTP_EXPIRATION_IN_MINUTES, (String) otpMap.get(JsonKey.OTP_EXPIRATION_IN_MINUTES)); smsTemplate.put( JsonKey.INSTALLATION_NAME, ProjectUtil.getConfigValue(JsonKey.SUNBIRD_INSTALLATION_DISPLAY_NAME)); - String sms; - if (StringUtils.isBlank(template)) { + String sms = ""; + if (StringUtils.isBlank(templateId)) { sms = otpService.getSmsBody(JsonKey.VERIFY_PHONE_OTP_TEMPLATE, smsTemplate, context); - } else if (StringUtils.isNotBlank(template) - && StringUtils.equals(template, JsonKey.WARD_LOGIN_OTP_TEMPLATE_ID)) { + } else if (StringUtils.equals(JsonKey.WARD_LOGIN_OTP_TEMPLATE_ID, templateId)) { sms = otpService.getSmsBody(JsonKey.OTP_PHONE_WARD_LOGIN_TEMPLATE, smsTemplate, context); - } else { + } else if (StringUtils.equals(JsonKey.RESET_PASSWORD_TEMPLATE_ID, templateId)) { sms = otpService.getSmsBody(JsonKey.OTP_PHONE_RESET_PASSWORD_TEMPLATE, smsTemplate, context); + } else if (StringUtils.equals(JsonKey.CONTACT_UPDATE_TEMPLATE_ID, templateId)) { + sms = otpService.getSmsBody(JsonKey.OTP_CONTACT_UPDATE_TEMPLATE_SMS, smsTemplate, context); } logger.debug(context, "OTPUtil:sendOTPViaSMS: SMS text = " + sms); @@ -128,33 +129,30 @@ public final class OTPUtil { } public static Request getRequestToSendOTPViaEmail( - Map<String, Object> emailTemplateMap, String otpType, RequestContext context) { - Request request = null; + Map<String, Object> emailTemplateMap, RequestContext context) { + Request request; if ((StringUtils.isBlank((String) emailTemplateMap.get(JsonKey.EMAIL)))) { - return request; + return null; } + String templateId = (String) emailTemplateMap.get(JsonKey.TEMPLATE_ID); String envName = ProjectUtil.getConfigValue(JsonKey.SUNBIRD_INSTALLATION_DISPLAY_NAME); - String emailSubject = null; - if (JsonKey.RESET_PASSWORD.equalsIgnoreCase(otpType)) { - emailSubject = ProjectUtil.getConfigValue(JsonKey.SUNBIRD_RESET_PASS_MAIL_SUBJECT); - } else { - // default fallback for all other otpType - emailSubject = ProjectUtil.getConfigValue(JsonKey.ONBOARDING_MAIL_SUBJECT); - } - emailTemplateMap.put(JsonKey.SUBJECT, ProjectUtil.formatMessage(emailSubject, envName)); List<String> reciptientsMail = new ArrayList<>(); reciptientsMail.add((String) emailTemplateMap.get(JsonKey.EMAIL)); emailTemplateMap.put(JsonKey.RECIPIENT_EMAILS, reciptientsMail); - if (StringUtils.isBlank((String) emailTemplateMap.get(JsonKey.TEMPLATE_ID))) { + if (StringUtils.isBlank(templateId)) { emailTemplateMap.put(JsonKey.EMAIL_TEMPLATE_TYPE, JsonKey.OTP); - } else if (StringUtils.isNotBlank((String) emailTemplateMap.get(JsonKey.TEMPLATE_ID)) - && StringUtils.equals( - (String) emailTemplateMap.get(JsonKey.TEMPLATE_ID), - JsonKey.WARD_LOGIN_OTP_TEMPLATE_ID)) { + emailTemplateMap.put(JsonKey.SUBJECT, JsonKey.EMAIL_VERIFICATION_SUBJECT); + } else if (StringUtils.equalsIgnoreCase(JsonKey.WARD_LOGIN_OTP_TEMPLATE_ID, templateId)) { emailTemplateMap.put(JsonKey.EMAIL_TEMPLATE_TYPE, JsonKey.OTP_EMAIL_WARD_LOGIN_TEMPLATE); - } else { - // send otp to email while reseting password from portal + String emailSubject = ProjectUtil.getConfigValue(JsonKey.ONBOARDING_MAIL_SUBJECT); + emailTemplateMap.put(JsonKey.SUBJECT, ProjectUtil.formatMessage(emailSubject, envName)); + } else if (StringUtils.equalsIgnoreCase(JsonKey.RESET_PASSWORD_TEMPLATE_ID, templateId)) { emailTemplateMap.put(JsonKey.EMAIL_TEMPLATE_TYPE, JsonKey.OTP_EMAIL_RESET_PASSWORD_TEMPLATE); + emailTemplateMap.put( + JsonKey.SUBJECT, ProjectUtil.getConfigValue(JsonKey.SUNBIRD_RESET_PASS_MAIL_SUBJECT)); + } else if (StringUtils.equalsIgnoreCase(JsonKey.CONTACT_UPDATE_TEMPLATE_ID, templateId)) { + emailTemplateMap.put(JsonKey.EMAIL_TEMPLATE_TYPE, JsonKey.OTP_CONTACT_UPDATE_TEMPLATE_EMAIL); + emailTemplateMap.put(JsonKey.SUBJECT, JsonKey.CONTACT_DETAILS_UPDATE_VERIFICATION_SUBJECT); } emailTemplateMap.put(JsonKey.INSTALLATION_NAME, envName); request = new Request(); @@ -164,11 +162,6 @@ public final class OTPUtil { return request; } - public static Request getRequestToSendOTPViaEmail( - Map<String, Object> emailTemplateMap, RequestContext context) { - return getRequestToSendOTPViaEmail(emailTemplateMap, null, context); - } - public static String getOTPExpirationInMinutes() { String expirationInSeconds = ProjectUtil.getConfigValue(JsonKey.SUNBIRD_OTP_EXPIRATION); int otpExpiration = Integer.parseInt(expirationInSeconds); diff --git a/service/src/test/java/org/sunbird/util/otp/OTPUtilTest.java b/service/src/test/java/org/sunbird/util/otp/OTPUtilTest.java index 924b25625..2c8718982 100644 --- a/service/src/test/java/org/sunbird/util/otp/OTPUtilTest.java +++ b/service/src/test/java/org/sunbird/util/otp/OTPUtilTest.java @@ -1,5 +1,9 @@ package org.sunbird.util.otp; +import static org.powermock.api.mockito.PowerMockito.when; + +import java.util.HashMap; +import java.util.Map; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -18,11 +22,6 @@ import org.sunbird.request.RequestContext; import org.sunbird.service.otp.OTPService; import org.sunbird.util.ProjectUtil; -import java.util.HashMap; -import java.util.Map; - -import static org.powermock.api.mockito.PowerMockito.when; - @RunWith(PowerMockRunner.class) @PrepareForTest({ OTPService.class, @@ -54,17 +53,24 @@ public class OTPUtilTest { when(ProjectUtil.getConfigValue(Mockito.anyString())).thenReturn("anyString"); OTPService otpService = PowerMockito.mock(OTPService.class); PowerMockito.whenNew(OTPService.class).withNoArguments().thenReturn(otpService); - when(otpService.getSmsBody(Mockito.anyString(),Mockito.anyMap(),Mockito.any(RequestContext.class))).thenReturn("some sms text"); + when(otpService.getSmsBody( + Mockito.anyString(), Mockito.anyMap(), Mockito.any(RequestContext.class))) + .thenReturn("some sms text"); ISmsProvider smsProvider = PowerMockito.mock(ISmsProvider.class); PowerMockito.mockStatic(SMSFactory.class); when(SMSFactory.getInstance()).thenReturn(smsProvider); - when(smsProvider.send(Mockito.anyString(),Mockito.anyString(),Mockito.anyString(), Mockito.any(RequestContext.class))).thenReturn(true); + when(smsProvider.send( + Mockito.anyString(), + Mockito.anyString(), + Mockito.anyString(), + Mockito.any(RequestContext.class))) + .thenReturn(true); Map<String, Object> otpMap = new HashMap<>(); - otpMap.put(JsonKey.PHONE,"9742511111"); - otpMap.put(JsonKey.TEMPLATE_ID,"someTemplateId"); - otpMap.put(JsonKey.OTP_EXPIRATION_IN_MINUTES,"30"); - otpMap.put(JsonKey.SUNBIRD_INSTALLATION_DISPLAY_NAME,"displayName"); - otpMap.put(JsonKey.COUNTRY_CODE,"91"); + otpMap.put(JsonKey.PHONE, "9742511111"); + otpMap.put(JsonKey.TEMPLATE_ID, "someTemplateId"); + otpMap.put(JsonKey.OTP_EXPIRATION_IN_MINUTES, "30"); + otpMap.put(JsonKey.SUNBIRD_INSTALLATION_DISPLAY_NAME, "displayName"); + otpMap.put(JsonKey.COUNTRY_CODE, "91"); boolean bool = OTPUtil.sendOTPViaSMS(otpMap, new RequestContext()); Assert.assertTrue(bool); } @@ -75,16 +81,23 @@ public class OTPUtilTest { when(ProjectUtil.getConfigValue(Mockito.anyString())).thenReturn("anyString"); OTPService otpService = PowerMockito.mock(OTPService.class); PowerMockito.whenNew(OTPService.class).withNoArguments().thenReturn(otpService); - when(otpService.getSmsBody(Mockito.anyString(),Mockito.anyMap(),Mockito.any(RequestContext.class))).thenReturn("some sms text"); + when(otpService.getSmsBody( + Mockito.anyString(), Mockito.anyMap(), Mockito.any(RequestContext.class))) + .thenReturn("some sms text"); ISmsProvider smsProvider = PowerMockito.mock(ISmsProvider.class); PowerMockito.mockStatic(SMSFactory.class); when(SMSFactory.getInstance()).thenReturn(smsProvider); - when(smsProvider.send(Mockito.anyString(),Mockito.anyString(),Mockito.anyString(), Mockito.any(RequestContext.class))).thenReturn(true); + when(smsProvider.send( + Mockito.anyString(), + Mockito.anyString(), + Mockito.anyString(), + Mockito.any(RequestContext.class))) + .thenReturn(true); Map<String, Object> otpMap = new HashMap<>(); - otpMap.put(JsonKey.PHONE,"9742511111"); - otpMap.put(JsonKey.OTP_EXPIRATION_IN_MINUTES,"30"); - otpMap.put(JsonKey.SUNBIRD_INSTALLATION_DISPLAY_NAME,"displayName"); - otpMap.put(JsonKey.COUNTRY_CODE,"91"); + otpMap.put(JsonKey.PHONE, "9742511111"); + otpMap.put(JsonKey.OTP_EXPIRATION_IN_MINUTES, "30"); + otpMap.put(JsonKey.SUNBIRD_INSTALLATION_DISPLAY_NAME, "displayName"); + otpMap.put(JsonKey.COUNTRY_CODE, "91"); boolean bool = OTPUtil.sendOTPViaSMS(otpMap, new RequestContext()); Assert.assertTrue(bool); } @@ -95,17 +108,80 @@ public class OTPUtilTest { when(ProjectUtil.getConfigValue(Mockito.anyString())).thenReturn("anyString"); OTPService otpService = PowerMockito.mock(OTPService.class); PowerMockito.whenNew(OTPService.class).withNoArguments().thenReturn(otpService); - when(otpService.getSmsBody(Mockito.anyString(),Mockito.anyMap(),Mockito.any(RequestContext.class))).thenReturn("some sms text"); + when(otpService.getSmsBody( + Mockito.anyString(), Mockito.anyMap(), Mockito.any(RequestContext.class))) + .thenReturn("some sms text"); ISmsProvider smsProvider = PowerMockito.mock(ISmsProvider.class); PowerMockito.mockStatic(SMSFactory.class); when(SMSFactory.getInstance()).thenReturn(smsProvider); - when(smsProvider.send(Mockito.anyString(),Mockito.anyString(),Mockito.anyString(), Mockito.any(RequestContext.class))).thenReturn(true); + when(smsProvider.send( + Mockito.anyString(), + Mockito.anyString(), + Mockito.anyString(), + Mockito.any(RequestContext.class))) + .thenReturn(true); Map<String, Object> otpMap = new HashMap<>(); - otpMap.put(JsonKey.PHONE,"9742511111"); - otpMap.put(JsonKey.TEMPLATE_ID,JsonKey.WARD_LOGIN_OTP_TEMPLATE_ID); - otpMap.put(JsonKey.OTP_EXPIRATION_IN_MINUTES,"30"); - otpMap.put(JsonKey.SUNBIRD_INSTALLATION_DISPLAY_NAME,"displayName"); - otpMap.put(JsonKey.COUNTRY_CODE,"91"); + otpMap.put(JsonKey.PHONE, "9742511111"); + otpMap.put(JsonKey.TEMPLATE_ID, JsonKey.WARD_LOGIN_OTP_TEMPLATE_ID); + otpMap.put(JsonKey.OTP_EXPIRATION_IN_MINUTES, "30"); + otpMap.put(JsonKey.SUNBIRD_INSTALLATION_DISPLAY_NAME, "displayName"); + otpMap.put(JsonKey.COUNTRY_CODE, "91"); + boolean bool = OTPUtil.sendOTPViaSMS(otpMap, new RequestContext()); + Assert.assertTrue(bool); + } + + @Test + public void sendOTPViaSMSTest4() throws Exception { + PowerMockito.mockStatic(ProjectUtil.class); + when(ProjectUtil.getConfigValue(Mockito.anyString())).thenReturn("anyString"); + OTPService otpService = PowerMockito.mock(OTPService.class); + PowerMockito.whenNew(OTPService.class).withNoArguments().thenReturn(otpService); + when(otpService.getSmsBody( + Mockito.anyString(), Mockito.anyMap(), Mockito.any(RequestContext.class))) + .thenReturn("some sms text"); + ISmsProvider smsProvider = PowerMockito.mock(ISmsProvider.class); + PowerMockito.mockStatic(SMSFactory.class); + when(SMSFactory.getInstance()).thenReturn(smsProvider); + when(smsProvider.send( + Mockito.anyString(), + Mockito.anyString(), + Mockito.anyString(), + Mockito.any(RequestContext.class))) + .thenReturn(true); + Map<String, Object> otpMap = new HashMap<>(); + otpMap.put(JsonKey.PHONE, "9742511111"); + otpMap.put(JsonKey.TEMPLATE_ID, JsonKey.RESET_PASSWORD_TEMPLATE_ID); + otpMap.put(JsonKey.OTP_EXPIRATION_IN_MINUTES, "30"); + otpMap.put(JsonKey.SUNBIRD_INSTALLATION_DISPLAY_NAME, "displayName"); + otpMap.put(JsonKey.COUNTRY_CODE, "91"); + boolean bool = OTPUtil.sendOTPViaSMS(otpMap, new RequestContext()); + Assert.assertTrue(bool); + } + + @Test + public void sendOTPViaSMSTest5() throws Exception { + PowerMockito.mockStatic(ProjectUtil.class); + when(ProjectUtil.getConfigValue(Mockito.anyString())).thenReturn("anyString"); + OTPService otpService = PowerMockito.mock(OTPService.class); + PowerMockito.whenNew(OTPService.class).withNoArguments().thenReturn(otpService); + when(otpService.getSmsBody( + Mockito.anyString(), Mockito.anyMap(), Mockito.any(RequestContext.class))) + .thenReturn("some sms text"); + ISmsProvider smsProvider = PowerMockito.mock(ISmsProvider.class); + PowerMockito.mockStatic(SMSFactory.class); + when(SMSFactory.getInstance()).thenReturn(smsProvider); + when(smsProvider.send( + Mockito.anyString(), + Mockito.anyString(), + Mockito.anyString(), + Mockito.any(RequestContext.class))) + .thenReturn(true); + Map<String, Object> otpMap = new HashMap<>(); + otpMap.put(JsonKey.PHONE, "9742511111"); + otpMap.put(JsonKey.TEMPLATE_ID, JsonKey.CONTACT_UPDATE_TEMPLATE_ID); + otpMap.put(JsonKey.OTP_EXPIRATION_IN_MINUTES, "30"); + otpMap.put(JsonKey.SUNBIRD_INSTALLATION_DISPLAY_NAME, "displayName"); + otpMap.put(JsonKey.COUNTRY_CODE, "91"); boolean bool = OTPUtil.sendOTPViaSMS(otpMap, new RequestContext()); Assert.assertTrue(bool); } @@ -113,38 +189,39 @@ public class OTPUtilTest { @Test public void sendOTPViaSMSWithoutPhoneTest() { Map<String, Object> otpMap = new HashMap<>(); - otpMap.put(JsonKey.TEMPLATE_ID,"someTemplateId"); - otpMap.put(JsonKey.OTP_EXPIRATION_IN_MINUTES,"30"); - otpMap.put(JsonKey.SUNBIRD_INSTALLATION_DISPLAY_NAME,"displayName"); - otpMap.put(JsonKey.COUNTRY_CODE,"91"); + otpMap.put(JsonKey.TEMPLATE_ID, "someTemplateId"); + otpMap.put(JsonKey.OTP_EXPIRATION_IN_MINUTES, "30"); + otpMap.put(JsonKey.SUNBIRD_INSTALLATION_DISPLAY_NAME, "displayName"); + otpMap.put(JsonKey.COUNTRY_CODE, "91"); boolean bool = OTPUtil.sendOTPViaSMS(otpMap, new RequestContext()); Assert.assertFalse(bool); } - //@Test + // @Test public void getEmailPhoneByUserIdTest() throws Exception { Map<String, Object> user = new HashMap<>(); - user.put(JsonKey.USER_ID,"1235-4578-156645"); - user.put(JsonKey.EMAIL,"xyz@xyz.com"); - user.put(JsonKey.PHONE,"9999999999"); + user.put(JsonKey.USER_ID, "1235-4578-156645"); + user.put(JsonKey.EMAIL, "xyz@xyz.com"); + user.put(JsonKey.PHONE, "9999999999"); OTPService otpService = PowerMockito.mock(OTPService.class); PowerMockito.whenNew(OTPService.class).withNoArguments().thenReturn(otpService); - String value = otpService.getEmailPhoneByUserId("1235-4578-156645",JsonKey.EMAIL, new RequestContext()); + String value = + otpService.getEmailPhoneByUserId("1235-4578-156645", JsonKey.EMAIL, new RequestContext()); Assert.assertNotNull(value); } @Test(expected = ProjectCommonException.class) public void getEmailPhoneByUserId2Test() { OTPService otpService = new OTPService(); - otpService.getEmailPhoneByUserId("1235-4578-156645",JsonKey.EMAIL, new RequestContext()); + otpService.getEmailPhoneByUserId("1235-4578-156645", JsonKey.EMAIL, new RequestContext()); } @Test(expected = ProjectCommonException.class) public void getEmailPhoneByUserId3Test() { Map<String, Object> user = new HashMap<>(); - user.put(JsonKey.USER_ID,"1235-4578-156645"); + user.put(JsonKey.USER_ID, "1235-4578-156645"); OTPService otpService = new OTPService(); - otpService.getEmailPhoneByUserId("1235-4578-156645",JsonKey.EMAIL, new RequestContext()); + otpService.getEmailPhoneByUserId("1235-4578-156645", JsonKey.EMAIL, new RequestContext()); } @Test @@ -153,7 +230,7 @@ public class OTPUtilTest { when(ProjectUtil.getConfigValue(Mockito.anyString())).thenReturn("anyString"); Map<String, Object> emailTemplateMap = new HashMap<>(); - emailTemplateMap.put(JsonKey.EMAIL,"xyz@xyz.com"); + emailTemplateMap.put(JsonKey.EMAIL, "xyz@xyz.com"); Request request = OTPUtil.getRequestToSendOTPViaEmail(emailTemplateMap, new RequestContext()); Assert.assertNotNull(request); @@ -176,8 +253,32 @@ public class OTPUtilTest { when(ProjectUtil.getConfigValue(Mockito.anyString())).thenReturn("anyString"); Map<String, Object> emailTemplateMap = new HashMap<>(); - emailTemplateMap.put(JsonKey.EMAIL,"xyz@xyz.com"); - emailTemplateMap.put(JsonKey.TEMPLATE_ID,JsonKey.WARD_LOGIN_OTP_TEMPLATE_ID); + emailTemplateMap.put(JsonKey.EMAIL, "xyz@xyz.com"); + emailTemplateMap.put(JsonKey.TEMPLATE_ID, JsonKey.WARD_LOGIN_OTP_TEMPLATE_ID); + Request request = OTPUtil.getRequestToSendOTPViaEmail(emailTemplateMap, new RequestContext()); + Assert.assertNotNull(request); + } + + @Test + public void getRequestToSendOTPViaEmailTest4() { + PowerMockito.mockStatic(ProjectUtil.class); + when(ProjectUtil.getConfigValue(Mockito.anyString())).thenReturn("anyString"); + + Map<String, Object> emailTemplateMap = new HashMap<>(); + emailTemplateMap.put(JsonKey.EMAIL, "xyz@xyz.com"); + emailTemplateMap.put(JsonKey.TEMPLATE_ID, JsonKey.RESET_PASSWORD_TEMPLATE_ID); + Request request = OTPUtil.getRequestToSendOTPViaEmail(emailTemplateMap, new RequestContext()); + Assert.assertNotNull(request); + } + + @Test + public void getRequestToSendOTPViaEmailTest5() { + PowerMockito.mockStatic(ProjectUtil.class); + when(ProjectUtil.getConfigValue(Mockito.anyString())).thenReturn("anyString"); + + Map<String, Object> emailTemplateMap = new HashMap<>(); + emailTemplateMap.put(JsonKey.EMAIL, "xyz@xyz.com"); + emailTemplateMap.put(JsonKey.TEMPLATE_ID, JsonKey.CONTACT_UPDATE_TEMPLATE_ID); Request request = OTPUtil.getRequestToSendOTPViaEmail(emailTemplateMap, new RequestContext()); Assert.assertNotNull(request); } -- GitLab