Skip to content

Commit

Permalink
updated tests, merged with 2.11 branch
Browse files Browse the repository at this point in the history
  • Loading branch information
fehguy committed Sep 21, 2014
1 parent deb2ba4 commit 2b9376b
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 75 deletions.
10 changes: 3 additions & 7 deletions samples/java-play2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ more about both the spec and the framework at http://swagger.wordnik.com. For m
about Wordnik's APIs, please visit http://developer.wordnik.com. There is an online version of this
server at http://petstore.swagger.wordnik.com/api/api-docs.json

## Version compatibility
=======
This version is compatible with Play 2.3.x and Swagger 1.3.10

### To build from source
Please follow instructions to build the top-level [swagger-core project](https://github.com/wordnik/swagger-core)

Expand All @@ -19,14 +15,14 @@ The swagger-play2 module lives in maven central:
```scala
val appDependencies: Seq[sbt.ModuleID] = Seq(
/* your other dependencies */
"com.wordnik" %% "swagger-play2" % "1.3.10"
"com.wordnik" %% "swagger-play2" % "1.3.10-SNAPSHOT"
)
```

You can run the sample app as such:

````
sbt run
play run
````

The application will listen on port 9000 and respond to `http://localhost:9000/api-docs`
The application will listen on port 9000 and respond to `http://localhost:9000/api-docs.json`
4 changes: 4 additions & 0 deletions samples/java-play2/conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ GET /api-docs controllers.ApiHelpController.getResources

# OPTIONS /*wholepath controllers.PetApiController.getOptions(wholepath)

GET /api-docs/admin controllers.ApiHelpController.getResource(path = "/admin")
GET /admin/health controllers.HealthController.getHealth
GET /admin/ping controllers.HealthController.ping

GET /api-docs/pet controllers.ApiHelpController.getResource(path = "/pet")
POST /pet controllers.PetApiController.addPet
PUT /pet controllers.PetApiController.updatePet
Expand Down
1 change: 1 addition & 0 deletions samples/java-play2/project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ object ApplicationBuild extends Build {
libraryDependencies ++= appDependencies,
resolvers := Seq(
"Local Maven Repository" at "file://"+Path.userHome.absolutePath+"/.m2/repository",
Resolver.url("Local Ivy Repository", url("file://"+Path.userHome.absolutePath+"/.ivy2/local"))(Resolver.ivyStylePatterns),
"sonatype-snapshots" at "https://oss.sonatype.org/content/repositories/snapshots",
"sonatype-releases" at "https://oss.sonatype.org/content/repositories/releases",
"java-net" at "http://download.java.net/maven/2",
Expand Down
116 changes: 116 additions & 0 deletions samples/java-play2/test/IntegrationSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package test

import com.wordnik.swagger.core._
import com.wordnik.swagger.model._

import org.json4s._
import org.json4s.JsonDSL._
import org.json4s.jackson.JsonMethods._
import org.json4s.jackson.Serialization.{read, write}

import org.specs2.mutable._

import play.api.test._
import play.api.test.Helpers._

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

class IntegrationSpec extends Specification {
implicit val formats = SwaggerSerializers.formats

"Application" should {
"have the proper resource metadata" in {
running(TestServer(3333)) {
val json = Source.fromURL("http://localhost:3333/api-docs").mkString
val doc = parse(json).extract[ResourceListing]
doc.swaggerVersion must_==("1.2")
}
}

"contain all apis defined in the routes without api key" in {
running(TestServer(3333)) {
val json = Source.fromURL("http://localhost:3333/api-docs").mkString
val doc = parse(json).extract[ResourceListing]
doc.apis.size must_==(3)
(doc.apis.map(_.path).toSet &
Set(
"/admin",
"/pet",
"/user")
).size must_==(3)
}
}

"contain all operations defined in the pet resource without api key" in {
running(TestServer(3333)) {
val json = Source.fromURL("http://localhost:3333/api-docs/pet").mkString
val doc = parse(json).extract[ApiListing]
(doc.models.get.keys.toSet &
Set(
"Category",
"Tag",
"Pet")
).size must_==(3)
}
}

"contain models without api key" in {
running(TestServer(3333)) {
val json = Source.fromURL("http://localhost:3333/api-docs/pet").mkString
val doc = parse(json).extract[ApiListing]
(doc.apis.map(_.path).toSet &
Set(
"/pet/findByTags",
"/pet/findByStatus",
"/pet/{id}")
).size must_==(3)
}
}

"no apis from store resource without valid api key" in {
running(TestServer(3333)) {
val json = Source.fromURL("http://localhost:3333/api-docs/store").mkString
val doc = parse(json).extract[ApiListing]
Option(doc.apis) must_==(Some(List.empty))
}
}

"no models from store resource without valid api key" in {
running(TestServer(3333)) {
val json = Source.fromURL("http://localhost:3333/api-docs/store").mkString
val doc = parse(json).extract[ApiListing]
doc.models must_==None
}
}

"contain apis from store resource valid api key" in {
running(TestServer(3333)) {
val json = Source.fromURL("http://localhost:3333/api-docs/store?api_key=special-key").mkString
val doc = parse(json).extract[ApiListing]
doc.apis.map(_.path).size must_==(2)
}
}

"contain correct models from store resource valid api key" in {
running(TestServer(3333)) {
val json = Source.fromURL("http://localhost:3333/api-docs/store?api_key=special-key").mkString
val doc = parse(json).extract[ApiListing]
doc.models.get.keys.toSet.size must_==(1)
}
}

"contain all operations defined in the pet resource with api key" in {
running(TestServer(3333)) {
val json = Source.fromURL("http://localhost:3333/api-docs/pet?api_key=special-key").mkString
val doc = parse(json).extract[ApiListing]
(doc.apis.map(_.path).toSet &
Set(
"/pet/findByTags",
"/pet/findByStatus",
"/pet/{id}")
).size must_==(3)
}
}
}
}
6 changes: 3 additions & 3 deletions samples/scala-play2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ server at http://petstore.swagger.wordnik.com/api/api-docs.json

## Version compatibility
=======
This version is compatible with Play 2.3.x and Swagger 1.3.10
This version is compatible with Play 2.2.0 and Swagger 1.3.10-SNAPSHOT

### To build Swagger from source (optional)
Please follow instructions to build the top-level [swagger-core project](https://github.com/wordnik/swagger-core)
Expand All @@ -19,14 +19,14 @@ The swagger-play2 module lives in maven central:
```scala
val appDependencies: Seq[sbt.ModuleID] = Seq(
/* your other dependencies */
"com.wordnik" %% "swagger-play2" % "1.3.10"
"com.wordnik" %% "swagger-play2" % "1.3.10-SNAPSHOT"
)
```

You can run the sample app as such:

````
sbt run
play run
````

The application will listen on port 9000 and respond to `http://localhost:9000/api-docs`
2 changes: 1 addition & 1 deletion samples/scala-play2/conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ GET /pet/:id @controllers.PetApiController.getPetById(id)

GET /api-docs/store controllers.ApiHelpController.getResource(path = "/store")
POST /store/order controllers.StoreApiController.placeOrder
GET /store/order controllers.StoreApiController.getOrders(isComplete: Boolean)
GET /store/order controllers.StoreApiController.getOrders(isComplete:Boolean)
GET /store/order/:orderId controllers.StoreApiController.getOrderById(orderId)
DELETE /store/order/:orderId controllers.StoreApiController.deleteOrder(orderId)

Expand Down
1 change: 1 addition & 0 deletions samples/scala-play2/project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ object ApplicationBuild extends Build {
libraryDependencies ++= appDependencies,
resolvers := Seq(
"Local Maven Repository" at "file://"+Path.userHome.absolutePath+"/.m2/repository",
Resolver.url("Local Ivy Repository", url("file://"+Path.userHome.absolutePath+"/.ivy2/local"))(Resolver.ivyStylePatterns),
"sonatype-snapshots" at "https://oss.sonatype.org/content/repositories/snapshots",
"sonatype-releases" at "https://oss.sonatype.org/content/repositories/releases",
"java-net" at "http://download.java.net/maven/2",
Expand Down
33 changes: 0 additions & 33 deletions samples/scala-play2/test/ApplicationSpec.scala

This file was deleted.

63 changes: 32 additions & 31 deletions samples/scala-play2/test/IntegrationSpec.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package test

import com.wordnik.swagger.core._
import com.wordnik.swagger.core.util.ScalaJsonUtil
import com.wordnik.swagger.model.ResourceListing
import com.wordnik.swagger.model.ApiDescription
import com.wordnik.swagger.model.Operation
import com.wordnik.swagger.model.ApiListing
import com.wordnik.swagger.model._

import org.json4s._
import org.json4s.JsonDSL._
import org.json4s.jackson.JsonMethods._
import org.json4s.jackson.Serialization.{read, write}

import org.specs2.mutable._

Expand All @@ -16,21 +17,21 @@ import scala.io._
import scala.collection.JavaConverters._

class IntegrationSpec extends Specification {
val mapper = ScalaJsonUtil.mapper
implicit val formats = SwaggerSerializers.formats

"Application" should {
"have the proper resource metadata" in {
running(TestServer(3333)) {
val json = Source.fromURL("http://localhost:3333/api-docs.json").mkString
val doc = mapper.readValue(json, classOf[ResourceListing])
val json = Source.fromURL("http://localhost:3333/api-docs").mkString
val doc = parse(json).extract[ResourceListing]
doc.swaggerVersion must_==("1.2")
}
}

"contain all apis defined in the routes without api key" in {
running(TestServer(3333)) {
val json = Source.fromURL("http://localhost:3333/api-docs.json").mkString
val doc = mapper.readValue(json, classOf[ResourceListing])
val json = Source.fromURL("http://localhost:3333/api-docs").mkString
val doc = parse(json).extract[ResourceListing]
doc.apis.size must_==(3)
(doc.apis.map(_.path).toSet &
Set(
Expand All @@ -43,8 +44,8 @@ class IntegrationSpec extends Specification {

"contain all operations defined in the pet resource without api key" in {
running(TestServer(3333)) {
val json = Source.fromURL("http://localhost:3333/api-docs.json/pet").mkString
val doc = mapper.readValue(json, classOf[ApiListing])
val json = Source.fromURL("http://localhost:3333/api-docs/pet").mkString
val doc = parse(json).extract[ApiListing]
(doc.models.get.keys.toSet &
Set(
"Category",
Expand All @@ -56,58 +57,58 @@ class IntegrationSpec extends Specification {

"contain models without api key" in {
running(TestServer(3333)) {
val json = Source.fromURL("http://localhost:3333/api-docs.json/pet").mkString
val doc = mapper.readValue(json, classOf[ApiListing])
val json = Source.fromURL("http://localhost:3333/api-docs/pet").mkString
val doc = parse(json).extract[ApiListing]
(doc.apis.map(_.path).toSet &
Set(
"/pet.json/findByTags",
"/pet.json/findByStatus",
"/pet.json/{id}")
"/pet/findByTags",
"/pet/findByStatus",
"/pet/{id}")
).size must_==(3)
}
}

"no apis from store resource without valid api key" in {
running(TestServer(3333)) {
val json = Source.fromURL("http://localhost:3333/api-docs.json/store").mkString
val doc = mapper.readValue(json, classOf[ApiListing])
Option(doc.apis) must_==(None)
val json = Source.fromURL("http://localhost:3333/api-docs/store").mkString
val doc = parse(json).extract[ApiListing]
Option(doc.apis) must_==(Some(List.empty))
}
}

"no models from store resource without valid api key" in {
running(TestServer(3333)) {
val json = Source.fromURL("http://localhost:3333/api-docs.json/store").mkString
val doc = mapper.readValue(json, classOf[ApiListing])
val json = Source.fromURL("http://localhost:3333/api-docs/store").mkString
val doc = parse(json).extract[ApiListing]
doc.models must_==None
}
}

"contain apis from store resource valid api key" in {
running(TestServer(3333)) {
val json = Source.fromURL("http://localhost:3333/api-docs.json/store?api_key=special-key").mkString
val doc = mapper.readValue(json, classOf[ApiListing])
val json = Source.fromURL("http://localhost:3333/api-docs/store?api_key=special-key").mkString
val doc = parse(json).extract[ApiListing]
doc.apis.map(_.path).size must_==(2)
}
}

"contain correct models from store resource valid api key" in {
running(TestServer(3333)) {
val json = Source.fromURL("http://localhost:3333/api-docs.json/store?api_key=special-key").mkString
val doc = mapper.readValue(json, classOf[ApiListing])
val json = Source.fromURL("http://localhost:3333/api-docs/store?api_key=special-key").mkString
val doc = parse(json).extract[ApiListing]
doc.models.get.keys.toSet.size must_==(1)
}
}

"contain all operations defined in the pet resource with api key" in {
running(TestServer(3333)) {
val json = Source.fromURL("http://localhost:3333/api-docs.json/pet?api_key=special-key").mkString
val doc = mapper.readValue(json, classOf[ApiListing])
val json = Source.fromURL("http://localhost:3333/api-docs/pet?api_key=special-key").mkString
val doc = parse(json).extract[ApiListing]
(doc.apis.map(_.path).toSet &
Set(
"/pet.json/findByTags",
"/pet.json/findByStatus",
"/pet.json/{id}")
"/pet/findByTags",
"/pet/findByStatus",
"/pet/{id}")
).size must_==(3)
}
}
Expand Down

0 comments on commit 2b9376b

Please sign in to comment.