Skip to content

Commit

Permalink
MAT-7960: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
chubert-sb committed Jan 15, 2025
1 parent f66a7b7 commit 5f6c9c7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import gov.cms.madie.terminology.exceptions.VsacResourceNotFoundException;
import gov.cms.madie.terminology.models.CodeSystem;
import gov.cms.madie.terminology.util.TerminologyServiceUtil;
import gov.cms.madie.models.measure.ManifestExpansion;
import gov.cms.madie.terminology.dto.ValueSetsSearchCriteria;
import io.netty.channel.ChannelOption;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -120,21 +119,6 @@ public String searchValueSets(String apiKey, Map<String, String> queryParams) {
return fetchResourceFromVsac(uri.toString(), apiKey, "bundle");
}

public String getValueSetResource(
String apiKey,
ValueSetsSearchCriteria.ValueSetParams valueSetParams,
String profile,
String includeDraft,
String activeOnly,
ManifestExpansion manifestExpansion) {
profile = StringUtils.isNotBlank(profile) ? defaultProfile : profile;
URI uri =
TerminologyServiceUtil.buildValueSetResourceUri(
valueSetParams, profile, includeDraft, activeOnly, manifestExpansion);

return fetchResourceFromVsac(uri.toString(), apiKey, "ValueSet");
}

public String getValueSetResources(String apiKey, ValueSetsSearchCriteria searchCriteria) {
String profile =
StringUtils.isNotBlank(searchCriteria.getProfile())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
import org.springframework.web.reactive.function.client.WebClientResponseException;

import java.io.IOException;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class FhirTerminologyServiceWebClientTest {
Expand All @@ -34,6 +39,8 @@ class FhirTerminologyServiceWebClientTest {

private ValueSetsSearchCriteria.ValueSetParams testValueSetParams;

private ValueSetsSearchCriteria searchCriteria;

FhirTerminologyServiceWebClient fhirTerminologyServiceWebClient;

@BeforeAll
Expand All @@ -44,6 +51,15 @@ static void setUp() throws IOException {

@BeforeEach
void initialize() {
searchCriteria =
ValueSetsSearchCriteria.builder()
.profile(null)
.includeDraft(null)
.activeOnly("false")
.manifestExpansion(new ManifestExpansion())
.valueSetParams(
List.of(ValueSetsSearchCriteria.ValueSetParams.builder().oid("test-vs-id").build()))
.build();
testValueSetParams = ValueSetsSearchCriteria.ValueSetParams.builder().oid("test-vs-id").build();
String baseUrl = String.format("http://localhost:%s", mockBackEnd.getPort());
fhirTerminologyServiceWebClient =
Expand Down Expand Up @@ -94,13 +110,15 @@ void getLatestValueSetResourceSuccessfully_when_noCustomSearchCriteriaIsProvided
.setResponseCode(200)
.setBody(MOCK_RESPONSE_STRING)
.addHeader("Content-Type", "application/fhir+json"));
when(fhirContext.newJsonParser()).thenReturn(FhirContext.forR4().newJsonParser());
String actualResponse =
fhirTerminologyServiceWebClient.getValueSetResource(
MOCK_API_KEY, testValueSetParams, null, null, "false", new ManifestExpansion());
fhirTerminologyServiceWebClient.getValueSetResources(MOCK_API_KEY, searchCriteria);
assertNotNull(actualResponse);
assertEquals(MOCK_RESPONSE_STRING, actualResponse);
RecordedRequest recordedRequest = mockBackEnd.takeRequest();
assertEquals("/ValueSet/test-vs-id/$expand?activeOnly=false", recordedRequest.getPath());
assertThat(
recordedRequest.getBody().readUtf8(),
containsString("/ValueSet/test-vs-id/$expand?activeOnly=false"));
}

@Test
Expand All @@ -111,15 +129,16 @@ void getDraftValueSetResourceSuccessfully_when_noCustomSearchCriteriaIsProvided(
.setResponseCode(200)
.setBody(MOCK_RESPONSE_STRING)
.addHeader("Content-Type", "application/fhir+json"));
when(fhirContext.newJsonParser()).thenReturn(FhirContext.forR4().newJsonParser());
searchCriteria.setIncludeDraft("true");
String actualResponse =
fhirTerminologyServiceWebClient.getValueSetResource(
MOCK_API_KEY, testValueSetParams, null, "yes", "false", new ManifestExpansion());
fhirTerminologyServiceWebClient.getValueSetResources(MOCK_API_KEY, searchCriteria);
assertNotNull(actualResponse);
assertEquals(MOCK_RESPONSE_STRING, actualResponse);
RecordedRequest recordedRequest = mockBackEnd.takeRequest();
assertEquals(
"/ValueSet/test-vs-id/$expand?includeDraft=true&activeOnly=false",
recordedRequest.getPath());
assertThat(
recordedRequest.getBody().readUtf8(),
containsString("ValueSet/test-vs-id/$expand?includeDraft=true&activeOnly=false"));
}

@Test
Expand All @@ -130,23 +149,21 @@ void getValueSetResourceSuccessfully_when_manifestExpansionIsProvided()
.setResponseCode(200)
.setBody(MOCK_RESPONSE_STRING)
.addHeader("Content-Type", "application/fhir+json"));
when(fhirContext.newJsonParser()).thenReturn(FhirContext.forR4().newJsonParser());
searchCriteria.setManifestExpansion(
ManifestExpansion.builder()
.id("test-manifest-456")
.fullUrl("https://cts.nlm.nih.gov/fhir/Library/test-manifest-456")
.build());
String actualResponse =
fhirTerminologyServiceWebClient.getValueSetResource(
MOCK_API_KEY,
testValueSetParams,
null,
null,
"true",
ManifestExpansion.builder()
.id("test-manifest-456")
.fullUrl("https://cts.nlm.nih.gov/fhir/Library/test-manifest-456")
.build());
fhirTerminologyServiceWebClient.getValueSetResources(MOCK_API_KEY, searchCriteria);
assertNotNull(actualResponse);
assertEquals(MOCK_RESPONSE_STRING, actualResponse);
RecordedRequest recordedRequest = mockBackEnd.takeRequest();
assertEquals(
"/ValueSet/test-vs-id/$expand?manifest=https://cts.nlm.nih.gov/fhir/Library/test-manifest-456",
recordedRequest.getPath());
assertThat(
recordedRequest.getBody().readUtf8(),
containsString(
"/ValueSet/test-vs-id/$expand?manifest=https://cts.nlm.nih.gov/fhir/Library/test-manifest-456"));
}

