Skip to content
Closed
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
19 changes: 19 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>

<!-- PDF -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf</artifactId>
<version>9.9.4</version>
</dependency>

<!-- DOCX / XLSX -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.3.0</version>
</dependency>
Comment on lines +47 to +64

Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Member Author

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 test code because this library gets imported with scope test. Flying Saucer is unnecessary indeed. I will remove it.

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Member Author

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.

Copy link
Copy Markdown
Member

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?

</dependencies>

<build>
Expand Down
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
}
}
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();
}
}
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;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package it.aboutbits.springboot.testing.persistance;
package it.aboutbits.springboot.testing.assertion.persistance;

import it.aboutbits.springboot.testing.spring.BeanAccessor;
import it.aboutbits.springboot.toolbox.persistence.ChangeAware;
Expand Down