From 09c5955415097b06102516dae1052d30b74277dc Mon Sep 17 00:00:00 2001 From: Jakub Stejskal Date: Wed, 7 Feb 2024 12:11:03 +0100 Subject: [PATCH] Add unit tests for the plugin (#15) Signed-off-by: Jakub Stejskal --- test-docs-generator-maven-plugin/pom.xml | 52 ++++++++++ .../main/java/io/skodjob/markdown/Table.java | 11 ++- .../java/io/skodjob/DocGeneratorTest.java | 83 ++++++++++++++++ .../java/io/skodjob/markdown/HeaderTest.java | 29 ++++++ .../java/io/skodjob/markdown/TableTest.java | 96 +++++++++++++++++++ .../io/skodjob/markdown/TextListTest.java | 63 ++++++++++++ .../src/test/resources/expected-docs.md | 51 ++++++++++ 7 files changed, 381 insertions(+), 4 deletions(-) create mode 100644 test-docs-generator-maven-plugin/src/test/java/io/skodjob/DocGeneratorTest.java create mode 100644 test-docs-generator-maven-plugin/src/test/java/io/skodjob/markdown/HeaderTest.java create mode 100644 test-docs-generator-maven-plugin/src/test/java/io/skodjob/markdown/TableTest.java create mode 100644 test-docs-generator-maven-plugin/src/test/java/io/skodjob/markdown/TextListTest.java create mode 100644 test-docs-generator-maven-plugin/src/test/resources/expected-docs.md diff --git a/test-docs-generator-maven-plugin/pom.xml b/test-docs-generator-maven-plugin/pom.xml index a13e866..fd90233 100644 --- a/test-docs-generator-maven-plugin/pom.xml +++ b/test-docs-generator-maven-plugin/pom.xml @@ -24,15 +24,57 @@ 3.9.3 3.9.0 2.2.1 + 2.2 + 5.10.2 + 1.10.2 + 3.1.2 false + + org.junit.jupiter + junit-jupiter-engine + ${junit.jupiter.version} + test + org.junit.jupiter junit-jupiter-api + ${junit.jupiter.version} + test + + + org.junit.jupiter + junit-jupiter-params + ${junit.jupiter.version} + test + + + org.junit.platform + junit-platform-commons + ${junit.platform.version} + test + + + org.junit.platform + junit-platform-launcher + ${junit.platform.version} + test + + + org.junit.platform + junit-platform-engine + ${junit.platform.version} + test + + + org.hamcrest + hamcrest + test + ${hamcrest.version} @@ -55,4 +97,14 @@ + + + + org.apache.maven.plugins + maven-surefire-plugin + true + ${maven.surefire.version} + + + diff --git a/test-docs-generator-maven-plugin/src/main/java/io/skodjob/markdown/Table.java b/test-docs-generator-maven-plugin/src/main/java/io/skodjob/markdown/Table.java index 9b4b016..ca1caf3 100644 --- a/test-docs-generator-maven-plugin/src/main/java/io/skodjob/markdown/Table.java +++ b/test-docs-generator-maven-plugin/src/main/java/io/skodjob/markdown/Table.java @@ -18,6 +18,9 @@ public class Table { * @return Markdown table in text format, returned as String */ public static String createTable(List headers, List rows) { + if (headers.isEmpty()) { + return ""; + } StringBuilder table = new StringBuilder(); table.append("|"); @@ -40,12 +43,12 @@ public static String createTable(List headers, List rows) { public static String createRow(String... content) { StringBuilder row = new StringBuilder(); + row.append("| "); for (String s : content) { - row.append("| ").append(s); - } - row.append(" |"); + row.append(s).append(" | "); + } - return row.toString(); + return row.substring(0, row.length() - 1); } } diff --git a/test-docs-generator-maven-plugin/src/test/java/io/skodjob/DocGeneratorTest.java b/test-docs-generator-maven-plugin/src/test/java/io/skodjob/DocGeneratorTest.java new file mode 100644 index 0000000..9129dd3 --- /dev/null +++ b/test-docs-generator-maven-plugin/src/test/java/io/skodjob/DocGeneratorTest.java @@ -0,0 +1,83 @@ +/* + * Copyright Skodjob authors. + * License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html). + */ +package io.skodjob; + +import io.skodjob.annotations.TestDoc; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Arrays; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class DocGeneratorTest { + @Test + void testCreateTableOfSteps() throws IOException { + String expectedFilePath = DocGeneratorTest.class.getClassLoader().getResource("expected-docs.md").getPath(); + String generatedFilePath = "target/io/test.md"; + DocGenerator.generate(TestClass.class, generatedFilePath); + + assertThat(compareFiles(expectedFilePath, generatedFilePath), is(true)); + } + + public static class TestClass { + + @TestDoc( + description = @TestDoc.Desc("Test checking that the application works as expected"), + steps = { + @TestDoc.Step(value = "Create object instance", expected = "Instance of an object is created"), + @TestDoc.Step(value = "Do a magic trick", expected = "Magic trick is done with success"), + @TestDoc.Step(value = "Clean up the test case", expected = "Everything is cleared") + }, + usecases = { + @TestDoc.Usecase(id = "core") + } + ) + void testMethodOne() { + + } + + @TestDoc( + description = @TestDoc.Desc("Test checking that the application works as expected. " + + "This is just a little bit longer line, nothing else."), + steps = { + @TestDoc.Step(value = "Create object instance", expected = "Instance of an object is created"), + @TestDoc.Step(value = "Do a magic trick", expected = "Magic trick is done with success"), + @TestDoc.Step(value = "Clean up the test case", expected = "Everything is cleared"), + @TestDoc.Step(value = "Do a magic cleanup check", expected = "Everything magically work") + }, + usecases = { + @TestDoc.Usecase(id = "core"), + @TestDoc.Usecase(id = "core+"), + @TestDoc.Usecase(id = "core+++") + } + ) + void testMethodTwo() { + + } + + @TestDoc( + description = @TestDoc.Desc("Test checking that the application works as expected. " + + "This is just a little bit longer line, nothing else."), + steps = { + }, + usecases = { + } + ) + void testMethodThree() { + + } + } + + public static boolean compareFiles(String filePath1, String filePath2) throws IOException { + byte[] file1Content = Files.readAllBytes(Paths.get(filePath1)); + byte[] file2Content = Files.readAllBytes(Paths.get(filePath2)); + + return Arrays.equals(file1Content, file2Content); + } +} diff --git a/test-docs-generator-maven-plugin/src/test/java/io/skodjob/markdown/HeaderTest.java b/test-docs-generator-maven-plugin/src/test/java/io/skodjob/markdown/HeaderTest.java new file mode 100644 index 0000000..8640309 --- /dev/null +++ b/test-docs-generator-maven-plugin/src/test/java/io/skodjob/markdown/HeaderTest.java @@ -0,0 +1,29 @@ +/* + * Copyright Skodjob authors. + * License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html). + */ +package io.skodjob.markdown; + +import org.junit.jupiter.api.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class HeaderTest { + + @Test + void testFirstLevelHeader() { + String text = "Something"; + String expectedHeader = "# Something"; + + assertThat(expectedHeader, is(Header.firstLevelHeader(text))); + } + + @Test + void testSecondLevelHeader() { + String text = "Something"; + String expectedHeader = "## Something"; + + assertThat(expectedHeader, is(Header.secondLevelHeader(text))); + } +} diff --git a/test-docs-generator-maven-plugin/src/test/java/io/skodjob/markdown/TableTest.java b/test-docs-generator-maven-plugin/src/test/java/io/skodjob/markdown/TableTest.java new file mode 100644 index 0000000..90fd3a0 --- /dev/null +++ b/test-docs-generator-maven-plugin/src/test/java/io/skodjob/markdown/TableTest.java @@ -0,0 +1,96 @@ +/* + * Copyright Skodjob authors. + * License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html). + */ +package io.skodjob.markdown; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class TableTest { + + @Test + void testCreateTable() { + // Test data + List headers = Arrays.asList("Name", "Age", "City"); + List rows = Arrays.asList("John Doe | 30 | New York", "Jane Smith | 25 | Los Angeles"); + + // Expected table + String expectedTable = "| Name | Age | City |\n" + + "| - | - | - |\n" + + "John Doe | 30 | New York\n" + + "Jane Smith | 25 | Los Angeles\n"; + + // Call the method + String actualTable = Table.createTable(headers, rows); + + // Assertions + assertThat(actualTable, is(expectedTable)); + } + + @Test + void testCreateTableWithEmptyHeadersAndRows() { + // Test data + List headers = Arrays.asList(); + List rows = Arrays.asList(); + + // Expected table + String expectedTable = ""; + + // Call the method + String actualTable = Table.createTable(headers, rows); + + // Assertions + assertThat(actualTable, is(expectedTable)); + } + + @Test + void testCreateTableWithEmptyRows() { + // Test data + List headers = Arrays.asList("Name", "Age"); + List rows = Arrays.asList(); + + // Expected table + String expectedTable = "| Name | Age |\n" + + "| - | - |\n"; + + // Call the method + String actualTable = Table.createTable(headers, rows); + + // Assertions + assertThat(actualTable, is(expectedTable)); + } + + @Test + void testCreateTableWithSingleHeaderAndRow() { + // Test data + List headers = Arrays.asList("Name"); + List rows = Arrays.asList("John Doe"); + + // Expected table + String expectedTable = "| Name |\n" + + "| - |\n" + + "John Doe\n"; + + // Call the method + String actualTable = Table.createTable(headers, rows); + + // Assertions + assertThat(actualTable, is(expectedTable)); + } + + @Test + void testCreateRow() { + String item1 = "item1"; + String item2 = "item2"; + String item3 = "item3"; + String expectedRow = String.format("| %s | %s | %s |", item1, item2, item3); + + assertThat(Table.createRow(item1, item2, item3), is(expectedRow)); + } +} diff --git a/test-docs-generator-maven-plugin/src/test/java/io/skodjob/markdown/TextListTest.java b/test-docs-generator-maven-plugin/src/test/java/io/skodjob/markdown/TextListTest.java new file mode 100644 index 0000000..2fc4c93 --- /dev/null +++ b/test-docs-generator-maven-plugin/src/test/java/io/skodjob/markdown/TextListTest.java @@ -0,0 +1,63 @@ +/* + * Copyright Skodjob authors. + * License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html). + */ +package io.skodjob.markdown; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class TextListTest { + + @Test + void testCreateTextList() { + // Test data + List objects = Arrays.asList("Apple", "Banana", "Orange"); + + // Expected list + String expectedList = "* Apple\n" + + "* Banana\n" + + "* Orange\n"; + + // Call the method + String actualList = TextList.createUnorderedList(objects); + + // Assertions + assertThat(actualList, is(expectedList)); + } + + @Test + void testCreateTextListWithEmptyList() { + // Test data + List objects = List.of(); + + // Expected list + String expectedList = ""; + + // Call the method + String actualList = TextList.createUnorderedList(objects); + + // Assertions + assertThat(actualList, is(expectedList)); + } + + @Test + void testCreateTextListWithSingleObject() { + // Test data + List objects = List.of("Apple"); + + // Expected list + String expectedList = "* Apple\n"; + + // Call the method + String actualList = TextList.createUnorderedList(objects); + + // Assertions + assertThat(actualList, is(expectedList)); + } +} diff --git a/test-docs-generator-maven-plugin/src/test/resources/expected-docs.md b/test-docs-generator-maven-plugin/src/test/resources/expected-docs.md new file mode 100644 index 0000000..cdbaf25 --- /dev/null +++ b/test-docs-generator-maven-plugin/src/test/resources/expected-docs.md @@ -0,0 +1,51 @@ +# TestClass + +## testMethodOne + +**Description:** Test checking that the application works as expected + +**Steps:** + +| Step | Action | Result | +| - | - | - | +| 1. | Create object instance | Instance of an object is created | +| 2. | Do a magic trick | Magic trick is done with success | +| 3. | Clean up the test case | Everything is cleared | + +**Use-cases:** + +* `core` + + +## testMethodTwo + +**Description:** Test checking that the application works as expected. This is just a little bit longer line, nothing else. + +**Steps:** + +| Step | Action | Result | +| - | - | - | +| 1. | Create object instance | Instance of an object is created | +| 2. | Do a magic trick | Magic trick is done with success | +| 3. | Clean up the test case | Everything is cleared | +| 4. | Do a magic cleanup check | Everything magically work | + +**Use-cases:** + +* `core` +* `core+` +* `core+++` + + +## testMethodThree + +**Description:** Test checking that the application works as expected. This is just a little bit longer line, nothing else. + +**Steps:** + +| Step | Action | Result | +| - | - | - | + +**Use-cases:** + +