16
16
17
17
package org .springframework .http .codec .protobuf ;
18
18
19
- import java .io .ByteArrayOutputStream ;
20
19
import java .io .IOException ;
21
- import java .io .OutputStream ;
22
- import java .util .ArrayList ;
23
- import java .util .Arrays ;
24
- import java .util .List ;
25
20
26
21
import com .google .protobuf .Message ;
27
22
import org .junit .Before ;
47
42
48
43
/**
49
44
* Unit tests for {@link ProtobufDecoder}.
50
- * TODO Make tests more readable
51
- * TODO Add a test where an input DataBuffer is larger than a message
52
45
*
53
46
* @author Sebastien Deleuze
54
47
*/
55
48
public class ProtobufDecoderTests extends AbstractDataBufferAllocatingTestCase {
56
49
57
50
private final static MimeType PROTOBUF_MIME_TYPE = new MimeType ("application" , "x-protobuf" );
58
51
59
- private final Msg testMsg = Msg .newBuilder ().setFoo ("Foo" ).setBlah (SecondMsg .newBuilder ().setBlah (123 ).build ()).build ();
52
+ private final SecondMsg secondMsg = SecondMsg .newBuilder ().setBlah (123 ).build ();
53
+
54
+ private final Msg testMsg = Msg .newBuilder ().setFoo ("Foo" ).setBlah (secondMsg ).build ();
55
+
56
+ private final SecondMsg secondMsg2 = SecondMsg .newBuilder ().setBlah (456 ).build ();
57
+
58
+ private final Msg testMsg2 = Msg .newBuilder ().setFoo ("Bar" ).setBlah (secondMsg2 ).build ();
60
59
61
60
private ProtobufDecoder decoder ;
62
61
@@ -82,51 +81,59 @@ public void canDecode() {
82
81
83
82
@ Test
84
83
public void decodeToMono () {
85
- byte [] body = this .testMsg .toByteArray ();
86
- Flux <DataBuffer > source = Flux .just (this .bufferFactory .wrap (body ));
84
+ DataBuffer data = this .bufferFactory .wrap (testMsg .toByteArray ());
87
85
ResolvableType elementType = forClass (Msg .class );
88
- Mono < Message > mono = this . decoder . decodeToMono ( source , elementType , null ,
89
- emptyMap ());
86
+
87
+ Mono < Message > mono = this . decoder . decodeToMono ( Flux . just ( data ), elementType , null , emptyMap ());
90
88
91
89
StepVerifier .create (mono )
92
- .expectNext (this .testMsg )
90
+ .expectNext (testMsg )
91
+ .verifyComplete ();
92
+ }
93
+
94
+ @ Test
95
+ public void decodeToMonoWithLargerDataBuffer () {
96
+ DataBuffer buffer = this .bufferFactory .allocateBuffer (1024 );
97
+ buffer .write (testMsg .toByteArray ());
98
+ ResolvableType elementType = forClass (Msg .class );
99
+
100
+ Mono <Message > mono = this .decoder .decodeToMono (Flux .just (buffer ), elementType , null , emptyMap ());
101
+
102
+ StepVerifier .create (mono )
103
+ .expectNext (testMsg )
93
104
.verifyComplete ();
94
105
}
95
106
96
107
@ Test
97
108
public void decodeChunksToMono () {
98
- byte [] body = this .testMsg .toByteArray ();
99
- List <DataBuffer > chunks = new ArrayList <>();
100
- chunks . add ( this . bufferFactory . wrap ( Arrays . copyOfRange ( body , 0 , 4 )));
101
- chunks . add ( this . bufferFactory . wrap ( Arrays . copyOfRange ( body , 4 , body . length ) ));
102
- Flux < DataBuffer > source = Flux . fromIterable ( chunks );
109
+ DataBuffer buffer = this .bufferFactory . wrap ( testMsg .toByteArray () );
110
+ Flux <DataBuffer > chunks = Flux . just (
111
+ buffer . slice ( 0 , 4 ),
112
+ buffer . slice ( 4 , buffer . readableByteCount () - 4 ));
113
+ DataBufferUtils . retain ( buffer );
103
114
ResolvableType elementType = forClass (Msg .class );
104
- Mono <Message > mono = this .decoder .decodeToMono (source , elementType , null ,
115
+
116
+ Mono <Message > mono = this .decoder .decodeToMono (chunks , elementType , null ,
105
117
emptyMap ());
106
118
107
119
StepVerifier .create (mono )
108
- .expectNext (this . testMsg )
120
+ .expectNext (testMsg )
109
121
.verifyComplete ();
110
122
}
111
123
112
124
@ Test
113
125
public void decode () throws IOException {
114
- Msg testMsg2 = Msg .newBuilder ().setFoo ("Bar" ).setBlah (SecondMsg .newBuilder ().setBlah (456 ).build ()).build ();
115
-
116
126
DataBuffer buffer = bufferFactory .allocateBuffer ();
117
- OutputStream outputStream = buffer .asOutputStream ();
118
- this .testMsg .writeDelimitedTo (outputStream );
119
-
127
+ testMsg .writeDelimitedTo (buffer .asOutputStream ());
120
128
DataBuffer buffer2 = bufferFactory .allocateBuffer ();
121
- OutputStream outputStream2 = buffer2 .asOutputStream ();
122
- testMsg2 .writeDelimitedTo (outputStream2 );
123
-
129
+ testMsg2 .writeDelimitedTo (buffer2 .asOutputStream ());
124
130
Flux <DataBuffer > source = Flux .just (buffer , buffer2 );
125
131
ResolvableType elementType = forClass (Msg .class );
132
+
126
133
Flux <Message > messages = this .decoder .decode (source , elementType , null , emptyMap ());
127
134
128
135
StepVerifier .create (messages )
129
- .expectNext (this . testMsg )
136
+ .expectNext (testMsg )
130
137
.expectNext (testMsg2 )
131
138
.verifyComplete ();
132
139
@@ -135,42 +142,50 @@ public void decode() throws IOException {
135
142
}
136
143
137
144
@ Test
138
- public void decodeChunks () throws IOException {
139
- Msg testMsg2 = Msg .newBuilder ().setFoo ("Bar" ).setBlah (SecondMsg .newBuilder ().setBlah (456 ).build ()).build ();
140
- List <DataBuffer > chunks = new ArrayList <>();
141
-
142
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
143
- this .testMsg .writeDelimitedTo (outputStream );
144
- byte [] byteArray = outputStream .toByteArray ();
145
- ByteArrayOutputStream outputStream2 = new ByteArrayOutputStream ();
146
- testMsg2 .writeDelimitedTo (outputStream2 );
147
- byte [] byteArray2 = outputStream2 .toByteArray ();
148
-
149
- chunks .add (this .bufferFactory .wrap (Arrays .copyOfRange (byteArray , 0 , 4 )));
150
- byte [] chunk2 = Arrays .copyOfRange (byteArray , 4 , byteArray .length );
151
- byte [] chunk3 = Arrays .copyOfRange (byteArray2 , 0 , 4 );
152
- byte [] combined = new byte [chunk2 .length + chunk3 .length ];
153
- for (int i = 0 ; i < combined .length ; ++i )
154
- {
155
- combined [i ] = i < chunk2 .length ? chunk2 [i ] : chunk3 [i - chunk2 .length ];
156
- }
157
- chunks .add (this .bufferFactory .wrap (combined ));
158
- chunks .add (this .bufferFactory .wrap (Arrays .copyOfRange (byteArray2 , 4 , byteArray2 .length )));
159
-
160
- Flux <DataBuffer > source = Flux .fromIterable (chunks );
145
+ public void decodeSplitChunks () throws IOException {
146
+ DataBuffer buffer = bufferFactory .allocateBuffer ();
147
+ testMsg .writeDelimitedTo (buffer .asOutputStream ());
148
+ DataBuffer buffer2 = bufferFactory .allocateBuffer ();
149
+ testMsg2 .writeDelimitedTo (buffer2 .asOutputStream ());
150
+ Flux <DataBuffer > chunks = Flux .just (
151
+ buffer .slice (0 , 4 ),
152
+ buffer .slice (4 , buffer .readableByteCount () - 4 ),
153
+ buffer2 .slice (0 , 2 ),
154
+ buffer2 .slice (2 , buffer2 .readableByteCount () - 2 ));
155
+
161
156
ResolvableType elementType = forClass (Msg .class );
162
- Flux <Message > messages = this .decoder .decode (source , elementType , null , emptyMap ());
157
+ Flux <Message > messages = this .decoder .decode (chunks , elementType , null , emptyMap ());
163
158
164
159
StepVerifier .create (messages )
165
- .expectNext (this . testMsg )
160
+ .expectNext (testMsg )
166
161
.expectNext (testMsg2 )
167
162
.verifyComplete ();
163
+
164
+ DataBufferUtils .release (buffer );
165
+ DataBufferUtils .release (buffer2 );
166
+ }
167
+
168
+ @ Test
169
+ public void decodeMergedChunks () throws IOException {
170
+ DataBuffer buffer = bufferFactory .allocateBuffer ();
171
+ testMsg .writeDelimitedTo (buffer .asOutputStream ());
172
+ testMsg .writeDelimitedTo (buffer .asOutputStream ());
173
+
174
+ ResolvableType elementType = forClass (Msg .class );
175
+ Flux <Message > messages = this .decoder .decode (Mono .just (buffer ), elementType , null , emptyMap ());
176
+
177
+ StepVerifier .create (messages )
178
+ .expectNext (testMsg )
179
+ .expectNext (testMsg )
180
+ .verifyComplete ();
181
+
182
+ DataBufferUtils .release (buffer );
168
183
}
169
184
170
185
@ Test
171
186
public void exceedMaxSize () {
172
187
this .decoder .setMaxMessageSize (1 );
173
- byte [] body = this . testMsg .toByteArray ();
188
+ byte [] body = testMsg .toByteArray ();
174
189
Flux <DataBuffer > source = Flux .just (this .bufferFactory .wrap (body ));
175
190
ResolvableType elementType = forClass (Msg .class );
176
191
Flux <Message > messages = this .decoder .decode (source , elementType , null ,
0 commit comments