-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DIAC-546 converting notification body's to pdf (#939)
* DIAC-546 temp commit to keep stuff saved * DIAC-546 converting notification body to pdf and writing them in if valid and not already done * DIAC-546 fixing sonar smells * DIAC-546 fixing stored notification constructor and allowing event * DIAC-546 fixing stored notification constructor and allowing event * DIAC-546 upload potential fix * DIAC-546 adding .PDF to end of filename * DIAC-546 adding subject to the StoredNotification object and making it a builder * DIAC-546 fixing tests * DIAC-546 fixing tests * DIAC-546 moving event access to admin and system * DIAC-546 capitalising failed statuses * DIAC-578 fix * DIAC-546 - fix build failure * DIAC-546 - fix unit test --------- Co-authored-by: alivenichoppa <97441560+alivenichoppa@users.noreply.github.com> Co-authored-by: KleoG <KleoG@users.noreply.github.com> Co-authored-by: kleog <kleogeorgiou@hotmail.com>
- Loading branch information
1 parent
d967979
commit 1c2de41
Showing
15 changed files
with
599 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/java/uk/gov/hmcts/reform/iacasedocumentsapi/domain/entities/StoredNotification.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities; | ||
|
||
import lombok.*; | ||
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.ccd.field.Document; | ||
|
||
@EqualsAndHashCode | ||
@ToString | ||
@Getter | ||
@Builder | ||
@AllArgsConstructor | ||
public class StoredNotification { | ||
@NonNull private String notificationId; | ||
@NonNull private String notificationDateSent; | ||
@NonNull private String notificationSentTo; | ||
@NonNull private String notificationBody; | ||
@Setter private Document notificationDocument; | ||
@NonNull private String notificationMethod; | ||
@NonNull private String notificationStatus; | ||
@NonNull private String notificationReference; | ||
@NonNull private String notificationSubject; | ||
private String notificationErrorMessage; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...s/reform/iacasedocumentsapi/domain/handlers/presubmit/SaveNotificationsToDataHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package uk.gov.hmcts.reform.iacasedocumentsapi.domain.handlers.presubmit; | ||
|
||
import org.springframework.stereotype.Component; | ||
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.AsylumCase; | ||
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.StoredNotification; | ||
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.ccd.CaseDetails; | ||
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.ccd.Event; | ||
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.ccd.callback.Callback; | ||
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.ccd.callback.PreSubmitCallbackResponse; | ||
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.ccd.callback.PreSubmitCallbackStage; | ||
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.ccd.field.Document; | ||
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.ccd.field.IdValue; | ||
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.handlers.PreSubmitCallbackHandler; | ||
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.service.SaveNotificationsToDataPdfService; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import static java.util.Collections.emptyList; | ||
import static java.util.Objects.requireNonNull; | ||
import static uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.AsylumCaseDefinition.NOTIFICATIONS; | ||
|
||
@Component | ||
public class SaveNotificationsToDataHandler implements PreSubmitCallbackHandler<AsylumCase> { | ||
|
||
private final SaveNotificationsToDataPdfService saveNotificationsToDataPdfService; | ||
|
||
public SaveNotificationsToDataHandler( | ||
SaveNotificationsToDataPdfService saveNotificationsToDataPdfService | ||
) { | ||
this.saveNotificationsToDataPdfService = saveNotificationsToDataPdfService; | ||
} | ||
|
||
public boolean canHandle( | ||
PreSubmitCallbackStage callbackStage, | ||
Callback<AsylumCase> callback | ||
) { | ||
requireNonNull(callbackStage, "callbackStage must not be null"); | ||
requireNonNull(callback, "callback must not be null"); | ||
|
||
return callbackStage == PreSubmitCallbackStage.ABOUT_TO_SUBMIT | ||
&& callback.getEvent() == Event.SAVE_NOTIFICATIONS_TO_DATA; | ||
} | ||
|
||
public PreSubmitCallbackResponse<AsylumCase> handle( | ||
PreSubmitCallbackStage callbackStage, | ||
Callback<AsylumCase> callback | ||
) { | ||
if (!canHandle(callbackStage, callback)) { | ||
throw new IllegalStateException("Cannot handle callback"); | ||
} | ||
|
||
final CaseDetails<AsylumCase> caseDetails = callback.getCaseDetails(); | ||
final AsylumCase asylumCase = caseDetails.getCaseData(); | ||
|
||
Optional<List<IdValue<StoredNotification>>> maybeExistingNotifications = | ||
asylumCase.read(NOTIFICATIONS); | ||
|
||
ArrayList<IdValue<StoredNotification>> newNotifications = new ArrayList<>(); | ||
List<String> invalidNotificationStatuses = List.of("Cancelled", "Failed", "Technical-failure", | ||
"Temporary-failure", "Permanent-failure", "Validation-failed", "Virus-scan-failed"); | ||
for (IdValue<StoredNotification> notification : maybeExistingNotifications.orElse(emptyList())) { | ||
StoredNotification storedNotification = notification.getValue(); | ||
if (storedNotification.getNotificationDocument() == null | ||
&& !invalidNotificationStatuses.contains(storedNotification.getNotificationStatus())) { | ||
String notificationBody = storedNotification.getNotificationBody(); | ||
String notificationReference = storedNotification.getNotificationReference(); | ||
Document notificationPdf = | ||
saveNotificationsToDataPdfService.createPdf(notificationBody, notificationReference); | ||
storedNotification.setNotificationDocument(notificationPdf); | ||
notification = new IdValue<>(notification.getId(), storedNotification); | ||
} | ||
newNotifications.add(notification); | ||
} | ||
asylumCase.write(NOTIFICATIONS, newNotifications); | ||
|
||
return new PreSubmitCallbackResponse<>(asylumCase); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...gov/hmcts/reform/iacasedocumentsapi/domain/service/SaveNotificationsToDataPdfService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package uk.gov.hmcts.reform.iacasedocumentsapi.domain.service; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.springframework.core.io.ByteArrayResource; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.stereotype.Service; | ||
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.ccd.field.Document; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
@Service | ||
public class SaveNotificationsToDataPdfService { | ||
|
||
private static final String PDF_CONTENT_TYPE = "application/pdf"; | ||
|
||
private final DocumentToPdfConverter documentToPdfConverter; | ||
private final DocumentUploader documentUploader; | ||
|
||
public SaveNotificationsToDataPdfService( | ||
DocumentUploader documentUploader, | ||
DocumentToPdfConverter documentToPdfConverter | ||
) { | ||
this.documentUploader = documentUploader; | ||
this.documentToPdfConverter = documentToPdfConverter; | ||
} | ||
|
||
public Document createPdf(String notificationBody, String notificationReference) { | ||
|
||
byte[] byteArray = notificationBody.getBytes(); | ||
Resource resource = new ByteArrayResource(byteArray); | ||
|
||
File notificationPdf = | ||
documentToPdfConverter.convertHtmlDocResourceToPdf(resource); | ||
|
||
ByteArrayResource byteArrayResource = getByteArrayResource( | ||
notificationPdf, | ||
notificationReference + ".PDF" | ||
); | ||
|
||
return documentUploader.upload(byteArrayResource, PDF_CONTENT_TYPE); | ||
} | ||
|
||
private ByteArrayResource getByteArrayResource(File notificationPdf, String filename) { | ||
|
||
byte[] byteArray; | ||
|
||
try { | ||
byteArray = FileUtils.readFileToByteArray(notificationPdf); | ||
|
||
} catch (IOException e) { | ||
throw new IllegalStateException("Error reading converted pdf"); | ||
} | ||
|
||
return new ByteArrayResource(byteArray) { | ||
@Override | ||
public String getFilename() { | ||
return filename; | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...t/java/uk/gov/hmcts/reform/iacasedocumentsapi/domain/entities/StoredNotificationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import uk.gov.hmcts.reform.iacasedocumentsapi.domain.entities.ccd.field.Document; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
import static org.mockito.Mockito.mock; | ||
|
||
class StoredNotificationTest { | ||
|
||
private final String notificationId = "someId"; | ||
private final String notificationDateSent = "someDateSent"; | ||
private final String notificationSentTo = "someSentTo"; | ||
private final String notificationMethod = "someMethod"; | ||
private final String notificationStatus = "someStatus"; | ||
private final String notificationBody = "someBody"; | ||
private final String notificationReference = "someReference"; | ||
private final String notificationSubject = "someSubject"; | ||
private final Document document = mock(Document.class); | ||
|
||
private StoredNotification storedNotification; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
storedNotification = | ||
StoredNotification.builder() | ||
.notificationId(notificationId) | ||
.notificationDateSent(notificationDateSent) | ||
.notificationSentTo(notificationSentTo) | ||
.notificationBody(notificationBody) | ||
.notificationMethod(notificationMethod) | ||
.notificationStatus(notificationStatus) | ||
.notificationReference(notificationReference) | ||
.notificationSubject(notificationSubject) | ||
.build(); | ||
} | ||
|
||
@Test | ||
void should_hold_onto_values() { | ||
assertThat(storedNotification.getNotificationId()).isEqualTo(notificationId); | ||
assertThat(storedNotification.getNotificationMethod()).isEqualTo(notificationMethod); | ||
assertThat(storedNotification.getNotificationSentTo()).isEqualTo(notificationSentTo); | ||
assertThat(storedNotification.getNotificationStatus()).isEqualTo(notificationStatus); | ||
assertThat(storedNotification.getNotificationBody()).isEqualTo(notificationBody); | ||
assertThat(storedNotification.getNotificationDateSent()).isEqualTo(notificationDateSent); | ||
assertThat(storedNotification.getNotificationReference()).isEqualTo(notificationReference); | ||
assertThat(storedNotification.getNotificationSubject()).isEqualTo(notificationSubject); | ||
assertThat(storedNotification.getNotificationDocument()).isNull(); | ||
storedNotification.setNotificationDocument(document); | ||
assertThat(storedNotification.getNotificationDocument()).isEqualTo(document); | ||
} | ||
|
||
@Test | ||
void should_not_allow_null_arguments_other_than_document() { | ||
|
||
StoredNotification.StoredNotificationBuilder builder = StoredNotification.builder(); | ||
assertThatThrownBy(() -> builder.notificationId(null)).isExactlyInstanceOf(NullPointerException.class); | ||
assertThatThrownBy(() -> builder.notificationDateSent(null)).isExactlyInstanceOf(NullPointerException.class); | ||
assertThatThrownBy(() -> builder.notificationSentTo(null)).isExactlyInstanceOf(NullPointerException.class); | ||
assertThatThrownBy(() -> builder.notificationBody(null)).isExactlyInstanceOf(NullPointerException.class); | ||
assertThatThrownBy(() -> builder.notificationMethod(null)).isExactlyInstanceOf(NullPointerException.class); | ||
assertThatThrownBy(() -> builder.notificationStatus(null)).isExactlyInstanceOf(NullPointerException.class); | ||
assertThatThrownBy(() -> builder.notificationReference(null)).isExactlyInstanceOf(NullPointerException.class); | ||
assertThatThrownBy(() -> builder.notificationSubject(null)).isExactlyInstanceOf(NullPointerException.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.