Skip to content

Commit

Permalink
Add method to zip multiple testcases (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
chubert-sb committed Jul 24, 2023
1 parent 403aa65 commit a9d70a0
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>gov.cms.madie.packaging</groupId>
<artifactId>packaging-utility</artifactId>
<version>0.2.2</version>
<version>0.2.3</version>

<name>packaging-utility</name>
<description>A simple packaging-utility.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -76,6 +80,30 @@ private byte[] getZipBundle(Bundle bundle, String exportFileName) throws Interna
}
}

private byte[] getTestCaseZipBundle(Map<String, Bundle> exportBundles)
throws InternalServerException {

IParser jsonParser = context.newJsonParser();

if (exportBundles.isEmpty()) {
return null;
}

Map<String, byte[]> 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<String, byte[]> entries = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,25 @@ private Map<String, String> 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<String, Bundle> multiTestCases = new HashMap<>();
multiTestCases.put("TC1", testCaseBundle1);
multiTestCases.put("TC2", testCaseBundle2);
byte[] tc1 = utility.getZipBundle(multiTestCases, null);
assertNotNull(tc1);

Map<String, String> zipContents = getZipContents(tc1);
assertThat(zipContents.size(), is(2));
assertThat(zipContents.containsKey("TC1.json"), is(true));
assertThat(zipContents.containsKey("TC2.json"), is(true));
}
}

0 comments on commit a9d70a0

Please sign in to comment.