From e4e024230f41bff34cc033822476d0d16efe44f7 Mon Sep 17 00:00:00 2001 From: adongare Date: Wed, 11 Dec 2024 18:21:24 -0500 Subject: [PATCH] MAT-7855 expand the valuesets by url instead of id --- .../controller/InternalTerminologyController.java | 11 ++++------- .../service/InternalTerminologyService.java | 15 ++++++--------- .../InternalTerminologyControllerMvcTest.java | 13 +++++++------ .../service/InternalTerminologyServiceTest.java | 14 +++++++------- 4 files changed, 24 insertions(+), 29 deletions(-) diff --git a/src/main/java/gov/cms/madie/terminology/controller/InternalTerminologyController.java b/src/main/java/gov/cms/madie/terminology/controller/InternalTerminologyController.java index 2348bee..4163d30 100644 --- a/src/main/java/gov/cms/madie/terminology/controller/InternalTerminologyController.java +++ b/src/main/java/gov/cms/madie/terminology/controller/InternalTerminologyController.java @@ -6,10 +6,7 @@ import org.hl7.fhir.r4.model.ValueSet; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; @RestController @RequestMapping(path = "/internal-terminology") @@ -19,9 +16,9 @@ public class InternalTerminologyController { private final FhirContext fhirContext; private final InternalTerminologyService internalTerminologyService; - @GetMapping(path = "ValueSet/{id}/expand", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity valueSetExpansion(@PathVariable String id) { - ValueSet valueSet = internalTerminologyService.getValueSetExpansionById(id); + @GetMapping(path = "ValueSet/expand", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity valueSetExpansion(@RequestParam String url) { + ValueSet valueSet = internalTerminologyService.getValueSetExpansionByUrl(url); return ResponseEntity.ok().body(fhirContext.newJsonParser().encodeResourceToString(valueSet)); } } diff --git a/src/main/java/gov/cms/madie/terminology/service/InternalTerminologyService.java b/src/main/java/gov/cms/madie/terminology/service/InternalTerminologyService.java index 9b2d6bd..8dea6cb 100644 --- a/src/main/java/gov/cms/madie/terminology/service/InternalTerminologyService.java +++ b/src/main/java/gov/cms/madie/terminology/service/InternalTerminologyService.java @@ -5,10 +5,7 @@ import gov.cms.madie.terminology.exceptions.HapiOperationException; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.hl7.fhir.r4.model.IdType; -import org.hl7.fhir.r4.model.OperationOutcome; -import org.hl7.fhir.r4.model.Parameters; -import org.hl7.fhir.r4.model.ValueSet; +import org.hl7.fhir.r4.model.*; import org.springframework.stereotype.Service; import java.util.stream.Collectors; @@ -22,22 +19,22 @@ public class InternalTerminologyService { /** * This method fetches the ValueSet expansion by id from HAPI server * - * @param id -> Value Set id + * @param url -> Value Set url * @return ValueSet -> Value Set with expansion details */ - public ValueSet getValueSetExpansionById(String id) { + public ValueSet getValueSetExpansionByUrl(String url) { try { - log.info("Fetching ValueSet expansion for {}", id); + log.info("Fetching ValueSet expansion for {}", url); Parameters parameters = hapiClient .operation() - .onInstance(new IdType("ValueSet", id)) + .onInstance(new IdType(new UriType(url))) .named("$expand") .withNoParameters(Parameters.class) .execute(); return (ValueSet) parameters.getParameter().get(0).getResource(); } catch (BaseServerResponseException ex) { - log.error("An error occurred while fetching expansion for the ValueSet[{}]", id, ex); + log.error("An error occurred while fetching expansion for the ValueSet[{}]", url, ex); OperationOutcome outcome = (OperationOutcome) ex.getOperationOutcome(); if (outcome != null) { String errors = diff --git a/src/test/java/gov/cms/madie/terminology/controller/InternalTerminologyControllerMvcTest.java b/src/test/java/gov/cms/madie/terminology/controller/InternalTerminologyControllerMvcTest.java index 786dad5..7518b95 100644 --- a/src/test/java/gov/cms/madie/terminology/controller/InternalTerminologyControllerMvcTest.java +++ b/src/test/java/gov/cms/madie/terminology/controller/InternalTerminologyControllerMvcTest.java @@ -35,6 +35,7 @@ class InternalTerminologyControllerMvcTest { @Autowired private MockMvc mockMvc; private static final String TEST_USER = "test.user"; + private static final String VALUE_SET_URL = "ValueSet/us-core-vaccines-cvx-1"; @Test void testGetValueSetExpansion() throws Exception { @@ -49,13 +50,13 @@ void testGetValueSetExpansion() throws Exception { valueSet.setId("us-core-vaccines-cvx-1"); valueSet.setExpansion(expansion); - when(internalTerminologyService.getValueSetExpansionById(anyString())).thenReturn(valueSet); + when(internalTerminologyService.getValueSetExpansionByUrl(anyString())).thenReturn(valueSet); when(fhirContext.newJsonParser()).thenReturn(FhirContext.forR4().newJsonParser()); MvcResult result = mockMvc .perform( MockMvcRequestBuilders.get( - "/internal-terminology/ValueSet/us-core-vaccines-cvx-1/expand") + "/internal-terminology/ValueSet/expand?url=" + VALUE_SET_URL) .with(user(TEST_USER)) .with(csrf()) .contentType(MediaType.APPLICATION_JSON_VALUE)) @@ -63,7 +64,7 @@ void testGetValueSetExpansion() throws Exception { .andReturn(); assertThat(result.getResponse().getStatus(), is(equalTo(200))); String content = result.getResponse().getContentAsString(); - verify(internalTerminologyService, times(1)).getValueSetExpansionById(anyString()); + verify(internalTerminologyService, times(1)).getValueSetExpansionByUrl(anyString()); assertThat( content, containsString( @@ -77,19 +78,19 @@ void testGetValueSetExpansionIfNotFound() throws Exception { doThrow(new HapiOperationException("Value set not found")) .when(internalTerminologyService) - .getValueSetExpansionById(anyString()); + .getValueSetExpansionByUrl(anyString()); MvcResult result = mockMvc .perform( MockMvcRequestBuilders.get( - "/internal-terminology/ValueSet/us-core-vaccines-cvx-1/expand") + "/internal-terminology/ValueSet/expand?url=" + VALUE_SET_URL) .with(user(TEST_USER)) .with(csrf()) .contentType(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isBadRequest()) .andReturn(); String content = result.getResponse().getContentAsString(); - verify(internalTerminologyService, times(1)).getValueSetExpansionById(anyString()); + verify(internalTerminologyService, times(1)).getValueSetExpansionByUrl(anyString()); assertThat(content, containsString("Value set not found")); } } diff --git a/src/test/java/gov/cms/madie/terminology/service/InternalTerminologyServiceTest.java b/src/test/java/gov/cms/madie/terminology/service/InternalTerminologyServiceTest.java index 0437ac7..48e721d 100644 --- a/src/test/java/gov/cms/madie/terminology/service/InternalTerminologyServiceTest.java +++ b/src/test/java/gov/cms/madie/terminology/service/InternalTerminologyServiceTest.java @@ -9,6 +9,7 @@ import org.hl7.fhir.instance.model.api.IBaseOperationOutcome; import org.hl7.fhir.r4.model.IdType; import org.hl7.fhir.r4.model.Parameters; +import org.hl7.fhir.r4.model.UriType; import org.hl7.fhir.r4.model.ValueSet; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; @@ -35,6 +36,9 @@ public class InternalTerminologyServiceTest { private IOperationUntyped operationUntyped; private IOperationUntypedWithInput operationUntypedWithInput; + private static final String VALUE_SET_URL = "ValueSet/us-core-vaccines-cvx-1"; + private IdType idType; + @BeforeEach public void setUp() { operation = mock(IOperation.class); @@ -42,13 +46,11 @@ public void setUp() { operationUntyped = mock(IOperationUntyped.class); operationUntypedWithInput = (IOperationUntypedWithInput) mock(IOperationUntypedWithInput.class); + idType = new IdType(new UriType(VALUE_SET_URL)); } @Test void testGetValueSetExpansionById() { - String valueSetId = "us-core-vaccines-cvx-1"; - var idType = new IdType("ValueSet", valueSetId); - // mock hapi client operation when(hapiClient.operation()).thenReturn(operation); when(operation.onInstance(idType)).thenReturn(operationUnnamed); @@ -61,14 +63,12 @@ void testGetValueSetExpansionById() { parameters.addParameter().setResource(valueSet); when(operationUntypedWithInput.execute()).thenReturn(parameters); - ValueSet expansion = service.getValueSetExpansionById(valueSetId); + ValueSet expansion = service.getValueSetExpansionByUrl(VALUE_SET_URL); assertThat(expansion, is(not(nullValue()))); } @Test void testGetValueSetExpansionByIdIfValueSetNotFound() { - String valueSetId = "us-core-vaccines-cvx-1"; - var idType = new IdType("ValueSet", valueSetId); FhirContext fhirContext = FhirContext.forR4(); // mock hapi client operation when(hapiClient.operation()).thenReturn(operation); @@ -86,7 +86,7 @@ void testGetValueSetExpansionByIdIfValueSetNotFound() { Exception ex = Assertions.assertThrows( - HapiOperationException.class, () -> service.getValueSetExpansionById(valueSetId)); + HapiOperationException.class, () -> service.getValueSetExpansionByUrl(VALUE_SET_URL)); assertThat(ex.getMessage(), is(equalTo("Resource not found"))); } }