From 93a83fdfc80a38dd238a22f5ac0b1ca277610261 Mon Sep 17 00:00:00 2001 From: chubert-sb <85900348+chubert-sb@users.noreply.github.com> Date: Mon, 24 Jul 2023 14:06:33 -0400 Subject: [PATCH] Add method to zip multiple testcases (#15) (#16) --- pom.xml | 2 +- .../utils/qicore411/PackagingUtilityImpl.java | 28 +++++++++++++++++++ .../qicore411/PackagingUtilityImplTest.java | 21 ++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 257170f..a5a0683 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ 4.0.0 gov.cms.madie.packaging packaging-utility - 0.2.2 + 0.2.3 packaging-utility A simple packaging-utility. diff --git a/src/main/java/gov/cms/madie/packaging/utils/qicore411/PackagingUtilityImpl.java b/src/main/java/gov/cms/madie/packaging/utils/qicore411/PackagingUtilityImpl.java index 35584a0..085315e 100644 --- a/src/main/java/gov/cms/madie/packaging/utils/qicore411/PackagingUtilityImpl.java +++ b/src/main/java/gov/cms/madie/packaging/utils/qicore411/PackagingUtilityImpl.java @@ -5,6 +5,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import org.apache.commons.lang3.StringUtils; import org.hl7.fhir.r4.model.Attachment; @@ -46,6 +47,9 @@ public byte[] getZipBundle(Object o, String exportFileName) throws InternalServe } else if (o instanceof Bundle) { Bundle bundle = (Bundle) o; return getZipBundle(bundle, exportFileName); + } else if (o instanceof Map) { + Map map = (Map) o; + return getTestCaseZipBundle(map); } else throw new InternalServerException( "Calling gicore411.PackagingUtilityImpl with invalid object"); @@ -76,6 +80,30 @@ private byte[] getZipBundle(Bundle bundle, String exportFileName) throws Interna } } + private byte[] getTestCaseZipBundle(Map exportBundles) + throws InternalServerException { + + IParser jsonParser = context.newJsonParser(); + + if (exportBundles.isEmpty()) { + return null; + } + + Map entries = + exportBundles.entrySet().stream() + .collect( + Collectors.toMap( + entry -> entry.getKey() + ".json", + entry -> + jsonParser + .setPrettyPrint(true) + .encodeResourceToString(entry.getValue()) + .getBytes())); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + return new ZipUtility().zipEntries(entries, baos); + } + private byte[] zipEntries(String exportFileName, IParser jsonParser, Bundle bundle) { Map entries = new HashMap<>(); diff --git a/src/test/java/gov/cms/madie/measure/utilities/qicore411/PackagingUtilityImplTest.java b/src/test/java/gov/cms/madie/measure/utilities/qicore411/PackagingUtilityImplTest.java index 6c998f9..0a77ee0 100644 --- a/src/test/java/gov/cms/madie/measure/utilities/qicore411/PackagingUtilityImplTest.java +++ b/src/test/java/gov/cms/madie/measure/utilities/qicore411/PackagingUtilityImplTest.java @@ -146,4 +146,25 @@ private Map getZipContents(byte[] inputBytes) throws IOException } return zipContents; } + + @Test + void testGetZipBundleForMultipleTestCases() throws IOException { + PackagingUtility utility = new PackagingUtilityImpl(); + String testCaseBundleJson1 = getStringFromTestResource("/testCaseBundle.json"); + String testCaseBundleJson2 = getStringFromTestResource("/testCaseBundle.json"); + Bundle testCaseBundle1 = + FhirContext.forR4().newJsonParser().parseResource(Bundle.class, testCaseBundleJson1); + Bundle testCaseBundle2 = + FhirContext.forR4().newJsonParser().parseResource(Bundle.class, testCaseBundleJson2); + Map multiTestCases = new HashMap<>(); + multiTestCases.put("TC1", testCaseBundle1); + multiTestCases.put("TC2", testCaseBundle2); + byte[] tc1 = utility.getZipBundle(multiTestCases, null); + assertNotNull(tc1); + + Map zipContents = getZipContents(tc1); + assertThat(zipContents.size(), is(2)); + assertThat(zipContents.containsKey("TC1.json"), is(true)); + assertThat(zipContents.containsKey("TC2.json"), is(true)); + } }