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);
}
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"));
}
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);
}
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);
}
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);
}
Aggregations