use of net.morimekta.providence.serializer.JsonSerializer in project providence by morimekta.
the class RollingFileMessageWriterTest method testTimeBasedRolling.
@Test
public void testTimeBasedRolling() throws IOException {
FakeClock clock = FakeClock.forCurrentTimeMillis(1234567890000L).withZone(ZoneId.of("Europe/Oslo"));
RollingFileMessageWriter writer = new RollingFileMessageWriter(tmp.getRoot(), new JsonSerializer().named(), "my-log.txt", new TimeBasedRollingPolicy(5, TimeUnit.MINUTES, "my-log-%d{yyyy-MM-dd_HH-mm}.txt", clock), new TimeBasedCleanupPolicy(15, TimeUnit.MINUTES, "my-log-%d{yyyy-MM-dd_HH-mm}.txt", clock));
writer.write(generator.generate(CompactFields.kDescriptor));
for (int i = 0; i < 10; ++i) {
clock.tick(107, TimeUnit.SECONDS);
writer.write(generator.generate(CompactFields.kDescriptor));
}
String[] files = tmp.getRoot().list();
assertThat(files, is(notNullValue()));
assertThat(ImmutableSortedSet.copyOf(files), is(ImmutableSortedSet.of("my-log.txt", "my-log-2009-02-13_22-35.txt", "my-log-2009-02-13_22-40.txt", "my-log-2009-02-13_22-45.txt")));
assertThat(Files.isSymbolicLink(tmp.getRoot().toPath().resolve("my-log.txt")), is(true));
assertThat(Files.readSymbolicLink(tmp.getRoot().toPath().resolve("my-log.txt")).toFile().getName(), is("my-log-2009-02-13_22-45.txt"));
for (int i = 0; i < 10; ++i) {
clock.tick(107, TimeUnit.SECONDS);
writer.write(generator.generate(CompactFields.kDescriptor));
}
files = tmp.getRoot().list();
assertThat(files, is(notNullValue()));
assertThat(ImmutableSortedSet.copyOf(files), is(ImmutableSortedSet.of("my-log.txt", "my-log-2009-02-13_22-55.txt", "my-log-2009-02-13_23-00.txt", "my-log-2009-02-13_23-05.txt")));
assertThat(Files.isSymbolicLink(tmp.getRoot().toPath().resolve("my-log.txt")), is(true));
assertThat(Files.readSymbolicLink(tmp.getRoot().toPath().resolve("my-log.txt")).toFile().getName(), is("my-log-2009-02-13_23-05.txt"));
writer = new RollingFileMessageWriter(tmp.getRoot(), new JsonSerializer().named(), "my-log.txt", new TimeBasedRollingPolicy(TimeUnit.HOURS, "my-log-%d{yyyy-MM-dd_HH-mm}.txt", clock));
writer.write(generator.generate(CompactFields.kDescriptor));
assertThat(Files.isSymbolicLink(tmp.getRoot().toPath().resolve("my-log.txt")), is(true));
assertThat(Files.readSymbolicLink(tmp.getRoot().toPath().resolve("my-log.txt")).toFile().getName(), is("my-log-2009-02-13_23-00.txt"));
}
use of net.morimekta.providence.serializer.JsonSerializer in project providence by morimekta.
the class RollingFileMessageWriterTest method testCallAndSeparator.
@Test
public void testCallAndSeparator() throws IOException {
RollingFileMessageWriter writer = new RollingFileMessageWriter(tmp.getRoot(), new JsonSerializer().named(), "my-log.txt", new SizeBasedRollingPolicy(tmp.getRoot(), 10000, "my-log-%03d.txt"));
writer.write(new PServiceCall<>("test", PServiceCallType.EXCEPTION, 73, new PApplicationException("boo", PApplicationExceptionType.BAD_SEQUENCE_ID)));
assertThat(writer.separator(), is(0));
assertThat(writer.separator(), is(0));
assertThat(writer.separator(), is(0));
writer.close();
String[] files = tmp.getRoot().list();
assertThat(files, is(notNullValue()));
assertThat(ImmutableSortedSet.copyOf(files), is(ImmutableSortedSet.of("my-log.txt", "my-log-001.txt")));
assertThat(Files.isSymbolicLink(tmp.getRoot().toPath().resolve("my-log.txt")), is(true));
assertThat(Files.readSymbolicLink(tmp.getRoot().toPath().resolve("my-log.txt")).toFile().getName(), is("my-log-001.txt"));
String content = new String(Files.readAllBytes(tmp.getRoot().toPath().resolve("my-log.txt")), StandardCharsets.UTF_8);
assertThat(content, is("[\"test\",\"exception\",73,{\"message\":\"boo\",\"id\":\"BAD_SEQUENCE_ID\"}]\n"));
}
use of net.morimekta.providence.serializer.JsonSerializer in project providence by morimekta.
the class GeneratorWatcherTest method json.
private <M extends PMessage<M, F>, F extends PField> String json(M message) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
new JsonSerializer().serialize(baos, message);
return new String(baos.toByteArray(), StandardCharsets.UTF_8);
}
use of net.morimekta.providence.serializer.JsonSerializer in project providence by morimekta.
the class GeneratorWatcherTest method testRandom_customSerializer.
@Test
public void testRandom_customSerializer() throws IOException {
Random random = new Random();
Fairy fairy = Fairy.create(Locale.ENGLISH);
GeneratorWatcher<SimpleGeneratorBase, SimpleGeneratorContext> watcher = GeneratorWatcher.create().setOutputSerializer(new JsonSerializer()).setMaxCollectionItems(2).setRandom(random).withGenerator(CompactFields.kDescriptor, gen -> gen.setAlwaysPresent(CompactFields._Field.NAME).setAlwaysAbsent(CompactFields._Field.LABEL).setValueGenerator(CompactFields._Field.NAME, ctx -> fairy.textProducer().word(1))).setFairy(fairy).dumpOnFailure();
watcher.starting(Description.EMPTY);
CompactFields compact = watcher.generate(CompactFields.kDescriptor);
assertThat(compact.getLabel(), is(nullValue()));
assertThat(compact.getName(), is(notNullValue()));
assertThat(compact.getName(), not(containsString(" ")));
assertThat(compact.hasId(), is(true));
assertThat(watcher.allGenerated(), hasItem(compact));
watcher.failed(new Throwable(), Description.EMPTY);
assertThat(console.output(), is(""));
assertThat(console.error(), is(json(compact) + "\n"));
}
use of net.morimekta.providence.serializer.JsonSerializer in project providence by morimekta.
the class JacksonTest method testJsonSerializerCompat.
@Test
public void testJsonSerializerCompat() throws IOException {
ObjectMapper mapper = new ObjectMapper();
ProvidenceModule.register(mapper);
JsonSerializer compact = new JsonSerializer();
JsonSerializer pretty = new JsonSerializer().pretty();
ByteArrayOutputStream out = new ByteArrayOutputStream();
for (int i = 0; i < 100; ++i) {
Containers containers = generator.generate(Containers.kDescriptor);
out.reset();
compact.serialize(out, containers);
Containers res = mapper.readValue(out.toByteArray(), Containers.class);
assertThat(res, is(equalToMessage(containers)));
out.reset();
pretty.serialize(out, containers);
res = mapper.readValue(out.toByteArray(), Containers.class);
assertThat(res, is(equalToMessage(containers)));
}
}
Aggregations