use of org.hl7.fhir.r5.conformance.ProfileUtilities in project org.hl7.fhir.core by hapifhir.
the class SnapShotGenerationTests method getSD.
private StructureDefinition getSD(String url, SnapShotGenerationTestsContext context) throws DefinitionException, FHIRException, IOException {
StructureDefinition sd = context.getByUrl(url);
if (sd == null) {
sd = TestingUtilities.context().fetchResource(StructureDefinition.class, url);
}
if (sd == null) {
throw new DefinitionException("Unable to find profile " + url);
}
if (!sd.hasSnapshot()) {
StructureDefinition base = getSD(sd.getBaseDefinition(), context);
ProfileUtilities pu = new ProfileUtilities(TestingUtilities.context(), messages, new TestPKP());
pu.setNewSlicingProcessing(true);
List<String> errors = new ArrayList<String>();
pu.sortDifferential(base, sd, url, errors, false);
if (!errors.isEmpty())
throw new FHIRException(errors.get(0));
pu.setIds(sd, false);
pu.generateSnapshot(base, sd, sd.getUrl(), "http://test.org/profile", sd.getName());
}
return sd;
}
use of org.hl7.fhir.r5.conformance.ProfileUtilities in project org.hl7.fhir.core by hapifhir.
the class SnapShotGenerationTests method testGen.
private void testGen(boolean fail, TestDetails test, SnapShotGenerationTestsContext context) throws Exception {
if (!Utilities.noString(test.register)) {
List<ValidationMessage> messages = new ArrayList<ValidationMessage>();
ProfileUtilities pu = new ProfileUtilities(TestingUtilities.context(), messages, null);
pu.setNewSlicingProcessing(true);
for (StructureDefinition sd : test.included) {
pu.setIds(sd, false);
}
for (StructureDefinition sd : test.included) {
if (!TestingUtilities.context().hasResource(StructureDefinition.class, sd.getUrl())) {
TestingUtilities.context().cacheResource(sd);
}
}
StructureDefinition base = TestingUtilities.context().fetchResource(StructureDefinition.class, test.included.get(0).getBaseDefinition());
if (base != null) {
pu.generateSnapshot(base, test.included.get(0), test.included.get(0).getUrl(), "http://test.org/profile", test.included.get(0).getName());
}
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(), context);
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(TestingUtilities.context(), messages, new TestPKP());
pu.setNewSlicingProcessing(test.isNewSliceProcessing());
pu.setThrowException(false);
pu.setDebug(test.isDebug());
pu.setIds(test.getSource(), false);
if (!TestingUtilities.context().hasPackage(CommonPackages.ID_XVER, CommonPackages.VER_XVER)) {
NpmPackage npm = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION).loadPackage(CommonPackages.ID_XVER, CommonPackages.VER_XVER);
TestingUtilities.context().loadFromPackage(npm, new TestLoader(new String[] { "StructureDefinition" }), new String[] { "StructureDefinition" });
}
pu.setXver(new XVerExtensionManager(TestingUtilities.context()));
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.context(), null, null, "http://hl7.org/fhir", "", null, ResourceRendererMode.END_USER);
rc.setDestDir(Utilities.path("[tmp]", "snapshot"));
rc.setProfileUtilities(new ProfileUtilities(TestingUtilities.context(), null, new TestPKP()));
RendererFactory.factory(output, rc).render(output);
}
if (!fail) {
test.output = output;
TestingUtilities.context().cacheResource(output);
File dst = new File(TestingUtilities.tempFile("snapshot", test.getId() + "-expected.xml"));
if (dst.exists())
dst.delete();
IOUtils.copy(TestingUtilities.loadTestResourceStream("r5", "snapshot-generation", test.getId() + "-expected.xml"), new FileOutputStream(dst));
new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(TestingUtilities.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 org.hl7.fhir.core by hapifhir.
the class ComparisonRenderer method renderProfile.
private void renderProfile(String id, ProfileComparison comp) throws IOException {
String template = templates.get("Profile");
Map<String, Base> vars = new HashMap<>();
ProfileComparer cs = new ProfileComparer(session, new ProfileUtilities(session.getContextLeft(), null, session.getPkp()), new ProfileUtilities(session.getContextRight(), null, session.getPkp()));
vars.put("left", new StringType(comp.getLeft().present()));
vars.put("right", new StringType(comp.getRight().present()));
vars.put("leftId", new StringType(comp.getLeft().getId()));
vars.put("rightId", new StringType(comp.getRight().getId()));
vars.put("leftUrl", new StringType(comp.getLeft().getUrl()));
vars.put("rightUrl", new StringType(comp.getRight().getUrl()));
vars.put("errors", new StringType(new XhtmlComposer(true).compose(cs.renderErrors(comp))));
vars.put("metadata", new StringType(new XhtmlComposer(true).compose(cs.renderMetadata(comp, "", ""))));
vars.put("structure", new StringType(new XhtmlComposer(true).compose(cs.renderStructure(comp, "", "", "http://hl7.org/fhir"))));
String cnt = processTemplate(template, "CodeSystem", vars);
TextFile.stringToFile(cnt, file(comp.getId() + ".html"));
new org.hl7.fhir.r5.formats.JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path(folder, comp.getId() + "-union.json")), comp.getUnion());
new org.hl7.fhir.r5.formats.JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path(folder, comp.getId() + "-intersection.json")), comp.getIntersection());
}
use of org.hl7.fhir.r5.conformance.ProfileUtilities in project org.hl7.fhir.core by hapifhir.
the class QuestionnaireBuilder method makeTypeList.
private ValueSet makeTypeList(StructureDefinition profile, List<TypeRefComponent> types, String path) {
ValueSet vs = new ValueSet();
vs.setName("Type options for " + path);
vs.setDescription(vs.present());
vs.setStatus(PublicationStatus.ACTIVE);
vs.setExpansion(new ValueSetExpansionComponent());
vs.getExpansion().setIdentifier(Factory.createUUID());
vs.getExpansion().setTimestampElement(DateTimeType.now());
for (TypeRefComponent t : types) {
if (t.hasTarget()) {
for (UriType u : t.getTargetProfile()) {
if (u.getValue().startsWith("http://hl7.org/fhir/StructureDefinition/")) {
ValueSetExpansionContainsComponent cc = vs.getExpansion().addContains();
cc.setCode(u.getValue().substring(40));
cc.setSystem("http://hl7.org/fhir/resource-types");
cc.setDisplay(cc.getCode());
}
}
} else if (!t.hasProfile()) {
ValueSetExpansionContainsComponent cc = vs.getExpansion().addContains();
cc.setCode(t.getWorkingCode());
cc.setDisplay(t.getWorkingCode());
cc.setSystem("http://hl7.org/fhir/data-types");
} else
for (UriType u : t.getProfile()) {
ProfileUtilities pu = new ProfileUtilities(context, null, null);
StructureDefinition ps = pu.getProfile(profile, u.getValue());
if (ps != null) {
ValueSetExpansionContainsComponent cc = vs.getExpansion().addContains();
cc.setCode(u.getValue());
cc.setDisplay(ps.getType());
cc.setSystem("http://hl7.org/fhir/resource-types");
}
}
}
return vs;
}
use of org.hl7.fhir.r5.conformance.ProfileUtilities in project org.hl7.fhir.core by hapifhir.
the class StructureDefinitionValidator method validateStructureDefinition.
public void validateStructureDefinition(List<ValidationMessage> errors, Element src, NodeStack stack) {
StructureDefinition sd = null;
try {
sd = loadAsSD(src);
List<ElementDefinition> snapshot = sd.getSnapshot().getElement();
sd.setSnapshot(null);
StructureDefinition base = context.fetchResource(StructureDefinition.class, sd.getBaseDefinition());
if (warning(errors, IssueType.NOTFOUND, stack.getLiteralPath(), base != null, I18nConstants.UNABLE_TO_FIND_BASE__FOR_, sd.getBaseDefinition(), "StructureDefinition, so can't check the differential")) {
if (rule(errors, IssueType.NOTFOUND, stack.getLiteralPath(), sd.hasDerivation(), I18nConstants.SD_MUST_HAVE_DERIVATION, sd.getUrl())) {
if (sd.getDerivation() == TypeDerivationRule.CONSTRAINT) {
List<ValidationMessage> msgs = new ArrayList<>();
ProfileUtilities pu = new ProfileUtilities(context, msgs, null);
pu.setXver(xverManager);
pu.generateSnapshot(base, sd, sd.getUrl(), "http://hl7.org/fhir/R4/", sd.getName());
if (msgs.size() > 0) {
for (ValidationMessage msg : msgs) {
// we need to set the location for the context
String loc = msg.getLocation();
if (loc.contains("#")) {
msg.setLocation(stack.getLiteralPath() + ".differential.element.where(path = '" + loc.substring(loc.indexOf("#") + 1) + "')");
} else {
msg.setLocation(stack.getLiteralPath());
}
errors.add(msg);
}
}
if (!snapshot.isEmpty() && wantCheckSnapshotUnchanged) {
int was = snapshot.size();
int is = sd.getSnapshot().getElement().size();
rule(errors, IssueType.NOTFOUND, stack.getLiteralPath(), was == is, I18nConstants.SNAPSHOT_EXISTING_PROBLEM, was, is);
}
}
}
if ("constraint".equals(src.getChildValue("derivation"))) {
rule(errors, IssueType.NOTFOUND, stack.getLiteralPath(), base.getKindElement().primitiveValue().equals(src.getChildValue("kind")), I18nConstants.SD_DERIVATION_KIND_MISMATCH, base.getKindElement().primitiveValue(), src.getChildValue("kind"));
}
}
} catch (FHIRException | IOException e) {
rule(errors, IssueType.EXCEPTION, stack.getLiteralPath(), false, I18nConstants.ERROR_GENERATING_SNAPSHOT, e.getMessage());
}
List<Element> differentials = src.getChildrenByName("differential");
List<Element> snapshots = src.getChildrenByName("snapshot");
for (Element differential : differentials) {
validateElementList(errors, differential, stack.push(differential, -1, null, null), false, snapshots.size() > 0, sd);
}
for (Element snapshot : snapshots) {
validateElementList(errors, snapshot, stack.push(snapshot, -1, null, null), true, true, sd);
}
}
Aggregations