Search in sources :

Example 6 with JsonSerializer

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"));
}
Also used : FakeClock(net.morimekta.testing.time.FakeClock) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) TimeBasedCleanupPolicy(net.morimekta.providence.mio.rolling.TimeBasedCleanupPolicy) TimeBasedRollingPolicy(net.morimekta.providence.mio.rolling.TimeBasedRollingPolicy) Test(org.junit.Test)

Example 7 with JsonSerializer

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"));
}
Also used : SizeBasedRollingPolicy(net.morimekta.providence.mio.rolling.SizeBasedRollingPolicy) PApplicationException(net.morimekta.providence.PApplicationException) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) Test(org.junit.Test)

Example 8 with JsonSerializer

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);
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString)

Example 9 with JsonSerializer

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"));
}
Also used : IntStream(java.util.stream.IntStream) CoreMatchers.is(org.hamcrest.CoreMatchers.is) ConsoleWatcher(net.morimekta.testing.rules.ConsoleWatcher) CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CoreMatchers.not(org.hamcrest.CoreMatchers.not) Random(java.util.Random) IOMessageWriter(net.morimekta.providence.mio.IOMessageWriter) ArrayList(java.util.ArrayList) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) Assert.assertThat(org.junit.Assert.assertThat) ExtraStreams(net.morimekta.util.ExtraStreams) ByteArrayInputStream(java.io.ByteArrayInputStream) Locale(java.util.Locale) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) Test(org.junit.Test) Description(org.junit.runner.Description) IOException(java.io.IOException) StandardCharsets(java.nio.charset.StandardCharsets) PMessage(net.morimekta.providence.PMessage) PField(net.morimekta.providence.descriptor.PField) Rule(org.junit.Rule) FastBinarySerializer(net.morimekta.providence.serializer.FastBinarySerializer) MessageReader(net.morimekta.providence.mio.MessageReader) PrettySerializer(net.morimekta.providence.serializer.PrettySerializer) CompactFields(net.morimekta.test.android.CompactFields) ProvidenceMatchers.equalToMessage(net.morimekta.providence.testing.ProvidenceMatchers.equalToMessage) IOMessageReader(net.morimekta.providence.mio.IOMessageReader) Fairy(io.codearte.jfairy.Fairy) Random(java.util.Random) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) Fairy(io.codearte.jfairy.Fairy) CompactFields(net.morimekta.test.android.CompactFields) Test(org.junit.Test)

Example 10 with JsonSerializer

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)));
    }
}
Also used : Containers(net.morimekta.test.jackson.Containers) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

JsonSerializer (net.morimekta.providence.serializer.JsonSerializer)16 Test (org.junit.Test)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 File (java.io.File)4 IOException (java.io.IOException)3 SizeBasedRollingPolicy (net.morimekta.providence.mio.rolling.SizeBasedRollingPolicy)2 PrettySerializer (net.morimekta.providence.serializer.PrettySerializer)2 CompactFields (net.morimekta.test.providence.core.CompactFields)2 Operation (net.morimekta.test.providence.core.calculator.Operation)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Fairy (io.codearte.jfairy.Fairy)1 BufferedInputStream (java.io.BufferedInputStream)1 InputStream (java.io.InputStream)1 UncheckedIOException (java.io.UncheckedIOException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ArrayList (java.util.ArrayList)1 Locale (java.util.Locale)1 Random (java.util.Random)1