♻️ Use actual JSON matchers for Server test
authorDavid Roden <github-a8in@qsheltier.de>
Sat, 25 Jan 2025 17:19:15 +0000 (18:19 +0100)
committerDavid Roden <github-a8in@qsheltier.de>
Sat, 25 Jan 2025 19:01:27 +0000 (20:01 +0100)
pom.xml
server/pom.xml
server/src/test/java/de/qsheltier/msta/ServerTest.java

diff --git a/pom.xml b/pom.xml
index 2d20018..4508734 100644 (file)
--- a/pom.xml
+++ b/pom.xml
                                <artifactId>jackson-databind</artifactId>
                                <version>2.18.1</version>
                        </dependency>
+                       <dependency>
+                               <groupId>com.spotify</groupId>
+                               <artifactId>hamcrest-jackson</artifactId>
+                               <version>1.3.2</version>
+                               <scope>test</scope>
+                       </dependency>
                </dependencies>
        </dependencyManagement>
 
index 00b8bb4..eb13148 100644 (file)
                        <groupId>com.fasterxml.jackson.core</groupId>
                        <artifactId>jackson-databind</artifactId>
                </dependency>
+               <dependency>
+                       <groupId>com.spotify</groupId>
+                       <artifactId>hamcrest-jackson</artifactId>
+               </dependency>
        </dependencies>
 
 </project>
index c724a8d..aa7606b 100644 (file)
@@ -14,6 +14,9 @@ import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.Timeout;
 import org.junit.jupiter.api.function.ThrowingConsumer;
 
+import static com.spotify.hamcrest.jackson.JsonMatchers.isJsonStringMatching;
+import static com.spotify.hamcrest.jackson.JsonMatchers.jsonObject;
+import static com.spotify.hamcrest.jackson.JsonMatchers.jsonText;
 import static java.nio.charset.StandardCharsets.UTF_8;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.equalTo;
@@ -37,7 +40,7 @@ public class ServerTest {
                        try (var connection = new Socket("localhost", server.getPort());
                             var reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
                                var line = reader.readLine();
-                               assertThat(line, equalTo("{\"event\":\"connected\"}"));
+                               assertThat(line, isJsonStringMatching(jsonObject().where("event", jsonText("connected"))));
                        }
                }
        }
@@ -81,7 +84,7 @@ public class ServerTest {
                try {
                        connectToServerAndWaitForEvent(reader -> {
                                var line = reader.readLine();
-                               assertThat(line, equalTo("{\"event\":\"new-window\"}"));
+                               assertThat(line, isJsonStringMatching(jsonObject().where("event", jsonText("new-window"))));
                        });
                } finally {
                        window.setVisible(false);
@@ -96,7 +99,7 @@ public class ServerTest {
                try {
                        connectToServerAndWaitForEvent(reader -> {
                                var line = reader.readLine();
-                               assertThat(line, equalTo("{\"event\":\"new-frame\"}"));
+                               assertThat(line, isJsonStringMatching(jsonObject().where("event", jsonText("new-frame"))));
                        });
                } finally {
                        frame.setVisible(false);
@@ -127,12 +130,12 @@ public class ServerTest {
                        try (var connection = new Socket("localhost", server.getPort());
                             var reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
                                var line = reader.readLine();
-                               assertThat(line, equalTo("{\"event\":\"connected\"}"));
+                               assertThat(line, isJsonStringMatching(jsonObject().where("event", jsonText("connected"))));
                        }
                        try (var connection = new Socket("localhost", server.getPort());
                             var reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
                                var line = reader.readLine();
-                               assertThat(line, equalTo("{\"event\":\"connected\"}"));
+                               assertThat(line, isJsonStringMatching(jsonObject().where("event", jsonText("connected"))));
                        }
                }
        }