diff --git a/source/Stargate-Examples-Tests/PetsRESTfulControllerTest.class.st b/source/Stargate-Examples-Tests/PetsRESTfulControllerTest.class.st index 5349c0c..f9dce30 100644 --- a/source/Stargate-Examples-Tests/PetsRESTfulControllerTest.class.st +++ b/source/Stargate-Examples-Tests/PetsRESTfulControllerTest.class.st @@ -147,6 +147,22 @@ PetsRESTfulControllerTest >> testCantUpdatePetIfMissingETag [ withExceptionDo: [ :error | self assert: error code equals: 428 ] ] +{ #category : #'tests - update' } +PetsRESTfulControllerTest >> testCantUpdatePetNameWhenEmpty [ + + self + createPet; + assert: petRepository findAll first name equals: 'Firulais'. + + self + should: [ + resourceController + updatePetBasedOn: ( self requestToUpdatePetIdentifiedBy: 1 nameTo: '' ) + within: self newHttpRequestContext + ] + raise: HTTPClientError unprocessableEntity +] + { #category : #'tests - update' } PetsRESTfulControllerTest >> testCantUpdatePetWhenETagDoesNotMatch [ diff --git a/source/Stargate-Examples/Pet.class.st b/source/Stargate-Examples/Pet.class.st index c7e5076..f73d7f9 100644 --- a/source/Stargate-Examples/Pet.class.st +++ b/source/Stargate-Examples/Pet.class.st @@ -21,6 +21,11 @@ Pet class >> named: aName ofType: aPetType [ { #category : #'instance creation' } Pet class >> named: aName ofType: aPetType withStatus: status [ + AssertionChecker + enforce: [ aName notEmpty ] + because: 'A pet must have a name' + raising: InstanceCreationFailed. + ^ self new initializeNamed: aName ofType: aPetType withStatus: status ] diff --git a/source/Stargate-Model/RESTfulRequestHandlerBehavior.class.st b/source/Stargate-Model/RESTfulRequestHandlerBehavior.class.st index f2be487..b07bf44 100644 --- a/source/Stargate-Model/RESTfulRequestHandlerBehavior.class.st +++ b/source/Stargate-Model/RESTfulRequestHandlerBehavior.class.st @@ -159,19 +159,21 @@ RESTfulRequestHandlerBehavior >> from: httpRequest within: requestContext get: f from: httpRequest within: requestContext get: [ :id | - | resourceToUpdate updatedResource | - - resourceToUpdate := findBlock cull: id. - self - assert: etag - matchesEntityTagOf: resourceToUpdate - encodedAs: httpRequest contentType - within: requestContext. - - updatedResource := self decode: httpRequest within: requestContext. - self exceptionHandler - handleConflictsDuring: [ updateBlock value: resourceToUpdate value: updatedResource ] - ] + | resourceToUpdate updatedResource | + + resourceToUpdate := findBlock cull: id. + self + assert: etag + matchesEntityTagOf: resourceToUpdate + encodedAs: httpRequest contentType + within: requestContext. + + updatedResource := self decode: httpRequest within: requestContext. + self exceptionHandler handleConflictsDuring: [ + self exceptionHandler handleDecodingFailedDuring: [ + updateBlock value: resourceToUpdate value: updatedResource ] + ] + ] ] { #category : #API }