Skip to content

Commit

Permalink
feat(MIWClient): adds response body in case errors in MIW response
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf4ood committed Jul 6, 2023
1 parent 35a4607 commit 93d1dde
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,20 @@ private <R> Result<R> handleSuccess(Response response, TypeReference<R> tr) {
}

private <R> Result<R> handleError(Response response) {
var msg = format("MIW API returned %s", response.code());
var body = "";
if (response.body() != null) {
try {
body = response.body().string();
} catch (IOException e) {
monitor.debug("Failed to read response from MIW");
return Result.failure(e.getMessage());
}
}
var msg = format("MIW API returned %s with body: %s", response.code(), body);
monitor.debug(msg);
return Result.failure(msg);
}


private Result<Request.Builder> baseRequestWithToken() {
return oauth2Client.obtainRequestToken()
.map(this::baseRequestWithToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@
public class MiwApiClientImplTest {

static final String BASE_URL = "http://localhost:8080";
private final Consumer<Request> emptyAcceptor = (r) -> {
};
Interceptor interceptor = mock(Interceptor.class);
MiwApiClientImpl client;
Monitor monitor = mock(Monitor.class);
MiwOauth2Client oauth2Client = mock(MiwOauth2Client.class);
ObjectMapper mapper = new ObjectMapper();

String participantId = "participantId";

String authorityId = "authorityId";

@BeforeEach
Expand Down Expand Up @@ -148,13 +148,15 @@ void createPresentation() throws IOException {
void createPresentation_fails_whenMiwFails() throws IOException {

when(interceptor.intercept(isA(Interceptor.Chain.class)))
.thenAnswer(invocation -> createResponse(500, invocation));
.thenAnswer(invocation -> createResponse(500, invocation, emptyAcceptor, "Request Failed"));

when(oauth2Client.obtainRequestToken()).thenReturn(Result.success(TokenRepresentation.Builder.newInstance().token("testToken").build()));

var result = client.createPresentation(List.of(), "audience");

assertThat(result).isNotNull().matches(Result::failed);
assertThat(result).isNotNull().matches(Result::failed)
.extracting(Result::getFailureDetail)
.satisfies(failure -> assertThat(failure).contains("Request Failed"));
}

@Test
Expand All @@ -166,7 +168,7 @@ void createPresentation_fails_whenTokenRequestFails() {

assertThat(result).isNotNull().matches(Result::failed);
}

@Test
void verifyPresentation() throws IOException {
var jwt = "jwt";
Expand Down Expand Up @@ -268,7 +270,7 @@ private Response createResponse(int code, InvocationOnMock invocation, Consumer<
.body(ResponseBody.create(bodyString, MediaType.parse("application/json")))
.build();
}

private String toJson(Object body) {
try {
return mapper.writeValueAsString(body);
Expand Down

0 comments on commit 93d1dde

Please sign in to comment.