Skip to content

Commit

Permalink
Merge pull request #102 from com-pas/process-riseclipse-messages
Browse files Browse the repository at this point in the history
Updated processing RiseClipse validation message to update ValidationError Response
  • Loading branch information
juancho0202 authored Aug 23, 2022
2 parents b3dbbae + 86e44cb commit a4807b5
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,26 @@ void init() {
@Test
void encode_WhenCalledWithRequest_ThenRequestConvertedToString() {
var validationMessage = "Some Validation Message";
var ruleName = "Rule Name 1";
var linenumber = 15;

var request = new SclValidateResponse();
request.setValidationErrorList(new ArrayList<>());
var validationError = new ValidationError();
validationError.setMessage(validationMessage);
validationError.setRuleName(ruleName);
validationError.setLinenumber(linenumber);
request.getValidationErrorList().add(validationError);

var result = encoder.encode(request);

var expectedResult = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
"<svs:SclValidateResponse xmlns:svs=\"" + SCL_VALIDATOR_SERVICE_V1_NS_URI + "\">" +
"<svs:ValidationErrors><svs:Message>" + validationMessage + "</svs:Message></svs:ValidationErrors>" +
"<svs:ValidationErrors>" +
"<svs:Message>" + validationMessage + "</svs:Message>" +
"<svs:RuleName>" + ruleName + "</svs:RuleName>" +
"<svs:Linenumber>" + linenumber + "</svs:Linenumber>" +
"</svs:ValidationErrors>" +
"</svs:SclValidateResponse>";
assertNotNull(result);
assertEquals(expectedResult, result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import java.util.ArrayList;
import java.util.List;

import static org.lfenergy.compas.scl.validator.util.MessageUtil.cleanupMessage;
import static org.lfenergy.compas.scl.validator.util.MessageUtil.createValidationError;

public class SclRiseClipseValidator implements SclValidator {
private static final Logger LOGGER = LoggerFactory.getLogger(SclRiseClipseValidator.class);
Expand Down Expand Up @@ -77,12 +77,16 @@ public List<ValidationError> validate(SclFileType type, String sclData) {
private void processDiagnostic(Diagnostic diagnostic, List<ValidationError> validationErrors) {
// If there are children in the diagnostic there are validation errors to be processed.
for (Diagnostic childDiagnostic : diagnostic.getChildren()) {
var validationError = new ValidationError();
validationErrors.add(validationError);

String message = cleanupMessage(childDiagnostic.getMessage());
validationError.setMessage(message);
LOGGER.debug("SCL Validation Error '{}'", message);
var validationError = createValidationError(childDiagnostic.getMessage());
if (validationError.isPresent()) {
validationErrors.add(validationError.get());
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("SCL Validation Error '{}' from Rule '{}' (Linenumber {})",
validationError.get().getMessage(),
validationError.get().getRuleName(),
validationError.get().getLinenumber());
}
}

// Also process the children of the children.
processDiagnostic(childDiagnostic, validationErrors);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,46 @@
// SPDX-License-Identifier: Apache-2.0
package org.lfenergy.compas.scl.validator.util;

import org.lfenergy.compas.scl.validator.model.ValidationError;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Optional;

public class MessageUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(MessageUtil.class);

MessageUtil() {
throw new UnsupportedOperationException("MessageUtil class");
}

public static String cleanupMessage(String message) {
String cleanedMessage = message;
if (cleanedMessage != null
&& cleanedMessage.toUpperCase().startsWith("ERROR;")) {
cleanedMessage = cleanedMessage.substring(6);
public static Optional<ValidationError> createValidationError(String message) {
if (message == null || message.isBlank()) {
return Optional.empty();
}
return cleanedMessage;

var validationError = new ValidationError();
var messageParts = message.split(";");
if (messageParts.length == 5) {
// The expected number of parts is found, the message and rule are set as-is, the linenumber is converted
// to a Long value,
validationError.setRuleName(messageParts[1]);
validationError.setMessage(messageParts[4]);

try {
validationError.setLinenumber(Long.parseLong(messageParts[3]));
} catch (NumberFormatException exp) {
validationError.setLinenumber(-1);
LOGGER.debug("Invalid linenumber '{}' found", messageParts[3], exp);
}
} else if (messageParts.length == 2) {
// It seems like an old message that starts with 'ERROR;', so only set the second part as Message
validationError.setMessage(messageParts[1]);
} else {
// If we can split it correctly, just put the message as-is to the response.
validationError.setMessage(message);
}

return Optional.of(validationError);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,11 @@ void validate_WhenCalled_ThenExpectedValidationErrorsReturned() throws IOExcepti

assertNotNull(result);
assertEquals(15, result.size());

// Check if parsing of the message is still working.
var firstMessage = result.get(0);
assertNotNull(firstMessage.getRuleName());
assertNotNull(firstMessage.getMessage());
assertTrue(firstMessage.getLinenumber() > 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;
import static org.lfenergy.compas.scl.validator.util.MessageUtil.cleanupMessage;
import static org.lfenergy.compas.scl.validator.util.MessageUtil.createValidationError;

class MessageUtilTest {
@Test
Expand All @@ -15,27 +15,81 @@ void constructor_WhenConstructorCalled_ThenShouldThrowExceptionCauseForbidden()
}

@Test
void cleanupMessage_WhenCalledWithNullMessage_ThenNullIsReturned() {
var result = cleanupMessage(null);
void createValidationError_WhenCalledNullPassed_ThenEmptyOptionalReturned() {
var result = createValidationError(null);

assertNull(result);
assertNotNull(result);
assertTrue(result.isEmpty());
}

@Test
void cleanupMessage_WhenCalledWithAlreadyCleanMessage_ThenSameMessageIsReturned() {
var expectedMessage = "Some validation message";
void createValidationError_WhenCalledBlankStringPassed_ThenEmptyOptionalReturned() {
var result = createValidationError("");

var result = cleanupMessage(expectedMessage);
assertNotNull(result);
assertTrue(result.isEmpty());
}

@Test
void createValidationError_WhenCalledWithoutParts_ThenSameMessageReturned() {
var message = "Just some message";

var result = createValidationError(message);

assertNotNull(result);
assertFalse(result.isEmpty());

var validationError = result.get();
assertNull(validationError.getRuleName());
assertEquals(0, validationError.getLinenumber());
assertEquals(message, validationError.getMessage());
}

@Test
void createValidationError_WhenCalledWithTooManyParts_ThenSameMessageReturned() {
var message = "ERROR;Part1;Part2;Part3;Part4;Just some message";

assertEquals(expectedMessage, result);
var result = createValidationError(message);

assertNotNull(result);
assertFalse(result.isEmpty());

var validationError = result.get();
assertNull(validationError.getRuleName());
assertEquals(0, validationError.getLinenumber());
assertEquals(message, validationError.getMessage());
}

@Test
void cleanupMessage_WhenCalledWithMessageThatStartWithError_ThenCleanedMessageIsReturned() {
var expectedMessage = "Some validation message";
void createValidationError_WhenCalledWithCorrectMessage_ThenConvertValidationErrorReturned() {
var message = "AnyLN (lnType=LN2) does not refer an existing LNodeType in DataTypeTemplates section";
var ruleName = "OCL/SemanticConstraints/AnyLN_RefersToLNodeType";
var linenumber = (long) 9;

var result = createValidationError("ERROR;" + ruleName + ";scl-file.scd;" + linenumber + ";" + message);

assertNotNull(result);
assertFalse(result.isEmpty());

var validationError = result.get();
assertEquals(ruleName, validationError.getRuleName());
assertEquals(linenumber, validationError.getLinenumber());
assertEquals(message, validationError.getMessage());
}

@Test
void createValidationError_WhenCalledWithInvalidLinenumber_ThenNegativeLinenumberReturned() {
var message = "AnyLN (lnType=LN2) does not refer an existing LNodeType in DataTypeTemplates section";
var ruleName = "OCL/SemanticConstraints/AnyLN_RefersToLNodeType";

var result = createValidationError("ERROR;" + ruleName + ";scl-file.scd;INVALID_LINENUMBER;" + message);

var result = cleanupMessage("ERROR;" + expectedMessage);
assertNotNull(result);
assertFalse(result.isEmpty());

assertEquals(expectedMessage, result);
var validationError = result.get();
assertEquals(ruleName, validationError.getRuleName());
assertEquals(-1, validationError.getLinenumber());
assertEquals(message, validationError.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,45 @@
@XmlAccessorType(XmlAccessType.FIELD)
public class ValidationError {
@Schema(description = "The message of the validation error that occurred.",
example = "ERROR:[SemanticConstraints] Terminal (name=T1) (line 27) does not refer an existing ConnectivityNode")
example = "Terminal (name=T1) (line 27) does not refer an existing ConnectivityNode")
@XmlElement(name = "Message",
namespace = SCL_VALIDATOR_SERVICE_V1_NS_URI,
required = true)
private String message;

@Schema(description = "The name of the rule in RiseClipse that created the validation error",
example = "SemanticConstraints")
@XmlElement(name = "RuleName",
namespace = SCL_VALIDATOR_SERVICE_V1_NS_URI)
private String ruleName;

@Schema(description = "The linenumber in the SCL file where the validation error occurred",
example = "9")
@XmlElement(name = "Linenumber",
namespace = SCL_VALIDATOR_SERVICE_V1_NS_URI)
private long linenumber;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public String getRuleName() {
return ruleName;
}

public void setRuleName(String ruleName) {
this.ruleName = ruleName;
}

public long getLinenumber() {
return linenumber;
}

public void setLinenumber(long linenumber) {
this.linenumber = linenumber;
}
}

0 comments on commit a4807b5

Please sign in to comment.