use of net.morimekta.test.providence.core.calculator.Operation in project providence by morimekta.
the class ProvidenceHelperTest method testFromJsonResource_pretty.
@Test
public void testFromJsonResource_pretty() throws IOException {
Operation op = net.morimekta.providence.util.ProvidenceHelper.fromJsonResource("/json/calculator/pretty.json", Operation.kDescriptor);
Operation expected = Operation.builder().setOperator(Operator.MULTIPLY).addToOperands(Operand.builder().setOperation(Operation.builder().setOperator(Operator.ADD).addToOperands(Operand.builder().setNumber(1234).build()).addToOperands(Operand.builder().setNumber(4.321).build()).build()).build()).addToOperands(Operand.builder().setImaginary(Imaginary.builder().setV(1.7).setI(-2.0).build()).build()).build();
assertEquals(op, expected);
}
use of net.morimekta.test.providence.core.calculator.Operation in project providence by morimekta.
the class ProvidenceHelperTest method testFromJsonResource_compact.
@Test
public void testFromJsonResource_compact() throws IOException {
Operation op = net.morimekta.providence.util.ProvidenceHelper.fromJsonResource("/json/calculator/compact.json", Operation.kDescriptor);
Operation expected = Operation.builder().setOperator(Operator.MULTIPLY).addToOperands(Operand.builder().setOperation(Operation.builder().setOperator(Operator.ADD).addToOperands(Operand.builder().setNumber(1234).build()).addToOperands(Operand.builder().setNumber(4.321).build()).build()).build()).addToOperands(Operand.builder().setImaginary(Imaginary.builder().setV(1.7).setI(-2.0).build()).build()).build();
assertEquals(op, expected);
}
use of net.morimekta.test.providence.core.calculator.Operation 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.
}
}
use of net.morimekta.test.providence.core.calculator.Operation 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;
}
}
use of net.morimekta.test.providence.core.calculator.Operation 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)));
}
Aggregations