Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sscssi 448 code coverage #4307

Open
wants to merge 6 commits into
base: SSCSSI-437-merge-pdf-email-common
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,6 @@ public SscsDocument storeDocument(UpdateDocParams updateDocParams) {
}
}

public SscsDocument storeSecureDocStore(byte[] content, String fileName, String documentType, SscsDocumentTranslationStatus documentTranslationStatus) {
return storeSecureDocStore(UpdateDocParams.builder()
.pdf(content)
.fileName(fileName)
.documentType(documentType)
.documentTranslationStatus(documentTranslationStatus)
.build());
}

public SscsDocument storeSecureDocStore(UpdateDocParams updateDocParams) {
ByteArrayMultipartFile file = ByteArrayMultipartFile.builder().content(updateDocParams.getPdf()).name(updateDocParams.getFileName())
.contentType(APPLICATION_PDF).build();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uk.gov.hmcts.reform.sscs.service;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
Expand All @@ -9,6 +10,7 @@
import static org.mockito.MockitoAnnotations.openMocks;
import static uk.gov.hmcts.reform.sscs.ccd.util.CaseDataUtils.buildCaseData;

import com.google.common.collect.Lists;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -105,6 +107,29 @@ public void mergeCorrespondenceIntoCcd() {
verify(ccdService).updateCaseWithoutRetry(any(), any(), any(), eq("Notification sent"), eq("Notification sent via Gov Notify"), any());
}

@Test
public void mergeCorrespondenceIntoCcdWhenThrowException() {

when(ccdService.updateCaseWithoutRetry(any(), any(), any(), any(), any(), any())).thenThrow(CcdException.class);
Correspondence correspondence = Correspondence.builder().value(
CorrespondenceDetails.builder()
.sentOn("22 Jan 2021 11:00")
.from("from")
.to("to")
.body("the body")
.subject("a subject")
.eventType("event")
.correspondenceType(CorrespondenceType.Email)
.build()).build();

SscsCaseData expectedCaseData = service.mergeCorrespondenceIntoCcd(caseData, correspondence);

assertNull(expectedCaseData);
verify(pdfServiceClient).generateFromHtml(any(), any());
verify(pdfStoreService).store(any(), eq("event 22 Jan 2021 11:00.pdf"), eq(CorrespondenceType.Email.name()));
verify(ccdService).updateCaseWithoutRetry(any(), any(), any(), eq("Notification sent"), eq("Notification sent via Gov Notify"), any());
}

@Test
public void shouldSuccessfullyMergeCorrespondenceIntoCcdV2() {
Long caseId = Long.valueOf(caseData.getCcdCaseId());
Expand All @@ -123,7 +148,13 @@ public void shouldSuccessfullyMergeCorrespondenceIntoCcdV2() {

verify(pdfServiceClient).generateFromHtml(any(), any());
verify(pdfStoreService).store(any(), eq("event 22 Jan 2021 11:00.pdf"), eq(CorrespondenceType.Email.name()));
verify(updateCcdCaseService).updateCaseV2(eq(caseId), eq(EventType.NOTIFICATION_SENT.getCcdType()), eq("Notification sent"), eq("Notification sent via Gov Notify"), any(), any(Consumer.class));
verify(updateCcdCaseService).updateCaseV2(eq(caseId), eq(EventType.NOTIFICATION_SENT.getCcdType()), eq("Notification sent"), eq("Notification sent via Gov Notify"), any(), caseDetailsConsumerArgumentCaptor.capture());
caseData.setCorrespondence(Lists.newArrayList(correspondence));
var sscsCaseDetails = SscsCaseDetails.builder().data(caseData).build();
caseDetailsConsumerArgumentCaptor.getValue().accept(sscsCaseDetails);

Correspondence result = sscsCaseDetails.getData().getCorrespondence().get(0);
assertEquals(CorrespondenceType.Email, result.getValue().getCorrespondenceType());
}

@Test
Expand Down Expand Up @@ -310,6 +341,38 @@ public void givenAReasonableAdjustmentPdfForALetterTypeWithExistingReasonableAdj
verifyReasonableAdjustmentsCorrespondenceUpdated();
}

@Test
public void givenAReasonableAdjustmentPdfForALetterTypeWithExistingReasonableAdjustments_whenThrowException() {
DocumentLink documentLink = DocumentLink.builder().documentUrl("Http://document").documentFilename("evidence-document.pdf").build();

when(pdfStoreService.store(any(), any(), eq(CorrespondenceType.Letter.name()))).thenReturn(sscsDocuments);
List<Correspondence> existingCorrespondence = new ArrayList<>();
existingCorrespondence.add(Correspondence.builder().value(CorrespondenceDetails.builder().sentOn("22 Oct 2020 11:33").documentLink(DocumentLink.builder().documentUrl("Testurl").build()).build()).build());
caseData.setReasonableAdjustmentsLetters(ReasonableAdjustmentsLetters.builder().appellant(existingCorrespondence).build());

Long caseId = Long.valueOf(caseData.getCcdCaseId());
when(updateCcdCaseService.updateCaseV2WithoutRetry(any(), any(), eq("Notification sent"), any(), any(), any(Consumer.class)))
.thenThrow(CcdException.class);
byte[] bytes = "String".getBytes();
Pdf pdf = new Pdf(bytes, "adocument");
List<Pdf> pdfs = Collections.singletonList(pdf);
Correspondence correspondence = Correspondence.builder().value(
CorrespondenceDetails.builder()
.sentOn("22 Jan 2021 11:33")
.from("from")
.to("to")
.subject("a subject")
.eventType("event")
.documentLink(documentLink)
.correspondenceType(CorrespondenceType.Letter)
.reasonableAdjustmentStatus(ReasonableAdjustmentStatus.REQUIRED)
.build()).build();

SscsCaseData expectedCaseData = service.mergeReasonableAdjustmentsCorrespondenceIntoCcd(pdfs, caseId, correspondence, LetterType.APPELLANT);
assertNull(expectedCaseData);
verifyReasonableAdjustmentsCorrespondenceUpdated();
}

@Test
@Parameters({"APPELLANT", "REPRESENTATIVE", "APPOINTEE", "JOINT_PARTY", "OTHER_PARTY"})
public void givenAReasonableAdjustmentBytesForALetterType_thenCreateReasonableAdjustmentsCorrespondenceIntoCcdForRelevantParty(LetterType letterType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.Matchers.samePropertyValuesAs;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
Expand All @@ -16,6 +17,7 @@
import static org.mockito.MockitoAnnotations.openMocks;
import static uk.gov.hmcts.reform.sscs.ccd.domain.EventType.UPLOAD_DOCUMENT;
import static uk.gov.hmcts.reform.sscs.ccd.domain.YesNo.NO;
import static uk.gov.hmcts.reform.sscs.ccd.util.CaseDataUtils.YES;
import static uk.gov.hmcts.reform.sscs.ccd.util.CaseDataUtils.buildCaseData;

import java.time.LocalDateTime;
Expand All @@ -38,13 +40,17 @@
import uk.gov.hmcts.reform.sscs.ccd.domain.SscsCaseDetails;
import uk.gov.hmcts.reform.sscs.ccd.domain.SscsDocument;
import uk.gov.hmcts.reform.sscs.ccd.domain.SscsDocumentDetails;
import uk.gov.hmcts.reform.sscs.ccd.domain.SscsDocumentTranslationStatus;
import uk.gov.hmcts.reform.sscs.ccd.exception.CcdException;
import uk.gov.hmcts.reform.sscs.ccd.service.CcdService;
import uk.gov.hmcts.reform.sscs.domain.UpdateDocParams;
import uk.gov.hmcts.reform.sscs.idam.IdamTokens;

@RunWith(JUnitParamsRunner.class)
public class CcdPdfServiceTest {

private static final String UPLOADED_DOCUMENT_INTO_SSCS = "Uploaded document into SSCS";

@InjectMocks
private CcdPdfService service;

Expand Down Expand Up @@ -214,4 +220,64 @@ public void mergeValidPdfAndStoreInDocumentStoreWithDescription() {
verify(pdfStoreService).storeDocument(params);
verify(ccdService).updateCase(any(), any(), any(), eq("SSCS - upload document event"), eq("My description"), any());
}

@Test
public void mergeValidPdfAndStoreInDocumentStoreWithoutDescription() {
byte[] pdf = {};
when(ccdService.updateCase(any(), any(), any(), any(), any(), any())).thenReturn(SscsCaseDetails.builder().data(caseData).build());

service.mergeDocIntoCcd("Myfile.pdf", pdf, 1L, caseData, IdamTokens.builder().build());

UpdateDocParams params = UpdateDocParams.builder().fileName("Myfile.pdf").pdf(pdf).caseId(1L).caseData(caseData).build();
verify(pdfStoreService).storeDocument(params);
verify(ccdService).updateCase(any(), any(), any(), eq("SSCS - upload document event"), eq(UPLOADED_DOCUMENT_INTO_SSCS), any());
}

@Test
public void mergeValidPdfAndStoreInDocumentStoreWithoutDescriptionAndUsingUpdateDocParams() {
byte[] pdf = {};
UpdateDocParams params = UpdateDocParams.builder().fileName("Myfile.pdf").pdf(pdf).caseId(1L).caseData(caseData).build();
when(ccdService.updateCase(any(), any(), any(), any(), any(), any())).thenReturn(SscsCaseDetails.builder().data(caseData).build());

service.mergeDocIntoCcd(params, IdamTokens.builder().build());

verify(pdfStoreService).storeDocument(params);
verify(ccdService).updateCase(any(), any(), any(), eq("SSCS - upload document event"), eq(UPLOADED_DOCUMENT_INTO_SSCS), any());
}

@Test
public void mergeValidPdfAndNotStoreWhenException() {
byte[] pdf = {};
UpdateDocParams params = UpdateDocParams.builder().fileName("Myfile.pdf").pdf(pdf).caseId(1L).caseData(caseData).build();
when(ccdService.updateCase(any(), any(), any(), any(), any(), any())).thenThrow(CcdException.class);

SscsCaseData expectedCaseData = service.mergeDocIntoCcd(params, IdamTokens.builder().build());

assertNull(expectedCaseData);
verify(pdfStoreService).storeDocument(params);
verify(ccdService).updateCase(any(), any(), any(), eq("SSCS - upload document event"), eq(UPLOADED_DOCUMENT_INTO_SSCS), any());
}

@Test
public void updateDocWhenUpdateDocParamsCaseIdIsNull() {
byte[] pdf = {};
UpdateDocParams params = UpdateDocParams.builder().fileName("Myfile.pdf").pdf(pdf).caseData(caseData).build();

SscsCaseData expectedCaseData = service.updateDoc(params);

assertThat(expectedCaseData, is(caseData));
verify(pdfStoreService).storeDocument(params);
}

@Test
public void updateDocWhenUpdateDocParamsTranslationStatusIsRequired() {
byte[] pdf = {};
UpdateDocParams params = UpdateDocParams.builder().fileName("Myfile.pdf").caseId(1L).documentTranslationStatus(SscsDocumentTranslationStatus.TRANSLATION_REQUIRED).pdf(pdf).caseData(caseData).build();

SscsCaseData expectedCaseData = service.updateDoc(params);

assertThat(expectedCaseData.getTranslationWorkOutstanding(), is(YES));
assertThat(expectedCaseData, is(caseData));
verify(pdfStoreService).storeDocument(params);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
import static java.util.Collections.singletonList;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
import static org.springframework.http.MediaType.APPLICATION_PDF;

import feign.FeignException;
import java.net.URI;
import java.util.Collections;
import java.util.List;
import org.junit.Test;
Expand All @@ -16,6 +22,7 @@
import uk.gov.hmcts.reform.document.domain.UploadResponse;
import uk.gov.hmcts.reform.sscs.ccd.domain.SscsDocument;
import uk.gov.hmcts.reform.sscs.ccd.domain.SscsDocumentDetails;
import uk.gov.hmcts.reform.sscs.ccd.domain.SscsDocumentTranslationStatus;
import uk.gov.hmcts.reform.sscs.domain.pdf.ByteArrayMultipartFile;
import uk.gov.hmcts.reform.sscs.idam.IdamService;
import uk.gov.hmcts.reform.sscs.idam.IdamTokens;
Expand Down Expand Up @@ -65,6 +72,14 @@ public void cannotConnectToDocumentStore() {
assertThat(documents.size(), is(0));
}

@Test
public void cannotConnectToDocumentStoreWithDocumentTranslationStatus() {
when(evidenceManagementService.upload(files, SSCS_USER)).thenThrow(new RestClientException("Cannot connect"));
List<SscsDocument> documents = pdfStoreService.store(content, filename, "appellantEvidence", SscsDocumentTranslationStatus.TRANSLATION_COMPLETE);

assertThat(documents.size(), is(0));
}

@Test
public void uploadsPdfAndExtractsLinkForSecureDocStore() {
uk.gov.hmcts.reform.ccd.document.am.model.UploadResponse uploadResponse = createUploadResponseSecureDocStore();
Expand All @@ -80,6 +95,67 @@ public void uploadsPdfAndExtractsLinkForSecureDocStore() {
assertThat(value.getDocumentLink().getDocumentUrl(), is(expectedHref));
}

@Test
public void uploadsPdfAndExtractsLinkForSecureDocStoreWithDocumentTranslationStatus() {
uk.gov.hmcts.reform.ccd.document.am.model.UploadResponse uploadResponse = createUploadResponseSecureDocStore();
IdamTokens idamTokens = IdamTokens.builder().idamOauth2Token("idamOauth2Token").build();
when(idamService.getIdamTokens()).thenReturn(idamTokens);
when(evidenceManagementSecureDocStoreService.upload(files, idamTokens)).thenReturn(uploadResponse);

List<SscsDocument> documents = pdfStoreSecureDocStore.store(content, filename, "appellantEvidence", SscsDocumentTranslationStatus.TRANSLATION_COMPLETE);

assertThat(documents.size(), is(1));
SscsDocumentDetails value = documents.get(0).getValue();
assertThat(value.getDocumentFileName(), is(filename));
assertThat(value.getDocumentLink().getDocumentUrl(), is(expectedHref));
}

@Test
public void uploadsPdfForSecureDocStoreWhenExceptionIsThrown() {
IdamTokens idamTokens = IdamTokens.builder().idamOauth2Token("idamOauth2Token").build();
when(idamService.getIdamTokens()).thenReturn(idamTokens);
when(evidenceManagementSecureDocStoreService.upload(files, idamTokens)).thenThrow(RestClientException.class);

List<SscsDocument> documents = pdfStoreSecureDocStore.store(content, filename, "appellantEvidence", SscsDocumentTranslationStatus.TRANSLATION_COMPLETE);

assertThat(documents.size(), is(0));
}

@Test
public void downloadPdfIfNotSecureDocStore() {
when(evidenceManagementService.download(any(URI.class), eq(SSCS_USER))).thenReturn(content);
byte[] expectedContent = pdfStoreService.download("http://test");

assertThat(expectedContent, is(content));
verify(evidenceManagementService).download(any(URI.class), eq(SSCS_USER));
verifyNoInteractions(evidenceManagementSecureDocStoreService);
}

@Test
public void downloadPdfSecureDocStore() {
IdamTokens idamTokens = IdamTokens.builder().idamOauth2Token("idamOauth2Token").build();
when(idamService.getIdamTokens()).thenReturn(idamTokens);
when(evidenceManagementSecureDocStoreService.download(any(), eq(idamTokens))).thenReturn(content);
byte[] expectedContent = pdfStoreSecureDocStore.download("http://test");

assertThat(expectedContent, is(content));
verify(evidenceManagementSecureDocStoreService).download(any(), eq(idamTokens));
verifyNoInteractions(evidenceManagementService);
}

@Test
public void downloadPdfSecureDocStoreWhenThrowException() {
IdamTokens idamTokens = IdamTokens.builder().idamOauth2Token("idamOauth2Token").build();
when(idamService.getIdamTokens()).thenReturn(idamTokens);
when(evidenceManagementService.download(any(URI.class), eq(SSCS_USER))).thenReturn(content);
when(evidenceManagementSecureDocStoreService.download(any(), eq(idamTokens))).thenThrow(FeignException.class);
byte[] expectedContent = pdfStoreSecureDocStore.download("http://test");

assertThat(expectedContent, is(content));
verify(evidenceManagementSecureDocStoreService).download(any(), eq(idamTokens));
verify(evidenceManagementService).download(any(URI.class), eq(SSCS_USER));
}

private uk.gov.hmcts.reform.ccd.document.am.model.UploadResponse createUploadResponseSecureDocStore() {
uk.gov.hmcts.reform.ccd.document.am.model.UploadResponse response = mock(uk.gov.hmcts.reform.ccd.document.am.model.UploadResponse.class);
uk.gov.hmcts.reform.ccd.document.am.model.Document document = createDocumentSecureDocStore();
Expand Down