SchemaValueConverter { public static Map<String, Object> convertMap(Schema schema, Map<String, Object> value) { Map<String, Schema.Type> fieldToType = schema.getFields().stream() .collect(toMap(Schema.Field::getName, Schema.Field::getType)); List<Schema.Field> requiredFields = schema.getFields().stream() .filter(field -> !field.isOptional()).collect(toList()); List<String> fieldsNotFoundInSchema = value.keySet().stream() .filter(f -> !(fieldToType.containsKey(f))).collect(toList()); List<Schema.Field> requiredFieldsNotFoundInValue = requiredFields.stream() .filter(f -> !(value.containsKey(f.getName()))).collect(toList()); if (!fieldsNotFoundInSchema.isEmpty()) { throw SchemaValidationFailedException.fieldsNotFoundInSchema(fieldsNotFoundInSchema); } if (!requiredFieldsNotFoundInValue.isEmpty()) { throw SchemaValidationFailedException.requiredFieldsNotFoundInValue( requiredFieldsNotFoundInValue.stream().map(Schema.Field::getName).collect(toList())); } Map<String, Object> result = new HashMap<>(); value.forEach((k, v) -> { if (v == null) { throw SchemaValidationFailedException.nullValueForField(k); } result.put(k, convert(fieldToType.get(k), v)); }); return result; } private  SchemaValueConverter(); static Map<String, Object> convertMap(Schema schema, Map<String, Object> value); static Object convert(Schema.Type type, Object value);  }
@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); }
@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); }
@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")); }
@Test(expected = SchemaValidationFailedException.class) public void convertMapValueDoesNotHaveRequiredField() { 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)); Map<String, Object> value = new HashMap<>(); value.put("a", new byte[] { 0x01, 0x02 }); value.put("c", Collections.singletonList("hello")); SchemaValueConverter.convertMap(schema, value); }
@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); }