Skip to content

Commit

Permalink
DTSPB-4475 fix token issue and handle npe
Browse files Browse the repository at this point in the history
  • Loading branch information
FeliTam committed Feb 12, 2025
1 parent 8a8d5d8 commit 28e11b3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ private ReturnedCases runQuery(String jsonQuery, CaseType caseType) {
tokenHeaders = new HttpHeaders();
tokenHeaders.setContentType(MediaType.APPLICATION_JSON);
tokenHeaders.add(SERVICE_AUTH, "Bearer " + serviceAuthTokenGenerator.generate());
tokenHeaders.add(AUTHORIZATION, securityUtils.getCaseworkerToken());
tokenHeaders.add(AUTHORIZATION, securityUtils.getSchedulerToken());
log.info("DONE securityUtils.getCaseworkerToken()");
} finally {
entity = new HttpEntity<>(jsonQuery, tokenHeaders);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,13 @@ public SendEmailResponse sendSmeeAndFordEmail(List<ReturnedCaseDetails> caseDeta
public void sendDisposalReminderEmail(ReturnedCaseDetails caseDetails) throws NotificationClientException {
log.info("Sending Disposal Reminder email");
String emailAddress = Optional.ofNullable(caseDetails.getData())
.map(this::getEmail)
.flatMap(data -> {
try {
return Optional.ofNullable(getEmail(data));
} catch (BadRequestException e) {
return Optional.empty();
}
})
.orElseGet(() -> getUserEmail(caseDetails.getId()));

if (emailAddress == null) {
Expand Down Expand Up @@ -566,14 +572,18 @@ private SendEmailResponse getSendEmailResponse(State state, String templateId, S
}

private String getEmail(CaseData caseData) {
switch (caseData.getApplicationType()) {
case SOLICITOR:
return caseData.getSolsSolicitorEmail().toLowerCase();
case PERSONAL:
return caseData.getPrimaryApplicantEmailAddress().toLowerCase();
default:
throw new BadRequestException("Unsupported application type");
if (caseData == null || caseData.getApplicationType() == null) {
throw new BadRequestException("Casedata or ApplicationType is null");
}
return switch (caseData.getApplicationType()) {
case SOLICITOR -> Optional.ofNullable(caseData.getSolsSolicitorEmail())
.map(String::toLowerCase)
.orElse(null);
case PERSONAL -> Optional.ofNullable(caseData.getPrimaryApplicantEmailAddress())
.map(String::toLowerCase)
.orElse(null);
default -> throw new BadRequestException("Unsupported application type");
};
}

private String getUserEmail(Long caseReference) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void sendEmailForInactiveCase(String switchDate, String runDate, long ina
log.info("Sending email for case id: {}", caseDetails.getId());
try {
notificationService.sendDisposalReminderEmail(caseDetails);
} catch (NotificationClientException e) {
} catch (NotificationClientException | RuntimeException e) {
log.info("Error sending email for case id: {}", caseDetails.getId());
failedCases.add(caseDetails.getId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void setUp() {
MockitoAnnotations.openMocks(this);

when(serviceAuthTokenGenerator.generate()).thenReturn("Bearer 321");
when(securityUtils.getCaseworkerToken()).thenReturn("Bearer 123");
when(securityUtils.getSchedulerToken()).thenReturn("Bearer 123");
when(headers.getAuthorizationHeaders()).thenReturn(new HttpHeaders());

when(ccdDataStoreAPIConfiguration.getHost()).thenReturn("http://localhost");
Expand Down

0 comments on commit 28e11b3

Please sign in to comment.