added tests

This commit is contained in:
Tony Tam 2012-09-20 14:19:58 -07:00
parent 3d379c82d1
commit 5a9979633c
4 changed files with 334 additions and 0 deletions

View File

@ -0,0 +1,18 @@
import com.wordnik.swagger.core.util.JsonUtil
import com.wordnik.swagger.codegen.util._
import com.wordnik.swagger.core.Documentation
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.FlatSpec
import org.scalatest.matchers.ShouldMatchers
import scala.collection.JavaConverters._
import scala.reflect.BeanProperty
@RunWith(classOf[JUnitRunner])
class BasicGeneratorTest extends FlatSpec with ShouldMatchers {
val json = ScalaJsonUtil.getJsonMapper
behavior of "BasicGenerator"
}

View File

@ -0,0 +1,125 @@
import com.wordnik.swagger.core.util.JsonUtil
import com.wordnik.swagger.core.Documentation
import com.wordnik.swagger.codegen.util._
import com.wordnik.swagger.codegen.language._
import com.wordnik.swagger.codegen.PathUtil
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.FlatSpec
import org.scalatest.matchers.ShouldMatchers
import scala.collection.JavaConverters._
import scala.reflect.BeanProperty
@RunWith(classOf[JUnitRunner])
class CodegenConfigTest extends FlatSpec with ShouldMatchers {
val json = ScalaJsonUtil.getJsonMapper
class SampleCodegenConfig extends CodegenConfig with PathUtil {
override def packageName = "com.test"
override def templateDir = "src/test/resources/sampleConfigTemplates"
override def destinationDir = {
val tmpFile = java.io.File.createTempFile("test",".tmp")
tmpFile.delete; tmpFile.mkdir
tmpFile.deleteOnExit
tmpFile.toString
}
override def escapeReservedWord(word: String) = "`" + word + "`"
override def typeMapping = Map("int" -> "integer")
override def invokerPackage = Some("com.wordnik.something")
override def apiPackage = Some("com.wordnik.api")
override def modelPackage = Some("com.wordnik.models")
override def reservedWords = Set("special")
override def importMapping = super.importMapping ++ Map("User" -> "com.mypackage.User")
}
val config = new SampleCodegenConfig
behavior of "PathUtil"
/*
* We will take an api in the spec and create an API name from it
*/
it should "convert an api name" in {
config.toApiName("fun") should be ("FunApi")
}
/*
* We need to generate an API name from the resource path,
* i.e. /foo will follow rules to become FooApi
*/
it should "convert a path" in {
config.apiNameFromPath("/foo/bar/cats/dogs") should be ("FooApi")
}
behavior of "CodegenConfig"
/*
* A response of type "void" will turn into a declaration of None
* for the template generator
*/
it should "process a response declaration" in {
config.processResponseDeclaration("void") should be (None)
}
/*
* if a response declaration is valid as-is, it will be
* unchanged
*/
it should "process an unchanged response" in {
config.processResponseDeclaration("string") should be (Some("string"))
}
/*
* if a typeMapping is configured, the response type declaration
* from a method should be translated
*/
it should "process an mapped response type" in {
config.processResponseDeclaration("int") should be (Some("integer"))
}
/*
* returns the invoker package from the config
*/
it should "get the invoker package" in {
config.invokerPackage should be (Some("com.wordnik.something"))
}
/*
* returns the api package
*/
it should "get the api package" in {
config.apiPackage should be (Some("com.wordnik.api"))
}
/*
* returns the model package
*/
it should "get the model package" in {
config.modelPackage should be (Some("com.wordnik.models"))
}
/*
* types are mapped between swagger types and language-specific
* types
*/
it should "convert to a declared type" in {
config.toDeclaredType("int") should be ("integer")
}
/*
* codegen should honor special imports to avoid generating
* classes
*/
it should "honor the import mapping" in {
config.importMapping("User") should be ("com.mypackage.User")
}
/*
* reserved words should be treated appropriately by the config,
* either by quoting or manipulating them with a prefix/suffix
*/
it should "quote a reserved var name" in {
config.toVarName("special") should be ("`special`")
}
}

View File

