Skip to content

Commit

Permalink
add integration tests for API.md
Browse files Browse the repository at this point in the history
  • Loading branch information
tylersouthwick committed Oct 2, 2017
1 parent 4500d96 commit b9dc982
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package com.nike.redwiggler.blueprint

import com.nike.redwiggler.core.models.{LiteralPathComponent, Path, PathComponent}
import com.nike.redwiggler.core.models.{LiteralPathComponent, Path, PathComponent, PlaceHolderPathComponent}

object BlueprintPathParser {

def apply(path : String) : Path = {
val seq = Path(path).components.reverse
Path(seq.tail.reverse) / cleanupLast(seq.headOption)
if (path.contains("{?")) {
apply(path.substring(0, path.indexOf("{?")))
} else {
Path(Path(path).components.map(parseComponent))
}
}

def cleanupLast(component : Option[PathComponent]) : Path = component match {
case Some(LiteralPathComponent(path)) if path.contains("{?") => Path(Seq(LiteralPathComponent(path.substring(0, path.indexOf("{?")))))
case Some(LiteralPathComponent(path)) => Path(Seq(LiteralPathComponent(path)))
case None => Path()
case Some(c) => Path(Seq(c))
private def parseComponent(component : PathComponent) : PathComponent = component match {
case LiteralPathComponent(c) if c.startsWith(":") => PlaceHolderPathComponent(c.substring(1))
case _ => component
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.nike.redwiggler.blueprint

import java.io.File
import java.util

import com.nike.redwiggler.blueprint.parser.BlueprintParser
import com.nike.redwiggler.core.EndpointSpecificationProvider
import com.nike.redwiggler.core.models._

import collection.JavaConverters._
import scala.io.Source

case class BlueprintSpecificationProvider(blueprint : String, blueprintParser: BlueprintParser) extends EndpointSpecificationProvider {

Expand Down Expand Up @@ -38,3 +40,9 @@ case class BlueprintSpecificationProvider(blueprint : String, blueprintParser: B
Some(JsonSchema(SchemaLoader.load(rawSchema)))
}
}

object BlueprintSpecificationProvider {
def apply(apiMd: File, blueprintParser: BlueprintParser) : BlueprintSpecificationProvider = {
BlueprintSpecificationProvider(Source.fromFile(apiMd).mkString, blueprintParser)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,28 @@ class BlueprintPathParserSpec extends FunSpec with Matchers {
path.asString should equal ("/my/api/v1")
}

it("should parse literal base path") {
val path = BlueprintPathParser("/")
path.asString should equal ("/")
}

it("should parse path with query parameters") {
val path = BlueprintPathParser("/my/api/v1{?foo,bar}")
path.asString should equal ("/my/api/v1")
}

it("should parse path with path parameters") {
val path = BlueprintPathParser("/my/api/:id")
path.asString should equal ("/my/api/{id}")
}

it("should parse path with path parameters in middle") {
val path = BlueprintPathParser("/my/api/:id/v1")
path.asString should equal ("/my/api/{id}/v1")
}

it("should parse path with path and query parameters") {
val path = BlueprintPathParser("/my/api/:id{?foo,bar}")
path.asString should equal ("/my/api/{id}")
}
}
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ lazy val root = (project in file("."))
.aggregate(core, swagger, restassured, html, blueprint)
.enablePlugins(ReadmeTests)
.settings(ReadmeTests.projectSettings)
.dependsOn(core, swagger, html)
.dependsOn(core, swagger, html, blueprint)
.settings(
testOptions in Test += Tests.Argument(TestFrameworks.ScalaTest, "-h", new File(target.value, "/test-reports-html").getAbsolutePath)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
FORMAT: 1A

# My Api Api

## Overview
**MyAPI** is a sample.


### GetItem [GET /my/resource/v2/:id]

+ Response 200 (application/json; charset=UTF-8)

+ Schema

{
"$schema":"http://json-schema.org/draft-04/schema#",
"type":"object",
"properties":{
"name":{
"type":"string",
},
"id":{
"type":"string",
"pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$"
},
}
}

### CreateItem [POST /my/resource/v2]

+ Request

+ Schema

{
"$schema":"http://json-schema.org/draft-04/schema#",
"type":"object",
"properties":{
"name":{
"type":"string",
},
"id":{
"type":"string",
},
}
}

+ Response 201 (application/json; charset=UTF-8)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[
{
"verb": "POST",
"path": "/my/resource/v2",
"passed": 0,
"total": 1
},
{
"verb": "GET",
"path": "/foobar",
"passed": 0,
"total": 1
},
{
"verb": "GET",
"path": "/my/resource/v2/{id}",
"passed": 1,
"total": 2
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"verb": "GET",
"path": "/my/resource/v2/25",
"responseHeaders": [],
"responseBody": "{\"id\": \"25\", \"name\": \"foo\"}",
"code": 200
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"verb": "GET",
"path": "/foobar",
"responseHeaders": [],
"code": 200
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"verb": "GET",
"path": "/my/resource/v2/53b78f35-2fbb-4207-9777-1b9aa1a05327",
"responseHeaders": [],
"responseBody": "{\"id\": \"53b78f35-2fbb-4207-9777-1b9aa1a05327\", \"name\": \"foo\"}",
"code": 200
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.nike.redwiggler.integrationtests
import java.io.File

import com.nike.redwiggler.blueprint.BlueprintSpecificationProvider
import com.nike.redwiggler.blueprint.parser.ProtagonistBlueprintParser
import com.nike.redwiggler.core.EndpointSpecificationProvider

class BlueprintSpec extends End2EndSpecBase("blueprint") {
override def specificationProvider(testDir: File): EndpointSpecificationProvider =
BlueprintSpecificationProvider(new File(testDir, "API.md"), ProtagonistBlueprintParser())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.nike.redwiggler.integrationtests


import java.util

import com.nike.redwiggler.core._
import com.nike.redwiggler.core.models.RedwigglerReport
import com.nike.redwiggler.html.HtmlReportProcessor
import org.scalatest.{FunSpec, Matchers}
import spray.json.{DefaultJsonProtocol, JsonParser}

import scala.io.Source

abstract class End2EndSpecBase(name : String) extends FunSpec with Matchers with DefaultJsonProtocol {

import java.io._

private val testDirs = new File(getClass.getResource(name).getFile).listFiles()

def specificationProvider(testDir: File): EndpointSpecificationProvider

for {
testDir <- testDirs
} {
it(s"should process ${testDir.getName}") {
Redwiggler(
callProvider = new GlobEndpointCallProvider(testDir, "requests.*.json"),
specificationProvider = specificationProvider(testDir),
reportProcessor = new ReportProcessor {
val file = File.createTempFile("redwiggler", ".json")
val writer = JsonReportProcessor(file)

override def process(reports: util.List[RedwigglerReport]): Unit = {
writer.process(reports)

val expected = JsonParser(Source.fromFile(new File(testDir, "expected.json")).mkString).convertTo[Seq[RedwigglerReportDetails]]
val actual = JsonParser(Source.fromFile(file).mkString).convertTo[Seq[RedwigglerReportDetails]]

expected should contain theSameElementsAs actual
}
}.andThen(HtmlReportProcessor(new File("/tmp/" + testDir.getName + ".html")))
)
}
}

}
Original file line number Diff line number Diff line change
@@ -1,42 +1,12 @@
package com.nike.redwiggler.integrationtests

import java.util
import java.io.File

import collection.JavaConverters._
import com.nike.redwiggler.core.models.RedwigglerReport
import com.nike.redwiggler.core._
import com.nike.redwiggler.html.HtmlReportProcessor
import com.nike.redwiggler.swagger.SwaggerEndpointSpecificationProvider
import org.scalatest.{FunSpec, Matchers}
import spray.json.{DefaultJsonProtocol, JsonParser}

import scala.io.Source
class SwaggerSpec extends End2EndSpecBase("swagger") {

class SwaggerSpec extends FunSpec with Matchers with DefaultJsonProtocol {

import java.io._
private val testDirs = new File(getClass.getResource("swagger").getFile).listFiles()

for {
testDir <- testDirs
} {
it(s"should process ${testDir.getName}") {
Redwiggler(
callProvider = new GlobEndpointCallProvider(testDir, "requests.*.json"),
specificationProvider = SwaggerEndpointSpecificationProvider(new File(testDir, "swagger.yaml")),
reportProcessor = new ReportProcessor {
val file = File.createTempFile("redwiggler", ".json")
val writer = JsonReportProcessor(file)
override def process(reports: util.List[RedwigglerReport]): Unit = {
writer.process(reports)

val expected = JsonParser(Source.fromFile(new File(testDir, "expected.json")).mkString).convertTo[Seq[RedwigglerReportDetails]]
val actual = JsonParser(Source.fromFile(file).mkString).convertTo[Seq[RedwigglerReportDetails]]

expected should contain theSameElementsAs actual
}
}.andThen(HtmlReportProcessor(new File("/tmp/" + testDir.getName + ".html" )))
)
}
}
override def specificationProvider(testDir: File): EndpointSpecificationProvider =
SwaggerEndpointSpecificationProvider(new File(testDir, "swagger.yaml"))
}

0 comments on commit b9dc982

Please sign in to comment.