View Javadoc
1   package de.aikiit.bilanzanalyser.reader;
2   
3   import de.aikiit.bilanzanalyser.entity.BilanzRow;
4   import org.junit.jupiter.api.Test;
5   import org.odftoolkit.odfdom.doc.OdfDocument;
6   import org.odftoolkit.odfdom.doc.OdfSpreadsheetDocument;
7   import org.odftoolkit.odfdom.doc.table.OdfTable;
8   import org.odftoolkit.odfdom.doc.table.OdfTableRow;
9   
10  import java.math.BigDecimal;
11  import java.time.LocalDate;
12  
13  import static de.aikiit.bilanzanalyser.reader.BilanzRowParser.cleanUpAmount;
14  import static de.aikiit.bilanzanalyser.reader.BilanzRowParser.fromOdfTableRow;
15  import static org.assertj.core.api.Assertions.assertThat;
16  
17  class BilanzRowParserTest {
18  
19      private static OdfTableRow createExampleRow(String dateValue, boolean withAmount) throws Exception {
20          try (OdfDocument document = OdfSpreadsheetDocument.newSpreadsheetDocument()) {
21              OdfTable t = OdfTable.newTable(document, 5, 6);
22              t.setTableName("Ausgaben");
23              assertThat(t.getRowCount()).isEqualTo(5);
24  
25              OdfTableRow newRow = t.getRowByIndex(0);
26              assertThat(newRow.getCellCount()).isEqualTo(6);
27  
28              for (int i = 0; i < newRow.getCellCount(); i++) {
29                  assertThat(newRow.getCellByIndex(0)).isNotNull();
30              }
31  
32              newRow.getCellByIndex(0).setStringValue(dateValue);
33              if (withAmount) {
34                  newRow.getCellByIndex(1).setStringValue("12.000,34 €");
35              } else {
36                  newRow.getCellByIndex(1).setStringValue("0,00 €");
37              }
38              newRow.getCellByIndex(2).setStringValue("Just a test row");
39              newRow.getCellByIndex(3).setStringValue("ReWe");
40              newRow.getCellByIndex(4).setStringValue("EC");
41              newRow.getCellByIndex(5).setStringValue("Lebensmittel");
42              return newRow;
43          }
44      }
45  
46      @Test
47      void fromOdfTableRowNPESafe() throws Exception {
48          assertThat(fromOdfTableRow(null)).isEmpty();
49          assertThat(fromOdfTableRow(createExampleRow("2025-10-02", true))).isPresent().contains(BilanzRow.builder() //
50                  .date(LocalDate.parse("2025-10-02")) //
51                  .amount(new BigDecimal("12000.34")) //
52                  .description("Just a test row") //
53                  .shop("ReWe").payment("EC").category("Lebensmittel").build());
54      }
55  
56      @Test
57      void fromOdfTableRowWithOutAmountIsSkipped() throws Exception {
58          assertThat(fromOdfTableRow(createExampleRow("1969-07-02", false))).isEmpty();
59      }
60  
61      @Test
62      void fromOdfTableRowSpecialDateHandlingMappedToDefault() throws Exception {
63          for (String date : new String[]{null, "?", " ?  ", " "}) {
64              assertThat(fromOdfTableRow(createExampleRow(date, true))).isPresent().contains(BilanzRow.builder() //
65                      .date(BilanzRow.FALLBACK_DATE) //
66                      .amount(new BigDecimal("12000.34")) //
67                      .description("Just a test row") //
68                      .shop("ReWe").payment("EC").category("Lebensmittel").build());
69          }
70      }
71  
72      @Test
73      void cleanUpAmountParsing() {
74          assertThat(cleanUpAmount(null)).isNull();
75          assertThat(cleanUpAmount("")).isEqualTo("");
76          assertThat(cleanUpAmount(" ")).isEqualTo("");
77          assertThat(cleanUpAmount("    €    ")).isEqualTo("");
78          assertThat(cleanUpAmount("   11.356,324 €    ")).isEqualTo("11.356,324");
79      }
80  }