From 60e0b18ae901934c66f8a3ad49b4c68285d18657 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 8 Nov 2022 15:03:11 +0000 Subject: [PATCH 1/4] Bump quarkus-universe-bom from 2.13.3.Final to 2.13.4.Final Bumps [quarkus-universe-bom](https://github.com/quarkusio/quarkus-platform) from 2.13.3.Final to 2.13.4.Final. - [Release notes](https://github.com/quarkusio/quarkus-platform/releases) - [Commits](https://github.com/quarkusio/quarkus-platform/compare/2.13.3.Final...2.13.4.Final) --- updated-dependencies: - dependency-name: io.quarkus:quarkus-universe-bom dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index bfc79d7..39e1922 100644 --- a/pom.xml +++ b/pom.xml @@ -20,7 +20,7 @@ SPDX-License-Identifier: Apache-2.0 UTF-8 0.0.4 - 2.13.3.Final + 2.13.4.Final 2.0.3 2.3.7 0.9.1 From 3b13a213f464329ff1a4bfa3bc41c50ea0bf1c0c Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 9 Nov 2022 15:02:49 +0000 Subject: [PATCH 2/4] Bump quarkus-universe-bom from 2.13.4.Final to 2.14.0.Final Bumps [quarkus-universe-bom](https://github.com/quarkusio/quarkus-platform) from 2.13.4.Final to 2.14.0.Final. - [Release notes](https://github.com/quarkusio/quarkus-platform/releases) - [Commits](https://github.com/quarkusio/quarkus-platform/compare/2.13.4.Final...2.14.0.Final) --- updated-dependencies: - dependency-name: io.quarkus:quarkus-universe-bom dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 39e1922..8f7a06b 100644 --- a/pom.xml +++ b/pom.xml @@ -20,7 +20,7 @@ SPDX-License-Identifier: Apache-2.0 UTF-8 0.0.4 - 2.13.4.Final + 2.14.0.Final 2.0.3 2.3.7 0.9.1 From ee3bce544ba03c5193e0214e05638ed77013e870 Mon Sep 17 00:00:00 2001 From: Dennis Labordus Date: Thu, 10 Nov 2022 07:57:16 +0100 Subject: [PATCH 3/4] Refactoring to be able to re-use error handling. Signed-off-by: Dennis Labordus --- .../core/websocket/WebsocketHandler.java | 20 +--- .../core/websocket/WebsocketSupport.java | 14 ++- .../core/websocket/WebsocketHandlerTest.java | 1 - .../core/websocket/WebsocketSupportTest.java | 112 +++++++++++++++++- 4 files changed, 127 insertions(+), 20 deletions(-) diff --git a/websocket-commons/src/main/java/org/lfenergy/compas/core/websocket/WebsocketHandler.java b/websocket-commons/src/main/java/org/lfenergy/compas/core/websocket/WebsocketHandler.java index 954d21b..1ca3053 100644 --- a/websocket-commons/src/main/java/org/lfenergy/compas/core/websocket/WebsocketHandler.java +++ b/websocket-commons/src/main/java/org/lfenergy/compas/core/websocket/WebsocketHandler.java @@ -3,14 +3,13 @@ // SPDX-License-Identifier: Apache-2.0 package org.lfenergy.compas.core.websocket; -import org.lfenergy.compas.core.commons.exception.CompasException; import org.lfenergy.compas.core.commons.model.ErrorResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.websocket.Session; -import static org.lfenergy.compas.core.commons.exception.CompasErrorCode.WEBSOCKET_GENERAL_ERROR_CODE; +import static org.lfenergy.compas.core.websocket.WebsocketSupport.handleException; /** * Simple Websocket Handler to handle the result from an executor being called and send this result to the Websocket @@ -26,23 +25,12 @@ public void execute(Session session, EventExecutor executor) { LOGGER.debug("Executing executor to retrieve response"); var result = executor.execute(); session.getAsyncRemote().sendObject(result); - } catch (RuntimeException re) { - handleException(session, re); + } catch (RuntimeException exp) { + LOGGER.info("Exception occurred during handling the websocket request", exp); + handleException(session, exp); } } - private void handleException(Session session, RuntimeException re) { - var response = new ErrorResponse(); - if (re instanceof CompasException) { - LOGGER.info("Handling CompasException thrown by Executor!", re); - response.addErrorMessage(((CompasException) re).getErrorCode(), re.getMessage()); - } else { - LOGGER.info("Handling RuntimeException thrown by Executor!", re); - response.addErrorMessage(WEBSOCKET_GENERAL_ERROR_CODE, re.getMessage()); - } - session.getAsyncRemote().sendObject(response); - } - @FunctionalInterface public interface EventExecutor { T execute(); diff --git a/websocket-commons/src/main/java/org/lfenergy/compas/core/websocket/WebsocketSupport.java b/websocket-commons/src/main/java/org/lfenergy/compas/core/websocket/WebsocketSupport.java index 0d6141f..36ba922 100644 --- a/websocket-commons/src/main/java/org/lfenergy/compas/core/websocket/WebsocketSupport.java +++ b/websocket-commons/src/main/java/org/lfenergy/compas/core/websocket/WebsocketSupport.java @@ -4,13 +4,14 @@ package org.lfenergy.compas.core.websocket; import org.lfenergy.compas.core.commons.exception.CompasException; +import org.lfenergy.compas.core.commons.model.ErrorResponse; +import javax.websocket.Session; import javax.xml.bind.JAXBContext; import java.io.StringReader; import java.io.StringWriter; -import static org.lfenergy.compas.core.commons.exception.CompasErrorCode.WEBSOCKET_DECODER_ERROR_CODE; -import static org.lfenergy.compas.core.commons.exception.CompasErrorCode.WEBSOCKET_ENCODER_ERROR_CODE; +import static org.lfenergy.compas.core.commons.exception.CompasErrorCode.*; public final class WebsocketSupport { WebsocketSupport() { @@ -45,4 +46,13 @@ public static T decode(String message, Class jaxbClass) { } } + public static void handleException(Session session, RuntimeException re) { + var response = new ErrorResponse(); + if (re instanceof CompasException) { + response.addErrorMessage(((CompasException) re).getErrorCode(), re.getMessage()); + } else { + response.addErrorMessage(WEBSOCKET_GENERAL_ERROR_CODE, re.getMessage()); + } + session.getAsyncRemote().sendObject(response); + } } diff --git a/websocket-commons/src/test/java/org/lfenergy/compas/core/websocket/WebsocketHandlerTest.java b/websocket-commons/src/test/java/org/lfenergy/compas/core/websocket/WebsocketHandlerTest.java index af8802f..986dbed 100644 --- a/websocket-commons/src/test/java/org/lfenergy/compas/core/websocket/WebsocketHandlerTest.java +++ b/websocket-commons/src/test/java/org/lfenergy/compas/core/websocket/WebsocketHandlerTest.java @@ -20,7 +20,6 @@ @ExtendWith(MockitoExtension.class) class WebsocketHandlerTest { - @Test void execute_WhenCalledSuccessful_ThenResponseSendToWebsocket() { var message = "Some message"; diff --git a/websocket-commons/src/test/java/org/lfenergy/compas/core/websocket/WebsocketSupportTest.java b/websocket-commons/src/test/java/org/lfenergy/compas/core/websocket/WebsocketSupportTest.java index c21cb36..773da2f 100644 --- a/websocket-commons/src/test/java/org/lfenergy/compas/core/websocket/WebsocketSupportTest.java +++ b/websocket-commons/src/test/java/org/lfenergy/compas/core/websocket/WebsocketSupportTest.java @@ -4,12 +4,122 @@ package org.lfenergy.compas.core.websocket; import org.junit.jupiter.api.Test; +import org.lfenergy.compas.core.commons.exception.CompasException; +import org.lfenergy.compas.core.commons.model.ErrorResponse; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; -import static org.junit.jupiter.api.Assertions.assertThrows; +import javax.websocket.RemoteEndpoint; +import javax.websocket.Session; + +import static org.junit.jupiter.api.Assertions.*; +import static org.lfenergy.compas.core.commons.CommonConstants.COMPAS_COMMONS_V1_NS_URI; +import static org.lfenergy.compas.core.commons.exception.CompasErrorCode.*; +import static org.lfenergy.compas.core.websocket.WebsocketSupport.*; +import static org.mockito.Mockito.*; class WebsocketSupportTest { @Test void constructor_WhenConstructorCalled_ThenShouldThrowExceptionCauseForbidden() { assertThrows(UnsupportedOperationException.class, WebsocketSupport::new); } + + @Test + void encode_WhenCalledWithErrorResponse_ThenXMLStringReturned() { + var errorCode = "ERR-0001"; + var errorMessage = "Error Message"; + var errorResponse = new ErrorResponse(); + errorResponse.addErrorMessage(errorCode, errorMessage); + + var result = encode(errorResponse, ErrorResponse.class); + + var expectedResult = "" + + "" + + "" + + "" + errorCode + "" + + "" + errorMessage + "" + + "" + + ""; + assertEquals(expectedResult, result); + } + + @Test + void encode_WhenCalledWithNoJaxbObject_ThenExceptionThrown() { + var exception = assertThrows(CompasException.class, () -> encode("Some Non JAXB String", String.class)); + assertEquals(WEBSOCKET_ENCODER_ERROR_CODE, exception.getErrorCode()); + assertEquals(CompasException.class, exception.getClass()); + } + + @Test + void decode_WhenCalledWithCorrectXML_ThenObjectReturned() { + var errorCode = "ERR-0001"; + var errorMessage = "Error Message"; + + var xmlMessage = "" + + "" + + "" + + "" + errorCode + "" + + "" + errorMessage + "" + + "" + + ""; + + var result = decode(xmlMessage, ErrorResponse.class); + + assertNotNull(result); + assertEquals(1, result.getErrorMessages().size()); + var message = result.getErrorMessages().get(0); + assertEquals(errorCode, message.getCode()); + assertEquals(errorMessage, message.getMessage()); + } + + + @Test + void decode_WhenCalledWithInvalidXML_ThenExceptionThrown() { + var xmlMessage = "" + + "" + + ""; + + var exception = assertThrows(CompasException.class, () -> decode(xmlMessage, ErrorResponse.class)); + assertEquals(WEBSOCKET_DECODER_ERROR_CODE, exception.getErrorCode()); + assertEquals(CompasException.class, exception.getClass()); + } + + @Test + void handleException_WhenCalledWithCompasException_ThenErrorResponseSendToSession() { + var errorCode = "ERR-0001"; + var errorMessage = "Error Message"; + var session = mockSession(); + + handleException(session, new CompasException(errorCode, errorMessage)); + + verifyErrorResponse(session, errorCode, errorMessage); + } + + @Test + void handleException_WhenCalledWithRuntimeException_ThenErrorResponseSendToSession() { + var errorMessage = "Error Message"; + var session = mockSession(); + + handleException(session, new RuntimeException(errorMessage)); + + verifyErrorResponse(session, WEBSOCKET_GENERAL_ERROR_CODE, errorMessage); + } + + private Session mockSession() { + var session = Mockito.mock(Session.class); + var async = Mockito.mock(RemoteEndpoint.Async.class); + when(session.getAsyncRemote()).thenReturn(async); + return session; + } + + private void verifyErrorResponse(Session session, String errorCode, String errorMessage) { + verify(session, times(1)).getAsyncRemote(); + ArgumentCaptor captor = ArgumentCaptor.forClass(ErrorResponse.class); + verify(session.getAsyncRemote(), times(1)).sendObject(captor.capture()); + var response = captor.getValue(); + assertEquals(1, response.getErrorMessages().size()); + var message = response.getErrorMessages().get(0); + assertEquals(errorCode, message.getCode()); + assertEquals(errorMessage, message.getMessage()); + } } From ebf0509a57d920e618530c4d1592be7a13e47769 Mon Sep 17 00:00:00 2001 From: Dennis Labordus Date: Thu, 10 Nov 2022 08:00:22 +0100 Subject: [PATCH 4/4] Refactoring to be able to re-use error handling. Signed-off-by: Dennis Labordus --- .../lfenergy/compas/core/websocket/WebsocketSupport.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/websocket-commons/src/main/java/org/lfenergy/compas/core/websocket/WebsocketSupport.java b/websocket-commons/src/main/java/org/lfenergy/compas/core/websocket/WebsocketSupport.java index 36ba922..563df3b 100644 --- a/websocket-commons/src/main/java/org/lfenergy/compas/core/websocket/WebsocketSupport.java +++ b/websocket-commons/src/main/java/org/lfenergy/compas/core/websocket/WebsocketSupport.java @@ -46,12 +46,12 @@ public static T decode(String message, Class jaxbClass) { } } - public static void handleException(Session session, RuntimeException re) { + public static void handleException(Session session, Throwable throwable) { var response = new ErrorResponse(); - if (re instanceof CompasException) { - response.addErrorMessage(((CompasException) re).getErrorCode(), re.getMessage()); + if (throwable instanceof CompasException) { + response.addErrorMessage(((CompasException) throwable).getErrorCode(), throwable.getMessage()); } else { - response.addErrorMessage(WEBSOCKET_GENERAL_ERROR_CODE, re.getMessage()); + response.addErrorMessage(WEBSOCKET_GENERAL_ERROR_CODE, throwable.getMessage()); } session.getAsyncRemote().sendObject(response); }