Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void serialize(JsonProperties.Null value, JsonGenerator gen, SerializerPr
module.addSerializer(new StdSerializer<byte[]>(byte[].class) {
@Override
public void serialize(byte[] value, JsonGenerator gen, SerializerProvider provider) throws IOException {
MAPPER.writeValueAsString(new String(value, StandardCharsets.ISO_8859_1));
gen.writeString(new String(value, StandardCharsets.ISO_8859_1));
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@
import static java.util.Objects.requireNonNull;
import static org.junit.jupiter.api.Assertions.assertEquals;

class IdlSchemaFormatterFactoryTest {
class TestIdlSchemaFormatterFactory {
@Test
void verifyIdlFormatting() throws IOException {
SchemaFormatter idlFormatter = SchemaFormatter.getInstance("idl");
assertEquals(IdlSchemaFormatter.class, idlFormatter.getClass());

String formattedHappyFlowSchema = getResourceAsString("../util/idl_utils_test_schema.avdl");
String schemaResourceName = "../idl/idl_utils_test_schema.avdl";
String formattedHappyFlowSchema = getResourceAsString(schemaResourceName);

String schemaResourceName = "../util/idl_utils_test_schema.avdl";
try (InputStream stream = getClass().getResourceAsStream(schemaResourceName)) {
Schema happyFlowSchema = new SchemaParser().parse(formattedHappyFlowSchema).mainSchema();
// The Avro project indents .avdl files less than common
String formatted = idlFormatter.format(happyFlowSchema).replaceAll(" ", "\t").replaceAll("\t", " ");
assertEquals(formattedHappyFlowSchema, formatted);
String formatted = idlFormatter.format(happyFlowSchema);
assertEquals(formatted.replace("\r", ""), formattedHappyFlowSchema.replace("\r", ""));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.LinkedHashMap;
import java.util.Map;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JacksonException;
import org.apache.avro.AvroRuntimeException;
import org.apache.avro.JsonProperties;
import org.apache.avro.Protocol;
Expand All @@ -39,7 +39,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class IdlUtilsTest {
public class TestIdlUtils {
@Test
public void idlUtilsUtilitiesThrowRuntimeExceptionsOnProgrammerError() {
assertThrows(IllegalStateException.class, () -> IdlUtils.getField(Object.class, "noSuchField"), "Programmer error");
Expand All @@ -62,7 +62,8 @@ public void validateHappyFlowForProtocol() throws IOException {
StringWriter buffer = new StringWriter();
IdlUtils.writeIdlProtocol(buffer, protocol);

assertEquals(getResourceAsString("idl_utils_test_protocol.avdl"), buffer.toString());
assertEquals(getResourceAsString("idl_utils_test_protocol.avdl").replace("\r", ""),
buffer.toString().replace("\r", ""));
}

private IdlFile parseIdlResource(String name) throws IOException {
Expand Down Expand Up @@ -92,9 +93,10 @@ public void validateHappyFlowForSingleSchema() throws IOException {
Schema mainSchema = idlFile.getMainSchema();

StringWriter buffer = new StringWriter();
IdlUtils.writeIdlSchema(buffer, mainSchema.getTypes().iterator().next());
IdlUtils.writeIdlSchema(buffer, mainSchema);

assertEquals(getResourceAsString("idl_utils_test_schema.avdl"), buffer.toString());
assertEquals(getResourceAsString("idl_utils_test_schema.avdl").replace("\r", ""),
buffer.toString().replace("\r", ""));
}

@Test
Expand Down Expand Up @@ -126,12 +128,12 @@ public void validateMapToJson() throws IOException {
Map<String, Object> data = new LinkedHashMap<>();
data.put("key", "name");
data.put("value", 81763);
assertEquals("{\"key\":\"name\",\"value\":81763}", callToJson(data));
assertEquals("{\"key\":\"name\", \"value\":81763}", callToJson(data));
}

@Test
public void validateCollectionToJson() throws IOException {
assertEquals("[123,\"abc\"]", callToJson(Arrays.asList(123, "abc")));
assertEquals("[123, \"abc\"]", callToJson(Arrays.asList(123, "abc")));
}

@Test
Expand Down Expand Up @@ -174,17 +176,18 @@ public void validateBooleanToJson() throws IOException {
assertEquals("true", callToJson(true));
}

@Test
public void validateByteArrayToJson() throws IOException {
assertEquals("\"123.456\"", callToJson("123.456".getBytes(StandardCharsets.ISO_8859_1)));
}

@Test
public void validateUnknownCannotBeWrittenAsJson() {
assertThrows(AvroRuntimeException.class, () -> callToJson(new Object()));
assertThrows(JacksonException.class, () -> callToJson(new Object()));
}

private String callToJson(Object datum) throws IOException {
StringWriter buffer = new StringWriter();
try (JsonGenerator generator = IdlUtils.MAPPER.createGenerator(buffer)) {
IdlUtils.MAPPER.writeValueAsString(datum);
}
return buffer.toString();
return IdlUtils.MAPPER.writeValueAsString(datum);
}

private enum SingleValue {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ protocol HappyFlow {
@aliases(["naming.OldMessage"])
record NewMessage {
string @generator("uuid-type1") id;
@my-key("my-value") string? @aliases(["text","msg"]) message = null;
@my-key("my-value") string? @aliases(["text", "msg"]) message = null;
@my-key("my-value") map<common.Flag> @order("DESCENDING") flags;
Counter mainCounter;
/** A list of counters. */
Expand All @@ -20,7 +20,9 @@ protocol HappyFlow {
}

@namespace("common")
enum Flag {ON, OFF, CANARY}
enum Flag {
ON, OFF, CANARY
}

record Counter {
string name;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace naming;

schema NewMessage;

/** A sample record type. */
@version(2)
@aliases(["naming.OldMessage"])
record NewMessage {
string @generator("uuid-type1") id;
@my-key("my-value") string? @aliases(["text", "msg"]) message = null;
@my-key("my-value") map<common.Flag> @order("DESCENDING") flags;
Counter mainCounter;
/** A list of counters. */
union{null, @my-key("my-value") array<Counter>} otherCounters = null;
Nonce nonce;
date my_date;
time_ms my_time;
timestamp_ms my_timestamp;
decimal(12,3) my_number = "123.456";
@logicalType("time-micros") long my_dummy;
}

@namespace("common")
enum Flag {
ON, OFF, CANARY
}

record Counter {
string name;
int count;
/** Because the Flag field is defined earlier in NewMessage, it's already defined and does not need repeating below. */
common.Flag flag;
}

fixed Nonce(8);

This file was deleted.