This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Service | |
public class FooService implements IFooService{ | |
@Autowired | |
IFooDAO dao; | |
@Override | |
public Long create( final Foo entity ){ | |
return this.dao.create( entity ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Repository | |
public class FooDAO extends HibernateDaoSupport implements IFooDAO{ | |
public Long create( final Foo entity ){ | |
Preconditions.checkNotNull( entity ); | |
return (Long) this.getHibernateTemplate().save( entity ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FooServiceUnitTest{ | |
FooService instance; | |
private HibernateTemplate hibernateTemplateMock; | |
@Before | |
public final void before(){ | |
this.instance = new FooService(); | |
this.instance.dao = new FooDAO(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 ); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 ); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void assertResponseCodeIs | |
( final HttpResponse response, final int expectedCode ){ | |
final int statusCode = httpResponse.getStatusLine().getStatusCode(); | |
assertEquals( expectedCode, statusCode ); | |
} |
OlderNewer