From ecd5008bed4381e6f94628bca11bdc35e75761a5 Mon Sep 17 00:00:00 2001 From: Ryan Bergman Date: Wed, 19 Feb 2020 07:26:14 -0600 Subject: [PATCH] a little test prefactoring for making a download monitor --- .../test/java/BehaviorTests/AsFileTest.java | 2 +- .../BehaviorTests/DownloadProgressTest.java | 68 ++++++++++++++ .../test/java/BehaviorTests/MockServer.java | 2 +- .../test/java/BehaviorTests/TestMonitor.java | 88 +++++++++++++++++++ .../BehaviorTests/UploadProgressTest.java | 72 +++------------ 5 files changed, 169 insertions(+), 63 deletions(-) create mode 100644 unirest/src/test/java/BehaviorTests/DownloadProgressTest.java create mode 100644 unirest/src/test/java/BehaviorTests/TestMonitor.java diff --git a/unirest/src/test/java/BehaviorTests/AsFileTest.java b/unirest/src/test/java/BehaviorTests/AsFileTest.java index e66557f44..841db021a 100644 --- a/unirest/src/test/java/BehaviorTests/AsFileTest.java +++ b/unirest/src/test/java/BehaviorTests/AsFileTest.java @@ -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()) diff --git a/unirest/src/test/java/BehaviorTests/DownloadProgressTest.java b/unirest/src/test/java/BehaviorTests/DownloadProgressTest.java new file mode 100644 index 000000000..8f75f17dc --- /dev/null +++ b/unirest/src/test/java/BehaviorTests/DownloadProgressTest.java @@ -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"); + } + +} diff --git a/unirest/src/test/java/BehaviorTests/MockServer.java b/unirest/src/test/java/BehaviorTests/MockServer.java index b2bb88d13..71ac97417 100644 --- a/unirest/src/test/java/BehaviorTests/MockServer.java +++ b/unirest/src/test/java/BehaviorTests/MockServer.java @@ -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); diff --git a/unirest/src/test/java/BehaviorTests/TestMonitor.java b/unirest/src/test/java/BehaviorTests/TestMonitor.java new file mode 100644 index 000000000..ff4730a18 --- /dev/null +++ b/unirest/src/test/java/BehaviorTests/TestMonitor.java @@ -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 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 progress = new ArrayList<>(); + long total; + long timesCalled; + } +} diff --git a/unirest/src/test/java/BehaviorTests/UploadProgressTest.java b/unirest/src/test/java/BehaviorTests/UploadProgressTest.java index e0fbe6a67..8dd9dfc19 100644 --- a/unirest/src/test/java/BehaviorTests/UploadProgressTest.java +++ b/unirest/src/test/java/BehaviorTests/UploadProgressTest.java @@ -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 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 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); - } }