Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ID-1570 - Fix dice-where and update dce-id to use the latest version #112

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions dice-where-downloader-lib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,25 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>3.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>3.4.6</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.10.1</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
public class LocalFileAcceptor implements FileAcceptor<Void> {

private static final Logger LOG = LoggerFactory.getLogger(LocalFileAcceptor.class);
public static final int BUFFER = 8192;
private static final int BUFFER = 8192;

private final Path destination;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,34 @@

public class StreamWithMD5Decorator extends InputStream {

private final DigestInputStream inputStream;
private final MessageDigest md5;
DigestInputStream inputStream;

private StreamWithMD5Decorator(DigestInputStream inputStream, MessageDigest md5) {
this.inputStream = inputStream;
this.md5 = md5;
inputStream.on(false);
}


public static StreamWithMD5Decorator of(InputStream inputStream) throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
DigestInputStream dis = new DigestInputStream(inputStream, md5);
return new StreamWithMD5Decorator(dis, md5);
}

public MD5Checksum md5() {
String hex = (new HexBinaryAdapter()).marshal(this.md5.digest());
String hex = (new HexBinaryAdapter()).marshal(md5.digest());
snackk marked this conversation as resolved.
Show resolved Hide resolved
return MD5Checksum.of(hex);
}

@Override
public int read() throws IOException {
return this.inputStream.read();
return inputStream.read();
}

@Override
public void close() throws IOException {
this.inputStream.close();
inputStream.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package technology.dice.dicewhere.downloader.stream;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

public class StreamWithMD5DecoratorTest {

private static final String PATH = "/maxmind/maxmind-city-1.zip";

@Test
@DisplayName("Should Successfully Read a Stream and calculate the entire stream digest")
public void shouldSuccessfullyReadAndCalculateDigestOfStream()
throws IOException, NoSuchAlgorithmException, URISyntaxException {
Path path = Path.of(getClass().getResource(PATH).toURI());
StreamWithMD5Decorator is = StreamWithMD5Decorator.of(new FileInputStream(path.toFile()));

String first = is.md5().stringFormat();
//Read from the stream
IOUtils.toString(is, Charset.defaultCharset());

//Calculate multiple Hashes
is.md5().stringFormat();
is.md5().stringFormat();

//Assert the Stream Hash before and after
assertEquals(first, is.md5().stringFormat());
}
}
Binary file not shown.
Loading