Skip to content

Commit

Permalink
a little test prefactoring for making a download monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
ryber committed Feb 19, 2020
1 parent aabfb90 commit ecd5008
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 63 deletions.
2 changes: 1 addition & 1 deletion unirest/src/test/java/BehaviorTests/AsFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void canSaveContentsIntoFileAsyncWithCallback() throws Exception {

@Test
public void canDownloadABinaryFile() throws Exception {
File f1 = TestUtil.rezFile("/image.jpg");
File f1 = TestUtil.rezFile("/spidey.jpg");

File f2 = Unirest.get(MockServer.BINARYFILE)
.asFile(test.toString())
Expand Down
68 changes: 68 additions & 0 deletions unirest/src/test/java/BehaviorTests/DownloadProgressTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* The MIT License
*
* Copyright for portions of unirest-java are held by Kong Inc (c) 2013.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package BehaviorTests;

import kong.unirest.Unirest;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.IOException;

import static kong.unirest.TestUtil.rezFile;

public class DownloadProgressTest extends BddTest {
@Rule
public TemporaryFolder disk = new TemporaryFolder();
private File targeFolder;

private TestMonitor monitor;
private File spidey;

@Override
public void setUp() {
super.setUp();
this.monitor = new TestMonitor();
spidey = rezFile("/spidey.jpg");

try {
targeFolder = disk.newFolder("test");
} catch (IOException e) {
throw new RuntimeException("waaaaaarg", e);
}
}

@Test
public void canAddUploadProgress() {
Unirest.post(MockServer.BINARYFILE)
//.uploadMonitor(monitor)
.asFile(targeFolder.getPath());

//assertSpideyFileUpload("spidey.jpg");
}

}
2 changes: 1 addition & 1 deletion unirest/src/test/java/BehaviorTests/MockServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ private static Object notFound(Request req, Response res) {
}

private static Object file(Request request, Response response) throws Exception {
File f = TestUtil.rezFile("/image.jpg");
File f = TestUtil.rezFile("/spidey.jpg");
response.raw().setContentType("application/octet-stream");
response.raw().setHeader("Content-Disposition", "attachment;filename=image.jpg");
response.status(200);
Expand Down
88 changes: 88 additions & 0 deletions unirest/src/test/java/BehaviorTests/TestMonitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* The MIT License
*
* Copyright for portions of unirest-java are held by Kong Inc (c) 2013.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package BehaviorTests;

import com.google.common.base.Strings;
import kong.unirest.ProgressMonitor;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import static java.util.Arrays.asList;
import static kong.unirest.TestUtil.defaultIfNull;
import static kong.unirest.TestUtil.rezFile;
import static org.junit.Assert.assertEquals;

class TestMonitor implements ProgressMonitor {
public File spidey = rezFile("/spidey.jpg");

private Map<String, Stats> stats = new HashMap<>();

@Override
public void accept(String field, String file, Long bytesWritten, Long totalBytes) {
String key = firstNotEmpty(file, field);
stats.compute(key, (f, s) -> {
s = defaultIfNull(s, Stats::new);
s.progress.add(bytesWritten);
s.timesCalled++;
s.total = totalBytes;
return s;
});
}

private String firstNotEmpty(String... s) {
return Stream.of(s)
.filter(string -> !Strings.isNullOrEmpty(string))
.findFirst()
.orElse("");
}

public Stats get(String fineName) {
return stats.getOrDefault(fineName, new Stats());
}

public void assertSpideyFileUpload() {
assertSpideyFileUpload(spidey.getName());
}

public void assertSpideyFileUpload(String name) {
Stats stat = get(name);
assertEquals(12, stat.timesCalled);
assertEquals(asList(4096L, 8192L, 12288L, 16384L, 20480L, 24576L, 28672L,
32768L, 36864L, 40960L, 45056L, 46246L), stat.progress);
assertEquals(spidey.length(), stat.total);
}

static class Stats {
List<Long> progress = new ArrayList<>();
long total;
long timesCalled;
}
}
72 changes: 11 additions & 61 deletions unirest/src/test/java/BehaviorTests/UploadProgressTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,122 +25,72 @@

package BehaviorTests;

import com.google.common.base.Strings;
import kong.unirest.ProgressMonitor;
import kong.unirest.Unirest;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import static java.util.Arrays.asList;
import static kong.unirest.TestUtil.defaultIfNull;
import static kong.unirest.TestUtil.rezFile;
import static org.junit.Assert.assertEquals;

public class UploadProgressTest extends BddTest {
private static class Monitor implements ProgressMonitor {
private Map<String, Stats> stats = new HashMap<>();

@Override
public void accept(String field, String file, Long bytesWritten, Long totalBytes) {
String key = firstNotEmpty(file, field);
stats.compute(key, (f, s) -> {
s = defaultIfNull(s, Stats::new);
s.progress.add(bytesWritten);
s.timesCalled++;
s.total = totalBytes;
return s;
});
}

private String firstNotEmpty(String... s) {
return Stream.of(s)
.filter(string -> !Strings.isNullOrEmpty(string))
.findFirst()
.orElse("");
}

public Stats get(String fineName) {
return stats.getOrDefault(fineName, new Stats());
}

static class Stats {
List<Long> progress = new ArrayList<>();
long total;
long timesCalled;
}
}

private Monitor monitor;
private File spidey;
private TestMonitor monitor;

@Override
public void setUp() {
super.setUp();
this.monitor = new Monitor();
spidey = rezFile("/spidey.jpg");
this.monitor = new TestMonitor();
}

@Test
public void canAddUploadProgress() {
Unirest.post(MockServer.POST)
.field("spidey", this.spidey)
.field("spidey", monitor.spidey)
.uploadMonitor(monitor)
.asEmpty();

assertSpideyFileUpload("spidey.jpg");
monitor.assertSpideyFileUpload();
}

@Test
public void canAddUploadProgressAsync() throws Exception {
Unirest.post(MockServer.POST)
.field("spidey", spidey)
.field("spidey", monitor.spidey)
.uploadMonitor(monitor)
.asEmpty();

assertSpideyFileUpload("spidey.jpg");
monitor.assertSpideyFileUpload();
}

@Test
public void canKeepTrackOfMultipleFiles() {
Unirest.post(MockServer.POST)
.field("spidey", this.spidey)
.field("spidey", monitor.spidey)
.field("other", rezFile("/test"))
.uploadMonitor(monitor)
.asEmpty();

assertSpideyFileUpload("spidey.jpg");
monitor.assertSpideyFileUpload();
assertOtherFileUpload();
}

@Test
public void canMonitorIfPassedAsInputStream() throws Exception {
Unirest.post(MockServer.POST)
.field("spidey", new FileInputStream(spidey))
.field("spidey", new FileInputStream(monitor.spidey))
.uploadMonitor(monitor)
.asEmpty();

assertSpideyFileUpload("spidey");
monitor.assertSpideyFileUpload("spidey");
}

private void assertOtherFileUpload() {
Monitor.Stats stat = monitor.get("test");
TestMonitor.Stats stat = monitor.get("test");
assertEquals(1, stat.timesCalled);
assertEquals(asList(19L), stat.progress);
assertEquals(19L, stat.total);
}

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

0 comments on commit ecd5008

Please sign in to comment.