@Test public void testJsonIoException() throws Exception { Marshaller<Type> marshaller = ProtoUtils.jsonMarshaller(Type.getDefaultInstance()); final IOException ioe = new IOException(); try { marshaller.parse(new ByteArrayInputStream("{}".getBytes("UTF-8")) { @Override public void close() throws IOException { throw ioe; } }); fail("Exception expected"); } catch (StatusRuntimeException ex) { assertEquals(Status.Code.INTERNAL, ex.getStatus().getCode()); assertEquals(ioe, ex.getCause()); } }
private static Optional<Message> marshallerPrototype(Marshaller<?> marshaller) { if (marshaller instanceof PrototypeMarshaller) { Object prototype = ((PrototypeMarshaller) marshaller).getMessagePrototype(); if (prototype instanceof Message) { return Optional.of((Message) prototype); } } return Optional.empty(); }
/** * Create a {@code Marshaller} for json protos of the same type as {@code defaultInstance}. * * <p>This is an unstable API and has not been optimized yet for performance. */ @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1786") public static <T extends Message> Marshaller<T> jsonMarshaller(final T defaultInstance) { final Parser parser = JsonFormat.parser(); final Printer printer = JsonFormat.printer(); return jsonMarshaller(defaultInstance, parser, printer); }
@Test public void testRoundtrip() throws Exception { Marshaller<Type> marshaller = ProtoUtils.marshaller(Type.getDefaultInstance()); InputStream is = marshaller.stream(proto); is = new ByteArrayInputStream(ByteStreams.toByteArray(is)); assertEquals(proto, marshaller.parse(is)); }
@Test public void testJsonRoundtrip() throws Exception { Marshaller<Type> marshaller = ProtoUtils.jsonMarshaller(Type.getDefaultInstance()); InputStream is = marshaller.stream(proto); is = new ByteArrayInputStream(ByteStreams.toByteArray(is)); assertEquals(proto, marshaller.parse(is)); }
@Test public void testJsonRepresentation() throws Exception { Marshaller<Type> marshaller = ProtoUtils.jsonMarshaller(Type.getDefaultInstance()); InputStream is = marshaller.stream(proto); String s = new String(ByteStreams.toByteArray(is), "UTF-8"); assertEquals("{\"name\":\"value\"}", s.replaceAll("\\s", "")); }
@Ignore("https://github.com/google/protobuf/issues/1470") @Test public void testJsonInvalid() throws Exception { Marshaller<Type> marshaller = ProtoUtils.jsonMarshaller(Type.getDefaultInstance()); try { marshaller.parse(new ByteArrayInputStream("{]".getBytes("UTF-8"))); fail("Expected exception"); } catch (StatusRuntimeException ex) { assertEquals(Status.Code.INTERNAL, ex.getStatus().getCode()); assertNotNull(ex.getCause()); } }
@Test public void testJsonInvalidProto() throws Exception { Marshaller<Type> marshaller = ProtoUtils.jsonMarshaller(Type.getDefaultInstance()); try { marshaller.parse(new ByteArrayInputStream("{\"\":3}".getBytes("UTF-8"))); fail("Expected exception"); } catch (StatusRuntimeException ex) { assertEquals(Status.Code.INTERNAL, ex.getStatus().getCode()); assertNotNull(ex.getCause()); } }
@Test public void testMismatch() throws Exception { Marshaller<Enum> enumMarshaller = ProtoLiteUtils.marshaller(Enum.getDefaultInstance()); // Enum's name and Type's name are both strings with tag 1. Enum altProto = Enum.newBuilder().setName(proto.getName()).build(); assertEquals(proto, marshaller.parse(enumMarshaller.stream(altProto))); }
@Test public void introspection() throws Exception { Marshaller<Enum> enumMarshaller = ProtoLiteUtils.marshaller(Enum.getDefaultInstance()); PrototypeMarshaller<Enum> prototypeMarshaller = (PrototypeMarshaller<Enum>) enumMarshaller; assertSame(Enum.getDefaultInstance(), prototypeMarshaller.getMessagePrototype()); assertSame(Enum.class, prototypeMarshaller.getMessageClass()); }
@Test public void testEmpty() throws IOException { Marshaller<Empty> marshaller = ProtoLiteUtils.marshaller(Empty.getDefaultInstance()); InputStream is = marshaller.stream(Empty.getDefaultInstance()); assertEquals(0, is.available()); byte[] b = new byte[10]; assertEquals(-1, is.read(b)); assertArrayEquals(new byte[10], b); // Do the same thing again, because the internal state may be different assertEquals(-1, is.read(b)); assertArrayEquals(new byte[10], b); assertEquals(-1, is.read()); assertEquals(0, is.available()); }
@Test public void parseFromKnowLengthInputStream() throws Exception { Marshaller<Type> marshaller = ProtoLiteUtils.marshaller(Type.getDefaultInstance()); Type expect = Type.newBuilder().setName("expected name").build(); Type result = marshaller.parse(new CustomKnownLengthInputStream(expect.toByteArray())); assertEquals(expect, result); }
public static <WReqT, WRespT> ClientInterceptor wrapClientInterceptor( final ClientInterceptor wrappedInterceptor, final Marshaller<WReqT> reqMarshaller, final Marshaller<WRespT> respMarshaller) { return ClientInterceptors.wrapClientInterceptor( wrappedInterceptor, reqMarshaller, respMarshaller); }
private static MessageType marshallerType(Marshaller<?> marshaller) { return marshaller instanceof PrototypeMarshaller ? MessageType.PROTOBUF : MessageType.UNKNOWN; }
/** Create a {@code Marshaller} for protos of the same type as {@code defaultInstance}. */ public static <T extends Message> Marshaller<T> marshaller(final T defaultInstance) { return ProtoLiteUtils.marshaller(defaultInstance); }