-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ID-1570 - Fix dice-where and update dce-id to use the latest version (#…
…113) * ID-1570 - Alternative way; * ID-1570 - working version; * ID-1570 - unnecessary code; * ID-1570 - unnecessary imports;
- Loading branch information
Showing
6 changed files
with
182 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...src/test/java/technology/dice/dicewhere/downloader/stream/StreamWithMD5DecoratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package technology.dice.dicewhere.downloader.stream; | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; | ||
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; | ||
|
||
import com.github.tomakehurst.wiremock.client.WireMock; | ||
import com.github.tomakehurst.wiremock.junit.WireMockRule; | ||
import com.github.tomakehurst.wiremock.matching.UrlPattern; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.net.HttpURLConnection; | ||
import java.net.URISyntaxException; | ||
import java.net.URL; | ||
import java.nio.charset.Charset; | ||
import java.nio.file.Path; | ||
import java.security.NoSuchAlgorithmException; | ||
import junit.framework.TestCase; | ||
import org.apache.commons.io.IOUtils; | ||
import org.junit.BeforeClass; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
import org.junit.internal.runners.JUnit4ClassRunner; | ||
import org.junit.runner.RunWith; | ||
|
||
@RunWith(JUnit4ClassRunner.class) | ||
public class StreamWithMD5DecoratorTest extends TestCase { | ||
|
||
private static final String PATH = "/maxmind/maxmind-city-1.zip"; | ||
|
||
@ClassRule static WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort()); | ||
|
||
@BeforeClass | ||
public static void beforeClass() { | ||
wireMockRule.start(); | ||
} | ||
|
||
@Test | ||
public void shouldSuccessfullyReadAndCalculateDigestOfStream() | ||
throws IOException, NoSuchAlgorithmException, URISyntaxException { | ||
Path path = Path.of(getClass().getResource(PATH).toURI()); | ||
StreamWithMD5Decorator is = StreamWithMD5Decorator.of(new FileInputStream(path.toFile())); | ||
|
||
// Exhaust stream for the complete hash digest | ||
byte[] buffer = new byte[8192]; | ||
while ((is.read(buffer)) != -1) {} | ||
|
||
String first = is.md5().stringFormat(); | ||
IOUtils.toString(is, Charset.defaultCharset()); | ||
|
||
// Assert the Stream Hash before and after | ||
assertEquals(first, is.md5().stringFormat()); | ||
assertEquals(first, "9c7dd68c8352f1c59a33efe0dca04f06"); | ||
} | ||
|
||
@Test | ||
public void shouldSuccessfullyReadAndCalculateDigestOfStreamFromHttp() | ||
throws IOException, NoSuchAlgorithmException, URISyntaxException { | ||
Path path = Path.of(getClass().getResource(PATH).toURI()); | ||
|
||
wireMockRule.stubFor( | ||
WireMock.get(UrlPattern.ANY) | ||
.willReturn( | ||
aResponse().withBody(IOUtils.toByteArray(new FileInputStream(path.toFile()))))); | ||
|
||
URL url = new URL("http://localhost:" + wireMockRule.port() + "/data/file.mdb"); | ||
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | ||
|
||
StreamWithMD5Decorator is = StreamWithMD5Decorator.of(connection.getInputStream()); | ||
// Exhaust stream for the complete hash digest | ||
byte[] buffer = new byte[8192]; | ||
while ((is.read(buffer)) != -1) {} | ||
|
||
String first = is.md5().stringFormat(); | ||
// Read from the stream | ||
IOUtils.toString(is, Charset.defaultCharset()); | ||
|
||
// Assert the Stream Hash before and after | ||
assertEquals(first, is.md5().stringFormat()); | ||
assertEquals(first, "9c7dd68c8352f1c59a33efe0dca04f06"); | ||
} | ||
} |
Binary file added
BIN
+1.86 KB
dice-where-downloader-lib/src/test/resources/maxmind/maxmind-city-1.zip
Binary file not shown.