Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(MIWClient): adds response body in case errors in MIW response #574

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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