Skip to content

Commit

Permalink
logic fix for library CQL in zip file, unit test, cleaning up pom and…
Browse files Browse the repository at this point in the history
… add formatting utility from other projects
  • Loading branch information
nmorasb committed Jul 11, 2023
1 parent 3a540bb commit 3c41bc1
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 133 deletions.
46 changes: 27 additions & 19 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@


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

<name>packaging-utility</name>
<description>A
Expand All @@ -21,13 +20,9 @@
<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 Down Expand Up @@ -69,11 +64,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 +117,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 +162,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 3c41bc1

Please sign in to comment.