Skip to content

Commit

Permalink
Merge pull request #1850 from LoganathanSekar7627/MOSIP-31735-notific…
Browse files Browse the repository at this point in the history
…ation-support-for-opencrvs

MOSIP-31735 Added support for configuring new reg type mapping to notification te…
  • Loading branch information
ckm007 authored Mar 1, 2024
2 parents d7af27d + c05b2b3 commit c3288cd
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.json.JSONException;
import org.json.simple.parser.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.event.EventListener;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -93,6 +94,7 @@ public class NotificationServiceImpl implements NotificationService {
private static final String DUPLICATE_UIN=NOTIFICATION_TEMPLATE_CODE+"duplicate.uin.";
private static final String TECHNICAL_ISSUE=NOTIFICATION_TEMPLATE_CODE+"technical.issue.";
private static final String PAUSED_FOR_ADDITIONAL_INFO=NOTIFICATION_TEMPLATE_CODE+"paused.for.additional.info.";
private static final String ADDITIONAL_REGTYPE_TEMPLATETYPE_MAP = "registration.processor.additional.regtype_templatetype_map";


/** The core audit request builder. */
Expand Down Expand Up @@ -129,6 +131,8 @@ public class NotificationServiceImpl implements NotificationService {

@Value("${registration.processor.notification_service_pausedforadditonalinfo_subscriber_callback_url}")
private String pausedForAdditonalInfoCallbackURL;

private Map<String,String> regtypeTemplateTypeMap;

/** The rest client service. */
@Autowired
Expand All @@ -143,6 +147,23 @@ public class NotificationServiceImpl implements NotificationService {

@Autowired
private Environment env;

@PostConstruct
public void postConstruct() {
String property = env.getProperty(ADDITIONAL_REGTYPE_TEMPLATETYPE_MAP);
if(property != null) {
try {
Map<String, String> map = mapper.readValue(property.getBytes(), Map.class);
regtypeTemplateTypeMap = Collections.unmodifiableMap(map);
} catch (IOException e) {
regProcLogger.debug(LoggerFileConstant.SESSIONID.toString(), LoggerFileConstant.REGISTRATIONID.toString(), "NA",
"NotificationServiceImpl::postConstruct-failed to parse property value of "+ ADDITIONAL_REGTYPE_TEMPLATETYPE_MAP);
regtypeTemplateTypeMap = Collections.unmodifiableMap(Map.of());
}
} else {
regtypeTemplateTypeMap = Collections.unmodifiableMap(Map.of());
}
}

// sends init subscribe req to hub
@Scheduled(fixedDelayString = "${mosip.regproc.websub.resubscription.delay.millisecs:43200000}",
Expand Down Expand Up @@ -287,6 +308,12 @@ else if (regtype.equalsIgnoreCase(RegistrationType.ACTIVATED.toString()))
type = NotificationTemplateType.UIN_UPDATE;
else if (regtype.equalsIgnoreCase(RegistrationType.DEACTIVATED.toString()))
type = NotificationTemplateType.UIN_UPDATE;
else {
String typeFromProp = regtypeTemplateTypeMap.get(regtype);
if(typeFromProp != null) {
type = NotificationTemplateType.valueOf(typeFromProp);
}
}
return type;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
Expand Down Expand Up @@ -32,6 +33,7 @@
import io.mosip.kernel.websub.api.verifier.AuthenticatedContentVerifier;
import io.mosip.registration.processor.core.code.ApiName;
import io.mosip.registration.processor.core.exception.ApisResourceAccessException;
import io.mosip.registration.processor.core.exception.util.PlatformErrorMessages;
import io.mosip.registration.processor.core.http.ResponseWrapper;
import io.mosip.registration.processor.core.notification.template.generator.dto.ResponseDto;
import io.mosip.registration.processor.core.notification.template.generator.dto.SmsResponseDto;
Expand All @@ -40,12 +42,12 @@
import io.mosip.registration.processor.core.packet.dto.Identity;
import io.mosip.registration.processor.core.spi.message.sender.MessageNotificationService;
import io.mosip.registration.processor.core.spi.restclient.RegistrationProcessorRestClientService;
import io.mosip.registration.processor.core.status.util.StatusUtil;
import io.mosip.registration.processor.core.workflow.dto.WorkflowCompletedEventDTO;
import io.mosip.registration.processor.core.workflow.dto.WorkflowPausedForAdditionalInfoEventDTO;
import io.mosip.registration.processor.message.sender.exception.EmailIdNotFoundException;
import io.mosip.registration.processor.message.sender.exception.PhoneNumberNotFoundException;
import io.mosip.registration.processor.message.sender.exception.TemplateGenerationFailedException;
import io.mosip.registration.processor.notification.service.NotificationService;
import io.mosip.registration.processor.notification.service.impl.NotificationServiceImpl;
import io.mosip.registration.processor.rest.client.audit.builder.AuditLogRequestBuilder;

Expand All @@ -56,8 +58,7 @@ public class NotificationServiceTest {
@Mock
private MessageNotificationService<SmsResponseDto, ResponseDto, MultipartFile[]> service;

@Mock
private ObjectMapper mapper;
private ObjectMapper mapper = new ObjectMapper();

@Mock
private RegistrationProcessorRestClientService<Object> restClientService;
Expand All @@ -73,7 +74,7 @@ public class NotificationServiceTest {
Identity identity = new Identity();

@InjectMocks
private NotificationService notificationService = new NotificationServiceImpl();
private NotificationServiceImpl notificationService = new NotificationServiceImpl();

@Value("${websub.hub.url}")
private String hubURL;
Expand All @@ -96,8 +97,12 @@ public void setup() throws Exception {
subscriptionChangeResponse.setTopic(topic);
ReflectionTestUtils.setField(notificationService, "notificationTypes", "SMS|EMAIL");
ReflectionTestUtils.setField(notificationService, "notificationEmails", "[email protected]");
ReflectionTestUtils.setField(notificationService, "mapper", mapper);
when(subs.subscribe(Mockito.any())).thenReturn(subscriptionChangeResponse);
when(authenticatedContentVerifier.verifyAuthorizedContentVerified(any(), any())).thenReturn(true);
Mockito.lenient().when(env.getProperty("registration.processor.additional.regtype_templatetype_map"))
.thenReturn("{\"OPENCRVS_NEW\":\"UIN_CREATED\"}");
notificationService.postConstruct();
}

@Test
Expand Down Expand Up @@ -127,6 +132,91 @@ public void testMessageSentUINGenerated() throws Exception {
completedEventDTO.setInstanceId("85425022110000120190117110505");
completedEventDTO.setResultCode("PROCESSED");
completedEventDTO.setWorkflowType("NEW");

when(auditLogRequestBuilder.createAuditRequestBuilder(anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString()))
.thenAnswer(invocation -> {
String moduleId = (String) invocation.getArguments()[0];
assertEquals(StatusUtil.MESSAGE_SENDER_NOTIF_SUCC.getMessage(), moduleId);
return null;
});

ResponseEntity<Void> res=notificationService.process(completedEventDTO);
assertEquals(200, res.getStatusCodeValue());
}

@Test
public void testMessageSentUINGenerated_regType_OPENCRVS_NEW() throws Exception {
List<TemplateDto> templates = new ArrayList<TemplateDto>();
TemplateDto templateEmail = new TemplateDto();
TemplateDto templateSMS = new TemplateDto();
TemplateResponseDto templateResponseDto = new TemplateResponseDto();
templateSMS.setTemplateTypeCode("RPR_UIN_GEN_SMS");
templates.add(templateSMS);
templateEmail.setTemplateTypeCode("RPR_UIN_GEN_EMAIL");
templates.add(templateEmail);
templateResponseDto.setTemplates(templates);
ResponseWrapper<TemplateResponseDto> responseWrapper=new ResponseWrapper<>();
responseWrapper.setResponse(templateResponseDto);
responseWrapper.setErrors(null);
SmsResponseDto smsResponse = new SmsResponseDto();
smsResponse.setStatus("success");
ResponseDto responseDto = new ResponseDto();
responseDto.setStatus("success");
Mockito.when(env.getProperty(any())).thenReturn("RPR_UIN_GEN_SMS").thenReturn("RPR_UIN_GEN_EMAIL")
.thenReturn("RPR_UIN_GEN_EMAIL_SUB");
when(service.sendSmsNotification(any(), any(), any(), any(), any(), any())).thenReturn(smsResponse);
when(service.sendEmailNotification(any(), any(), any(), any(), any(), any(), any(), any(), any())).thenReturn(responseDto);
when(restClientService.getApi(Mockito.eq(ApiName.TEMPLATES), any(), Mockito.eq(""), Mockito.eq(""), Mockito.eq(ResponseWrapper.class))).thenReturn(responseWrapper);
WorkflowCompletedEventDTO completedEventDTO= new WorkflowCompletedEventDTO();
completedEventDTO.setInstanceId("85425022110000120190117110505");
completedEventDTO.setResultCode("PROCESSED");
completedEventDTO.setWorkflowType("OPENCRVS_NEW");

when(auditLogRequestBuilder.createAuditRequestBuilder(anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString()))
.thenAnswer(invocation -> {
String moduleId = (String) invocation.getArguments()[0];
assertEquals(StatusUtil.MESSAGE_SENDER_NOTIF_SUCC.getMessage(), moduleId);
return null;
});

ResponseEntity<Void> res=notificationService.process(completedEventDTO);
assertEquals(200, res.getStatusCodeValue());
}

@Test
public void testMessageSentUINGenerated_regType_XYZ() throws Exception {
List<TemplateDto> templates = new ArrayList<TemplateDto>();
TemplateDto templateEmail = new TemplateDto();
TemplateDto templateSMS = new TemplateDto();
TemplateResponseDto templateResponseDto = new TemplateResponseDto();
templateSMS.setTemplateTypeCode("RPR_UIN_GEN_SMS");
templates.add(templateSMS);
templateEmail.setTemplateTypeCode("RPR_UIN_GEN_EMAIL");
templates.add(templateEmail);
templateResponseDto.setTemplates(templates);
ResponseWrapper<TemplateResponseDto> responseWrapper=new ResponseWrapper<>();
responseWrapper.setResponse(templateResponseDto);
responseWrapper.setErrors(null);
SmsResponseDto smsResponse = new SmsResponseDto();
smsResponse.setStatus("success");
ResponseDto responseDto = new ResponseDto();
responseDto.setStatus("success");
Mockito.when(env.getProperty(any())).thenReturn("RPR_UIN_GEN_SMS").thenReturn("RPR_UIN_GEN_EMAIL")
.thenReturn("RPR_UIN_GEN_EMAIL_SUB");
when(service.sendSmsNotification(any(), any(), any(), any(), any(), any())).thenReturn(smsResponse);
when(service.sendEmailNotification(any(), any(), any(), any(), any(), any(), any(), any(), any())).thenReturn(responseDto);
when(restClientService.getApi(Mockito.eq(ApiName.TEMPLATES), any(), Mockito.eq(""), Mockito.eq(""), Mockito.eq(ResponseWrapper.class))).thenReturn(responseWrapper);
WorkflowCompletedEventDTO completedEventDTO= new WorkflowCompletedEventDTO();
completedEventDTO.setInstanceId("85425022110000120190117110505");
completedEventDTO.setResultCode("PROCESSED");
completedEventDTO.setWorkflowType("XYZ");

when(auditLogRequestBuilder.createAuditRequestBuilder(anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString()))
.thenAnswer(invocation -> {
String moduleId = (String) invocation.getArguments()[0];
assertEquals(PlatformErrorMessages.RPR_EMAIL_PHONE_TEMPLATE_NOTIFICATION_MISSING.getMessage(), moduleId);
return null;
});

ResponseEntity<Void> res=notificationService.process(completedEventDTO);
assertEquals(200, res.getStatusCodeValue());
Expand Down

0 comments on commit c3288cd

Please sign in to comment.