From 3ded264bd90a620d05cbada16374ac3ad008aa7f Mon Sep 17 00:00:00 2001 From: Grebennikov Roman Date: Tue, 30 Aug 2016 16:14:03 +0300 Subject: [PATCH] create workdir if it does not exist --- build.sbt | 1 - .../s3mock/provider/FileProvider.scala | 3 ++ .../s3mock/ListBucketEmptyWorkdirTest.scala | 37 +++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 src/test/scala/io/findify/s3mock/ListBucketEmptyWorkdirTest.scala diff --git a/build.sbt b/build.sbt index 4516c2c..eea2afb 100644 --- a/build.sbt +++ b/build.sbt @@ -17,7 +17,6 @@ libraryDependencies ++= Seq( "org.scalatest" %% "scalatest" % "3.0.0" % "test", "com.amazonaws" % "aws-java-sdk-s3" % "1.11.29" % "test", "com.typesafe.akka" %% "akka-stream-testkit" % akkaVersion % "test", - //"com.typesafe.akka" %% "akka-slf4j" % akkaVersion, "commons-io" % "commons-io" % "2.5" % "test", "ch.qos.logback" % "logback-classic" % "1.1.7" % "test" ) diff --git a/src/main/scala/io/findify/s3mock/provider/FileProvider.scala b/src/main/scala/io/findify/s3mock/provider/FileProvider.scala index d36e203..8061e0f 100644 --- a/src/main/scala/io/findify/s3mock/provider/FileProvider.scala +++ b/src/main/scala/io/findify/s3mock/provider/FileProvider.scala @@ -15,6 +15,9 @@ import scala.util.Random * Created by shutty on 8/9/16. */ class FileProvider(dir:String) extends Provider with LazyLogging { + val workDir = File(dir) + if (!workDir.exists) workDir.createDirectories() + def listBuckets: ListAllMyBuckets = { val buckets = File(dir).list.map(f => Bucket(f.name, DateTime(f.lastModifiedTime.toEpochMilli))).toList logger.debug(s"listing buckets: ${buckets.map(_.name)}") diff --git a/src/test/scala/io/findify/s3mock/ListBucketEmptyWorkdirTest.scala b/src/test/scala/io/findify/s3mock/ListBucketEmptyWorkdirTest.scala new file mode 100644 index 0000000..37c725d --- /dev/null +++ b/src/test/scala/io/findify/s3mock/ListBucketEmptyWorkdirTest.scala @@ -0,0 +1,37 @@ +package io.findify.s3mock + +import java.util.UUID + +import scala.collection.JavaConverters._ +import com.amazonaws.auth.BasicAWSCredentials +import com.amazonaws.services.s3.AmazonS3Client +import io.findify.s3mock.provider.FileProvider +import org.scalatest.{BeforeAndAfterAll, FlatSpec, Matchers} + +/** + * Created by shutty on 8/30/16. + */ +class ListBucketEmptyWorkdirTest extends FlatSpec with Matchers with BeforeAndAfterAll { + lazy val s3 = new AmazonS3Client(new BasicAWSCredentials("hello", "world")) + + val workDir = s"/tmp/${UUID.randomUUID()}" + lazy val server = new S3Mock(8001, new FileProvider(workDir)) + + override def beforeAll = { + s3.setEndpoint("http://127.0.0.1:8001") + server.start + } + override def afterAll = { + server.stop + } + + "s3mock" should "list bucket with empty prefix" in { + s3.createBucket("list") + s3.putObject("list", "foo1", "xxx") + s3.putObject("list", "foo2", "xxx") + val list = s3.listObjects("list").getObjectSummaries.asScala.toList + list.map(_.getKey).forall(_.startsWith("/foo")) shouldBe true + } + + +}