diff --git a/protobuf/src/test/java/io/grpc/protobuf/ProtoUtilsTest.java b/protobuf/src/test/java/io/grpc/protobuf/ProtoUtilsTest.java index 8281da89ca..b13ef71d8c 100644 --- a/protobuf/src/test/java/io/grpc/protobuf/ProtoUtilsTest.java +++ b/protobuf/src/test/java/io/grpc/protobuf/ProtoUtilsTest.java @@ -32,24 +32,30 @@ package io.grpc.protobuf; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; import com.google.common.io.ByteStreams; import com.google.protobuf.MessageLite; import com.google.protobuf.Type; import io.grpc.MethodDescriptor.Marshaller; +import io.grpc.Status; +import io.grpc.StatusRuntimeException; +import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import java.io.ByteArrayInputStream; +import java.io.IOException; import java.io.InputStream; /** Unit tests for {@link ProtoUtils}. */ @RunWith(JUnit4.class) public class ProtoUtilsTest { - private Type proto = Type.newBuilder().setName("name").build(); + private Type proto = Type.newBuilder().setName("value").build(); @Test public void testRoundtrip() throws Exception { @@ -74,4 +80,63 @@ public class ProtoUtilsTest { assertEquals("google.protobuf.Type-bin", ProtoUtils.keyForProto(Type.getDefaultInstance()).originalName()); } + + @Test + public void testJsonRoundtrip() throws Exception { + Marshaller 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 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 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 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 testJsonIoException() throws Exception { + Marshaller 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()); + } + } }