public class TestUtils {
private static final ObjectMapper mapper = new ObjectMapper();
private static final ObjectMapper snsEventMapper = new ObjectMapper();
private static final ObjectMapper dynamodbEventMapper = new ObjectMapper();
static {
mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
mapper.setPropertyNamingStrategy(new UpperCaseRecordsPropertyNamingStrategy());
mapper.registerModule(new TestJacksonMapperModule());
snsEventMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
snsEventMapper.setPropertyNamingStrategy(PropertyNamingStrategy.PASCAL_CASE_TO_CAMEL_CASE);
snsEventMapper.registerModule(new TestJacksonMapperModule());
dynamodbEventMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
dynamodbEventMapper.setPropertyNamingStrategy(new UpperCaseRecordsPropertyNamingStrategy());
dynamodbEventMapper.registerModule(new TestJacksonMapperModule());
dynamodbEventMapper.addMixIn(Record.class, DynamodbEventMixin.RecordMixin.class);
dynamodbEventMapper.addMixIn(StreamRecord.class, DynamodbEventMixin.StreamRecordMixin.class);
dynamodbEventMapper.addMixIn(AttributeValue.class, DynamodbEventMixin.AttributeValueMixIn.class);
}
private static final DateTimeFormatter dateTimeFormatter =
ISODateTimeFormat.dateTime()
.withZone(new FixedDateTimeZone("GMT", "GMT", 0, 0));
public static <T> T parse(String resource, Class<T> clazz)
throws IOException {
InputStream stream = TestUtils.class.getResourceAsStream(resource);
try {
if (clazz == S3Event.class) {
String json = IOUtils.toString(stream);
S3EventNotification event = S3EventNotification.parseJson(json);
@SuppressWarnings("unchecked")
T result = (T) new S3Event(event.getRecords());
return result;
} else if (clazz == SNSEvent.class) {
return snsEventMapper.readValue(stream, clazz);
} else if (clazz == DynamodbEvent.class) {
return dynamodbEventMapper.readValue(stream, clazz);
} else {
return mapper.readValue(stream, clazz);
}
} finally {
stream.close();
}
}
private static class TestJacksonMapperModule extends SimpleModule {
private static final long serialVersionUID = 1L;
public TestJacksonMapperModule() {
super("TestJacksonMapperModule");
super.addSerializer(DateTime.class, new DateTimeSerializer());
super.addDeserializer(DateTime.class, new DateTimeDeserializer());
}
}
private static class DateTimeSerializer extends JsonSerializer<DateTime> {
@Override
public void serialize(
DateTime value,
JsonGenerator gen,
SerializerProvider provider) throws IOException {
gen.writeString(dateTimeFormatter.print(value));
}
}
private static class DateTimeDeserializer
extends JsonDeserializer<DateTime> {
@Override
public DateTime deserialize(
JsonParser parser,
DeserializationContext context) throws IOException {
return dateTimeFormatter.parseDateTime(parser.getText());
}
}
private static class UpperCaseRecordsPropertyNamingStrategy
extends PropertyNamingStrategy.PropertyNamingStrategyBase {
private static final long serialVersionUID = 1L;
@Override
public String translate(String propertyName) {
if (propertyName.equals("records")) {
return "Records";
}
return propertyName;
}
}