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

Verify PDF mapping in journey tests #592

Merged
merged 2 commits into from
Feb 21, 2024
Merged
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
4 changes: 4 additions & 0 deletions src/main/resources/templates/fragments/toolbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,9 @@
<th:block th:replace="~{fragments/localeSelect :: translate_noscript}"></th:block>
</div>
</header>
<th:block th:if="${#arrays.contains(@environment.getActiveProfiles(), 'test')}">
<br/>Download links (demo-only):
<a class="link--subtle" target="_blank" rel="noopener noreferrer" th:if="${submission != null}" th:href="'/download/laDigitalAssister/' + ${submission.getId()}">SNAP Applicant PDF</a>
</th:block>
<th:block th:replace="~{fragments/demoBanner :: demoBanner(demoText=#{demo.banner-text}, demoProfile=${@environment.getProperty('demo')})}"/>
</th:block>
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package org.ladocuploader.app.journeys;

import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.PdfReader;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.ladocuploader.app.testutils.AbstractBasePageTest;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -44,7 +49,6 @@

testPage.clickContinue();
assertThat(testPage.getTitle()).isEqualTo(message("home-address.title"));

}

@Test
Expand Down Expand Up @@ -165,7 +169,7 @@

assertThat(testPage.getTitle()).isEqualTo(message("special-situations.title"));
}

@Test
void expeditedSnapFlow() {
loadUserPersonalData();
Expand Down Expand Up @@ -226,7 +230,7 @@
List<WebElement> ethnicityInputs = driver.findElements(By.cssSelector("input[id*='householdMemberEthnicity_wildcard_']"));

ethnicityInputs.stream()
.filter(ei -> ei.getAttribute("value").equals("Hispanic or Latino"))
.filter(ei -> ei.getAttribute("value").equals("Hispanic or Latino"))
.forEach(ei -> {
ei.click();
// make sure found them, even with the site language being in Vietnamese
Expand Down Expand Up @@ -256,9 +260,9 @@

// choose a few for each
raceInputs.stream()
.filter(ri -> {
.filter(ri -> {
String value = ri.getAttribute("value");
return value.equals("Alaskan Native") || value.equals("Black or African American");
return value.equals("Alaskan Native") || value.equals("Black or African American");
})
.forEach(ri -> {
ri.click();
Expand Down Expand Up @@ -744,7 +748,7 @@

assertThat(testPage.getTitle()).isEqualTo(message("elderlycare-amount.title"));
testPage.enter("expensesElderlyCare", "123");
testPage.clickContinue();;
testPage.clickContinue();

var title = testPage.getTitle();
if ("ECE link".equals(title)) {
Expand Down Expand Up @@ -884,8 +888,28 @@

// Confirmation page
assertThat(testPage.getTitle()).isEqualTo(message("confirmation.title"));

verifyPDF();
}

private void verifyPDF() {
testPage.clickLink("SNAP Applicant PDF");
await().until(pdfDownloadCompletes());
File pdfFile = path.toFile().listFiles()[0];
try (PdfReader actualReader = new PdfReader(new FileInputStream(pdfFile));

Check warning

Code scanning / CodeQL

Potential input resource leak Warning test

This FileInputStream is not always closed on method exit.
PdfReader expectedReader = new PdfReader(new FileInputStream("src/test/resources/output/test_la_application_for_assistance.pdf"))) {

Check warning

Code scanning / CodeQL

Potential input resource leak Warning test

This FileInputStream is not always closed on method exit.
AcroFields actualAcroFields = actualReader.getAcroFields();
AcroFields expectedAcroFields = expectedReader.getAcroFields();

assertThat(actualAcroFields.getAllFields().size()).isEqualTo(expectedAcroFields.getAllFields().size());
for (String expectedField : expectedAcroFields.getAllFields().keySet()) {
assertThat(actualAcroFields.getField(expectedField)).isEqualTo(expectedAcroFields.getField(expectedField));
}
} catch (IOException e) {
log.error("Failed to generate PDF: %s", e);
throw new RuntimeException(e);
}
}

void loadUserPersonalData() {
testPage.navigateToFlowScreen("laDigitalAssister/parish");
Expand Down Expand Up @@ -914,7 +938,7 @@
testPage.selectRadio("householdMemberSex", "F");
testPage.clickContinue();
}

void loadAddressData() {
testPage.navigateToFlowScreen("laDigitalAssister/homeAddress");
testPage.enter("homeAddressStreetAddress1", "123 Test St");
Expand All @@ -925,7 +949,7 @@
testPage.clickElementById("sameAsHomeAddress-true");
testPage.clickContinue();
}

void loadContactData() {
testPage.navigateToFlowScreen("laDigitalAssister/contactInfo");
testPage.enter("emailAddress", "test@gmail.com");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
package org.ladocuploader.app.testutils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

import org.junit.jupiter.api.BeforeEach;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
Expand All @@ -20,10 +9,25 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.context.MessageSource;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.Callable;

import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;

@SpringBootTest(webEnvironment = RANDOM_PORT)
@Import({WebDriverConfiguration.class})
Expand Down Expand Up @@ -96,4 +100,14 @@ public String message(String message) {
return messageSource
.getMessage(message, null, Locale.ENGLISH);
}

protected List<File> getAllFiles() {
return Arrays.stream(Objects.requireNonNull(path.toFile().listFiles()))
.filter(file -> file.getName().endsWith(".pdf"))
.toList();
}

protected Callable<Boolean> pdfDownloadCompletes() {
return () -> getAllFiles().size() > 0;
}
}
2 changes: 2 additions & 0 deletions src/test/resources/application-test.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
form-flow:
pdf:
generate-flattened: false
disabled-flows: ~
uploads:
default-doc-type: "NotSet"
Expand Down
Binary file not shown.