updated test to match changes in petstore as deployed

This commit is contained in:
Tony Tam 2013-09-16 16:23:56 -07:00
parent 93677742d3
commit e4a4f25f22
12 changed files with 188 additions and 81 deletions

View File

@ -40,10 +40,14 @@ object ApiInvoker {
val defaultHeaders: HashMap[String, String] = HashMap()
val hostMap: HashMap[String, Client] = HashMap()
def escapeString(value: String): String = {
def escape(value: String): String = {
URLEncoder.encode(value, "utf-8").replaceAll("\\+", "%20")
}
def escape(value: Long): String = value.toString
def escape(value: Double): String = value.toString
def escape(value: Float): String = value.toString
def deserialize(json: String, containerType: String, cls: Class[_]) = {
if (cls == classOf[String]) {
json match {
@ -55,12 +59,12 @@ object ApiInvoker {
}
} else {
containerType.toLowerCase match {
case "list" => {
case "array" => {
val typeInfo = mapper.getTypeFactory().constructCollectionType(classOf[java.util.List[_]], cls)
val response = mapper.readValue(json, typeInfo).asInstanceOf[java.util.List[_]]
response.asScala.toList
}
case "array" => {
case "list" => {
val typeInfo = mapper.getTypeFactory().constructCollectionType(classOf[java.util.List[_]], cls)
val response = mapper.readValue(json, typeInfo).asInstanceOf[java.util.List[_]]
response.asScala.toList
@ -84,12 +88,11 @@ object ApiInvoker {
} else null
}
def invokeApi(host: String, path: String, method: String, queryParams: Map[String, String], body: AnyRef, headerParams: Map[String, String], contentType: String) = {
def invokeApi(host: String, path: String, method: String, queryParams: Map[String, String], body: AnyRef, headerParams: Map[String, String], contentType: String): String = {
val client = getClient(host)
val querystring = queryParams.filter(k => k._2 != null).map(k => (escapeString(k._1) + "=" + escapeString(k._2))).mkString("?", "&", "")
val querystring = queryParams.filter(k => k._2 != null).map(k => (escape(k._1) + "=" + escape(k._2))).mkString("?", "&", "")
val builder = client.resource(host + path + querystring).accept(contentType)
headerParams.map(p => builder.header(p._1, p._2))
defaultHeaders.map(p => {
headerParams.contains(p._1) match {
@ -124,12 +127,22 @@ object ApiInvoker {
}
case _ => null
}
response.getClientResponseStatus() match {
case ClientResponse.Status.OK => response.getEntity(classOf[String])
response.getClientResponseStatus().getStatusCode() match {
case 204 => ""
case code: Int if (Range(200, 299).contains(code)) => {
response.hasEntity() match {
case true => response.getEntity(classOf[String])
case false => ""
}
}
case _ => {
val entity = response.hasEntity() match {
case true => response.getEntity(classOf[String])
case false => "no data"
}
throw new ApiException(
response.getClientResponseStatus().getStatusCode(),
response.getEntity(classOf[String]))
entity)
}
}
}
@ -147,12 +160,6 @@ object ApiInvoker {
}
}
class ApiException extends Exception {
var code = 0
def this(code: Int, msg: String) = {
this()
}
}
class ApiException(val code: Int, msg: String) extends RuntimeException(msg)

View File

@ -1,10 +1,12 @@
package com.wordnik.petstore.api
import java.io.File
import com.wordnik.petstore.model.Pet
import com.wordnik.client.ApiInvoker
import com.wordnik.client.ApiException
import java.io.File
import java.util.Date
import scala.collection.mutable.HashMap
@ -14,9 +16,9 @@ class PetApi {
def addHeader(key: String, value: String) = apiInvoker.defaultHeaders += key -> value
def getPetById (petId: String) : Option[Pet]= {
def getPetById (petId: Long) : Option[Pet]= {
// create path and map variables
val path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}",apiInvoker.escapeString(petId))
val path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}",apiInvoker.escape(petId))
val contentType = {
@ -27,7 +29,7 @@ class PetApi {
val headerParams = new HashMap[String, String]
// verify required params are set
(Set(petId) - null).size match {
(List(petId).filter(_ != null)).size match {
case 1 => // all required values set
case _ => throw new Exception("missing required params")
}
@ -44,7 +46,7 @@ class PetApi {
}
def deletePet (petId: String) = {
// create path and map variables
val path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}",apiInvoker.escapeString(petId))
val path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}",apiInvoker.escape(petId))
val contentType = {
@ -55,7 +57,7 @@ class PetApi {
val headerParams = new HashMap[String, String]
// verify required params are set
(Set(petId) - null).size match {
(List(petId).filter(_ != null)).size match {
case 1 => // all required values set
case _ => throw new Exception("missing required params")
}
@ -69,6 +71,87 @@ class PetApi {
case ex: ApiException => throw ex
}
}
def partialUpdate (petId: String, body: Pet) : Option[List[Pet]]= {
// create path and map variables
val path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}",apiInvoker.escape(petId))
val contentType = {
if(body != null && body.isInstanceOf[File] )
"multipart/form-data"
else "application/json"
}
// query params
val queryParams = new HashMap[String, String]
val headerParams = new HashMap[String, String]
// verify required params are set
(List(petId, body).filter(_ != null)).size match {
case 2 => // all required values set
case _ => throw new Exception("missing required params")
}
try {
apiInvoker.invokeApi(basePath, path, "PATCH", queryParams.toMap, body, headerParams.toMap, contentType) match {
case s: String =>
Some(ApiInvoker.deserialize(s, "Array", classOf[Pet]).asInstanceOf[List[Pet]])
case _ => None
}
} catch {
case ex: ApiException if ex.code == 404 => None
case ex: ApiException => throw ex
}
}
def updatePetWithForm (petId: String, name: String, status: String) = {
// create path and map variables
val path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}",apiInvoker.escape(petId))
val contentType = {
"application/json"}
// query params
val queryParams = new HashMap[String, String]
val headerParams = new HashMap[String, String]
// verify required params are set
(List(petId).filter(_ != null)).size match {
case 1 => // all required values set
case _ => throw new Exception("missing required params")
}
try {
apiInvoker.invokeApi(basePath, path, "POST", queryParams.toMap, None, headerParams.toMap, contentType) match {
case s: String =>
case _ => None
}
} catch {
case ex: ApiException if ex.code == 404 => None
case ex: ApiException => throw ex
}
}
def uploadFile (additionalMetadata: String, body: File) = {
// create path and map variables
val path = "/pet/uploadImage".replaceAll("\\{format\\}","json")
val contentType = {
if(body != null && body.isInstanceOf[File] )
"multipart/form-data"
else "application/json"
}
// query params
val queryParams = new HashMap[String, String]
val headerParams = new HashMap[String, String]
try {
apiInvoker.invokeApi(basePath, path, "POST", queryParams.toMap, body, headerParams.toMap, contentType) match {
case s: String =>
case _ => None
}
} catch {
case ex: ApiException if ex.code == 404 => None
case ex: ApiException => throw ex
}
}
def addPet (body: Pet) = {
// create path and map variables
val path = "/pet".replaceAll("\\{format\\}","json")
@ -83,7 +166,7 @@ class PetApi {
val headerParams = new HashMap[String, String]
// verify required params are set
(Set(body) - null).size match {
(List(body).filter(_ != null)).size match {
case 1 => // all required values set
case _ => throw new Exception("missing required params")
}
@ -111,7 +194,7 @@ class PetApi {
val headerParams = new HashMap[String, String]
// verify required params are set
(Set(body) - null).size match {
(List(body).filter(_ != null)).size match {
case 1 => // all required values set
case _ => throw new Exception("missing required params")
}
@ -136,7 +219,7 @@ class PetApi {
val headerParams = new HashMap[String, String]
// verify required params are set
(Set(status) - null).size match {
(List(status).filter(_ != null)).size match {
case 1 => // all required values set
case _ => throw new Exception("missing required params")
}
@ -144,7 +227,7 @@ class PetApi {
try {
apiInvoker.invokeApi(basePath, path, "GET", queryParams.toMap, None, headerParams.toMap, contentType) match {
case s: String =>
Some(ApiInvoker.deserialize(s, "array", classOf[Pet]).asInstanceOf[List[Pet]])
Some(ApiInvoker.deserialize(s, "Array", classOf[Pet]).asInstanceOf[List[Pet]])
case _ => None
}
} catch {
@ -163,7 +246,7 @@ class PetApi {
val headerParams = new HashMap[String, String]
// verify required params are set
(Set(tags) - null).size match {
(List(tags).filter(_ != null)).size match {
case 1 => // all required values set
case _ => throw new Exception("missing required params")
}
@ -171,7 +254,7 @@ class PetApi {
try {
apiInvoker.invokeApi(basePath, path, "GET", queryParams.toMap, None, headerParams.toMap, contentType) match {
case s: String =>
Some(ApiInvoker.deserialize(s, "array", classOf[Pet]).asInstanceOf[List[Pet]])
Some(ApiInvoker.deserialize(s, "Array", classOf[Pet]).asInstanceOf[List[Pet]])
case _ => None
}
} catch {

View File

@ -5,6 +5,7 @@ import com.wordnik.client.ApiInvoker
import com.wordnik.client.ApiException
import java.io.File
import java.util.Date
import scala.collection.mutable.HashMap
@ -16,7 +17,7 @@ class StoreApi {
def getOrderById (orderId: String) : Option[Order]= {
// create path and map variables
val path = "/store/order/{orderId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "orderId" + "\\}",apiInvoker.escapeString(orderId))
val path = "/store/order/{orderId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "orderId" + "\\}",apiInvoker.escape(orderId))
val contentType = {
@ -27,7 +28,7 @@ class StoreApi {
val headerParams = new HashMap[String, String]
// verify required params are set
(Set(orderId) - null).size match {
(List(orderId).filter(_ != null)).size match {
case 1 => // all required values set
case _ => throw new Exception("missing required params")
}
@ -44,7 +45,7 @@ class StoreApi {
}
def deleteOrder (orderId: String) = {
// create path and map variables
val path = "/store/order/{orderId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "orderId" + "\\}",apiInvoker.escapeString(orderId))
val path = "/store/order/{orderId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "orderId" + "\\}",apiInvoker.escape(orderId))
val contentType = {
@ -55,7 +56,7 @@ class StoreApi {
val headerParams = new HashMap[String, String]
// verify required params are set
(Set(orderId) - null).size match {
(List(orderId).filter(_ != null)).size match {
case 1 => // all required values set
case _ => throw new Exception("missing required params")
}
@ -83,7 +84,7 @@ class StoreApi {
val headerParams = new HashMap[String, String]
// verify required params are set
(Set(body) - null).size match {
(List(body).filter(_ != null)).size match {
case 1 => // all required values set
case _ => throw new Exception("missing required params")
}

View File

@ -5,6 +5,7 @@ import com.wordnik.client.ApiInvoker
import com.wordnik.client.ApiException
import java.io.File
import java.util.Date
import scala.collection.mutable.HashMap
@ -28,7 +29,7 @@ class UserApi {
val headerParams = new HashMap[String, String]
// verify required params are set
(Set(body) - null).size match {
(List(body).filter(_ != null)).size match {
case 1 => // all required values set
case _ => throw new Exception("missing required params")
}
@ -42,7 +43,7 @@ class UserApi {
case ex: ApiException => throw ex
}
}
def createUsersWithArrayInput (body: List[User]) = {
def createUsersWithArrayInput (body: Array[User]) = {
// create path and map variables
val path = "/user/createWithArray".replaceAll("\\{format\\}","json")
val contentType = {
@ -56,7 +57,7 @@ class UserApi {
val headerParams = new HashMap[String, String]
// verify required params are set
(Set(body) - null).size match {
(List(body).filter(_ != null)).size match {
case 1 => // all required values set
case _ => throw new Exception("missing required params")
}
@ -70,7 +71,7 @@ class UserApi {
case ex: ApiException => throw ex
}
}
def createUsersWithListInput (body: List[User]) = {
def createUsersWithListInput (body: Array[User]) = {
// create path and map variables
val path = "/user/createWithList".replaceAll("\\{format\\}","json")
val contentType = {
@ -84,7 +85,7 @@ class UserApi {
val headerParams = new HashMap[String, String]
// verify required params are set
(Set(body) - null).size match {
(List(body).filter(_ != null)).size match {
case 1 => // all required values set
case _ => throw new Exception("missing required params")
}
@ -100,7 +101,7 @@ class UserApi {
}
def updateUser (username: String, body: User) = {
// create path and map variables
val path = "/user/{username}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "username" + "\\}",apiInvoker.escapeString(username))
val path = "/user/{username}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "username" + "\\}",apiInvoker.escape(username))
val contentType = {
@ -114,7 +115,7 @@ class UserApi {
val headerParams = new HashMap[String, String]
// verify required params are set
(Set(username, body) - null).size match {
(List(username, body).filter(_ != null)).size match {
case 2 => // all required values set
case _ => throw new Exception("missing required params")
}
@ -130,7 +131,7 @@ class UserApi {
}
def deleteUser (username: String) = {
// create path and map variables
val path = "/user/{username}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "username" + "\\}",apiInvoker.escapeString(username))
val path = "/user/{username}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "username" + "\\}",apiInvoker.escape(username))
val contentType = {
@ -141,7 +142,7 @@ class UserApi {
val headerParams = new HashMap[String, String]
// verify required params are set
(Set(username) - null).size match {
(List(username).filter(_ != null)).size match {
case 1 => // all required values set
case _ => throw new Exception("missing required params")
}
@ -157,7 +158,7 @@ class UserApi {
}
def getUserByName (username: String) : Option[User]= {
// create path and map variables
val path = "/user/{username}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "username" + "\\}",apiInvoker.escapeString(username))
val path = "/user/{username}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "username" + "\\}",apiInvoker.escape(username))
val contentType = {
@ -168,7 +169,7 @@ class UserApi {
val headerParams = new HashMap[String, String]
// verify required params are set
(Set(username) - null).size match {
(List(username).filter(_ != null)).size match {
case 1 => // all required values set
case _ => throw new Exception("missing required params")
}
@ -194,7 +195,7 @@ class UserApi {
val headerParams = new HashMap[String, String]
// verify required params are set
(Set(username, password) - null).size match {
(List(username, password).filter(_ != null)).size match {
case 2 => // all required values set
case _ => throw new Exception("missing required params")
}

View File

@ -1,6 +1,8 @@
package com.wordnik.petstore.model
case class Category (
name: String,
id: Long)
/* Category unique identifier */
id: Long,
/* Name of the category */
name: String)

View File

@ -2,10 +2,14 @@ package com.wordnik.petstore.model
import java.util.Date
case class Order (
/* Unique identifier for the order */
id: Long,
/* Order Status */
status: String,
/* ID of pet being ordered */
petId: Long,
/* Number of pets ordered */
quantity: Int,
/* Status of the order */
status: String,
/* Date shipped, only if it has been */
shipDate: Date)

View File

@ -3,11 +3,16 @@ package com.wordnik.petstore.model
import com.wordnik.petstore.model.Category
import com.wordnik.petstore.model.Tag
case class Pet (
name: String,
/* Unique identifier for the Pet */
id: Long,
/* Category the pet is in */
category: Category,
/* Friendly name of the pet */
name: String,
/* Image URLs */
photoUrls: List[String],
/* Tags assigned to this pet */
tags: List[Tag],
/* pet status in the store */
status: String,
photoUrls: List[String],
category: Category)
status: String)

View File

@ -1,6 +1,8 @@
package com.wordnik.petstore.model
case class Tag (
name: String,
id: Long)
/* Unique identifier for the tag */
id: Long,
/* Friendly name for the tag */
name: String)

View File

@ -1,12 +1,19 @@
package com.wordnik.petstore.model
case class User (
/* Unique identifier for the user */
id: Long,
firstName: String,
/* Unique username */
username: String,
/* First name of the user */
firstName: String,
/* Last name of the user */
lastName: String,
/* Email address of the user */
email: String,
/* Password name of the user */
password: String,
/* Phone number of the user */
phone: String,
/* User Status */
userStatus: Int)

View File

@ -16,7 +16,7 @@ class PetApiTest extends FlatSpec with ShouldMatchers {
val api = new PetApi
it should "fetch a pet" in {
api.getPetById("1") match {
api.getPetById(1) match {
case Some(pet) => {
pet should not be (null)
pet.id should be(1)
@ -27,16 +27,16 @@ class PetApiTest extends FlatSpec with ShouldMatchers {
it should "add a new pet" in {
val pet = Pet(
"dragon",
1000,
(for (i <- (1 to 5)) yield Tag("tag-" + i, i)).toList,
"lost",
Category(1, "sold"),
"dragon",
(for (i <- (1 to 10)) yield "http://foo.com/photo/" + i).toList,
Category("sold", 1)
(for (i <- (1 to 5)) yield Tag(i, "tag-" + i)).toList,
"lost"
)
api.addPet(pet)
api.getPetById("1000") match {
api.getPetById(1000) match {
case Some(pet) => {
pet.id should be(1000)
pet.tags.size should be(5)
@ -52,17 +52,17 @@ class PetApiTest extends FlatSpec with ShouldMatchers {
it should "update a pet" in {
val pet = Pet(
"programmer",
1000,
(for (i <- (1 to 5)) yield Tag("tag-" + i, i)).toList,
"confused",
Category(1, "sold"),
"programmer",
(for (i <- (1 to 10)) yield "http://foo.com/photo/" + i).toList,
Category("sold", 1)
(for (i <- (1 to 5)) yield Tag(i, "tag-" + i)).toList,
"confused"
)
api.addPet(pet)
api.getPetById("1000") match {
api.getPetById(1000) match {
case Some(pet) => {
pet.name should be("programmer")
pet.status should be("confused")
@ -71,7 +71,7 @@ class PetApiTest extends FlatSpec with ShouldMatchers {
}
val updatedPet = pet.copy(status="fulfilled")
api.updatePet(updatedPet)
api.getPetById("1000") match {
api.getPetById(1000) match {
case Some(pet) => {
pet.name should be("programmer")
pet.status should be("fulfilled")

View File

@ -33,10 +33,10 @@ class StoreApiTest extends FlatSpec with ShouldMatchers {
it should "place an order" in {
val now = new java.util.Date
val order = Order (
1000,
"pending",
10,
1000,
101,
"pending",
now)
api.placeOrder(order)
@ -56,9 +56,9 @@ class StoreApiTest extends FlatSpec with ShouldMatchers {
val now = new java.util.Date
val order = Order(
1001,
"pending",
10,
101,
"pending",
now)
api.placeOrder(order)
@ -72,10 +72,5 @@ class StoreApiTest extends FlatSpec with ShouldMatchers {
}
case None =>
}
api.deleteOrder("1001")
intercept[ApiException] {
api.getOrderById("1001") should be (None)
}
}
}

View File

@ -49,8 +49,8 @@ class UserApiTest extends FlatSpec with ShouldMatchers {
it should "create a user" in {
val user = User(
1002,
"Johnny",
"johnny",
"Johnny",
"Rocket",
"johnny@fail.com",
"XXXXXXXXXXX",
@ -72,14 +72,14 @@ class UserApiTest extends FlatSpec with ShouldMatchers {
val userArray = (for (i <- (1 to 2)) yield {
User(
2000 + i,
"Johnny",
"johnny-" + i,
"Johnny",
"Rocket-" + i,
"johnny-" + i + "@fail.com",
"XXXXXXXXXXX",
"408-867-5309",
1)
}).toList
}).toArray
api.createUsersWithArrayInput(userArray)
for (i <- (1 to 2)) {
@ -97,14 +97,14 @@ class UserApiTest extends FlatSpec with ShouldMatchers {
val userList = (for (i <- (1 to 3)) yield {
User(
3000 + i,
"Johnny",
"fred-" + i,
"Johnny",
"Rocket-" + i,
"fred-" + i + "@fail.com",
"XXXXXXXXXXX",
"408-867-5309",
1)
}).toList
}).toArray
api.createUsersWithListInput(userList)
for (i <- (1 to 3)) {
@ -121,8 +121,8 @@ class UserApiTest extends FlatSpec with ShouldMatchers {
it should "update a user" in {
val user = User(
4000,
"Tony",
"tony",
"Tony",
"Tiger",
"tony@fail.com",
"XXXXXXXXXXX",