Skip to content

Commit

Permalink
Change properties names
Browse files Browse the repository at this point in the history
  • Loading branch information
Grzegorz Kołakowski committed Oct 15, 2024
1 parent 2589b95 commit 65f49b3
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ public JavaNetSinkHttpClient(
ComposeHttpStatusCodeCheckerConfig checkerConfig =
ComposeHttpStatusCodeCheckerConfig.builder()
.properties(properties)
.deprecatedErrorWhiteListPrefix(HTTP_ERROR_SINK_CODE_WHITE_LIST)
.deprecatedCodePrefix(HTTP_ERROR_SINK_CODES_LIST)
.errorWhiteListPrefix("") // TODO: sink not refactored yet
.errorCodePrefix("")
.retryableWhiteListPrefix("")
.retryableCodePrefix("")
.whiteListPrefix(HTTP_ERROR_SINK_CODE_WHITE_LIST)
.errorCodePrefix(HTTP_ERROR_SINK_CODES_LIST)
.nonRetryableErrorWhiteListPrefix("") // TODO: sink not refactored yet
.nonRetryableErrorCodePrefix("")
.retryableErrorWhiteListPrefix("")
.retryableErrorCodePrefix("")
.build();

this.statusCodeChecker = new ComposeHttpStatusCodeChecker(checkerConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,40 @@ public class ComposeHttpStatusCodeChecker implements HttpStatusCodeChecker {
private static final int MIN_HTTP_STATUS_CODE = 100;
private static final int MAX_HTTP_STATUS_CODE = 599;

private static final Predicate<Integer> DEFAULT_ERROR_CODES =
private static final Predicate<Integer> DEFAULT_NON_RETRYABLE_ERROR_CODES =
new TypeStatusCodeCheckerPredicate(HttpResponseCodeType.CLIENT_ERROR);
private static final Predicate<Integer> DEFAULT_RETRYABLE_ERROR_CODES =
new TypeStatusCodeCheckerPredicate(HttpResponseCodeType.SERVER_ERROR);
private static final Predicate<Integer> DEFAULT_DEPRECATED_ERROR_CODES =
DEFAULT_ERROR_CODES.or(DEFAULT_RETRYABLE_ERROR_CODES);
DEFAULT_NON_RETRYABLE_ERROR_CODES.or(DEFAULT_RETRYABLE_ERROR_CODES);

private final Predicate<Integer> retryableErrorStatusCodes;
private final Predicate<Integer> notRetryableErrorStatusCodes;
private final Predicate<Integer> nonRetryableErrorStatusCodes;

public ComposeHttpStatusCodeChecker(ComposeHttpStatusCodeCheckerConfig config) {
// Handle deprecated configuration for backward compatibility.
if (areDeprecatedPropertiesUsed(config)) {
notRetryableErrorStatusCodes = buildPredicate(config, config.getDeprecatedCodePrefix(),
config.getDeprecatedErrorWhiteListPrefix(), DEFAULT_DEPRECATED_ERROR_CODES);
nonRetryableErrorStatusCodes = buildPredicate(config, config.getErrorCodePrefix(),
config.getWhiteListPrefix(), DEFAULT_DEPRECATED_ERROR_CODES);
retryableErrorStatusCodes = integer -> false;
} else {
retryableErrorStatusCodes = buildPredicate(config, config.getRetryableCodePrefix(),
config.getRetryableWhiteListPrefix(), DEFAULT_RETRYABLE_ERROR_CODES);
notRetryableErrorStatusCodes = buildPredicate(config, config.getErrorCodePrefix(),
config.getErrorWhiteListPrefix(), DEFAULT_ERROR_CODES);
retryableErrorStatusCodes = buildPredicate(config, config.getRetryableErrorCodePrefix(),
config.getRetryableErrorWhiteListPrefix(), DEFAULT_RETRYABLE_ERROR_CODES);
nonRetryableErrorStatusCodes =
buildPredicate(config, config.getNonRetryableErrorCodePrefix(),
config.getNonRetryableErrorWhiteListPrefix(),
DEFAULT_NON_RETRYABLE_ERROR_CODES);
}
}

private boolean areDeprecatedPropertiesUsed(ComposeHttpStatusCodeCheckerConfig config) {
boolean whiteListDefined =
!isNullOrWhitespaceOnly(config.getDeprecatedErrorWhiteListPrefix());
boolean codeListDefined = !isNullOrWhitespaceOnly(config.getDeprecatedCodePrefix());
boolean whiteListDefined = !isNullOrWhitespaceOnly(config.getWhiteListPrefix());
boolean codeListDefined = !isNullOrWhitespaceOnly(config.getErrorCodePrefix());

return (whiteListDefined && !isNullOrWhitespaceOnly(
config.getProperties().getProperty(config.getDeprecatedErrorWhiteListPrefix())))
|| (codeListDefined && !isNullOrWhitespaceOnly(
config.getProperties().getProperty(config.getDeprecatedCodePrefix())));
config.getProperties().getProperty(config.getWhiteListPrefix()))) ||
(codeListDefined && !isNullOrWhitespaceOnly(
config.getProperties().getProperty(config.getErrorCodePrefix())));
}

private Predicate<Integer> buildPredicate(
Expand Down Expand Up @@ -143,8 +144,8 @@ public HttpResponseStatus checkStatus(int statusCode) {
MAX_HTTP_STATUS_CODE)
);

if (notRetryableErrorStatusCodes.test(statusCode)) {
return HttpResponseStatus.FAILURE_NOT_RETRYABLE;
if (nonRetryableErrorStatusCodes.test(statusCode)) {
return HttpResponseStatus.FAILURE_NON_RETRYABLE;
} else if (retryableErrorStatusCodes.test(statusCode)) {
return HttpResponseStatus.FAILURE_RETRYABLE;
} else {
Expand All @@ -157,17 +158,17 @@ public HttpResponseStatus checkStatus(int statusCode) {
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public static class ComposeHttpStatusCodeCheckerConfig {

private final String deprecatedErrorWhiteListPrefix;
private final String whiteListPrefix;

private final String deprecatedCodePrefix;
private final String errorCodePrefix;

private final String errorWhiteListPrefix;
private final String nonRetryableErrorWhiteListPrefix;

private final String errorCodePrefix;
private final String nonRetryableErrorCodePrefix;

private final String retryableWhiteListPrefix;
private final String retryableErrorWhiteListPrefix;

private final String retryableCodePrefix;
private final String retryableErrorCodePrefix;

private final Properties properties;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ public enum HttpResponseStatus {
/**
* Request failed but cannot be retried.
*/
FAILURE_NOT_RETRYABLE,
FAILURE_NON_RETRYABLE,
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ public JavaNetHttpPollingClient(
ComposeHttpStatusCodeCheckerConfig checkerConfig =
ComposeHttpStatusCodeCheckerConfig.builder()
.properties(options.getProperties())
.deprecatedErrorWhiteListPrefix(HTTP_ERROR_SOURCE_LOOKUP_CODE_WHITE_LIST)
.deprecatedCodePrefix(HTTP_ERROR_SOURCE_LOOKUP_CODES_LIST)
.errorWhiteListPrefix(HTTP_ERROR_NON_RETRYABLE_SOURCE_LOOKUP_CODE_WHITE_LIST)
.errorCodePrefix(HTTP_ERROR_NON_RETRYABLE_SOURCE_LOOKUP_CODES_LIST)
.retryableWhiteListPrefix(HTTP_ERROR_RETRYABLE_SOURCE_LOOKUP_CODE_WHITE_LIST)
.retryableCodePrefix(HTTP_ERROR_RETRYABLE_SOURCE_LOOKUP_CODES_LIST)
.whiteListPrefix(HTTP_ERROR_SOURCE_LOOKUP_CODE_WHITE_LIST)
.errorCodePrefix(HTTP_ERROR_SOURCE_LOOKUP_CODES_LIST)
.nonRetryableErrorWhiteListPrefix(
HTTP_ERROR_NON_RETRYABLE_SOURCE_LOOKUP_CODE_WHITE_LIST)
.nonRetryableErrorCodePrefix(HTTP_ERROR_NON_RETRYABLE_SOURCE_LOOKUP_CODES_LIST)
.retryableErrorWhiteListPrefix(HTTP_ERROR_RETRYABLE_SOURCE_LOOKUP_CODE_WHITE_LIST)
.retryableErrorCodePrefix(HTTP_ERROR_RETRYABLE_SOURCE_LOOKUP_CODES_LIST)
.build();

this.statusCodeChecker = new ComposeHttpStatusCodeChecker(checkerConfig);
Expand Down Expand Up @@ -157,7 +158,7 @@ private Optional<RowData> queryAndProcess(RowData lookupData) throws Exception {
);
Response parsedResponse = processHttpResponse(httpResponse, request);
if (parsedResponse.getStatus() == HttpResponseStatus.SUCCESS
|| parsedResponse.getStatus() == HttpResponseStatus.FAILURE_NOT_RETRYABLE) {
|| parsedResponse.getStatus() == HttpResponseStatus.FAILURE_NON_RETRYABLE) {
return parsedResponse.getRowData();
} else {
if (tryCount == maxRetryCount) {
Expand Down Expand Up @@ -197,9 +198,9 @@ private Response processHttpResponse(
if (httpResponseStatus == HttpResponseStatus.SUCCESS) {
log.trace("Returned successful status code [%s].");
return Response.success(responseBodyDecoder.deserialize(responseBody.getBytes()));
} else if (httpResponseStatus == HttpResponseStatus.FAILURE_NOT_RETRYABLE) {
} else if (httpResponseStatus == HttpResponseStatus.FAILURE_NON_RETRYABLE) {
log.warn(format("Returned not retryable error status code [%s].", statusCode));
return Response.notRetryable();
return Response.nonRetryable();
} else if (httpResponseStatus == HttpResponseStatus.FAILURE_RETRYABLE) {
log.warn(format("Returned retryable error status code [%s].", statusCode));
return Response.retryable();
Expand All @@ -220,8 +221,8 @@ static Response success(RowData rowData) {
return new Response(HttpResponseStatus.SUCCESS, rowData);
}

static Response notRetryable() {
return new Response(HttpResponseStatus.FAILURE_NOT_RETRYABLE, null);
static Response nonRetryable() {
return new Response(HttpResponseStatus.FAILURE_NON_RETRYABLE, null);
}

static Response retryable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.getindata.connectors.http.internal.status.ComposeHttpStatusCodeChecker.ComposeHttpStatusCodeCheckerConfig;
import static com.getindata.connectors.http.internal.status.HttpResponseStatus.FAILURE_NOT_RETRYABLE;
import static com.getindata.connectors.http.internal.status.HttpResponseStatus.FAILURE_NON_RETRYABLE;
import static com.getindata.connectors.http.internal.status.HttpResponseStatus.FAILURE_RETRYABLE;
import static com.getindata.connectors.http.internal.status.HttpResponseStatus.SUCCESS;

Expand All @@ -33,8 +33,8 @@ void shouldReturnAppropriateStatusByDefault() {
assertThat(codeChecker.checkStatus(100)).isEqualTo(SUCCESS);
assertThat(codeChecker.checkStatus(200)).isEqualTo(SUCCESS);
assertThat(codeChecker.checkStatus(302)).isEqualTo(SUCCESS);
assertThat(codeChecker.checkStatus(400)).isEqualTo(FAILURE_NOT_RETRYABLE);
assertThat(codeChecker.checkStatus(404)).isEqualTo(FAILURE_NOT_RETRYABLE);
assertThat(codeChecker.checkStatus(400)).isEqualTo(FAILURE_NON_RETRYABLE);
assertThat(codeChecker.checkStatus(404)).isEqualTo(FAILURE_NON_RETRYABLE);
assertThat(codeChecker.checkStatus(500)).isEqualTo(FAILURE_RETRYABLE);
assertThat(codeChecker.checkStatus(501)).isEqualTo(FAILURE_RETRYABLE);
assertThat(codeChecker.checkStatus(503)).isEqualTo(FAILURE_RETRYABLE);
Expand All @@ -54,14 +54,14 @@ void shouldReturnAppropriateStatus() {
HttpStatusCodeChecker codeChecker = new ComposeHttpStatusCodeChecker(checkerConfig);

assertAll(() -> {
assertThat(codeChecker.checkStatus(100)).isEqualTo(FAILURE_NOT_RETRYABLE);
assertThat(codeChecker.checkStatus(100)).isEqualTo(FAILURE_NON_RETRYABLE);
assertThat(codeChecker.checkStatus(200)).isEqualTo(SUCCESS);
assertThat(codeChecker.checkStatus(400)).isEqualTo(FAILURE_NOT_RETRYABLE);
assertThat(codeChecker.checkStatus(400)).isEqualTo(FAILURE_NON_RETRYABLE);
assertThat(codeChecker.checkStatus(404)).isEqualTo(FAILURE_RETRYABLE);
assertThat(codeChecker.checkStatus(500)).isEqualTo(FAILURE_RETRYABLE);
assertThat(codeChecker.checkStatus(501)).isEqualTo(SUCCESS);
assertThat(codeChecker.checkStatus(503)).isEqualTo(FAILURE_RETRYABLE);
assertThat(codeChecker.checkStatus(505)).isEqualTo(FAILURE_NOT_RETRYABLE);
assertThat(codeChecker.checkStatus(505)).isEqualTo(FAILURE_NON_RETRYABLE);
});
}

Expand All @@ -87,13 +87,13 @@ void shouldParseWhiteList() {
.withFailMessage(
"Not on a white list but matches 3XX range. "
+ "Should be considered as error code.")
.isEqualTo(FAILURE_NOT_RETRYABLE);
.isEqualTo(FAILURE_NON_RETRYABLE);
});
}

@Test
void shouldParseErrorCodeList() {
List<Integer> notRetryableCodes = List.of(100, 202, 404);
List<Integer> nonRetryableCodes = List.of(100, 202, 404);
List<Integer> retryableCodes = List.of(302, 502);
Properties properties = new Properties();
properties.setProperty(NOT_RETRYABLE_CODE_PROPERTY, "100, 202, 404");
Expand All @@ -103,8 +103,8 @@ void shouldParseErrorCodeList() {
HttpStatusCodeChecker codeChecker = new ComposeHttpStatusCodeChecker(checkerConfig);

assertAll(() -> {
notRetryableCodes.forEach(code -> assertThat(codeChecker.checkStatus(code))
.isEqualTo(FAILURE_NOT_RETRYABLE));
nonRetryableCodes.forEach(code -> assertThat(codeChecker.checkStatus(code))
.isEqualTo(FAILURE_NON_RETRYABLE));
retryableCodes.forEach(code -> assertThat(codeChecker.checkStatus(code))
.isEqualTo(FAILURE_RETRYABLE));
});
Expand All @@ -115,15 +115,15 @@ void shouldParseErrorCodeRange() {
Properties properties = new Properties();
properties.setProperty(NOT_RETRYABLE_CODE_PROPERTY, "1XX, 2XX");
properties.setProperty(RETRYABLE_CODE_PROPERTY, "3XX, 4XX");
List<Integer> notRetryableCodes = List.of(100, 110, 200, 220);
List<Integer> nonRetryableCodes = List.of(100, 110, 200, 220);
List<Integer> retryableCodes = List.of(301, 404);

ComposeHttpStatusCodeCheckerConfig checkerConfig = prepareCheckerConfig(properties);
HttpStatusCodeChecker codeChecker = new ComposeHttpStatusCodeChecker(checkerConfig);

assertAll(() -> {
notRetryableCodes.forEach(code -> assertThat(codeChecker.checkStatus(code))
.isEqualTo(FAILURE_NOT_RETRYABLE));
nonRetryableCodes.forEach(code -> assertThat(codeChecker.checkStatus(code))
.isEqualTo(FAILURE_NON_RETRYABLE));
retryableCodes.forEach(code -> assertThat(codeChecker.checkStatus(code))
.isEqualTo(FAILURE_RETRYABLE));
assertThat(codeChecker.checkStatus(503))
Expand All @@ -138,15 +138,15 @@ void shouldIgnoreRedundantWhiteSpacesOrEmptyOrRepeatedValues() {
Properties properties = new Properties();
properties.setProperty(NOT_RETRYABLE_CODE_PROPERTY, " , 100,200, 300, , 303 ,200 ");
properties.setProperty(RETRYABLE_CODE_PROPERTY, ",5XX, 4XX,, ,");
List<Integer> notRetryableCodes = List.of(100, 200, 300, 303);
List<Integer> nonRetryableCodes = List.of(100, 200, 300, 303);
List<Integer> retryableCodes = List.of(500, 501, 400, 401);

ComposeHttpStatusCodeCheckerConfig checkerConfig = prepareCheckerConfig(properties);
HttpStatusCodeChecker codeChecker = new ComposeHttpStatusCodeChecker(checkerConfig);

assertAll(() -> {
notRetryableCodes.forEach(code -> assertThat(codeChecker.checkStatus(code))
.isEqualTo(FAILURE_NOT_RETRYABLE));
nonRetryableCodes.forEach(code -> assertThat(codeChecker.checkStatus(code))
.isEqualTo(FAILURE_NON_RETRYABLE));
retryableCodes.forEach(code -> assertThat(codeChecker.checkStatus(code))
.isEqualTo(FAILURE_RETRYABLE));
});
Expand Down Expand Up @@ -184,10 +184,10 @@ void shouldThrowOnInvalidCodeRangeInRetryableError(String listCode) {
private ComposeHttpStatusCodeCheckerConfig prepareCheckerConfig(Properties properties) {
return ComposeHttpStatusCodeCheckerConfig.builder()
.properties(properties)
.errorCodePrefix(NOT_RETRYABLE_CODE_PROPERTY)
.errorWhiteListPrefix(NOT_RETRYABLE_WHITELIST_PROPERTY)
.retryableCodePrefix(RETRYABLE_CODE_PROPERTY)
.retryableWhiteListPrefix(RETRYABLE_WHITELIST_PROPERTY)
.nonRetryableErrorCodePrefix(NOT_RETRYABLE_CODE_PROPERTY)
.nonRetryableErrorWhiteListPrefix(NOT_RETRYABLE_WHITELIST_PROPERTY)
.retryableErrorCodePrefix(RETRYABLE_CODE_PROPERTY)
.retryableErrorWhiteListPrefix(RETRYABLE_WHITELIST_PROPERTY)
.build();
}
}

0 comments on commit 65f49b3

Please sign in to comment.