Skip to content

Commit

Permalink
feat(MIWClient): adds response body in case errors in MIW response (#574
Browse files Browse the repository at this point in the history
)
  • Loading branch information
wolf4ood authored Jul 6, 2023
1 parent 57dd5a7 commit f9574c8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,26 @@ private <R> Result<R> handleSuccess(Response response, TypeReference<R> tr) {
var body = Objects.requireNonNull(response.body()).string();
return Result.success(mapper.readValue(body, tr));
} catch (IOException e) {
monitor.debug("Failed to parse response from MIW");
monitor.severe("Failed to parse response from MIW");
return Result.failure(e.getMessage());
}
}

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


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 @@ -49,21 +49,23 @@
import static org.eclipse.tractusx.edc.iam.ssi.miw.api.MiwApiClientImpl.PRESENTATIONS_VALIDATION_PATH;
import static org.eclipse.tractusx.edc.iam.ssi.miw.api.MiwApiClientImpl.VERIFIABLE_CREDENTIALS;
import static org.eclipse.tractusx.edc.iam.ssi.miw.api.MiwApiClientImpl.VP_FIELD;
import static org.mockito.ArgumentMatchers.contains;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

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 +150,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);

verify(monitor).severe(contains("Request Failed"));
}

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

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

@Test
void verifyPresentation() throws IOException {
var jwt = "jwt";
Expand Down

0 comments on commit f9574c8

Please sign in to comment.