-
Notifications
You must be signed in to change notification settings - Fork 0
add assertion helpers #5
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
Closed
SirCotare
wants to merge
1
commit into
main
from
ab-299-from-aichner-to-boilerplate-assertion-helpers
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
68 changes: 68 additions & 0 deletions
68
src/main/java/it/aboutbits/springboot/testing/assertion/document/DocxAssert.java
This file contains hidden or 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,68 @@ | ||
| package it.aboutbits.springboot.testing.assertion.document; | ||
|
|
||
| import lombok.SneakyThrows; | ||
| import org.apache.poi.xwpf.usermodel.XWPFDocument; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| public final class DocxAssert { | ||
| private final XWPFDocument document; | ||
|
|
||
| private DocxAssert(XWPFDocument document) { | ||
| this.document = document; | ||
| } | ||
|
|
||
| @SneakyThrows(IOException.class) | ||
| public static DocxAssert assertThatDocx(InputStream inputStream) { | ||
| assertThat(inputStream).isNotNull(); | ||
|
|
||
| var document = new XWPFDocument(inputStream); | ||
|
|
||
| return new DocxAssert(document); | ||
| } | ||
|
|
||
| public static DocxAssert assertThatDocx(ByteArrayOutputStream outputStream) { | ||
| assertThat(outputStream).isNotNull(); | ||
|
|
||
| return assertThatDocx(new ByteArrayInputStream(outputStream.toByteArray())); | ||
| } | ||
|
|
||
| public static DocxAssert assertThatDocx(byte[] bytea) { | ||
| assertThat(bytea).isNotNull(); | ||
|
|
||
| return assertThatDocx(new ByteArrayInputStream(bytea)); | ||
| } | ||
|
|
||
| public DocxAssert hasContent() { | ||
| assertThat(getTextContent()).isNotBlank(); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public DocxAssert containsText(String text) { | ||
| assertThat(text).isNotNull(); | ||
|
|
||
| assertThat(getTextContent()).contains(text); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| private String getTextContent() { | ||
| var sb = new StringBuilder(); | ||
| var paragraphs = document.getParagraphs(); | ||
|
|
||
| for (var paragraph : paragraphs) { | ||
| var runs = paragraph.getRuns(); | ||
| for (var run : runs) { | ||
| sb.append(run.getText(0)).append(" "); | ||
| } | ||
| } | ||
|
|
||
| return sb.toString().trim(); // Remove leading/trailing whitespaces | ||
| } | ||
| } |
83 changes: 83 additions & 0 deletions
83
src/main/java/it/aboutbits/springboot/testing/assertion/document/PdfAssert.java
This file contains hidden or 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,83 @@ | ||
| package it.aboutbits.springboot.testing.assertion.document; | ||
|
|
||
| import lombok.SneakyThrows; | ||
| import org.apache.pdfbox.Loader; | ||
| import org.apache.pdfbox.pdmodel.PDDocument; | ||
| import org.apache.pdfbox.text.PDFTextStripper; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| public final class PdfAssert { | ||
| private final PDDocument pdDocument; | ||
|
|
||
| private String textContent = null; | ||
|
|
||
| private PdfAssert(PDDocument pdDocument) { | ||
| this.pdDocument = pdDocument; | ||
| } | ||
|
|
||
| @SneakyThrows(IOException.class) | ||
| public static PdfAssert assertThatPdf(InputStream inputStream) { | ||
| assertThat(inputStream).isNotNull(); | ||
|
|
||
| var document = Loader.loadPDF(inputStream.readAllBytes()); | ||
|
|
||
| return new PdfAssert(document); | ||
| } | ||
|
|
||
| public static PdfAssert assertThatPdf(ByteArrayOutputStream outputStream) { | ||
| assertThat(outputStream).isNotNull(); | ||
|
|
||
| return assertThatPdf(new ByteArrayInputStream(outputStream.toByteArray())); | ||
| } | ||
|
|
||
| public static PdfAssert assertThatPdf(byte[] bytea) { | ||
| assertThat(bytea).isNotNull(); | ||
|
|
||
| return assertThatPdf(new ByteArrayInputStream(bytea)); | ||
| } | ||
|
|
||
| public PdfAssert hasNumberOfPages(int expectedNumberOfPages) { | ||
| assertThat(pdDocument.getNumberOfPages()).isEqualTo(expectedNumberOfPages); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public PdfAssert hasContent() { | ||
| assertThat(getTextContent()).isNotBlank(); | ||
| assertThat(pdDocument.getNumberOfPages()).isNotZero(); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public PdfAssert containsText(String text) { | ||
| assertThat(text).isNotNull(); | ||
|
|
||
| assertThat(getTextContent()).contains(cleanTextForComparison(text)); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| @SneakyThrows(IOException.class) | ||
| private String getTextContent() { | ||
| if (textContent == null) { | ||
| var textStripper = new PDFTextStripper(); | ||
| textStripper.setStartPage(0); | ||
| textStripper.setEndPage(pdDocument.getNumberOfPages()); | ||
|
|
||
| textContent = textStripper.getText(pdDocument); | ||
| } | ||
|
|
||
| return cleanTextForComparison(textContent); | ||
| } | ||
|
|
||
| // ignore line breaks, and replace multiple following spaces with a single space character | ||
| private String cleanTextForComparison(String text) { | ||
| return text.replace("\\R", "").replaceAll("\\s", "").trim(); | ||
| } | ||
| } |
99 changes: 99 additions & 0 deletions
99
src/main/java/it/aboutbits/springboot/testing/assertion/document/XlsxAssert.java
This file contains hidden or 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,99 @@ | ||
| package it.aboutbits.springboot.testing.assertion.document; | ||
|
|
||
| import lombok.SneakyThrows; | ||
| import org.apache.poi.xssf.usermodel.XSSFWorkbook; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| public final class XlsxAssert { | ||
| private final XSSFWorkbook workbook; | ||
|
|
||
| private String textContent = null; | ||
|
|
||
| private XlsxAssert(XSSFWorkbook workbook) { | ||
| this.workbook = workbook; | ||
| } | ||
|
|
||
| @SneakyThrows(IOException.class) | ||
| public static XlsxAssert assertThatXlsx(InputStream inputStream) { | ||
| assertThat(inputStream).isNotNull(); | ||
|
|
||
| var workbook = new XSSFWorkbook(inputStream); | ||
|
|
||
| return new XlsxAssert(workbook); | ||
| } | ||
|
|
||
| public static XlsxAssert assertThatXlsx(ByteArrayOutputStream outputStream) { | ||
| assertThat(outputStream).isNotNull(); | ||
|
|
||
| return assertThatXlsx(new ByteArrayInputStream(outputStream.toByteArray())); | ||
| } | ||
|
|
||
| public static XlsxAssert assertThatXlsx(byte[] bytea) { | ||
| assertThat(bytea).isNotNull(); | ||
|
|
||
| return assertThatXlsx(new ByteArrayInputStream(bytea)); | ||
| } | ||
|
|
||
| public XlsxAssert hasNumberOfSheets(int expectedNumberOfPages) { | ||
| assertThat(workbook.getNumberOfSheets()).isEqualTo(expectedNumberOfPages); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public XlsxAssert hasContent() { | ||
| assertThat(getTextContent()).isNotBlank(); | ||
| assertThat(workbook.getNumberOfSheets()).isNotZero(); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public XlsxAssert containsText(String text) { | ||
| assertThat(text).isNotNull(); | ||
|
|
||
| assertThat(getTextContent()).contains(text); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| private String getTextContent() { | ||
| if (textContent == null) { | ||
| var allContent = new StringBuilder(); | ||
|
|
||
| for (int i = 0; i < workbook.getNumberOfSheets(); i++) { | ||
| var sheet = workbook.getSheetAt(i); | ||
|
|
||
| for (var row : sheet) { | ||
| for (var cell : row) { | ||
| switch (cell.getCellType()) { | ||
| case STRING: | ||
| allContent.append(cell.getStringCellValue()).append("\t"); | ||
| break; | ||
| case NUMERIC: | ||
| allContent.append(cell.getNumericCellValue()).append("\t"); | ||
| break; | ||
| case BOOLEAN: | ||
| allContent.append(cell.getBooleanCellValue()).append("\t"); | ||
| break; | ||
| case FORMULA: | ||
| allContent.append(cell.getCellFormula()).append("\t"); | ||
| break; | ||
| default: | ||
| allContent.append("\t"); | ||
| } | ||
| } | ||
| allContent.append("\n"); | ||
| } | ||
| } | ||
|
|
||
| textContent = allContent.toString(); | ||
| } | ||
|
|
||
| return textContent; | ||
| } | ||
| } |
2 changes: 1 addition & 1 deletion
2
...esting/persistance/PersistenceAssert.java → ...ertion/persistance/PersistenceAssert.java
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really want to add those dependencies to the project? We might not have PDF and Docx / Xlsx in all our projects.
@alexlanz @Piiit Wdyt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally would not add it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It only adds those dependencies to the
testcode because this library gets imported with scope test. Flying Saucer is unnecessary indeed. I will remove it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PDF is quite common, maybe that is used in most projects, and could therefore be added, would not add the other two... nevertheless, we could have separate libs to handle such files, and those libs could then have testing tools.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I'll consider this PR to be rejected and close it then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about that, would add the PDF assert in the meantime, until we have a dedicated lib... wdyt?