Skip to content

Commit 9f4e4ee

Browse files
committed
(amihaiemil#108) Add the body to the UnexpectedResponseException
As per PR review: * PayloadOf now extends JsonResource * Added the payload to the exception message of UnexpectedResponseException
1 parent e72083b commit 9f4e4ee

File tree

4 files changed

+36
-149
lines changed

4 files changed

+36
-149
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Collection;
2929
import java.util.Map;
3030
import java.util.Set;
31+
import java.util.function.Supplier;
3132
import javax.json.JsonArray;
3233
import javax.json.JsonNumber;
3334
import javax.json.JsonObject;
@@ -53,6 +54,14 @@ abstract class JsonResource implements JsonObject {
5354
* The JsonObject resource in question.
5455
*/
5556
private final JsonObject resource;
57+
58+
/**
59+
* Ctor.
60+
* @param resource Supply the JsonObject.
61+
*/
62+
JsonResource(final Supplier<JsonObject> resource) {
63+
this(resource.get());
64+
}
5665

5766
/**
5867
* Ctor.

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

Lines changed: 3 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,8 @@
2626
package com.amihaiemil.docker;
2727

2828
import java.io.IOException;
29-
import java.util.Collection;
30-
import java.util.Map;
31-
import java.util.Set;
32-
import java.util.function.Supplier;
3329
import javax.json.Json;
34-
import javax.json.JsonArray;
35-
import javax.json.JsonNumber;
3630
import javax.json.JsonObject;
37-
import javax.json.JsonString;
38-
import javax.json.JsonValue;
3931
import org.apache.http.HttpEntityEnclosingRequest;
4032
import org.apache.http.HttpRequest;
4133
import org.apache.http.HttpResponse;
@@ -48,20 +40,15 @@
4840
* @since 0.0.1
4941
* @todo #108:30min Add tests for PayloadOf.
5042
*/
51-
final class PayloadOf implements JsonObject {
52-
/**
53-
* The request's payload.
54-
*/
55-
private final JsonObject json;
56-
43+
final class PayloadOf extends JsonResource {
5744
/**
5845
* Ctor.
5946
*
6047
* @param request The http request
6148
* @throws IllegalStateException if the request's payload cannot be read
6249
*/
6350
PayloadOf(final HttpRequest request) {
64-
this(() -> {
51+
super(() -> {
6552
try {
6653
final JsonObject body;
6754
if (request instanceof HttpEntityEnclosingRequest) {
@@ -87,7 +74,7 @@ final class PayloadOf implements JsonObject {
8774
* @throws IllegalStateException if the response's payload cannot be read
8875
*/
8976
PayloadOf(final HttpResponse response) {
90-
this(() -> {
77+
super(() -> {
9178
try {
9279
return Json.createReader(
9380
response.getEntity().getContent()
@@ -99,133 +86,4 @@ final class PayloadOf implements JsonObject {
9986
}
10087
});
10188
}
102-
103-
/**
104-
* Ctor.
105-
* @param json The json.
106-
* @throws IllegalStateException if the payload cannot be read
107-
*/
108-
private PayloadOf(final Supplier<JsonObject> json) {
109-
this.json = json.get();
110-
}
111-
112-
@Override
113-
public JsonArray getJsonArray(final String name) {
114-
return this.json.getJsonArray(name);
115-
}
116-
117-
@Override
118-
public JsonObject getJsonObject(final String name) {
119-
return this.json.getJsonObject(name);
120-
}
121-
122-
@Override
123-
public JsonNumber getJsonNumber(final String name) {
124-
return this.json.getJsonNumber(name);
125-
}
126-
127-
@Override
128-
public JsonString getJsonString(final String name) {
129-
return this.json.getJsonString(name);
130-
}
131-
132-
@Override
133-
public String getString(final String name) {
134-
return this.json.getString(name);
135-
}
136-
137-
@Override
138-
public String getString(final String name, final String defaultValue) {
139-
return this.json.getString(name, defaultValue);
140-
}
141-
142-
@Override
143-
public int getInt(final String name) {
144-
return this.json.getInt(name);
145-
}
146-
147-
@Override
148-
public int getInt(final String name, final int defaultValue) {
149-
return this.json.getInt(name, defaultValue);
150-
}
151-
152-
@Override
153-
public boolean getBoolean(final String name) {
154-
return this.json.getBoolean(name);
155-
}
156-
157-
@Override
158-
public boolean getBoolean(final String name, final boolean defaultValue) {
159-
return this.json.getBoolean(name, defaultValue);
160-
}
161-
162-
@Override
163-
public boolean isNull(final String name) {
164-
return this.json.isNull(name);
165-
}
166-
167-
@Override
168-
public ValueType getValueType() {
169-
return this.json.getValueType();
170-
}
171-
172-
@Override
173-
public int size() {
174-
return this.json.size();
175-
}
176-
177-
@Override
178-
public boolean isEmpty() {
179-
return this.json.isEmpty();
180-
}
181-
182-
@Override
183-
public boolean containsKey(final Object key) {
184-
return this.json.containsKey(key);
185-
}
186-
187-
@Override
188-
public boolean containsValue(final Object value) {
189-
return this.json.containsValue(value);
190-
}
191-
192-
@Override
193-
public JsonValue get(final Object key) {
194-
return this.json.get(key);
195-
}
196-
197-
@Override
198-
public JsonValue put(final String key, final JsonValue value) {
199-
return this.json.put(key, value);
200-
}
201-
202-
@Override
203-
public JsonValue remove(final Object key) {
204-
return this.json.remove(key);
205-
}
206-
207-
@Override
208-
public void putAll(final Map<? extends String, ? extends JsonValue> map) {
209-
this.json.putAll(map);
210-
}
211-
212-
@Override
213-
public void clear() {
214-
this.json.clear();
215-
}
216-
217-
@Override
218-
public Set<String> keySet() {
219-
return this.json.keySet();
220-
}
221-
222-
@Override
223-
public Collection<JsonValue> values() {
224-
return this.json.values();
225-
}
226-
227-
@Override
228-
public Set<Entry<String, JsonValue>> entrySet() {
229-
return this.json.entrySet();
230-
}
23189
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,11 @@ public UnexpectedResponseException(
6969
final String endpoint, final int actualStatus,
7070
final int expectedStatus, final JsonObject body
7171
) {
72-
// @checkstyle LineLength (1 line)
73-
super("Expected status " + expectedStatus + " but got " + actualStatus + " when calling " + endpoint);
72+
super(String.format(
73+
// @checkstyle LineLength (1 line)
74+
"Expected status %s but got %s when calling %s. Response body was %s",
75+
expectedStatus, actualStatus, endpoint, body.toString()
76+
));
7477
this.endpoint = endpoint;
7578
this.actualStatus = actualStatus;
7679
this.expectedStatus = expectedStatus;

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,29 @@ public void returnsMessage() {
9292
"/uri", HttpStatus.SC_NOT_FOUND,
9393
HttpStatus.SC_OK, Json.createObjectBuilder().build()
9494
).getMessage(),
95-
Matchers.equalTo(
96-
"Expected status 200 but got 404 when calling /uri"
95+
Matchers.startsWith(
96+
// @checkstyle LineLength (1 line)
97+
"Expected status 200 but got 404 when calling /uri. Response body was"
9798
)
9899
);
99100
}
100101

102+
/**
103+
* UnexpectedResponseException appends the payload to the message.
104+
*/
105+
@Test
106+
public void returnsMessageWithPayload() {
107+
final JsonObject payload = Json.createObjectBuilder()
108+
.add("message", "Some error")
109+
.build();
110+
MatcherAssert.assertThat(
111+
new UnexpectedResponseException(
112+
"/uri", HttpStatus.SC_OK, HttpStatus.SC_OK, payload
113+
).getMessage(),
114+
Matchers.endsWith(payload.toString())
115+
);
116+
}
117+
101118
/**
102119
* UnexpectedResponseException returns the payload.
103120
*/

0 commit comments

Comments
 (0)