[uploadselectionfilter] implement with tests

This commit is contained in:
Paul Campbell 2019-05-09 17:34:17 +01:00
parent 5090a78baa
commit 7d688954f7
3 changed files with 57 additions and 7 deletions

View file

@ -1,21 +1,31 @@
package net.kemitix.s3thorp package net.kemitix.s3thorp
import java.io.File
import fs2.Stream import fs2.Stream
import cats.effect.IO import cats.effect.IO
import net.kemitix.s3thorp.Main.putStrLn import net.kemitix.s3thorp.Sync.{Hash, LocalFile}
import java.security.{MessageDigest, DigestInputStream}
import java.io.{File, FileInputStream}
trait UploadSelectionFilter { trait UploadSelectionFilter {
private def md5File(localFile: LocalFile): Hash = {
val buffer = new Array[Byte](8192)
val md5 = MessageDigest.getInstance("MD5")
val dis = new DigestInputStream(new FileInputStream(localFile), md5)
try { while (dis.read(buffer) != -1) { } } finally { dis.close() }
md5.digest.map("%02x".format(_)).mkString
}
def uploadRequiredFilter: Either[File, S3MetaData] => Stream[IO, File] = { def uploadRequiredFilter: Either[File, S3MetaData] => Stream[IO, File] = {
case Left(file) => Stream(file) case Left(file) => Stream(file)
case Right(s3Metadata) => case Right(s3Metadata) =>
Stream.eval(for { Stream.eval(for {
_ <- putStrLn(s"upload required: ${s3Metadata.localFile}") localHash <- IO(md5File(s3Metadata.localFile))
//md5File(localFile) } yield (s3Metadata.localFile, localHash)).
//filter(localHash => options.force || localHash != metadataHash) filter { case (_, localHash) => localHash != s3Metadata.remoteHash }.
} yield s3Metadata.localFile) map {case (localFile,_) => localFile}
} }
} }

View file

@ -0,0 +1,39 @@
package net.kemitix.s3thorp
import java.io.File
import java.time.Instant
import org.scalatest.FunSpec
class UploadSelectionFilterSuite extends FunSpec {
new UploadSelectionFilter {
describe("uploadRequiredFilter") {
val localFile = new File("src/test/scala/net/kemitix/s3thorp/test-file-for-hash.txt")
val localHash = "0cbfe978783bd7950d5da4ff85e4af37"
def invokeSubject(input: Either[File, S3MetaData]) = {
uploadRequiredFilter(input).compile.toList.unsafeRunSync()
}
describe("when supplied a file") {
val input = Left(localFile)
it("should be marked for upload") {
assertResult(List(localFile))(invokeSubject(input))
}
}
describe("when supplied S3MetaData") {
describe("when hash is different") {
val input = Right(S3MetaData(localFile, "", "doesn't match any hash", Instant.now))
it("should be marked for upload") {
assertResult(List(localFile))(invokeSubject(input))
}
}
describe("when hash is the same") {
val input = Right(S3MetaData(localFile, "", localHash, Instant.now))
it("should not be marked for upload") {
assertResult(List())(invokeSubject(input))
}
}
}
}
}
}

View file

@ -0,0 +1 @@
This is a test file for checking the md5 hash generator.