Skip to content

Instantly share code, notes, and snippets.

View eugenp's full-sized avatar

Eugen eugenp

View GitHub Profile
@eugenp
eugenp / DiscoverabilityCreationTest.java
Created November 3, 2011 22:36
RESTful Web Service Discoverability with Spring MVC 3.1, part 4 - the CREATION test
@Test
public void whenResourceIsCreated_thenURIOfTheNewlyCreatedResourceIsDiscoverable(){
// When
Foo unpersistedResource = new Foo( randomAlphabetic( 6 ) );
Response createResponse = givenAuthenticated().contentType( MIME_JSON )
.body( unpersistedResource ).post( paths.getFooURL() );
final String uriOfNewlyCreatedResource = createResp
.getHeader( HttpHeaders.LOCATION );
// Then
@eugenp
eugenp / CreationListener.java
Created November 3, 2011 22:44
RESTful Web Service Discoverability with Spring MVC 3.1, part X - the CREATION listener
@Component
class AddLinkHeaderListener implements ApplicationListener< EntityCreated >{
@Override
public void onApplicationEvent( final EntityCreated entityCreatedEvent ){
Preconditions.checkNotNull( entityCreatedEvent );
final HttpServletRequest request = entityCreatedEvent.getRequest();
final HttpServletResponse response = entityCreatedEvent.getResponse();
final long idOfNewEntity = entityCreatedEvent.getIdOfNewEntity();
@eugenp
eugenp / AllowHTTPMethodsTest.java
Created November 4, 2011 20:48
RESTful Web Service Discoverability with Spring MVC 3.1, part 4 - the Allow HTTP methods test
@Test
public void
whenInvalidPOSTIsSentToValidURIOfResource_thenAllowHeaderListsTheAllowedActions(){
// Given
final String uriOfExistingResource = restTemplate.createResource();
// When
Response res = givenAuthenticated().post( uriOfExistingResource );
// Then
@eugenp
eugenp / SecurityUtil.java
Created November 5, 2011 14:23
part 5 - the Security test utils
public String authenticateAsAdmin(){
return this.authenticate( "eparaschiv", "eparaschiv" );
}
public String authenticate( String username, String password ){
Response response = RestAssured.given().
.param( UsernamePasswordAuthenticationFilter.SPRING_SECURITY_FORM_USERNAME_KEY, username )
.param( UsernamePasswordAuthenticationFilter.SPRING_SECURITY_FORM_PASSWORD_KEY, password )
.post( this.examplePaths.getLoginURL() );
Preconditions.checkState( response.getStatusCode() == 302 );
@eugenp
eugenp / DiscoverabilityCreationTest.java
Created November 5, 2011 15:49
RESTful Web Service Discoverability with Spring MVC 3.1, part 4 - the Discover Creation URI test
@Test
public void whenResourceIsRetrieved_thenURIToCreateANewResourceIsDiscoverable(){
// Given
String uriOfNewlyCreatedResource = this.restTemplate.createResource();
// When
Response response = this.givenAuthenticated().get( uriOfNewlyCreatedResource );
// Then
String uriForResourceCreation = response.getHeader( "Link" );
@eugenp
eugenp / HTTPHeaderUtils.java
Created November 5, 2011 23:05
RESTful Web Service Discoverability with Spring MVC 3.1, part 4 - the HTTP Header utils
public static List< String > parseLinkHeader( String linkHeader ){
Preconditions.checkNotNull( linkHeader );
List< String > linkHeaders = Lists.newArrayList();
String[] links = linkHeader.split( ", " );
for( final String link : links ){
int positionOfSeparator = link.indexOf( ';' );
linkHeaders.add( link.substring( 1, positionOfSeparator - 1 ) );
}
@eugenp
eugenp / GetAllDiscoverableTest.java
Created November 5, 2011 23:20
RESTful Web Service Discoverability with Spring MVC 3.1, part 4 - the Discover GET all test
@Test
public void whenResourceIsRetrieved_thenURIToGetAllResourcesIsDiscoverable(){
// Given
String uriOfExistingResource = restTemplate.createResource();
// When
Response getResponse = givenAuthenticated().get( uriOfExistingResource );
// Then
String uriToAllResources = HTTPLinkHeaderUtils.extractURIByRel
@eugenp
eugenp / FooController.java
Created November 12, 2011 15:42
REST Service Discoverability with Spring, part 5 - the Controller and event decoupling
@RequestMapping( value = "admin/foo/{id}",method = RequestMethod.GET )
@ResponseBody
public Foo get( @PathVariable( "id" ) Long id,
HttpServletRequest request, HttpServletResponse response ){
Foo resourceById = RestPreconditions.checkNotNull( service.getById( id ) );
eventPublisher.publishEvent
( new SingleResourceRetrieved( this, request, response ) );
return resourceById;
}
@eugenp
eugenp / CreationListener.java
Created November 12, 2011 15:50
REST Service Discoverability with Spring, part 5 - the Resource Creation listener
@Component
class ResourceCreatedDiscoverabilityListener
implements ApplicationListener< ResourceCreated >{
@Override
public void onApplicationEvent( ResourceCreated resourceCreatedEvent ){
Preconditions.checkNotNull( resourceCreatedEvent );
HttpServletRequest request = resourceCreatedEvent.getRequest();
HttpServletResponse response = resourceCreatedEvent.getResponse();
@eugenp
eugenp / ResourceRetrievedListener.java
Created November 12, 2011 15:53
REST Service Discoverability with Spring, part 5 - the Single Resource Retrieved listener
@Component
class SingleResourceRetrievedDiscoverabilityListener
implements ApplicationListener< SingleResourceRetrieved >{
@Override
public void onApplicationEvent( SingleResourceRetrieved resourceRetrievedEvent ){
Preconditions.checkNotNull( resourceRetrievedEvent );
HttpServletRequest request = resourceRetrievedEvent.getRequest();
HttpServletResponse response = resourceRetrievedEvent.getResponse();