From 2ced7417a97883260ce347fd1cf080196181d68c Mon Sep 17 00:00:00 2001 From: tal66 <77445020+tal66@users.noreply.github.com> Date: Mon, 13 Dec 2021 19:44:21 +0200 Subject: [PATCH] format JSON of expected response in generated test files format json generated in test files format json in test files resources add method assertEqualJsonFiles() to compare json files using ObjectMapper, instead of text based compare fix typo (missing 'n' in "Antoine Griezmann"'s name) --- .../ExpectedResponseFileGenerator.java | 17 +++++++++++++++ .../JsonWithoutSqlExecutionController.java | 4 ++-- .../testgen/util/FileContentVerifier.java | 21 ++++++++++++++++++- .../json-with-n-plus-one-select-expected.json | 10 ++++++++- .../json-without-sql-expected.json | 10 ++++++++- .../json-with-n-plus-one-select-expected.json | 10 ++++++++- .../json-without-sql-expected.json | 10 ++++++++- 7 files changed, 75 insertions(+), 7 deletions(-) diff --git a/spring-boot-2/src/main/java/org/quickperf/web/spring/testgeneration/ExpectedResponseFileGenerator.java b/spring-boot-2/src/main/java/org/quickperf/web/spring/testgeneration/ExpectedResponseFileGenerator.java index 954b155..a25b387 100644 --- a/spring-boot-2/src/main/java/org/quickperf/web/spring/testgeneration/ExpectedResponseFileGenerator.java +++ b/spring-boot-2/src/main/java/org/quickperf/web/spring/testgeneration/ExpectedResponseFileGenerator.java @@ -12,6 +12,8 @@ */ package org.quickperf.web.spring.testgeneration; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import org.quickperf.web.spring.HttpContentType; class ExpectedResponseFileGenerator { @@ -24,6 +26,9 @@ private ExpectedResponseFileGenerator() { TestFile generate(String content, String contentType, String fileNameWithoutExtension) { String responseExtension = findExpectedResponseFileExtension(contentType); String expectedResponseFileName = fileNameWithoutExtension + "-expected" + responseExtension; + if (responseExtension.equals(".json")){ + content = formatJson(content); + } return new TestFile(expectedResponseFileName, content); } @@ -37,4 +42,16 @@ private String findExpectedResponseFileExtension(String contentType) { return ".txt"; } + private String formatJson(String content){ + ObjectMapper mapper = new ObjectMapper(); + try { + Object str = mapper.readValue(content, Object.class); + String jsonPrettyPrint = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(str); + content = jsonPrettyPrint; + } catch (JsonProcessingException e) { + // do nothing. original content will be used + } + return content; + } + } diff --git a/spring-boot2-test/src/main/java/org/quickperf/spring/springboottest/controller/JsonWithoutSqlExecutionController.java b/spring-boot2-test/src/main/java/org/quickperf/spring/springboottest/controller/JsonWithoutSqlExecutionController.java index 8adebc2..46588ab 100644 --- a/spring-boot2-test/src/main/java/org/quickperf/spring/springboottest/controller/JsonWithoutSqlExecutionController.java +++ b/spring-boot2-test/src/main/java/org/quickperf/spring/springboottest/controller/JsonWithoutSqlExecutionController.java @@ -26,8 +26,8 @@ public class JsonWithoutSqlExecutionController { @GetMapping(HttpUrl.JSON_WITHOUT_SQL) public List jsonWithoutSql() { PlayerWithTeamName paulPogba = new PlayerWithTeamName("Paul", "Pogba", "Manchester United"); - PlayerWithTeamName antoineGriezman = new PlayerWithTeamName("Antoine", "Griezman", "Barcelona" ); - return asList(paulPogba, antoineGriezman); + PlayerWithTeamName antoineGriezmann = new PlayerWithTeamName("Antoine", "Griezmann", "Barcelona" ); + return asList(paulPogba, antoineGriezmann); } } diff --git a/spring-boot2-test/src/test/java/testgen/util/FileContentVerifier.java b/spring-boot2-test/src/test/java/testgen/util/FileContentVerifier.java index aca1e21..a2f8b5b 100644 --- a/spring-boot2-test/src/test/java/testgen/util/FileContentVerifier.java +++ b/spring-boot2-test/src/test/java/testgen/util/FileContentVerifier.java @@ -12,11 +12,16 @@ */ package testgen.util; +import com.fasterxml.jackson.databind.ObjectMapper; + import java.io.File; +import java.io.IOException; +import java.io.UncheckedIOException; import java.nio.file.Path; import java.nio.file.Paths; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; public class FileContentVerifier { @@ -34,7 +39,12 @@ public void compareGeneratedFileWithReferenceFile(String fileName) { File testFolder = new File(pathOfFolderContainingGeneratedFiles); File generatedSqlFile = new File(testFolder, fileName); File expectedSqlFile = new File(folderContainingReferenceFiles, fileName); - assertThat(generatedSqlFile).hasSameTextualContentAs(expectedSqlFile); + + if (fileName.endsWith(".json")){ + assertEqualJsonFiles(expectedSqlFile, generatedSqlFile); + } else { + assertThat(generatedSqlFile).hasSameTextualContentAs(expectedSqlFile); + } } private File findFolderContainingReferenceFiles(String referenceFolderRelativePath) { @@ -47,4 +57,13 @@ private File findFolderContainingReferenceFiles(String referenceFolderRelativePa + File.separator + referenceFolderRelativePath); } + private void assertEqualJsonFiles(File expectedSqlFile, File generatedSqlFile) { + ObjectMapper mapper = new ObjectMapper(); + try { + assertEquals(mapper.readTree(expectedSqlFile), mapper.readTree(generatedSqlFile)); + } catch (IOException e) { + throw new UncheckedIOException(String.format("Unable to compare json files:\n<%s> \nand:\n<%s>", expectedSqlFile.getAbsoluteFile(), generatedSqlFile.getAbsoluteFile()), e); + } + } + } diff --git a/spring-boot2-test/src/test/resources/testgen-expected/junit4/json-with-n-plus-one-select/json-with-n-plus-one-select-expected.json b/spring-boot2-test/src/test/resources/testgen-expected/junit4/json-with-n-plus-one-select/json-with-n-plus-one-select-expected.json index 753c660..2c38da1 100644 --- a/spring-boot2-test/src/test/resources/testgen-expected/junit4/json-with-n-plus-one-select/json-with-n-plus-one-select-expected.json +++ b/spring-boot2-test/src/test/resources/testgen-expected/junit4/json-with-n-plus-one-select/json-with-n-plus-one-select-expected.json @@ -1 +1,9 @@ -[{"firstName":"Paul","lastName":"Pogba","team":"Manchester United"},{"firstName":"Antoine","lastName":"Griezmann","team":"Barcelona"}] \ No newline at end of file +[ { + "firstName" : "Paul", + "lastName" : "Pogba", + "team" : "Manchester United" +}, { + "firstName" : "Antoine", + "lastName" : "Griezmann", + "team" : "Barcelona" +} ] \ No newline at end of file diff --git a/spring-boot2-test/src/test/resources/testgen-expected/junit4/json-without-sql/json-without-sql-expected.json b/spring-boot2-test/src/test/resources/testgen-expected/junit4/json-without-sql/json-without-sql-expected.json index 8a3bc2a..2c38da1 100644 --- a/spring-boot2-test/src/test/resources/testgen-expected/junit4/json-without-sql/json-without-sql-expected.json +++ b/spring-boot2-test/src/test/resources/testgen-expected/junit4/json-without-sql/json-without-sql-expected.json @@ -1 +1,9 @@ -[{"firstName":"Paul","lastName":"Pogba","team":"Manchester United"},{"firstName":"Antoine","lastName":"Griezman","team":"Barcelona"}] \ No newline at end of file +[ { + "firstName" : "Paul", + "lastName" : "Pogba", + "team" : "Manchester United" +}, { + "firstName" : "Antoine", + "lastName" : "Griezmann", + "team" : "Barcelona" +} ] \ No newline at end of file diff --git a/spring-boot2-test/src/test/resources/testgen-expected/junit5/json-with-n-plus-one-select/json-with-n-plus-one-select-expected.json b/spring-boot2-test/src/test/resources/testgen-expected/junit5/json-with-n-plus-one-select/json-with-n-plus-one-select-expected.json index 753c660..2c38da1 100644 --- a/spring-boot2-test/src/test/resources/testgen-expected/junit5/json-with-n-plus-one-select/json-with-n-plus-one-select-expected.json +++ b/spring-boot2-test/src/test/resources/testgen-expected/junit5/json-with-n-plus-one-select/json-with-n-plus-one-select-expected.json @@ -1 +1,9 @@ -[{"firstName":"Paul","lastName":"Pogba","team":"Manchester United"},{"firstName":"Antoine","lastName":"Griezmann","team":"Barcelona"}] \ No newline at end of file +[ { + "firstName" : "Paul", + "lastName" : "Pogba", + "team" : "Manchester United" +}, { + "firstName" : "Antoine", + "lastName" : "Griezmann", + "team" : "Barcelona" +} ] \ No newline at end of file diff --git a/spring-boot2-test/src/test/resources/testgen-expected/junit5/json-without-sql/json-without-sql-expected.json b/spring-boot2-test/src/test/resources/testgen-expected/junit5/json-without-sql/json-without-sql-expected.json index 8a3bc2a..2c38da1 100644 --- a/spring-boot2-test/src/test/resources/testgen-expected/junit5/json-without-sql/json-without-sql-expected.json +++ b/spring-boot2-test/src/test/resources/testgen-expected/junit5/json-without-sql/json-without-sql-expected.json @@ -1 +1,9 @@ -[{"firstName":"Paul","lastName":"Pogba","team":"Manchester United"},{"firstName":"Antoine","lastName":"Griezman","team":"Barcelona"}] \ No newline at end of file +[ { + "firstName" : "Paul", + "lastName" : "Pogba", + "team" : "Manchester United" +}, { + "firstName" : "Antoine", + "lastName" : "Griezmann", + "team" : "Barcelona" +} ] \ No newline at end of file