Skip to content

Commit ecd5008

Browse files
committed
a little test prefactoring for making a download monitor
1 parent aabfb90 commit ecd5008

File tree

5 files changed

+169
-63
lines changed

5 files changed

+169
-63
lines changed

unirest/src/test/java/BehaviorTests/AsFileTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public void canSaveContentsIntoFileAsyncWithCallback() throws Exception {
102102

103103
@Test
104104
public void canDownloadABinaryFile() throws Exception {
105-
File f1 = TestUtil.rezFile("/image.jpg");
105+
File f1 = TestUtil.rezFile("/spidey.jpg");
106106

107107
File f2 = Unirest.get(MockServer.BINARYFILE)
108108
.asFile(test.toString())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* The MIT License
3+
*
4+
* Copyright for portions of unirest-java are held by Kong Inc (c) 2013.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining
7+
* a copy of this software and associated documentation files (the
8+
* "Software"), to deal in the Software without restriction, including
9+
* without limitation the rights to use, copy, modify, merge, publish,
10+
* distribute, sublicense, and/or sell copies of the Software, and to
11+
* permit persons to whom the Software is furnished to do so, subject to
12+
* the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be
15+
* included in all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
package BehaviorTests;
27+
28+
import kong.unirest.Unirest;
29+
import org.junit.Rule;
30+
import org.junit.Test;
31+
import org.junit.rules.TemporaryFolder;
32+
33+
import java.io.File;
34+
import java.io.IOException;
35+
36+
import static kong.unirest.TestUtil.rezFile;
37+
38+
public class DownloadProgressTest extends BddTest {
39+
@Rule
40+
public TemporaryFolder disk = new TemporaryFolder();
41+
private File targeFolder;
42+
43+
private TestMonitor monitor;
44+
private File spidey;
45+
46+
@Override
47+
public void setUp() {
48+
super.setUp();
49+
this.monitor = new TestMonitor();
50+
spidey = rezFile("/spidey.jpg");
51+
52+
try {
53+
targeFolder = disk.newFolder("test");
54+
} catch (IOException e) {
55+
throw new RuntimeException("waaaaaarg", e);
56+
}
57+
}
58+
59+
@Test
60+
public void canAddUploadProgress() {
61+
Unirest.post(MockServer.BINARYFILE)
62+
//.uploadMonitor(monitor)
63+
.asFile(targeFolder.getPath());
64+
65+
//assertSpideyFileUpload("spidey.jpg");
66+
}
67+
68+
}

unirest/src/test/java/BehaviorTests/MockServer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ private static Object notFound(Request req, Response res) {
165165
}
166166

167167
private static Object file(Request request, Response response) throws Exception {
168-
File f = TestUtil.rezFile("/image.jpg");
168+
File f = TestUtil.rezFile("/spidey.jpg");
169169
response.raw().setContentType("application/octet-stream");
170170
response.raw().setHeader("Content-Disposition", "attachment;filename=image.jpg");
171171
response.status(200);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* The MIT License
3+
*
4+
* Copyright for portions of unirest-java are held by Kong Inc (c) 2013.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining
7+
* a copy of this software and associated documentation files (the
8+
* "Software"), to deal in the Software without restriction, including
9+
* without limitation the rights to use, copy, modify, merge, publish,
10+
* distribute, sublicense, and/or sell copies of the Software, and to
11+
* permit persons to whom the Software is furnished to do so, subject to
12+
* the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be
15+
* included in all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
package BehaviorTests;
27+
28+
import com.google.common.base.Strings;
29+
import kong.unirest.ProgressMonitor;
30+
31+
import java.io.File;
32+
import java.util.ArrayList;
33+
import java.util.HashMap;
34+
import java.util.List;
35+
import java.util.Map;
36+
import java.util.stream.Stream;
37+
38+
import static java.util.Arrays.asList;
39+
import static kong.unirest.TestUtil.defaultIfNull;
40+
import static kong.unirest.TestUtil.rezFile;
41+
import static org.junit.Assert.assertEquals;
42+
43+
class TestMonitor implements ProgressMonitor {
44+
public File spidey = rezFile("/spidey.jpg");
45+
46+
private Map<String, Stats> stats = new HashMap<>();
47+
48+
@Override
49+
public void accept(String field, String file, Long bytesWritten, Long totalBytes) {
50+
String key = firstNotEmpty(file, field);
51+
stats.compute(key, (f, s) -> {
52+
s = defaultIfNull(s, Stats::new);
53+
s.progress.add(bytesWritten);
54+
s.timesCalled++;
55+
s.total = totalBytes;
56+
return s;
57+
});
58+
}
59+
60+
private String firstNotEmpty(String... s) {
61+
return Stream.of(s)
62+
.filter(string -> !Strings.isNullOrEmpty(string))
63+
.findFirst()
64+
.orElse("");
65+
}
66+
67+
public Stats get(String fineName) {
68+
return stats.getOrDefault(fineName, new Stats());
69+
}
70+
71+
public void assertSpideyFileUpload() {
72+
assertSpideyFileUpload(spidey.getName());
73+
}
74+
75+
public void assertSpideyFileUpload(String name) {
76+
Stats stat = get(name);
77+
assertEquals(12, stat.timesCalled);
78+
assertEquals(asList(4096L, 8192L, 12288L, 16384L, 20480L, 24576L, 28672L,
79+
32768L, 36864L, 40960L, 45056L, 46246L), stat.progress);
80+
assertEquals(spidey.length(), stat.total);
81+
}
82+
83+
static class Stats {
84+
List<Long> progress = new ArrayList<>();
85+
long total;
86+
long timesCalled;
87+
}
88+
}

unirest/src/test/java/BehaviorTests/UploadProgressTest.java

+11-61
Original file line numberDiff line numberDiff line change
@@ -25,122 +25,72 @@
2525

2626
package BehaviorTests;
2727

28-
import com.google.common.base.Strings;
29-
import kong.unirest.ProgressMonitor;
3028
import kong.unirest.Unirest;
3129
import org.junit.Test;
3230

33-
import java.io.File;
3431
import java.io.FileInputStream;
35-
import java.util.ArrayList;
36-
import java.util.HashMap;
37-
import java.util.List;
38-
import java.util.Map;
39-
import java.util.stream.Stream;
4032

4133
import static java.util.Arrays.asList;
42-
import static kong.unirest.TestUtil.defaultIfNull;
4334
import static kong.unirest.TestUtil.rezFile;
4435
import static org.junit.Assert.assertEquals;
4536

4637
public class UploadProgressTest extends BddTest {
47-
private static class Monitor implements ProgressMonitor {
48-
private Map<String, Stats> stats = new HashMap<>();
49-
50-
@Override
51-
public void accept(String field, String file, Long bytesWritten, Long totalBytes) {
52-
String key = firstNotEmpty(file, field);
53-
stats.compute(key, (f, s) -> {
54-
s = defaultIfNull(s, Stats::new);
55-
s.progress.add(bytesWritten);
56-
s.timesCalled++;
57-
s.total = totalBytes;
58-
return s;
59-
});
60-
}
61-
62-
private String firstNotEmpty(String... s) {
63-
return Stream.of(s)
64-
.filter(string -> !Strings.isNullOrEmpty(string))
65-
.findFirst()
66-
.orElse("");
67-
}
68-
69-
public Stats get(String fineName) {
70-
return stats.getOrDefault(fineName, new Stats());
71-
}
72-
73-
static class Stats {
74-
List<Long> progress = new ArrayList<>();
75-
long total;
76-
long timesCalled;
77-
}
78-
}
7938

80-
private Monitor monitor;
81-
private File spidey;
39+
private TestMonitor monitor;
8240

8341
@Override
8442
public void setUp() {
8543
super.setUp();
86-
this.monitor = new Monitor();
87-
spidey = rezFile("/spidey.jpg");
44+
this.monitor = new TestMonitor();
8845
}
8946

9047
@Test
9148
public void canAddUploadProgress() {
9249
Unirest.post(MockServer.POST)
93-
.field("spidey", this.spidey)
50+
.field("spidey", monitor.spidey)
9451
.uploadMonitor(monitor)
9552
.asEmpty();
9653

97-
assertSpideyFileUpload("spidey.jpg");
54+
monitor.assertSpideyFileUpload();
9855
}
9956

10057
@Test
10158
public void canAddUploadProgressAsync() throws Exception {
10259
Unirest.post(MockServer.POST)
103-
.field("spidey", spidey)
60+
.field("spidey", monitor.spidey)
10461
.uploadMonitor(monitor)
10562
.asEmpty();
10663

107-
assertSpideyFileUpload("spidey.jpg");
64+
monitor.assertSpideyFileUpload();
10865
}
10966

11067
@Test
11168
public void canKeepTrackOfMultipleFiles() {
11269
Unirest.post(MockServer.POST)
113-
.field("spidey", this.spidey)
70+
.field("spidey", monitor.spidey)
11471
.field("other", rezFile("/test"))
11572
.uploadMonitor(monitor)
11673
.asEmpty();
11774

118-
assertSpideyFileUpload("spidey.jpg");
75+
monitor.assertSpideyFileUpload();
11976
assertOtherFileUpload();
12077
}
12178

12279
@Test
12380
public void canMonitorIfPassedAsInputStream() throws Exception {
12481
Unirest.post(MockServer.POST)
125-
.field("spidey", new FileInputStream(spidey))
82+
.field("spidey", new FileInputStream(monitor.spidey))
12683
.uploadMonitor(monitor)
12784
.asEmpty();
12885

129-
assertSpideyFileUpload("spidey");
86+
monitor.assertSpideyFileUpload("spidey");
13087
}
13188

13289
private void assertOtherFileUpload() {
133-
Monitor.Stats stat = monitor.get("test");
90+
TestMonitor.Stats stat = monitor.get("test");
13491
assertEquals(1, stat.timesCalled);
13592
assertEquals(asList(19L), stat.progress);
13693
assertEquals(19L, stat.total);
13794
}
13895

139-
private void assertSpideyFileUpload(String name) {
140-
Monitor.Stats stat = monitor.get(name);
141-
assertEquals(12, stat.timesCalled);
142-
assertEquals(asList(4096L, 8192L, 12288L, 16384L, 20480L, 24576L, 28672L,
143-
32768L, 36864L, 40960L, 45056L, 46246L), stat.progress);
144-
assertEquals(this.spidey.length(), stat.total);
145-
}
14696
}

0 commit comments

Comments
 (0)