From 3a18c667774051923e4130af4cd75fbbee5387f0 Mon Sep 17 00:00:00 2001 From: CeciliaLiu8 Date: Wed, 17 Jan 2024 14:20:35 -0600 Subject: [PATCH 1/2] MAT-6175 get human readable for export --- .../config/CqlElmTranslatorClientConfig.java | 3 ++ .../cms/madie/services/PackagingService.java | 6 ++++ .../services/TranslationServiceClient.java | 23 +++++++++++++ src/main/resources/application.yml | 1 + .../madie/services/PackagingServiceTest.java | 8 +++++ .../TranslationServiceClientTest.java | 32 +++++++++++++++++++ 6 files changed, 73 insertions(+) diff --git a/src/main/java/gov/cms/madie/config/CqlElmTranslatorClientConfig.java b/src/main/java/gov/cms/madie/config/CqlElmTranslatorClientConfig.java index 7a529fe..fd4acd7 100644 --- a/src/main/java/gov/cms/madie/config/CqlElmTranslatorClientConfig.java +++ b/src/main/java/gov/cms/madie/config/CqlElmTranslatorClientConfig.java @@ -16,6 +16,9 @@ public class CqlElmTranslatorClientConfig { @Value("${madie.cql-elm.service.cql-elm-urn}") private String cqlElmUrn; + @Value("${madie.cql-elm.service.human-readable}") + private String humanReadableUrl; + @Bean public RestTemplate elmTranslatorRestTemplate() { return new RestTemplate(); diff --git a/src/main/java/gov/cms/madie/services/PackagingService.java b/src/main/java/gov/cms/madie/services/PackagingService.java index d4c4af6..127dde3 100644 --- a/src/main/java/gov/cms/madie/services/PackagingService.java +++ b/src/main/java/gov/cms/madie/services/PackagingService.java @@ -36,6 +36,12 @@ public byte[] createMeasurePackage(Measure measure, String accessToken) { entries.put(resourcesDir + entryName + ".xml", translatedLibrary.getElmXml().getBytes()); entries.put(cqlDir + entryName + ".cql", translatedLibrary.getCql().getBytes()); } + String humanReadable = translationServiceClient.getMeasureBundleExport(measure, accessToken); + if (humanReadable != null) { + entries.put( + measure.getEcqmTitle() + "-v" + measure.getVersion() + "-QDM" + ".html", + humanReadable.getBytes()); + } ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); return new ZipUtility().zipEntries(entries, outputStream); } diff --git a/src/main/java/gov/cms/madie/services/TranslationServiceClient.java b/src/main/java/gov/cms/madie/services/TranslationServiceClient.java index 0503f6f..35f3663 100644 --- a/src/main/java/gov/cms/madie/services/TranslationServiceClient.java +++ b/src/main/java/gov/cms/madie/services/TranslationServiceClient.java @@ -3,6 +3,7 @@ import gov.cms.madie.Exceptions.TranslationServiceException; import gov.cms.madie.config.CqlElmTranslatorClientConfig; import gov.cms.madie.models.dto.TranslatedLibrary; +import gov.cms.madie.models.measure.Measure; import lombok.extern.slf4j.Slf4j; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpEntity; @@ -41,4 +42,26 @@ public List getTranslatedLibraries(String cql, String accessT throw new TranslationServiceException(msg, ex); } } + + public String getMeasureBundleExport(Measure measure, String accessToken) { + URI uri = + URI.create( + translatorClientConfig.getBaseUrl() + translatorClientConfig.getHumanReadableUrl()); + + HttpHeaders headers = new HttpHeaders(); + headers.set(HttpHeaders.AUTHORIZATION, accessToken); + HttpEntity measureEntity = new HttpEntity<>(measure, headers); + try { + log.info("fetching measure bundle human readable"); + return elmTranslatorRestTemplate + .exchange(uri, HttpMethod.PUT, measureEntity, String.class) + .getBody(); + } catch (Exception ex) { + String msg = + "An issue occurred while fetching measure bundle human readable for measure id: " + + measure.getId(); + log.error(msg, ex); + throw new TranslationServiceException(msg, ex); + } + } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index fc6ab43..5f67b4c 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -9,6 +9,7 @@ madie: service: base-url: ${ELM_TRANSLATOR_SERVICE_URL:http://localhost:8084/api} cql-elm-urn: /cql/elm + human-readable: /human-readable management: endpoints: diff --git a/src/test/java/gov/cms/madie/services/PackagingServiceTest.java b/src/test/java/gov/cms/madie/services/PackagingServiceTest.java index dc47661..6448ea1 100644 --- a/src/test/java/gov/cms/madie/services/PackagingServiceTest.java +++ b/src/test/java/gov/cms/madie/services/PackagingServiceTest.java @@ -36,6 +36,12 @@ void setUp() throws Exception { Measure.builder() .id("1") .ecqmTitle("test") + .version( + gov.cms.madie.models.common.Version.builder() + .major(1) + .minor(2) + .revisionNumber(3) + .build()) .cql("fake cql") .model(String.valueOf(ModelType.QDM_5_6)) .build(); @@ -61,12 +67,14 @@ void testCreateMeasurePackage() { .build(); when(translationServiceClient.getTranslatedLibraries(measure.getCql(), TOKEN)) .thenReturn(List.of(library1, library2)); + when(translationServiceClient.getMeasureBundleExport(measure, TOKEN)).thenReturn("success"); byte[] packageContents = packagingService.createMeasurePackage(measure, TOKEN); String packageString = new String(packageContents); String library1FileName = library1.getName() + "-" + library1.getVersion(); assertThat(packageString, containsString(library1FileName + ".cql")); assertThat(packageString, containsString(library1FileName + ".xml")); assertThat(packageString, containsString(library1FileName + ".json")); + assertThat(packageString, containsString("test-v1.2.003-QDM.html")); } @Test diff --git a/src/test/java/gov/cms/madie/services/TranslationServiceClientTest.java b/src/test/java/gov/cms/madie/services/TranslationServiceClientTest.java index a508033..13dd016 100644 --- a/src/test/java/gov/cms/madie/services/TranslationServiceClientTest.java +++ b/src/test/java/gov/cms/madie/services/TranslationServiceClientTest.java @@ -3,6 +3,8 @@ import gov.cms.madie.Exceptions.TranslationServiceException; import gov.cms.madie.config.CqlElmTranslatorClientConfig; import gov.cms.madie.models.dto.TranslatedLibrary; +import gov.cms.madie.models.measure.Measure; + import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -70,4 +72,34 @@ void getTranslatedLibrariesWhenTranslationServiceFailed() { message); assertThat(ex.getMessage(), containsString(message)); } + + @Test + void testGetMeasureBundleExportSuccess() { + when(translatorClientConfig.getBaseUrl()).thenReturn("baseurl"); + when(translatorClientConfig.getHumanReadableUrl()).thenReturn("/human-readable"); + when(elmTranslatorRestTemplate.exchange( + any(URI.class), eq(HttpMethod.PUT), any(HttpEntity.class), any(Class.class))) + .thenReturn(ResponseEntity.ok("success")); + + String humanReadable = + translationServiceClient.getMeasureBundleExport(Measure.builder().build(), "token"); + assertThat(humanReadable, is(equalTo("success"))); + } + + @Test + void testGetMeasureBundleExportFailure() { + String message = + "An issue occurred while fetching measure bundle human readable for measure id: testMeasureId"; + when(elmTranslatorRestTemplate.exchange( + any(URI.class), eq(HttpMethod.PUT), any(HttpEntity.class), any(Class.class))) + .thenThrow(new TranslationServiceException(message, new Exception())); + Exception ex = + assertThrows( + TranslationServiceException.class, + () -> + translationServiceClient.getMeasureBundleExport( + Measure.builder().id("testMeasureId").build(), "token"), + message); + assertThat(ex.getMessage(), containsString(message)); + } } From 33031f2ca5202af705e35d52ca871ea7f3c140c9 Mon Sep 17 00:00:00 2001 From: CeciliaLiu8 Date: Wed, 17 Jan 2024 15:25:00 -0600 Subject: [PATCH 2/2] change method name --- .../java/gov/cms/madie/services/PackagingService.java | 2 +- .../cms/madie/services/TranslationServiceClient.java | 7 +++---- .../gov/cms/madie/services/PackagingServiceTest.java | 2 +- .../madie/services/TranslationServiceClientTest.java | 10 +++++----- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/main/java/gov/cms/madie/services/PackagingService.java b/src/main/java/gov/cms/madie/services/PackagingService.java index 127dde3..235652f 100644 --- a/src/main/java/gov/cms/madie/services/PackagingService.java +++ b/src/main/java/gov/cms/madie/services/PackagingService.java @@ -36,7 +36,7 @@ public byte[] createMeasurePackage(Measure measure, String accessToken) { entries.put(resourcesDir + entryName + ".xml", translatedLibrary.getElmXml().getBytes()); entries.put(cqlDir + entryName + ".cql", translatedLibrary.getCql().getBytes()); } - String humanReadable = translationServiceClient.getMeasureBundleExport(measure, accessToken); + String humanReadable = translationServiceClient.getHumanReadable(measure, accessToken); if (humanReadable != null) { entries.put( measure.getEcqmTitle() + "-v" + measure.getVersion() + "-QDM" + ".html", diff --git a/src/main/java/gov/cms/madie/services/TranslationServiceClient.java b/src/main/java/gov/cms/madie/services/TranslationServiceClient.java index 35f3663..a3badc0 100644 --- a/src/main/java/gov/cms/madie/services/TranslationServiceClient.java +++ b/src/main/java/gov/cms/madie/services/TranslationServiceClient.java @@ -43,7 +43,7 @@ public List getTranslatedLibraries(String cql, String accessT } } - public String getMeasureBundleExport(Measure measure, String accessToken) { + public String getHumanReadable(Measure measure, String accessToken) { URI uri = URI.create( translatorClientConfig.getBaseUrl() + translatorClientConfig.getHumanReadableUrl()); @@ -52,14 +52,13 @@ public String getMeasureBundleExport(Measure measure, String accessToken) { headers.set(HttpHeaders.AUTHORIZATION, accessToken); HttpEntity measureEntity = new HttpEntity<>(measure, headers); try { - log.info("fetching measure bundle human readable"); + log.info("fetching human readable"); return elmTranslatorRestTemplate .exchange(uri, HttpMethod.PUT, measureEntity, String.class) .getBody(); } catch (Exception ex) { String msg = - "An issue occurred while fetching measure bundle human readable for measure id: " - + measure.getId(); + "An issue occurred while fetching human readable for measure id: " + measure.getId(); log.error(msg, ex); throw new TranslationServiceException(msg, ex); } diff --git a/src/test/java/gov/cms/madie/services/PackagingServiceTest.java b/src/test/java/gov/cms/madie/services/PackagingServiceTest.java index 6448ea1..f4ff7df 100644 --- a/src/test/java/gov/cms/madie/services/PackagingServiceTest.java +++ b/src/test/java/gov/cms/madie/services/PackagingServiceTest.java @@ -67,7 +67,7 @@ void testCreateMeasurePackage() { .build(); when(translationServiceClient.getTranslatedLibraries(measure.getCql(), TOKEN)) .thenReturn(List.of(library1, library2)); - when(translationServiceClient.getMeasureBundleExport(measure, TOKEN)).thenReturn("success"); + when(translationServiceClient.getHumanReadable(measure, TOKEN)).thenReturn("success"); byte[] packageContents = packagingService.createMeasurePackage(measure, TOKEN); String packageString = new String(packageContents); String library1FileName = library1.getName() + "-" + library1.getVersion(); diff --git a/src/test/java/gov/cms/madie/services/TranslationServiceClientTest.java b/src/test/java/gov/cms/madie/services/TranslationServiceClientTest.java index 13dd016..ba6a612 100644 --- a/src/test/java/gov/cms/madie/services/TranslationServiceClientTest.java +++ b/src/test/java/gov/cms/madie/services/TranslationServiceClientTest.java @@ -74,7 +74,7 @@ void getTranslatedLibrariesWhenTranslationServiceFailed() { } @Test - void testGetMeasureBundleExportSuccess() { + void testGetHumanReadableSuccess() { when(translatorClientConfig.getBaseUrl()).thenReturn("baseurl"); when(translatorClientConfig.getHumanReadableUrl()).thenReturn("/human-readable"); when(elmTranslatorRestTemplate.exchange( @@ -82,14 +82,14 @@ void testGetMeasureBundleExportSuccess() { .thenReturn(ResponseEntity.ok("success")); String humanReadable = - translationServiceClient.getMeasureBundleExport(Measure.builder().build(), "token"); + translationServiceClient.getHumanReadable(Measure.builder().build(), "token"); assertThat(humanReadable, is(equalTo("success"))); } @Test - void testGetMeasureBundleExportFailure() { + void testGetHumanReadableFailure() { String message = - "An issue occurred while fetching measure bundle human readable for measure id: testMeasureId"; + "An issue occurred while fetching human readable for measure id: testMeasureId"; when(elmTranslatorRestTemplate.exchange( any(URI.class), eq(HttpMethod.PUT), any(HttpEntity.class), any(Class.class))) .thenThrow(new TranslationServiceException(message, new Exception())); @@ -97,7 +97,7 @@ void testGetMeasureBundleExportFailure() { assertThrows( TranslationServiceException.class, () -> - translationServiceClient.getMeasureBundleExport( + translationServiceClient.getHumanReadable( Measure.builder().id("testMeasureId").build(), "token"), message); assertThat(ex.getMessage(), containsString(message));