Search in sources :

Example 11 with Schema

use of com.hortonworks.registries.common.Schema in project streamline by hortonworks.

the class SchemaValueConverterTest method convertMapValueHasUndefinedField.

@Test(expected = SchemaValidationFailedException.class)
public void convertMapValueHasUndefinedField() {
    Schema schema = Schema.of(Schema.Field.of("a", Schema.Type.BINARY), Schema.Field.of("b", Schema.Type.ARRAY), Schema.Field.of("c", Schema.Type.STRING));
    Map<String, Object> value = new HashMap<>();
    value.put("a", new byte[] { 0x01, 0x02 });
    value.put("b", Collections.singletonList("hello"));
    value.put("c", "hello");
    value.put("d", "world");
    SchemaValueConverter.convertMap(schema, value);
}
Also used : HashMap(java.util.HashMap) Schema(com.hortonworks.registries.common.Schema) Test(org.junit.Test)

Example 12 with Schema

use of com.hortonworks.registries.common.Schema in project streamline by hortonworks.

the class SchemaValueConverterTest method convertMapValueDoesNotHaveOptionalField.

@Test
public void convertMapValueDoesNotHaveOptionalField() throws ParserException {
    Schema schema = Schema.of(Schema.Field.of("a", Schema.Type.BINARY), Schema.Field.optional("b", Schema.Type.STRING), Schema.Field.of("c", Schema.Type.ARRAY));
    Map<String, Object> value = new HashMap<>();
    value.put("a", new byte[] { 0x01, 0x02 });
    value.put("c", Collections.singletonList("hello"));
    Map<String, Object> converted = SchemaValueConverter.convertMap(schema, value);
    Assert.assertEquals(value.size(), converted.size());
    Assert.assertTrue(Schema.Type.BINARY.valueOfSameType(converted.get("a")));
    Assert.assertTrue(Arrays.equals(new byte[] { 0x01, 0x02 }, (byte[]) converted.get("a")));
    Assert.assertTrue(Schema.Type.ARRAY.valueOfSameType(converted.get("c")));
    Assert.assertEquals(Collections.singletonList("hello"), converted.get("c"));
}
Also used : HashMap(java.util.HashMap) Schema(com.hortonworks.registries.common.Schema) Test(org.junit.Test)

Example 13 with Schema

use of com.hortonworks.registries.common.Schema in project streamline by hortonworks.

the class SchemaValueConverterTest method convertMap.

@Test
public void convertMap() throws Exception {
    Schema schema = Schema.of(Schema.Field.of("a", Schema.Type.BINARY), Schema.Field.of("b", Schema.Type.STRING), Schema.Field.of("c", Schema.Type.ARRAY), Schema.Field.of("d", Schema.Type.LONG), Schema.Field.of("e", Schema.Type.DOUBLE));
    Map<String, Object> value = new HashMap<>();
    value.put("a", new byte[] { 0x01, 0x02 });
    value.put("b", "hello");
    value.put("c", Collections.singletonList("hello"));
    value.put("d", 1234);
    value.put("e", 123.456f);
    Map<String, Object> converted = SchemaValueConverter.convertMap(schema, value);
    Assert.assertEquals(value.size(), converted.size());
    Assert.assertTrue(Schema.Type.BINARY.valueOfSameType(converted.get("a")));
    Assert.assertTrue(Arrays.equals(new byte[] { 0x01, 0x02 }, (byte[]) converted.get("a")));
    Assert.assertTrue(Schema.Type.STRING.valueOfSameType(converted.get("b")));
    Assert.assertEquals("hello", converted.get("b"));
    Assert.assertTrue(Schema.Type.ARRAY.valueOfSameType(converted.get("c")));
    Assert.assertEquals(Collections.singletonList("hello"), converted.get("c"));
    Assert.assertTrue(Schema.Type.LONG.valueOfSameType(converted.get("d")));
    Assert.assertEquals(1234L, converted.get("d"));
    Assert.assertTrue(Schema.Type.DOUBLE.valueOfSameType(converted.get("e")));
    Assert.assertEquals(123.456d, (double) converted.get("e"), 0.001);
}
Also used : HashMap(java.util.HashMap) Schema(com.hortonworks.registries.common.Schema) Test(org.junit.Test)

Example 14 with Schema

use of com.hortonworks.registries.common.Schema in project streamline by hortonworks.

the class SchemaValueConverterTest method convertMapHavingNull.

@Test(expected = SchemaValidationFailedException.class)
public void convertMapHavingNull() throws Exception {
    Schema schema = Schema.of(Schema.Field.of("a", Schema.Type.STRING), Schema.Field.of("b", Schema.Type.LONG));
    Map<String, Object> value = new HashMap<>();
    value.put("a", null);
    value.put("b", 1234);
    SchemaValueConverter.convertMap(schema, value);
}
Also used : HashMap(java.util.HashMap) Schema(com.hortonworks.registries.common.Schema) Test(org.junit.Test)

Example 15 with Schema

use of com.hortonworks.registries.common.Schema in project streamline by hortonworks.

the class SchemaTest method testSerializeNestedSchema.

@Test
public void testSerializeNestedSchema() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    Schema nested = new Schema();
    Schema.Field f1 = new Schema.Field("field1", Type.INTEGER);
    Schema.Field x = new Schema.Field("x", Type.INTEGER);
    Schema.Field y = new Schema.Field("y", Type.INTEGER);
    Schema.Field f2 = Schema.NestedField.of("field2", Arrays.asList(x, y));
    nested.setFields(Arrays.asList(f1, f2));
    String expected = "{\"fields\":[{\"name\":\"field1\",\"type\":\"INTEGER\",\"optional\":false}," + "{\"name\":\"field2\",\"type\":\"NESTED\",\"optional\":false,\"fields\":[{\"name\":\"x\",\"type\":\"INTEGER\"," + "\"optional\":false},{\"name\":\"y\",\"type\":\"INTEGER\",\"optional\":false}]}]}";
    assertEquals(expected, mapper.writeValueAsString(nested));
    Schema schema2;
    schema2 = mapper.readValue(expected, Schema.class);
    assertEquals(nested, schema2);
}
Also used : Schema(com.hortonworks.registries.common.Schema) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

Schema (com.hortonworks.registries.common.Schema)17 HashMap (java.util.HashMap)9 Test (org.junit.Test)8 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 ArrayList (java.util.ArrayList)4 IOException (java.io.IOException)3 Map (java.util.Map)3 Stream (com.hortonworks.streamline.streams.layout.component.Stream)2 BulkNormalizationConfig (com.hortonworks.streamline.streams.layout.component.impl.normalization.BulkNormalizationConfig)2 FieldBasedNormalizationConfig (com.hortonworks.streamline.streams.layout.component.impl.normalization.FieldBasedNormalizationConfig)2 NormalizationConfig (com.hortonworks.streamline.streams.layout.component.impl.normalization.NormalizationConfig)2 NormalizationProcessor (com.hortonworks.streamline.streams.layout.component.impl.normalization.NormalizationProcessor)2 InputStream (java.io.InputStream)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 Predicate (com.google.common.base.Predicate)1 ParserException (com.hortonworks.registries.common.exception.ParserException)1 SchemaFieldInfo (com.hortonworks.registries.schemaregistry.SchemaFieldInfo)1