Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

import jakarta.json.JsonObject;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;

import static org.junit.jupiter.api.Assertions.*;

public class DeserViaCreatorTest extends TestBase
{
static class Pojo {
Expand All @@ -19,6 +23,7 @@ public Pojo(@JsonProperty("s") String s, @JsonProperty("o") JsonObject o) {
}
}

@Test
public void testCreatorDeser() throws Exception
{
final ObjectMapper mapper = sharedMapper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,75 +9,81 @@
import jakarta.json.JsonValue;
import java.util.Objects;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.*;

public class JsonMergePatchDeserializationTest extends TestBase {

private static final ObjectMapper MAPPER = newMapper();

@Test
public void testObjectDeserializationAndPatching() throws Exception {
final String json = "{" +
"\"name\":\"Json\"" +
"}";

final JsonMergePatch jsonMergePatch = MAPPER.readValue(json, JsonMergePatch.class);
final JsonValue jsonPatchAsJsonValue = jsonMergePatch.toJsonValue();
assertThat(jsonPatchAsJsonValue, instanceOf(JsonObject.class));
assertInstanceOf(JsonObject.class, jsonPatchAsJsonValue);

final JsonObject jsonPatchAsJsonObject = jsonPatchAsJsonValue.asJsonObject();
assertTrue(jsonPatchAsJsonObject.containsKey("name"));
assertThat(jsonPatchAsJsonObject.get("name"), instanceOf(JsonString.class));
assertThat(jsonPatchAsJsonObject.getString("name"), is("Json"));
assertInstanceOf(JsonString.class, jsonPatchAsJsonObject.get("name"));
assertEquals("Json", jsonPatchAsJsonObject.getString("name"));


assertThat(serializeAsString(jsonPatchAsJsonValue), is(json));
assertEquals(json, serializeAsString(jsonPatchAsJsonValue));

final Person person = new Person("John", "Smith");
final JsonValue personJson = MAPPER.convertValue(person, JsonValue.class);
final JsonValue patchedPersonJson = jsonMergePatch.apply(personJson);
final Person patchedPerson = MAPPER.convertValue(patchedPersonJson, Person.class);
assertThat(patchedPerson, is(new Person("Json", "Smith")));
assertEquals(new Person("Json", "Smith"), patchedPerson);
}

@Test
public void testArrayDeserializationAndPatching() throws Exception {
final String json = "[" +
"\"name\",\"Json\"" +
"]";

final JsonMergePatch jsonMergePatch = MAPPER.readValue(json, JsonMergePatch.class);
final JsonValue jsonPatchAsJsonValue = jsonMergePatch.toJsonValue();
assertThat(jsonPatchAsJsonValue, instanceOf(JsonArray.class));
assertThat(jsonPatchAsJsonValue).isInstanceOf(JsonArray.class);


final JsonArray jsonPatchAsJsonArray = jsonPatchAsJsonValue.asJsonArray();
assertThat(jsonPatchAsJsonArray.size(), is(2));
assertThat(jsonPatchAsJsonArray.get(0), instanceOf(JsonString.class));
assertThat(((JsonString)jsonPatchAsJsonArray.get(0)).getString(), is("name"));
assertThat(jsonPatchAsJsonArray.get(1), instanceOf(JsonString.class));
assertThat(((JsonString)jsonPatchAsJsonArray.get(1)).getString(), is("Json"));
assertThat(jsonPatchAsJsonArray.size()).isEqualTo(2);
assertThat(jsonPatchAsJsonArray.get(0)).isInstanceOf(JsonString.class);
assertThat(((JsonString) jsonPatchAsJsonArray.get(0)).getString()).isEqualTo("name");
assertThat(jsonPatchAsJsonArray.get(1)).isInstanceOf(JsonString.class);
assertThat(((JsonString) jsonPatchAsJsonArray.get(1)).getString()).isEqualTo("Json");

assertThat(serializeAsString(jsonPatchAsJsonValue), is(json));
assertThat(serializeAsString(jsonPatchAsJsonValue)).isEqualTo(json);

final Person person = new Person("John", "Smith");
final JsonValue personJson = MAPPER.convertValue(person, JsonValue.class);
final JsonValue patchedPersonJson = jsonMergePatch.apply(personJson);
assertThat(patchedPersonJson, instanceOf(JsonArray.class));
assertThat(patchedPersonJson).isInstanceOf(JsonArray.class);
}

@Test
public void testScalarDeserializationAndPatching() throws Exception {
final String json = "\"name\"";

final JsonMergePatch jsonMergePatch = MAPPER.readValue(json, JsonMergePatch.class);
final JsonValue jsonPatchAsJsonValue = jsonMergePatch.toJsonValue();
assertThat(jsonPatchAsJsonValue, instanceOf(JsonString.class));
assertThat(jsonPatchAsJsonValue).isInstanceOf(JsonString.class);

assertThat(serializeAsString(jsonPatchAsJsonValue), is(json));
assertThat(serializeAsString(jsonPatchAsJsonValue)).isEqualTo(json);

final Person person = new Person("John", "Smith");
final JsonValue personJson = MAPPER.convertValue(person, JsonValue.class);
final JsonValue patchedPersonJson = jsonMergePatch.apply(personJson);
assertThat(patchedPersonJson, instanceOf(JsonString.class));
assertThat(((JsonString)patchedPersonJson).getString(), is("name"));
assertThat(patchedPersonJson).isInstanceOf(JsonString.class);
assertThat(((JsonString) patchedPersonJson).getString()).isEqualTo("name");
}

static class Person {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@

import jakarta.json.JsonMergePatch;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class JsonMergePatchSerializationTest extends TestBase
{
private static final ObjectMapper MAPPER = newMapper();

@Test
public void testSimpleSerialization() throws Exception
{
// First need a patch so deserialization must work
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@

import java.util.Objects;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.*;

public class JsonPatchDeserializationTest extends TestBase {

private static final ObjectMapper MAPPER = newMapper();

@Test
public void testDeserializationAndPatching() throws Exception {
final String json = "[" +
"{" +
Expand All @@ -25,28 +27,28 @@ public void testDeserializationAndPatching() throws Exception {

final JsonPatch jsonPatch = MAPPER.readValue(json, JsonPatch.class);
final JsonArray jsonPatchAsJsonArray = jsonPatch.toJsonArray();
assertThat(jsonPatchAsJsonArray.get(0), instanceOf(JsonObject.class));
assertThat(jsonPatchAsJsonArray.get(0)).isInstanceOf(JsonObject.class);

final JsonObject firstOperation = jsonPatchAsJsonArray.get(0).asJsonObject();
assertTrue(firstOperation.containsKey("op"));
assertThat(firstOperation.get("op"), instanceOf(JsonString.class));
assertThat(firstOperation.getString("op"), is("replace"));
assertThat(firstOperation.get("op")).isInstanceOf(JsonString.class);
assertThat(firstOperation.getString("op")).isEqualTo("replace");

assertTrue(firstOperation.containsKey("path"));
assertThat(firstOperation.get("path"), instanceOf(JsonString.class));
assertThat(firstOperation.getString("path"), is("/name"));
assertThat(firstOperation.get("path")).isInstanceOf(JsonString.class);
assertThat(firstOperation.getString("path")).isEqualTo("/name");

assertTrue(firstOperation.containsKey("value"));
assertThat(firstOperation.get("value"), instanceOf(JsonString.class));
assertThat(firstOperation.getString("value"), is("Json"));
assertThat(firstOperation.get("value")).isInstanceOf(JsonString.class);
assertThat(firstOperation.getString("value")).isEqualTo("Json");

assertThat(serializeAsString(jsonPatchAsJsonArray), is(json));
assertThat(serializeAsString(jsonPatchAsJsonArray)).isEqualTo(json);

final Person person = new Person("John", "Smith");
final JsonStructure personJson = MAPPER.convertValue(person, JsonStructure.class);
final JsonStructure patchedPersonJson = jsonPatch.apply(personJson);
final Person patchedPerson = MAPPER.convertValue(patchedPersonJson, Person.class);
assertThat(patchedPerson, is(new Person("Json", "Smith")));
assertThat(patchedPerson).isEqualTo(new Person("Json", "Smith"));
}

static class Person {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@

import jakarta.json.JsonPatch;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class JsonPatchSerializationTest extends TestBase
{
private static final ObjectMapper MAPPER = newMapper();

@Test
public void testSimpleSerialization() throws Exception
{
// First need a patch so deserialization must work
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import jakarta.json.*;
import jakarta.json.JsonValue.ValueType;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.beans.ConstructorProperties;
import java.math.BigDecimal;

import static org.junit.jupiter.api.Assertions.*;

public class JsonValueDeserializationTest extends TestBase
{
static class ObjectImpl {
Expand All @@ -24,6 +28,7 @@ public ObjectImpl(JsonValue obj1, JsonValue obj2) {

private final ObjectMapper MAPPER = newMapper();

@Test
public void testSimpleArray() throws Exception
{
final String JSON = "[1,true,\"foo\"]";
Expand All @@ -43,6 +48,7 @@ public void testSimpleArray() throws Exception
assertEquals(JSON, serializeAsString(v));
}

@Test
public void testNestedArray() throws Exception
{
final String JSON = "[1,[false,45],{\"foo\":13}]";
Expand All @@ -58,6 +64,7 @@ public void testNestedArray() throws Exception
assertEquals(JSON, serializeAsString(v));
}

@Test
public void testSimpleObject() throws Exception
{
final String JSON = "{\"a\":12.5,\"b\":\"Text\"}";
Expand All @@ -79,6 +86,7 @@ public void testSimpleObject() throws Exception
assertEquals(JSON, serializeAsString(v));
}

@Test
public void testNestedObject() throws Exception
{
final String JSON = "{\"array\":[1,2],\"obj\":{\"first\":true}}";
Expand All @@ -101,6 +109,7 @@ public void testNestedObject() throws Exception
}

// for [datatype-jsr353#5]
@Test
public void testBinaryNode() throws Exception
{
ObjectNode root = MAPPER.createObjectNode();
Expand All @@ -116,6 +125,7 @@ public void testBinaryNode() throws Exception
}

// for [datatype-jsr353#16]
@Test
public void testNullNode() throws Exception
{
final String serializedNull = MAPPER.writeValueAsString(JsonValue.NULL);
Expand All @@ -125,6 +135,7 @@ public void testNullNode() throws Exception
}

// for [datatype-jsr353#19]
@Test
public void testConstructorProperties() throws Exception
{
ObjectImpl ob = MAPPER.readValue("{\"obj1\":{}}", ObjectImpl.class);
Expand All @@ -136,6 +147,7 @@ public void testConstructorProperties() throws Exception
assertSame(JsonValue.NULL, ob2.obj2);
}

@Test
public void testBigInteger() throws Exception
{
final String JSON = "[2e308]";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@

import jakarta.json.*;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class JsonValueSerializationTest extends TestBase
{
@Test
public void testSimpleArray() throws Exception
{
JsonArray arr = arrayBuilder()
Expand All @@ -17,6 +22,7 @@ public void testSimpleArray() throws Exception
assertEquals("[true,null,123,15.25]", serializeAsString(arr));
}

@Test
public void testNestedArray() throws Exception
{
JsonArray arr = arrayBuilder()
Expand All @@ -27,6 +33,7 @@ public void testNestedArray() throws Exception
assertEquals("[1,[false,45],{\"foo\":13}]", serializeAsString(arr));
}

@Test
public void testSimpleObject() throws Exception
{
JsonObject ob = objectBuilder()
Expand All @@ -37,6 +44,7 @@ public void testSimpleObject() throws Exception
assertEquals("{\"a\":123,\"b\":\"Text\"}", serializeAsString(ob));
}

@Test
public void testNestedObject() throws Exception
{
JsonObject ob = objectBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import jakarta.json.*;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.ObjectMapper;

import static org.junit.jupiter.api.Assertions.*;

public class PolymorphicTest extends TestBase
{
static class Wrapper {
Expand All @@ -19,6 +23,7 @@ public Wrapper() { }
.polymorphicTypeValidator(new NoCheckSubTypeValidator())
.build();

@Test
public void testObjectAsTyped() throws Exception
{
final String INPUT = "{\"array\":[1,2],\"obj\":{\"first\":true}}";
Expand All @@ -36,6 +41,7 @@ public void testObjectAsTyped() throws Exception
assertEquals(1, arr.getInt(0));
}

@Test
public void testArrayAsTyped() throws Exception
{
final String INPUT = "[1,{\"a\":true}]";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;

public abstract class TestBase extends junit.framework.TestCase
public abstract class TestBase
{
static class NoCheckSubTypeValidator
extends PolymorphicTypeValidator.Base
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.core.Versioned;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class TestVersions extends TestBase
{
@Test
public void testModuleVersionAndName() throws IOException
{
JSONPModule module = new JSONPModule();
Expand Down
Loading