Skip to content

Commit 51b6d01

Browse files
author
Georgi Neykov
committed
test(protocol): Add unit tests for BytesReader.peek()
- Add missing tests for the `peek()` Method.html - Make the usage of `ProtocolResponseWriter` more safer by adding scope and state checks and updating affected classes - Add the `ResponseBody.writeTo(BufferedSink)` method
1 parent af94f1d commit 51b6d01

File tree

10 files changed

+287
-121
lines changed

10 files changed

+287
-121
lines changed

binapi-client/src/main/java/com/pcloud/networking/client/RealCall.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright (c) 2017 pCloud AG
2+
* Copyright (c) 2020 pCloud AG
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -21,6 +21,7 @@
2121
import com.pcloud.networking.protocol.ProtocolRequestWriter;
2222
import com.pcloud.networking.protocol.ProtocolResponseReader;
2323
import com.pcloud.utils.IOUtils;
24+
import okio.BufferedSink;
2425
import okio.BufferedSource;
2526
import okio.ByteString;
2627
import okio.Okio;
@@ -244,6 +245,11 @@ public Endpoint endpoint() {
244245
return endpoint;
245246
}
246247

248+
@Override
249+
public void writeTo(BufferedSink sink) throws IOException {
250+
source.readAll(sink);
251+
}
252+
247253
@Override
248254
public ResponseData data() throws IOException {
249255
int scope = reader.currentScope();

binapi-client/src/main/java/com/pcloud/networking/client/RealMultiCall.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017 pCloud AG
2+
* Copyright (c) 2020 pCloud AG
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323
import com.pcloud.networking.protocol.ProtocolResponseReader;
2424
import com.pcloud.utils.IOUtils;
2525
import okio.Buffer;
26+
import okio.BufferedSink;
2627
import okio.BufferedSource;
2728
import okio.ByteString;
2829
import okio.Okio;
@@ -457,6 +458,11 @@ public Endpoint endpoint() {
457458
return endpoint;
458459
}
459460

461+
@Override
462+
public void writeTo(BufferedSink sink) throws IOException {
463+
source.readAll(sink);
464+
}
465+
460466
@Override
461467
public void close() {
462468
source.close();
@@ -508,6 +514,11 @@ public Endpoint endpoint() {
508514
return endpoint;
509515
}
510516

517+
@Override
518+
public void writeTo(BufferedSink sink) throws IOException {
519+
bufferedSource.readAll(sink);
520+
}
521+
511522
@Override
512523
public void close() throws IOException {
513524
source.close();

binapi-client/src/main/java/com/pcloud/networking/client/ResponseBody.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.pcloud.networking.protocol.ProtocolReader;
2020
import com.pcloud.networking.protocol.ValueReader;
21+
import okio.BufferedSink;
2122
import okio.ByteString;
2223

2324
import java.io.Closeable;
@@ -97,6 +98,14 @@ public final byte[] valuesByteArray() throws IOException {
9798
*/
9899
public abstract Endpoint endpoint();
99100

101+
/**
102+
* Write the body content to a {@linkplain BufferedSink}
103+
*
104+
* @param sink the receiver of the data
105+
* @throws IOException on failed IO operations
106+
*/
107+
public abstract void writeTo(BufferedSink sink) throws IOException;
108+
100109
@Override
101110
public String toString() {
102111
return String.format("(%s)->[Response]: %d bytes", endpoint(), contentLength());

binapi-client/src/test/java/com/pcloud/networking/client/RealApiChannelTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,17 @@ public void writer_Writes_To_Connection_Sink() throws Exception {
113113

114114
private ByteString mockResponse() throws IOException {
115115
return new ResponseBytesWriter()
116+
.beginObject()
116117
.writeValue("result", 5000)
118+
.endObject()
117119
.bytes();
118120
}
119121

120122
private ByteString mockDataResponse() throws IOException {
121123
return new ResponseBytesWriter()
124+
.beginObject()
122125
.setData(ByteString.encodeUtf8("Some data"))
126+
.endObject()
123127
.bytes();
124128
}
125129

@@ -212,7 +216,7 @@ public void Reader_endResponse_Throws_ClosedChannelException_On_Closed_Instance(
212216
public void Reader_beginArray_Throws_ClosedChannelException_On_Closed_Instance() throws Exception {
213217
ApiChannel apiChannel = createChannelInstance();
214218
connection.readBuffer().write(mockResponse());
215-
apiChannel.reader().beginResponse();
219+
apiChannel.reader().beginResponse();
216220
apiChannel.close();
217221
expectException(apiChannel.reader(), ClosedChannelException.class).beginArray();
218222
}
@@ -221,14 +225,18 @@ public void Reader_beginArray_Throws_ClosedChannelException_On_Closed_Instance()
221225
public void Reader_endArray_Throws_ClosedChannelException_On_Closed_Instance() throws Exception {
222226
ApiChannel apiChannel = createChannelInstance();
223227
connection.readBuffer().write(new ResponseBytesWriter()
228+
.beginObject()
229+
.writeKey("array")
224230
.beginArray()
225231
.write("1")
226232
.endArray()
233+
.endObject()
227234
.bytes());
228235

229236
ProtocolResponseReader reader = apiChannel.reader();
230237
reader.beginResponse();
231238
reader.beginObject();
239+
reader.readString();
232240
reader.beginArray();
233241
apiChannel.close();
234242
expectException(reader, ClosedChannelException.class).endArray();

binapi-client/src/test/java/com/pcloud/networking/client/RealMultiCallTest.java

Lines changed: 45 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,20 @@
1717
package com.pcloud.networking.client;
1818

1919
import com.pcloud.networking.protocol.ResponseBytesWriter;
20+
import okio.ByteString;
2021
import org.assertj.core.api.ThrowableAssert;
21-
import org.junit.AfterClass;
22-
import org.junit.Before;
23-
import org.junit.BeforeClass;
24-
import org.junit.Rule;
25-
import org.junit.Test;
22+
import org.junit.*;
2623
import org.junit.rules.ExpectedException;
2724
import org.mockito.ArgumentCaptor;
2825
import org.mockito.invocation.InvocationOnMock;
2926
import org.mockito.stubbing.Answer;
3027

3128
import java.io.IOException;
3229
import java.nio.ByteBuffer;
33-
import java.util.ArrayList;
34-
import java.util.Arrays;
35-
import java.util.Collection;
36-
import java.util.Collections;
37-
import java.util.List;
38-
import java.util.Map;
39-
import java.util.concurrent.ExecutorService;
40-
import java.util.concurrent.SynchronousQueue;
41-
import java.util.concurrent.ThreadFactory;
42-
import java.util.concurrent.ThreadPoolExecutor;
43-
import java.util.concurrent.TimeUnit;
44-
import java.util.concurrent.TimeoutException;
30+
import java.util.*;
31+
import java.util.concurrent.*;
4532

4633
import static org.assertj.core.api.Assertions.assertThatThrownBy;
47-
import static org.hamcrest.CoreMatchers.hasItem;
4834
import static org.junit.Assert.*;
4935
import static org.mockito.Mockito.*;
5036

@@ -270,7 +256,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
270256
verify(connection).close();
271257
}
272258

273-
@Test
259+
@Test
274260
public void test_Interactor_WritesRequests() throws IOException {
275261

276262
Request request1 = Request.create()
@@ -292,26 +278,26 @@ public void test_Interactor_WritesRequests() throws IOException {
292278
.build())
293279
.build();
294280

295-
Request expectedRequest1 = Request.create()
296-
.methodName("someApiMethod")
297-
.body(TestRequestBody.create()
298-
.setValue("id", 0)
299-
.setValue("arg1", "value1")
300-
.setValue("arg2", "value2")
301-
.setValue("arg3", "value3")
302-
.setValue("arg4", "value4")
303-
.build())
304-
.build();
305-
306-
Request expectedRequest2 = request1.newRequest()
307-
.body(TestRequestBody.create()
308-
.setValue("id", 1)
309-
.setValue("arg5", "value5")
310-
.setValue("arg6", "value6")
311-
.setValue("arg7", "value7")
312-
.setValue("arg8", "value8")
313-
.build())
314-
.build();
281+
Request expectedRequest1 = Request.create()
282+
.methodName("someApiMethod")
283+
.body(TestRequestBody.create()
284+
.setValue("id", 0)
285+
.setValue("arg1", "value1")
286+
.setValue("arg2", "value2")
287+
.setValue("arg3", "value3")
288+
.setValue("arg4", "value4")
289+
.build())
290+
.build();
291+
292+
Request expectedRequest2 = request1.newRequest()
293+
.body(TestRequestBody.create()
294+
.setValue("id", 1)
295+
.setValue("arg5", "value5")
296+
.setValue("arg6", "value6")
297+
.setValue("arg7", "value7")
298+
.setValue("arg8", "value8")
299+
.build())
300+
.build();
315301

316302
Connection connection = spy(new DummyConnection());
317303
retrofitConnectionProvider(connection);
@@ -365,24 +351,30 @@ public void interactor_ReadsResponses() throws IOException {
365351

366352
List<ResponseBytesWriter> expectedResponses = Arrays.asList(
367353
new ResponseBytesWriter()
354+
.beginObject()
368355
.writeValue("id", 0)
369-
.writeValue("result", 0),
356+
.writeValue("result", 0)
357+
.endObject(),
370358
new ResponseBytesWriter()
359+
.beginObject()
371360
.writeValue("id", 1)
372361
.writeValue("result", 5000)
373-
.writeValue("error", "Something went ka-boom."),
362+
.writeValue("error", "Something went ka-boom.")
363+
.endObject(),
374364
new ResponseBytesWriter()
365+
.beginObject()
375366
.writeValue("id", 2)
376367
.writeValue("result", 2000)
377-
.writeValue("error", "Token went ka-boom."));
368+
.writeValue("error", "Token went ka-boom.")
369+
.endObject());
378370
Connection connection = spy(DummyConnection.withResponses(expectedResponses));
379371
retrofitConnectionProvider(connection);
380372

381373
MultiCall call = createMultiCall(request1, request1, request1);
382374
Interactor interactor = call.start();
383375
interactor.submitRequests(Integer.MAX_VALUE);
384376

385-
while (interactor.hasNextResponse()){
377+
while (interactor.hasNextResponse()) {
386378
assertContainsResponse(expectedResponses, interactor.nextResponse());
387379
}
388380

@@ -394,14 +386,18 @@ public void interactor_ReadsResponses() throws IOException {
394386
}
395387

396388
private static void assertContainsResponse(Collection<ResponseBytesWriter> responses, Response response) throws IOException {
397-
Map<String,?> responseValues = response.responseBody().toValues();
398-
399-
Collection<Map<String,?>> expectedValues = new ArrayList<>(responses.size());
400-
for (ResponseBytesWriter bytes : responses){
401-
expectedValues.add(bytes.toValues());
389+
ByteString responseBytes = new okio.Buffer()
390+
.writeIntLe((int) response.responseBody().contentLength())
391+
.write(response.responseBody().valuesBytes())
392+
.snapshot();
393+
for (ResponseBytesWriter writer : responses) {
394+
ByteString expected = writer.bytes();
395+
if (expected.equals(responseBytes)) {
396+
return;
397+
}
402398
}
403399

404-
assertThat(expectedValues, hasItem(responseValues));
400+
throw new AssertionError("Response does not match any of the expected responses.");
405401
}
406402

407403
private byte[] getMockByteDataResponse(int numberOfRequests) {

0 commit comments

Comments
 (0)