This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from aaronp/cucumber
Partial fix for issue #44 — added cucumber support
- Loading branch information
Showing
19 changed files
with
482 additions
and
79 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
name := "basic-cucumber" | ||
|
||
version := "1.0.0" | ||
|
||
scalaVersion := "2.11.11" | ||
|
||
enablePlugins(DockerPlugin, DockerComposePlugin, CucumberPlugin) | ||
|
||
libraryDependencies ++= { | ||
val cucumber = List("core", "jvm", "junit").map(suffix => | ||
"info.cukes" % s"cucumber-$suffix" % "1.2.5" % "test") :+ ("info.cukes" %% "cucumber-scala" % "1.2.5" % "test") | ||
|
||
cucumber ::: List( | ||
"org.scalactic" %% "scalactic" % "3.0.4" % "test", | ||
"org.scalatest" %% "scalatest" % "3.0.4" % ("test->*"), | ||
"org.pegdown" % "pegdown" % "1.6.0" % ("test->*"), | ||
"junit" % "junit" % "4.12" % "test" | ||
) | ||
} | ||
|
||
CucumberPlugin.glue := "classpath:" | ||
CucumberPlugin.features += "classpath:" | ||
|
||
//Set the image creation Task to be the one used by sbt-docker | ||
dockerImageCreationTask := docker.value | ||
|
||
testPassUseCucumber := true | ||
|
||
imageNames in docker := Seq(ImageName( | ||
repository = name.value.toLowerCase, | ||
tag = Some(version.value)) | ||
) | ||
|
||
// create a docker file with a file /inputs/example.input | ||
dockerfile in docker := { | ||
|
||
val classpath: Classpath = (fullClasspath in Test).value | ||
sLog.value.debug(s"Classpath is ${classpath.files.mkString("\n")}\n") | ||
|
||
new Dockerfile { | ||
val dockerAppPath = "/app/" | ||
from("java") | ||
add(classpath.files, dockerAppPath) | ||
|
||
entryPoint("java", "-cp", s"$dockerAppPath:$dockerAppPath*", "example.CalculatorServer") | ||
} | ||
} |
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,6 @@ | ||
version: '3' | ||
services: | ||
basic-cucumber: | ||
image: basic-cucumber:1.0.0 | ||
ports: | ||
- 8080:8080 |
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 @@ | ||
sbt.version=0.13.16 |
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,3 @@ | ||
addSbtPlugin("se.marcuslonnberg" % "sbt-docker" % "1.4.1") | ||
addSbtPlugin("com.tapad" % "sbt-docker-compose" % "1.0.29-SNAPSHOT") | ||
addSbtPlugin("com.waioeka.sbt" % "cucumber-plugin" % "0.1.2") |
40 changes: 40 additions & 0 deletions
40
examples/basic-with-tests-cucumber/src/main/scala/example/Calculator.scala
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,40 @@ | ||
package example | ||
|
||
import java.nio.file.{Files, Path, Paths} | ||
|
||
import scala.collection.JavaConverters._ | ||
|
||
object Calculator { | ||
|
||
def main(args: Array[String]): Unit = { | ||
args.toList match { | ||
case Nil => println(s"Usage: Expected either two ints or a file path") | ||
case List(filePath) => | ||
println(apply(Paths.get(filePath))) | ||
case List(a, b) => | ||
println(add(a.toInt, b.toInt)) | ||
case err => println(s"Usage: Expected either two ints or a file path, but got $err") | ||
} | ||
} | ||
|
||
def add(x: Int, y: Int): Int = x + y | ||
|
||
def subtract(x: Int, y: Int): Int = x - y | ||
|
||
val PlusR = """(\d+)\s*\+\s*(\d+)""".r | ||
val MinusR = """(\d+)\s*-\s*(\d+)""".r | ||
|
||
/** | ||
* Evaluates the first line of the input file to be an addition or subtration operation. | ||
* | ||
* This is just added to demonstrate e.g. composing two containers w/ shared volumes | ||
*/ | ||
def apply(input: Path): Int = { | ||
Files.readAllLines(input).asScala.headOption match { | ||
case Some(PlusR(a, b)) => add(a.toInt, b.toInt) | ||
case Some(MinusR(a, b)) => subtract(a.toInt, b.toInt) | ||
case _ => sys.error("Whacha talkin' bout, willis?") | ||
} | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
examples/basic-with-tests-cucumber/src/main/scala/example/CalculatorClient.scala
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,24 @@ | ||
package example | ||
|
||
import scala.sys.process._ | ||
|
||
case class CalculatorClient(hostPort: String) { | ||
|
||
def add(x: Int, y: Int) = curl("add", x, y) | ||
|
||
def subtract(x: Int, y: Int) = curl("subtract", x, y) | ||
|
||
private def curl(path: String, x: Int, y: Int) = { | ||
val url = s"${hostPort}/$path/$x/$y" | ||
println(s"curling $url") | ||
val output = s"curl $url".!! | ||
try { | ||
output.trim.toInt | ||
} catch{ | ||
case nfe : NumberFormatException => | ||
println(output) | ||
throw new Exception(s"$url responded with: >${output}< : $nfe", nfe) | ||
} | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
examples/basic-with-tests-cucumber/src/main/scala/example/CalculatorServer.scala
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,50 @@ | ||
package example | ||
|
||
import java.net.InetSocketAddress | ||
|
||
import com.sun.net.httpserver.{HttpExchange, HttpHandler, HttpServer} | ||
|
||
object CalculatorServer extends App { | ||
def start(port: Int) = { | ||
val server = HttpServer.create(new InetSocketAddress(port), 0) | ||
server.createContext("/", Handler) | ||
server.start() | ||
server | ||
} | ||
|
||
|
||
object Handler extends HttpHandler { | ||
|
||
val AddInput = """/add/([-0-9]+)/([-0-9]+)/?""".r | ||
val SubtractInput = """/subtract/([-0-9]+)/([-0-9]+)/?""".r | ||
|
||
override def handle(ex: HttpExchange): Unit = { | ||
val path = ex.getRequestURI.toString | ||
|
||
val resultOpt = path match { | ||
case AddInput(a, b) => Option(Calculator.add(a.toInt, b.toInt)) | ||
case SubtractInput(a, b) => | ||
Option(Calculator.subtract(a.toInt, b.toInt)) | ||
case _ => None | ||
} | ||
|
||
val replyString = resultOpt match { | ||
case Some(x) => | ||
val response = x.toString | ||
ex.sendResponseHeaders(200, response.length) | ||
response | ||
case None => | ||
val response = s"Unknown path: $path" | ||
ex.sendResponseHeaders(404, response.length) | ||
response | ||
} | ||
|
||
ex.getResponseBody.write(replyString.getBytes) | ||
} | ||
} | ||
|
||
|
||
val port = args.headOption.map(_.toInt).getOrElse(8080) | ||
println(s"Starting calculator server on port $port w/ user args ${args.mkString(": [", ",", "]")}") | ||
start(port) | ||
} |
9 changes: 9 additions & 0 deletions
9
examples/basic-with-tests-cucumber/src/main/scala/example/ClientApp.scala
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,9 @@ | ||
package example | ||
|
||
object ClientApp extends App { | ||
val client = CalculatorClient("http://localhost:8080") | ||
|
||
println(client.add(5, 6)) | ||
println(client.subtract(5, 6)) | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
examples/basic-with-tests-cucumber/src/test/resources/calculator-service.feature
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,26 @@ | ||
Feature: Remote Server/Client contract | ||
|
||
Background: | ||
And a calculator client against http://localhost:8080 | ||
|
||
Scenario Outline: Addition | ||
Given a remote request to add <lhs> and <rhs> | ||
Then The response should be <sum> | ||
Examples: | ||
| lhs | rhs | sum | | ||
| -1 | 1 | 0 | | ||
| 0 | 0 | 0 | | ||
| 1 | 0 | 1 | | ||
| 0 | 1 | 1 | | ||
| 1 | 2 | 3 | | ||
|
||
Scenario Outline: Subtraction | ||
Given a remote request to subtract <rhs> from <lhs> | ||
Then The response should be <result> | ||
Examples: | ||
| lhs | rhs | result | | ||
| -1 | 1 | -2 | | ||
| 0 | 0 | 0 | | ||
| 1 | 0 | 1 | | ||
| 0 | 1 | -1 | | ||
| 1 | 2 | -1 | |
23 changes: 23 additions & 0 deletions
23
examples/basic-with-tests-cucumber/src/test/resources/calculator.feature
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,23 @@ | ||
Feature: Calculator Operations | ||
|
||
Scenario Outline: Addition | ||
Given <lhs> + <rhs> | ||
Then The result should be <sum> | ||
Examples: | ||
| lhs | rhs | sum | | ||
| -1 | 1 | 0 | | ||
| 0 | 0 | 0 | | ||
| 1 | 0 | 1 | | ||
| 0 | 1 | 1 | | ||
| 1 | 2 | 3 | | ||
|
||
Scenario Outline: Subtraction | ||
Given <lhs> - <rhs> | ||
Then The result should be <result> | ||
Examples: | ||
| lhs | rhs | result | | ||
| -1 | 1 | -2 | | ||
| 0 | 0 | 0 | | ||
| 1 | 0 | 1 | | ||
| 0 | 1 | -1 | | ||
| 1 | 2 | -1 | |
20 changes: 20 additions & 0 deletions
20
examples/basic-with-tests-cucumber/src/test/scala/CalculatorSteps.scala
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,20 @@ | ||
package example | ||
|
||
import cucumber.api.scala.{EN, ScalaDsl} | ||
import org.scalatest.Matchers | ||
|
||
|
||
class CalculatorSteps extends ScalaDsl with EN with Matchers { | ||
|
||
var lastResult = Int.MinValue | ||
|
||
Given("""^(.+) \+ (.+)$""") { (lhs: Int, rhs: Int) => | ||
lastResult = Calculator.add(lhs, rhs) | ||
} | ||
Given("""^(.+) - (.+)$""") { (lhs: Int, rhs: Int) => | ||
lastResult = Calculator.subtract(lhs, rhs) | ||
} | ||
Then("""^The result should be ([-0-9]+)$""") { (expected: Int) => | ||
lastResult shouldBe expected | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
examples/basic-with-tests-cucumber/src/test/scala/CucumberTest.scala
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,14 @@ | ||
package example | ||
|
||
import cucumber.api.CucumberOptions | ||
import cucumber.api.junit.Cucumber | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(classOf[Cucumber]) | ||
@CucumberOptions( | ||
features = Array("classpath:"), | ||
glue = Array("classpath:"), | ||
plugin = Array("pretty", "html:target/cucumber/report.html"), | ||
strict = true | ||
) | ||
class CucumberTest |
37 changes: 37 additions & 0 deletions
37
examples/basic-with-tests-cucumber/src/test/scala/ServiceSteps.scala
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,37 @@ | ||
package example | ||
|
||
import cucumber.api.scala.{EN, ScalaDsl} | ||
import org.scalatest.Matchers | ||
import org.scalatest.concurrent.{Eventually, ScalaFutures} | ||
|
||
object ServiceSteps { | ||
lazy val defaultStartedService = { | ||
CalculatorServer.start(8080) | ||
} | ||
} | ||
|
||
class ServiceSteps extends ScalaDsl with EN with Matchers with ScalaFutures with Eventually { | ||
|
||
var lastResult = Int.MinValue | ||
var client: CalculatorClient = null | ||
|
||
/** | ||
* This assumes a running service mapped against the host machine at the given location | ||
*/ | ||
Given("""^a calculator client against (.+)$""") { hostPort: String => | ||
client = CalculatorClient(hostPort) | ||
|
||
// prove connectivity eagerly within this step | ||
client.add(0, 0) shouldBe 0 | ||
} | ||
|
||
Given("""^a remote request to add (.+) and (.+)$""") { (lhs: Int, rhs: Int) => | ||
lastResult = client.add(lhs, rhs) | ||
} | ||
Given("""^a remote request to subtract (.+) from (.+)$""") { (rhs: Int, lhs: Int) => | ||
lastResult = client.subtract(lhs, rhs) | ||
} | ||
Then("""^The response should be ([-0-9]+)$""") { (expected: Int) => | ||
lastResult shouldBe expected | ||
} | ||
} |
Oops, something went wrong.