Skip to content

Commit b4b7d16

Browse files
author
Dennis Labordus
committed
Added extra Error Handling in the Endpoint.
Signed-off-by: Dennis Labordus <[email protected]>
1 parent 0e15ff6 commit b4b7d16

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

app/src/main/java/org/lfenergy/compas/scl/validator/rest/v1/SclValidatorServerEndpoint.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import io.quarkus.security.Authenticated;
77
import io.vertx.mutiny.core.eventbus.EventBus;
8+
import org.lfenergy.compas.core.websocket.ErrorResponseEncoder;
89
import org.lfenergy.compas.scl.extensions.model.SclFileType;
910
import org.lfenergy.compas.scl.validator.rest.v1.event.SclValidatorEventRequest;
1011
import org.lfenergy.compas.scl.validator.rest.v1.model.SclValidateRequest;
@@ -21,13 +22,14 @@
2122
import javax.websocket.server.PathParam;
2223
import javax.websocket.server.ServerEndpoint;
2324

25+
import static org.lfenergy.compas.core.websocket.WebsocketSupport.handleException;
2426
import static org.lfenergy.compas.scl.validator.rest.SclResourceConstants.TYPE_PATH_PARAM;
2527

2628
@Authenticated
2729
@ApplicationScoped
2830
@ServerEndpoint(value = "/validate-ws/v1/{" + TYPE_PATH_PARAM + "}",
2931
decoders = {SclValidateRequestDecoder.class, SclValidateResponseDecoder.class},
30-
encoders = {SclValidateRequestEncoder.class, SclValidateResponseEncoder.class})
32+
encoders = {SclValidateRequestEncoder.class, SclValidateResponseEncoder.class, ErrorResponseEncoder.class})
3133
public class SclValidatorServerEndpoint {
3234
private static final Logger LOGGER = LoggerFactory.getLogger(SclValidatorServerEndpoint.class);
3335

@@ -53,6 +55,7 @@ public void onMessage(Session session, SclValidateRequest request, @PathParam(TY
5355
@OnError
5456
public void onError(Session session, @PathParam(TYPE_PATH_PARAM) String type, Throwable throwable) {
5557
LOGGER.warn("Error with session {} for type {}.", session.getId(), type, throwable);
58+
handleException(session, throwable);
5659
}
5760

5861
@OnClose

app/src/test/java/org/lfenergy/compas/scl/validator/rest/v1/SclValidatorServerEndpointTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import io.quarkus.test.junit.mockito.InjectMock;
99
import io.quarkus.test.security.TestSecurity;
1010
import org.junit.jupiter.api.Test;
11+
import org.lfenergy.compas.core.commons.model.ErrorMessage;
12+
import org.lfenergy.compas.core.commons.model.ErrorResponse;
13+
import org.lfenergy.compas.core.websocket.ErrorResponseDecoder;
1114
import org.lfenergy.compas.scl.extensions.model.SclFileType;
1215
import org.lfenergy.compas.scl.validator.model.ValidationError;
1316
import org.lfenergy.compas.scl.validator.rest.v1.model.SclValidateRequest;
@@ -28,12 +31,14 @@
2831

2932
import static org.junit.jupiter.api.Assertions.assertEquals;
3033
import static org.junit.jupiter.api.Assertions.assertNotNull;
34+
import static org.lfenergy.compas.core.commons.exception.CompasErrorCode.WEBSOCKET_DECODER_ERROR_CODE;
3135
import static org.mockito.Mockito.*;
3236

3337
@QuarkusTest
3438
@TestSecurity(user = "test-user")
3539
class SclValidatorServerEndpointTest {
3640
private static final LinkedBlockingDeque<ValidationError> validationErrors = new LinkedBlockingDeque<>();
41+
private static final LinkedBlockingDeque<ErrorMessage> errorQueue = new LinkedBlockingDeque<>();
3742

3843
@InjectMock
3944
private SclValidatorService sclValidatorService;
@@ -60,11 +65,32 @@ void validate_WhenCalled_ThenExpectedResponseIsRetrieved() throws Exception {
6065
}
6166
}
6267

68+
@Test
69+
void validate_WhenCalledWithInvalidText_ThenExceptionThrown() throws Exception {
70+
try (Session session = ContainerProvider.getWebSocketContainer().connectToServer(ErrorClient.class, uri)) {
71+
session.getAsyncRemote().sendText("Invalid Message");
72+
73+
var errorMessage = errorQueue.poll(10, TimeUnit.SECONDS);
74+
assertEquals(WEBSOCKET_DECODER_ERROR_CODE, errorMessage.getCode());
75+
assertEquals(0, errorQueue.size());
76+
}
77+
}
78+
6379
@ClientEndpoint(decoders = SclValidateResponseDecoder.class)
6480
private static class Client {
6581
@OnMessage
6682
public void onMessage(SclValidateResponse response) {
6783
validationErrors.addAll(response.getValidationErrorList());
6884
}
6985
}
86+
87+
@ClientEndpoint(decoders = ErrorResponseDecoder.class)
88+
static class ErrorClient {
89+
@OnMessage
90+
public void onMessage(ErrorResponse response) {
91+
if (response.getErrorMessages().size() > 0) {
92+
errorQueue.addAll(response.getErrorMessages());
93+
}
94+
}
95+
}
7096
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ SPDX-License-Identifier: Apache-2.0
2424
<sonarqube-plugin.version>3.2.0</sonarqube-plugin.version>
2525

2626
<compas.scl.xsd.version>0.0.4</compas.scl.xsd.version>
27-
<compas.core.version>0.10.0</compas.core.version>
27+
<compas.core.version>0.10.1</compas.core.version>
2828

2929
<quarkus.platform.version>2.13.4.Final</quarkus.platform.version>
3030
<slf4j.version>2.0.3</slf4j.version>

0 commit comments

Comments
 (0)