diff --git a/samples/java-play2/README.md b/samples/java-play2/README.md index 2072e2cb64..87d8d2fe5a 100644 --- a/samples/java-play2/README.md +++ b/samples/java-play2/README.md @@ -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) @@ -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` diff --git a/samples/java-play2/conf/routes b/samples/java-play2/conf/routes index 9879b015cb..ff06a5d464 100644 --- a/samples/java-play2/conf/routes +++ b/samples/java-play2/conf/routes @@ -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 diff --git a/samples/java-play2/project/Build.scala b/samples/java-play2/project/Build.scala index 52a26cd092..8a9fc544c2 100644 --- a/samples/java-play2/project/Build.scala +++ b/samples/java-play2/project/Build.scala @@ -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", diff --git a/samples/java-play2/test/IntegrationSpec.scala b/samples/java-play2/test/IntegrationSpec.scala new file mode 100644 index 0000000000..49acee8dde --- /dev/null +++ b/samples/java-play2/test/IntegrationSpec.scala @@ -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) + } + } + } +} \ No newline at end of file diff --git a/samples/scala-play2/README.md b/samples/scala-play2/README.md index a032360a81..e4ce2df764 100644 --- a/samples/scala-play2/README.md +++ b/samples/scala-play2/README.md @@ -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) @@ -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` diff --git a/samples/scala-play2/conf/routes b/samples/scala-play2/conf/routes index 413fa544f1..b42f7640e1 100644 --- a/samples/scala-play2/conf/routes +++ b/samples/scala-play2/conf/routes @@ -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) diff --git a/samples/scala-play2/project/Build.scala b/samples/scala-play2/project/Build.scala index 45f4374b50..44df5b1a61 100644 --- a/samples/scala-play2/project/Build.scala +++ b/samples/scala-play2/project/Build.scala @@ -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", diff --git a/samples/scala-play2/test/ApplicationSpec.scala b/samples/scala-play2/test/ApplicationSpec.scala deleted file mode 100644 index 40011c13cf..0000000000 --- a/samples/scala-play2/test/ApplicationSpec.scala +++ /dev/null @@ -1,33 +0,0 @@ -package test - -import org.specs2.mutable._ - -import play.api.test._ -import play.api.test.Helpers._ - -/** - * Add your spec here. - * You can mock out a whole application including requests, plugins etc. - * For more information, consult the wiki. - */ -class ApplicationSpec extends Specification { - /* - "Application" should { - - "send 404 on a bad request" in { - running(FakeApplication()) { - route(FakeRequest(GET, "/boum")) must beNone - } - } - - "render the index page" in { - running(FakeApplication()) { - val home = route(FakeRequest(GET, "/")).get - - status(home) must equalTo(OK) - contentType(home) must beSome.which(_ == "text/html") - contentAsString(home) must contain ("Your new application is ready.") - } - } - }*/ -} \ No newline at end of file diff --git a/samples/scala-play2/test/IntegrationSpec.scala b/samples/scala-play2/test/IntegrationSpec.scala index 3c8740a06f..d619a3b19c 100644 --- a/samples/scala-play2/test/IntegrationSpec.scala +++ b/samples/scala-play2/test/IntegrationSpec.scala @@ -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._ @@ -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( @@ -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", @@ -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) } }