diff --git a/src/main/java/gov/cms/madie/services/PackagingService.java b/src/main/java/gov/cms/madie/services/PackagingService.java index c590290..35c0f25 100644 --- a/src/main/java/gov/cms/madie/services/PackagingService.java +++ b/src/main/java/gov/cms/madie/services/PackagingService.java @@ -41,23 +41,25 @@ public byte[] createMeasurePackage(Measure measure, String accessToken) { Map entries = new HashMap<>(); for (TranslatedLibrary translatedLibrary : translatedLibraries) { String entryName = translatedLibrary.getName() + "-" + translatedLibrary.getVersion(); - entries.put(resourcesDir + entryName + ".json", translatedLibrary.getElmJson().getBytes()); - entries.put(resourcesDir + entryName + ".xml", translatedLibrary.getElmXml().getBytes()); - entries.put(cqlDir + entryName + ".cql", translatedLibrary.getCql().getBytes()); + entries.put( + resourcesDir + entryName.trim() + ".json", translatedLibrary.getElmJson().getBytes()); + entries.put( + resourcesDir + entryName.trim() + ".xml", translatedLibrary.getElmXml().getBytes()); + entries.put(cqlDir + entryName.trim() + ".cql", translatedLibrary.getCql().getBytes()); } CqlLookups cqlLookups = translationServiceClient.getCqlLookups(qdmMeasure, accessToken); final String humanReadable = humanReadableService.generate(measure, cqlLookups); if (humanReadable != null) { entries.put( - measure.getEcqmTitle() + "-v" + measure.getVersion() + "-QDM" + ".html", + measure.getEcqmTitle().trim() + "-v" + measure.getVersion() + "-QDM" + ".html", humanReadable.getBytes()); } - String hqmf = hqmfService.generateHqmf(qdmMeasure, cqlLookups); - entries.put( - measure.getEcqmTitle() + "-v" + measure.getVersion() + "-QDM" + ".xml", hqmf.getBytes()); - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - return new ZipUtility().zipEntries(entries, outputStream); + String hqmf = hqmfService.generateHqmf(qdmMeasure, cqlLookups); + entries.put( + measure.getEcqmTitle().trim() + "-v" + measure.getVersion() + "-QDM" + ".xml", + hqmf.getBytes()); + return new ZipUtility().zipEntries(entries, new ByteArrayOutputStream()); } public byte[] createQRDA(QrdaRequestDTO qrdaRequestDTO, String accessToken) { diff --git a/src/test/java/gov/cms/madie/services/PackagingServiceTest.java b/src/test/java/gov/cms/madie/services/PackagingServiceTest.java index 061fa73..f546710 100644 --- a/src/test/java/gov/cms/madie/services/PackagingServiceTest.java +++ b/src/test/java/gov/cms/madie/services/PackagingServiceTest.java @@ -142,13 +142,13 @@ void testCreateMeasurePackageWhenHqmfGenerationFailed() { when(humanReadableService.generate(any(Measure.class), any(CqlLookups.class))) .thenReturn("success"); - Mockito.doThrow(new PackagingException("An error occurred that caused the HQMF generation to fail")) + Mockito.doThrow( + new PackagingException("An error occurred that caused the HQMF generation to fail")) .when(hqmfService) .generateHqmf(any(QdmMeasure.class), any(CqlLookups.class)); Exception exception = assertThrows( - PackagingException.class, - () -> packagingService.createMeasurePackage(measure, TOKEN)); + PackagingException.class, () -> packagingService.createMeasurePackage(measure, TOKEN)); assertThat( exception.getMessage(), containsString("An error occurred that caused the HQMF generation to fail")); @@ -234,4 +234,37 @@ void testCreateQrdaHandlesNullHtmlEntry() { assertThat(new String(qrda), containsString("html/")); assertThat(new String(qrda), containsString("2_test.html")); } + + @Test + void trimFileNameValues() { + QdmMeasure msr = measure.toBuilder().ecqmTitle(" test ").build(); + TranslatedLibrary library1 = + TranslatedLibrary.builder() + .name("Lib one") + .version("0.0.000") + .elmJson("elm xml") + .elmXml("elm xml") + .cql("cql") + .build(); + TranslatedLibrary library2 = + TranslatedLibrary.builder() + .name("Lib two") + .version("0.0.001") + .elmJson("elm xml") + .elmXml("elm xml") + .cql("cql") + .build(); + CqlLookups cqlLookups = CqlLookups.builder().build(); + when(translationServiceClient.getTranslatedLibraries(msr.getCql(), TOKEN)) + .thenReturn(List.of(library1, library2)); + when(translationServiceClient.getCqlLookups(any(QdmMeasure.class), anyString())) + .thenReturn(CqlLookups.builder().build()); + when(hqmfService.generateHqmf(msr, cqlLookups)).thenReturn("this is a test hqmf"); + when(humanReadableService.generate(any(Measure.class), any(CqlLookups.class))) + .thenReturn("success"); + byte[] packageContents = packagingService.createMeasurePackage(msr, TOKEN); + String packageString = new String(packageContents); + assertThat(packageString, containsString("test-v1.2.003-QDM.html")); + assertThat(packageString, containsString("test-v1.2.003-QDM.xml")); + } }