Skip to content

Commit

Permalink
Fix update
Browse files Browse the repository at this point in the history
  • Loading branch information
malkusch committed Nov 6, 2023
1 parent d1f8d2a commit 9631942
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/main/java/de/malkusch/km200/KM200.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,20 @@ private <T> HttpResponse<T> send(HttpRequest request, BodyHandler<T> bodyHandler
throws IOException, InterruptedException, KM200Exception {

var response = http.send(request, bodyHandler);
return switch (response.statusCode()) {
case 200 -> response;
case 403 -> throw new KM200Exception.Forbidden(request.uri() + " is forbidden");
case 404 -> throw new KM200Exception.NotFound(request.uri() + " was not found");
case 423 -> throw new KM200Exception.Locked(request.uri() + " was locked");
case 500 -> throw new KM200Exception.ServerError(request.uri() + " resulted in a server error");
default -> throw new KM200Exception(request.uri() + " failed with response code " + response.statusCode());
};
var status = response.statusCode();

if (status >= 200 && status <= 299) {
return response;

} else {
throw switch (status) {
case 403 -> new KM200Exception.Forbidden(request.uri() + " is forbidden");
case 404 -> new KM200Exception.NotFound(request.uri() + " was not found");
case 423 -> new KM200Exception.Locked(request.uri() + " was locked");
case 500 -> new KM200Exception.ServerError(request.uri() + " resulted in a server error");
default -> new KM200Exception(request.uri() + " failed with response code " + status);
};
}
}

public double queryDouble(String path) throws KM200Exception, IOException, InterruptedException {
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/de/malkusch/km200/KM200Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,26 @@ public void stubSystem() throws Exception {
stubFor(get("/system").willReturn(ok(loadBody("system"))));
}

@ParameterizedTest
@ValueSource(ints = { 200, 201, 204, 299 })
public void updateShouldSucceedWith2xx(int status) throws Exception {
stubFor(post("/update").willReturn(status(status)));
var km200 = new KM200(URI, TIMEOUT, GATEWAY_PASSWORD, PRIVATE_PASSWORD, SALT);

km200.update("/update", 42);
}

@ParameterizedTest
@ValueSource(ints = { 200, 201, 299 })
public void queryShouldSucceedWith2xx(int status) throws Exception {
stubFor(get("/query").willReturn(status(status).withBody(loadBody("gateway.DateTime"))));
var km200 = new KM200(URI, TIMEOUT, GATEWAY_PASSWORD, PRIVATE_PASSWORD, SALT);

var dateTime = km200.queryString("/query");

assertEquals("2021-09-21T10:49:25", dateTime);
}

@ParameterizedTest
@ValueSource(strings = { "http://localhost:" + PORT, "http://localhost:" + PORT + "/" })
public void queryShouldReplaceSlashesInURI(String uri) throws Exception {
Expand Down

0 comments on commit 9631942

Please sign in to comment.