Skip to content

Instantly share code, notes, and snippets.

View eugenp's full-sized avatar

Eugen eugenp

View GitHub Profile
@eugenp
eugenp / FooService.java
Created October 8, 2011 09:33
Testing the Service Layer - sample Service
@Service
public class FooService implements IFooService{
@Autowired
IFooDAO dao;
@Override
public Long create( final Foo entity ){
return this.dao.create( entity );
}
@eugenp
eugenp / FooDAO.java
Created October 8, 2011 10:54
Testing the Service Layer - the DAO layer
@Repository
public class FooDAO extends HibernateDaoSupport implements IFooDAO{
public Long create( final Foo entity ){
Preconditions.checkNotNull( entity );
return (Long) this.getHibernateTemplate().save( entity );
}
}
@eugenp
eugenp / FooServiceUnitTest.java
Created October 8, 2011 11:51
Testing the Service Layer - the Foo service unit test
public class FooServiceUnitTest{
FooService instance;
private HibernateTemplate hibernateTemplateMock;
@Before
public final void before(){
this.instance = new FooService();
this.instance.dao = new FooDAO();
@eugenp
eugenp / RetrieveUtils.java
Created October 9, 2011 20:08
Integration testing of a REST API - retrieve utilities
public static String retrieveJsonFromResponse( final HttpResponse response )
throws IOException{
Preconditions.checkNotNull( response );
return IOUtils.toString( response.getEntity().getContent() );
}
public static < T >T retrieveResourceFromResponse
( final HttpResponse response, final Class< T > clazz ) throws IOException{
Preconditions.checkNotNull( response );
@eugenp
eugenp / DecorateUtil.java
Created October 9, 2011 20:17
Integration testing of a REST API - decorate utilities
public static < T >HttpEntityEnclosingRequest decorateRequestWithResource
( final HttpEntityEnclosingRequest request, final T resource )
throws IOException{
Preconditions.checkNotNull( request );
Preconditions.checkNotNull( resource );
final String resourceAsJson = JsonUtil.convertResourceToJson( resource );
return JsonUtil.decorateRequestWithJson( request, resourceAsJson );
}
@eugenp
eugenp / ResponseCodeTest.java
Created October 9, 2011 20:21
Integration testing of a REST API - Testing the HTTP response code
@Test
public void givenUserDoesNotExists_whenUserInfoIsRetrieved_then404IsReceived()
throws ClientProtocolException, IOException{
// Given
String name = randomAlphabetic( 8 );
HttpUriRequest request = new HttpGet( "https://api.github.com/users/" + name );
// When
HttpResponse httpResponse = httpClient.execute( request );
@eugenp
eugenp / JsonPayloadTest.java
Created October 9, 2011 20:51
Integration testing of a REST API - Testing the Json payload in the HTTP response
@Test
public void givenUserExists_whenUserInformationIsRetrieved_thenRetrievedResourceIsCorrect()
throws ClientProtocolException, IOException{
// Given
HttpUriRequest request = new HttpGet( "https://api.github.com/users/eugenp" );
// When
HttpResponse response = new DefaultHttpClient().execute( request );
// Then
@eugenp
eugenp / ConversionUtils.java
Created October 12, 2011 22:31
Integration testing of a REST API - conversion utilities
public static < T >String convertResourceToJson( final T resource )
throws IOException{
Preconditions.checkNotNull( resource );
return new ObjectMapper().writeValueAsString( resource );
}
public static < T >T convertJsonToResource
( final String json, final Class< T > clazzOfResource ) throws IOException{
Preconditions.checkNotNull( json );
@eugenp
eugenp / ContentTypeHeaderTest.java
Created October 13, 2011 19:46
Integration testing of a REST API - Testing the ContentType header for the HTTP response
@Test
public void givenRequestWithNoAcceptHeader_whenRequestIsExecuted_thenDefaultResponseContentTypeIsJson()
throws ClientProtocolException, IOException{
// Given
String jsonMimeType = "application/json";
HttpUriRequest request = new HttpGet( "https://api.github.com/users/eugenp" );
// When
HttpResponse response = this.httpClient.execute( request );
@eugenp
eugenp / RestAssert.java
Created October 15, 2011 14:30
Integration testing of a REST API - Custom REST Assert
public static void assertResponseCodeIs
( final HttpResponse response, final int expectedCode ){
final int statusCode = httpResponse.getStatusLine().getStatusCode();
assertEquals( expectedCode, statusCode );
}