Skip to content

Commit

Permalink
modify url to not be public and add getter to retrieve it. add setter…
Browse files Browse the repository at this point in the history
… that sets the url internally and also on the wrapped BoundRequestBuilder
  • Loading branch information
rabeyta committed Jun 2, 2017
1 parent bc64185 commit cdfcfc6
Show file tree
Hide file tree
Showing 2 changed files with 203 additions and 4 deletions.
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);
}
}

0 comments on commit cdfcfc6

Please sign in to comment.