Commit 840b2c9f authored by Jacek Furmankiewicz's avatar Jacek Furmankiewicz

BDDs for complex nested customer/customer address test cases

parent 80435508
......@@ -283,7 +283,7 @@ class RequestRouter:
response = self.__generateResponse(request, val, request.code)
except exceptions.TypeError as ex:
log.err(ex)
log.msg(ex,logLevel=logging.WARN)
response = self.__createErrorResponse(request,400,"%s" % ex)
except RESTException as ex:
......@@ -299,7 +299,7 @@ class RequestRouter:
response = self.__createErrorResponse(request,500,"Unexpected server error: %s\n%s" % (type(ex),ex))
else:
log.msg(ex,logLevel=logging.WARN)
log.msg("URL %s not found" % path,logLevel=logging.WARN)
response = self.__createErrorResponse(request,404,"URL '%s' not found\n" % request.path)
except Exception as ex:
......
......@@ -16,7 +16,7 @@ Feature: REST App
Then I expect HTTP code 201
@full
@customer
Scenario: Full Customer lifecycle
When as user 'None:None' I GET 'http://127.0.0.1:8085/customer'
Then I expect HTTP code 200
......@@ -24,19 +24,19 @@ Feature: REST App
"""
[
{
"addresses": [],
"addresses": {},
"customerId": "d2",
"firstName": "John",
"lastName": "Doe2"
},
{
"addresses": [],
"addresses": {},
"customerId": "d3",
"firstName": "John",
"lastName": "Doe3"
},
{
"addresses": [],
"addresses": {},
"customerId": "d1",
"firstName": "John",
"lastName": "Doe1"
......@@ -51,7 +51,7 @@ Feature: REST App
And I expect JSON content
"""
{
"addresses": [],
"addresses": {},
"customerId": "c1",
"firstName": "John",
"lastName": "Doe"
......@@ -65,7 +65,7 @@ Feature: REST App
And I expect JSON content
"""
{
"addresses": [],
"addresses": {},
"customerId": "c1",
"firstName": "Jill",
"lastName": "Jones"
......@@ -86,3 +86,26 @@ Feature: REST App
[]
"""
@customer_address
Scenario: Full Customer Address lifecycle
When as user 'None:None' I GET 'http://127.0.0.1:8085/customer/d1/address'
Then I expect HTTP code 200
And I expect JSON content
"""
{}
"""
# add 1
When as user 'None:None' I POST 'http://127.0.0.1:8085/customer/d1/address' with 'addressId=HOME&streetNumber=100&streetName=MyStreet&stateCode=CA&countryCode=US'
Then I expect HTTP code 201
When as user 'None:None' I GET 'http://127.0.0.1:8085/customer/d1/address/HOME'
Then I expect HTTP code 200
And I expect JSON content
"""
{
"countryCode": "US",
"stateCode": "CA",
"streetName": "MyStreet",
"streetNumber": "100"
}
"""
\ No newline at end of file
Using step definitions from: '../steps'
@url_routing
Feature: URL routing
CorePost should be able to
correctly route requests
......
......@@ -5,72 +5,132 @@ Server tests
from corepost import Response, NotFoundException, AlreadyExistsException
from corepost.web import RESTResource, route, Http
from twisted.python import log
import sys
#log.startLogging(sys.stdout)
class DB():
"""Fake in-memory DB for testing"""
customers = {}
class Customer():
@classmethod
def getAllCustomers(cls):
return DB.customers.values()
@classmethod
def getCustomer(cls,customerId):
if customerId in DB.customers:
return DB.customers[customerId]
else:
raise NotFoundException("Customer",customerId)
@classmethod
def saveCustomer(cls,customer):
if customer.customerId in DB.customers:
raise AlreadyExistsException("Customer",customer.customerId)
else:
DB.customers[customer.customerId] = customer
@classmethod
def deleteCustomer(cls,customerId):
if customerId in DB.customers:
del(DB.customers[customerId])
else:
raise NotFoundException("Customer",customerId)
@classmethod
def deleteAllCustomers(cls):
DB.customers.clear()
@classmethod
def getCustomerAddress(cls,customerId,addressId):
c = DB.getCustomer(customerId)
if addressId in c.addresses:
return c.addresses[addressId]
else:
raise NotFoundException("Customer Address",addressId)
class Customer:
"""Represents customer entity"""
def __init__(self,customerId,firstName,lastName):
(self.customerId,self.firstName,self.lastName) = (customerId,firstName,lastName)
self.addresses = []
self.addresses = {}
class CustomerAddress():
class CustomerAddress:
"""Represents customer address entity"""
def __init__(self,customer,streetNumber,streetName,stateCode,countryCode):
(self.customer,self.streetNumber,self.streetName.self.stateCode,self.countryCode) = (customer,streetNumber,streetName,stateCode,countryCode)
def __init__(self,streetNumber,streetName,stateCode,countryCode):
(self.streetNumber,self.streetName,self.stateCode,self.countryCode) = (streetNumber,streetName,stateCode,countryCode)
class CustomerRestService():
class CustomerRESTService():
path = "/customer"
@route("/")
def getAll(self,request):
return DB.customers.values()
return DB.getAllCustomers()
@route("/<customerId>")
def get(self,request,customerId):
if customerId in DB.customers:
return DB.customers[customerId]
else:
raise NotFoundException("Customer", customerId)
return DB.getCustomer(customerId)
@route("/",Http.POST)
def post(self,request,customerId,firstName,lastName):
if customerId in DB.customers:
raise AlreadyExistsException("Customer",customerId)
else:
DB.customers[customerId] = Customer(customerId, firstName, lastName)
customer = Customer(customerId, firstName, lastName)
DB.saveCustomer(customer)
return Response(201)
@route("/<customerId>",Http.PUT)
def put(self,request,customerId,firstName,lastName):
if customerId in DB.customers:
DB.customers[customerId].firstName = firstName
DB.customers[customerId].lastName = lastName
c = DB.getCustomer(customerId)
(c.firstName,c.lastName) = (firstName,lastName)
return Response(200)
else:
raise NotFoundException("Customer", customerId)
@route("/<customerId>",Http.DELETE)
def delete(self,request,customerId):
if customerId in DB.customers:
del(DB.customers[customerId])
DB.deleteCustomer(customerId)
return Response(200)
else:
raise NotFoundException("Customer", customerId)
@route("/",Http.DELETE)
def deleteAll(self,request):
DB.customers.clear()
DB.deleteAllCustomers()
return Response(200)
class CustomerAddressRESTService():
path = "/customer/<customerId>/address"
@route("/")
def getAll(self,request,customerId):
return DB.getCustomer(customerId).addresses
@route("/<addressId>")
def get(self,request,customerId,addressId):
return DB.getCustomerAddress(customerId, addressId)
@route("/",Http.POST)
def post(self,request,customerId,addressId,streetNumber,streetName,stateCode,countryCode):
c = DB.getCustomer(customerId)
address = CustomerAddress(streetNumber,streetName,stateCode,countryCode)
c.addresses[addressId] = address
return Response(201)
@route("/<addressId>",Http.PUT)
def put(self,request,customerId,addressId,streetNumber,streetName,stateCode,countryCode):
address = DB.getCustomerAddress(customerId, addressId)
(address.streetNumber,address.streetName,address.stateCode,address.countryCode) = (streetNumber,streetName,stateCode,countryCode)
return Response(200)
@route("/<addressId>",Http.DELETE)
def delete(self,request,customerId,addressId):
DB.getCustomerAddress(customerId, addressId) #validate address exists
del(DB.getCustomer(customerId).addresses[addressId])
return Response(200)
@route("/",Http.DELETE)
def deleteAll(self,request,customerId):
c = DB.getCustomer(customerId)
c.addresses = {}
return Response(200)
def run_rest_app():
app = RESTResource((CustomerRestService(),))
app = RESTResource((CustomerRESTService(),CustomerAddressRESTService()))
app.run(8085)
if __name__ == "__main__":
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment