Search in sources :

Example 1 with Containers

use of net.morimekta.test.providence.core.Containers in project providence by morimekta.

the class SerializerTest method setUpData.

@BeforeClass
public static void setUpData() throws IOException, ExceptionFields {
    MessageGenerator gen = new MessageGenerator().addFactory(f -> {
        if (f.equals(Operand._Field.OPERATION)) {
            return () -> Operation.builder().setOperator(Operator.ADD).addToOperands(Operand.withNumber(123)).addToOperands(Operand.withNumber(321)).build();
        }
        return null;
    });
    if (operation == null) {
        operation = gen.generate(Operation.kDescriptor);
    }
    if (containers == null) {
        containers = new ArrayList<>();
        for (int i = 0; i < 1; ++i) {
            containers.add(gen.generate(Containers.kDescriptor));
        }
    }
    serviceCalls = new ArrayList<>();
    /**
     * Temporary setup needed to generate
     */
    ContainerService.Iface impl = pC -> {
        if (pC == null) {
            throw new PApplicationException("", PApplicationExceptionType.INTERNAL_ERROR);
        }
        if (pC.mutate().presentFields().isEmpty()) {
            throw gen.generate(ExceptionFields.kDescriptor);
        }
        return CompactFields.builder().setName("" + pC.hashCode()).setId(pC.hashCode()).build();
    };
    PProcessor processor = new ContainerService.Processor(impl);
    PServiceCallHandler handler = new PServiceCallHandler() {

        @Nullable
        @Override
        @SuppressWarnings("unchecked")
        public <Request extends PMessage<Request, RequestField>, Response extends PMessage<Response, ResponseField>, RequestField extends PField, ResponseField extends PField> PServiceCall<Response, ResponseField> handleCall(PServiceCall<Request, RequestField> call, PService service) throws IOException {
            serviceCalls.add(call);
            try {
                PServiceCall response = processor.handleCall(call, service);
                serviceCalls.add(response);
                return response;
            } catch (PApplicationException e) {
                PServiceCall ex = new PServiceCall(call.getMethod(), PServiceCallType.EXCEPTION, call.getSequence(), e);
                serviceCalls.add(ex);
                return ex;
            }
        }
    };
    ContainerService.Client client = new ContainerService.Client(handler);
    client.load(gen.generate(Containers.kDescriptor));
    try {
        client.load(Containers.builder().build());
    } catch (ExceptionFields e) {
    // ignore.
    }
    try {
        // NPE -> PApplicationException
        client.load(null);
    } catch (PApplicationException e) {
    // ignore.
    }
}
Also used : Operand(net.morimekta.test.providence.core.calculator.Operand) CoreMatchers.is(org.hamcrest.CoreMatchers.is) BeforeClass(org.junit.BeforeClass) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PApplicationException(net.morimekta.providence.PApplicationException) MessageCollectors(net.morimekta.providence.streams.MessageCollectors) EqualToMessage(net.morimekta.providence.util_internal.EqualToMessage) Binary(net.morimekta.util.Binary) PServiceCallType(net.morimekta.providence.PServiceCallType) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) PService(net.morimekta.providence.descriptor.PService) Containers(net.morimekta.test.providence.core.Containers) PProcessor(net.morimekta.providence.PProcessor) ByteArrayInputStream(java.io.ByteArrayInputStream) PApplicationExceptionType(net.morimekta.providence.PApplicationExceptionType) MessageGenerator(net.morimekta.providence.util_internal.MessageGenerator) Assert.fail(org.junit.Assert.fail) Nullable(javax.annotation.Nullable) ExtraMatchers.equalToLines(net.morimekta.testing.ExtraMatchers.equalToLines) Operation(net.morimekta.test.providence.core.calculator.Operation) UTF_8(java.nio.charset.StandardCharsets.UTF_8) PServiceCallHandler(net.morimekta.providence.PServiceCallHandler) Test(org.junit.Test) IOException(java.io.IOException) MessageStreams(net.morimekta.providence.streams.MessageStreams) ContainerService(net.morimekta.test.providence.core.ContainerService) Collectors(java.util.stream.Collectors) File(java.io.File) PMessage(net.morimekta.providence.PMessage) PField(net.morimekta.providence.descriptor.PField) CompactFields(net.morimekta.test.providence.core.CompactFields) List(java.util.List) Rule(org.junit.Rule) PServiceCall(net.morimekta.providence.PServiceCall) ExceptionFields(net.morimekta.test.providence.core.ExceptionFields) TokenizerException(net.morimekta.providence.serializer.pretty.TokenizerException) ConsumeAll(net.morimekta.test.providence.core.ConsumeAll) Assert.assertEquals(org.junit.Assert.assertEquals) Operator(net.morimekta.test.providence.core.calculator.Operator) InputStream(java.io.InputStream) PProcessor(net.morimekta.providence.PProcessor) MessageGenerator(net.morimekta.providence.util_internal.MessageGenerator) PField(net.morimekta.providence.descriptor.PField) PProcessor(net.morimekta.providence.PProcessor) ExceptionFields(net.morimekta.test.providence.core.ExceptionFields) PServiceCallHandler(net.morimekta.providence.PServiceCallHandler) PMessage(net.morimekta.providence.PMessage) PApplicationException(net.morimekta.providence.PApplicationException) PServiceCall(net.morimekta.providence.PServiceCall) ContainerService(net.morimekta.test.providence.core.ContainerService) PService(net.morimekta.providence.descriptor.PService) BeforeClass(org.junit.BeforeClass)

