Add unit tests for proto-based JSON marshaller

This commit is contained in:
Eric Anderson 2016-04-28 22:58:18 -07:00
parent 9de87e3acd
commit f83db83391
1 changed files with 66 additions and 1 deletions

View File

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