@Test
Expand All @@ -158,28 +175,30 @@ void getValueSetResourceSuccessfully_when_ValueSetVersionIsProvided()
.setBody(MOCK_RESPONSE_STRING)
.addHeader("Content-Type", "application/fhir+json"));
testValueSetParams.setVersion("test-value-set-version-2024");
searchCriteria.setValueSetParams(List.of(testValueSetParams));
when(fhirContext.newJsonParser()).thenReturn(FhirContext.forR4().newJsonParser());
String actualResponse =
fhirTerminologyServiceWebClient.getValueSetResource(
MOCK_API_KEY, testValueSetParams, null, null, "false", new ManifestExpansion());
fhirTerminologyServiceWebClient.getValueSetResources(MOCK_API_KEY, searchCriteria);
assertNotNull(actualResponse);
assertEquals(MOCK_RESPONSE_STRING, actualResponse);
RecordedRequest recordedRequest = mockBackEnd.takeRequest();
assertEquals(
"/ValueSet/test-vs-id/$expand?valueSetVersion=test-value-set-version-2024",
recordedRequest.getPath());
assertThat(
recordedRequest.getBody().readUtf8(),
containsString("ValueSet/test-vs-id/$expand?valueSetVersion=test-value-set-version-2024"));
}

@Test
void getValueSetResource_ReturnsException() throws InterruptedException {
testValueSetParams.setVersion("");
mockBackEnd.enqueue(new MockResponse().setResponseCode(HttpStatus.UNAUTHORIZED.value()));
when(fhirContext.newJsonParser()).thenReturn(FhirContext.forR4().newJsonParser());
assertThrows(
WebClientResponseException.class,
() ->
fhirTerminologyServiceWebClient.getValueSetResource(
MOCK_API_KEY, testValueSetParams, null, null, "false", new ManifestExpansion()));
() -> fhirTerminologyServiceWebClient.getValueSetResources(MOCK_API_KEY, searchCriteria));
RecordedRequest recordedRequest = mockBackEnd.takeRequest();
assertEquals("/ValueSet/test-vs-id/$expand?activeOnly=false", recordedRequest.getPath());
assertThat(
recordedRequest.getBody().readUtf8(),
containsString("/ValueSet/test-vs-id/$expand?activeOnly=false"));
}

@Test
Expand Down

0 comments on commit 5f6c9c7

Please sign in to comment.