Example 2 with Containers

use of net.morimekta.test.providence.core.Containers in project providence by morimekta.

the class SerializerTest method testSerializer.

/**
 * Test that the serializer can serialize and deserialize a test-set of
 * random data. This is not testing backward compatibility of the
 * serializer.
 *
 * @param serializer The serializer to test.
 */
private void testSerializer(Serializer serializer) throws IOException {
    try {
        // Just a sanity check.
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ByteArrayInputStream bais;
        int size;
        // simple message.
        {
            baos.reset();
            size = serializer.serialize(baos, operation);
            assertEquals(baos.size(), size);
            bais = new ByteArrayInputStream(baos.toByteArray());
            Operation actual = serializer.deserialize(bais, Operation.kDescriptor);
            assertEquals(actual, operation);
        }
        // complex message, one at a time.
        for (Containers expected : containers) {
            baos.reset();
            size = serializer.serialize(baos, expected);
            assertEquals(baos.size(), size);
            bais = new ByteArrayInputStream(baos.toByteArray());
            Containers actual;
            try {
                actual = serializer.deserialize(bais, Containers.kDescriptor);
            } catch (TokenizerException e) {
                System.err.println(new String(baos.toByteArray(), UTF_8));
                System.err.println(e.asString());
                e.printStackTrace();
                fail("oops");
                return;
            }
            assertThat(actual, new EqualToMessage<>(expected));
        }
        // complex message in stream.
        {
            baos.reset();
            boolean first = true;
            size = 0;
            for (Containers c : containers) {
                if (first) {
                    first = false;
                } else {
                    baos.write('\n');
                    size += 1;
                }
                size += serializer.serialize(baos, c);
            }
            assertEquals(baos.size(), size);
            bais = new ByteArrayInputStream(baos.toByteArray());
            first = true;
            for (Containers expected : containers) {
                if (first) {
                    first = false;
                } else {
                    assertThat(bais.read(), is((int) '\n'));
                }
                Containers actual = serializer.deserialize(bais, Containers.kDescriptor);
                assertThat(actual, new EqualToMessage<>(expected));
            }
            assertEquals(0, bais.available());
        }
        try {
            if (serializer instanceof PrettySerializer) {
                String tmp = new String(baos.toByteArray(), UTF_8);
                bais = new ByteArrayInputStream(tmp.replaceFirst("providence[.]Containers", "providence.ConsumeAll").getBytes(UTF_8));
            } else {
                bais = new ByteArrayInputStream(baos.toByteArray());
            }
            boolean first = true;
            for (Containers ignore : containers) {
                if (first) {
                    first = false;
                } else {
                    assertThat(bais.read(), is((int) '\n'));
                }
                ConsumeAll actual = serializer.deserialize(bais, ConsumeAll.kDescriptor);
                assertThat(actual, new EqualToMessage<>(ConsumeAll.builder().build()));
            }
        } catch (TokenizerException e) {
            System.err.println(e.asString());
            throw e;
        }
        // service
        for (PServiceCall<?, ?> call : serviceCalls) {
            baos.reset();
            int i = serializer.serialize(baos, call);
            assertThat(i, is(baos.size()));
            bais = new ByteArrayInputStream(baos.toByteArray());
            PServiceCall<?, ?> re = serializer.deserialize(bais, ContainerService.kDescriptor);
            assertThat(re, is(call));
        }
    } catch (SerializerException e) {
        System.err.println(e.asString());
        throw e;
    }
}
Also used : Containers(net.morimekta.test.providence.core.Containers) TokenizerException(net.morimekta.providence.serializer.pretty.TokenizerException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Operation(net.morimekta.test.providence.core.calculator.Operation) ConsumeAll(net.morimekta.test.providence.core.ConsumeAll) EqualToMessage(net.morimekta.providence.util_internal.EqualToMessage) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 3 with Containers

use of net.morimekta.test.providence.core.Containers in project providence by morimekta.

the class SerializerTest method testOutput.

/**
 * Tests the current output. This tests the byte-stream generated by serialising with the current generated code /
 */
public void testOutput(Serializer serializer, String resource) throws IOException {
    Binary expected;
    List<Containers> source;
    try (InputStream r = SerializerTest.class.getResourceAsStream(resource)) {
        if (r == null) {
            File file = new File("src/test/resources" + resource);
            File testing = new File("providence-core");
            if (testing.isDirectory()) {
                file = new File(testing, file.toString());
            }
            containers.stream().limit(10).collect(MessageCollectors.toFile(file, serializer));
            fail("No such resource to compare: " + resource);
            return;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len;
        while ((len = r.read(buffer, 0, 1024)) > 0) {
            out.write(buffer, 0, len);
        }
        expected = Binary.wrap(out.toByteArray());
        source = MessageStreams.stream(new ByteArrayInputStream(out.toByteArray()), serializer, Containers.kDescriptor).collect(Collectors.toList());
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    source.stream().collect(MessageCollectors.toStream(out, serializer));
    Binary actual = Binary.wrap(out.toByteArray());
    if (serializer.binaryProtocol()) {
        assertEquals("Hex data comparison.", expected.toHexString(), actual.toHexString());
    } else {
        assertThat(new String(expected.get()), is(equalToLines(new String(actual.get()))));
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Containers(net.morimekta.test.providence.core.Containers) Binary(net.morimekta.util.Binary) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File)

Example 4 with Containers

use of net.morimekta.test.providence.core.Containers in project providence by morimekta.

the class LogformatterTest method testFormat_EverythingDefault.

@Test
public void testFormat_EverythingDefault() throws IOException {
    Containers containers = MessageStreams.resource("/compat/binary.data", new BinarySerializer(), Containers.kDescriptor).findFirst().orElseThrow(() -> new AssertionError("resource"));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    new PrettySerializer().config().serialize(baos, containers);
    assertThat(new LogFormatter(true).format(containers), is(equalToLines(new String(baos.toByteArray(), UTF_8))));
}
Also used : PrettySerializer(net.morimekta.providence.serializer.PrettySerializer) Containers(net.morimekta.test.providence.core.Containers) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) Test(org.junit.Test)

Example 5 with Containers

use of net.morimekta.test.providence.core.Containers in project providence by morimekta.

the class PrettySerializerTest method testConfig_2.

@Test
public void testConfig_2() throws IOException {
    ByteArrayInputStream in = new ByteArrayInputStream(ResourceUtils.getResourceAsBytes("/compat/config.cfg"));
    PrettySerializer serializer = new PrettySerializer().config();
    Containers c = serializer.deserialize(in, Containers.kDescriptor);
    assertThat(c, is(notNullValue()));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Containers(net.morimekta.test.providence.core.Containers) Test(org.junit.Test)

Aggregations

Containers (net.morimekta.test.providence.core.Containers)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 Test (org.junit.Test)3 File (java.io.File)2 InputStream (java.io.InputStream)2 TokenizerException (net.morimekta.providence.serializer.pretty.TokenizerException)2 EqualToMessage (net.morimekta.providence.util_internal.EqualToMessage)2 ConsumeAll (net.morimekta.test.providence.core.ConsumeAll)2 Operation (net.morimekta.test.providence.core.calculator.Operation)2 Binary (net.morimekta.util.Binary)2 IOException (java.io.IOException)1 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Nullable (javax.annotation.Nullable)1 PApplicationException (net.morimekta.providence.PApplicationException)1 PApplicationExceptionType (net.morimekta.providence.PApplicationExceptionType)1 PMessage (net.morimekta.providence.PMessage)1