Skip to content

Commit

Permalink
Merge pull request #189 from com-pas/develop
Browse files Browse the repository at this point in the history
New release
  • Loading branch information
Dennis Labordus authored Nov 10, 2022
2 parents 31f5182 + 89d00c5 commit eeab93c
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ SPDX-License-Identifier: Apache-2.0
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<compas.scl.xsd.version>0.0.4</compas.scl.xsd.version>
<quarkus.platform.version>2.13.3.Final</quarkus.platform.version>
<quarkus.platform.version>2.14.0.Final</quarkus.platform.version>
<slf4j.version>2.0.3</slf4j.version>
<jaxb.bind.version>2.3.7</jaxb.bind.version>
<openpojo.version>0.9.1</openpojo.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,23 +25,12 @@ public void execute(Session session, EventExecutor<T> 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> {
T execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -45,4 +46,13 @@ public static <T> T decode(String message, Class<T> jaxbClass) {
}
}

public static void handleException(Session session, Throwable throwable) {
var response = new ErrorResponse();
if (throwable instanceof CompasException) {
response.addErrorMessage(((CompasException) throwable).getErrorCode(), throwable.getMessage());
} else {
response.addErrorMessage(WEBSOCKET_GENERAL_ERROR_CODE, throwable.getMessage());
}
session.getAsyncRemote().sendObject(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

@ExtendWith(MockitoExtension.class)
class WebsocketHandlerTest {

@Test
void execute_WhenCalledSuccessful_ThenResponseSendToWebsocket() {
var message = "Some message";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
"<compas-commons:ErrorResponse xmlns:compas-commons=\"" + COMPAS_COMMONS_V1_NS_URI + "\">" +
"<compas-commons:ErrorMessage>" +
"<compas-commons:Code>" + errorCode + "</compas-commons:Code>" +
"<compas-commons:Message>" + errorMessage + "</compas-commons:Message>" +
"</compas-commons:ErrorMessage>" +
"</compas-commons:ErrorResponse>";
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 = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
"<compas-commons:ErrorResponse xmlns:compas-commons=\"" + COMPAS_COMMONS_V1_NS_URI + "\">" +
"<compas-commons:ErrorMessage>" +
"<compas-commons:Code>" + errorCode + "</compas-commons:Code>" +
"<compas-commons:Message>" + errorMessage + "</compas-commons:Message>" +
"</compas-commons:ErrorMessage>" +
"</compas-commons:ErrorResponse>";

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 = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
"<compas-commons:InvalidResponse xmlns:compas-commons=\"" + COMPAS_COMMONS_V1_NS_URI + "\">" +
"</compas-commons:InvalidResponse>";

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<ErrorResponse> 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());
}
}

0 comments on commit eeab93c

Please sign in to comment.