Skip to content

Commit

Permalink
Merge pull request #12 from MeasureAuthoringTool/develop
Browse files Browse the repository at this point in the history
Release 0.2.1
  • Loading branch information
nmorasb authored Jul 12, 2023
2 parents e8b9911 + 8449dd0 commit 36fbb87
Show file tree
Hide file tree
Showing 8 changed files with 205 additions and 139 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/maven-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@ jobs:
- name: Publish SNAPSHOT version to GitHub Packages
if: contains(steps.project.outputs.version, '-SNAPSHOT')
run: mvn -B --no-transfer-progress deploy -DskipTests
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
55 changes: 30 additions & 25 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,22 @@


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

<name>packaging-utility</name>
<description>A
simple packaging-utility.</description>
<description>A simple packaging-utility.</description>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>

<junit-jupiter.version>5.7.2</junit-jupiter.version>
<junit-jupiter.version>5.9.3</junit-jupiter.version>
<slf4j.version>2.0.7</slf4j.version>
<hapi.fhir.r4.version>6.4.1</hapi.fhir.r4.version>


<junit-jupiter.version>5.7.2</junit-jupiter.version>
<slf4j.version>2.0.7</slf4j.version>

</properties>


Expand All @@ -39,7 +32,7 @@
<enabled>true</enabled>
</releases>
<id>github</id>
<name>MADiE java models</name>
<name>MADiE Packaging Utility</name>
<url>
https://maven.pkg.github.com/measureauthoringtool/madie-java-models</url>
</repository>
Expand All @@ -48,8 +41,7 @@
<repository>
<id>github</id>
<name>MADiE java models</name>
<url>
https://maven.pkg.github.com/measureauthoringtool/packaging-utility</url>
<url>https://maven.pkg.github.com/measureauthoringtool/packaging-utility</url>
</repository>
</distributionManagement>

