use of org.hl7.fhir.r5.conformance.ProfileUtilities in project org.hl7.fhir.core by hapifhir.
the class ProfileUtilitiesTests method testMinValueChange.
/**
* Change min value
*/
@Test
void testMinValueChange() {
// Given
StructureDefinition focus = new StructureDefinition();
StructureDefinition base = TestingUtilities.context().fetchResource(StructureDefinition.class, "http://hl7.org/fhir/StructureDefinition/Appointment").copy();
focus.setUrl(Utilities.makeUuidUrn());
focus.setBaseDefinition(base.getUrl());
focus.setType(base.getType());
focus.setDerivation(TypeDerivationRule.CONSTRAINT);
ElementDefinition id = focus.getDifferential().addElement();
id.setPath("Appointment.minutesDuration");
id.setMinValue(new IntegerType(1));
List<ValidationMessage> messages = new ArrayList<>();
// When
new ProfileUtilities(TestingUtilities.context(), messages, null).generateSnapshot(base, focus, focus.getUrl(), "http://test.org", "Simple Test");
// Then
boolean ok = base.getSnapshot().getElement().size() == focus.getSnapshot().getElement().size();
for (int i = 0; i < base.getSnapshot().getElement().size(); i++) {
if (ok) {
ElementDefinition b = base.getSnapshot().getElement().get(i);
ElementDefinition f = focus.getSnapshot().getElement().get(i);
b.setRequirements(null);
f.setRequirements(null);
for (ElementDefinitionConstraintComponent c : b.getConstraint()) {
c.setSource(null);
}
for (ElementDefinitionConstraintComponent c : f.getConstraint()) {
c.setSource(null);
}
if (!f.hasBase() || !b.getPath().equals(f.getPath())) {
ok = false;
} else {
if (f.getPath().equals("Appointment.minutesDuration")) {
ok = f.getMinValue() instanceof IntegerType && ((IntegerType) f.getMinValue()).getValue() == 1;
if (ok) {
// Can't set minValue to null so change base minValue to IntegerType(1)
b.setMinValue(new IntegerType(1));
}
}
if (!Base.compareDeep(b, f, true)) {
ok = false;
}
}
}
}
Assertions.assertTrue(ok);
}
use of org.hl7.fhir.r5.conformance.ProfileUtilities in project org.hl7.fhir.core by hapifhir.
the class ValidationTests method loadProfile.
public StructureDefinition loadProfile(String filename, String contents, List<ValidationMessage> messages, boolean debug) throws IOException, FHIRFormatError, FileNotFoundException, FHIRException, DefinitionException {
StructureDefinition sd = (StructureDefinition) loadResource(filename, contents);
ProfileUtilities pu = new ProfileUtilities(TestingUtilities.getSharedWorkerContext(version), messages, null);
pu.setDebug(debug);
if (!sd.hasSnapshot()) {
StructureDefinition base = TestingUtilities.getSharedWorkerContext(version).fetchResource(StructureDefinition.class, sd.getBaseDefinition());
pu.generateSnapshot(base, sd, sd.getUrl(), null, sd.getTitle());
// (debugging) new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path("[tmp]", sd.getId()+".xml")), sd);
}
for (Resource r : sd.getContained()) {
if (r instanceof StructureDefinition) {
StructureDefinition childSd = (StructureDefinition) r;
if (!childSd.hasSnapshot()) {
StructureDefinition base = TestingUtilities.getSharedWorkerContext(version).fetchResource(StructureDefinition.class, childSd.getBaseDefinition());
pu.generateSnapshot(base, childSd, childSd.getUrl(), null, childSd.getTitle());
}
}
}
return sd;
}
use of org.hl7.fhir.r5.conformance.ProfileUtilities in project org.hl7.fhir.core by hapifhir.
the class ComparisonTests method genSnapshot.
private void genSnapshot(ProfileUtilities utils, StructureDefinition sd) {
StructureDefinition base = context.fetchTypeDefinition(sd.getType());
utils.generateSnapshot(base, sd, sd.getUrl(), "http://hl7.org/fhir/r4", sd.present());
}
use of org.hl7.fhir.r5.conformance.ProfileUtilities in project org.hl7.fhir.core by hapifhir.
the class SnapShotGenerationXTests method testGen.
private void testGen(boolean fail, TestDetails test) throws Exception {
if (!Utilities.noString(test.register)) {
List<ValidationMessage> messages = new ArrayList<ValidationMessage>();
ProfileUtilities pu = new ProfileUtilities(UtilitiesXTests.context(version), messages, null);
pu.setNewSlicingProcessing(true);
pu.setIds(test.included, false);
StructureDefinition base = UtilitiesXTests.context(version).fetchResource(StructureDefinition.class, test.included.getBaseDefinition());
if (base != null) {
pu.generateSnapshot(base, test.included, test.included.getUrl(), "http://test.org/profile", test.included.getName());
}
if (!UtilitiesXTests.context(version).hasResource(StructureDefinition.class, test.included.getUrl()))
UtilitiesXTests.context(version).cacheResource(test.included);
int ec = 0;
for (ValidationMessage vm : messages) {
if (vm.getLevel() == IssueSeverity.ERROR) {
System.out.println(vm.summary());
ec++;
}
}
if (ec > 0)
throw new FHIRException("register gen failed: " + messages.toString());
}
StructureDefinition base = getSD(test.getSource().getBaseDefinition());
if (!base.getUrl().equals(test.getSource().getBaseDefinition()))
throw new Exception("URL mismatch on base: " + base.getUrl() + " wanting " + test.getSource().getBaseDefinition());
StructureDefinition output = test.getSource().copy();
ProfileUtilities pu = new ProfileUtilities(UtilitiesXTests.context(version), messages, new TestPKP());
pu.setNewSlicingProcessing(test.isNewSliceProcessing());
pu.setThrowException(false);
pu.setDebug(test.isDebug());
pu.setIds(test.getSource(), false);
if (test.isSort()) {
List<String> errors = new ArrayList<String>();
int lastCount = output.getDifferential().getElement().size();
pu.sortDifferential(base, output, test.getSource().getName(), errors, false);
if (errors.size() > 0)
throw new FHIRException("Sort failed: " + errors.toString());
}
try {
messages.clear();
pu.generateSnapshot(base, output, test.getSource().getUrl(), "http://test.org/profile", test.getSource().getName());
List<ValidationMessage> ml = new ArrayList<>();
for (ValidationMessage vm : messages) {
if (vm.getLevel() == IssueSeverity.ERROR) {
ml.add(vm);
}
}
if (ml.size() > 0) {
throw new FHIRException("Snapshot Generation failed: " + ml.toString());
}
} catch (Throwable e) {
System.out.println("\r\nException: " + e.getMessage());
throw e;
}
if (output.getDifferential().hasElement()) {
RenderingContext rc = new RenderingContext(TestingUtilities.getSharedWorkerContext(), null, null, "http://hl7.org/fhir", "", null, ResourceRendererMode.END_USER);
rc.setDestDir(makeTempDir());
rc.setProfileUtilities(new ProfileUtilities(TestingUtilities.getSharedWorkerContext(), null, new TestPKP()));
RendererFactory.factory(output, rc).render(output);
}
if (!fail) {
test.output = output;
UtilitiesXTests.context(version).cacheResource(output);
File dst = new File(UtilitiesXTests.tempFile("snapshot", test.getId() + "-expected.xml"));
if (dst.exists())
dst.delete();
IOUtils.copy(UtilitiesXTests.loadTestResourceStream("rX", "snapshot-generation", test.getId() + "-expected.xml"), new FileOutputStream(dst));
new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(UtilitiesXTests.tempFile("snapshot", test.getId() + "-actual.xml")), output);
StructureDefinition t1 = test.expected.copy();
t1.setText(null);
StructureDefinition t2 = test.output.copy();
t2.setText(null);
Assertions.assertTrue(t1.equalsDeep(t2), "Output does not match expected");
}
}
use of org.hl7.fhir.r5.conformance.ProfileUtilities in project dpc-app by CMSgov.
the class DPCProfileSupport method loadProfiles.
private Map<String, StructureDefinition> loadProfiles(FhirContext ctx) {
logger.info("Loading resource profiles");
final Map<String, StructureDefinition> definitionMap = new HashMap<>();
// Generate a validator to pull the base definitions from.
final DefaultProfileValidationSupport defaultValidation = new DefaultProfileValidationSupport();
final HapiWorkerContext hapiWorkerContext = new HapiWorkerContext(ctx, defaultValidation);
final ProfileUtilities profileUtilities = new ProfileUtilities(hapiWorkerContext, new ArrayList<>(), null);
final IParser parser = ctx.newJsonParser();
ServiceLoaderHelpers.getLoaderStream(IProfileLoader.class).map(profileLoader -> toStructureDefinition(parser, profileLoader.getPath())).filter(Objects::nonNull).map(diffStructure -> mergeDiff(ctx, defaultValidation, profileUtilities, diffStructure)).forEach(structure -> definitionMap.put(structure.getUrl(), structure));
return definitionMap;
}
Aggregations