split up for testability

This commit is contained in:
Tony Tam
2012-09-20 22:13:09 -07:00
parent 2579ed311a
commit cdc1bbc133
4 changed files with 58 additions and 29 deletions

View File

@@ -84,16 +84,43 @@ abstract class BasicGenerator extends CodegenConfig with PathUtil {
if (apis == null)
throw new Exception("No APIs specified by resource")
val subDocs = ApiExtractor.extractApiDocs(basePath, apis.toList, apiKey)
val models = CoreUtils.extractAllModels(subDocs)
// val models = CoreUtils.extractAllModels(subDocs)
new SwaggerSpecValidator(doc, subDocs).validate()
val allModels = new HashMap[String, DocumentationSchema]
val operations = extractOperations(subDocs, allModels)
val apiMap = groupApisToFiles(operations)
processApiMap(apiMap)
processModelMap(allModels)
codegen.writeSupportingClasses(apiMap.toMap, allModels.toMap)
}
def processModelMap(models: HashMap[String, DocumentationSchema]) = {
val modelBundleList = new ListBuffer[Map[String, AnyRef]]
for ((name, schema) <- models) {
if (!defaultIncludes.contains(name)) {
val m = new HashMap[String, AnyRef]
m += "name" -> name
m += "className" -> name
m += "apis" -> None
m += "models" -> List((name, schema))
m += "package" -> modelPackage
m += "invokerPackage" -> invokerPackage
m += "outputDirectory" -> (destinationDir + File.separator + modelPackage.getOrElse("").replaceAll("\\.", File.separator))
m += "newline" -> "\n"
modelBundleList += m.toMap
for ((file, suffix) <- modelTemplateFiles) {
m += "filename" -> (name + suffix)
generateAndWrite(m.toMap, file)
}
}
}
}
def processApiMap(apiMap: Map[(String, String), List[(String, DocumentationOperation)]] ) = {
for ((identifier, operationList) <- apiMap) {
val basePath = identifier._1
val name = identifier._2
@@ -115,27 +142,6 @@ abstract class BasicGenerator extends CodegenConfig with PathUtil {
generateAndWrite(m.toMap, file)
}
}
val modelBundleList = new ListBuffer[Map[String, AnyRef]]
for ((name, schema) <- allModels) {
if (!defaultIncludes.contains(name)) {
val m = new HashMap[String, AnyRef]
m += "name" -> name
m += "className" -> name
m += "apis" -> None
m += "models" -> List((name, schema))
m += "package" -> modelPackage
m += "invokerPackage" -> invokerPackage
m += "outputDirectory" -> (destinationDir + File.separator + modelPackage.getOrElse("").replaceAll("\\.", File.separator))
m += "newline" -> "\n"
modelBundleList += m.toMap
for ((file, suffix) <- modelTemplateFiles) {
m += "filename" -> (name + suffix)
generateAndWrite(m.toMap, file)
}
}
}
codegen.writeSupportingClasses(apiMap.toMap, allModels.toMap)
}
def generateAndWrite(bundle: Map[String, AnyRef], templateFile: String) = {
@@ -150,7 +156,7 @@ abstract class BasicGenerator extends CodegenConfig with PathUtil {
println("wrote " + filename)
}
def groupApisToFiles(operations: List[(String /*basePath*/ , String /*apiPath*/ , DocumentationOperation /* operation*/ )]): Map[(String, String), ListBuffer[(String, DocumentationOperation)]] = {
def groupApisToFiles(operations: List[(String /*basePath*/ , String /*apiPath*/ , DocumentationOperation /* operation*/ )]): Map[(String, String), List[(String, DocumentationOperation)]] = {
val opMap = new HashMap[(String, String), ListBuffer[(String, DocumentationOperation)]]
for ((basePath, apiPath, operation) <- operations) {
val className = resourceNameFromFullPath(apiPath)
@@ -161,6 +167,6 @@ abstract class BasicGenerator extends CodegenConfig with PathUtil {
})
listToAddTo += Tuple2(apiPath, operation)
}
opMap.toMap
opMap.map(m => (m._1, m._2.toList)).toMap
}
}

View File

@@ -81,7 +81,7 @@ class BasicScalaGenerator extends BasicGenerator {
val e = "java.util.List[%s]" format toDeclaredType(inner)
(e, toDefaultValue(inner, obj))
}
case e: String => (e, toDefaultValue(e, obj))
case e: String => (toDeclaredType(e), toDefaultValue(e, obj))
}
}

View File

@@ -442,7 +442,7 @@ class Codegen(config: CodegenConfig) {
}
}
def writeSupportingClasses(apis: Map[(String, String), scala.collection.mutable.ListBuffer[(String, com.wordnik.swagger.core.DocumentationOperation)]], models: Map[String, DocumentationSchema]) = {
def writeSupportingClasses(apis: Map[(String, String), List[(String, com.wordnik.swagger.core.DocumentationOperation)]], models: Map[String, DocumentationSchema]) = {
val rootDir = new java.io.File(".")
val engine = new TemplateEngine(Some(rootDir))

View File

@@ -50,4 +50,27 @@ class BasicGeneratorTest extends FlatSpec with ShouldMatchers {
orderApi.getParameters.size should be (1)
orderApi.getErrorResponses.size should be (1)
}
it should "verify ops are grouped by path correctly" in {
val resourceListing = json.readValue(ResourceExtractor.extractListing("src/test/resources/petstore/resources.json", None), classOf[Documentation])
val subDocs = ApiExtractor.extractApiDocs("src/test/resources/petstore", resourceListing.getApis.asScala.toList)
val allModels = new HashMap[String, DocumentationSchema]()
implicit val basePath = "http://localhost:8080/api"
val generator = new SampleGenerator
val ops = generator.extractOperations(subDocs, allModels)
val apiMap = generator.groupApisToFiles(ops)
// verify all apis are there
(apiMap.keys.map(m => m._2).toSet & Set("user", "pet", "store")).size should be (3)
// inspect the store apis
val orderApis = apiMap("http://petstore.swagger.wordnik.com/api","store").groupBy(_._1).toMap
val orderOperations = orderApis("/store.{format}/order/{orderId}").map(m => m._2)
// 2 operations
orderOperations.size should be (2)
(orderOperations.map(m => m.httpMethod).toSet & Set("GET", "DELETE")).size should be (2)
(orderOperations.map(m => m.nickname).toSet & Set("getOrderById", "deleteOrder")).size should be (2)
}
}