Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAT-5968: Add method to zip multiple testcases #15

Merged
merged 1 commit into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}