Skip to content

Commit 753c71a

Browse files
committed
cleanup
1 parent 6e6c3e6 commit 753c71a

5 files changed

Lines changed: 93 additions & 131 deletions

File tree

‎spring-rest/src/test/java/org/baeldung/okhttp/OkHttpFileUploadingLiveTest.java‎

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package org.baeldung.okhttp;
22

3+
import static org.baeldung.client.Consts.APPLICATION_PORT;
34
import static org.hamcrest.Matchers.equalTo;
45
import static org.junit.Assert.assertFalse;
56
import static org.junit.Assert.assertThat;
67

78
import java.io.File;
89
import java.io.IOException;
910

10-
import org.baeldung.okhttp.ProgressRequestWrapper;
11-
import org.junit.Before;
12-
import org.junit.Test;
13-
1411
import okhttp3.Call;
1512
import okhttp3.MediaType;
1613
import okhttp3.MultipartBody;
@@ -19,59 +16,48 @@
1916
import okhttp3.RequestBody;
2017
import okhttp3.Response;
2118

19+
import org.junit.Before;
20+
import org.junit.Test;
21+
2222
public class OkHttpFileUploadingLiveTest {
2323

24-
private static final String BASE_URL = "http://localhost:8080/spring-rest";
24+
private static final String BASE_URL = "http://localhost:" + APPLICATION_PORT + "/spring-rest";
2525

2626
OkHttpClient client;
2727

2828
@Before
2929
public void init() {
30-
client = new OkHttpClient();
30+
client = new OkHttpClient();
3131
}
3232

3333
@Test
3434
public void whenUploadFile_thenCorrect() throws IOException {
3535

36-
RequestBody requestBody = new MultipartBody.Builder()
37-
.setType(MultipartBody.FORM)
38-
.addFormDataPart("file", "file.txt",
39-
RequestBody.create(MediaType.parse("application/octet-stream"), new File("src/test/resources/test.txt")))
40-
.build();
36+
final RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("file", "file.txt", RequestBody.create(MediaType.parse("application/octet-stream"), new File("src/test/resources/test.txt"))).build();
4137

42-
Request request = new Request.Builder()
43-
.url(BASE_URL + "/users/upload")
44-
.post(requestBody)
45-
.build();
38+
final Request request = new Request.Builder().url(BASE_URL + "/users/upload").post(requestBody).build();
4639

47-
Call call = client.newCall(request);
48-
Response response = call.execute();
40+
final Call call = client.newCall(request);
41+
final Response response = call.execute();
4942

5043
assertThat(response.code(), equalTo(200));
5144
}
5245

5346
@Test
5447
public void whenGetUploadFileProgress_thenCorrect() throws IOException {
5548

56-
RequestBody requestBody = new MultipartBody.Builder()
57-
.setType(MultipartBody.FORM)
58-
.addFormDataPart("file", "file.txt",
59-
RequestBody.create(MediaType.parse("application/octet-stream"), new File("src/test/resources/test.txt")))
60-
.build();
49+
final RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("file", "file.txt", RequestBody.create(MediaType.parse("application/octet-stream"), new File("src/test/resources/test.txt"))).build();
6150

62-
ProgressRequestWrapper countingBody = new ProgressRequestWrapper(requestBody, (long bytesWritten, long contentLength) -> {
51+
final ProgressRequestWrapper countingBody = new ProgressRequestWrapper(requestBody, (long bytesWritten, long contentLength) -> {
6352

64-
float percentage = 100f * bytesWritten / contentLength;
53+
final float percentage = (100f * bytesWritten) / contentLength;
6554
assertFalse(Float.compare(percentage, 100) > 0);
6655
});
6756

68-
Request request = new Request.Builder()
69-
.url(BASE_URL + "/users/upload")
70-
.post(countingBody)
71-
.build();
57+
final Request request = new Request.Builder().url(BASE_URL + "/users/upload").post(countingBody).build();
7258

73-
Call call = client.newCall(request);
74-
Response response = call.execute();
59+
final Call call = client.newCall(request);
60+
final Response response = call.execute();
7561

7662
assertThat(response.code(), equalTo(200));
7763

‎spring-rest/src/test/java/org/baeldung/okhttp/OkHttpGetLiveTest.java‎

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
package org.baeldung.okhttp;
22

3+
import static org.baeldung.client.Consts.APPLICATION_PORT;
34
import static org.hamcrest.CoreMatchers.equalTo;
45
import static org.junit.Assert.assertThat;
56
import static org.junit.Assert.fail;
67

78
import java.io.IOException;
89

9-
import org.junit.Before;
10-
import org.junit.Test;
11-
1210
import okhttp3.Call;
1311
import okhttp3.Callback;
1412
import okhttp3.HttpUrl;
1513
import okhttp3.OkHttpClient;
1614
import okhttp3.Request;
1715
import okhttp3.Response;
1816

17+
import org.junit.Before;
18+
import org.junit.Test;
19+
1920
public class OkHttpGetLiveTest {
2021

21-
private static final String BASE_URL = "http://localhost:8080/spring-rest";
22+
private static final String BASE_URL = "http://localhost:" + APPLICATION_PORT + "/spring-rest";
2223

2324
OkHttpClient client;
2425

@@ -30,40 +31,42 @@ public void init() {
3031

3132
@Test
3233
public void whenGetRequest_thenCorrect() throws IOException {
33-
Request request = new Request.Builder().url(BASE_URL + "/date").build();
34+
final Request request = new Request.Builder().url(BASE_URL + "/date").build();
3435

35-
Call call = client.newCall(request);
36-
Response response = call.execute();
36+
final Call call = client.newCall(request);
37+
final Response response = call.execute();
3738

3839
assertThat(response.code(), equalTo(200));
3940
}
4041

4142
@Test
4243
public void whenGetRequestWithQueryParameter_thenCorrect() throws IOException {
43-
HttpUrl.Builder urlBuilder = HttpUrl.parse(BASE_URL + "/ex/bars").newBuilder();
44+
final HttpUrl.Builder urlBuilder = HttpUrl.parse(BASE_URL + "/ex/bars").newBuilder();
4445
urlBuilder.addQueryParameter("id", "1");
4546

46-
String url = urlBuilder.build().toString();
47+
final String url = urlBuilder.build().toString();
4748

48-
Request request = new Request.Builder().url(url).build();
49+
final Request request = new Request.Builder().url(url).build();
4950

50-
Call call = client.newCall(request);
51-
Response response = call.execute();
51+
final Call call = client.newCall(request);
52+
final Response response = call.execute();
5253

5354
assertThat(response.code(), equalTo(200));
5455
}
5556

5657
@Test
5758
public void whenAsynchronousGetRequest_thenCorrect() throws InterruptedException {
58-
Request request = new Request.Builder().url(BASE_URL + "/date").build();
59+
final Request request = new Request.Builder().url(BASE_URL + "/date").build();
5960

60-
Call call = client.newCall(request);
61+
final Call call = client.newCall(request);
6162

6263
call.enqueue(new Callback() {
64+
@Override
6365
public void onResponse(Call call, Response response) throws IOException {
6466
System.out.println("OK");
6567
}
6668

69+
@Override
6770
public void onFailure(Call call, IOException e) {
6871
fail();
6972
}

‎spring-rest/src/test/java/org/baeldung/okhttp/OkHttpMiscLiveTest.java‎

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,55 @@
11
package org.baeldung.okhttp;
22

3-
import okhttp3.*;
4-
import org.junit.Test;
5-
import org.slf4j.Logger;
6-
import org.slf4j.LoggerFactory;
3+
import static org.baeldung.client.Consts.APPLICATION_PORT;
4+
75
import java.io.File;
86
import java.io.IOException;
97
import java.util.concurrent.Executors;
108
import java.util.concurrent.ScheduledExecutorService;
119
import java.util.concurrent.TimeUnit;
12-
import org.junit.Before;
13-
import org.junit.Test;
14-
import org.slf4j.Logger;
15-
import org.slf4j.LoggerFactory;
10+
1611
import okhttp3.Cache;
1712
import okhttp3.Call;
1813
import okhttp3.OkHttpClient;
1914
import okhttp3.Request;
2015
import okhttp3.Response;
2116

17+
import org.junit.Before;
18+
import org.junit.Test;
19+
import org.slf4j.Logger;
20+
import org.slf4j.LoggerFactory;
21+
2222
public class OkHttpMiscLiveTest {
2323

24-
private static final String BASE_URL = "http://localhost:8080/spring-rest";
24+
private static final String BASE_URL = "http://localhost:" + APPLICATION_PORT + "/spring-rest";
2525
private static Logger logger = LoggerFactory.getLogger(OkHttpMiscLiveTest.class);
2626

2727
OkHttpClient client;
2828

2929
@Before
3030
public void init() {
3131

32-
client = new OkHttpClient();
32+
client = new OkHttpClient();
3333
}
3434

3535
@Test
3636
public void whenSetRequestTimeout_thenFail() throws IOException {
37-
OkHttpClient clientWithTimeout = new OkHttpClient.Builder()
38-
.readTimeout(1, TimeUnit.SECONDS)
39-
.build();
37+
final OkHttpClient clientWithTimeout = new OkHttpClient.Builder().readTimeout(1, TimeUnit.SECONDS).build();
4038

41-
Request request = new Request.Builder()
42-
.url(BASE_URL + "/delay/2") // This URL is served with a 2 second delay.
43-
.build();
39+
final Request request = new Request.Builder().url(BASE_URL + "/delay/2") // This URL is served with a 2 second delay.
40+
.build();
4441

45-
Call call = clientWithTimeout.newCall(request);
46-
Response response = call.execute();
42+
final Call call = clientWithTimeout.newCall(request);
43+
final Response response = call.execute();
4744
response.close();
4845
}
4946

5047
@Test(expected = IOException.class)
5148
public void whenCancelRequest_thenCorrect() throws IOException {
52-
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
49+
final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
5350

54-
Request request = new Request.Builder()
55-
.url(BASE_URL + "/delay/2") // This URL is served with a 2 second delay.
56-
.build();
51+
final Request request = new Request.Builder().url(BASE_URL + "/delay/2") // This URL is served with a 2 second delay.
52+
.build();
5753

5854
final int seconds = 1;
5955
final long startNanos = System.nanoTime();
@@ -63,42 +59,38 @@ public void whenCancelRequest_thenCorrect() throws IOException {
6359
// Schedule a job to cancel the call in 1 second.
6460
executor.schedule(() -> {
6561

66-
logger.debug("Canceling call: " + (System.nanoTime() - startNanos) / 1e9f);
62+
logger.debug("Canceling call: " + ((System.nanoTime() - startNanos) / 1e9f));
6763
call.cancel();
68-
logger.debug("Canceled call: " + (System.nanoTime() - startNanos) / 1e9f);
64+
logger.debug("Canceled call: " + ((System.nanoTime() - startNanos) / 1e9f));
6965

7066
}, seconds, TimeUnit.SECONDS);
7167

72-
logger.debug("Executing call: " + (System.nanoTime() - startNanos) / 1e9f);
73-
Response response = call.execute();
74-
logger.debug("Call completed: " + (System.nanoTime() - startNanos) / 1e9f, response);
68+
logger.debug("Executing call: " + ((System.nanoTime() - startNanos) / 1e9f));
69+
final Response response = call.execute();
70+
logger.debug("Call completed: " + ((System.nanoTime() - startNanos) / 1e9f), response);
7571
}
7672

7773
@Test
78-
public void whenSetResponseCache_thenCorrect() throws IOException {
74+
public void whenSetResponseCache_thenCorrect() throws IOException {
7975

80-
int cacheSize = 10 * 1024 * 1024; // 10 MiB
81-
File cacheDirectory = new File("src/test/resources/cache");
82-
Cache cache = new Cache(cacheDirectory, cacheSize);
76+
final int cacheSize = 10 * 1024 * 1024; // 10 MiB
77+
final File cacheDirectory = new File("src/test/resources/cache");
78+
final Cache cache = new Cache(cacheDirectory, cacheSize);
8379

84-
OkHttpClient clientCached = new OkHttpClient.Builder()
85-
.cache(cache)
86-
.build();
80+
final OkHttpClient clientCached = new OkHttpClient.Builder().cache(cache).build();
8781

88-
Request request = new Request.Builder()
89-
.url("http://publicobject.com/helloworld.txt")
90-
.build();
82+
final Request request = new Request.Builder().url("http://publicobject.com/helloworld.txt").build();
9183

92-
Response response1 = clientCached.newCall(request).execute();
84+
final Response response1 = clientCached.newCall(request).execute();
9385
logResponse(response1);
9486

95-
Response response2 = clientCached.newCall(request).execute();
87+
final Response response2 = clientCached.newCall(request).execute();
9688
logResponse(response2);
9789
}
9890

9991
private void logResponse(Response response) throws IOException {
10092

101-
logger.debug("Response response: " + response);
93+
logger.debug("Response response: " + response);
10294
logger.debug("Response cache response: " + response.cacheResponse());
10395
logger.debug("Response network response: " + response.networkResponse());
10496
logger.debug("Response responseBody: " + response.body().string());

0 commit comments

Comments
 (0)