Skip to content

Commit

Permalink
MAT-7855 expand the valuesets by url instead of id
Browse files Browse the repository at this point in the history
  • Loading branch information
adongare committed Dec 11, 2024
1 parent 97cdb53 commit e4e0242
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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<String> valueSetExpansion(@PathVariable String id) {
ValueSet valueSet = internalTerminologyService.getValueSetExpansionById(id);
@GetMapping(path = "ValueSet/expand", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> valueSetExpansion(@RequestParam String url) {
ValueSet valueSet = internalTerminologyService.getValueSetExpansionByUrl(url);
return ResponseEntity.ok().body(fhirContext.newJsonParser().encodeResourceToString(valueSet));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -49,21 +50,21 @@ 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))
.andExpect(status().isOk())
.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(
Expand All @@ -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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,20 +36,21 @@ 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);
operationUnnamed = mock(IOperationUnnamed.class);
operationUntyped = mock(IOperationUntyped.class);
operationUntypedWithInput =
(IOperationUntypedWithInput<Parameters>) 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);
Expand All @@ -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);
Expand All @@ -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")));
}
}

0 comments on commit e4e0242

Please sign in to comment.