Skip to content

Commit

Permalink
MAT-6175 get human readable for export
Browse files Browse the repository at this point in the history
  • Loading branch information
CeciliaLiu8 committed Jan 17, 2024
1 parent 850a929 commit 3a18c66
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/gov/cms/madie/services/PackagingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/gov/cms/madie/services/TranslationServiceClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,4 +42,26 @@ public List<TranslatedLibrary> 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<Measure> 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);
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}

0 comments on commit 3a18c66

Please sign in to comment.