Skip to content

Commit e1ceff8

Browse files
committed
Adding handle to multiple MatchStatus.
1 parent be41814 commit e1ceff8

File tree

4 files changed

+90
-15
lines changed

4 files changed

+90
-15
lines changed

src/main/java/com/amihaiemil/docker/MatchStatus.java

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@
2525
*/
2626
package com.amihaiemil.docker;
2727

28-
import java.net.URI;
2928
import org.apache.http.HttpResponse;
3029
import org.apache.http.client.ResponseHandler;
3130

31+
import java.net.URI;
32+
import java.util.Arrays;
33+
import java.util.List;
34+
import java.util.stream.Collectors;
35+
3236
/**
3337
* An Apache ResponseHandler that tries to match the Response's status code
3438
* with the expected one.
@@ -42,32 +46,54 @@ final class MatchStatus implements ResponseHandler<HttpResponse> {
4246
* Called URI.
4347
*/
4448
private final URI called;
45-
49+
4650
/**
4751
* Expected status.
4852
*/
49-
private final int expected;
50-
53+
private final List<Integer> expected;
54+
5155
/**
5256
* Ctor.
5357
* @param called Called URI.
5458
* @param expected Expected Http status code.
5559
*/
5660
MatchStatus(final URI called, final int expected) {
61+
this(called, Arrays.asList(expected));
62+
}
63+
64+
/**
65+
* Ctor.
66+
* @param called Called URI.
67+
* @param expected Expected a iterable Http status code.
68+
*/
69+
MatchStatus(final URI called, final Integer... expected) {
70+
this(called, Arrays.asList(expected));
71+
}
72+
73+
/**
74+
* Primary Ctor.
75+
* @param called Called URI.
76+
* @param expected Expected a iterable Http status code.
77+
*/
78+
MatchStatus(final URI called, final List<Integer> expected) {
5779
this.called = called;
5880
this.expected = expected;
5981
}
60-
82+
6183
@Override
6284
public HttpResponse handleResponse(final HttpResponse response) {
6385
final int actual = response.getStatusLine().getStatusCode();
64-
if(actual != this.expected) {
65-
throw new UnexpectedResponseException(
66-
this.called.toString(), actual,
67-
this.expected, new PayloadOf(response)
68-
);
86+
for(final Integer statusCode: this.expected){
87+
if(statusCode == actual) {
88+
return response;
89+
}
6990
}
70-
return response;
91+
String codes = this.expected.stream()
92+
.map(Object::toString)
93+
.collect(Collectors.joining(" "));
94+
throw new UnexpectedResponseException(
95+
this.called.toString(), actual,
96+
codes, new PayloadOf(response)
97+
);
7198
}
72-
7399
}

src/main/java/com/amihaiemil/docker/UnexpectedResponseException.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public final class UnexpectedResponseException extends RuntimeException {
5050
/**
5151
* Expected response status.
5252
*/
53-
private final int expectedStatus;
53+
private final String expectedStatus;
5454

5555
/**
5656
* The response's body.
@@ -68,6 +68,21 @@ public final class UnexpectedResponseException extends RuntimeException {
6868
public UnexpectedResponseException(
6969
final String endpoint, final int actualStatus,
7070
final int expectedStatus, final JsonObject body
71+
) {
72+
this(endpoint, actualStatus, String.valueOf(expectedStatus), body);
73+
}
74+
75+
/**
76+
* Primary Ctor.
77+
* @param endpoint Endpoint that was called.
78+
* @param actualStatus Received status.
79+
* @param expectedStatus Expected status.
80+
* @param body The response's body.
81+
*/
82+
// @checkstyle ParameterNumber (3 lines)
83+
public UnexpectedResponseException(
84+
final String endpoint, final int actualStatus,
85+
final String expectedStatus, final JsonObject body
7186
) {
7287
super(String.format(
7388
// @checkstyle LineLength (1 line)
@@ -100,7 +115,7 @@ public int actualStatus() {
100115
* Expected status.
101116
* @return Integer HTTP status.
102117
*/
103-
public int expectedStatus() {
118+
public String expectedStatus() {
104119
return this.expectedStatus;
105120
}
106121

src/test/java/com/amihaiemil/docker/MatchStatusTestCase.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,21 @@ public void returnsResponseOnMatch() throws Exception {
6666
Matchers.is(resp)
6767
);
6868
}
69+
70+
/**
71+
* {@link MatchStatus} returns the HttpResponse if one of the status
72+
* code match.
73+
* @throws Exception If something goes wrong.
74+
*/
75+
@Test
76+
public void returnsResponseOnMatchOne() throws Exception {
77+
final HttpResponse resp = new Response(HttpStatus.SC_NO_CONTENT);
78+
MatcherAssert.assertThat(
79+
new MatchStatus(URI.create("/test/ur"),
80+
HttpStatus.SC_OK, HttpStatus.SC_NO_CONTENT)
81+
.handleResponse(resp),
82+
Matchers.is(resp)
83+
);
84+
}
6985

7086
}

src/test/java/com/amihaiemil/docker/UnexpectedResponseExceptionTestCase.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void returnsExpectedStatus() {
6464
"/uri", HttpStatus.SC_NOT_FOUND,
6565
HttpStatus.SC_OK, Json.createObjectBuilder().build()
6666
).expectedStatus(),
67-
Matchers.equalTo(HttpStatus.SC_OK)
67+
Matchers.equalTo(String.valueOf(HttpStatus.SC_OK))
6868
);
6969
}
7070

@@ -99,6 +99,24 @@ public void returnsMessage() {
9999
);
100100
}
101101

102+
/**
103+
* UnexpectedResponseException has a proper message.
104+
*/
105+
@Test
106+
public void returnsMessages() {
107+
MatcherAssert.assertThat(
108+
new UnexpectedResponseException(
109+
"/uri", HttpStatus.SC_NOT_FOUND,
110+
String.valueOf(HttpStatus.SC_OK),
111+
Json.createObjectBuilder().build()
112+
).getMessage(),
113+
Matchers.startsWith(
114+
// @checkstyle LineLength (1 line)
115+
"Expected status 200 but got 404 when calling /uri. Response body was"
116+
)
117+
);
118+
}
119+
102120
/**
103121
* UnexpectedResponseException appends the payload to the message.
104122
*/

0 commit comments

Comments
 (0)