From dd68c31fb4d9f9fe6eeb417ca5ba0798287c98e8 Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Wed, 14 Aug 2013 14:41:35 -0700 Subject: [PATCH] fixed tests --- src/test/scala/BasicObjcGeneratorTest.scala | 272 ------------------ .../ModelSerializerValidations.scala | 10 +- .../swaggerSpec1_2/ModelSerializersTest.scala | 12 +- 3 files changed, 11 insertions(+), 283 deletions(-) delete mode 100644 src/test/scala/BasicObjcGeneratorTest.scala diff --git a/src/test/scala/BasicObjcGeneratorTest.scala b/src/test/scala/BasicObjcGeneratorTest.scala deleted file mode 100644 index 750c2daa05a..00000000000 --- a/src/test/scala/BasicObjcGeneratorTest.scala +++ /dev/null @@ -1,272 +0,0 @@ -/** - * Copyright 2013 Wordnik, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import com.wordnik.swagger.model._ -import com.wordnik.swagger.codegen.{BasicObjcGenerator, Codegen, PathUtil} -import com.wordnik.swagger.codegen.util._ -import com.wordnik.swagger.codegen.language._ - -import org.junit.runner.RunWith -import org.scalatest.junit.JUnitRunner -import org.scalatest.FlatSpec -import org.scalatest.matchers.ShouldMatchers - -import scala.collection.mutable.HashMap - -@RunWith(classOf[JUnitRunner]) -class BasicObjcGeneratorTest extends FlatSpec with ShouldMatchers { - val config = new BasicObjcGenerator - - behavior of "BasicObjcGenerator" - /* - * 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 (Some("void")) - } - - /* - * swagger strings are turned into Objective-C NSString* - */ - it should "process a string response" in { - config.processResponseDeclaration("string") should be (Some("NSString*")) - } - - /* - * swagger int is turned into Objective-c Int - */ - it should "process an unmapped response type" in { - config.processResponseDeclaration("int") should be (Some("NSNumber*")) - } - - /* - * returns the invoker package from the config - */ - it should "get the invoker package" in { - config.invokerPackage should be (None) - } - - /* - * returns the api package - */ - it should "get the api package" in { - config.apiPackage should be (None) - } - - /* - * returns the model package - */ - it should "get the model package" in { - config.modelPackage should be (None) - } - - /* - * types are mapped between swagger types and language-specific - * types - */ - it should "convert to a declared type" in { - config.toDeclaredType("string") should be ("NSString*") - config.toDeclaredType("int") should be ("NSNumber*") - config.toDeclaredType("float") should be ("NSNumber*") - config.toDeclaredType("long") should be ("NSNumber*") - config.toDeclaredType("double") should be ("NSNumber*") - config.toDeclaredType("object") should be ("NSObject*") - config.toDeclaredType("User") should be ("NIKUser*") - } - - /* - * declarations are used in models, and types need to be - * mapped appropriately - */ - it should "convert a string a declaration" in { - val expected = Map("string" -> ("NSString*", "null"), - "int" -> ("NSNumber*", "null"), - "float" -> ("NSNumber*", "null"), - "long" -> ("NSNumber*", "null"), - "double" -> ("NSNumber*", "null"), - "object" -> ("NSObject*", "null")) - expected.map(e => { - val model = ModelProperty(e._1, "nothing") - config.toDeclaration(model) should be (e._2) - }) - } - - /* - * codegen should honor special imports to avoid generating - * classes - */ - it should "honor the import mapping" in { - config.importMapping("Date") should be ("NIKDate") - } - - /* - * single tick reserved words - */ - it should "quote a reserved var name" in { - config.toVarName("char") should be ("_char") - } - - /* - * support list declarations with string inner value and the correct default value - */ - it should "create a declaration with a List of strings" in { - val property = ModelProperty( - `type` = "Array", - qualifiedType = "nothing", - items=Some(ModelRef(`type`= "string"))) - val m = config.toDeclaration(property) - m._1 should be ("NSArray*") - m._2 should be ("null") - } - - /* - * support list declarations with int inner value and the correct default value - */ - it should "create a declaration with a List of ints" in { - val property = ModelProperty( - `type` = "Array", - qualifiedType = "nothing", - items=Some(ModelRef(`type`= "int"))) - val m = config.toDeclaration(property) - m._1 should be ("NSArray*") - m._2 should be ("null") - } - - /* - * support list declarations with float inner value and the correct default value - */ - it should "create a declaration with a List of floats" in { - val property = ModelProperty( - `type` = "Array", - qualifiedType = "nothing", - items=Some(ModelRef(`type`= "float"))) - val m = config.toDeclaration(property) - m._1 should be ("NSArray*") - m._2 should be ("null") - } - - /* - * support list declarations with double inner value and the correct default value - */ - it should "create a declaration with a List of doubles" in { - val property = ModelProperty( - `type` = "Array", - qualifiedType = "nothing", - items=Some(ModelRef(`type`= "double"))) - val m = config.toDeclaration(property) - m._1 should be ("NSArray*") - m._2 should be ("null") - } - - /* - * support list declarations with complex inner value and the correct default value - */ - it should "create a declaration with a List of complex objects" in { - val property = ModelProperty( - `type` = "Array", - qualifiedType = "nothing", - items=Some(ModelRef(`type`= "User"))) - val m = config.toDeclaration(property) - m._1 should be ("NSArray*") - m._2 should be ("null") - } - - it should "verify an api map with path param" in { - val resourceListing = ResourceExtractor.fetchListing("src/test/resources/petstore-1.1/resources.json", None) - val apis = ApiExtractor.extractApiOperations("1.1", "src/test/resources/petstore-1.1", resourceListing.apis) - val codegen = new Codegen(config) - val petApi = apis.filter(doc => doc.resourcePath == "/pet").head - - val endpoint = petApi.apis.filter(api => api.path == "/pet.{format}/{petId}").head - val operation = endpoint.operations.filter(op => op.method == "GET").head - val m = codegen.apiToMap("http://my.api.com/api", operation) - - m("path") should be ("http://my.api.com/api") - m("bodyParams").asInstanceOf[List[_]].size should be (0) - m("httpMethod") should be ("GET") - // Pet => NIKPet - m("returnBaseType") should be (Some("NIKPet")) - m("returnTypeIsPrimitive") should be (None) - m("pathParams").asInstanceOf[List[_]].size should be (1) - - val idParam = m("pathParams").asInstanceOf[List[_]].head.asInstanceOf[HashMap[String, _]] - idParam("paramName") should be ("petId") - idParam("dataType") should be ("NSString*") - idParam("required") should be ("true") - idParam("swaggerDataType") should be ("string") - idParam("baseName") should be ("petId") - idParam("type") should be ("path") - idParam("allowMultiple") should be ("false") - idParam("defaultValue") should be (None) - } - - it should "verify an api map with query params" in { - val resourceListing = ResourceExtractor.fetchListing("src/test/resources/petstore-1.1/resources.json", None) - val apis = ApiExtractor.extractApiOperations("1.1", "src/test/resources/petstore-1.1", resourceListing.apis) - val codegen = new Codegen(config) - val petApi = apis.filter(doc => doc.resourcePath == "/pet").head - - val endpoint = petApi.apis.filter(api => api.path == "/pet.{format}/findByTags").head - val operation = endpoint.operations.filter(op => op.method == "GET").head - val m = codegen.apiToMap("http://my.api.com/api", operation) - - m("path") should be ("http://my.api.com/api") - m("bodyParams").asInstanceOf[List[_]].size should be (0) - m("httpMethod") should be ("GET") - - // Pet => NIKPet - m("returnBaseType") should be (Some("NIKPet")) - m("returnType") should be (Some("NSArray*")) - m("returnTypeIsPrimitive") should be (None) - m("pathParams").asInstanceOf[List[_]].size should be (0) - m("returnContainer") should be ("List") - m("requiredParamCount") should be ("1") - - val queryParams = m("queryParams").asInstanceOf[List[_]] - queryParams.size should be (1) - - val queryParam = queryParams.head.asInstanceOf[HashMap[String, _]] - queryParam("type") should be ("query") - queryParam("dataType") should be ("NSString*") - queryParam("required") should be ("true") - queryParam("paramName") should be ("tags") - queryParam("swaggerDataType") should be ("string") - queryParam("allowMultiple") should be ("true") - } - - it should "create an api file" in { - implicit val basePath = "http://localhost:8080/api" - val codegen = new Codegen(config) - val resourceListing = ResourceExtractor.fetchListing("src/test/resources/petstore-1.1/resources.json", None) - - val apis = ApiExtractor.extractApiOperations("1.1", "src/test/resources/petstore-1.1", resourceListing.apis) - val petApi = apis.filter(doc => doc.resourcePath == "/pet").head - - val endpoint = petApi.apis.filter(api => api.path == "/pet.{format}/findByTags").head - val operation = endpoint.operations.filter(op => op.method == "GET").head - val m = codegen.apiToMap("http://my.api.com/api", operation) - - val allModels = new HashMap[String, Model] - val operations = config.extractApiOperations(apis, allModels) - - val apiMap = config.groupOperationsToFiles(operations) - val bundle = config.prepareApiBundle(apiMap) - val apiFiles = config.bundleToSource(bundle, config.apiTemplateFiles.toMap) - - apiFiles.size should be (6) - } -} diff --git a/src/test/scala/swaggerSpec1_2/ModelSerializerValidations.scala b/src/test/scala/swaggerSpec1_2/ModelSerializerValidations.scala index 55053cdb8ac..60dc434cfef 100644 --- a/src/test/scala/swaggerSpec1_2/ModelSerializerValidations.scala +++ b/src/test/scala/swaggerSpec1_2/ModelSerializerValidations.scala @@ -183,7 +183,7 @@ class OperationValidationTest extends FlatSpec with ShouldMatchers { List.empty, List(Parameter("id", Some("the id"), Some("-1"), false, true, "string", AllowableListValues(List("a","b","c")), "query")) ) - write(op) should be ("""{"method":"get","summary":"the summary","notes":"the notes","type":"string","nickname":"getMeSomeStrings","parameters":[{"name":"id","description":"the id","defaultValue":"-1","required":false,"allowMultiple":true,"type":"string","allowableValues":{"valueType":"LIST","values":["a","b","c"]},"paramType":"query"}]}""") + write(op) should be ("""{"method":"get","summary":"the summary","notes":"the notes","type":"string","nickname":"getMeSomeStrings","parameters":[{"name":"id","description":"the id","defaultValue":"-1","required":false,"allowMultiple":true,"type":"string","paramType":"query","enum":["a","b","c"]}]}""") } } @@ -355,7 +355,7 @@ class ModelValidationTest extends FlatSpec with ShouldMatchers { it should "serialize a model" in { val ref = Model("Foo", "Bar", "Bar", (LinkedHashMap("s" -> ModelProperty("string", "string", 0, true, Some("a string"))))) - write(ref) should be ("""{"id":"Foo","name":"Bar","properties":{"s":{"type":"string","required":true,"description":"a string"}}}""") + write(ref) should be ("""{"id":"Foo","name":"Bar","required":["s"],"properties":{"s":{"type":"string","description":"a string"}}}""") } } @@ -427,7 +427,7 @@ class ModelPropertyValidationTest extends FlatSpec with ShouldMatchers { it should "serialize a model property with allowable values and ref" in { val p = ModelProperty("string", "string", 0, false, Some("nice"), AllowableListValues(List("a","b")),Some(ModelRef("Foo",Some("Bar")))) - write(p) should be ("""{"type":"string","required":false,"description":"nice","allowableValues":{"valueType":"LIST","values":["a","b"]},"items":{"type":"Foo","$ref":"Bar"}}""") + write(p) should be ("""{"type":"string","description":"nice","items":{"type":"Foo","$ref":"Bar"},"enum":["a","b"]}""") } it should "deserialize a model property with allowable values" in { @@ -456,7 +456,7 @@ class ModelPropertyValidationTest extends FlatSpec with ShouldMatchers { it should "serialize a model property with allowable values" in { val p = ModelProperty("string", "string", 0, false, Some("nice"), AllowableListValues(List("a","b"))) - write(p) should be ("""{"type":"string","required":false,"description":"nice","allowableValues":{"valueType":"LIST","values":["a","b"]}}""") + write(p) should be ("""{"type":"string","description":"nice","enum":["a","b"]}""") } it should "deserialize a model property" in { @@ -480,7 +480,7 @@ class ModelPropertyValidationTest extends FlatSpec with ShouldMatchers { it should "serialize a model property" in { val p = ModelProperty("string", "string", 0, false, Some("nice")) - write(p) should be ("""{"type":"string","required":false,"description":"nice"}""") + write(p) should be ("""{"type":"string","description":"nice"}""") } } diff --git a/src/test/scala/swaggerSpec1_2/ModelSerializersTest.scala b/src/test/scala/swaggerSpec1_2/ModelSerializersTest.scala index d7d81d5fa65..332b25864b8 100644 --- a/src/test/scala/swaggerSpec1_2/ModelSerializersTest.scala +++ b/src/test/scala/swaggerSpec1_2/ModelSerializersTest.scala @@ -203,7 +203,7 @@ class ApiDescriptionSerializersTest extends FlatSpec with ShouldMatchers { List(Parameter("id", Some("the id"), Some("-1"), false, true, "string", AllowableListValues(List("a","b","c")), "query")) )) ) - write(l) should be ("""{"path":"/foo/bar","description":"the description","operations":[{"method":"get","summary":"the summary","notes":"the notes","type":"string","nickname":"getMeSomeStrings","parameters":[{"name":"id","description":"the id","defaultValue":"-1","required":false,"allowMultiple":true,"type":"string","allowableValues":{"valueType":"LIST","values":["a","b","c"]},"paramType":"query"}]}]}""") + write(l) should be ("""{"path":"/foo/bar","description":"the description","operations":[{"method":"get","summary":"the summary","notes":"the notes","type":"string","nickname":"getMeSomeStrings","parameters":[{"name":"id","description":"the id","defaultValue":"-1","required":false,"allowMultiple":true,"type":"string","paramType":"query","enum":["a","b","c"]}]}]}""") } } @@ -271,7 +271,7 @@ class OperationSerializersTest extends FlatSpec with ShouldMatchers { List.empty, List(Parameter("id", Some("the id"), Some("-1"), false, true, "string", AllowableListValues(List("a","b","c")), "query")) ) - write(op) should be ("""{"method":"get","summary":"the summary","notes":"the notes","type":"string","nickname":"getMeSomeStrings","parameters":[{"name":"id","description":"the id","defaultValue":"-1","required":false,"allowMultiple":true,"type":"string","allowableValues":{"valueType":"LIST","values":["a","b","c"]},"paramType":"query"}]}""") + write(op) should be ("""{"method":"get","summary":"the summary","notes":"the notes","type":"string","nickname":"getMeSomeStrings","parameters":[{"name":"id","description":"the id","defaultValue":"-1","required":false,"allowMultiple":true,"type":"string","paramType":"query","enum":["a","b","c"]}]}""") } } @@ -442,7 +442,7 @@ class ModelSerializationTest extends FlatSpec with ShouldMatchers { it should "serialize a model" in { val ref = Model("Foo", "Bar", "Bar", (LinkedHashMap("s" -> ModelProperty("string", "string", 0, true, Some("a string"))))) - write(ref) should be ("""{"id":"Foo","name":"Bar","properties":{"s":{"type":"string","required":true,"description":"a string"}}}""") + write(ref) should be ("""{"id":"Foo","name":"Bar","required":["s"],"properties":{"s":{"type":"string","description":"a string"}}}""") } } @@ -514,7 +514,7 @@ class ModelPropertySerializationTest extends FlatSpec with ShouldMatchers { it should "serialize a model property with allowable values and ref" in { val p = ModelProperty("string", "string", 0, false, Some("nice"), AllowableListValues(List("a","b")),Some(ModelRef("Foo",Some("Bar")))) - write(p) should be ("""{"type":"string","required":false,"description":"nice","allowableValues":{"valueType":"LIST","values":["a","b"]},"items":{"type":"Foo","$ref":"Bar"}}""") + write(p) should be ("""{"type":"string","description":"nice","items":{"type":"Foo","$ref":"Bar"},"enum":["a","b"]}""") } it should "deserialize a model property with allowable values" in { @@ -543,7 +543,7 @@ class ModelPropertySerializationTest extends FlatSpec with ShouldMatchers { it should "serialize a model property with allowable values" in { val p = ModelProperty("string", "string", 0, false, Some("nice"), AllowableListValues(List("a","b"))) - write(p) should be ("""{"type":"string","required":false,"description":"nice","allowableValues":{"valueType":"LIST","values":["a","b"]}}""") + write(p) should be ("""{"type":"string","description":"nice","enum":["a","b"]}""") } it should "deserialize a model property" in { @@ -567,7 +567,7 @@ class ModelPropertySerializationTest extends FlatSpec with ShouldMatchers { it should "serialize a model property" in { val p = ModelProperty("string", "string", 0, false, Some("nice")) - write(p) should be ("""{"type":"string","required":false,"description":"nice"}""") + write(p) should be ("""{"type":"string","description":"nice"}""") } }