use of co.cask.cdap.internal.io.ReflectionSchemaGenerator in project cdap by caskdata.
the class ScheduleSpecificationCodecTest method testAppConfigurerRoute.
@Test
public void testAppConfigurerRoute() throws Exception {
Application app = new AbstractApplication() {
@Override
public void configure() {
// intentionally use the deprecated scheduleWorkflow method to for timeSchedule
// to test TimeSchedule deserialization
scheduleWorkflow(Schedules.builder("timeSchedule").createTimeSchedule("0 * * * *"), "workflow");
scheduleWorkflow(Schedules.builder("streamSizeSchedule").createDataSchedule(Schedules.Source.STREAM, "stream", 1), "workflow");
}
};
ApplicationSpecification specification = Specifications.from(app);
ApplicationSpecificationAdapter gsonAdapater = ApplicationSpecificationAdapter.create(new ReflectionSchemaGenerator());
String jsonStr = gsonAdapater.toJson(specification);
ApplicationSpecification deserializedSpec = gsonAdapater.fromJson(jsonStr);
Assert.assertEquals(new TimeSchedule("timeSchedule", "", "0 * * * *"), deserializedSpec.getSchedules().get("timeSchedule").getSchedule());
Assert.assertEquals(new StreamSizeSchedule("streamSizeSchedule", "", "stream", 1), deserializedSpec.getSchedules().get("streamSizeSchedule").getSchedule());
}
use of co.cask.cdap.internal.io.ReflectionSchemaGenerator in project cdap by caskdata.
the class RecordWithString method testEmptyValue.
// this tests that the datum reader treats empty fields correctly. It reproduces the issue in ENG-2404.
@Test
public void testEmptyValue() throws UnsupportedTypeException, IOException {
Schema schema = new ReflectionSchemaGenerator().generate(RecordWithString.class);
TypeRepresentation typeRep = new TypeRepresentation(RecordWithString.class);
DatumWriter<RecordWithString> datumWriter = new ReflectionDatumWriter<>(schema);
@SuppressWarnings("unchecked") ReflectionDatumReader<RecordWithString> datumReader = new ReflectionDatumReader<>(schema, (TypeToken<RecordWithString>) TypeToken.of(typeRep.toType()));
RecordWithString record = new RecordWithString();
record.setA(42);
record.setTheString("");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
BinaryEncoder encoder = new BinaryEncoder(bos);
datumWriter.encode(record, encoder);
byte[] bytes = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
BinaryDecoder decoder = new BinaryDecoder(bis);
RecordWithString rec = datumReader.read(decoder, schema);
Assert.assertEquals(record.getA(), rec.getA());
Assert.assertEquals(record.getTheString(), rec.getTheString());
}
use of co.cask.cdap.internal.io.ReflectionSchemaGenerator in project cdap by caskdata.
the class RecordWithString method testReduceProjection.
@Test
public void testReduceProjection() throws IOException, UnsupportedTypeException {
PipedOutputStream output = new PipedOutputStream();
PipedInputStream input = new PipedInputStream(output);
Schema sourceSchema = new ReflectionSchemaGenerator().generate(MoreFields.class);
Schema targetSchema = new ReflectionSchemaGenerator().generate(LessFields.class);
MoreFields moreFields = new MoreFields(10, 20.2, "30", ImmutableList.of("1", "2"));
new ReflectionDatumWriter<MoreFields>(sourceSchema).encode(moreFields, new BinaryEncoder(output));
LessFields lessFields = new ReflectionDatumReader<>(targetSchema, TypeToken.of(LessFields.class)).read(new BinaryDecoder(input), sourceSchema);
Assert.assertEquals("30", lessFields.k);
Assert.assertEquals(moreFields.inner.b, lessFields.inner.b);
}
use of co.cask.cdap.internal.io.ReflectionSchemaGenerator in project cdap by caskdata.
the class SchemaTest method testGenerateSchema.
@Test
public void testGenerateSchema() throws UnsupportedTypeException {
Schema schema = (new ReflectionSchemaGenerator()).generate((new TypeToken<Child<Node>>() {
}).getType());
Gson gson = new GsonBuilder().registerTypeAdapter(Schema.class, new SchemaTypeAdapter()).create();
Assert.assertEquals(schema, gson.fromJson(gson.toJson(schema), Schema.class));
}
use of co.cask.cdap.internal.io.ReflectionSchemaGenerator in project cdap by caskdata.
the class SchemaTest method testParseJson.
@Test
public void testParseJson() throws IOException, UnsupportedTypeException {
Schema schema = new ReflectionSchemaGenerator().generate(Node.class);
Assert.assertEquals(schema, Schema.parseJson(schema.toString()));
}
Aggregations