@ -0,0 +1,105 @@
import com.wordnik.swagger.core.util.JsonUtil
import com.wordnik.swagger.core.Documentation
import com.wordnik.swagger.codegen.BasicScalaGenerator
import com.wordnik.swagger.codegen.util._
import com.wordnik.swagger.codegen.language._
import com.wordnik.swagger.codegen.PathUtil
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.FlatSpec
import org.scalatest.matchers.ShouldMatchers
import scala.collection.JavaConverters._
import scala.reflect.BeanProperty
@RunWith(classOf[JUnitRunner])
class BasicScalaGeneratorTest extends FlatSpec with ShouldMatchers {
val json = ScalaJsonUtil.getJsonMapper
val config = new BasicScalaGenerator
behavior of "PathUtil"
/*
* We will take an api in the spec and create an API name from it
*/
it should "convert an api name" in {
config.toApiName("fun") should be ("FunApi")
}
/*
* We need to generate an API name from the resource path,
* i.e. /foo will follow rules to become FooApi
*/
it should "convert a path" in {
config.apiNameFromPath("/foo/bar/cats/dogs") should be ("FooApi")
}
behavior of "BasicScalaGenerator"
/*
* A response of type "void" will turn into a declaration of None
* for the template generator
*/
it should "process a response declaration" in {
config.processResponseDeclaration("void") should be (None)
}
/*
* swagger strings are turned into scala Strings
*/
it should "process a string response" in {
config.processResponseDeclaration("string") should be (Some("String"))
}
/*
* swagger int is turned into scala Int
*/
it should "process an unmapped response type" in {
config.processResponseDeclaration("int") should be (Some("Int"))
}
/*
* returns the invoker package from the config
*/
it should "get the invoker package" in {
config.invokerPackage should be (Some("com.wordnik.client.common"))
}
/*
* returns the api package
*/
it should "get the api package" in {
config.apiPackage should be (Some("com.wordnik.client.api"))
}
/*
* returns the model package
*/
it should "get the model package" in {
config.modelPackage should be (Some("com.wordnik.client.model"))
}
/*
* types are mapped between swagger types and language-specific
* types
*/
it should "convert to a declared type" in {
config.toDeclaredType("int") should be ("Int")
}
/*
* codegen should honor special imports to avoid generating
* classes
*/
it should "honor the import mapping" in {
config.importMapping("Date") should be ("java.util.Date")
}
/*
* single tick reserved words
*/
it should "quote a reserved var name" in {
config.toVarName("package") should be ("`package`")
}
}

View File

@ -0,0 +1,86 @@
import com.wordnik.swagger.core.util.JsonUtil
import com.wordnik.swagger.codegen.util._
import com.wordnik.swagger.core.Documentation
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.FlatSpec
import org.scalatest.matchers.ShouldMatchers
import scala.collection.JavaConverters._
import scala.reflect.BeanProperty
@RunWith(classOf[JUnitRunner])
class ResourceExtractorTest extends FlatSpec with ShouldMatchers {
val json = ScalaJsonUtil.getJsonMapper
behavior of "ResourceExtractor"
it should "get 3 apis from a resource listing" in {
// todo: change to return documentation object
val jsonString = ResourceExtractor.extractListing("src/test/resources/petstore/resources.json")
val resourceListing = json.readValue(jsonString, classOf[Documentation])
resourceListing should not be(null)
resourceListing.getApis.size should be (3)
}
}
@RunWith(classOf[JUnitRunner])
class ApiExtractorTest extends FlatSpec with ShouldMatchers {
val json = ScalaJsonUtil.getJsonMapper
behavior of "ApiExtractor"
it should "verify the deserialization of the store api" in {
val resourceListing = json.readValue(ResourceExtractor.extractListing("src/test/resources/petstore/resources.json", None), classOf[Documentation])
val docs = ApiExtractor.extractApiDocs("src/test/resources/petstore", resourceListing.getApis.asScala.toList)
val m = docs.map(t => (t.resourcePath, t)).toMap
val storeApi = m("/store")
storeApi should not be (null)
storeApi.getApis.size should be (2)
val f = storeApi.getApis.asScala.map(m => (m.path, m)).toMap
(f.keys.toSet & Set("/store.{format}/order/{orderId}","/store.{format}/order")).size should be (2)
val storeOps = f("/store.{format}/order/{orderId}")
val ops = storeOps.getOperations.asScala.map(o => (o.nickname, o)).toMap
val getOrderById = ops("getOrderById")
getOrderById should not be null
getOrderById.httpMethod should be ("GET")
getOrderById.getParameters.size should be (1)
getOrderById.getErrorResponses.size should be (2)
}
}
@RunWith(classOf[JUnitRunner])
class CoreUtilsTest extends FlatSpec with ShouldMatchers {
val json = ScalaJsonUtil.getJsonMapper
sys.props += "fileMap" -> "src/test/resources/petstore"
behavior of "CoreUtils"
it should "verify models are extracted" in {
val jsonString = ResourceExtractor.extractListing("src/test/resources/petstore/resources.json")
val resourceListing = json.readValue(jsonString, classOf[Documentation])
val apis = ApiExtractor.extractApiDocs("src/test/resources/petstore", resourceListing.getApis.asScala.toList)
val cu = CoreUtils.extractAllModels(apis)
cu.size should be (5)
(cu.keys.toSet & Set("User", "Tag", "Pet", "Category", "Order")).size should be (5)
}
it should "verify operation names" in {
val jsonString = ResourceExtractor.extractListing("src/test/resources/petstore/pet.json")
val petApi = json.readValue(jsonString, classOf[Documentation])
val eps = petApi.getApis.asScala.map(api => (api.path, api)).toMap
val ops = eps("/pet.{format}").getOperations.asScala.map(ep => (ep.nickname, ep)).toMap
ops.size should be (2)
(ops.keys.toSet & Set("addPet", "updatePet")).size should be (2)
}
}