Skip to content

Commit

Permalink
Merge pull request #45 from com-pas/develop
Browse files Browse the repository at this point in the history
New release
  • Loading branch information
Dennis Labordus authored Apr 4, 2022
2 parents 66a091f + 4ffa16e commit bd85278
Show file tree
Hide file tree
Showing 27 changed files with 1,165 additions and 20 deletions.
16 changes: 11 additions & 5 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,20 @@ git submodule update
Importing the project is a bit harder for the SCL Validator then normal. It's caused because of the submodules that are
needed from RiseClipse. These projects are Eclipse projects using Eclipse Tycho to build and Eclipse project structure.

A way to make everything work in IntelliJ is importing the project in the following way.
The first step to make it work in IntelliJ is that an Eclipse version needs to be available on your local machine. The
version we know that's working can be found [here](https://www.eclipse.org/downloads/packages/release/2019-06/r)
Download (and install/unzip) Eclipse.

Next a way to make everything work in IntelliJ is importing the project in the following way.

- First step is to just import everything like it are Maven projects;
- Next we need to add a Global Library in IntelliJ. Open the Module Settings and select "Global Libraries". And a new
libraries and name it "ECLIPSE". Point it to the directory "<ECLIPSE_INSTALL_DIR>/plugins";
- Next step is to re-import the RiseClipse Submodule as Eclipse;
- In IntelliJ select "File" -> "New" -> "Module from Existing Sources...";
- Select one of the RiseClipse Submodules, for instance "riseclipse-metamodel-scl2003";
- Next select "Eclipse" by "Import module from External Model";
- Follow the rest of the wizard, only to remember to select all subprojects that are available in the directory;
- In IntelliJ select "File" -> "New" -> "Module from Existing Sources...";
- Select one of the RiseClipse Submodules, for instance "riseclipse-metamodel-scl2003";
- Next select "Eclipse" by "Import module from External Model";
- Follow the rest of the wizard, only to remember to select all subprojects that are available in the directory;

Now the module should be correctly imported in IntelliJ to be used. Check the Module Settings of one of the subprojects
to check if the directory "src" is a Java Source Directory, for instance the module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import io.quarkus.runtime.Startup;
import org.lfenergy.compas.core.commons.ElementConverter;
import org.lfenergy.compas.scl.validator.xsd.SclXsdValidator;
import org.lfenergy.compas.scl.validator.collector.CompasOclFileCollector;
import org.lfenergy.compas.scl.validator.collector.OclFileCollector;
import org.lfenergy.compas.scl.validator.common.NsdocFinder;
Expand Down Expand Up @@ -37,6 +38,11 @@ public SclRiseClipseValidator createSclRiseClipseValidator(OclFileCollector oclF
}

@Produces
@ApplicationScoped
public SclXsdValidator createSclXsdValidator() {
return new SclXsdValidator();
}

@Startup
@ApplicationScoped
public NsdocFinder createNsdocFinder(ValidatorProperties properties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,19 @@ void createSclRiseClipseValidator_WhenCalled_ThenObjectReturned() {

verify(validatorProperties, times(1)).tempDirectory();
}

@Test
void createSclXsdValidator_WhenCalled_ThenObjectReturned() {
assertNotNull(new CompasSclValidatorConfiguration().createSclXsdValidator());
}

@Test
void createNsdocFinder_WhenCalled_ThenObjectReturned() {
when(validatorProperties.nsdocDirectory()).thenReturn("./target/nsodcdir");

assertNotNull(new CompasSclValidatorConfiguration().createNsdocFinder(
validatorProperties));

verify(validatorProperties, times(1)).nsdocDirectory();
}
}
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ SPDX-License-Identifier: Apache-2.0
<surefire-plugin.version>3.0.0-M5</surefire-plugin.version>
<sonarqube-plugin.version>3.2.0</sonarqube-plugin.version>

<compas.scl.xsd.version>0.0.4</compas.scl.xsd.version>
<compas.core.version>0.8.0</compas.core.version>

<quarkus.platform.version>2.7.5.Final</quarkus.platform.version>
Expand Down Expand Up @@ -67,6 +68,12 @@ SPDX-License-Identifier: Apache-2.0
<scope>import</scope>
</dependency>

<dependency>
<groupId>org.lfenergy.compas.xsd</groupId>
<artifactId>compas-scl-xsd</artifactId>
<version>${compas.scl.xsd.version}</version>
</dependency>

<dependency>
<groupId>org.lfenergy.compas.scl.validator</groupId>
<artifactId>service</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.lfenergy.compas.scl.validator.util.OclUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
import java.io.IOException;
import java.nio.file.Path;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.*;
import static org.lfenergy.compas.scl.validator.util.TestSupportUtil.readSCL;

class SclRiseClipseValidatorTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

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 javax.enterprise.context.ApplicationScoped;
Expand All @@ -14,13 +15,21 @@
@ApplicationScoped
public class SclValidatorService {
private final SclValidator validator;
private final SclXsdValidator xsdValidator;

@Inject
public SclValidatorService(SclValidator validator) {
public SclValidatorService(SclValidator validator, SclXsdValidator xsdValidator) {
this.validator = validator;
this.xsdValidator = xsdValidator;
}

public List<ValidationError> validate(SclFileType type, String sclData) {
return validator.validate(type, sclData);
var errors = xsdValidator.validate(sclData);

if (errors.isEmpty()) {
errors = validator.validate(type, sclData);
}

return errors;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
// SPDX-License-Identifier: Apache-2.0
package org.lfenergy.compas.scl.validator.service;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
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.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Collections;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -21,28 +23,45 @@
@ExtendWith(MockitoExtension.class)
class SclValidatorServiceTest {

@InjectMocks
private SclValidatorService sclValidatorService;

@Mock
private SclValidator sclValidator;

@BeforeEach
void beforeEach() {
sclValidatorService = new SclValidatorService(sclValidator);
}
@Mock
private SclXsdValidator xsdValidator;

@Test
void validate_WhenCalled_ThenExpectedListReturned() {
var type = SclFileType.CID;
var sclData = "Some String";

when(sclValidator.validate(type, sclData)).thenReturn(List.of(new ValidationError()));
when(xsdValidator.validate(sclData)).thenReturn(Collections.emptyList());

var result = sclValidatorService.validate(type, sclData);

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

verify(sclValidator, times(1)).validate(type, sclData);
verify(xsdValidator, times(1)).validate(sclData);
}

@Test
void validate_WhenXsdErrorsAreReturned_ThenSclValidatorIsNeverCalled() {
var type = SclFileType.CID;
var sclData = "Some String";

when(xsdValidator.validate(sclData)).thenReturn(List.of(new ValidationError()));

var result = sclValidatorService.validate(type, sclData);

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

verify(xsdValidator, times(1)).validate(sclData);
verify(sclValidator, never()).validate(type, sclData);
}
}
4 changes: 4 additions & 0 deletions validator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ SPDX-License-Identifier: Apache-2.0
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.lfenergy.compas.xsd</groupId>
<artifactId>compas-scl-xsd</artifactId>
</dependency>
<dependency>
<groupId>org.lfenergy.compas.core</groupId>
<artifactId>commons</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class SclValidatorErrorCode {

public static final String NO_SCL_ELEMENT_FOUND_ERROR_CODE = "SVS-0001";
public static final String LOADING_SCL_FILE_ERROR_CODE = "SVS-0002";
public static final String LOADING_XSD_FILE_ERROR_CODE = "SVS-0003";

public static final String LOADING_OCL_FILES_FAILED = "SVS-1001";
public static final String LOADING_CUSTOM_OCL_FILES_FAILED = "SVS-1002";
Expand All @@ -19,6 +20,7 @@ public class SclValidatorErrorCode {
public static final String WRITE_TO_OCL_TEMP_FILES_FAILED = "SVS-2003";
public static final String OCL_MODEL_PACKAGE_NOT_FOUND = "SVS-2005";
public static final String NO_URI_PASSED = "SVS-2006";
public static final String RESOURCE_RESOLVER_FAILED = "SVS-2007";

public static final String CALCULATING_CHECKSUM_FAILED = "SVS-3001";
public static final String DETERMINING_ID_FAILED = "SVS-3002";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// SPDX-FileCopyrightText: 2022 Alliander N.V.
//
// SPDX-License-Identifier: Apache-2.0
package org.lfenergy.compas.scl.validator.xsd;

import org.lfenergy.compas.scl.validator.exception.SclValidatorException;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import static org.lfenergy.compas.scl.validator.exception.SclValidatorErrorCode.LOADING_SCL_FILE_ERROR_CODE;
import static org.lfenergy.compas.scl.validator.util.StaxUtil.getAttributeValue;
import static org.lfenergy.compas.scl.validator.util.StaxUtil.isElement;

public class SclInfo {
private static final String SCL_ELEMENT_NAME = "SCL";

private String version = null;
private String revision = null;
private String release = null;

public SclInfo(String sclData) {
try (var fis = new ByteArrayInputStream(sclData.getBytes(StandardCharsets.UTF_8))) {
var xmlInputFactory = getXMLInputFactory();
var reader = xmlInputFactory.createXMLEventReader(fis);

while (reader.hasNext()) {
processEvent(reader.nextEvent());
}
} catch (IOException | XMLStreamException exp) {
throw new SclValidatorException(LOADING_SCL_FILE_ERROR_CODE, "Error loading SCL File", exp);
}
}

private void processEvent(XMLEvent nextEvent) {
if (nextEvent.isStartElement()) {
processStartElement(nextEvent.asStartElement());
}
}

private void processStartElement(StartElement element) {
if (isElement(element, SCL_ELEMENT_NAME)) {
version = getAttributeValue(element, "version");
revision = getAttributeValue(element, "revision");
release = getAttributeValue(element, "release");
}
}

private XMLInputFactory getXMLInputFactory() {
var xmlInputFactory = XMLInputFactory.newInstance();
xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
return xmlInputFactory;
}

public String getSclVersion() {
var sclVersion = "";
if (version != null) sclVersion += version;
if (revision != null) sclVersion += revision;
if (release != null) sclVersion += release;

return sclVersion;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-FileCopyrightText: 2022 Alliander N.V.
//
// SPDX-License-Identifier: Apache-2.0
package org.lfenergy.compas.scl.validator.xsd;

import org.lfenergy.compas.scl.validator.model.ValidationError;

import java.util.ArrayList;
import java.util.List;

public class SclXsdValidator {

public List<ValidationError> validate(String sclData) {
var validationErrors = new ArrayList<ValidationError>();

var xsdValidator = new XSDValidator(validationErrors, sclData);
xsdValidator.validate();

return validationErrors;
}
}
Loading

0 comments on commit bd85278

Please sign in to comment.