From ccccb61c9976eebff4703ff068bb5ced1da02ad8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martynas=20Mickevi=C4=8Dius?= Date: Thu, 19 Jan 2017 18:08:25 +0200 Subject: [PATCH 1/2] Run DynamoDB local in docker This allows concurrent execution of all tests. --- build.sbt | 52 ++++++++------- docker-compose.yml | 4 ++ docs/src/main/paradox/connectors.md | 8 +-- docs/src/main/paradox/dynamodb.md | 26 +++++++- .../alpakka/dynamodb/impl/AwsClient.scala | 18 +++--- .../dynamodb/impl/DynamoClientImpl.scala | 6 +- .../dynamodb/javadsl/DynamoClient.scala | 4 +- .../dynamodb/scaladsl/DynamoClient.scala | 4 +- .../dynamodb/scaladsl/DynamoImplicits.scala | 2 +- .../alpakka/dynamodb/ExampleJavaSpec.java | 64 ------------------- .../stream/alpakka/dynamodb/ExampleTest.java | 62 ++++++++++++++++++ .../stream/alpakka/dynamodb/ExampleSpec.scala | 14 +--- .../stream/alpakka/dynamodb/ItemSpec.scala | 28 ++++---- .../stream/alpakka/dynamodb/LocalDynamo.scala | 19 ------ .../stream/alpakka/dynamodb/TableSpec.scala | 23 +++---- .../stream/alpakka/dynamodb/TestOps.scala | 12 +++- project/Dependencies.scala | 4 +- 17 files changed, 171 insertions(+), 179 deletions(-) delete mode 100644 dynamodb/src/test/java/akka/stream/alpakka/dynamodb/ExampleJavaSpec.java create mode 100644 dynamodb/src/test/java/akka/stream/alpakka/dynamodb/ExampleTest.java delete mode 100644 dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/LocalDynamo.scala diff --git a/build.sbt b/build.sbt index 32cf79172a..d81651680f 100644 --- a/build.sbt +++ b/build.sbt @@ -1,7 +1,7 @@ lazy val alpakka = project .in(file(".")) .enablePlugins(PublishUnidoc) - .aggregate(amqp, cassandra, docs, files, hbase, mqtt, s3, simpleCodecs, sqs, ftp, jms) + .aggregate(amqp, cassandra, dynamodb, docs, files, ftp, hbase, jms, mqtt, s3, simpleCodecs, sqs) lazy val amqp = project .enablePlugins(AutomateHeaderPlugin) @@ -21,9 +21,7 @@ lazy val dynamodb = project .enablePlugins(AutomateHeaderPlugin) .settings( name := "akka-stream-alpakka-dynamodb", - resolvers += "DynamoDBLocal" at "http://dynamodb-local.s3-website-us-west-2.amazonaws.com/release/", - Dependencies.DynamoDB, - parallelExecution in Test := false + Dependencies.DynamoDB ) lazy val files = project // The name file is taken by `sbt.file`! @@ -34,6 +32,14 @@ lazy val files = project // The name file is taken by `sbt.file`! Dependencies.File ) +lazy val ftp = project + .enablePlugins(AutomateHeaderPlugin) + .settings( + name := "akka-stream-alpakka-ftp", + Dependencies.Ftp, + parallelExecution in Test := false + ) + lazy val hbase = project .enablePlugins(AutomateHeaderPlugin) .settings( @@ -42,6 +48,14 @@ lazy val hbase = project fork in Test := true ) +lazy val jms = project + .enablePlugins(AutomateHeaderPlugin) + .settings( + name := "akka-stream-alpakka-jms", + Dependencies.Jms, + parallelExecution in Test := false + ) + lazy val mqtt = project .enablePlugins(AutomateHeaderPlugin) .settings( @@ -56,35 +70,19 @@ lazy val s3 = project Dependencies.S3 ) -lazy val sqs = project - .in(file("sqs")) - .enablePlugins(AutomateHeaderPlugin) - .settings( - name := "akka-stream-alpakka-sqs", - Dependencies.Sqs - ) - -lazy val ftp = project - .enablePlugins(AutomateHeaderPlugin) - .settings( - name := "akka-stream-alpakka-ftp", - Dependencies.Ftp, - parallelExecution in Test := false - ) - -lazy val jms = project +lazy val simpleCodecs = project + .in(file("simple-codecs")) .enablePlugins(AutomateHeaderPlugin) .settings( - name := "akka-stream-alpakka-jms", - Dependencies.Jms, - parallelExecution in Test := false + name := "akka-stream-alpakka-simple-codecs" ) -lazy val simpleCodecs = project - .in(file("simple-codecs")) +lazy val sqs = project + .in(file("sqs")) .enablePlugins(AutomateHeaderPlugin) .settings( - name := "akka-stream-alpakka-simple-codecs" + name := "akka-stream-alpakka-sqs", + Dependencies.Sqs ) lazy val docs = project diff --git a/docker-compose.yml b/docker-compose.yml index 9d99d3bf95..89ba60a938 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -22,3 +22,7 @@ services: - "1883:1883" volumes: - ./mqtt/src/test/travis:/mqtt/config/conf.d + dynamodb: + image: deangiberson/aws-dynamodb-local + ports: + - "8001:8000" diff --git a/docs/src/main/paradox/connectors.md b/docs/src/main/paradox/connectors.md index 39bbd61d2e..fbb4b08874 100644 --- a/docs/src/main/paradox/connectors.md +++ b/docs/src/main/paradox/connectors.md @@ -3,14 +3,14 @@ @@@ index * [AMQP Connector](amqp.md) +* [AWS DynamoDB Connector](dynamodb.md) +* [AWS SQS Connector](sqs.md) * [Cassandra Connector](cassandra.md) -* [DynamoDB Connector](dynamodb.md) * [File Connectors](file.md) -* [HBase Connectors](hbase.md) -* [MQTT Connector](mqtt.md) * [FTP Connector](ftp.md) -* [SQS Connector](sqs.md) +* [HBase Connectors](hbase.md) * [JMS Connectors](jms.md) +* [MQTT Connector](mqtt.md) * [External Connectors](external-connectors.md) @@@ diff --git a/docs/src/main/paradox/dynamodb.md b/docs/src/main/paradox/dynamodb.md index 2979305dfc..23a7158035 100644 --- a/docs/src/main/paradox/dynamodb.md +++ b/docs/src/main/paradox/dynamodb.md @@ -41,7 +41,7 @@ Scala : @@snip (../../../../dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/ExampleSpec.scala) { #init-client } Java -: @@snip (../../../../dynamodb/src/test/java/akka/stream/alpakka/dynamodb/ExampleJavaSpec.java) { #init-client } +: @@snip (../../../../dynamodb/src/test/java/akka/stream/alpakka/dynamodb/ExampleTest.java) { #init-client } You can then create the client with a settings object. @@ -49,7 +49,7 @@ Scala : @@snip (../../../../dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/ExampleSpec.scala) { #client-construct } Java -: @@snip (../../../../dynamodb/src/test/java/akka/stream/alpakka/dynamodb/ExampleJavaSpec.java) { #client-construct } +: @@snip (../../../../dynamodb/src/test/java/akka/stream/alpakka/dynamodb/ExampleTest.java) { #client-construct } We can now send requests to DynamoDB across the connection. @@ -57,4 +57,24 @@ Scala : @@snip (../../../../dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/ExampleSpec.scala) { #simple-request } Java -: @@snip (../../../../dynamodb/src/test/java/akka/stream/alpakka/dynamodb/ExampleJavaSpec.java) { #simple-request } +: @@snip (../../../../dynamodb/src/test/java/akka/stream/alpakka/dynamodb/ExampleTest.java) { #simple-request } + +### Running the example code + +The code in this guide is part of runnable tests of this project. You are welcome to edit the code and run it in sbt. + +> Test code requires DynamoDB server running in the background. You can start one quickly using docker: +> +> `docker run --rm -p 8001:8000 deangiberson/aws-dynamodb-local` + +Scala +: ``` + sbt + > dynamodb/testOnly *Spec + ``` + +Java +: ``` + sbt + > dynamodb/testOnly *Test + ``` diff --git a/dynamodb/src/main/scala/akka/stream/alpakka/dynamodb/impl/AwsClient.scala b/dynamodb/src/main/scala/akka/stream/alpakka/dynamodb/impl/AwsClient.scala index 01a38ccde1..761c82cc88 100644 --- a/dynamodb/src/main/scala/akka/stream/alpakka/dynamodb/impl/AwsClient.scala +++ b/dynamodb/src/main/scala/akka/stream/alpakka/dynamodb/impl/AwsClient.scala @@ -3,23 +3,23 @@ */ package akka.stream.alpakka.dynamodb.impl -import java.io.{ ByteArrayInputStream, InputStream } +import java.io.{ByteArrayInputStream, InputStream} import java.util.concurrent.atomic.AtomicInteger import akka.NotUsed import akka.actor.ActorSystem import akka.http.scaladsl.Http.HostConnectionPool -import akka.http.scaladsl.model.{ ContentType, HttpEntity, _ } +import akka.http.scaladsl.model.{ContentType, HttpEntity, _} import akka.stream.alpakka.dynamodb.AwsOp -import akka.stream.alpakka.dynamodb.impl.AwsClient.{ AwsConnect, AwsRequestMetadata } +import akka.stream.alpakka.dynamodb.impl.AwsClient.{AwsConnect, AwsRequestMetadata} import akka.stream.scaladsl.Flow -import akka.stream.{ ActorAttributes, ActorMaterializer, Supervision } -import com.amazonaws.auth.{ AWS4Signer, DefaultAWSCredentialsProviderChain } -import com.amazonaws.http.{ HttpMethodName, HttpResponseHandler, HttpResponse => AWSHttpResponse } -import com.amazonaws.{ DefaultRequest, HttpMethod => _, _ } +import akka.stream.{ActorAttributes, ActorMaterializer, Supervision} +import com.amazonaws.auth.{AWS4Signer, DefaultAWSCredentialsProviderChain} +import com.amazonaws.http.{HttpMethodName, HttpResponseHandler, HttpResponse => AWSHttpResponse} +import com.amazonaws.{DefaultRequest, HttpMethod => _, _} -import scala.concurrent.{ ExecutionContext, Future } -import scala.util.{ Failure, Success, Try } +import scala.concurrent.{ExecutionContext, Future} +import scala.util.{Failure, Success, Try} private[alpakka] object AwsClient { diff --git a/dynamodb/src/main/scala/akka/stream/alpakka/dynamodb/impl/DynamoClientImpl.scala b/dynamodb/src/main/scala/akka/stream/alpakka/dynamodb/impl/DynamoClientImpl.scala index fe14407d68..f493ddaa3c 100644 --- a/dynamodb/src/main/scala/akka/stream/alpakka/dynamodb/impl/DynamoClientImpl.scala +++ b/dynamodb/src/main/scala/akka/stream/alpakka/dynamodb/impl/DynamoClientImpl.scala @@ -6,11 +6,11 @@ package akka.stream.alpakka.dynamodb.impl import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.MediaType.NotCompressible -import akka.http.scaladsl.model.{ ContentType, MediaType } +import akka.http.scaladsl.model.{ContentType, MediaType} import akka.stream.ActorMaterializer import akka.stream.alpakka.dynamodb.AwsOp -import akka.stream.alpakka.dynamodb.impl.AwsClient.{ AwsConnect, AwsRequestMetadata } -import akka.stream.scaladsl.{ Sink, Source } +import akka.stream.alpakka.dynamodb.impl.AwsClient.{AwsConnect, AwsRequestMetadata} +import akka.stream.scaladsl.{Sink, Source} import com.amazonaws.AmazonServiceException import com.amazonaws.http.HttpResponseHandler diff --git a/dynamodb/src/main/scala/akka/stream/alpakka/dynamodb/javadsl/DynamoClient.scala b/dynamodb/src/main/scala/akka/stream/alpakka/dynamodb/javadsl/DynamoClient.scala index 454ec4e493..861641d4d0 100644 --- a/dynamodb/src/main/scala/akka/stream/alpakka/dynamodb/javadsl/DynamoClient.scala +++ b/dynamodb/src/main/scala/akka/stream/alpakka/dynamodb/javadsl/DynamoClient.scala @@ -6,9 +6,9 @@ package akka.stream.alpakka.dynamodb.javadsl import akka.actor.ActorSystem import akka.stream.ActorMaterializer import akka.stream.alpakka.dynamodb.AwsOp -import akka.stream.alpakka.dynamodb.impl.{ DynamoClientImpl, DynamoSettings } +import akka.stream.alpakka.dynamodb.impl.{DynamoClientImpl, DynamoSettings} import akka.stream.alpakka.dynamodb.scaladsl.DynamoImplicits -import akka.stream.scaladsl.{ Sink, Source } +import akka.stream.scaladsl.{Sink, Source} import com.amazonaws.services.dynamodbv2.model._ import scala.concurrent.Future diff --git a/dynamodb/src/main/scala/akka/stream/alpakka/dynamodb/scaladsl/DynamoClient.scala b/dynamodb/src/main/scala/akka/stream/alpakka/dynamodb/scaladsl/DynamoClient.scala index 2f4211948f..59a57abf3d 100644 --- a/dynamodb/src/main/scala/akka/stream/alpakka/dynamodb/scaladsl/DynamoClient.scala +++ b/dynamodb/src/main/scala/akka/stream/alpakka/dynamodb/scaladsl/DynamoClient.scala @@ -6,8 +6,8 @@ package akka.stream.alpakka.dynamodb.scaladsl import akka.actor.ActorSystem import akka.stream.ActorMaterializer import akka.stream.alpakka.dynamodb.AwsOp -import akka.stream.alpakka.dynamodb.impl.{ DynamoClientImpl, DynamoSettings } -import akka.stream.scaladsl.{ Sink, Source } +import akka.stream.alpakka.dynamodb.impl.{DynamoClientImpl, DynamoSettings} +import akka.stream.scaladsl.{Sink, Source} object DynamoClient { def apply(settings: DynamoSettings)(implicit system: ActorSystem, materializer: ActorMaterializer) = diff --git a/dynamodb/src/main/scala/akka/stream/alpakka/dynamodb/scaladsl/DynamoImplicits.scala b/dynamodb/src/main/scala/akka/stream/alpakka/dynamodb/scaladsl/DynamoImplicits.scala index d08b6a4746..2c2c42c79a 100644 --- a/dynamodb/src/main/scala/akka/stream/alpakka/dynamodb/scaladsl/DynamoImplicits.scala +++ b/dynamodb/src/main/scala/akka/stream/alpakka/dynamodb/scaladsl/DynamoImplicits.scala @@ -8,7 +8,7 @@ import akka.stream.alpakka.dynamodb.impl.DynamoProtocol import com.amazonaws.http.HttpResponseHandler import com.amazonaws.services.dynamodbv2.model._ import com.amazonaws.transform.Marshaller -import com.amazonaws.{ AmazonWebServiceResponse, Request } +import com.amazonaws.{AmazonWebServiceResponse, Request} object DynamoImplicits extends DynamoProtocol { diff --git a/dynamodb/src/test/java/akka/stream/alpakka/dynamodb/ExampleJavaSpec.java b/dynamodb/src/test/java/akka/stream/alpakka/dynamodb/ExampleJavaSpec.java deleted file mode 100644 index 4b2e97a6a4..0000000000 --- a/dynamodb/src/test/java/akka/stream/alpakka/dynamodb/ExampleJavaSpec.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (C) 2016 Lightbend Inc. - */ -package akka.stream.alpakka.dynamodb; - -import akka.actor.ActorSystem; -import akka.stream.ActorMaterializer; -import akka.stream.alpakka.dynamodb.impl.DynamoSettings; -import akka.stream.alpakka.dynamodb.javadsl.DynamoClient; -import akka.testkit.JavaTestKit; -import com.amazonaws.services.dynamodbv2.local.main.ServerRunner; -import com.amazonaws.services.dynamodbv2.local.server.DynamoDBProxyServer; -import com.amazonaws.services.dynamodbv2.model.DescribeLimitsRequest; -import com.amazonaws.services.dynamodbv2.model.DescribeLimitsResult; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; -import scala.concurrent.Await; -import scala.concurrent.Future; -import scala.concurrent.duration.Duration; - -public class ExampleJavaSpec { - - static ActorSystem system; - static ActorMaterializer materializer; - static DynamoSettings dynamoSettings; - static DynamoClient dynamoClient; - static DynamoDBProxyServer serverRunner; - - @BeforeClass - public static void setup() throws Exception { - - //#init-client - system = ActorSystem.create(); - materializer = ActorMaterializer.create(system); - //#init-client - - //#client-construct - dynamoSettings = DynamoSettings.apply(system); - dynamoClient = DynamoClient.create(dynamoSettings, system, materializer); - //#client-construct - - String[] serverArgs = new String[] { "-port", Integer.toString(dynamoSettings.port()), "-inMemory" }; - serverRunner = ServerRunner.createServerFromCommandLineArgs(serverArgs); - serverRunner.start(); - - } - - @AfterClass - public static void teardown() throws Exception { - JavaTestKit.shutdownActorSystem(system); - serverRunner.stop(); - } - - @Test - public void listTables() throws Exception { - //#simple-request - final Future describeLimitsResultFuture = dynamoClient.describeLimits(new DescribeLimitsRequest()); - //#simple-request - final Duration duration = Duration.create(5, "seconds"); - DescribeLimitsResult result = Await.result(describeLimitsResultFuture, duration); - } - -} diff --git a/dynamodb/src/test/java/akka/stream/alpakka/dynamodb/ExampleTest.java b/dynamodb/src/test/java/akka/stream/alpakka/dynamodb/ExampleTest.java new file mode 100644 index 0000000000..41af29b0ee --- /dev/null +++ b/dynamodb/src/test/java/akka/stream/alpakka/dynamodb/ExampleTest.java @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2016 Lightbend Inc. + */ +package akka.stream.alpakka.dynamodb; + +import akka.actor.ActorSystem; +import akka.japi.Pair; +import akka.stream.ActorMaterializer; +import akka.stream.alpakka.dynamodb.impl.DynamoSettings; +import akka.stream.alpakka.dynamodb.javadsl.DynamoClient; +import com.amazonaws.services.dynamodbv2.model.ListTablesRequest; +import com.amazonaws.services.dynamodbv2.model.ListTablesResult; +import org.junit.BeforeClass; +import org.junit.Test; +import scala.concurrent.Await; +import scala.concurrent.Future; +import scala.concurrent.duration.Duration; + +public class ExampleTest { + + static ActorSystem system; + static ActorMaterializer materializer; + static DynamoSettings settings; + static DynamoClient client; + + public static Pair setupMaterializer() { + //#init-client + final ActorSystem system = ActorSystem.create(); + final ActorMaterializer materializer = ActorMaterializer.create(system); + //#init-client + return Pair.create(system, materializer); + } + + public static Pair setupClient() { + //#client-construct + final DynamoSettings settings = DynamoSettings.apply(system); + final DynamoClient client = DynamoClient.create(settings, system, materializer); + //#client-construct + return Pair.create(settings, client); + } + + @BeforeClass + public static void setup() throws Exception { + final Pair sysmat = setupMaterializer(); + system = sysmat.first(); + materializer = sysmat.second(); + + final Pair setclient = setupClient(); + settings = setclient.first(); + client = setclient.second(); + } + + @Test + public void listTables() throws Exception { + //#simple-request + final Future listTablesResultFuture = client.listTables(new ListTablesRequest()); + //#simple-request + final Duration duration = Duration.create(5, "seconds"); + ListTablesResult result = Await.result(listTablesResultFuture, duration); + } + +} diff --git a/dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/ExampleSpec.scala b/dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/ExampleSpec.scala index c7ddf5886e..307560a379 100644 --- a/dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/ExampleSpec.scala +++ b/dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/ExampleSpec.scala @@ -7,24 +7,16 @@ import akka.actor.ActorSystem import akka.stream.ActorMaterializer import akka.stream.alpakka.dynamodb.impl.DynamoSettings import akka.stream.alpakka.dynamodb.scaladsl._ -import akka.testkit.{ SocketUtil, TestKit } +import akka.testkit.TestKit import com.amazonaws.services.dynamodbv2.model._ -import org.scalatest.{ BeforeAndAfterAll, Matchers, WordSpecLike } +import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpecLike} -import scala.concurrent.{ Await, Future } +import scala.concurrent.{Await, Future} import scala.concurrent.duration._ class ExampleSpec extends TestKit(ActorSystem("ExampleSpec")) with WordSpecLike with Matchers with BeforeAndAfterAll { val settings = DynamoSettings(system) - val localDynamo = new LocalDynamo(settings) - - override def beforeAll(): Unit = localDynamo.start() - - override def afterAll(): Unit = { - localDynamo.stop() - system.terminate() - } "DynamoDB Client" should { diff --git a/dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/ItemSpec.scala b/dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/ItemSpec.scala index e5c36264bd..2e3a8c040a 100644 --- a/dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/ItemSpec.scala +++ b/dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/ItemSpec.scala @@ -7,33 +7,26 @@ import akka.actor.ActorSystem import akka.stream._ import akka.stream.alpakka.dynamodb.impl.DynamoSettings import akka.stream.alpakka.dynamodb.scaladsl._ -import akka.testkit.{ SocketUtil, TestKit } +import akka.testkit.TestKit import org.scalatest._ +import scala.collection.JavaConverters._ + class ItemSpec extends TestKit(ActorSystem("ItemSpec")) with AsyncWordSpecLike with Matchers with BeforeAndAfterAll { implicit val materializer = ActorMaterializer() implicit val ec = system.dispatcher - val settings = DynamoSettings(system).copy(port = SocketUtil.temporaryServerAddress().getPort) - val localDynamo = new LocalDynamo(settings) - + val settings = DynamoSettings(system) val client = DynamoClient(settings) - override def beforeAll(): Unit = localDynamo.start() - - override def afterAll(): Unit = { - localDynamo.stop() - system.terminate() - } - "DynamoDB Client" should { - import ItemSpecOps._ import DynamoImplicits._ + import ItemSpecOps._ "1) list zero tables" in { - client.single(listTablesRequest).map(_.getTableNames.size shouldBe 0) + client.single(listTablesRequest).map(_.getTableNames.asScala.count(_ == tableName) shouldBe 0) } "2) create a table" in { @@ -41,7 +34,7 @@ class ItemSpec extends TestKit(ActorSystem("ItemSpec")) with AsyncWordSpecLike w } "3) find a new table" in { - client.single(listTablesRequest).map(_.getTableNames.size shouldBe 1) + client.single(listTablesRequest).map(_.getTableNames.asScala.count(_ == tableName) shouldBe 1) } "4) put an item and read it back" in { @@ -59,6 +52,13 @@ class ItemSpec extends TestKit(ActorSystem("ItemSpec")) with AsyncWordSpecLike w client.single(deleteItemRequest).flatMap(_ => client.single(getItemRequest)).map(_.getItem() shouldEqual null) } + "7) delete table" in { + client + .single(deleteTableRequest) + .flatMap(_ => client.single(listTablesRequest)) + .map(_.getTableNames.asScala.count(_ == tableName) shouldEqual 0) + } + } } diff --git a/dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/LocalDynamo.scala b/dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/LocalDynamo.scala deleted file mode 100644 index a5642470aa..0000000000 --- a/dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/LocalDynamo.scala +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright (C) 2016 Lightbend Inc. - */ -package akka.stream.alpakka.dynamodb - -import akka.stream.alpakka.dynamodb.impl.DynamoSettings -import com.amazonaws.services.dynamodbv2.local.main.ServerRunner -import com.amazonaws.services.dynamodbv2.local.server.DynamoDBProxyServer - -class LocalDynamo(settings: DynamoSettings) { - - private val serverArgs = Array("-port", settings.port.toString, "-inMemory") - private val server: DynamoDBProxyServer = ServerRunner.createServerFromCommandLineArgs(serverArgs) - - def start(): Unit = server.start() - - def stop(): Unit = server.stop() - -} diff --git a/dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/TableSpec.scala b/dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/TableSpec.scala index 2b2ad53a68..eeedacf985 100644 --- a/dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/TableSpec.scala +++ b/dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/TableSpec.scala @@ -5,26 +5,19 @@ package akka.stream.alpakka.dynamodb import akka.actor.ActorSystem import akka.stream.ActorMaterializer -import akka.stream.alpakka.dynamodb.impl.{ DynamoClientImpl, DynamoSettings } +import akka.stream.alpakka.dynamodb.impl.DynamoSettings import akka.stream.alpakka.dynamodb.scaladsl.DynamoClient -import akka.testkit.{ SocketUtil, TestKit } -import org.scalatest.{ AsyncWordSpecLike, BeforeAndAfterAll, Matchers } +import akka.testkit.TestKit +import org.scalatest.{AsyncWordSpecLike, BeforeAndAfterAll, Matchers} + +import scala.collection.JavaConverters._ class TableSpec extends TestKit(ActorSystem("TableSpec")) with AsyncWordSpecLike with Matchers with BeforeAndAfterAll { implicit val materializer = ActorMaterializer() implicit val ec = system.dispatcher - val settings = DynamoSettings(system).copy(port = SocketUtil.temporaryServerAddress().getPort) - val localDynamo = new LocalDynamo(settings) - - override def beforeAll(): Unit = localDynamo.start() - - override def afterAll(): Unit = { - localDynamo.stop() - system.terminate() - } - + val settings = DynamoSettings(system) val client = DynamoClient(settings) "DynamoDB Client" should { @@ -37,7 +30,7 @@ class TableSpec extends TestKit(ActorSystem("TableSpec")) with AsyncWordSpecLike } "2) list tables" in { - client.single(listTablesRequest).map(_.getTableNames.size shouldEqual 1) + client.single(listTablesRequest).map(_.getTableNames.asScala.count(_ == tableName) shouldEqual 1) } "3) describe table" in { @@ -56,7 +49,7 @@ class TableSpec extends TestKit(ActorSystem("TableSpec")) with AsyncWordSpecLike client .single(deleteTableRequest) .flatMap(_ => client.single(listTablesRequest)) - .map(_.getTableNames.size shouldEqual 0) + .map(_.getTableNames.asScala.count(_ == tableName) shouldEqual 0) } } diff --git a/dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/TestOps.scala b/dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/TestOps.scala index d9529bbb26..2ccb6c4b03 100644 --- a/dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/TestOps.scala +++ b/dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/TestOps.scala @@ -9,7 +9,7 @@ import scala.collection.JavaConverters._ trait TestOps { - val tableName = "test" + val tableName: String val keyCol = "kkey" val sortCol = "sort" @@ -38,12 +38,16 @@ trait TestOps { ) val describeTableRequest = new DescribeTableRequest().withTableName(tableName) + + val deleteTableRequest = new DeleteTableRequest().withTableName(tableName) } } object ItemSpecOps extends TestOps { + override val tableName = "ItemSpecOps" + val listTablesRequest = common.listTablesRequest val createTableRequest = common.createTableRequest @@ -79,10 +83,14 @@ object ItemSpecOps extends TestOps { .withKeyConditionExpression(s"$keyCol = :k") .withExpressionAttributeValues(Map(":k" -> S("A")).asJava) + val deleteTableRequest = common.deleteTableRequest + } object TableSpecOps extends TestOps { + override val tableName = "TableSpecOps" + val createTableRequest = common.createTableRequest val listTablesRequest = common.listTablesRequest @@ -96,6 +104,6 @@ object TableSpecOps extends TestOps { .withProvisionedThroughput( new ProvisionedThroughput().withWriteCapacityUnits(newMaxLimit).withReadCapacityUnits(newMaxLimit)) - val deleteTableRequest = new DeleteTableRequest().withTableName(tableName) + val deleteTableRequest = common.deleteTableRequest } diff --git a/project/Dependencies.scala b/project/Dependencies.scala index 34f1dbe237..96d5a23567 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -60,9 +60,7 @@ object Dependencies { libraryDependencies ++= Seq( "com.amazonaws" % "aws-java-sdk-core" % "1.11.73", // ApacheV2 "com.amazonaws" % "aws-java-sdk-dynamodb" % "1.11.73", // ApacheV2 - "com.typesafe.akka" %% "akka-http" % AkkaHttpVersion, - "com.amazonaws" % "DynamoDBLocal" % "1.11.0" % Test, // ApacheV2 - "com.almworks.sqlite4java" % "sqlite4java" % "0.213" % Test // ApacheV2 + "com.typesafe.akka" %% "akka-http" % AkkaHttpVersion ) ) From 3d94ec7c6faa966caa7febe70aa1288263c8baf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martynas=20Mickevi=C4=8Dius?= Date: Thu, 19 Jan 2017 18:37:20 +0200 Subject: [PATCH 2/2] Provide some credentials This prevents the default aws credentials chain throwing an exception. The dynamodb local server that is used for tests does not require any credentials anyway. --- .../test/java/akka/stream/alpakka/dynamodb/ExampleTest.java | 3 +++ .../scala/akka/stream/alpakka/dynamodb/ExampleSpec.scala | 5 +++++ .../test/scala/akka/stream/alpakka/dynamodb/ItemSpec.scala | 5 +++++ .../test/scala/akka/stream/alpakka/dynamodb/TableSpec.scala | 5 +++++ 4 files changed, 18 insertions(+) diff --git a/dynamodb/src/test/java/akka/stream/alpakka/dynamodb/ExampleTest.java b/dynamodb/src/test/java/akka/stream/alpakka/dynamodb/ExampleTest.java index 41af29b0ee..7b33ddba94 100644 --- a/dynamodb/src/test/java/akka/stream/alpakka/dynamodb/ExampleTest.java +++ b/dynamodb/src/test/java/akka/stream/alpakka/dynamodb/ExampleTest.java @@ -41,6 +41,9 @@ public static Pair setupClient() { @BeforeClass public static void setup() throws Exception { + System.setProperty("aws.accessKeyId", "someKeyId"); + System.setProperty("aws.secretKey", "someSecretKey"); + final Pair sysmat = setupMaterializer(); system = sysmat.first(); materializer = sysmat.second(); diff --git a/dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/ExampleSpec.scala b/dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/ExampleSpec.scala index 307560a379..ad31cc2aca 100644 --- a/dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/ExampleSpec.scala +++ b/dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/ExampleSpec.scala @@ -18,6 +18,11 @@ class ExampleSpec extends TestKit(ActorSystem("ExampleSpec")) with WordSpecLike val settings = DynamoSettings(system) + override def beforeAll() = { + System.setProperty("aws.accessKeyId", "someKeyId") + System.setProperty("aws.secretKey", "someSecretKey") + } + "DynamoDB Client" should { "provide a simple usage example" in { diff --git a/dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/ItemSpec.scala b/dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/ItemSpec.scala index 2e3a8c040a..e466df1c6b 100644 --- a/dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/ItemSpec.scala +++ b/dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/ItemSpec.scala @@ -20,6 +20,11 @@ class ItemSpec extends TestKit(ActorSystem("ItemSpec")) with AsyncWordSpecLike w val settings = DynamoSettings(system) val client = DynamoClient(settings) + override def beforeAll() = { + System.setProperty("aws.accessKeyId", "someKeyId") + System.setProperty("aws.secretKey", "someSecretKey") + } + "DynamoDB Client" should { import DynamoImplicits._ diff --git a/dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/TableSpec.scala b/dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/TableSpec.scala index eeedacf985..4e9ed37fb8 100644 --- a/dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/TableSpec.scala +++ b/dynamodb/src/test/scala/akka/stream/alpakka/dynamodb/TableSpec.scala @@ -20,6 +20,11 @@ class TableSpec extends TestKit(ActorSystem("TableSpec")) with AsyncWordSpecLike val settings = DynamoSettings(system) val client = DynamoClient(settings) + override def beforeAll() = { + System.setProperty("aws.accessKeyId", "someKeyId") + System.setProperty("aws.secretKey", "someSecretKey") + } + "DynamoDB Client" should { import TableSpecOps._