Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
[#13] Fixed existing broken tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nurkiewicz committed Dec 2, 2014
1 parent 6c2d5f6 commit 072a36c
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ abstract class LocationFindingExecutor implements LocationReceiving {
LocationFindingExecutor(RestOperations restOperations, RetryExecutor retryExecutor) {
this.restOperations = restOperations
this.retryExecutor = retryExecutor
this.restExecutor = new RestExecutor<>(restOperations, retryExecutor, Object)
this.restExecutor = new RestExecutor<>(restOperations, retryExecutor)
}

protected abstract HttpMethod getHttpMethod()

@Override
URI forLocation() {
return getLocation(restExecutor.exchange(httpMethod, params))
return getLocation(restExecutor.exchange(httpMethod, params, params.request.class))
}

@Override
ListenableFuture<URI> forLocationAsync() {
ListenableFuture<ResponseEntity> future = restExecutor.exchangeAsync(httpMethod, params)
ListenableFuture<ResponseEntity> future = restExecutor.exchangeAsync(httpMethod, params, params.request.class)
return Futures.transform(future, {ResponseEntity entity -> getLocation(entity)} as Function)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ abstract class ResponseTypeRelatedRequestsExecutor<T> {

protected final RestExecutor<T> restExecutor
protected final Map params
private final Class<T> responseType

ResponseTypeRelatedRequestsExecutor(Map params, RestOperations restOperations, RetryExecutor retryExecutor, Class<T> responseType) {
this.params = params
this.restExecutor = new RestExecutor(restOperations, retryExecutor, responseType)
this.responseType = responseType
this.restExecutor = new RestExecutor(restOperations, retryExecutor)
}

protected abstract SpringHttpMethod getHttpMethod()
Expand All @@ -40,7 +42,7 @@ abstract class ResponseTypeRelatedRequestsExecutor<T> {
}

ListenableFuture<ResponseEntity<T>> exchangeAsync() {
return restExecutor.exchangeAsync(httpMethod, params)
return restExecutor.exchangeAsync(httpMethod, params, responseType)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,26 @@ import static com.ofg.infrastructure.web.resttemplate.fluent.common.response.exe
final class RestExecutor<T> {
private final RestOperations restOperations
private final RetryExecutor retryExecutor
private final Class<T> responseType

RestExecutor(RestOperations restOperations, RetryExecutor retryExecutor, Class<T> responseType) {
RestExecutor(RestOperations restOperations, RetryExecutor retryExecutor) {
this.restOperations = restOperations
this.retryExecutor = retryExecutor
this.responseType = responseType
}

ResponseEntity<T> exchange(HttpMethod httpMethod, Map params) {
return exchangeAsync(httpMethod, params).get()
ResponseEntity<T> exchange(HttpMethod httpMethod, Map params, Class<T> responseType) {
return exchangeAsync(httpMethod, params, responseType).get()
}

ListenableFuture<ResponseEntity<T>> exchangeAsync(HttpMethod httpMethod, Map params) {
ListenableFuture<ResponseEntity<T>> exchangeAsync(HttpMethod httpMethod, Map params, Class<T> responseType) {
if (params.url) {
return callUrlWithRetry(httpMethod, params)
return callUrlWithRetry(httpMethod, params, responseType)
} else if (params.urlTemplate) {
return callUrlTemplateWithRetry(httpMethod, params)
return callUrlTemplateWithRetry(httpMethod, params, responseType)
}
throw new InvalidHttpMethodParametersException(params)
}

protected ListenableFuture<ResponseEntity<T>> callUrlTemplateWithRetry(HttpMethod httpMethod, Map params) {
protected ListenableFuture<ResponseEntity<T>> callUrlTemplateWithRetry(HttpMethod httpMethod, Map params, Class<T> responseType) {
return retryExecutor.getWithRetry {
//TODO Correlation ID
return restOperations.exchange(
Expand All @@ -52,7 +50,7 @@ final class RestExecutor<T> {
}
}

protected ListenableFuture<ResponseEntity<T>> callUrlWithRetry(HttpMethod httpMethod, Map params) {
protected ListenableFuture<ResponseEntity<T>> callUrlWithRetry(HttpMethod httpMethod, Map params, Class<T> responseType) {
return retryExecutor.getWithRetry {
//TODO Correlation ID
restOperations.exchange(
Expand All @@ -63,7 +61,7 @@ final class RestExecutor<T> {
}
}

private HttpEntity<Object> getHttpEntityFrom(Map params) {
static HttpEntity<Object> getHttpEntityFrom(Map params) {
if (params.httpEntity) {
return params.httpEntity as HttpEntity
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ class OptionsAllowHeaderExecutor implements AllowHeaderReceiving {

OptionsAllowHeaderExecutor(RestOperations restOperations, RetryExecutor retryExecutor, Map params) {
this.params = params
this.restExecutor = new RestExecutor<>(restOperations, retryExecutor, Object)
this.restExecutor = new RestExecutor<>(restOperations, retryExecutor)
}

@Override
ListenableFuture<HttpMethod> allowAsync() {
ListenableFuture<ResponseEntity> future = restExecutor.exchangeAsync(OPTIONS, params)
ListenableFuture<ResponseEntity> future = restExecutor.exchangeAsync(OPTIONS, params, Object)
return Futures.transform(future, {ResponseEntity entity -> extractAllow(entity)} as Function)
}

@Override
Set<HttpMethod> allow() {
return extractAllow(restExecutor.exchange(OPTIONS, params))
return extractAllow(restExecutor.exchange(OPTIONS, params, Object))
}

private Set<HttpMethod> extractAllow(ResponseEntity entity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class OptionsMethodBuilder implements
this.restOperations = restOperations
params.host = host
withHeaders = new AllowContainingWithHeaders(this, params, predefinedHeaders)
allowHeaderExecutor = new OptionsAllowHeaderExecutor(restOperations, null, params)
allowHeaderExecutor = new OptionsAllowHeaderExecutor(restOperations, retryExecutor, params)
this.retryExecutor = retryExecutor
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ class HttpEntityUtilsSpec extends Specification {

public static final int UNKOWN_HEADER_VALUE = -1

def 'should fail to instantiate a utility class'() {
when:
RestExecutor.newInstance()
then:
thrown(UnsupportedOperationException)
}

def 'should create HttpEntity from a filled argument map'() {
given:
long expectedExpires = 1000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import static org.springframework.http.HttpStatus.OK

class OptionsHttpMethodBuilderSpec extends HttpMethodSpec {

public static final String RESPONSE_BODY = '''{"sample":"response"}'''
public static final Class<String> RESPONSE_TYPE = String

def "should use only url template without provided service url to retrieve an object"() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class PutHttpMethodBuilderSpec extends HttpMethodSpec {
.body(REQUEST_BODY)
.forLocation()
then:
1 * restOperations.exchange(expectedLocation,
PUT,
{ HttpEntity httpEntity -> httpEntity.body == REQUEST_BODY } as HttpEntity,
1 * restOperations.exchange(expectedLocation,
PUT,
{ HttpEntity httpEntity -> httpEntity.body == REQUEST_BODY } as HttpEntity,
RESPONSE_TYPE) >> responseEntityWith(expectedLocation)
actualLocation == expectedLocation
}
Expand Down

0 comments on commit 072a36c

Please sign in to comment.