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

Fix to identify unsuccessful Http response codes #84

Merged
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
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.sashirestela</groupId>
<artifactId>cleverclient</artifactId>
<version>1.6.0</version>
<version>1.6.1</version>
<packaging>jar</packaging>

<name>cleverclient</name>
Expand Down Expand Up @@ -53,7 +53,7 @@
<!-- Dependencies Versions -->
<slf4j.version>2.0.16</slf4j.version>
<lombok.version>1.18.36</lombok.version>
<jackson.version>2.18.1</jackson.version>
<jackson.version>2.18.2</jackson.version>
<junit.version>5.11.3</junit.version>
<mockito.version>5.14.2</mockito.version>
<!-- Plugins Versions -->
Expand All @@ -65,7 +65,7 @@
<helper.version>3.6.0</helper.version>
<versions.version>2.18.0</versions.version>
<source.version>3.3.1</source.version>
<javadoc.version>3.11.1</javadoc.version>
<javadoc.version>3.11.2</javadoc.version>
<gpg.version>3.2.7</gpg.version>
<sonatype.version>1.7.0</sonatype.version>
<spotless.version>2.43.0</spotless.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import io.github.sashirestela.cleverclient.support.CleverClientException.HttpResponseInfo.HttpRequestInfo;
import io.github.sashirestela.cleverclient.support.ReturnType;
import io.github.sashirestela.cleverclient.util.CommonUtil;
import io.github.sashirestela.cleverclient.util.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
Expand Down Expand Up @@ -46,7 +46,8 @@ public abstract class HttpSender {
@SuppressWarnings("unchecked")
protected void throwExceptionIfErrorIsPresent(HttpResponse<?> response, Class<?> clazz) {
logger.debug("Response Code : {}", response.statusCode());
if (!CommonUtil.isInHundredsOf(response.statusCode(), HttpURLConnection.HTTP_OK)) {
if (CommonUtil.isBetweenHundredsOf(response.statusCode(), Constant.HTTP_CLIENT_ERROR_CODE,
Constant.HTTP_SERVER_ERROR_CODE)) {
var data = "";
if (Stream.class.equals(clazz)) {
data = ((Stream<String>) response.body())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ public static <T> T[] concatArrays(T[] array1, T[] array2) {
}

public static boolean isInHundredsOf(int value, int range) {
return Math.floor(value / 100.0) * 100 == range;
return value >= range && value < (range + 100);
}

public static boolean isBetweenHundredsOf(int value, int minRange, int maxRange) {
return value >= minRange && value < (maxRange + 100);
}

public static Map<String, String> createMapString(String... keyValPairs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ private Constant() {

public static final String HTTP_ERROR_MESSAGE = "HTTP interaction failed: server returned a {0} response status.";

public static final int HTTP_CLIENT_ERROR_CODE = 400;

public static final int HTTP_SERVER_ERROR_CODE = 500;

}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void shouldReturnJoinedArraysWhenTwoArePassed() {
}

@Test
void shouldDetectIfValueIsInTheRangeWhenTwoValuesAreCompared() {
void shouldDetectIfSomeValueIsInHundredsOfOneLimit() {
Object[][] testData = {
{ 199, 200, false },
{ 200, 200, true },
Expand All @@ -107,6 +107,22 @@ void shouldDetectIfValueIsInTheRangeWhenTwoValuesAreCompared() {
}
}

@Test
void shouldDetectIfSomeValueIsBetweenHundredsOfTwoLimits() {
Object[][] testData = {
{ 399, 400, 500, false },
{ 400, 400, 500, true },
{ 500, 400, 500, true },
{ 599, 400, 500, true },
{ 600, 400, 500, false }
};
for (var data : testData) {
var actualResult = CommonUtil.isBetweenHundredsOf((int) data[0], (int) data[1], (int) data[2]);
var expectedResult = (boolean) data[3];
assertEquals(expectedResult, actualResult);
}
}

@Test
void shouldCreateMapStringWhenAStringListIsPassed() {
var expectedMap = new HashMap<String, String>();
Expand Down
Loading