@ -17,8 +17,10 @@
@@ -17,8 +17,10 @@
package org.springframework.http.converter.protobuf ;
import java.io.IOException ;
import java.nio.charset.Charset ;
import com.google.protobuf.Message ;
import com.google.protobuf.util.JsonFormat ;
import org.junit.Before ;
import org.junit.Test ;
@ -36,6 +38,7 @@ import static org.mockito.Mockito.*;
@@ -36,6 +38,7 @@ import static org.mockito.Mockito.*;
*
* @author Alex Antonov
* @author Juergen Hoeller
* @author Andreas Ahlenstorf
* /
public class ProtobufHttpMessageConverterTests {
@ -104,7 +107,7 @@ public class ProtobufHttpMessageConverterTests {
@@ -104,7 +107,7 @@ public class ProtobufHttpMessageConverterTests {
}
@Test
public void write ( ) throws IOException {
public void writeProtobuf ( ) throws IOException {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage ( ) ;
MediaType contentType = ProtobufHttpMessageConverter . PROTOBUF ;
this . converter . write ( this . testMsg , contentType , outputMessage ) ;
@ -121,6 +124,54 @@ public class ProtobufHttpMessageConverterTests {
@@ -121,6 +124,54 @@ public class ProtobufHttpMessageConverterTests {
assertEquals ( "sample.proto" , schemaHeader ) ;
}
@Test
public void writeJsonWithGoogleProtobuf ( ) throws IOException {
this . converter = new ProtobufHttpMessageConverter (
new ProtobufHttpMessageConverter . ProtobufJavaUtilSupport ( null , null ) ,
this . registryInitializer ) ;
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage ( ) ;
MediaType contentType = MediaType . APPLICATION_JSON_UTF8 ;
this . converter . write ( this . testMsg , contentType , outputMessage ) ;
assertEquals ( contentType , outputMessage . getHeaders ( ) . getContentType ( ) ) ;
final String body = outputMessage . getBodyAsString ( Charset . forName ( "UTF-8" ) ) ;
assertFalse ( "body is empty" , body . isEmpty ( ) ) ;
Msg . Builder builder = Msg . newBuilder ( ) ;
JsonFormat . parser ( ) . merge ( body , builder ) ;
assertEquals ( this . testMsg , builder . build ( ) ) ;
assertNull ( outputMessage . getHeaders ( ) . getFirst (
ProtobufHttpMessageConverter . X_PROTOBUF_MESSAGE_HEADER ) ) ;
assertNull ( outputMessage . getHeaders ( ) . getFirst (
ProtobufHttpMessageConverter . X_PROTOBUF_SCHEMA_HEADER ) ) ;
}
@Test
public void writeJsonWithJavaFormat ( ) throws IOException {
this . converter = new ProtobufHttpMessageConverter (
new ProtobufHttpMessageConverter . ProtobufJavaFormatSupport ( ) ,
this . registryInitializer ) ;
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage ( ) ;
MediaType contentType = MediaType . APPLICATION_JSON_UTF8 ;
this . converter . write ( this . testMsg , contentType , outputMessage ) ;
assertEquals ( contentType , outputMessage . getHeaders ( ) . getContentType ( ) ) ;
final String body = outputMessage . getBodyAsString ( Charset . forName ( "UTF-8" ) ) ;
assertFalse ( "body is empty" , body . isEmpty ( ) ) ;
Msg . Builder builder = Msg . newBuilder ( ) ;
JsonFormat . parser ( ) . merge ( body , builder ) ;
assertEquals ( this . testMsg , builder . build ( ) ) ;
assertNull ( outputMessage . getHeaders ( ) . getFirst (
ProtobufHttpMessageConverter . X_PROTOBUF_MESSAGE_HEADER ) ) ;
assertNull ( outputMessage . getHeaders ( ) . getFirst (
ProtobufHttpMessageConverter . X_PROTOBUF_SCHEMA_HEADER ) ) ;
}
@Test
public void defaultContentType ( ) throws Exception {
assertEquals ( ProtobufHttpMessageConverter . PROTOBUF , this . converter . getDefaultContentType ( this . testMsg ) ) ;