Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAT-4091 Modify the Error message returned from CQL-ELM Translator #12

Merged
merged 4 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>gov.cms.madie</groupId>
<artifactId>madie-translator-commons</artifactId>
<version>0.0.4-SNAPSHOT</version>
<version>0.0.5-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>17</java.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package gov.cms.madie.cql_elm_translator.exceptions;

public class LibraryResourceLoaderException extends RuntimeException {
public LibraryResourceLoaderException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gov.cms.madie.cql_elm_translator.service;

import gov.cms.madie.cql_elm_translator.exceptions.LibraryResourceLoaderException;
import gov.cms.madie.cql_elm_translator.utils.cql.cql_translator.MadieLibrarySourceProvider;
import gov.cms.mat.cql.CqlTextParser;
import gov.cms.mat.cql.elements.UsingProperties;
Expand All @@ -10,9 +11,9 @@
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
Expand Down Expand Up @@ -45,10 +46,9 @@ public String getLibraryCql(String name, String version, String accessToken) {
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", accessToken);

ResponseEntity<String> responseEntity =
restTemplate.exchange(uri, HttpMethod.GET, new HttpEntity<>(headers), String.class);

if (responseEntity.getStatusCode().is2xxSuccessful()) {
try {
ResponseEntity<String> responseEntity =
restTemplate.exchange(uri, HttpMethod.GET, new HttpEntity<>(headers), String.class);
if (responseEntity.hasBody()) {
log.debug("Retrieved a valid cqlPayload");
List<String> supportedLibraries =
Expand Down Expand Up @@ -79,15 +79,19 @@ public String getLibraryCql(String name, String version, String accessToken) {
log.error("Cannot find Cql payload in the response");
return null;
}
} else if (responseEntity.getStatusCode().equals(HttpStatus.NOT_FOUND)) {
log.error("Cannot find a Cql Library with name: {}, version: {}", name, version);
} else if (responseEntity.getStatusCode().equals(HttpStatus.CONFLICT)) {
log.error(
"Multiple libraries found with name: {}, version: {}, but only one was expected",
name,
version);
} catch (HttpClientErrorException.NotFound ex) {
String message =
String.format("Library resource %s version '%s' is not found.", name, version);
log.error(message);
throw new LibraryResourceLoaderException(message);
} catch (HttpClientErrorException.Conflict ex) {
String message =
String.format(
"Multiple libraries found with name: %s, version: %s, but only one was expected.",
name, version);
log.error(message);
throw new LibraryResourceLoaderException(message);
}
return null;
}

private URI buildMadieLibraryServiceUri(String name, String version) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gov.cms.madie.cql_elm_translator.services;

import gov.cms.madie.cql_elm_translator.exceptions.LibraryResourceLoaderException;
import gov.cms.mat.cql.elements.UsingProperties;
import gov.cms.madie.cql_elm_translator.service.CqlLibraryService;
import gov.cms.madie.cql_elm_translator.utils.cql.cql_translator.MadieLibrarySourceProvider;
Expand All @@ -16,17 +17,18 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.net.URISyntaxException;

import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.when;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class CqlLibraryServiceTest {
Expand All @@ -39,7 +41,7 @@ class CqlLibraryServiceTest {

private URI libraryUri;

private final String cqlLibraryName = "cqlLibraryName";
private final String cqlLibraryName = "FHIRHelpers";

private final String cqlLibraryVersion = "1.0.000";

Expand Down Expand Up @@ -106,23 +108,35 @@ void getLibraryCqlReturnsNull() {
}

@Test
void getLibraryCqlReturnsNullWhenNotFound() {
when(restTemplate.exchange(
libraryUri, HttpMethod.GET, new HttpEntity<>(httpHeaders), String.class))
.thenReturn(new ResponseEntity<>(null, HttpStatus.NOT_FOUND));
String responseBody =
cqlLibraryService.getLibraryCql(cqlLibraryName, cqlLibraryVersion, accessToken);
assertNull(responseBody);
void getLibraryCqlWhenLibraryNotFound() {
HttpClientErrorException response = mock(HttpClientErrorException.NotFound.class);
doThrow(response)
.when(restTemplate)
.exchange(libraryUri, HttpMethod.GET, new HttpEntity<>(httpHeaders), String.class);
Exception ex =
assertThrows(
LibraryResourceLoaderException.class,
() -> cqlLibraryService.getLibraryCql(cqlLibraryName, cqlLibraryVersion, accessToken));
assertThat(
ex.getMessage(),
is(equalTo("Library resource FHIRHelpers version '1.0.000' is not found.")));
}

@Test
void getLibraryCqlReturnsNullWhenConflict() {
when(restTemplate.exchange(
libraryUri, HttpMethod.GET, new HttpEntity<>(httpHeaders), String.class))
.thenReturn(new ResponseEntity<>(null, HttpStatus.CONFLICT));
String responseBody =
cqlLibraryService.getLibraryCql(cqlLibraryName, cqlLibraryVersion, accessToken);
assertNull(responseBody);
HttpClientErrorException response = mock(HttpClientErrorException.Conflict.class);
doThrow(response)
.when(restTemplate)
.exchange(libraryUri, HttpMethod.GET, new HttpEntity<>(httpHeaders), String.class);
Exception ex =
assertThrows(
LibraryResourceLoaderException.class,
() -> cqlLibraryService.getLibraryCql(cqlLibraryName, cqlLibraryVersion, accessToken));
assertThat(
ex.getMessage(),
is(
equalTo(
"Multiple libraries found with name: FHIRHelpers, version: 1.0.000, but only one was expected.")));
}

@Test
Expand Down
Loading