Skip to content

Commit

Permalink
Merge pull request #81 from corese4rch/javadoc-for-request-configurer
Browse files Browse the repository at this point in the history
Add javadocs for RequestConfigurer, make RequestBuilder implement Req…
  • Loading branch information
yrkkap committed Oct 31, 2019
2 parents 57a07d8 + 6e3e8c4 commit 13dfa64
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public Builder acceptCompressed(boolean acceptCompressed) {
}

@Override
public Builder logEnabled(boolean logEnable) {
this.logEnabled = logEnable;
public Builder logEnabled(boolean logEnabled) {
this.logEnabled = logEnabled;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,34 @@

import java.time.Duration;

/**
* Interface that defines protocol for {@link RequestConfiguration} building.
*
* @param <T>
*/
public interface RequestConfigurer<T> {

/**
* Sets requestTimeout of request.
*
* @param duration request timeout
* @return this builder
*/
T requestTimeout(Duration duration);

/**
* Sets whether client should accept compressed response body.
*
* @param acceptCompressed whether accept compressed
* @return this builder
*/
T acceptCompressed(boolean acceptCompressed);

T logEnabled(boolean logEnable);
/**
* Sets flag that defines if request body and url should be logged with level INFO.
*
* @param logEnabled whether accept compressed
* @return this builder
*/
T logEnabled(boolean logEnabled);
}
15 changes: 12 additions & 3 deletions src/main/java/coresearch/cvurl/io/model/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ public static ConfigurationBuilder builder(HttpClient httpClient) {
return new ConfigurationBuilder(httpClient);
}

/**
* Creates {@link ConfigurationBuilder} with default {@link HttpClient} used to build {@link Configuration} object.
*
* @return new ConfigurationWithClientPropertiesBuilder
*/
public static ConfigurationBuilder builderWithDefaultHttpClient() {
return new ConfigurationBuilder(HttpClient.newHttpClient());
}

/**
* Creates configuration with default properties.
*
Expand Down Expand Up @@ -216,12 +225,12 @@ public ConfigurationBuilder acceptCompressed(boolean acceptCompressed) {
/**
* Sets a feature flag that defines if we logging every request url and body with level INFO or not.
*
* @param logEnable flag
* @param logEnabled flag
* @return this {@link Configuration}
*/
@Override
public ConfigurationBuilder logEnabled(boolean logEnable) {
this.requestConfigurationBuilder.logEnabled(logEnable);
public ConfigurationBuilder logEnabled(boolean logEnabled) {
this.requestConfigurationBuilder.logEnabled(logEnabled);
return this;
}

Expand Down
31 changes: 30 additions & 1 deletion src/main/java/coresearch/cvurl/io/request/RequestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import coresearch.cvurl.io.constant.HttpContentEncoding;
import coresearch.cvurl.io.constant.HttpHeader;
import coresearch.cvurl.io.constant.HttpMethod;
import coresearch.cvurl.io.internal.configuration.RequestConfigurer;
import coresearch.cvurl.io.mapper.BodyType;
import coresearch.cvurl.io.model.Configuration;
import coresearch.cvurl.io.internal.configuration.RequestConfiguration;
Expand All @@ -28,7 +29,7 @@
*
* @param <T>
*/
public class RequestBuilder<T extends RequestBuilder<T>> implements Request {
public class RequestBuilder<T extends RequestBuilder<T>> implements Request, RequestConfigurer<RequestBuilder> {

protected final Configuration configuration;
protected final RequestConfiguration.Builder requestConfigurationBuilder;
Expand Down Expand Up @@ -102,19 +103,47 @@ public T queryParams(Map<String, String> queryParams) {
*
* @param timeout request timeout
* @return this builder
* @deprecated Use {@link #requestTimeout(Duration)} instead
*/
@SuppressWarnings("unchecked")
@Deprecated(since = "1.2", forRemoval = true)
public T timeout(Duration timeout) {
this.requestConfigurationBuilder.requestTimeout(timeout);
return (T) this;
}

/**
* Sets whether this client should accept compressed response body.
*
* @return this builder
*/
@SuppressWarnings("unchecked")
public T acceptCompressed() {
this.requestConfigurationBuilder.acceptCompressed(true);
return (T) this;
}

@SuppressWarnings("unchecked")
@Override
public T requestTimeout(Duration timeout) {
this.requestConfigurationBuilder.requestTimeout(timeout);
return (T) this;
}

@SuppressWarnings("unchecked")
@Override
public T acceptCompressed(boolean acceptCompressed) {
this.requestConfigurationBuilder.acceptCompressed(acceptCompressed);
return (T) this;
}

@SuppressWarnings("unchecked")
@Override
public T logEnabled(boolean logEnabled) {
this.requestConfigurationBuilder.logEnabled(logEnabled);
return (T) this;
}

/**
* Builds new {@link Request}.
*
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/coresearch/cvurl/io/request/CVurlRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import coresearch.cvurl.io.exception.UnexpectedResponseException;
import coresearch.cvurl.io.helper.ObjectGenerator;
import coresearch.cvurl.io.helper.model.User;
import coresearch.cvurl.io.internal.configuration.RequestConfiguration;
import coresearch.cvurl.io.mapper.BodyType;
import coresearch.cvurl.io.model.Configuration;
import coresearch.cvurl.io.model.Response;
Expand All @@ -25,6 +26,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.http.HttpClient;
Expand Down Expand Up @@ -868,11 +870,49 @@ public void asObjectWithGenericBodyTypeOnUnparseableBodyShouldThrowResponseMappi
assertEquals(body, responseMappingException.getResponse().getBody());
}

@Test
public void overwritingGlobalRequestConfigurationTest() {
//given
var cVurl = new CVurl(Configuration.builderWithDefaultHttpClient()
.requestTimeout(Duration.ofSeconds(1))
.acceptCompressed(false)
.logEnabled(false)
.build());

var timeout = Duration.ofSeconds(15);
var acceptCompressed = true;
var logEnabled = true;

//when
Request request = cvurl.get(url)
.requestTimeout(timeout)
.acceptCompressed(acceptCompressed)
.logEnabled(logEnabled)
.create();

//then
var requestConfiguration = getRequestConfiguration((CVurlRequest) request);
assertEquals(requestConfiguration.getRequestTimeout()
.orElseThrow(() -> new IllegalStateException("No request timeout, it should be set")), timeout);
assertEquals(requestConfiguration.isAcceptCompressed(), acceptCompressed);
assertEquals(requestConfiguration.isLogEnabled(), logEnabled);
}

private byte[] compressWithGZIP(String str) throws IOException {
var out = new ByteArrayOutputStream();
try (var gzipOutputStream = new GZIPOutputStream(out)) {
gzipOutputStream.write(str.getBytes());
}
return out.toByteArray();
}

private RequestConfiguration getRequestConfiguration(CVurlRequest request) {
try {
Field requestConfigurationField = request.getClass().getDeclaredField("requestConfiguration");
requestConfigurationField.setAccessible(true);
return (RequestConfiguration) requestConfigurationField.get(request);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}

0 comments on commit 13dfa64

Please sign in to comment.