/** * Print a message with the JSON representation of the SourceRecord. * * @param record the source record; may not be null */ public static void print(SourceRecord record) { Testing.print(SchemaUtil.asString(record)); }
protected static void assertEquals(Object o1, Object o2) { // assertThat(o1).isEqualTo(o2); if (o1 instanceof Schema && o2 instanceof Schema) { assertConnectSchemasAreEqual(null, (Schema) o1, (Schema) o2); } else if (!equals(o1, o2)) { fail(SchemaUtil.asString(o1) + " was not equal to " + SchemaUtil.asString(o2)); } }
@Test @FixFor("DBZ-759") public void correctlySerializesByteBuffer() { final ByteBuffer buffer = ByteBuffer.allocate(3); buffer.put(new byte[]{11, 13, 17}); assertThat(SchemaUtil.asString(buffer)).isEqualTo("[11, 13, 17]"); }
/** * Asserts that the two given schemas are equal. * * @param fieldName * name of the field owning that schema, if it's not a top-level schema * @param actual * the actual schema * @param expected * the expected schema */ public static void assertConnectSchemasAreEqual(String fieldName, Schema actual, Schema expected) { if (!areConnectSchemasEqual(actual, expected)) { // first try failing with an assertion message that shows the actual difference assertThat(SchemaUtil.asString(actual)).describedAs("field name: " + fieldName).isEqualTo(SchemaUtil.asString(expected)); // compare schema parameters assertThat(actual.parameters()).describedAs("field '" + fieldName + "' parameters").isEqualTo(expected.parameters()); // fall-back just in case (e.g. differences of element schemas of arrays) fail("field '" + fieldName + "': " + SchemaUtil.asString(actual) + " was not equal to " + SchemaUtil.asString(expected)); } }
/** * Verify the given {@link SourceRecord} has the expected value in the given fieldPath. * * @param record the source record; may not be null * @param fieldPath the field path to validate, separated by '/' * @param expectedValue the expected value in the source records field path. */ public static void assertValueField(SourceRecord record, String fieldPath, Object expectedValue) { Object value = record.value(); String[] fieldNames = fieldPath.split("/"); String pathSoFar = null; for (int i=0; i!=fieldNames.length; ++i) { String fieldName = fieldNames[i]; if (value instanceof Struct) { value = ((Struct)value).get(fieldName); } else { // We expected the value to be a struct ... String path = pathSoFar == null ? "record value" : ("'" + pathSoFar + "'"); String msg = "Expected the " + path + " to be a Struct but was " + value.getClass().getSimpleName() + " in record: " + SchemaUtil.asString(record); fail(msg); } pathSoFar = pathSoFar == null ? fieldName : pathSoFar + "/" + fieldName; } assertSameValue(value,expectedValue); }
@Test @FixFor("DBZ-759") public void correctlySerializesByteArray() { assertThat(SchemaUtil.asString(new byte[]{1, 3, 5, 7})).isEqualTo("[1, 3, 5, 7]"); }
private void assertSchema(Struct content) { if (schema == null) { return; } Schema schema = content.schema(); Field field = schema.field(fieldName); assertNotNull(fieldName + " not found in schema " + SchemaUtil.asString(schema), field); VerifyRecord.assertConnectSchemasAreEqual(field.name(), field.schema(), this.schema); } }
private void applyCommand(Document record, ConsumerCompletion result) { if (isCommand(record)) { Testing.debug("applying command: " + SchemaUtil.asString(record)); String command = record.getString(CONTROL_KEY); if (CONTROL_RESTART.equalsIgnoreCase(command)) { // We're supposed to restart the connector, so stop it ... result.restartRequested(); String msg = "Stopping connector after record as requested"; Testing.debug(msg); throw new StopConnectorException(msg); } else if (CONTROL_STOP.equalsIgnoreCase(command)) { // We're supposed to restart the connector, so stop it ... result.stop(); String msg = "Stopping connector after record as requested"; Testing.debug(msg); throw new StopConnectorException(msg); } } }
if (o1 == null) { if (o2 == null) return; fail(nameOf(keyOrValue, field) + " was null but expected " + SchemaUtil.asString(o2)); fail("expecting a null " + nameOf(keyOrValue, field) + " but found " + SchemaUtil.asString(o1)); Collection<Object> m2 = (Collection<Object>) o2; if (m1.size() != m2.size()) { fail("Collection at " + nameOf(keyOrValue, field) + " has " + SchemaUtil.asString(m1) + " but expected " + SchemaUtil.asString(m2)); Struct struct2 = (Struct) o2; if (!Objects.equals(struct1.schema(), struct2.schema())) { fail("Schema at " + nameOf(keyOrValue, field) + " is " + SchemaUtil.asString(struct1.schema()) + " but expected " + SchemaUtil.asString(struct2.schema()));
public static void assertEquals(SourceRecord actual, SourceRecord expected, Predicate<String> ignoreFields, Map<String, RecordValueComparator> comparatorsByName, Map<String, RecordValueComparator> comparatorsBySchemaName) { assertThat(actual).isNotNull(); assertThat(expected).isNotNull(); assertEquals(null, actual.sourcePartition(), expected.sourcePartition(), "sourcePartition", "", ignoreFields, comparatorsByName, comparatorsBySchemaName); assertEquals(null, actual.sourceOffset(), expected.sourceOffset(), "sourceOffset", "", ignoreFields, comparatorsByName, comparatorsBySchemaName); assertThat(actual.topic()).isEqualTo(expected.topic()); assertThat(actual.kafkaPartition()).isEqualTo(expected.kafkaPartition()); Schema actualKeySchema = actual.keySchema(); Schema actualValueSchema = actual.valueSchema(); Schema expectedKeySchema = expected.keySchema(); Schema expectedValueSchema = expected.valueSchema(); if (!Objects.equals(actualKeySchema, expectedKeySchema)) { String actualStr = SchemaUtil.asString(actualKeySchema); String expectedStr = SchemaUtil.asString(expectedKeySchema); assertThat(actualStr).as("The key schema for record with key " + SchemaUtil.asString(actual.key()) + " did not match expected schema").isEqualTo(expectedStr); } if (!Objects.equals(actualValueSchema, expectedValueSchema)) { String actualStr = SchemaUtil.asString(actualValueSchema); String expectedStr = SchemaUtil.asString(expectedValueSchema); assertThat(actualStr).isEqualTo(expectedStr); } assertEquals(actualKeySchema, actual.key(), expected.key(), "key", "", ignoreFields, comparatorsByName, comparatorsBySchemaName); assertEquals(actualValueSchema, actual.value(), expected.value(), "value", "", ignoreFields, comparatorsByName, comparatorsBySchemaName); }
@Test @FixFor("DBZ-759") public void correctlySerializesStructWithByteArray() { Schema schema = SchemaBuilder.struct() .field("some_field", SchemaBuilder.bytes().build()) .build(); Struct struct = new Struct(schema).put("some_field", new byte[]{1, 3, 5, 7}); assertThat(SchemaUtil.asString(struct)).isEqualTo("{\"some_field\" : [1, 3, 5, 7]}"); } }
PreviousContext prev = LoggingContext.forConnector(getClass().getSimpleName(), "runner", spec.name()); try { Testing.debug("actual record: " + SchemaUtil.asString(actualRecord)); timeSinceLastRecord.reset(); if (isEndCommand(expected)) { result.error(); String msg = "Source record was found but not expected: " + SchemaUtil.asString(actualRecord); Testing.debug(msg); throw new MismatchRecordException(msg, actualRecordHistory, expectedRecordHistory); } else if (isCommand(expected)) { Testing.debug("applying command: " + SchemaUtil.asString(expected)); applyCommand(expected, result); } else { Testing.debug("expected record: " + SchemaUtil.asString(expectedRecord)); } catch (AssertionError e) { result.error(); String msg = "Source record with key " + SchemaUtil.asString(actualRecord.key()) + " did not match expected record: " + e.getMessage(); Testing.debug(msg); Testing.print(""); Testing.print("FAILURE in connector integration test '" + spec.name() + "' in class " + getClass()); Testing.print(" actual record: " + SchemaUtil.asString(actualHistory.getLast())); Testing.print(" expected record: " + SchemaUtil.asString(expectedHistory.getLast())); Testing.print(mismatch.getMessage()); Testing.print("");
Testing.printError(t); Testing.print("error " + msg); Testing.print(" key: " + SchemaUtil.asString(record.key())); Testing.print(" key deserialized from JSON: " + prettyJson(keyJson)); if (keyWithSchema != null) { Testing.print(" key to/from JSON: " + SchemaUtil.asString(keyWithSchema.value())); Testing.print(" key to/from Avro: " + SchemaUtil.asString(avroKeyWithSchema.value())); Testing.print(" value: " + SchemaUtil.asString(record.value())); Testing.print(" value deserialized from JSON: " + prettyJson(valueJson)); if (valueWithSchema != null) { Testing.print(" value to/from JSON: " + SchemaUtil.asString(valueWithSchema.value())); Testing.print(" value to/from Avro: " + SchemaUtil.asString(avroValueWithSchema.value()));
private void assertSchema(Struct content) { if (schema == null) { return; } Schema schema = content.schema(); Field field = schema.field(fieldName); assertNotNull(fieldName + " not found in schema " + SchemaUtil.asString(schema), field); VerifyRecord.assertConnectSchemasAreEqual(field.name(), field.schema(), this.schema); } }
private void applyCommand(Document record, ConsumerCompletion result) { if (isCommand(record)) { Testing.debug("applying command: " + SchemaUtil.asString(record)); String command = record.getString(CONTROL_KEY); if (CONTROL_RESTART.equalsIgnoreCase(command)) { // We're supposed to restart the connector, so stop it ... result.restartRequested(); String msg = "Stopping connector after record as requested"; Testing.debug(msg); throw new StopConnectorException(msg); } else if (CONTROL_STOP.equalsIgnoreCase(command)) { // We're supposed to restart the connector, so stop it ... result.stop(); String msg = "Stopping connector after record as requested"; Testing.debug(msg); throw new StopConnectorException(msg); } } }
PreviousContext prev = LoggingContext.forConnector(getClass().getSimpleName(), "runner", spec.name()); try { Testing.debug("actual record: " + SchemaUtil.asString(actualRecord)); timeSinceLastRecord.reset(); if (isEndCommand(expected)) { result.error(); String msg = "Source record was found but not expected: " + SchemaUtil.asString(actualRecord); Testing.debug(msg); throw new MismatchRecordException(msg, actualRecordHistory, expectedRecordHistory); } else if (isCommand(expected)) { Testing.debug("applying command: " + SchemaUtil.asString(expected)); applyCommand(expected, result); } else { Testing.debug("expected record: " + SchemaUtil.asString(expectedRecord)); } catch (AssertionError e) { result.error(); String msg = "Source record with key " + SchemaUtil.asString(actualRecord.key()) + " did not match expected record: " + e.getMessage(); Testing.debug(msg); Testing.print(""); Testing.print("FAILURE in connector integration test '" + spec.name() + "' in class " + getClass()); Testing.print(" actual record: " + SchemaUtil.asString(actualHistory.getLast())); Testing.print(" expected record: " + SchemaUtil.asString(expectedHistory.getLast())); Testing.print(mismatch.getMessage()); Testing.print("");