diff --git a/app/pom.xml b/app/pom.xml index 9fa9ac2..ae1207f 100644 --- a/app/pom.xml +++ b/app/pom.xml @@ -38,7 +38,11 @@ SPDX-License-Identifier: Apache-2.0 org.lfenergy.compas.core - jaxrs-commons + rest-commons + + + org.lfenergy.compas.core + websocket-commons diff --git a/app/src/main/java/org/lfenergy/compas/scl/validator/rest/exception/NsdocNotFoundExceptionHandler.java b/app/src/main/java/org/lfenergy/compas/scl/validator/rest/exception/NsdocNotFoundExceptionHandler.java index 20f2d72..8a49eb9 100644 --- a/app/src/main/java/org/lfenergy/compas/scl/validator/rest/exception/NsdocNotFoundExceptionHandler.java +++ b/app/src/main/java/org/lfenergy/compas/scl/validator/rest/exception/NsdocNotFoundExceptionHandler.java @@ -3,7 +3,7 @@ // SPDX-License-Identifier: Apache-2.0 package org.lfenergy.compas.scl.validator.rest.exception; -import org.lfenergy.compas.core.jaxrs.model.ErrorResponse; +import org.lfenergy.compas.core.commons.model.ErrorResponse; import org.lfenergy.compas.scl.validator.exception.NsdocFileNotFoundException; import javax.ws.rs.core.Response; diff --git a/app/src/main/java/org/lfenergy/compas/scl/validator/rest/v1/SclValidatorServerEndpoint.java b/app/src/main/java/org/lfenergy/compas/scl/validator/rest/v1/SclValidatorServerEndpoint.java index d8747ec..2b3bfe7 100644 --- a/app/src/main/java/org/lfenergy/compas/scl/validator/rest/v1/SclValidatorServerEndpoint.java +++ b/app/src/main/java/org/lfenergy/compas/scl/validator/rest/v1/SclValidatorServerEndpoint.java @@ -5,6 +5,7 @@ import io.quarkus.security.Authenticated; import io.vertx.mutiny.core.eventbus.EventBus; +import org.lfenergy.compas.core.websocket.ErrorResponseEncoder; import org.lfenergy.compas.scl.extensions.model.SclFileType; import org.lfenergy.compas.scl.validator.rest.v1.event.SclValidatorEventRequest; import org.lfenergy.compas.scl.validator.rest.v1.model.SclValidateRequest; @@ -21,13 +22,14 @@ import javax.websocket.server.PathParam; import javax.websocket.server.ServerEndpoint; +import static org.lfenergy.compas.core.websocket.WebsocketSupport.handleException; import static org.lfenergy.compas.scl.validator.rest.SclResourceConstants.TYPE_PATH_PARAM; @Authenticated @ApplicationScoped @ServerEndpoint(value = "/validate-ws/v1/{" + TYPE_PATH_PARAM + "}", decoders = {SclValidateRequestDecoder.class, SclValidateResponseDecoder.class}, - encoders = {SclValidateRequestEncoder.class, SclValidateResponseEncoder.class}) + encoders = {SclValidateRequestEncoder.class, SclValidateResponseEncoder.class, ErrorResponseEncoder.class}) public class SclValidatorServerEndpoint { private static final Logger LOGGER = LoggerFactory.getLogger(SclValidatorServerEndpoint.class); @@ -53,6 +55,7 @@ public void onMessage(Session session, SclValidateRequest request, @PathParam(TY @OnError public void onError(Session session, @PathParam(TYPE_PATH_PARAM) String type, Throwable throwable) { LOGGER.warn("Error with session {} for type {}.", session.getId(), type, throwable); + handleException(session, throwable); } @OnClose diff --git a/app/src/main/java/org/lfenergy/compas/scl/validator/rest/v1/event/SclValidatorEventHandler.java b/app/src/main/java/org/lfenergy/compas/scl/validator/rest/v1/event/SclValidatorEventHandler.java index ce8b6c9..84edcf8 100644 --- a/app/src/main/java/org/lfenergy/compas/scl/validator/rest/v1/event/SclValidatorEventHandler.java +++ b/app/src/main/java/org/lfenergy/compas/scl/validator/rest/v1/event/SclValidatorEventHandler.java @@ -4,6 +4,7 @@ package org.lfenergy.compas.scl.validator.rest.v1.event; import io.quarkus.vertx.ConsumeEvent; +import org.lfenergy.compas.core.websocket.WebsocketHandler; import org.lfenergy.compas.scl.validator.rest.v1.model.SclValidateResponse; import org.lfenergy.compas.scl.validator.service.SclValidatorService; @@ -24,10 +25,10 @@ public SclValidatorEventHandler(SclValidatorService sclValidatorService) { @ConsumeEvent(value = "validate-ws", blocking = true) public void validateWebsocketsEvent(SclValidatorEventRequest request) { - var response = new SclValidateResponse(); - response.setValidationErrorList(sclValidatorService.validate(request.getType(), request.getSclData())); - - var session = request.getSession(); - session.getAsyncRemote().sendObject(response); + new WebsocketHandler().execute(request.getSession(), () -> { + var response = new SclValidateResponse(); + response.setValidationErrorList(sclValidatorService.validate(request.getType(), request.getSclData())); + return response; + }); } } diff --git a/app/src/main/java/org/lfenergy/compas/scl/validator/rest/v1/websocket/SclValidateRequestDecoder.java b/app/src/main/java/org/lfenergy/compas/scl/validator/rest/v1/websocket/SclValidateRequestDecoder.java index 24f577f..99a5ef0 100644 --- a/app/src/main/java/org/lfenergy/compas/scl/validator/rest/v1/websocket/SclValidateRequestDecoder.java +++ b/app/src/main/java/org/lfenergy/compas/scl/validator/rest/v1/websocket/SclValidateRequestDecoder.java @@ -3,22 +3,11 @@ // SPDX-License-Identifier: Apache-2.0 package org.lfenergy.compas.scl.validator.rest.v1.websocket; -import org.lfenergy.compas.scl.validator.exception.SclValidatorException; +import org.lfenergy.compas.core.websocket.AbstractDecoder; +import org.lfenergy.compas.core.websocket.WebsocketSupport; import org.lfenergy.compas.scl.validator.rest.v1.model.SclValidateRequest; -import javax.websocket.Decoder; -import javax.websocket.EndpointConfig; -import javax.xml.bind.JAXBContext; -import java.io.StringReader; - -import static org.lfenergy.compas.scl.validator.exception.SclValidatorErrorCode.WEBSOCKET_DECODER_ERROR_CODE; - -public class SclValidateRequestDecoder implements Decoder.Text { - @Override - public void init(EndpointConfig endpointConfig) { - // do nothing. - } - +public class SclValidateRequestDecoder extends AbstractDecoder { @Override public boolean willDecode(String message) { return (message != null); @@ -26,20 +15,6 @@ public boolean willDecode(String message) { @Override public SclValidateRequest decode(String message) { - try { - var jaxbContext = JAXBContext.newInstance(SclValidateRequest.class); - var unmarshaller = jaxbContext.createUnmarshaller(); - var reader = new StringReader(message); - return (SclValidateRequest) unmarshaller.unmarshal(reader); - } catch (Exception exp) { - throw new SclValidatorException(WEBSOCKET_DECODER_ERROR_CODE, - "Error unmarshalling SCL Validate Request from Websockets.", - exp); - } - } - - @Override - public void destroy() { - // do nothing. + return WebsocketSupport.decode(message, SclValidateRequest.class); } } diff --git a/app/src/main/java/org/lfenergy/compas/scl/validator/rest/v1/websocket/SclValidateRequestEncoder.java b/app/src/main/java/org/lfenergy/compas/scl/validator/rest/v1/websocket/SclValidateRequestEncoder.java index a3759c6..7f936e9 100644 --- a/app/src/main/java/org/lfenergy/compas/scl/validator/rest/v1/websocket/SclValidateRequestEncoder.java +++ b/app/src/main/java/org/lfenergy/compas/scl/validator/rest/v1/websocket/SclValidateRequestEncoder.java @@ -3,40 +3,13 @@ // SPDX-License-Identifier: Apache-2.0 package org.lfenergy.compas.scl.validator.rest.v1.websocket; -import org.lfenergy.compas.scl.validator.exception.SclValidatorException; +import org.lfenergy.compas.core.websocket.AbstractEncoder; +import org.lfenergy.compas.core.websocket.WebsocketSupport; import org.lfenergy.compas.scl.validator.rest.v1.model.SclValidateRequest; -import javax.websocket.Encoder; -import javax.websocket.EndpointConfig; -import javax.xml.bind.JAXBContext; -import java.io.StringWriter; - -import static org.lfenergy.compas.scl.validator.exception.SclValidatorErrorCode.WEBSOCKET_ENCODER_ERROR_CODE; - -public class SclValidateRequestEncoder implements Encoder.Text { - @Override - public void init(EndpointConfig endpointConfig) { - // do nothing. - } - - @Override - public String encode(SclValidateRequest request) { - try { - var jaxbContext = JAXBContext.newInstance(SclValidateRequest.class); - var marshaller = jaxbContext.createMarshaller(); - - var st = new StringWriter(); - marshaller.marshal(request, st); - return st.toString(); - } catch (Exception exp) { - throw new SclValidatorException(WEBSOCKET_ENCODER_ERROR_CODE, - "Error marshalling SCL Validate Request from Websockets.", - exp); - } - } - +public class SclValidateRequestEncoder extends AbstractEncoder { @Override - public void destroy() { - // do nothing. + public String encode(SclValidateRequest jaxbObject) { + return WebsocketSupport.encode(jaxbObject, SclValidateRequest.class); } } diff --git a/app/src/main/java/org/lfenergy/compas/scl/validator/rest/v1/websocket/SclValidateResponseDecoder.java b/app/src/main/java/org/lfenergy/compas/scl/validator/rest/v1/websocket/SclValidateResponseDecoder.java index 4cbda9c..ea5e757 100644 --- a/app/src/main/java/org/lfenergy/compas/scl/validator/rest/v1/websocket/SclValidateResponseDecoder.java +++ b/app/src/main/java/org/lfenergy/compas/scl/validator/rest/v1/websocket/SclValidateResponseDecoder.java @@ -3,22 +3,11 @@ // SPDX-License-Identifier: Apache-2.0 package org.lfenergy.compas.scl.validator.rest.v1.websocket; -import org.lfenergy.compas.scl.validator.exception.SclValidatorException; +import org.lfenergy.compas.core.websocket.AbstractDecoder; +import org.lfenergy.compas.core.websocket.WebsocketSupport; import org.lfenergy.compas.scl.validator.rest.v1.model.SclValidateResponse; -import javax.websocket.Decoder; -import javax.websocket.EndpointConfig; -import javax.xml.bind.JAXBContext; -import java.io.StringReader; - -import static org.lfenergy.compas.scl.validator.exception.SclValidatorErrorCode.WEBSOCKET_DECODER_ERROR_CODE; - -public class SclValidateResponseDecoder implements Decoder.Text { - @Override - public void init(EndpointConfig endpointConfig) { - // do nothing. - } - +public class SclValidateResponseDecoder extends AbstractDecoder { @Override public boolean willDecode(String message) { return (message != null); @@ -26,20 +15,6 @@ public boolean willDecode(String message) { @Override public SclValidateResponse decode(String message) { - try { - var jaxbContext = JAXBContext.newInstance(SclValidateResponse.class); - var unmarshaller = jaxbContext.createUnmarshaller(); - var reader = new StringReader(message); - return (SclValidateResponse) unmarshaller.unmarshal(reader); - } catch (Exception exp) { - throw new SclValidatorException(WEBSOCKET_DECODER_ERROR_CODE, - "Error unmarshalling SCL Validate Response from Websockets.", - exp); - } - } - - @Override - public void destroy() { - // do nothing. + return WebsocketSupport.decode(message, SclValidateResponse.class); } } diff --git a/app/src/main/java/org/lfenergy/compas/scl/validator/rest/v1/websocket/SclValidateResponseEncoder.java b/app/src/main/java/org/lfenergy/compas/scl/validator/rest/v1/websocket/SclValidateResponseEncoder.java index 751768b..24a29e1 100644 --- a/app/src/main/java/org/lfenergy/compas/scl/validator/rest/v1/websocket/SclValidateResponseEncoder.java +++ b/app/src/main/java/org/lfenergy/compas/scl/validator/rest/v1/websocket/SclValidateResponseEncoder.java @@ -3,40 +3,13 @@ // SPDX-License-Identifier: Apache-2.0 package org.lfenergy.compas.scl.validator.rest.v1.websocket; -import org.lfenergy.compas.scl.validator.exception.SclValidatorException; +import org.lfenergy.compas.core.websocket.AbstractEncoder; +import org.lfenergy.compas.core.websocket.WebsocketSupport; import org.lfenergy.compas.scl.validator.rest.v1.model.SclValidateResponse; -import javax.websocket.Encoder; -import javax.websocket.EndpointConfig; -import javax.xml.bind.JAXBContext; -import java.io.StringWriter; - -import static org.lfenergy.compas.scl.validator.exception.SclValidatorErrorCode.WEBSOCKET_ENCODER_ERROR_CODE; - -public class SclValidateResponseEncoder implements Encoder.Text { - @Override - public void init(EndpointConfig endpointConfig) { - // do nothing. - } - - @Override - public String encode(SclValidateResponse response) { - try { - var jaxbContext = JAXBContext.newInstance(SclValidateResponse.class); - var marshaller = jaxbContext.createMarshaller(); - - var st = new StringWriter(); - marshaller.marshal(response, st); - return st.toString(); - } catch (Exception exp) { - throw new SclValidatorException(WEBSOCKET_ENCODER_ERROR_CODE, - "Error marshalling SCL Validate Response from Websockets.", - exp); - } - } - +public class SclValidateResponseEncoder extends AbstractEncoder { @Override - public void destroy() { - // do nothing. + public String encode(SclValidateResponse jaxbObject) { + return WebsocketSupport.encode(jaxbObject, SclValidateResponse.class); } } diff --git a/app/src/test/java/org/lfenergy/compas/scl/validator/rest/CompasSclValidatorConfigurationTest.java b/app/src/test/java/org/lfenergy/compas/scl/validator/rest/CompasSclValidatorConfigurationTest.java index 9ddea95..187157c 100644 --- a/app/src/test/java/org/lfenergy/compas/scl/validator/rest/CompasSclValidatorConfigurationTest.java +++ b/app/src/test/java/org/lfenergy/compas/scl/validator/rest/CompasSclValidatorConfigurationTest.java @@ -12,7 +12,8 @@ import java.nio.file.Path; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) class CompasSclValidatorConfigurationTest { @@ -32,7 +33,7 @@ void createOclFileCollector_WhenCalled_ThenObjectReturned() { assertNotNull(new CompasSclValidatorConfiguration().createOclFileCollector(validatorProperties)); - verify(validatorProperties, times(1)).oclCustomDirectory(); + verify(validatorProperties).oclCustomDirectory(); } @Test @@ -43,7 +44,7 @@ void createSclRiseClipseValidator_WhenCalled_ThenObjectReturned() { oclFileCollector, validatorProperties)); - verify(validatorProperties, times(1)).tempDirectory(); + verify(validatorProperties).tempDirectory(); } @Test @@ -58,6 +59,6 @@ void createNsdocFinder_WhenCalled_ThenObjectReturned() { assertNotNull(new CompasSclValidatorConfiguration().createNsdocFinder( validatorProperties)); - verify(validatorProperties, times(1)).nsdocDirectory(); + verify(validatorProperties).nsdocDirectory(); } } diff --git a/app/src/test/java/org/lfenergy/compas/scl/validator/rest/exception/NsdocNotFoundExceptionHandlerTest.java b/app/src/test/java/org/lfenergy/compas/scl/validator/rest/exception/NsdocNotFoundExceptionHandlerTest.java index 81d548a..05f50f5 100644 --- a/app/src/test/java/org/lfenergy/compas/scl/validator/rest/exception/NsdocNotFoundExceptionHandlerTest.java +++ b/app/src/test/java/org/lfenergy/compas/scl/validator/rest/exception/NsdocNotFoundExceptionHandlerTest.java @@ -4,7 +4,7 @@ package org.lfenergy.compas.scl.validator.rest.exception; import org.junit.jupiter.api.Test; -import org.lfenergy.compas.core.jaxrs.model.ErrorResponse; +import org.lfenergy.compas.core.commons.model.ErrorResponse; import org.lfenergy.compas.scl.validator.exception.NsdocFileNotFoundException; import static javax.ws.rs.core.Response.Status.NOT_FOUND; diff --git a/app/src/test/java/org/lfenergy/compas/scl/validator/rest/v1/NsdocResourceTest.java b/app/src/test/java/org/lfenergy/compas/scl/validator/rest/v1/NsdocResourceTest.java index 2d08d69..5d42a85 100644 --- a/app/src/test/java/org/lfenergy/compas/scl/validator/rest/v1/NsdocResourceTest.java +++ b/app/src/test/java/org/lfenergy/compas/scl/validator/rest/v1/NsdocResourceTest.java @@ -9,7 +9,6 @@ import io.quarkus.test.security.TestSecurity; import io.restassured.http.ContentType; import org.junit.jupiter.api.Test; -import org.lfenergy.compas.core.jaxrs.JaxrsConstants; import org.lfenergy.compas.scl.validator.exception.NsdocFileNotFoundException; import org.lfenergy.compas.scl.validator.exception.SclValidatorErrorCode; import org.lfenergy.compas.scl.validator.model.NsdocFile; @@ -22,9 +21,11 @@ import static io.restassured.path.xml.config.XmlPathConfig.xmlPathConfig; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.lfenergy.compas.core.commons.CommonConstants.COMPAS_COMMONS_V1_NS_URI; import static org.lfenergy.compas.scl.validator.SclValidatorConstants.SCL_VALIDATOR_SERVICE_V1_NS_URI; import static org.lfenergy.compas.scl.validator.rest.SclResourceConstants.ID_PARAM; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; @QuarkusTest @TestHTTPEndpoint(NsdocResource.class) @@ -52,7 +53,7 @@ void list_WhenCalled_ThenListReturned() { var files = xmlPath.getList("svs:NsdocListResponse.svs:NsdocFile"); assertNotNull(files); assertEquals(1, files.size()); - verify(nsdocService, times(1)).list(); + verify(nsdocService).list(); } @Test @@ -78,7 +79,7 @@ void get_WhenCalled_ThenContentReturned() { var nsdocFile = xmlPath.getString("svs:NsdocResponse.svs:NsdocFile"); assertNotNull(nsdocFile); assertEquals(result, nsdocFile); - verify(nsdocService, times(1)).get(id); + verify(nsdocService).get(id); } @Test @@ -99,7 +100,7 @@ void get_WhenCalledWithUnknownFile_Then404Returned() { .response(); var xmlPath = response.xmlPath() - .using(xmlPathConfig().declaredNamespace("commons", JaxrsConstants.COMPAS_COMMONS_V1_NS_URI)); + .using(xmlPathConfig().declaredNamespace("commons", COMPAS_COMMONS_V1_NS_URI)); var messages = xmlPath.getList("commons:ErrorResponse.commons:ErrorMessage"); assertNotNull(messages); assertEquals(1, messages.size()); @@ -107,6 +108,6 @@ void get_WhenCalledWithUnknownFile_Then404Returned() { var code = xmlPath.getString("commons:ErrorResponse.commons:ErrorMessage.commons:Code"); assertEquals(SclValidatorErrorCode.NSDOC_FILE_NOT_FOUND, code); - verify(nsdocService, times(1)).get(id); + verify(nsdocService).get(id); } } diff --git a/app/src/test/java/org/lfenergy/compas/scl/validator/rest/v1/SclValidatorResourceTest.java b/app/src/test/java/org/lfenergy/compas/scl/validator/rest/v1/SclValidatorResourceTest.java index 04f1a75..9644ad9 100644 --- a/app/src/test/java/org/lfenergy/compas/scl/validator/rest/v1/SclValidatorResourceTest.java +++ b/app/src/test/java/org/lfenergy/compas/scl/validator/rest/v1/SclValidatorResourceTest.java @@ -24,7 +24,8 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.lfenergy.compas.scl.validator.SclValidatorConstants.SCL_VALIDATOR_SERVICE_V1_NS_URI; import static org.lfenergy.compas.scl.validator.rest.SclResourceConstants.TYPE_PATH_PARAM; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; @QuarkusTest @TestHTTPEndpoint(SclValidatorResource.class) @@ -58,6 +59,6 @@ void validate_WhenCalled_ThenExpectedResponseIsRetrieved() throws IOException { var errors = xmlPath.getList("svs:SclValidateResponse.svs:ValidationErrors"); assertNotNull(errors); assertEquals(1, errors.size()); - verify(sclValidatorService, times(1)).validate(sclFileTye, request.getSclData()); + verify(sclValidatorService).validate(sclFileTye, request.getSclData()); } } diff --git a/app/src/test/java/org/lfenergy/compas/scl/validator/rest/v1/SclValidatorServerEndpointTest.java b/app/src/test/java/org/lfenergy/compas/scl/validator/rest/v1/SclValidatorServerEndpointTest.java index 6b24a79..747ed2f 100644 --- a/app/src/test/java/org/lfenergy/compas/scl/validator/rest/v1/SclValidatorServerEndpointTest.java +++ b/app/src/test/java/org/lfenergy/compas/scl/validator/rest/v1/SclValidatorServerEndpointTest.java @@ -8,6 +8,9 @@ import io.quarkus.test.junit.mockito.InjectMock; import io.quarkus.test.security.TestSecurity; import org.junit.jupiter.api.Test; +import org.lfenergy.compas.core.commons.model.ErrorMessage; +import org.lfenergy.compas.core.commons.model.ErrorResponse; +import org.lfenergy.compas.core.websocket.ErrorResponseDecoder; import org.lfenergy.compas.scl.extensions.model.SclFileType; import org.lfenergy.compas.scl.validator.model.ValidationError; import org.lfenergy.compas.scl.validator.rest.v1.model.SclValidateRequest; @@ -28,12 +31,15 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.mockito.Mockito.*; +import static org.lfenergy.compas.core.commons.exception.CompasErrorCode.WEBSOCKET_DECODER_ERROR_CODE; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; @QuarkusTest @TestSecurity(user = "test-user") class SclValidatorServerEndpointTest { private static final LinkedBlockingDeque validationErrors = new LinkedBlockingDeque<>(); + private static final LinkedBlockingDeque errorQueue = new LinkedBlockingDeque<>(); @InjectMock private SclValidatorService sclValidatorService; @@ -56,7 +62,18 @@ void validate_WhenCalled_ThenExpectedResponseIsRetrieved() throws Exception { assertNotNull(validationErrors.poll(10, TimeUnit.SECONDS)); assertEquals(0, validationErrors.size()); - verify(sclValidatorService, times(1)).validate(sclFileTye, request.getSclData()); + verify(sclValidatorService).validate(sclFileTye, request.getSclData()); + } + } + + @Test + void validate_WhenCalledWithInvalidText_ThenExceptionThrown() throws Exception { + try (Session session = ContainerProvider.getWebSocketContainer().connectToServer(ErrorClient.class, uri)) { + session.getAsyncRemote().sendText("Invalid Message"); + + var errorMessage = errorQueue.poll(10, TimeUnit.SECONDS); + assertEquals(WEBSOCKET_DECODER_ERROR_CODE, errorMessage.getCode()); + assertEquals(0, errorQueue.size()); } } @@ -67,4 +84,14 @@ public void onMessage(SclValidateResponse response) { validationErrors.addAll(response.getValidationErrorList()); } } + + @ClientEndpoint(decoders = ErrorResponseDecoder.class) + static class ErrorClient { + @OnMessage + public void onMessage(ErrorResponse response) { + if (response.getErrorMessages().size() > 0) { + errorQueue.addAll(response.getErrorMessages()); + } + } + } } diff --git a/app/src/test/java/org/lfenergy/compas/scl/validator/rest/v1/event/SclValidatorEventHandlerTest.java b/app/src/test/java/org/lfenergy/compas/scl/validator/rest/v1/event/SclValidatorEventHandlerTest.java index de1b5e1..48403af 100644 --- a/app/src/test/java/org/lfenergy/compas/scl/validator/rest/v1/event/SclValidatorEventHandlerTest.java +++ b/app/src/test/java/org/lfenergy/compas/scl/validator/rest/v1/event/SclValidatorEventHandlerTest.java @@ -5,6 +5,8 @@ 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.lfenergy.compas.scl.extensions.model.SclFileType; import org.lfenergy.compas.scl.validator.model.ValidationError; import org.lfenergy.compas.scl.validator.rest.v1.model.SclValidateResponse; @@ -20,7 +22,10 @@ import java.util.ArrayList; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.Mockito.*; +import static org.lfenergy.compas.core.commons.exception.CompasErrorCode.WEBSOCKET_GENERAL_ERROR_CODE; +import static org.lfenergy.compas.scl.validator.exception.SclValidatorErrorCode.LOADING_OCL_FILES_FAILED; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) class SclValidatorEventHandlerTest { @@ -39,25 +44,73 @@ void validateWebsocketsEvent_WhenCalled_ThenExpectedCallsAreMade() { var session = Mockito.mock(Session.class); var async = Mockito.mock(RemoteEndpoint.Async.class); when(session.getAsyncRemote()).thenReturn(async); - - var request = Mockito.mock(SclValidatorEventRequest.class); - when(request.getSession()).thenReturn(session); - when(request.getType()).thenReturn(type); - when(request.getSclData()).thenReturn(sclData); + var request = new SclValidatorEventRequest(session, type, sclData); when(service.validate(type, sclData)).thenReturn(veList); eventHandler.validateWebsocketsEvent(request); - verify(session, times(1)).getAsyncRemote(); + verify(session).getAsyncRemote(); ArgumentCaptor captor = ArgumentCaptor.forClass(SclValidateResponse.class); - verify(async, times(1)).sendObject(captor.capture()); + verify(async).sendObject(captor.capture()); var response = captor.getValue(); assertEquals(veList, response.getValidationErrorList()); - verify(service, times(1)).validate(type, sclData); - verify(request, times(1)).getSession(); - verify(request, times(1)).getType(); - verify(request, times(1)).getSclData(); + verify(service).validate(type, sclData); + } + + @Test + void validateWebsocketsEvent_WhenCalledAndCompasExceptionThrownByService_ThenErrorResponseReturned() { + var type = SclFileType.CID; + var sclData = "Some SCL Data"; + var errorMessage = "Some Error"; + + var session = Mockito.mock(Session.class); + var async = Mockito.mock(RemoteEndpoint.Async.class); + when(session.getAsyncRemote()).thenReturn(async); + var request = new SclValidatorEventRequest(session, type, sclData); + + when(service.validate(type, sclData)) + .thenThrow(new CompasException(LOADING_OCL_FILES_FAILED, errorMessage)); + + eventHandler.validateWebsocketsEvent(request); + + verify(session).getAsyncRemote(); + ArgumentCaptor captor = ArgumentCaptor.forClass(ErrorResponse.class); + verify(async).sendObject(captor.capture()); + var response = captor.getValue(); + assertEquals(1, response.getErrorMessages().size()); + var message = response.getErrorMessages().get(0); + assertEquals(LOADING_OCL_FILES_FAILED, message.getCode()); + assertEquals(errorMessage, message.getMessage()); + + verify(service).validate(type, sclData); + } + + @Test + void validateWebsocketsEvent_WhenCalledAndRuntimeExceptionThrownByService_ThenErrorResponseReturned() { + var type = SclFileType.CID; + var sclData = "Some SCL Data"; + var errorMessage = "Some Error"; + + var session = Mockito.mock(Session.class); + var async = Mockito.mock(RemoteEndpoint.Async.class); + when(session.getAsyncRemote()).thenReturn(async); + var request = new SclValidatorEventRequest(session, type, sclData); + + when(service.validate(type, sclData)).thenThrow(new RuntimeException(errorMessage)); + + eventHandler.validateWebsocketsEvent(request); + + verify(session).getAsyncRemote(); + ArgumentCaptor captor = ArgumentCaptor.forClass(ErrorResponse.class); + verify(async).sendObject(captor.capture()); + var response = captor.getValue(); + assertEquals(1, response.getErrorMessages().size()); + var message = response.getErrorMessages().get(0); + assertEquals(WEBSOCKET_GENERAL_ERROR_CODE, message.getCode()); + assertEquals(errorMessage, message.getMessage()); + + verify(service).validate(type, sclData); } } diff --git a/app/src/test/java/org/lfenergy/compas/scl/validator/rest/v1/websocket/SclValidateRequestDecoderTest.java b/app/src/test/java/org/lfenergy/compas/scl/validator/rest/v1/websocket/SclValidateRequestDecoderTest.java index 39e7f8c..27f2082 100644 --- a/app/src/test/java/org/lfenergy/compas/scl/validator/rest/v1/websocket/SclValidateRequestDecoderTest.java +++ b/app/src/test/java/org/lfenergy/compas/scl/validator/rest/v1/websocket/SclValidateRequestDecoderTest.java @@ -6,13 +6,13 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.lfenergy.compas.scl.validator.exception.SclValidatorException; +import org.lfenergy.compas.core.commons.exception.CompasException; import javax.xml.bind.UnmarshalException; import static org.junit.jupiter.api.Assertions.*; +import static org.lfenergy.compas.core.commons.exception.CompasErrorCode.WEBSOCKET_DECODER_ERROR_CODE; import static org.lfenergy.compas.scl.validator.SclValidatorConstants.SCL_VALIDATOR_SERVICE_V1_NS_URI; -import static org.lfenergy.compas.scl.validator.exception.SclValidatorErrorCode.WEBSOCKET_DECODER_ERROR_CODE; class SclValidateRequestDecoderTest { private SclValidateRequestDecoder decoder; @@ -54,16 +54,7 @@ void decode_WhenCalledWithWrongXML_ThenExceptionThrown() { + "" + ""; - var exception = assertThrows(SclValidatorException.class, () -> decoder.decode(message)); - assertEquals(WEBSOCKET_DECODER_ERROR_CODE, exception.getErrorCode()); - assertEquals(UnmarshalException.class, exception.getCause().getClass()); - } - - @Test - void decode_WhenCalledWithInCorrectMessage_ThenExceptionThrown() { - var message = "Incorrect Meesage"; - - var exception = assertThrows(SclValidatorException.class, () -> decoder.decode(message)); + var exception = assertThrows(CompasException.class, () -> decoder.decode(message)); assertEquals(WEBSOCKET_DECODER_ERROR_CODE, exception.getErrorCode()); assertEquals(UnmarshalException.class, exception.getCause().getClass()); } diff --git a/app/src/test/java/org/lfenergy/compas/scl/validator/rest/v1/websocket/SclValidateRequestEncoderTest.java b/app/src/test/java/org/lfenergy/compas/scl/validator/rest/v1/websocket/SclValidateRequestEncoderTest.java index cc3e574..af33064 100644 --- a/app/src/test/java/org/lfenergy/compas/scl/validator/rest/v1/websocket/SclValidateRequestEncoderTest.java +++ b/app/src/test/java/org/lfenergy/compas/scl/validator/rest/v1/websocket/SclValidateRequestEncoderTest.java @@ -6,12 +6,11 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.lfenergy.compas.scl.validator.exception.SclValidatorException; import org.lfenergy.compas.scl.validator.rest.v1.model.SclValidateRequest; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.lfenergy.compas.scl.validator.SclValidatorConstants.SCL_VALIDATOR_SERVICE_V1_NS_URI; -import static org.lfenergy.compas.scl.validator.exception.SclValidatorErrorCode.WEBSOCKET_ENCODER_ERROR_CODE; class SclValidateRequestEncoderTest { private SclValidateRequestEncoder encoder; @@ -38,13 +37,6 @@ void encode_WhenCalledWithRequest_ThenRequestConvertedToString() { assertEquals(expectedResult, result); } - @Test - void encode_WhenCalledWithNull_ThenExceptionThrown() { - var exception = assertThrows(SclValidatorException.class, () -> encoder.encode(null)); - assertEquals(WEBSOCKET_ENCODER_ERROR_CODE, exception.getErrorCode()); - assertEquals(IllegalArgumentException.class, exception.getCause().getClass()); - } - @AfterEach void destroy() { encoder.destroy(); diff --git a/app/src/test/java/org/lfenergy/compas/scl/validator/rest/v1/websocket/SclValidateResponseDecoderTest.java b/app/src/test/java/org/lfenergy/compas/scl/validator/rest/v1/websocket/SclValidateResponseDecoderTest.java index 2a07049..ea5310a 100644 --- a/app/src/test/java/org/lfenergy/compas/scl/validator/rest/v1/websocket/SclValidateResponseDecoderTest.java +++ b/app/src/test/java/org/lfenergy/compas/scl/validator/rest/v1/websocket/SclValidateResponseDecoderTest.java @@ -6,13 +6,13 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.lfenergy.compas.scl.validator.exception.SclValidatorException; +import org.lfenergy.compas.core.commons.exception.CompasException; import javax.xml.bind.UnmarshalException; import static org.junit.jupiter.api.Assertions.*; +import static org.lfenergy.compas.core.commons.exception.CompasErrorCode.WEBSOCKET_DECODER_ERROR_CODE; import static org.lfenergy.compas.scl.validator.SclValidatorConstants.SCL_VALIDATOR_SERVICE_V1_NS_URI; -import static org.lfenergy.compas.scl.validator.exception.SclValidatorErrorCode.WEBSOCKET_DECODER_ERROR_CODE; class SclValidateResponseDecoderTest { private SclValidateResponseDecoder decoder; @@ -69,16 +69,7 @@ void decode_WhenCalledWithWrongXML_ThenExceptionThrown() { + "" + ""; - var exception = assertThrows(SclValidatorException.class, () -> decoder.decode(message)); - assertEquals(WEBSOCKET_DECODER_ERROR_CODE, exception.getErrorCode()); - assertEquals(UnmarshalException.class, exception.getCause().getClass()); - } - - @Test - void decode_WhenCalledWithInCorrectMessage_ThenExceptionThrown() { - var message = "Incorrect Meesage"; - - var exception = assertThrows(SclValidatorException.class, () -> decoder.decode(message)); + var exception = assertThrows(CompasException.class, () -> decoder.decode(message)); assertEquals(WEBSOCKET_DECODER_ERROR_CODE, exception.getErrorCode()); assertEquals(UnmarshalException.class, exception.getCause().getClass()); } diff --git a/app/src/test/java/org/lfenergy/compas/scl/validator/rest/v1/websocket/SclValidateResponseEncoderTest.java b/app/src/test/java/org/lfenergy/compas/scl/validator/rest/v1/websocket/SclValidateResponseEncoderTest.java index f06cba9..d67ccbe 100644 --- a/app/src/test/java/org/lfenergy/compas/scl/validator/rest/v1/websocket/SclValidateResponseEncoderTest.java +++ b/app/src/test/java/org/lfenergy/compas/scl/validator/rest/v1/websocket/SclValidateResponseEncoderTest.java @@ -6,15 +6,14 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.lfenergy.compas.scl.validator.exception.SclValidatorException; import org.lfenergy.compas.scl.validator.model.ValidationError; import org.lfenergy.compas.scl.validator.rest.v1.model.SclValidateResponse; import java.util.ArrayList; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.lfenergy.compas.scl.validator.SclValidatorConstants.SCL_VALIDATOR_SERVICE_V1_NS_URI; -import static org.lfenergy.compas.scl.validator.exception.SclValidatorErrorCode.WEBSOCKET_ENCODER_ERROR_CODE; class SclValidateResponseEncoderTest { private SclValidateResponseEncoder encoder; @@ -56,13 +55,6 @@ void encode_WhenCalledWithRequest_ThenRequestConvertedToString() { assertEquals(expectedResult, result); } - @Test - void encode_WhenCalledWithNull_ThenExceptionThrown() { - var exception = assertThrows(SclValidatorException.class, () -> encoder.encode(null)); - assertEquals(WEBSOCKET_ENCODER_ERROR_CODE, exception.getErrorCode()); - assertEquals(IllegalArgumentException.class, exception.getCause().getClass()); - } - @AfterEach void destroy() { encoder.destroy(); diff --git a/pom.xml b/pom.xml index 4d28b60..1fb475c 100644 --- a/pom.xml +++ b/pom.xml @@ -24,7 +24,7 @@ SPDX-License-Identifier: Apache-2.0 3.2.0 0.0.4 - 0.9.3 + 0.10.2 2.14.0.Final 2.0.3 @@ -101,7 +101,12 @@ SPDX-License-Identifier: Apache-2.0 org.lfenergy.compas.core - jaxrs-commons + rest-commons + ${compas.core.version} + + + org.lfenergy.compas.core + websocket-commons ${compas.core.version} diff --git a/riseclipse/validator-riseclipse/src/test/java/org/lfenergy/compas/scl/validator/impl/OclFileLoaderTest.java b/riseclipse/validator-riseclipse/src/test/java/org/lfenergy/compas/scl/validator/impl/OclFileLoaderTest.java index 6738c4e..bd5c744 100644 --- a/riseclipse/validator-riseclipse/src/test/java/org/lfenergy/compas/scl/validator/impl/OclFileLoaderTest.java +++ b/riseclipse/validator-riseclipse/src/test/java/org/lfenergy/compas/scl/validator/impl/OclFileLoaderTest.java @@ -116,7 +116,7 @@ void prepareValidator_whenCalledWithValidator_ThenValidatorIsAdded() { var validator = mock(ComposedEValidator.class); loader.prepareValidator(validator); - verify(validator, times(1)).addChild(any(CompleteOCLEObjectValidator.class)); + verify(validator).addChild(any(CompleteOCLEObjectValidator.class)); } @AfterEach diff --git a/service/src/test/java/org/lfenergy/compas/scl/validator/service/NsdocServiceTest.java b/service/src/test/java/org/lfenergy/compas/scl/validator/service/NsdocServiceTest.java index 51e4bf5..a50ba93 100644 --- a/service/src/test/java/org/lfenergy/compas/scl/validator/service/NsdocServiceTest.java +++ b/service/src/test/java/org/lfenergy/compas/scl/validator/service/NsdocServiceTest.java @@ -16,7 +16,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) class NsdocServiceTest { @@ -40,7 +41,7 @@ void list_WhenCalled_ThenEmptyListReturned() { assertNotNull(result); assertEquals(1, result.size()); - verify(nsdocFinder, times(1)).getNsdocFiles(); + verify(nsdocFinder).getNsdocFiles(); } @Test @@ -55,6 +56,6 @@ void get_WhenCalled_ThenContentReturned() { assertNotNull(result); assertEquals(content, result); - verify(nsdocFinder, times(1)).getNsdocFile(id); + verify(nsdocFinder).getNsdocFile(id); } } \ No newline at end of file diff --git a/service/src/test/java/org/lfenergy/compas/scl/validator/service/SclValidatorServiceTest.java b/service/src/test/java/org/lfenergy/compas/scl/validator/service/SclValidatorServiceTest.java index dbd8ca4..8dd4f47 100644 --- a/service/src/test/java/org/lfenergy/compas/scl/validator/service/SclValidatorServiceTest.java +++ b/service/src/test/java/org/lfenergy/compas/scl/validator/service/SclValidatorServiceTest.java @@ -7,8 +7,8 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.lfenergy.compas.scl.extensions.model.SclFileType; import org.lfenergy.compas.scl.validator.SclValidator; -import org.lfenergy.compas.scl.validator.xsd.SclXsdValidator; import org.lfenergy.compas.scl.validator.model.ValidationError; +import org.lfenergy.compas.scl.validator.xsd.SclXsdValidator; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @@ -45,8 +45,8 @@ void validate_WhenCalled_ThenExpectedListReturned() { assertNotNull(result); assertEquals(1, result.size()); - verify(sclValidator, times(1)).validate(type, sclData); - verify(xsdValidator, times(1)).validate(sclData); + verify(sclValidator).validate(type, sclData); + verify(xsdValidator).validate(sclData); } @Test @@ -61,7 +61,7 @@ void validate_WhenXsdErrorsAreReturned_ThenSclValidatorIsNeverCalled() { assertNotNull(result); assertEquals(1, result.size()); - verify(xsdValidator, times(1)).validate(sclData); + verify(xsdValidator).validate(sclData); verify(sclValidator, never()).validate(type, sclData); } } \ No newline at end of file diff --git a/validator/src/main/java/org/lfenergy/compas/scl/validator/exception/SclValidatorErrorCode.java b/validator/src/main/java/org/lfenergy/compas/scl/validator/exception/SclValidatorErrorCode.java index 58eac4b..18912e0 100644 --- a/validator/src/main/java/org/lfenergy/compas/scl/validator/exception/SclValidatorErrorCode.java +++ b/validator/src/main/java/org/lfenergy/compas/scl/validator/exception/SclValidatorErrorCode.java @@ -13,9 +13,6 @@ public class SclValidatorErrorCode { public static final String LOADING_XSD_FILE_ERROR_CODE = "SVS-0003"; public static final String CREATE_XPATH_ELEMENT_ERROR_CODE = "SVS-0004"; - public static final String WEBSOCKET_DECODER_ERROR_CODE = "SVS-0100"; - public static final String WEBSOCKET_ENCODER_ERROR_CODE = "SVS-0101"; - public static final String LOADING_OCL_FILES_FAILED = "SVS-1001"; public static final String LOADING_CUSTOM_OCL_FILES_FAILED = "SVS-1002"; diff --git a/validator/src/test/java/org/lfenergy/compas/scl/validator/xsd/XSDErrorHandlerTest.java b/validator/src/test/java/org/lfenergy/compas/scl/validator/xsd/XSDErrorHandlerTest.java index 1909a0e..c99abcc 100644 --- a/validator/src/test/java/org/lfenergy/compas/scl/validator/xsd/XSDErrorHandlerTest.java +++ b/validator/src/test/java/org/lfenergy/compas/scl/validator/xsd/XSDErrorHandlerTest.java @@ -27,7 +27,8 @@ import static org.lfenergy.compas.scl.validator.exception.SclValidatorErrorCode.CREATE_XPATH_ELEMENT_ERROR_CODE; import static org.lfenergy.compas.scl.validator.xsd.XSDErrorHandler.DEFAULT_PREFIX; import static org.lfenergy.compas.scl.validator.xsd.XSDErrorHandler.DEFAULT_RULE_NAME; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) class XSDErrorHandlerTest { @@ -95,7 +96,7 @@ private void assertValidationError(List errorList, String messa assertNull(errorList.get(0).getLineNumber()); assertNull(errorList.get(0).getColumnNumber()); - verify(validator, times(1)).getProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.CURRENT_ELEMENT_NODE_PROPERTY); + verify(validator).getProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.CURRENT_ELEMENT_NODE_PROPERTY); } @Test