From 7cda9691930a4b7869d1932a11c196429fb89bf5 Mon Sep 17 00:00:00 2001 From: Tom Saunders Date: Wed, 4 Dec 2024 12:08:04 +0000 Subject: [PATCH] coverage --- .../probate/service/NotificationService.java | 4 +- .../template/pdf/PDFManagementService.java | 4 +- .../NotificationControllerUnitTest.java | 31 ++++++++ .../service/NotificationServiceTest.java | 79 +++++++++++++++++++ 4 files changed, 113 insertions(+), 5 deletions(-) diff --git a/src/main/java/uk/gov/hmcts/probate/service/NotificationService.java b/src/main/java/uk/gov/hmcts/probate/service/NotificationService.java index f3ca9f5e39..449cde6c1d 100644 --- a/src/main/java/uk/gov/hmcts/probate/service/NotificationService.java +++ b/src/main/java/uk/gov/hmcts/probate/service/NotificationService.java @@ -430,7 +430,7 @@ private Document getGeneratedSentEmailDocument(SendEmailResponse response, Strin return pdfManagementService.generateAndUpload(sentEmail, docType); } - private String rerenderAsXhtml(String textToEscape) { + String rerenderAsXhtml(String textToEscape) { final Safelist safelist = Safelist.relaxed(); final org.jsoup.nodes.Document.OutputSettings outputSettings = new org.jsoup.nodes.Document.OutputSettings() @@ -441,7 +441,7 @@ private String rerenderAsXhtml(String textToEscape) { return Jsoup.clean(textToEscape, "", safelist, outputSettings); } - private Document getGeneratedDocument( + Document getGeneratedDocument( final TemplatePreview response, final String emailAddress, final DocumentType docType) { diff --git a/src/main/java/uk/gov/hmcts/probate/service/template/pdf/PDFManagementService.java b/src/main/java/uk/gov/hmcts/probate/service/template/pdf/PDFManagementService.java index 21b46411b6..4c8d0f84bb 100644 --- a/src/main/java/uk/gov/hmcts/probate/service/template/pdf/PDFManagementService.java +++ b/src/main/java/uk/gov/hmcts/probate/service/template/pdf/PDFManagementService.java @@ -95,9 +95,7 @@ public Document generateAndUpload(CaveatCallbackRequest callbackRequest, Documen } public Document generateAndUpload(SentEmail sentEmail, DocumentType documentType) { - final String json = toJson(sentEmail, documentType); - log.info("json:\n\n{}\n\n", json); - return generateAndUpload(json, documentType); + return generateAndUpload(toJson(sentEmail, documentType), documentType); } private Document generateAndUpload(String json, DocumentType documentType) { diff --git a/src/test/java/uk/gov/hmcts/probate/controller/NotificationControllerUnitTest.java b/src/test/java/uk/gov/hmcts/probate/controller/NotificationControllerUnitTest.java index 18df0afa5f..882bffc86b 100644 --- a/src/test/java/uk/gov/hmcts/probate/controller/NotificationControllerUnitTest.java +++ b/src/test/java/uk/gov/hmcts/probate/controller/NotificationControllerUnitTest.java @@ -414,6 +414,37 @@ void shouldSendEmailPreview() { assertThat(stringResponseEntity.getStatusCode(), is(HttpStatus.OK)); } + @Test + void shouldHandleFailureToPreview() throws NotificationClientException { + CaseDetails caseDetails = new CaseDetails(CaseData.builder() + .applicationType(SOLICITOR) + .registryLocation("Manchester") + .solsSolicitorEmail("solicitor@probate-test.com") + .solsSolicitorAppReference("1234-5678-9012") + .languagePreferenceWelsh("No") + .removedRepresentative(RemovedRepresentative.builder() + .solicitorEmail("solicitor@gmail.com") + .solicitorFirstName("FirstName") + .solicitorLastName("LastName").build()) + .build(), LAST_MODIFIED, ID); + callbackRequest = new CallbackRequest(caseDetails); + document = Document.builder() + .documentDateAdded(LocalDate.now()) + .documentFileName("fileName") + .documentGeneratedBy("generatedBy") + .documentLink( + DocumentLink.builder().documentUrl("url").documentFilename("file") + .documentBinaryUrl("binary").build()) + .documentType(DocumentType.SENT_EMAIL) + .build(); + callbackResponse = CallbackResponse.builder().errors(Collections.EMPTY_LIST).build(); + when(informationRequestService.emailPreview(any())).thenReturn(document); + when(notificationService.emailPreviewAlt(any())).thenThrow(NotificationClientException.class); + ResponseEntity stringResponseEntity = + notificationController.emailPreview(callbackRequest); + assertThat(stringResponseEntity.getStatusCode(), is(HttpStatus.OK)); + } + @Test void shouldNotSendNocEmailForBulkScanCaseFirstNoc() throws NotificationClientException { setUpMocks(NOC); diff --git a/src/test/java/uk/gov/hmcts/probate/service/NotificationServiceTest.java b/src/test/java/uk/gov/hmcts/probate/service/NotificationServiceTest.java index cabb22285d..61434c1142 100644 --- a/src/test/java/uk/gov/hmcts/probate/service/NotificationServiceTest.java +++ b/src/test/java/uk/gov/hmcts/probate/service/NotificationServiceTest.java @@ -1,5 +1,6 @@ package uk.gov.hmcts.probate.service; +import org.json.JSONObject; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -9,6 +10,7 @@ import uk.gov.hmcts.probate.config.notifications.EmailAddresses; import uk.gov.hmcts.probate.config.notifications.NotificationTemplates; import uk.gov.hmcts.probate.config.properties.registries.RegistriesProperties; +import uk.gov.hmcts.probate.model.DocumentType; import uk.gov.hmcts.probate.service.documentmanagement.DocumentManagementService; import uk.gov.hmcts.probate.service.notification.CaveatPersonalisationService; import uk.gov.hmcts.probate.service.notification.GrantOfRepresentationPersonalisationService; @@ -23,6 +25,7 @@ import uk.gov.hmcts.reform.authorisation.generators.AuthTokenGenerator; import uk.gov.service.notify.NotificationClient; import uk.gov.service.notify.NotificationClientException; +import uk.gov.service.notify.TemplatePreview; import java.util.Collections; import java.util.List; @@ -30,6 +33,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; class NotificationServiceTest { @@ -138,4 +142,79 @@ void givenPersonalisationWithNoIssue_whenCommonValidation_thenReturnsAllOk() thr assertEquals(CommonNotificationResult.ALL_OK, result); } + + @Test + void testRerender() { + notificationService.rerenderAsXhtml(""); + } + + @Test + void testGetDocument_master() { + final JSONObject templateData = new JSONObject(); + templateData.put("id", "00000000-0000-0000-0000-000000000000"); + templateData.put("type", "email"); + templateData.put("version", 1); + templateData.put("body", "body"); + templateData.put("subject", "subject"); + templateData.put("html", "html"); + final TemplatePreview templatePreview = new TemplatePreview(templateData); + + + when(featureToggleServiceMock.chooseDocGen()).thenReturn(FeatureToggleService.DocGen.MASTER); + when(markdownTransformationServiceMock.toHtml(any())).thenReturn(""); + + notificationService.getGeneratedDocument(templatePreview, "emailAddr", DocumentType.SENT_EMAIL); + } + + @Test + void testGetDocument_pr() { + final JSONObject templateData = new JSONObject(); + templateData.put("id", "00000000-0000-0000-0000-000000000000"); + templateData.put("type", "email"); + templateData.put("version", 1); + templateData.put("body", "body"); + templateData.put("subject", "subject"); + templateData.put("html", "html"); + final TemplatePreview templatePreview = new TemplatePreview(templateData); + + + when(featureToggleServiceMock.chooseDocGen()).thenReturn(FeatureToggleService.DocGen.PR); + when(sentEmailPersonalisationServiceMock.getPersonalisation(any())).thenReturn(Map.of()); + + notificationService.getGeneratedDocument(templatePreview, "emailAddr", DocumentType.SENT_EMAIL); + } + + @Test + void testGetDocument_html() { + final JSONObject templateData = new JSONObject(); + templateData.put("id", "00000000-0000-0000-0000-000000000000"); + templateData.put("type", "email"); + templateData.put("version", 1); + templateData.put("body", "body"); + templateData.put("subject", "subject"); + templateData.put("html", "html"); + final TemplatePreview templatePreview = new TemplatePreview(templateData); + + + when(featureToggleServiceMock.chooseDocGen()).thenReturn(FeatureToggleService.DocGen.HTML); + + notificationService.getGeneratedDocument(templatePreview, "emailAddr", DocumentType.SENT_EMAIL); + } + + @Test + void testGetDocument_htmlProc() { + final JSONObject templateData = new JSONObject(); + templateData.put("id", "00000000-0000-0000-0000-000000000000"); + templateData.put("type", "email"); + templateData.put("version", 1); + templateData.put("body", "body"); + templateData.put("subject", "subject"); + templateData.put("html", "html"); + final TemplatePreview templatePreview = new TemplatePreview(templateData); + + + when(featureToggleServiceMock.chooseDocGen()).thenReturn(FeatureToggleService.DocGen.HTML_PROC); + + notificationService.getGeneratedDocument(templatePreview, "emailAddr", DocumentType.SENT_EMAIL); + } }