forked from loafle/openapi-generator-original
added model test
This commit is contained in:
parent
7e7005abe2
commit
b0993379ee
@ -118,40 +118,47 @@ object CoreUtils {
|
|||||||
|
|
||||||
val subNames = new HashSet[String]
|
val subNames = new HashSet[String]
|
||||||
// look inside top-level models
|
// look inside top-level models
|
||||||
requiredModels.map(model => {
|
recurseModels(requiredModels.toMap, modelObjects.toMap, subNames)
|
||||||
model._2.properties.foreach(prop => {
|
|
||||||
val subObject = prop._2
|
|
||||||
if (containers.contains(subObject.`type`)) {
|
|
||||||
subObject.items match {
|
|
||||||
case Some(subItem) => {
|
|
||||||
val sn = subItem.ref.getOrElse(subItem.`type`)
|
|
||||||
if(sn != null)
|
|
||||||
subNames += sn
|
|
||||||
}
|
|
||||||
case _ =>
|
|
||||||
}
|
|
||||||
} else subNames += subObject.`type`
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
// look inside submodels
|
|
||||||
modelObjects.filter(obj => subNames.contains(obj._1)).foreach(model => {
|
|
||||||
model._2.properties.foreach(prop => {
|
|
||||||
val subObject = prop._2
|
|
||||||
if (containers.contains(subObject.`type`)) {
|
|
||||||
subObject.items match {
|
|
||||||
case Some(subItem) => {
|
|
||||||
val sn = subItem.ref.getOrElse(subItem.`type`)
|
|
||||||
if(sn != null)
|
|
||||||
subNames += sn
|
|
||||||
}
|
|
||||||
case _ =>
|
|
||||||
}
|
|
||||||
} else subNames += subObject.`type`
|
|
||||||
})
|
|
||||||
})
|
|
||||||
val subModels = modelObjects.filter(obj => subNames.contains(obj._1))
|
val subModels = modelObjects.filter(obj => subNames.contains(obj._1))
|
||||||
val allModels = requiredModels ++ subModels
|
val allModels = requiredModels ++ subModels
|
||||||
allModels.filter(m => primitives.contains(m._1) == false).toMap
|
allModels.filter(m => primitives.contains(m._1) == false).toMap
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def recurseModels(requiredModels: Map[String, Model], allModels: Map[String, Model], subNames: HashSet[String]) = {
|
||||||
|
requiredModels.map(m => recurseModel(m._2, allModels, subNames))
|
||||||
|
}
|
||||||
|
|
||||||
|
def recurseModel(model: Model, allModels: Map[String, Model], subNames: HashSet[String]): Unit = {
|
||||||
|
model.properties.foreach(prop => {
|
||||||
|
val subObject = prop._2
|
||||||
|
val propertyName = containers.contains(subObject.`type`) match {
|
||||||
|
case true => subObject.items match {
|
||||||
|
case Some(subItem) => {
|
||||||
|
Option(subItem.ref.getOrElse(subItem.`type`)) match {
|
||||||
|
case Some(sn) => Some(sn)
|
||||||
|
case _ => None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case _ => None
|
||||||
|
}
|
||||||
|
case false => Some(subObject.`type`)
|
||||||
|
}
|
||||||
|
propertyName match {
|
||||||
|
case Some(property) => subNames.contains(property) match {
|
||||||
|
case false => {
|
||||||
|
allModels.containsKey(property) match {
|
||||||
|
case true => {
|
||||||
|
recurseModel(allModels(property), allModels, subNames)
|
||||||
|
}
|
||||||
|
case false =>
|
||||||
|
}
|
||||||
|
subNames += property
|
||||||
|
}
|
||||||
|
case true =>
|
||||||
|
}
|
||||||
|
case None =>
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
149
src/test/scala/CoreUtilsTest.scala
Normal file
149
src/test/scala/CoreUtilsTest.scala
Normal file
@ -0,0 +1,149 @@
|
|||||||
|
import com.wordnik.swagger.codegen.util.CoreUtils
|
||||||
|
|
||||||
|
import com.wordnik.swagger.model._
|
||||||
|
import com.wordnik.swagger.codegen.util._
|
||||||
|
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import org.scalatest.junit.JUnitRunner
|
||||||
|
import org.scalatest.FlatSpec
|
||||||
|
import org.scalatest.matchers.ShouldMatchers
|
||||||
|
|
||||||
|
import org.json4s.jackson.JsonMethods._
|
||||||
|
import org.json4s.jackson.Serialization.read
|
||||||
|
|
||||||
|
import scala.collection.mutable.LinkedHashMap
|
||||||
|
|
||||||
|
@RunWith(classOf[JUnitRunner])
|
||||||
|
class CoreUtilsTest extends FlatSpec with ShouldMatchers {
|
||||||
|
sys.props += "fileMap" -> "src/test/resources/petstore"
|
||||||
|
|
||||||
|
behavior of "CoreUtils"
|
||||||
|
|
||||||
|
it should "verify models are extracted" in {
|
||||||
|
val resourceListing = ResourceExtractor.fetchListing("src/test/resources/petstore/resources.json")
|
||||||
|
val apis = ApiExtractor.extractApiOperations("src/test/resources/petstore", resourceListing.apis)
|
||||||
|
|
||||||
|
val cu = CoreUtils.extractAllModels2(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 resourceListing = ResourceExtractor.fetchListing("src/test/resources/petstore/resources.json")
|
||||||
|
val apis = ApiExtractor.extractApiOperations("src/test/resources/petstore", resourceListing.apis)
|
||||||
|
|
||||||
|
val petApi = apis.filter(api => api.resourcePath == "/pet").head
|
||||||
|
val eps = petApi.apis.map(api => (api.path, api)).toMap
|
||||||
|
val ops = eps("/pet.{format}").operations.map(ep => (ep.nickname, ep)).toMap
|
||||||
|
|
||||||
|
ops.size should be (2)
|
||||||
|
|
||||||
|
(ops.keys.toSet & Set("addPet", "updatePet")).size should be (2)
|
||||||
|
}
|
||||||
|
|
||||||
|
it should "find required models" in {
|
||||||
|
val apis = CoreUtilsTest.sampleApis
|
||||||
|
|
||||||
|
val models = CoreUtils.extractApiModels(apis.head)
|
||||||
|
models.size should be (5)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
object CoreUtilsTest {
|
||||||
|
implicit val formats = SwaggerSerializers.formats
|
||||||
|
|
||||||
|
def sampleApis = {
|
||||||
|
parse("""
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"apiVersion": "0.2",
|
||||||
|
"swaggerVersion": "1.1",
|
||||||
|
"basePath": "http://api.helloreverb.com/api",
|
||||||
|
"resourcePath": "/mysteries",
|
||||||
|
"apis": [
|
||||||
|
{
|
||||||
|
"path": "/mysteries.{format}/{petId}",
|
||||||
|
"description": "As the name suggests",
|
||||||
|
"operations": [
|
||||||
|
{
|
||||||
|
"httpMethod": "GET",
|
||||||
|
"summary": "You find amazing htings here",
|
||||||
|
"responseClass": "DeepMystery",
|
||||||
|
"nickname": "getMysteryById",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"name": "id",
|
||||||
|
"description": "ID of mystery",
|
||||||
|
"paramType": "path",
|
||||||
|
"required": true,
|
||||||
|
"allowMultiple": false,
|
||||||
|
"dataType": "string"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"models": {
|
||||||
|
"DeepMystery": {
|
||||||
|
"id": "DeepMystery",
|
||||||
|
"properties": {
|
||||||
|
"id": {
|
||||||
|
"type": "Mystery1"
|
||||||
|
},
|
||||||
|
"name": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Mystery1": {
|
||||||
|
"id": "Mystery1",
|
||||||
|
"properties": {
|
||||||
|
"mystery2": {
|
||||||
|
"type": "Mystery2"
|
||||||
|
},
|
||||||
|
"name": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Mystery2": {
|
||||||
|
"id": "Mystery2",
|
||||||
|
"properties": {
|
||||||
|
"mystery3": {
|
||||||
|
"type": "Mystery3"
|
||||||
|
},
|
||||||
|
"name": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Mystery3": {
|
||||||
|
"id": "Mystery3",
|
||||||
|
"properties": {
|
||||||
|
"mystery4": {
|
||||||
|
"type": "Mystery4"
|
||||||
|
},
|
||||||
|
"name": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Mystery4": {
|
||||||
|
"id": "Mystery4",
|
||||||
|
"properties": {
|
||||||
|
"id": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"name": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
""").extract[List[ApiListing]]
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user