Skip to content

Commit

Permalink
(#108) Add the body to the UnexpectedResponseException
Browse files Browse the repository at this point in the history
As per PR review:
* PayloadOf now extends JsonResource
* Added the payload to the exception message of
  UnexpectedResponseException
  • Loading branch information
llorllale committed May 31, 2018
1 parent e72083b commit 9f4e4ee
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 149 deletions.
9 changes: 9 additions & 0 deletions src/main/java/com/amihaiemil/docker/JsonResource.java
Expand Up @@ -28,6 +28,7 @@
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
import javax.json.JsonArray;
import javax.json.JsonNumber;
import javax.json.JsonObject;
Expand All @@ -53,6 +54,14 @@ abstract class JsonResource implements JsonObject {
* The JsonObject resource in question.
*/
private final JsonObject resource;

/**
* Ctor.
* @param resource Supply the JsonObject.
*/
JsonResource(final Supplier<JsonObject> resource) {
this(resource.get());
}

/**
* Ctor.
Expand Down
148 changes: 3 additions & 145 deletions src/main/java/com/amihaiemil/docker/PayloadOf.java
Expand Up @@ -26,16 +26,8 @@
package com.amihaiemil.docker;

import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonNumber;
import javax.json.JsonObject;
import javax.json.JsonString;
import javax.json.JsonValue;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
Expand All @@ -48,20 +40,15 @@
* @since 0.0.1
* @todo #108:30min Add tests for PayloadOf.
*/
final class PayloadOf implements JsonObject {
/**
* The request's payload.
*/
private final JsonObject json;

final class PayloadOf extends JsonResource {
/**
* Ctor.
*
* @param request The http request
* @throws IllegalStateException if the request's payload cannot be read
*/
PayloadOf(final HttpRequest request) {
this(() -> {
super(() -> {
try {
final JsonObject body;
if (request instanceof HttpEntityEnclosingRequest) {
Expand All @@ -87,7 +74,7 @@ final class PayloadOf implements JsonObject {
* @throws IllegalStateException if the response's payload cannot be read
*/
PayloadOf(final HttpResponse response) {
this(() -> {
super(() -> {
try {
return Json.createReader(
response.getEntity().getContent()
Expand All @@ -99,133 +86,4 @@ final class PayloadOf implements JsonObject {
}
});
}

/**
* Ctor.
* @param json The json.
* @throws IllegalStateException if the payload cannot be read
*/
private PayloadOf(final Supplier<JsonObject> json) {
this.json = json.get();
}

@Override
public JsonArray getJsonArray(final String name) {
return this.json.getJsonArray(name);
}

@Override
public JsonObject getJsonObject(final String name) {
return this.json.getJsonObject(name);
}

@Override
public JsonNumber getJsonNumber(final String name) {
return this.json.getJsonNumber(name);
}

@Override
public JsonString getJsonString(final String name) {
return this.json.getJsonString(name);
}

@Override
public String getString(final String name) {
return this.json.getString(name);
}

@Override
public String getString(final String name, final String defaultValue) {
return this.json.getString(name, defaultValue);
}

@Override
public int getInt(final String name) {
return this.json.getInt(name);
}

@Override
public int getInt(final String name, final int defaultValue) {
return this.json.getInt(name, defaultValue);
}

@Override
public boolean getBoolean(final String name) {
return this.json.getBoolean(name);
}

@Override
public boolean getBoolean(final String name, final boolean defaultValue) {
return this.json.getBoolean(name, defaultValue);
}

@Override
public boolean isNull(final String name) {
return this.json.isNull(name);
}

@Override
public ValueType getValueType() {
return this.json.getValueType();
}

@Override
public int size() {
return this.json.size();
}

@Override
public boolean isEmpty() {
return this.json.isEmpty();
}

@Override
public boolean containsKey(final Object key) {
return this.json.containsKey(key);
}

@Override
public boolean containsValue(final Object value) {
return this.json.containsValue(value);
}

@Override
public JsonValue get(final Object key) {
return this.json.get(key);
}

@Override
public JsonValue put(final String key, final JsonValue value) {
return this.json.put(key, value);
}

@Override
public JsonValue remove(final Object key) {
return this.json.remove(key);
}

@Override
public void putAll(final Map<? extends String, ? extends JsonValue> map) {
this.json.putAll(map);
}

@Override
public void clear() {
this.json.clear();
}

@Override
public Set<String> keySet() {
return this.json.keySet();
}

@Override
public Collection<JsonValue> values() {
return this.json.values();
}

@Override
public Set<Entry<String, JsonValue>> entrySet() {
return this.json.entrySet();
}
}
Expand Up @@ -69,8 +69,11 @@ public UnexpectedResponseException(
final String endpoint, final int actualStatus,
final int expectedStatus, final JsonObject body
) {
// @checkstyle LineLength (1 line)
super("Expected status " + expectedStatus + " but got " + actualStatus + " when calling " + endpoint);
super(String.format(
// @checkstyle LineLength (1 line)
"Expected status %s but got %s when calling %s. Response body was %s",
expectedStatus, actualStatus, endpoint, body.toString()
));
this.endpoint = endpoint;
this.actualStatus = actualStatus;
this.expectedStatus = expectedStatus;
Expand Down
Expand Up @@ -92,12 +92,29 @@ public void returnsMessage() {
"/uri", HttpStatus.SC_NOT_FOUND,
HttpStatus.SC_OK, Json.createObjectBuilder().build()
).getMessage(),
Matchers.equalTo(
"Expected status 200 but got 404 when calling /uri"
Matchers.startsWith(
// @checkstyle LineLength (1 line)
"Expected status 200 but got 404 when calling /uri. Response body was"
)
);
}

/**
* UnexpectedResponseException appends the payload to the message.
*/
@Test
public void returnsMessageWithPayload() {
final JsonObject payload = Json.createObjectBuilder()
.add("message", "Some error")
.build();
MatcherAssert.assertThat(
new UnexpectedResponseException(
"/uri", HttpStatus.SC_OK, HttpStatus.SC_OK, payload
).getMessage(),
Matchers.endsWith(payload.toString())
);
}

/**
* UnexpectedResponseException returns the payload.
*/
Expand Down

0 comments on commit 9f4e4ee

Please sign in to comment.