Expand All @@ -69,11 +61,6 @@
<artifactId>hapi-fhir-structures-r4</artifactId>
<version>${hapi.fhir.r4.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down Expand Up @@ -127,12 +114,6 @@
<version>5.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven
Expand Down Expand Up @@ -178,6 +159,30 @@
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>com.theoryinpractise</groupId>
<artifactId>googleformatter-maven-plugin</artifactId>
<version>1.7.3</version>
<executions>
<execution>
<id>reformat-sources</id>
<phase>process-sources</phase>
<goals>
<goal>format</goal>
</goals>
<configuration>
<includeStale>false</includeStale>
<style>GOOGLE</style>
<filterModified>false</filterModified>
<skip>false</skip>
<fixImports>false</fixImports>
<maxLineLength>100</maxLineLength>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<reporting>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ public class InternalServerException extends RuntimeException {
public InternalServerException(Exception ex) {
super(ex);
}

public InternalServerException(String message) {
super(message);
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package gov.cms.madie.packaging.utils;



import gov.cms.madie.packaging.exceptions.InternalServerException;

public interface PackagingUtility {

byte[] getZipBundle(Object export, String exportFileName) throws InternalServerException;

byte[] getZipBundle(Object export, String exportFileName) throws InternalServerException;
}
14 changes: 7 additions & 7 deletions src/main/java/gov/cms/madie/packaging/utils/ZipUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@

@Slf4j
public class ZipUtility {


public byte[] zipEntries(Map<String, byte[]> entries, ByteArrayOutputStream baos) {

ZipOutputStream zos = makeZos(baos);

public byte[] zipEntries(Map<String, byte[]> entries, ByteArrayOutputStream baos) {

ZipOutputStream zos = makeZos(baos);
try {
for (Entry<String, byte[]> reporte : entries.entrySet()) {
ZipEntry entry = new ZipEntry(reporte.getKey());
Expand All @@ -39,7 +38,8 @@ public byte[] zipEntries(Map<String, byte[]> entries, ByteArrayOutputStream baos
}
return baos.toByteArray();
}

protected ZipOutputStream makeZos(ByteArrayOutputStream baos) {
return new ZipOutputStream(baos) ;
}
return new ZipOutputStream(baos);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,29 @@ public PackagingUtilityImpl() {
private static final String TEXT_CQL = "text/cql";
private static final String CQL_DIRECTORY = "cql/";
private static final String RESOURCES_DIRECTORY = "resources/";

@Override
public byte[] getZipBundle(Object o, String exportFileName) throws InternalServerException {
if (o instanceof Export) {
Export export = (Export)o;
String measureBundle = export.getMeasureBundleJson();
IParser jsonParser = context.newJsonParser();

org.hl7.fhir.r4.model.Bundle bundle =
(org.hl7.fhir.r4.model.Bundle) jsonParser.parseResource(measureBundle);
return getZipBundle(bundle, exportFileName) ;
}
else if (o instanceof Bundle) {
Bundle bundle = (Bundle)o;
return getZipBundle(bundle, exportFileName) ;
}
else throw new InternalServerException("Calling gicore411.PackagingUtilityImpl with invalid object");

if (o instanceof Export) {
Export export = (Export) o;
String measureBundle = export.getMeasureBundleJson();
IParser jsonParser = context.newJsonParser();

org.hl7.fhir.r4.model.Bundle bundle =
(org.hl7.fhir.r4.model.Bundle) jsonParser.parseResource(measureBundle);
return getZipBundle(bundle, exportFileName);
} else if (o instanceof Bundle) {
Bundle bundle = (Bundle) o;
return getZipBundle(bundle, exportFileName);
} else
throw new InternalServerException(
"Calling gicore411.PackagingUtilityImpl with invalid object");
}

private byte[] getZipBundle(Bundle bundle, String exportFileName) throws InternalServerException {


IParser jsonParser = context.newJsonParser();
IParser xmlParser = context.newXmlParser();


if (bundle == null) {
return null;
Expand Down Expand Up @@ -97,9 +95,7 @@ private byte[] zipEntries(
for (CqlLibrary library : cqlLibraries) {
String filePath =
CQL_DIRECTORY + library.getCqlLibraryName() + "-" + library.getVersion() + ".cql";
String data = library.getCql();

entries.put(filePath, xmlBytes);
entries.put(filePath, library.getCql().getBytes());
}
// add Library Resources to Export
List<Library> libraries = getLibraryResources(bundle);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package gov.cms.madie.measure.utilities.qicore411;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;

import org.apache.commons.io.FilenameUtils;
import org.hl7.fhir.r4.model.Bundle;
import org.junit.jupiter.api.Test;

Expand All @@ -14,9 +16,17 @@
import gov.cms.madie.packaging.utils.PackagingUtility;
import gov.cms.madie.packaging.utils.qicore411.PackagingUtilityImpl;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import static org.hamcrest.CoreMatchers.is;

class PackagingUtilityImplTest {

@Test
@Test
void testGetZipExport() {
PackagingUtility utility = new PackagingUtilityImpl();
Export export = new Export();
Expand All @@ -31,22 +41,90 @@ void testGetZipExport() {

assertNotNull(results);
}

@Test
void testGetZipBundle_Error() {
PackagingUtility utility = new PackagingUtilityImpl();
Bundle bundle = new Bundle();
PackagingUtility utility = new PackagingUtilityImpl();
Bundle bundle = new Bundle();
assertThrows(InternalServerException.class, () -> utility.getZipBundle(bundle, "widget"));
}

@Test
void testGetZipBundle() {
PackagingUtility utility = new PackagingUtilityImpl();

final String json = gov.cms.madie.packaging.utils.JsonBits.BUNDLE;
Bundle bundle = FhirContext.forR4().newJsonParser().parseResource(Bundle.class, json);

assertNotNull(utility.getZipBundle(bundle, "widget"));

}

@Test
void testGetZipBundleWithLibraries() throws IOException {
PackagingUtility utility = new PackagingUtilityImpl();

final String bundleJson = gov.cms.madie.packaging.utils.JsonBits.BUNDLE;
Bundle bundle = FhirContext.forR4().newJsonParser().parseResource(Bundle.class, bundleJson);
byte[] widgets = utility.getZipBundle(bundle, "widget");
assertNotNull(widgets);

// Make some assertions on the contents of the zip
// 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();
}

assertThat(zipContents.containsKey("widget.xml"), is(true));
assertThat(
zipContents.get("widget.xml").startsWith("<Bundle xmlns=\"http://hl7.org/fhir\">"),
is(true));

assertThat(zipContents.containsKey("TestCreateNewLibrary-1.0.000.xml"), is(true));
assertThat(
zipContents
.get("TestCreateNewLibrary-1.0.000.xml")
.startsWith("<Library xmlns=\"http://hl7.org/fhir\">"),
is(true));

assertThat(zipContents.containsKey("widget.json"), is(true));
assertThat(
zipContents.get("widget.json").startsWith("{\n \"resourceType\": \"Bundle\""), is(true));

assertThat(zipContents.containsKey("widget.html"), is(true));
assertThat(zipContents.get("widget.html").startsWith("<!DOCTYPE html>"), is(true));

assertThat(zipContents.containsKey("TestCreateNewLibrary-1.0.000.cql"), is(true));
assertThat(
zipContents
.get("TestCreateNewLibrary-1.0.000.cql")
.startsWith("library TestCreateNewLibrary version '1.0.000'"),
is(true));

assertThat(zipContents.containsKey("TestCreateNewLibrary-1.0.000.json"), is(true));
assertThat(
zipContents
.get("TestCreateNewLibrary-1.0.000.json")
.startsWith("{\n \"resourceType\": \"Library\","),
is(true));
}
}
Loading

0 comments on commit 36fbb87

Please sign in to comment.