Search in sources :

Example 31 with JsonNode

use of com.fasterxml.jackson.databind.JsonNode in project kafka by apache.

the class JsonConverterTest method testJsonSchemaMetadataTranslation.

// Schema metadata
@Test
public void testJsonSchemaMetadataTranslation() {
    JsonNode converted = parse(converter.fromConnectData(TOPIC, Schema.BOOLEAN_SCHEMA, true));
    validateEnvelope(converted);
    assertEquals(parse("{ \"type\": \"boolean\", \"optional\": false }"), converted.get(JsonSchema.ENVELOPE_SCHEMA_FIELD_NAME));
    assertEquals(true, converted.get(JsonSchema.ENVELOPE_PAYLOAD_FIELD_NAME).booleanValue());
    converted = parse(converter.fromConnectData(TOPIC, Schema.OPTIONAL_BOOLEAN_SCHEMA, null));
    validateEnvelope(converted);
    assertEquals(parse("{ \"type\": \"boolean\", \"optional\": true }"), converted.get(JsonSchema.ENVELOPE_SCHEMA_FIELD_NAME));
    assertTrue(converted.get(JsonSchema.ENVELOPE_PAYLOAD_FIELD_NAME).isNull());
    converted = parse(converter.fromConnectData(TOPIC, SchemaBuilder.bool().defaultValue(true).build(), true));
    validateEnvelope(converted);
    assertEquals(parse("{ \"type\": \"boolean\", \"optional\": false, \"default\": true }"), converted.get(JsonSchema.ENVELOPE_SCHEMA_FIELD_NAME));
    assertEquals(true, converted.get(JsonSchema.ENVELOPE_PAYLOAD_FIELD_NAME).booleanValue());
    converted = parse(converter.fromConnectData(TOPIC, SchemaBuilder.bool().required().name("bool").version(3).doc("the documentation").parameter("foo", "bar").build(), true));
    validateEnvelope(converted);
    assertEquals(parse("{ \"type\": \"boolean\", \"optional\": false, \"name\": \"bool\", \"version\": 3, \"doc\": \"the documentation\", \"parameters\": { \"foo\": \"bar\" }}"), converted.get(JsonSchema.ENVELOPE_SCHEMA_FIELD_NAME));
    assertEquals(true, converted.get(JsonSchema.ENVELOPE_PAYLOAD_FIELD_NAME).booleanValue());
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Example 32 with JsonNode

use of com.fasterxml.jackson.databind.JsonNode in project kafka by apache.

the class JsonConverterTest method nullSchemaAndArrayToJson.

@Test
public void nullSchemaAndArrayToJson() {
    // This still needs to do conversion of data, null schema means "anything goes". Make sure we mix and match
    // types to verify conversion still works.
    JsonNode converted = parse(converter.fromConnectData(TOPIC, null, Arrays.asList(1, "string", true)));
    validateEnvelopeNullSchema(converted);
    assertTrue(converted.get(JsonSchema.ENVELOPE_SCHEMA_FIELD_NAME).isNull());
    assertEquals(JsonNodeFactory.instance.arrayNode().add(1).add("string").add(true), converted.get(JsonSchema.ENVELOPE_PAYLOAD_FIELD_NAME));
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Example 33 with JsonNode

use of com.fasterxml.jackson.databind.JsonNode in project kafka by apache.

the class JsonConverterTest method structToJson.

@Test
public void structToJson() {
    Schema schema = SchemaBuilder.struct().field("field1", Schema.BOOLEAN_SCHEMA).field("field2", Schema.STRING_SCHEMA).field("field3", Schema.STRING_SCHEMA).field("field4", Schema.BOOLEAN_SCHEMA).build();
    Struct input = new Struct(schema).put("field1", true).put("field2", "string2").put("field3", "string3").put("field4", false);
    JsonNode converted = parse(converter.fromConnectData(TOPIC, schema, input));
    validateEnvelope(converted);
    assertEquals(parse("{ \"type\": \"struct\", \"optional\": false, \"fields\": [{ \"field\": \"field1\", \"type\": \"boolean\", \"optional\": false }, { \"field\": \"field2\", \"type\": \"string\", \"optional\": false }, { \"field\": \"field3\", \"type\": \"string\", \"optional\": false }, { \"field\": \"field4\", \"type\": \"boolean\", \"optional\": false }] }"), converted.get(JsonSchema.ENVELOPE_SCHEMA_FIELD_NAME));
    assertEquals(JsonNodeFactory.instance.objectNode().put("field1", true).put("field2", "string2").put("field3", "string3").put("field4", false), converted.get(JsonSchema.ENVELOPE_PAYLOAD_FIELD_NAME));
}
Also used : Schema(org.apache.kafka.connect.data.Schema) JsonNode(com.fasterxml.jackson.databind.JsonNode) Struct(org.apache.kafka.connect.data.Struct) Test(org.junit.Test)

Example 34 with JsonNode

use of com.fasterxml.jackson.databind.JsonNode in project kafka by apache.

the class JsonConverterTest method noSchemaToJson.

@Test
public void noSchemaToJson() {
    Map<String, Boolean> props = Collections.singletonMap("schemas.enable", false);
    converter.configure(props, true);
    JsonNode converted = parse(converter.fromConnectData(TOPIC, null, true));
    assertTrue(converted.isBoolean());
    assertEquals(true, converted.booleanValue());
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Example 35 with JsonNode

use of com.fasterxml.jackson.databind.JsonNode in project kafka by apache.

the class JsonConverterTest method stringToJson.

@Test
public void stringToJson() {
    JsonNode converted = parse(converter.fromConnectData(TOPIC, Schema.STRING_SCHEMA, "test-string"));
    validateEnvelope(converted);
    assertEquals(parse("{ \"type\": \"string\", \"optional\": false }"), converted.get(JsonSchema.ENVELOPE_SCHEMA_FIELD_NAME));
    assertEquals("test-string", converted.get(JsonSchema.ENVELOPE_PAYLOAD_FIELD_NAME).textValue());
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)879 Test (org.junit.Test)224 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)217 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)142 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)125 IOException (java.io.IOException)91 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)86 HashMap (java.util.HashMap)79 HttpGet (org.apache.http.client.methods.HttpGet)68 JsonException (jmri.server.json.JsonException)66 Deployment (org.activiti.engine.test.Deployment)66 ArrayList (java.util.ArrayList)62 InputStream (java.io.InputStream)55 StringEntity (org.apache.http.entity.StringEntity)54 ByteArrayInputStream (java.io.ByteArrayInputStream)51 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)49 Task (org.activiti.engine.task.Task)41 HttpPost (org.apache.http.client.methods.HttpPost)39 Map (java.util.Map)33 Tree (org.apache.jackrabbit.oak.api.Tree)30