use of org.apache.kafka.connect.data.Field in project connect-utils by jcustenborder.
the class AssertStruct method assertStruct.
public static void assertStruct(final Struct expected, final Struct actual, String message) {
String prefix = Strings.isNullOrEmpty(message) ? "" : message + ": ";
if (null == expected) {
assertNull(actual, prefix + "actual should be null.");
return;
}
assertSchema(expected.schema(), actual.schema(), "schema does not match.");
final Set<String> logicalTypes = ImmutableSet.of(Timestamp.LOGICAL_NAME, Date.LOGICAL_NAME, Time.LOGICAL_NAME, Decimal.LOGICAL_NAME);
for (Field field : expected.schema().fields()) {
log.trace("assertStruct() - testing field '{}'", field.name());
final Object expectedValue = expected.get(field.name());
final Object actualValue = actual.get(field.name());
if (!Strings.isNullOrEmpty(field.schema().name()) && logicalTypes.contains(field.schema().name())) {
assertEquals(expectedValue, actualValue, prefix + field.name() + " does not match.");
} else {
switch(field.schema().type()) {
case ARRAY:
assertTrue(null == expectedValue || expectedValue instanceof List);
assertTrue(null == actualValue || actualValue instanceof List);
List<Object> expectedArray = (List<Object>) expectedValue;
List<Object> actualArray = (List<Object>) actualValue;
assertEquals(expectedArray, actualArray, prefix + field.name() + " does not match.");
break;
case MAP:
assertTrue(null == expectedValue || expectedValue instanceof Map);
assertTrue(null == actualValue || actualValue instanceof Map);
Map<Object, Object> expectedMap = (Map<Object, Object>) expectedValue;
Map<Object, Object> actualMap = (Map<Object, Object>) actualValue;
assertEquals(expectedMap, actualMap, prefix + field.name() + " does not match.");
break;
case STRUCT:
assertTrue(null == expectedValue || expectedValue instanceof Struct);
assertTrue(null == actualValue || actualValue instanceof Struct);
Struct expectedStruct = (Struct) expectedValue;
Struct actualStruct = (Struct) actualValue;
assertStruct(expectedStruct, actualStruct, prefix + field.name() + " does not match.");
break;
case BYTES:
assertTrue(null == expectedValue || expectedValue instanceof byte[]);
assertTrue(null == actualValue || actualValue instanceof byte[]);
byte[] expectedByteArray = (byte[]) expectedValue;
byte[] actualByteArray = (byte[]) actualValue;
assertEquals(null == expectedByteArray ? "" : BaseEncoding.base32Hex().encode(expectedByteArray).toString(), null == actualByteArray ? "" : BaseEncoding.base32Hex().encode(actualByteArray).toString(), prefix + field.name() + " does not match.");
break;
default:
assertEquals(expectedValue, actualValue, prefix + field.name() + " does not match.");
break;
}
}
}
}
use of org.apache.kafka.connect.data.Field in project connect-utils by jcustenborder.
the class AssertSchema method assertSchema.
public static void assertSchema(final Schema expected, final Schema actual, String message) {
final String prefix = Strings.isNullOrEmpty(message) ? "" : message + ": ";
if (null == expected) {
assertNull(actual, prefix + "actual should not be null.");
return;
}
assertNotNull(expected, prefix + "expected schema should not be null.");
assertNotNull(actual, prefix + "actual schema should not be null.");
assertEquals(expected.name(), actual.name(), prefix + "schema.name() should match.");
assertEquals(expected.type(), actual.type(), prefix + "schema.type() should match.");
assertEquals(expected.defaultValue(), actual.defaultValue(), prefix + "schema.defaultValue() should match.");
assertEquals(expected.isOptional(), actual.isOptional(), prefix + "schema.isOptional() should match.");
assertEquals(expected.doc(), actual.doc(), prefix + "schema.doc() should match.");
assertEquals(expected.version(), actual.version(), prefix + "schema.version() should match.");
assertMap(expected.parameters(), actual.parameters(), prefix + "schema.parameters() should match.");
if (null != expected.defaultValue()) {
assertNotNull(actual.defaultValue(), "actual.defaultValue() should not be null.");
Class<?> expectedType = null;
switch(expected.type()) {
case INT8:
expectedType = Byte.class;
break;
case INT16:
expectedType = Short.class;
break;
case INT32:
expectedType = Integer.class;
break;
case INT64:
expectedType = Long.class;
break;
case FLOAT32:
expectedType = Float.class;
break;
case FLOAT64:
expectedType = Float.class;
break;
default:
break;
}
if (null != expectedType) {
assertTrue(actual.defaultValue().getClass().isAssignableFrom(expectedType), String.format("actual.defaultValue() should be a %s", expectedType.getSimpleName()));
}
}
switch(expected.type()) {
case ARRAY:
assertSchema(expected.valueSchema(), actual.valueSchema(), message + "valueSchema does not match.");
break;
case MAP:
assertSchema(expected.keySchema(), actual.keySchema(), message + "keySchema does not match.");
assertSchema(expected.valueSchema(), actual.valueSchema(), message + "valueSchema does not match.");
break;
case STRUCT:
List<Field> expectedFields = expected.fields();
List<Field> actualFields = actual.fields();
assertEquals(expectedFields.size(), actualFields.size(), prefix + "Number of fields do not match.");
for (int i = 0; i < expectedFields.size(); i++) {
Field expectedField = expectedFields.get(i);
Field actualField = actualFields.get(i);
assertField(expectedField, actualField, "index " + i);
}
break;
}
}
use of org.apache.kafka.connect.data.Field in project connect-utils by jcustenborder.
the class MarkdownFormatter method toMarkdown.
public static String toMarkdown(Schema schema) {
Preconditions.checkNotNull(schema, "schema cannot be null.");
StringBuilder builder = new StringBuilder();
builder.append("## ");
if (Strings.isNullOrEmpty(schema.name())) {
builder.append(schema.type());
} else {
builder.append(schema.name());
}
if (!Strings.isNullOrEmpty(schema.doc())) {
builder.append("\n\n");
builder.append(schema.doc());
}
if (Schema.Type.STRUCT == schema.type()) {
List<List<String>> rows = new ArrayList<>();
rows.add(ImmutableList.of("Name", "Optional", "Schema", "Default Value", "Documentation"));
for (Field field : schema.fields()) {
Schema fieldSchema = field.schema();
List<String> row = ImmutableList.of(field.name(), String.valueOf(fieldSchema.isOptional()), schema(fieldSchema), fieldSchema.defaultValue() == null ? "" : fieldSchema.defaultValue().toString(), Strings.isNullOrEmpty(fieldSchema.doc()) ? "" : fieldSchema.doc());
rows.add(row);
}
builder.append("\n\n");
builder.append(markdownTable(rows));
} else if (Schema.Type.MAP == schema.type()) {
} else {
}
return builder.toString();
}
use of org.apache.kafka.connect.data.Field in project ksql by confluentinc.
the class JoinNodeTest method shouldHaveAllFieldsFromJoinedInputs.
@Test
public void shouldHaveAllFieldsFromJoinedInputs() {
setupTopicClientExpectations(1, 1);
buildJoin();
final MetaStore metaStore = MetaStoreFixture.getNewMetaStore();
final StructuredDataSource source1 = metaStore.getSource("TEST1");
final StructuredDataSource source2 = metaStore.getSource("TEST2");
final Set<String> expected = source1.getSchema().fields().stream().map(field -> "T1." + field.name()).collect(Collectors.toSet());
expected.addAll(source2.getSchema().fields().stream().map(field -> "T2." + field.name()).collect(Collectors.toSet()));
final Set<String> fields = stream.getSchema().fields().stream().map(Field::name).collect(Collectors.toSet());
assertThat(fields, equalTo(expected));
}
use of org.apache.kafka.connect.data.Field in project ksql by confluentinc.
the class KsqlStructuredDataOutputNodeTest method shouldPartitionByFieldNameInPartitionByProperty.
@Test
public void shouldPartitionByFieldNameInPartitionByProperty() {
createOutputNode(Collections.singletonMap(DdlConfig.PARTITION_BY_PROPERTY, "field2"));
final SchemaKStream schemaKStream = buildStream();
final Field keyField = schemaKStream.getKeyField();
assertThat(keyField, equalTo(new Field("field2", 1, Schema.STRING_SCHEMA)));
assertThat(schemaKStream.getSchema().fields(), equalTo(schema.fields()));
}
Aggregations