Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide Method to update the URL in a RequestBuilderWrapper and maintain modifications to the BoundRequestBuilder #57

Merged
merged 1 commit into from
Jun 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
@SuppressWarnings({"WeakerAccess", "OptionalUsedAsFieldOrParameterType"})
public class RequestBuilderWrapper {

public final String url;
public final String httpMethod;
String url;
String httpMethod;
public final AsyncHttpClient.BoundRequestBuilder requestBuilder;
/**
* An Optional containing a custom circuit breaker if a custom one should be used, or empty if the request sender
Expand All @@ -34,12 +34,12 @@ public class RequestBuilderWrapper {
* (i.e. all calls to the same host will use the same circuit breaker). If you need something more (or less) fine
* grained than that then you'll need to provide a custom circuit breaker.
*/
public final Optional<CircuitBreaker<Response>> customCircuitBreaker;
Optional<CircuitBreaker<Response>> customCircuitBreaker;
/**
* Set this to true if you don't want *any* circuit breaker to be used - if this is false then {@link
* #customCircuitBreaker} will be used to determine which circuit breaker to use (custom vs. default).
*/
public final boolean disableCircuitBreaker;
boolean disableCircuitBreaker;

private ChannelHandlerContext ctx;

Expand All @@ -66,4 +66,53 @@ ChannelHandlerContext getCtx() {
void setCtx(ChannelHandlerContext ctx) {
this.ctx = ctx;
}

public void setCustomCircuitBreaker(Optional<CircuitBreaker<Response>> customCircuitBreaker) {
this.customCircuitBreaker = customCircuitBreaker;
}

public Optional<CircuitBreaker<Response>> getCustomCircuitBreaker() {
return customCircuitBreaker;
}

public void setDisableCircuitBreaker(boolean disableCircuitBreaker) {
this.disableCircuitBreaker = disableCircuitBreaker;
}

public boolean isDisableCircuitBreaker() {
return disableCircuitBreaker;
}

/**
* <p>Use this method to update the url stored inside this {@link RequestBuilderWrapper}
* and the wrapped {@link AsyncHttpClient.BoundRequestBuilder}
*
* <p>Setting the url only on the wrapped {@link AsyncHttpClient.BoundRequestBuilder} will impact logging
* and circuit breakers potentially. Use this method to keep the two in sync.
*/
public void setUrl(String url) {
this.url = url;
requestBuilder.setUrl(url);
}

public String getUrl() {
return url;
}

/**
* <p>Use this method to update the httpMethod stored inside this {@link RequestBuilderWrapper}
* and the wrapped {@link AsyncHttpClient.BoundRequestBuilder}
*
* <p>Setting the httpMethod only on the wrapped {@link AsyncHttpClient.BoundRequestBuilder} will impact logging
* and circuit breakers potentially. Use this method to keep the two in sync.
*/
public void setHttpMethod(String httpMethod) {
this.httpMethod = httpMethod;
requestBuilder.setMethod(httpMethod);
}

public String getHttpMethod() {
return httpMethod;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package com.nike.riposte.client.asynchttp.ning;

import com.nike.fastbreak.CircuitBreaker;
import com.nike.fastbreak.CircuitBreakerImpl;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.Response;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpMethod;
import org.junit.Before;
import org.junit.Test;

import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;


public class RequestBuilderWrapperTest {

private RequestBuilderWrapper requestBuilderWrapper;
private String url;
private String httpMethod;
private AsyncHttpClient.BoundRequestBuilder requestBuilder;
private Optional<CircuitBreaker<Response>> customCircuitBreaker;
private boolean disableCircuitBreaker;

@Before
public void setup() {
url = "http://localhost.com";
httpMethod = HttpMethod.GET.name();
requestBuilder = mock(AsyncHttpClient.BoundRequestBuilder.class);
customCircuitBreaker = Optional.of(new CircuitBreakerImpl<Response>());
disableCircuitBreaker = true;
}

@Test
public void constructor_sets_values_as_expected() {
// given
requestBuilderWrapper = new RequestBuilderWrapper(
url,
httpMethod,
requestBuilder,
customCircuitBreaker,
disableCircuitBreaker);

// then
assertThat(requestBuilderWrapper.url).isEqualTo(url);
assertThat(requestBuilderWrapper.httpMethod).isEqualTo(httpMethod);
assertThat(requestBuilderWrapper.customCircuitBreaker).isEqualTo(customCircuitBreaker);
assertThat(requestBuilderWrapper.disableCircuitBreaker).isEqualTo(disableCircuitBreaker);
}

@Test
public void get_set_ChannelHandlerContext_works_as_expected() {
// given
requestBuilderWrapper = new RequestBuilderWrapper(
url,
httpMethod,
requestBuilder,
customCircuitBreaker,
disableCircuitBreaker);

ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);

// when
requestBuilderWrapper.setCtx(ctx);

// then
assertThat(ctx).isEqualTo(requestBuilderWrapper.getCtx());
}

@Test
public void get_set_Url_works_as_expected() {
// given
requestBuilderWrapper = new RequestBuilderWrapper(
url,
httpMethod,
requestBuilder,
customCircuitBreaker,
disableCircuitBreaker);

String alternateUrl = "http://alteredUrl.testing";

// when
requestBuilderWrapper.setUrl(alternateUrl);

// then
assertThat(alternateUrl).isEqualTo(requestBuilderWrapper.getUrl());
verify(requestBuilder).setUrl(alternateUrl);
}

@Test
public void get_set_HttpMethod_works_as_expected() {
// given
requestBuilderWrapper = new RequestBuilderWrapper(
url,
httpMethod,
requestBuilder,
customCircuitBreaker,
disableCircuitBreaker);

String alteredMethod = "POST";

// when
requestBuilderWrapper.setHttpMethod(alteredMethod);

// then
assertThat(alteredMethod).isEqualTo(requestBuilderWrapper.getHttpMethod());
verify(requestBuilder).setMethod(alteredMethod);
}

@Test
public void get_set_DisableCircuitBreaker_works_as_expected() {
// given
requestBuilderWrapper = new RequestBuilderWrapper(
url,
httpMethod,
requestBuilder,
customCircuitBreaker,
disableCircuitBreaker);

boolean alteredDisableCircuitBreaker = false;

// when
requestBuilderWrapper.setDisableCircuitBreaker(alteredDisableCircuitBreaker);

// then
assertThat(requestBuilderWrapper.isDisableCircuitBreaker()).isFalse();
}

@Test
public void get_set_CustomCircuitBreaker_works_as_expected() {
// given
requestBuilderWrapper = new RequestBuilderWrapper(
url,
httpMethod,
requestBuilder,
customCircuitBreaker,
disableCircuitBreaker);

Optional<CircuitBreaker<Response>> alteredCircuitBreaker = Optional.of(new CircuitBreakerImpl<>());

// when
requestBuilderWrapper.setCustomCircuitBreaker(alteredCircuitBreaker);

// then
assertThat(requestBuilderWrapper.getCustomCircuitBreaker()).isEqualTo(alteredCircuitBreaker);
}
}