From a6f3ee83199e53d34d7a881c3cad16c081b637bc Mon Sep 17 00:00:00 2001 From: James Agnew Date: Wed, 29 Jan 2020 09:35:22 -0500 Subject: [PATCH] Add a unit test --- .../jpa/stresstest/StressTestParserTest.java | 47 ++++++++++++------- .../ca/uhn/fhir/parser/JsonParserR4Test.java | 13 +++++ 2 files changed, 43 insertions(+), 17 deletions(-) diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/stresstest/StressTestParserTest.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/stresstest/StressTestParserTest.java index 99a9759f409..493d493239e 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/stresstest/StressTestParserTest.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/stresstest/StressTestParserTest.java @@ -20,27 +20,40 @@ public class StressTestParserTest extends BaseTest { FhirContext ctx = FhirContext.forR4(); String input = loadResource("/org/hl7/fhir/r4/model/valueset/valuesets.xml"); - String json = ctx.newJsonParser().encodeResourceToString(ctx.newXmlParser().parseResource(input)); + Bundle parsed = ctx.newXmlParser().parseResource(Bundle.class, input); + String json = ctx.newJsonParser().encodeResourceToString(parsed); StopWatch sw = null; - int loops = 100; + int loops = 200; + +// for (int i = 0; i < loops; i++) { +// ctx.newXmlParser().parseResource(input); +// if (i < 50) { +// ourLog.info("Parsed XML {} times", i); +// continue; +// } else if (i == 50) { +// sw = new StopWatch(); +// continue; +// } +// ourLog.info("Parsed XML {} times - {}ms/pass", i, sw.getMillisPerOperation(i - 50)); +// } + +// for (int i = 0; i < loops; i++) { +// Bundle parsed = (Bundle) ctx.newJsonParser().parseResource(json); +// if (i < 50) { +// ourLog.info("Parsed JSON with {} entries {} times", parsed.getEntry().size(), i); +// continue; +// } else if (i == 50) { +// sw = new StopWatch(); +// continue; +// } +// ourLog.info("Parsed JSON {} times - {}ms/pass", i, sw.getMillisPerOperation(i - 50)); +// } for (int i = 0; i < loops; i++) { - ctx.newXmlParser().parseResource(input); + ctx.newJsonParser().encodeResourceToString(parsed); if (i < 50) { - ourLog.info("Parsed XML {} times", i); - continue; - } else if (i == 50) { - sw = new StopWatch(); - continue; - } - ourLog.info("Parsed XML {} times - {}ms/pass", i, sw.getMillisPerOperation(i - 50)); - } - - for (int i = 0; i < loops; i++) { - Bundle parsed = (Bundle) ctx.newJsonParser().parseResource(json); - if (i < 50) { - ourLog.info("Parsed JSON with {} entries {} times", parsed.getEntry().size(), i); + ourLog.info("Serialized JSON with {} entries {} times", parsed.getEntry().size(), i); continue; } else if (i == 50) { sw = new StopWatch(); @@ -49,6 +62,6 @@ public class StressTestParserTest extends BaseTest { ourLog.info("Parsed JSON {} times - {}ms/pass", i, sw.getMillisPerOperation(i - 50)); } - } + } diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/parser/JsonParserR4Test.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/parser/JsonParserR4Test.java index cc5dfa8e9d7..2f3d7d92a5b 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/parser/JsonParserR4Test.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/parser/JsonParserR4Test.java @@ -79,6 +79,19 @@ public class JsonParserR4Test extends BaseTest { assertThat(output, containsString("\"Questionnaire/123/_history/456\"")); } + @Test + public void testPrettyPrint() { + ourCtx.getParserOptions().setDontStripVersionsFromReferencesAtPaths("QuestionnaireResponse.questionnaire"); + + QuestionnaireResponse qr = new QuestionnaireResponse(); + qr.getQuestionnaireElement().setValueAsString("Questionnaire/123/_history/456"); + + String output = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(qr); + ourLog.info(output); + + assertThat(output, containsString("\n \"resourceType\"")); + } + /** * See #814 */