diff options
author | Jon Bratseth <bratseth@verizonmedia.com> | 2019-03-22 14:17:06 +0100 |
---|---|---|
committer | Jon Bratseth <bratseth@verizonmedia.com> | 2019-03-22 14:17:06 +0100 |
commit | 90cc2bce893af67d0fbb6b16ea29276d9ee5573a (patch) | |
tree | 806e2b61943d9f83524469c032061e380bcfbca6 /vespa-http-client/src/test/java | |
parent | 0f16f5bb7869dec2f2c4677d3b3e07d1ec788408 (diff) |
Remove testutils dependency
Diffstat (limited to 'vespa-http-client/src/test/java')
-rw-r--r-- | vespa-http-client/src/test/java/com/yahoo/vespa/http/client/JsonTestHelper.java | 52 | ||||
-rw-r--r-- | vespa-http-client/src/test/java/com/yahoo/vespa/http/client/runner/JsonReaderTest.java | 2 |
2 files changed, 53 insertions, 1 deletions
diff --git a/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/JsonTestHelper.java b/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/JsonTestHelper.java new file mode 100644 index 00000000000..0081497fb20 --- /dev/null +++ b/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/JsonTestHelper.java @@ -0,0 +1,52 @@ +// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.vespa.http.client; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.base.Joiner; +import org.hamcrest.MatcherAssert; + +import static uk.co.datumedge.hamcrest.json.SameJSONAs.sameJSONAs; + +import java.io.UncheckedIOException; + +public class JsonTestHelper { + + private static final ObjectMapper mapper = new ObjectMapper(); + + /** + * Convenience method to input JSON without escaping double quotes and newlines + * Each parameter represents a line of JSON encoded data + * The lines are joined with newline and single quotes are replaced with double quotes + */ + public static String inputJson(String... lines) { + return Joiner.on("\n").join(lines).replaceAll("'", "\""); + } + + /** Structurally compare two JSON encoded strings */ + public static void assertJsonEquals(String inputJson, String expectedJson) { + MatcherAssert.assertThat(inputJson, sameJSONAs(expectedJson)); + } + + /** Structurally compare a {@link JsonNode} and a JSON string. */ + public static void assertJsonEquals(JsonNode left, String rightJson) { + try { + String leftJson = mapper.writeValueAsString(left); + assertJsonEquals(leftJson, rightJson); + } catch (JsonProcessingException e) { + throw new UncheckedIOException(e); + } + } + + /** Structurally compare two {@link JsonNode}s. */ + public static void assertJsonEquals(JsonNode left, JsonNode right) { + try { + String leftJson = mapper.writeValueAsString(left); + String rightJson = mapper.writeValueAsString(right); + assertJsonEquals(leftJson, rightJson); + } catch (JsonProcessingException e) { + throw new UncheckedIOException(e); + } + } +} diff --git a/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/runner/JsonReaderTest.java b/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/runner/JsonReaderTest.java index 134274ff869..e2046fe17a6 100644 --- a/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/runner/JsonReaderTest.java +++ b/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/runner/JsonReaderTest.java @@ -12,7 +12,7 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; -import static com.yahoo.test.json.JsonTestHelper.inputJson; +import static com.yahoo.vespa.http.client.JsonTestHelper.inputJson; import static org.hamcrest.core.Is.is; import static org.junit.Assert.*; |