### Question:
SetSchemaMetadata implements Transformation<R> { protected static Object updateSchemaIn(Object keyOrValue, Schema updatedSchema) { if (keyOrValue instanceof Struct) { Struct origStruct = (Struct) keyOrValue; Struct newStruct = new Struct(updatedSchema); for (Field field : updatedSchema.fields()) { newStruct.put(field, origStruct.get(field)); } return newStruct; } return keyOrValue; }  @Override void configure(Map<String, ?> configs); @Override R apply(R record); @Override ConfigDef config(); @Override void close(); static final String OVERVIEW_DOC; static final ConfigDef CONFIG_DEF; }### Answer:
@Test public void updateSchemaOfNonStruct() { Object value = new Integer(1); Object updatedValue = SetSchemaMetadata.updateSchemaIn(value, Schema.INT32_SCHEMA); assertSame(value, updatedValue); }
@Test public void updateSchemaOfStruct() { final String fieldName1 = "f1"; final String fieldName2 = "f2"; final String fieldValue1 = "value1"; final int fieldValue2 = 1; final Schema schema = SchemaBuilder.struct() .name("my.orig.SchemaDefn") .field(fieldName1, Schema.STRING_SCHEMA) .field(fieldName2, Schema.INT32_SCHEMA) .build(); final Struct value = new Struct(schema).put(fieldName1, fieldValue1).put(fieldName2, fieldValue2); final Schema newSchema = SchemaBuilder.struct() .name("my.updated.SchemaDefn") .field(fieldName1, Schema.STRING_SCHEMA) .field(fieldName2, Schema.INT32_SCHEMA) .build(); Struct newValue = (Struct) SetSchemaMetadata.updateSchemaIn(value, newSchema); assertMatchingSchema(newValue, newSchema); }
@Test public void updateSchemaOfNonStruct() { Object value = Integer.valueOf(1); Object updatedValue = SetSchemaMetadata.updateSchemaIn(value, Schema.INT32_SCHEMA); assertSame(value, updatedValue); }
@Test public void updateSchemaOfNull() { Object updatedValue = SetSchemaMetadata.updateSchemaIn(null, Schema.INT32_SCHEMA); assertEquals(null, updatedValue); }