From f9574c80f3006da111d01ce14853fdc30751ff4a Mon Sep 17 00:00:00 2001 From: Enrico Risa Date: Thu, 6 Jul 2023 12:38:31 +0200 Subject: [PATCH] feat(MIWClient): adds response body in case errors in MIW response (#574) --- .../edc/iam/ssi/miw/api/MiwApiClientImpl.java | 18 +++++++++++++----- .../iam/ssi/miw/api/MiwApiClientImplTest.java | 12 ++++++++---- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/edc-extensions/ssi/ssi-miw-credential-client/src/main/java/org/eclipse/tractusx/edc/iam/ssi/miw/api/MiwApiClientImpl.java b/edc-extensions/ssi/ssi-miw-credential-client/src/main/java/org/eclipse/tractusx/edc/iam/ssi/miw/api/MiwApiClientImpl.java index 6ff698565..c8de50c4b 100644 --- a/edc-extensions/ssi/ssi-miw-credential-client/src/main/java/org/eclipse/tractusx/edc/iam/ssi/miw/api/MiwApiClientImpl.java +++ b/edc-extensions/ssi/ssi-miw-credential-client/src/main/java/org/eclipse/tractusx/edc/iam/ssi/miw/api/MiwApiClientImpl.java @@ -164,18 +164,26 @@ private Result handleSuccess(Response response, TypeReference 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 Result 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 baseRequestWithToken() { return oauth2Client.obtainRequestToken() .map(this::baseRequestWithToken); diff --git a/edc-extensions/ssi/ssi-miw-credential-client/src/test/java/org/eclipse/tractusx/edc/iam/ssi/miw/api/MiwApiClientImplTest.java b/edc-extensions/ssi/ssi-miw-credential-client/src/test/java/org/eclipse/tractusx/edc/iam/ssi/miw/api/MiwApiClientImplTest.java index 753893be5..ebec7cc35 100644 --- a/edc-extensions/ssi/ssi-miw-credential-client/src/test/java/org/eclipse/tractusx/edc/iam/ssi/miw/api/MiwApiClientImplTest.java +++ b/edc-extensions/ssi/ssi-miw-credential-client/src/test/java/org/eclipse/tractusx/edc/iam/ssi/miw/api/MiwApiClientImplTest.java @@ -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 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 @@ -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 @@ -166,7 +170,7 @@ void createPresentation_fails_whenTokenRequestFails() { assertThat(result).isNotNull().matches(Result::failed); } - + @Test void verifyPresentation() throws IOException { var jwt = "jwt";