Skip to content

Commit

Permalink
Merge pull request #192 from com-pas/develop
Browse files Browse the repository at this point in the history
New release
  • Loading branch information
Dennis Labordus authored Nov 14, 2022
2 parents eeab93c + 3aae5dd commit 83e3344
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// SPDX-License-Identifier: Apache-2.0
package org.lfenergy.compas.core.commons.model;

import javax.validation.constraints.NotBlank;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
Expand All @@ -11,8 +12,10 @@

@XmlAccessorType(XmlAccessType.FIELD)
public class ErrorMessage {
@NotBlank
@XmlElement(name = "Code", namespace = COMPAS_COMMONS_V1_NS_URI, required = true)
private String code;
@NotBlank
@XmlElement(name = "Message", namespace = COMPAS_COMMONS_V1_NS_URI, required = true)
private String message;
@XmlElement(name = "Property", namespace = COMPAS_COMMONS_V1_NS_URI, required = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// SPDX-License-Identifier: Apache-2.0
package org.lfenergy.compas.core.commons.model;

import javax.validation.Valid;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
Expand All @@ -15,6 +16,7 @@
@XmlRootElement(name = "ErrorResponse", namespace = COMPAS_COMMONS_V1_NS_URI)
@XmlAccessorType(XmlAccessType.FIELD)
public class ErrorResponse {
@Valid
@XmlElement(name = "ErrorMessage", namespace = COMPAS_COMMONS_V1_NS_URI, required = true)
private List<ErrorMessage> errorMessages = new ArrayList<>();

Expand Down
14 changes: 14 additions & 0 deletions websocket-commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ SPDX-License-Identifier: Apache-2.0
<artifactId>commons</artifactId>
</dependency>

<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
</dependency>
<dependency>
<groupId>jakarta.websocket</groupId>
<artifactId>jakarta.websocket-api</artifactId>
Expand Down Expand Up @@ -54,6 +58,16 @@ SPDX-License-Identifier: Apache-2.0
<artifactId>jaxb-impl</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>jakarta.el</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.lfenergy.compas.core.commons.exception.CompasException;
import org.lfenergy.compas.core.commons.model.ErrorResponse;

import javax.validation.ConstraintViolationException;
import javax.validation.Validation;
import javax.websocket.Session;
import javax.xml.bind.JAXBContext;
import java.io.StringReader;
Expand Down Expand Up @@ -38,7 +40,19 @@ public static <T> T decode(String message, Class<T> jaxbClass) {
var jaxbContext = JAXBContext.newInstance(jaxbClass);
var unmarshaller = jaxbContext.createUnmarshaller();
var reader = new StringReader(message);
return jaxbClass.cast(unmarshaller.unmarshal(reader));
var jaxbObject = jaxbClass.cast(unmarshaller.unmarshal(reader));

try (var factory = Validation.buildDefaultValidatorFactory()) {
var validator = factory.getValidator();
var constraintViolations = validator.validate(jaxbObject);
if (!constraintViolations.isEmpty()) {
throw new ConstraintViolationException(constraintViolations);
}
}

return jaxbObject;
} catch (ConstraintViolationException exp) {
throw exp;
} catch (Exception exp) {
throw new CompasException(WEBSOCKET_DECODER_ERROR_CODE,
"Error unmarshalling to class '" + jaxbClass.getName() + "' from Websockets.",
Expand All @@ -49,7 +63,15 @@ 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());
var ce = (CompasException) throwable;
response.addErrorMessage(ce.getErrorCode(), ce.getMessage());
} else if (throwable instanceof ConstraintViolationException) {
var cve = (ConstraintViolationException) throwable;
cve.getConstraintViolations()
.forEach(constraintViolation ->
response.addErrorMessage(VALIDATION_ERROR,
constraintViolation.getMessage(),
constraintViolation.getPropertyPath().toString()));
} else {
response.addErrorMessage(WEBSOCKET_GENERAL_ERROR_CODE, throwable.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,28 @@
// SPDX-License-Identifier: Apache-2.0
package org.lfenergy.compas.core.websocket;

import org.hibernate.validator.internal.engine.path.PathImpl;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
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 org.mockito.junit.jupiter.MockitoExtension;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.websocket.RemoteEndpoint;
import javax.websocket.Session;
import java.util.Set;

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.*;

@ExtendWith(MockitoExtension.class)
class WebsocketSupportTest {
@Test
void constructor_WhenConstructorCalled_ThenShouldThrowExceptionCauseForbidden() {
Expand Down Expand Up @@ -72,6 +79,18 @@ void decode_WhenCalledWithCorrectXML_ThenObjectReturned() {
assertEquals(errorMessage, message.getMessage());
}

@Test
void decode_WhenCalledWithValidationErrors_ThenConstraintViolationExceptionThrown() {
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:Message>Some message</compas-commons:Message>" +
"</compas-commons:ErrorMessage>" +
"</compas-commons:ErrorResponse>";

var exception = assertThrows(ConstraintViolationException.class, () -> decode(xmlMessage, ErrorResponse.class));
assertEquals(1, exception.getConstraintViolations().size());
}

@Test
void decode_WhenCalledWithInvalidXML_ThenExceptionThrown() {
Expand All @@ -95,6 +114,24 @@ void handleException_WhenCalledWithCompasException_ThenErrorResponseSendToSessio
verifyErrorResponse(session, errorCode, errorMessage);
}

@Test
void handleException_WhenCalledWithConstraintViolationException_ThenErrorResponseSendToSession() {
var session = mockSession();

var errorMessage = "Error Message";
var path = PathImpl.createRootPath();

ConstraintViolation<String> constraintViolation = mock(ConstraintViolation.class);
when(constraintViolation.getMessage()).thenReturn(errorMessage);
when(constraintViolation.getPropertyPath()).thenReturn(path);

var exception = new ConstraintViolationException(Set.of(constraintViolation));

handleException(session, exception);

verifyErrorResponse(session, VALIDATION_ERROR, errorMessage);
}

@Test
void handleException_WhenCalledWithRuntimeException_ThenErrorResponseSendToSession() {
var errorMessage = "Error Message";
Expand All @@ -115,7 +152,7 @@ private Session mockSession() {
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());
verify(session.getAsyncRemote()).sendObject(captor.capture());
var response = captor.getValue();
assertEquals(1, response.getErrorMessages().size());
var message = response.getErrorMessages().get(0);
Expand Down

0 comments on commit 83e3344

Please sign in to comment.