Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from gmaslowski/REMOVING_DRY_IN_HTTP_CLIENT
Removing DRYs in JenkinsHttpClient.
- Loading branch information
Showing
3 changed files
with
143 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/main/java/com/offbytwo/jenkins/client/validator/HttpResponseValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.offbytwo.jenkins.client.validator; | ||
|
||
import org.apache.http.HttpResponse; | ||
import org.apache.http.client.HttpResponseException; | ||
|
||
public class HttpResponseValidator { | ||
|
||
public void validateResponse(HttpResponse response) throws HttpResponseException { | ||
int status = response.getStatusLine().getStatusCode(); | ||
if (status < 200 || status >= 300) { | ||
throw new HttpResponseException(status, response.getStatusLine().getReasonPhrase()); | ||
} | ||
} | ||
|
||
} |
87 changes: 87 additions & 0 deletions
87
src/test/java/com/offbytwo/jenkins/client/validator/HttpRequestValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package com.offbytwo.jenkins.client.validator; | ||
|
||
import static junit.framework.TestCase.assertFalse; | ||
import static junit.framework.TestCase.assertTrue; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.Mockito.mock; | ||
|
||
import org.apache.http.HttpResponse; | ||
import org.apache.http.StatusLine; | ||
import org.apache.http.client.HttpResponseException; | ||
import org.junit.Test; | ||
|
||
public class HttpRequestValidatorTest { | ||
|
||
private boolean httpResponseExceptionThrown = false; | ||
private HttpResponse httpResponse; | ||
|
||
private HttpResponseValidator validator = new HttpResponseValidator(); | ||
|
||
@Test | ||
public void shouldThrowHttpResponseExceptionWhenStatusIsLowerThan200() { | ||
// given | ||
httpResponse = givenResponseWithCode(170); | ||
|
||
// when | ||
httpResponseExceptionThrown = validateResponse(httpResponse); | ||
|
||
// then | ||
assertTrue(httpResponseExceptionThrown); | ||
} | ||
|
||
@Test | ||
public void shouldThrowHttpResponseExceptionWhenStatusIsHigherThan300() { | ||
// given | ||
httpResponse = givenResponseWithCode(304); | ||
|
||
// when | ||
httpResponseExceptionThrown = validateResponse(httpResponse); | ||
|
||
// then | ||
assertTrue(httpResponseExceptionThrown); | ||
} | ||
|
||
@Test | ||
public void shouldThrowHttpResponseExceptionWhenStatusIs300() { | ||
// given | ||
httpResponse = givenResponseWithCode(300); | ||
|
||
// when | ||
httpResponseExceptionThrown = validateResponse(httpResponse); | ||
|
||
// then | ||
assertTrue(httpResponseExceptionThrown); | ||
} | ||
|
||
@Test | ||
public void shouldNotThrowHttpResponseExceptionWhenStatusIsBetween200and300() { | ||
// given | ||
httpResponse = givenResponseWithCode(220); | ||
|
||
// when | ||
httpResponseExceptionThrown = validateResponse(httpResponse); | ||
|
||
// then | ||
assertFalse(httpResponseExceptionThrown); | ||
} | ||
|
||
private HttpResponse givenResponseWithCode(Integer statusCode) { | ||
HttpResponse httpResponse = mock(HttpResponse.class); | ||
StatusLine statusLine = mock(StatusLine.class); | ||
|
||
given(httpResponse.getStatusLine()).willReturn(statusLine); | ||
given(statusLine.getStatusCode()).willReturn(statusCode); | ||
|
||
return httpResponse; | ||
} | ||
|
||
private boolean validateResponse(HttpResponse httpResponse) { | ||
boolean httpResponseExceptionThrown = false; | ||
try { | ||
validator.validateResponse(httpResponse); | ||
} catch (HttpResponseException e) { | ||
httpResponseExceptionThrown = true; | ||
} | ||
return httpResponseExceptionThrown; | ||
} | ||
} |