Skip to content

Commit

Permalink
merge remote-tracking branch 'origin/master' into feature-kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
JPercival committed Dec 12, 2024
2 parents 650c1ab + b4b0701 commit 214a32c
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ cql-all.ipr
.DS_STORE
out
.project
.kotlin
.classpath
.settings
.kotlin
Expand Down
29 changes: 29 additions & 0 deletions Src/java/cql-to-elm/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
plugins {
id 'cql.library-conventions'
id 'cql.xjc-conventions'
}

ext {
jacksonVersion = project['jackson.version']
}

dependencies {
api project(':cql')
api project(':model')
api project(':elm')
api 'org.fhir:ucum:1.0.8'
api 'org.apache.commons:commons-text:1.10.0'

// TODO: This dependencies are required due the the fact that the CqlTranslatorOptionsMapper lives
// in the cql-to-elm project. Ideally, we'd factor out all serialization dependencies into common
// libraries such that we could swap out jackson for something else. In the meantime, these are
// "implementation" dependencies so that they are not exported downstream.
implementation "com.fasterxml.jackson.module:jackson-module-jakarta-xmlbind-annotations:${jacksonVersion}"
testImplementation project(':elm-jackson')
testImplementation project(':model-jackson')
testImplementation project(':quick')
testImplementation project(':qdm')
testImplementation 'com.github.reinert:jjschema:1.16'
testImplementation 'com.tngtech.archunit:archunit:1.2.1'
testImplementation 'org.skyscreamer:jsonassert:1.5.1'
}
2 changes: 2 additions & 0 deletions Src/java/elm-jaxb/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ plugins {

dependencies {
api(project(":elm"))
testImplementation(project(":cql-to-elm"))
testImplementation(project(":model-jaxb"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.cqframework.cql.elm.serializing.jaxb;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

public class SerializationRoundTripTest {

public static String[] dataMethod() {
return new String[] {
"OperatorTests/ArithmeticOperators.cql",
"OperatorTests/ComparisonOperators.cql",
"OperatorTests/ListOperators.cql",
};
}

private static String pathForFile(String cqlFile) {
return "../cql-to-elm/src/test/resources/org/cqframework/cql/cql2elm/" + cqlFile;
}

private static final ElmXmlLibraryReader reader = new ElmXmlLibraryReader();
private static final ElmXmlLibraryWriter writer = new ElmXmlLibraryWriter();

@ParameterizedTest
@MethodSource("dataMethod")
void roundTrip(String cqlFile) throws IOException {
var translator = TestUtils.createTranslator(pathForFile(cqlFile));
assertEquals(0, translator.getErrors().size());

var xml = translator.toXml();
var library = reader.read(new StringReader(xml));
var stringWriter = new StringWriter();
writer.write(library, stringWriter);
assertEquals(xml, stringWriter.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.cqframework.cql.elm.serializing.jaxb;

import java.io.InputStream;
import org.cqframework.cql.cql2elm.LibraryContentType;
import org.cqframework.cql.cql2elm.LibrarySourceProvider;
import org.hl7.elm.r1.VersionedIdentifier;

public class TestLibrarySourceProvider implements LibrarySourceProvider {

private String path = "LibraryTests";

public TestLibrarySourceProvider() {}

public TestLibrarySourceProvider(String path) {
this.path = path;
}

@Override
public InputStream getLibrarySource(VersionedIdentifier libraryIdentifier) {
return getLibraryContent(libraryIdentifier, LibraryContentType.CQL);
}

@Override
public InputStream getLibraryContent(VersionedIdentifier libraryIdentifier, LibraryContentType type) {
return TestLibrarySourceProvider.class.getResourceAsStream(getFileName(libraryIdentifier, type));
}

private String getFileName(VersionedIdentifier libraryIdentifier, LibraryContentType type) {
return String.format(
"%s/%s%s.%s",
path,
libraryIdentifier.getId(),
libraryIdentifier.getVersion() != null ? ("-" + libraryIdentifier.getVersion()) : "",
type.toString().toLowerCase());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.cqframework.cql.elm.serializing.jaxb;

import java.io.IOException;
import org.cqframework.cql.cql2elm.CqlCompilerOptions;
import org.cqframework.cql.cql2elm.CqlTranslator;
import org.cqframework.cql.cql2elm.LibraryManager;
import org.cqframework.cql.cql2elm.ModelManager;

public class TestUtils {
public static CqlTranslator createTranslator(String testFileName, CqlCompilerOptions.Options... options)
throws IOException {
return CqlTranslator.fromFile(testFileName, getLibraryManager(options));
}

private static LibraryManager getLibraryManager(CqlCompilerOptions.Options... options) {
final ModelManager modelManager = new ModelManager();
final CqlCompilerOptions compilerOptions = new CqlCompilerOptions(options);
return getLibraryManager(compilerOptions, modelManager);
}

private static LibraryManager getLibraryManager(CqlCompilerOptions options, ModelManager modelManager) {
final LibraryManager libraryManager = new LibraryManager(modelManager, options);
libraryManager.getLibrarySourceLoader().registerProvider(new TestLibrarySourceProvider());
return libraryManager;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.cqframework.cql.elm.serializing.jaxb;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.io.StringReader;
import java.nio.file.Path;
import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.cqframework.cql.cql2elm.CqlTranslator;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;

class XSDValidationTest {
private static final Logger logger = LoggerFactory.getLogger(XSDValidationTest.class);

public static String[] dataMethod() {
return new String[] {
"OperatorTests/ArithmeticOperators.cql",
"OperatorTests/ComparisonOperators.cql",
"OperatorTests/ListOperators.cql",
};
}

private static final Validator validator = createValidator();

private static Validator createValidator() {
try {
var schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
var schema = schemaFactory.newSchema(
Path.of("../../cql-lm/schema/elm/library.xsd").toFile());
return schema.newValidator();
} catch (SAXException e) {
throw new RuntimeException(e);
}
}

private static String pathForFile(String cqlFile) {
return "../cql-to-elm/src/test/resources/org/cqframework/cql/cql2elm/" + cqlFile;
}

private static boolean validateXMLAgainstXSD(String cqlFile, String xml) {
try {
validator.validate(new StreamSource(new StringReader(xml)));
return true;
} catch (IOException | SAXException e) {
logger.error("error validating XML against XSD for file {}", cqlFile, e);
}
return false;
}

@Test
void ensureValidatorFailsForBadXml() {
var xml = "<library xmlns=\"urn:hl7-org:elm:r1\"></library>";
var source = new StreamSource(new StringReader(xml));
assertThrows(SAXException.class, () -> validator.validate(source));
}

@ParameterizedTest
@MethodSource("dataMethod")
void validateXML(String cqlFile) throws IOException {
var path = pathForFile(cqlFile);
var t = TestUtils.createTranslator(path);
assertTrue(t.getErrors().isEmpty());

var xml = CqlTranslator.convertToXml(t.toELM());
assertTrue(validateXMLAgainstXSD(cqlFile, xml));
}
}

0 comments on commit 214a32c

Please sign in to comment.