Search in sources :

Example 1 with JsonSerializer

use of net.morimekta.providence.serializer.JsonSerializer in project providence by morimekta.

the class MessageStreamsTest method testResource.

@Test
public void testResource() {
    try {
        MessageStreams.resource("/no_such_resource.json", new JsonSerializer(), CompactFields.kDescriptor);
        fail("no exception");
    } catch (IOException e) {
        assertThat(e.getMessage(), is("No such resource /no_such_resource.json"));
    }
}
Also used : JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Test(org.junit.Test)

Example 2 with JsonSerializer

use of net.morimekta.providence.serializer.JsonSerializer in project providence by morimekta.

the class IOMessageRWTest method testReadable.

@Test
public void testReadable() throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (MessageWriter writer = new IOMessageWriter(baos, new JsonSerializer())) {
        writer.write(m1);
        writer.separator();
        writer.write(m2);
    }
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    try (MessageReader reader = new IOMessageReader(bais, new JsonSerializer())) {
        assertThat(m1, is(equalTo(reader.read(CompactFields.kDescriptor))));
        assertThat(m2, is(equalTo(reader.read(OptionalFields.kDescriptor))));
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) Test(org.junit.Test)

Example 3 with JsonSerializer

use of net.morimekta.providence.serializer.JsonSerializer in project providence by morimekta.

the class IOMessageRWTest method testService.

@Test
public void testService() throws IOException {
    String content = "[\"calculate\",\"call\",44,{\"op\":{\"operator\":\"ADD\",\"operands\":[]}}]";
    ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes(UTF_8));
    PServiceCall<Operation, Operation._Field> call = null;
    try (MessageReader reader = new IOMessageReader(in, new JsonSerializer())) {
        call = reader.read(Calculator.kDescriptor);
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try (MessageWriter writer = new IOMessageWriter(out, new JsonSerializer().named())) {
        writer.write(call);
    }
    assertThat(new String(out.toByteArray(), UTF_8), is(equalTo(content)));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Operation(net.morimekta.test.providence.core.calculator.Operation) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 4 with JsonSerializer

use of net.morimekta.providence.serializer.JsonSerializer in project providence by morimekta.

the class QueuedFileMessageWriterTest method testClose_withQueue.

@Test
public void testClose_withQueue() throws IOException, InterruptedException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    IOMessageWriter target = new IOMessageWriter(baos, new JsonSerializer().named());
    ExecutorService executor = mock(ExecutorService.class);
    when(executor.isShutdown()).thenReturn(false);
    when(executor.awaitTermination(1000L, TimeUnit.MILLISECONDS)).thenReturn(true);
    QueuedMessageWriter writer = new QueuedMessageWriter(target, executor);
    writer.write(CompactFields.builder().setId(1).setName("Name").build());
    writer.write(new PServiceCall<>("name", PServiceCallType.REPLY, 42, CompactFields.builder().setId(1).setName("Name").build()));
    writer.close();
    verify(executor).submit(any(Runnable.class));
    verify(executor).isShutdown();
    verify(executor).shutdown();
    verify(executor).awaitTermination(1000L, TimeUnit.MILLISECONDS);
    verifyNoMoreInteractions(executor);
    assertThat(new String(baos.toByteArray()), is("[\"Name\",1]\n" + "[\"name\",\"reply\",42,[\"Name\",1]]\n" + ""));
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) Test(org.junit.Test)

Example 5 with JsonSerializer

use of net.morimekta.providence.serializer.JsonSerializer in project providence by morimekta.

the class RollingFileMessageWriterTest method testSizeBasedRolling.

@Test
public void testSizeBasedRolling() throws IOException {
    CompactFields cf = new CompactFields(" * the message writer MUST be assigned a rolling policy", 1234567890, "Also note that");
    RollingFileMessageWriter writer = new RollingFileMessageWriter(tmp.getRoot(), new JsonSerializer().named(), "my-log.txt", new SizeBasedRollingPolicy(tmp.getRoot(), 1000, "my-log-%03d.txt"), new KeepLastNCleanupPolicy(5, "my-log-[\\d]{3}.txt"));
    writer.write(cf);
    for (int i = 0; i < 100; ++i) {
        writer.write(cf);
    }
    String[] files = tmp.getRoot().list();
    assertThat(files, is(notNullValue()));
    assertThat(ImmutableSortedSet.copyOf(files), is(ImmutableSortedSet.of("my-log.txt", "my-log-005.txt", "my-log-006.txt", "my-log-007.txt", "my-log-008.txt", "my-log-009.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-009.txt"));
    writer = new RollingFileMessageWriter(tmp.getRoot(), new JsonSerializer().named(), "my-log.txt", new SizeBasedRollingPolicy(tmp.getRoot(), 1000, "my-log-%03d.txt"), new KeepLastNCleanupPolicy(5, "my-log-[\\d]{3}.txt"));
    writer.write(cf);
    for (int i = 0; i < 30; ++i) {
        writer.write(cf);
    }
    files = tmp.getRoot().list();
    assertThat(files, is(notNullValue()));
    assertThat(ImmutableSortedSet.copyOf(files), is(ImmutableSortedSet.of("my-log.txt", "my-log-001.txt", "my-log-002.txt", "my-log-003.txt", "my-log-008.txt", "my-log-009.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-003.txt"));
    writer = new RollingFileMessageWriter(tmp.getRoot(), new JsonSerializer().named(), "my-log.txt", new SizeBasedRollingPolicy(tmp.getRoot(), 1000, "my-log-%03d.txt"), new KeepLastNCleanupPolicy(5, "my-log-[\\d]{3}.txt"));
    writer.write(cf);
    for (int i = 0; i < 30; ++i) {
        writer.write(cf);
    }
    files = tmp.getRoot().list();
    assertThat(files, is(notNullValue()));
    assertThat(ImmutableSortedSet.copyOf(files), is(ImmutableSortedSet.of("my-log.txt", "my-log-002.txt", "my-log-003.txt", "my-log-004.txt", "my-log-005.txt", "my-log-006.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-006.txt"));
}
Also used : SizeBasedRollingPolicy(net.morimekta.providence.mio.rolling.SizeBasedRollingPolicy) KeepLastNCleanupPolicy(net.morimekta.providence.mio.rolling.KeepLastNCleanupPolicy) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) CompactFields(net.morimekta.test.providence.core.CompactFields) 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