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

Release 0.2.2 #14

Merged
merged 5 commits into from
Jul 12, 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
11 changes: 4 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>


<groupId>gov.cms.madie.packaging</groupId>
<artifactId>packaging-utility</artifactId>
<version>0.2.1</version>
<version>0.2.2</version>

<name>packaging-utility</name>
<description>A simple packaging-utility.</description>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<url>http://madie.cms.gov</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>17</java.version>
Expand All @@ -32,15 +29,15 @@
<enabled>true</enabled>
</releases>
<id>github</id>
<name>MADiE Packaging Utility</name>
<name>MADiE java models</name>
<url>
https://maven.pkg.github.com/measureauthoringtool/madie-java-models</url>
</repository>
</repositories>
<distributionManagement>
<repository>
<id>github</id>
<name>MADiE java models</name>
<name>MADiE Packaging Utility</name>
<url>https://maven.pkg.github.com/measureauthoringtool/packaging-utility</url>
</repository>
</distributionManagement>
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/gov/cms/madie/packaging/utils/ResourceFileUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package gov.cms.madie.packaging.utils;

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.util.Objects;

public interface ResourceFileUtil {
default String getStringFromTestResource(String resource) {
File file = new File(Objects.requireNonNull(this.getClass().getResource(resource)).getFile());

try {
return new String(Files.readAllBytes(file.toPath()));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,31 @@ private byte[] getZipBundle(Bundle bundle, String exportFileName) throws Interna
if (bundle == null) {
return null;
}
org.hl7.fhir.r4.model.DomainResource measure =
(org.hl7.fhir.r4.model.DomainResource) ResourceUtils.getResource(bundle, "Measure");
if (measure == null) {
throw new InternalServerException("Measure is Null");
if (ResourceUtils.isMeasureBundle(bundle)) {
org.hl7.fhir.r4.model.DomainResource measure =
(org.hl7.fhir.r4.model.DomainResource) ResourceUtils.getResource(bundle, "Measure");
String humanReadable = measure.getText().getDivAsString();

String template = ResourceUtils.getData("/templates/HumanReadable.liquid");
String humanReadableWithCSS =
template.replace("human_readable_content_holder", humanReadable);

return zipEntries(exportFileName, jsonParser, xmlParser, bundle, humanReadableWithCSS);
} else if (ResourceUtils.isPatientBundle(bundle)) {
return zipEntries(exportFileName, jsonParser, bundle);
} else {
throw new InternalServerException("Unable to find Measure or Patient Bundle");
}
String humanReadable = measure.getText().getDivAsString();
}

String template = ResourceUtils.getData("/templates/HumanReadable.liquid");
String humanReadableWithCSS = template.replace("human_readable_content_holder", humanReadable);
private byte[] zipEntries(String exportFileName, IParser jsonParser, Bundle bundle) {
Map<String, byte[]> entries = new HashMap<>();

byte[] result = zipEntries(exportFileName, jsonParser, xmlParser, bundle, humanReadableWithCSS);
return result;
byte[] jsonBytes = jsonParser.setPrettyPrint(true).encodeResourceToString(bundle).getBytes();
entries.put(exportFileName + ".json", jsonBytes);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
return new ZipUtility().zipEntries(entries, baos);
}

private byte[] zipEntries(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,26 @@ public static Resource getResource(Bundle bundleResource, String resourceType) {
.findFirst();
return measureEntry.map(Bundle.BundleEntryComponent::getResource).orElse(null);
}

public static boolean isMeasureBundle(Bundle bundleResource) {
if (bundleResource == null) {
return false;
}
return bundleResource.getEntry().stream()
.anyMatch(
entry ->
StringUtils.equalsIgnoreCase(
"Measure", entry.getResource().getResourceType().toString()));
}

public static boolean isPatientBundle(Bundle bundleResource) {
if (bundleResource == null) {
return false;
}
return bundleResource.getEntry().stream()
.anyMatch(
entry ->
StringUtils.equalsIgnoreCase(
"Patient", entry.getResource().getResourceType().toString()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;

import gov.cms.madie.packaging.utils.ResourceFileUtil;
import org.apache.commons.io.FilenameUtils;
import org.hl7.fhir.r4.model.Bundle;
import org.junit.jupiter.api.Test;
Expand All @@ -24,7 +25,7 @@

import static org.hamcrest.CoreMatchers.is;

class PackagingUtilityImplTest {
class PackagingUtilityImplTest implements ResourceFileUtil {

@Test
void testGetZipExport() {
Expand Down Expand Up @@ -71,28 +72,7 @@ void testGetZipBundleWithLibraries() throws IOException {
// Doing this in-memory to prevent writing to file system on build servers..this may need to
// change
// in the future if the test bundle is too large
Map<String, String> zipContents = new HashMap<>();

try (var zipInputStream = new ZipInputStream(new ByteArrayInputStream(widgets))) {
ZipEntry entry;
byte[] buffer = new byte[2048];

while ((entry = zipInputStream.getNextEntry()) != null) {
int size;
String filename = FilenameUtils.getName(entry.getName());
var byteArrayOutputStream = new ByteArrayOutputStream();
while ((size = zipInputStream.read(buffer)) > 0) {
byteArrayOutputStream.write(buffer, 0, size);
}

String fileContents = byteArrayOutputStream.toString();
byteArrayOutputStream.flush();
zipInputStream.closeEntry();
zipContents.put(filename, fileContents);
}

zipInputStream.closeEntry();
}
Map<String, String> zipContents = getZipContents(widgets);

assertThat(zipContents.containsKey("widget.xml"), is(true));
assertThat(
Expand Down Expand Up @@ -127,4 +107,43 @@ void testGetZipBundleWithLibraries() throws IOException {
.startsWith("{\n \"resourceType\": \"Library\","),
is(true));
}

@Test
void testGetZipBundleForTestCases() throws IOException {
PackagingUtility utility = new PackagingUtilityImpl();
String testCaseBundleJson = getStringFromTestResource("/testCaseBundle.json");
Bundle testCaseBundle =
FhirContext.forR4().newJsonParser().parseResource(Bundle.class, testCaseBundleJson);
byte[] tc1 = utility.getZipBundle(testCaseBundle, "TC1");
assertNotNull(tc1);

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

private Map<String, String> getZipContents(byte[] inputBytes) throws IOException {
Map<String, String> zipContents = new HashMap<>();
try (var zipInputStream = new ZipInputStream(new ByteArrayInputStream(inputBytes))) {
ZipEntry entry;
byte[] buffer = new byte[2048];

while ((entry = zipInputStream.getNextEntry()) != null) {
int size;
String filename = FilenameUtils.getName(entry.getName());
var byteArrayOutputStream = new ByteArrayOutputStream();
while ((size = zipInputStream.read(buffer)) > 0) {
byteArrayOutputStream.write(buffer, 0, size);
}

String fileContents = byteArrayOutputStream.toString();
byteArrayOutputStream.flush();
zipInputStream.closeEntry();
zipContents.put(filename, fileContents);
}

zipInputStream.closeEntry();
}
return zipContents;
}
}
Loading