-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge remote-tracking branch 'origin/master' into feature-kotlin
- Loading branch information
Showing
7 changed files
with
210 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ cql-all.ipr | |
.DS_STORE | ||
out | ||
.project | ||
.kotlin | ||
.classpath | ||
.settings | ||
.kotlin | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...xb/src/test/java/org/cqframework/cql/elm/serializing/jaxb/SerializationRoundTripTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...axb/src/test/java/org/cqframework/cql/elm/serializing/jaxb/TestLibrarySourceProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
Src/java/elm-jaxb/src/test/java/org/cqframework/cql/elm/serializing/jaxb/TestUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...va/elm-jaxb/src/test/java/org/cqframework/cql/elm/serializing/jaxb/XSDValidationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |