diff options
author | Bjørn Christian Seime <bjorncs@vespa.ai> | 2024-02-15 14:17:05 +0100 |
---|---|---|
committer | Bjørn Christian Seime <bjorncs@vespa.ai> | 2024-02-15 15:19:43 +0100 |
commit | 0df7382fcebf4d5768a59f7a15ac23391ba1023f (patch) | |
tree | 054c94c833adb38811eb325ca05b0705d8062298 /container-core/src/test/java/com/yahoo | |
parent | fe7cfe2a7c6c549acedd217f73fa4fc6340c81eb (diff) |
Add simple Slime wrapper throwing 400 on missing/invalid content
Diffstat (limited to 'container-core/src/test/java/com/yahoo')
-rw-r--r-- | container-core/src/test/java/com/yahoo/restapi/JsonTest.java | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/container-core/src/test/java/com/yahoo/restapi/JsonTest.java b/container-core/src/test/java/com/yahoo/restapi/JsonTest.java new file mode 100644 index 00000000000..276c9b55ea4 --- /dev/null +++ b/container-core/src/test/java/com/yahoo/restapi/JsonTest.java @@ -0,0 +1,95 @@ +package com.yahoo.restapi; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * @author bjorncs + */ +class JsonTest { + + @Test + void parses_json_correctly() { + var text = + """ + { + "string": "bar", + "integer": 42, + "floaty": 8.25, + "bool": true, + "array": [1, 2, 3], + "quux": { + "corge": "grault" + } + } + """; + var json = Json.of(text); + + // Primitive members + assertEquals("bar", json.f("string").asString()); + assertTrue(json.f("string").asOptionalString().isPresent()); + assertEquals("bar", json.f("string").asOptionalString().get()); + assertEquals(42L, json.f("integer").asLong()); + assertEquals(42D, json.f("integer").asDouble()); + assertEquals(8.25D, json.f("floaty").asDouble()); + assertEquals(8L, json.f("floaty").asLong()); + assertTrue(json.f("bool").asBool()); + + // Array member + assertEquals(3, json.f("array").length()); + assertEquals(1L, json.f("array").entry(0).asLong()); + assertEquals(2L, json.f("array").entry(1).asLong()); + assertEquals(3L, json.f("array").entry(2).asLong()); + json.f("array").forEachEntry((i, entry) -> assertEquals(i + 1, entry.asLong())); + int counter = 0; + for (var entry : json.f("array")) { + assertEquals(++counter, entry.asLong()); + } + + // Object member + assertEquals("grault", json.f("quux").f("corge").asString()); + json.f("quux").forEachField((name, child) -> { + assertEquals("corge", name); + assertEquals("grault", child.asString()); + }); + } + + @Test + void throws_on_missing_and_invalid_members() { + var text = + """ + { + "string": "bar" + } + """; + var json = Json.of(text); + + var exception = assertThrows(RestApiException.BadRequest.class, () -> json.f("unknown").asString()); + assertEquals("Missing JSON member 'unknown'", exception.getMessage()); + + exception = assertThrows(RestApiException.BadRequest.class, () -> json.a(0)); + assertEquals("Expected JSON to be a 'array' but got 'object'", exception.getMessage()); + + exception = assertThrows(RestApiException.BadRequest.class, () -> json.f("string").f("unknown")); + assertEquals("Expected JSON member 'string' to be a 'object' but got 'string'", exception.getMessage()); + + exception = assertThrows(RestApiException.BadRequest.class, () -> json.f("string").asLong()); + assertEquals("Expected JSON member 'string' to be a 'integer' or 'float' but got 'string'", exception.getMessage()); + } + + @Test + void fallback_to_default_if_field_missing() { + var text = + """ + { + "string": "bar" + } + """; + var json = Json.of(text); + assertEquals("foo", json.f("unknown").asString("foo")); + assertEquals("foo", json.f("unknown").asOptionalString().orElse("foo")); + assertEquals("bar", json.f("string").asString("foo")); + assertEquals("bar", json.f("string").asOptionalString().orElse("foo")); + } +} |