|
25 | 25 |
|
26 | 26 | package BehaviorTests;
|
27 | 27 |
|
28 |
| -import com.google.common.base.Strings; |
29 |
| -import kong.unirest.ProgressMonitor; |
30 | 28 | import kong.unirest.Unirest;
|
31 | 29 | import org.junit.Test;
|
32 | 30 |
|
33 |
| -import java.io.File; |
34 | 31 | 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; |
40 | 32 |
|
41 | 33 | import static java.util.Arrays.asList;
|
42 |
| -import static kong.unirest.TestUtil.defaultIfNull; |
43 | 34 | import static kong.unirest.TestUtil.rezFile;
|
44 | 35 | import static org.junit.Assert.assertEquals;
|
45 | 36 |
|
46 | 37 | 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 |
| - } |
79 | 38 |
|
80 |
| - private Monitor monitor; |
81 |
| - private File spidey; |
| 39 | + private TestMonitor monitor; |
82 | 40 |
|
83 | 41 | @Override
|
84 | 42 | public void setUp() {
|
85 | 43 | super.setUp();
|
86 |
| - this.monitor = new Monitor(); |
87 |
| - spidey = rezFile("/spidey.jpg"); |
| 44 | + this.monitor = new TestMonitor(); |
88 | 45 | }
|
89 | 46 |
|
90 | 47 | @Test
|
91 | 48 | public void canAddUploadProgress() {
|
92 | 49 | Unirest.post(MockServer.POST)
|
93 |
| - .field("spidey", this.spidey) |
| 50 | + .field("spidey", monitor.spidey) |
94 | 51 | .uploadMonitor(monitor)
|
95 | 52 | .asEmpty();
|
96 | 53 |
|
97 |
| - assertSpideyFileUpload("spidey.jpg"); |
| 54 | + monitor.assertSpideyFileUpload(); |
98 | 55 | }
|
99 | 56 |
|
100 | 57 | @Test
|
101 | 58 | public void canAddUploadProgressAsync() throws Exception {
|
102 | 59 | Unirest.post(MockServer.POST)
|
103 |
| - .field("spidey", spidey) |
| 60 | + .field("spidey", monitor.spidey) |
104 | 61 | .uploadMonitor(monitor)
|
105 | 62 | .asEmpty();
|
106 | 63 |
|
107 |
| - assertSpideyFileUpload("spidey.jpg"); |
| 64 | + monitor.assertSpideyFileUpload(); |
108 | 65 | }
|
109 | 66 |
|
110 | 67 | @Test
|
111 | 68 | public void canKeepTrackOfMultipleFiles() {
|
112 | 69 | Unirest.post(MockServer.POST)
|
113 |
| - .field("spidey", this.spidey) |
| 70 | + .field("spidey", monitor.spidey) |
114 | 71 | .field("other", rezFile("/test"))
|
115 | 72 | .uploadMonitor(monitor)
|
116 | 73 | .asEmpty();
|
117 | 74 |
|
118 |
| - assertSpideyFileUpload("spidey.jpg"); |
| 75 | + monitor.assertSpideyFileUpload(); |
119 | 76 | assertOtherFileUpload();
|
120 | 77 | }
|
121 | 78 |
|
122 | 79 | @Test
|
123 | 80 | public void canMonitorIfPassedAsInputStream() throws Exception {
|
124 | 81 | Unirest.post(MockServer.POST)
|
125 |
| - .field("spidey", new FileInputStream(spidey)) |
| 82 | + .field("spidey", new FileInputStream(monitor.spidey)) |
126 | 83 | .uploadMonitor(monitor)
|
127 | 84 | .asEmpty();
|
128 | 85 |
|
129 |
| - assertSpideyFileUpload("spidey"); |
| 86 | + monitor.assertSpideyFileUpload("spidey"); |
130 | 87 | }
|
131 | 88 |
|
132 | 89 | private void assertOtherFileUpload() {
|
133 |
| - Monitor.Stats stat = monitor.get("test"); |
| 90 | + TestMonitor.Stats stat = monitor.get("test"); |
134 | 91 | assertEquals(1, stat.timesCalled);
|
135 | 92 | assertEquals(asList(19L), stat.progress);
|
136 | 93 | assertEquals(19L, stat.total);
|
137 | 94 | }
|
138 | 95 |
|
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 |
| - } |
146 | 96 | }
|
0 commit comments