Skip to content

Commit

Permalink
use ClasspathUtil (#5263)
Browse files Browse the repository at this point in the history
  • Loading branch information
fil512 authored Aug 31, 2023
1 parent f38df4b commit 590e6ba
Show file tree
Hide file tree
Showing 15 changed files with 49 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
import ca.uhn.fhir.rest.server.exceptions.ResourceVersionConflictException;
import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException;
import ca.uhn.fhir.util.ClasspathUtil;
import com.google.common.collect.Lists;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.RandomStringUtils;
Expand Down Expand Up @@ -447,7 +448,7 @@ public void testCreateBuiltInProfiles() throws Exception {
String name = "profiles-resources";
ourLog.info("Uploading " + name);
String vsContents;
vsContents = IOUtils.toString(FhirResourceDaoDstu3Test.class.getResourceAsStream("/org/hl7/fhir/dstu3/model/profile/" + name + ".xml"), StandardCharsets.UTF_8);
vsContents = ClasspathUtil.loadResource("/org/hl7/fhir/dstu3/model/profile/" + name + ".xml");

bundle = myFhirContext.newXmlParser().parseResource(org.hl7.fhir.dstu3.model.Bundle.class, vsContents);
for (BundleEntryComponent i : bundle.getEntry()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ private <T extends org.hl7.fhir.dstu3.model.Resource> T find(Bundle theBundle, C
}

private Bundle loadBundle(String theFileName) throws IOException {
String req = IOUtils.toString(FhirSystemDaoDstu3Test.class.getResourceAsStream(theFileName), StandardCharsets.UTF_8);
String req = ClasspathUtil.loadResource(theFileName);
return myFhirContext.newXmlParser().parseResource(Bundle.class, req);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
import ca.uhn.fhir.rest.server.exceptions.ResourceVersionConflictException;
import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException;
import ca.uhn.fhir.util.ClasspathUtil;
import com.google.common.base.Charsets;
import com.google.common.collect.Lists;
import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -814,7 +815,7 @@ public void testCreateBuiltInProfiles() throws Exception {
String name = "profiles-resources";
ourLog.info("Uploading " + name);
String vsContents;
vsContents = IOUtils.toString(FhirResourceDaoR4Test.class.getResourceAsStream("/org/hl7/fhir/r4/model/profile/" + name + ".xml"), StandardCharsets.UTF_8);
vsContents = ClasspathUtil.loadResource("/org/hl7/fhir/r4/model/profile/" + name + ".xml");

bundle = myFhirContext.newXmlParser().parseResource(org.hl7.fhir.r4.model.Bundle.class, vsContents);
for (BundleEntryComponent i : bundle.getEntry()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2333,7 +2333,7 @@ public void testTransactionCreateWithPutUsingUrl() {

@Test
public void testTransactionCreateWithPutUsingUrl2() throws Exception {
String req = IOUtils.toString(FhirSystemDaoR4Test.class.getResourceAsStream("/r4/bundle.xml"), StandardCharsets.UTF_8);
String req = ClasspathUtil.loadResource("/r4/bundle.xml");
Bundle request = myFhirContext.newXmlParser().parseResource(Bundle.class, req);
mySystemDao.transaction(mySrd, request);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public JobInstance awaitJobHasStatus(String theBatchJobId, int theSecondsToWait,
} catch (ConditionTimeoutException e) {
String statuses = myJobPersistence.fetchInstances(100, 0)
.stream()
.map(t -> t.getJobDefinitionId() + "/" + t.getStatus().name())
.map(t -> t.getInstanceId() + " " + t.getJobDefinitionId() + "/" + t.getStatus().name())
.collect(Collectors.joining("\n"));
String currentStatus = myJobCoordinator.getInstance(theBatchJobId).getStatus().name();
fail("Job " + theBatchJobId + " still has status " + currentStatus + " - All statuses:\n" + statuses);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import ca.uhn.fhir.parser.IParserErrorHandler.IParseLocation;
import ca.uhn.fhir.parser.PatientWithExtendedContactDstu3.CustomContactComponent;
import ca.uhn.fhir.parser.XmlParserDstu2_1Test.TestPatientFor327;
import ca.uhn.fhir.util.ClasspathUtil;
import ca.uhn.fhir.util.TestUtil;
import com.google.common.collect.Sets;
import net.sf.json.JSON;
Expand Down Expand Up @@ -1222,7 +1223,7 @@ public void testOmitResourceId() {
@Test
@Disabled
public void testParseAndEncodeBundle() throws Exception {
String content = IOUtils.toString(JsonParserDstu2_1Test.class.getResourceAsStream("/bundle-example.json"), StandardCharsets.UTF_8);
String content = ClasspathUtil.loadResource("/bundle-example.json");

Bundle parsed = ourCtx.newXmlParser().parseResource(Bundle.class, content);
assertEquals("Bundle/example/_history/1", parsed.getIdElement().getValue());
Expand Down Expand Up @@ -1271,7 +1272,7 @@ public void testParseAndEncodeBundle() throws Exception {
@Test
@Disabled
public void testParseAndEncodeBundleFromXmlToJson() throws Exception {
String content = IOUtils.toString(JsonParserDstu2_1Test.class.getResourceAsStream("/bundle-example2.xml"), StandardCharsets.UTF_8);
String content = ClasspathUtil.loadResource("/bundle-example2.xml");

Bundle parsed = ourCtx.newXmlParser().parseResource(Bundle.class, content);

Expand All @@ -1296,7 +1297,7 @@ public void testParseAndEncodeBundleFromXmlToJson() throws Exception {
@Test
@Disabled
public void testParseAndEncodeBundleNewStyle() throws Exception {
String content = IOUtils.toString(JsonParserDstu2_1Test.class.getResourceAsStream("/bundle-example.json"), StandardCharsets.UTF_8);
String content = ClasspathUtil.loadResource("/bundle-example.json");

Bundle parsed = ourCtx.newJsonParser().parseResource(Bundle.class, content);
assertEquals("Bundle/example/_history/1", parsed.getIdElement().getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import ca.uhn.fhir.parser.IParserErrorHandler.IParseLocation;
import ca.uhn.fhir.parser.PatientWithCustomCompositeExtension.FooParentExtension;
import ca.uhn.fhir.rest.api.Constants;
import ca.uhn.fhir.util.ClasspathUtil;
import ca.uhn.fhir.util.TestUtil;
import com.google.common.collect.Sets;
import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -1707,7 +1708,7 @@ public void testOmitResourceId() {
@Test
@Disabled
public void testParseAndEncodeBundle() throws Exception {
String content = IOUtils.toString(XmlParserDstu2_1Test.class.getResourceAsStream("/bundle-example.xml"), StandardCharsets.UTF_8);
String content = ClasspathUtil.loadResource("/bundle-example.xml");

Bundle parsed = ourCtx.newXmlParser().parseResource(Bundle.class, content);
assertEquals("Bundle/example/_history/1", parsed.getIdElement().getValue());
Expand Down Expand Up @@ -1742,7 +1743,7 @@ public void testParseAndEncodeBundle() throws Exception {
@Test
@Disabled
public void testParseAndEncodeBundleNewStyle() throws Exception {
String content = IOUtils.toString(XmlParserDstu2_1Test.class.getResourceAsStream("/bundle-example.xml"), StandardCharsets.UTF_8);
String content = ClasspathUtil.loadResource("/bundle-example.xml");

IParser newXmlParser = ourCtx.newXmlParser();
Bundle parsed = newXmlParser.parseResource(Bundle.class, content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import ca.uhn.fhir.parser.IParserErrorHandler.IParseLocation;
import ca.uhn.fhir.rest.api.Constants;
import ca.uhn.fhir.rest.client.api.IGenericClient;
import ca.uhn.fhir.util.ClasspathUtil;
import ca.uhn.fhir.util.TestUtil;
import com.google.common.collect.Sets;
import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -1057,7 +1058,7 @@ public void testEncodeAndParseProfiledDatatype() {
public void testEncodeAndParseProfiledDatatypeChoice() throws Exception {
IParser xmlParser = ourCtx.newXmlParser();

String input = IOUtils.toString(XmlParser.class.getResourceAsStream("/medicationstatement_invalidelement.xml"), StandardCharsets.UTF_8);
String input = ClasspathUtil.loadResource("/medicationstatement_invalidelement.xml");
MedicationStatement ms = xmlParser.parseResource(MedicationStatement.class, input);
SimpleQuantityDt q = (SimpleQuantityDt) ms.getDosage().get(0).getQuantity();
assertEquals("1", q.getValueElement().getValueAsString());
Expand Down Expand Up @@ -2005,7 +2006,7 @@ public void testOmitResourceId() {

@Test
public void testParseAndEncodeBundle() throws Exception {
String content = IOUtils.toString(XmlParserDstu2Test.class.getResourceAsStream("/bundle-example.xml"), StandardCharsets.UTF_8);
String content = ClasspathUtil.loadResource("/bundle-example.xml");

Bundle parsed = ourCtx.newXmlParser().parseResource(Bundle.class, content);
assertEquals("Bundle/example/_history/1", parsed.getId().getValue());
Expand Down Expand Up @@ -2039,7 +2040,7 @@ public void testParseAndEncodeBundle() throws Exception {

@Test
public void testParseAndEncodeBundleNewStyle() throws Exception {
String content = IOUtils.toString(XmlParserDstu2Test.class.getResourceAsStream("/bundle-example.xml"), StandardCharsets.UTF_8);
String content = ClasspathUtil.loadResource("/bundle-example.xml");

IParser newXmlParser = ourCtx.newXmlParser();
ca.uhn.fhir.model.dstu2.resource.Bundle parsed = newXmlParser.parseResource(ca.uhn.fhir.model.dstu2.resource.Bundle.class, content);
Expand Down Expand Up @@ -2512,7 +2513,7 @@ public void testParseBundleWithBinary() {
*/
@Test
public void testParseBundleWithLinksOfUnknownRelation() throws Exception {
String input = IOUtils.toString(XmlParserDstu2Test.class.getResourceAsStream("/bundle_orion.xml"), StandardCharsets.UTF_8);
String input = ClasspathUtil.loadResource("/bundle_orion.xml");
ca.uhn.fhir.model.dstu2.resource.Bundle parsed = ourCtx.newXmlParser().parseResource(ca.uhn.fhir.model.dstu2.resource.Bundle.class, input);

Link link = parsed.getLink().get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import ca.uhn.fhir.parser.IParser;
import ca.uhn.fhir.parser.StrictErrorHandler;
import ca.uhn.fhir.parser.XmlParserDstu2Test.TestPatientFor327;
import ca.uhn.fhir.util.ClasspathUtil;
import ca.uhn.fhir.util.TestUtil;
import ca.uhn.fhir.validation.schematron.SchematronBaseValidator;
import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -125,7 +126,7 @@ public void testSchemaBundleValidator() throws IOException {

@Test
public void testSchemaBundleValidatorFails() throws IOException {
String res = IOUtils.toString(ResourceValidatorDstu2Test.class.getResourceAsStream("/bundle-example.json"), StandardCharsets.UTF_8);
String res = ClasspathUtil.loadResource("/bundle-example.json");
Bundle b = ourCtx.newJsonParser().parseResource(Bundle.class, res);


Expand Down Expand Up @@ -154,7 +155,7 @@ public void testSchemaBundleValidatorFails() throws IOException {

@Test
public void testSchemaBundleValidatorIsSuccessful() throws IOException {
String res = IOUtils.toString(ResourceValidatorDstu2Test.class.getResourceAsStream("/bundle-example.json"), StandardCharsets.UTF_8);
String res = ClasspathUtil.loadResource("/bundle-example.json");
Bundle b = ourCtx.newJsonParser().parseResource(Bundle.class, res);

ourLog.debug(ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(b));
Expand Down Expand Up @@ -220,7 +221,7 @@ public void testSchemaResourceValidator() throws IOException {

@Test
public void testSchematronResourceValidator() throws IOException {
String res = IOUtils.toString(ResourceValidatorDstu2Test.class.getResourceAsStream("/patient-example-dicom.json"), StandardCharsets.UTF_8);
String res = ClasspathUtil.loadResource("/patient-example-dicom.json");
Patient p = ourCtx.newJsonParser().parseResource(Patient.class, res);

FhirValidator val = ourCtx.newValidator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import ca.uhn.fhir.parser.XmlParserDstu3Test.TestPatientFor327;
import ca.uhn.fhir.parser.json.BaseJsonLikeValue.ScalarType;
import ca.uhn.fhir.parser.json.BaseJsonLikeValue.ValueType;
import ca.uhn.fhir.util.ClasspathUtil;
import ca.uhn.fhir.util.TestUtil;
import ca.uhn.fhir.validation.FhirValidator;
import ca.uhn.fhir.validation.ValidationResult;
Expand Down Expand Up @@ -147,7 +148,7 @@ public void testEncodedResourceWithIncorrectRepresentationOfDecimalTypeToJson()
*/
@Test
public void testBadMessageForUnknownElement() throws IOException {
String input = IOUtils.toString(JsonParserDstu3Test.class.getResourceAsStream("/bad_parse_bundle_1.json"), StandardCharsets.UTF_8);
String input = ClasspathUtil.loadResource("/bad_parse_bundle_1.json");

IParser p = ourCtx.newJsonParser();
p.setParserErrorHandler(new StrictErrorHandler());
Expand All @@ -165,7 +166,7 @@ public void testBadMessageForUnknownElement() throws IOException {
*/
@Test
public void testBadMessageForUnknownElement2() throws IOException {
String input = IOUtils.toString(JsonParserDstu3Test.class.getResourceAsStream("/bad_parse_bundle_2.json"), StandardCharsets.UTF_8);
String input = ClasspathUtil.loadResource("/bad_parse_bundle_2.json");

IParser p = ourCtx.newJsonParser();
p.setParserErrorHandler(new StrictErrorHandler());
Expand Down Expand Up @@ -1730,7 +1731,7 @@ public void testOverrideResourceIdWithBundleEntryFullUrlDisabled_ConfiguredOnPar
@Test
@Disabled
public void testParseAndEncodeBundle() throws Exception {
String content = IOUtils.toString(JsonParserDstu3Test.class.getResourceAsStream("/bundle-example.json"), StandardCharsets.UTF_8);
String content = ClasspathUtil.loadResource("/bundle-example.json");

Bundle parsed = ourCtx.newXmlParser().parseResource(Bundle.class, content);
assertEquals("Bundle/example/_history/1", parsed.getIdElement().getValue());
Expand Down Expand Up @@ -1779,7 +1780,7 @@ public void testParseAndEncodeBundle() throws Exception {
@Test
@Disabled
public void testParseAndEncodeBundleFromXmlToJson() throws Exception {
String content = IOUtils.toString(JsonParserDstu3Test.class.getResourceAsStream("/bundle-example2.xml"), StandardCharsets.UTF_8);
String content = ClasspathUtil.loadResource("/bundle-example2.xml");

Bundle parsed = ourCtx.newXmlParser().parseResource(Bundle.class, content);

Expand Down Expand Up @@ -1954,7 +1955,7 @@ public void testParseBundleWithBinary() {
*/
@Test
public void testParseCommunicationWithThreeTypes() throws IOException {
String content = IOUtils.toString(JsonParserDstu3Test.class.getResourceAsStream("/tara-test.json"), StandardCharsets.UTF_8);
String content = ClasspathUtil.loadResource("/tara-test.json");
Communication comm = ourCtx.newJsonParser().parseResource(Communication.class, content);

assertEquals(3, comm.getPayload().size());
Expand Down Expand Up @@ -2199,7 +2200,7 @@ public void testParseMissingArray() throws IOException {

// We're lenient so we accept it. Maybe this could change, or be a warning in future though

String input = IOUtils.toString(JsonParserDstu3Test.class.getResourceAsStream("/missing_array.json"), StandardCharsets.UTF_8);
String input = ClasspathUtil.loadResource("/missing_array.json");
RelatedPerson rp = ourCtx.newJsonParser().parseResource(RelatedPerson.class, input);
assertEquals(1, rp.getName().size());
assertEquals("Doe", rp.getName().get(0).getFamily());
Expand Down Expand Up @@ -2424,7 +2425,7 @@ public void testReportSerializeWithMatchingId() {
*/
@Test
public void testUnexpectedElementsWithUnderscoreAtStartOfName() throws Exception {
String input = IOUtils.toString(JsonParserDstu3Test.class.getResourceAsStream("/bug477.json"), StandardCharsets.UTF_8);
String input = ClasspathUtil.loadResource("/bug477.json");

IParserErrorHandler errorHandler = mock(IParserErrorHandler.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import ca.uhn.fhir.parser.FooMessageHeaderWithExplicitField.FooMessageSourceComponent;
import ca.uhn.fhir.parser.IParserErrorHandler.IParseLocation;
import ca.uhn.fhir.parser.PatientWithCustomCompositeExtension.FooParentExtension;
import ca.uhn.fhir.util.ClasspathUtil;
import ca.uhn.fhir.util.TestUtil;
import com.google.common.collect.Sets;
import org.apache.commons.io.FileUtils;
Expand Down Expand Up @@ -2387,7 +2388,7 @@ public void testOverrideResourceIdWithBundleEntryFullUrlDisabled_ConfiguredOnPar
@Test
@Disabled
public void testParseAndEncodeBundle() throws Exception {
String content = IOUtils.toString(XmlParserDstu3Test.class.getResourceAsStream("/bundle-example.xml"), StandardCharsets.UTF_8);
String content = ClasspathUtil.loadResource("/bundle-example.xml");

Bundle parsed = ourCtx.newXmlParser().parseResource(Bundle.class, content);
assertEquals("Bundle/example/_history/1", parsed.getIdElement().getValue());
Expand Down Expand Up @@ -2422,7 +2423,7 @@ public void testParseAndEncodeBundle() throws Exception {
@Test
@Disabled
public void testParseAndEncodeBundleNewStyle() throws Exception {
String content = IOUtils.toString(XmlParserDstu3Test.class.getResourceAsStream("/bundle-example.xml"), StandardCharsets.UTF_8);
String content = ClasspathUtil.loadResource("/bundle-example.xml");

IParser newXmlParser = ourCtx.newXmlParser();
Bundle parsed = newXmlParser.parseResource(Bundle.class, content);
Expand Down Expand Up @@ -3004,7 +3005,7 @@ public void testParseBundleWithBinary() {
*/
@Test
public void testParseBundleWithLinksOfUnknownRelation() throws Exception {
String input = IOUtils.toString(XmlParserDstu3Test.class.getResourceAsStream("/bundle_orion.xml"), StandardCharsets.UTF_8);
String input = ClasspathUtil.loadResource("/bundle_orion.xml");
Bundle parsed = ourCtx.newXmlParser().parseResource(Bundle.class, input);

BundleLinkComponent link = parsed.getLink().get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ca.uhn.fhir.model.api.annotation.Child;
import ca.uhn.fhir.model.api.annotation.ResourceDef;
import ca.uhn.fhir.rest.api.Constants;
import ca.uhn.fhir.util.ClasspathUtil;
import ca.uhn.fhir.util.TestUtil;
import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
Expand Down Expand Up @@ -1162,7 +1163,7 @@ public void testSimpleParse() throws DataFormatException, IOException {
@Test
public void testSimpleResourceEncode() throws IOException {

String xmlString = IOUtils.toString(JsonParser.class.getResourceAsStream("/example-patient-general-hl7orgdstu2.xml"), StandardCharsets.UTF_8);
String xmlString = ClasspathUtil.loadResource("/example-patient-general-hl7orgdstu2.xml");
Patient obs = ourCtx.newXmlParser().parseResource(Patient.class, xmlString);

List<Extension> undeclaredExtensions = obs.getContact().get(0).getName().getFamily().get(0).getExtension();
Expand All @@ -1175,7 +1176,7 @@ public void testSimpleResourceEncode() throws IOException {
String encoded = jsonParser.encodeResourceToString(obs);
ourLog.info(encoded);

String jsonString = IOUtils.toString(JsonParser.class.getResourceAsStream("/example-patient-general-hl7orgdstu2.json"), StandardCharsets.UTF_8);
String jsonString = ClasspathUtil.loadResource("/example-patient-general-hl7orgdstu2.json");

JSON expected = JSONSerializer.toJSON(jsonString);
JSON actual = JSONSerializer.toJSON(encoded.trim());
Expand Down Expand Up @@ -1232,7 +1233,7 @@ public void testParsePrimitiveExtension() {
@Test
public void testSimpleResourceEncodeWithCustomType() throws IOException, SAXException {

String jsonString = IOUtils.toString(JsonParser.class.getResourceAsStream("/example-patient-general-hl7orgdstu2.json"), StandardCharsets.UTF_8);
String jsonString = ClasspathUtil.loadResource("/example-patient-general-hl7orgdstu2.json");
MyObservationWithExtensions obs = ourCtx.newJsonParser().parseResource(MyObservationWithExtensions.class, jsonString);

{
Expand All @@ -1255,7 +1256,7 @@ public void testSimpleResourceEncodeWithCustomType() throws IOException, SAXExce
String encoded = xmlParser.encodeResourceToString(obs);
encoded = encoded.replaceAll("<!--.*-->", "").replace("\n", "").replace("\r", "").replaceAll(">\\s+<", "><");

String xmlString = IOUtils.toString(JsonParser.class.getResourceAsStream("/example-patient-general-hl7orgdstu2.xml"), StandardCharsets.UTF_8);
String xmlString = ClasspathUtil.loadResource("/example-patient-general-hl7orgdstu2.xml");
xmlString = xmlString.replaceAll("<!--.*-->", "").replace("\n", "").replace("\r", "").replaceAll(">\\s+<", "><");

ourLog.info("Expected: " + xmlString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.parser.IParser;
import ca.uhn.fhir.util.ClasspathUtil;
import org.apache.commons.io.IOUtils;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.r4.hapi.ctx.HapiWorkerContext;
Expand Down Expand Up @@ -60,7 +61,7 @@ public void getChildPropertiesPrimitiveTest() throws FHIRException {

@BeforeEach
public void setUp() throws IOException {
final String sdString = IOUtils.toString(PropertyTest.class.getResourceAsStream("/customPatientSd.xml"), StandardCharsets.UTF_8);
final String sdString = ClasspathUtil.loadResource("/customPatientSd.xml");
final IParser parser = ourCtx.newXmlParser();
sd = parser.parseResource(StructureDefinition.class, sdString);
workerContext = new HapiWorkerContext(ourCtx, ourCtx.getValidationSupport());
Expand Down
Loading

0 comments on commit 590e6ba

Please sign in to comment.