Skip to content

Commit

Permalink
MAT-7855 write unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adongare committed Nov 7, 2024
1 parent a4cadf7 commit 8c22f59
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package gov.cms.madie.terminology.controller;

import ca.uhn.fhir.context.FhirContext;
import gov.cms.madie.terminology.exceptions.HapiOperationException;
import gov.cms.madie.terminology.service.InternalTerminologyService;
import org.hl7.fhir.r4.model.ValueSet;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import java.security.Principal;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
import static org.hl7.fhir.r4.model.ValueSet.ValueSetExpansionContainsComponent;
import static org.hl7.fhir.r4.model.ValueSet.ValueSetExpansionComponent;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(InternalTerminologyController.class)
class InternalTerminologyControllerMvcTest {

@MockBean private InternalTerminologyService internalTerminologyService;
@MockBean private FhirContext fhirContext;

@Autowired private MockMvc mockMvc;
private static final String TEST_USER = "test.user";

@Test
void testGetValueSetExpansion() throws Exception {
Principal principal = mock(Principal.class);
when(principal.getName()).thenReturn(TEST_USER);

var contains = new ValueSetExpansionContainsComponent();
contains.setCode("02").setDisplay("trivalent poliovirus vaccine, live, oral").setSystem("CVX");
var expansion = new ValueSetExpansionComponent();
expansion.addContains(contains);
ValueSet valueSet = new ValueSet();
valueSet.setId("us-core-vaccines-cvx-1");
valueSet.setExpansion(expansion);

when(internalTerminologyService.getValueSetExpansionById(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")
.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());
assertThat(
content,
containsString(
"{\"resourceType\":\"ValueSet\",\"id\":\"us-core-vaccines-cvx-1\",\"expansion\":{\"contains\":[{\"system\":\"CVX\",\"code\":\"02\",\"display\":\"trivalent poliovirus vaccine, live, oral\"}]}}"));
}

@Test
void testGetValueSetExpansionIfNotFound() throws Exception {
Principal principal = mock(Principal.class);
when(principal.getName()).thenReturn(TEST_USER);

doThrow(new HapiOperationException("Value set not found"))
.when(internalTerminologyService)
.getValueSetExpansionById(anyString());
MvcResult result =
mockMvc
.perform(
MockMvcRequestBuilders.get(
"/internal-terminology/ValueSet/us-core-vaccines-cvx-1/expand")
.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());
assertThat(content, containsString("Value set not found"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package gov.cms.madie.terminology.service;

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.rest.client.api.IGenericClient;
import ca.uhn.fhir.rest.gclient.*;
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
import ca.uhn.fhir.util.OperationOutcomeUtil;
import gov.cms.madie.terminology.exceptions.HapiOperationException;
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.ValueSet;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;

import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
public class InternalTerminologyServiceTest {

@Mock private IGenericClient hapiClient;

@InjectMocks private InternalTerminologyService service;

private IOperation operation;
private IOperationUnnamed operationUnnamed;
private IOperationUntyped operationUntyped;
private IOperationUntypedWithInput operationUntypedWithInput;

@BeforeEach
public void setUp() {
operation = mock(IOperation.class);
operationUnnamed = mock(IOperationUnnamed.class);
operationUntyped = mock(IOperationUntyped.class);
operationUntypedWithInput =
(IOperationUntypedWithInput<Parameters>) mock(IOperationUntypedWithInput.class);
}

@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);
when(operationUnnamed.named("$expand")).thenReturn(operationUntyped);
when(operationUntyped.withNoParameters(Parameters.class)).thenReturn(operationUntypedWithInput);

// mock response
Parameters parameters = new Parameters();
ValueSet valueSet = new ValueSet();
parameters.addParameter().setResource(valueSet);
when(operationUntypedWithInput.execute()).thenReturn(parameters);

ValueSet expansion = service.getValueSetExpansionById(valueSetId);
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);
when(operation.onInstance(idType)).thenReturn(operationUnnamed);
when(operationUnnamed.named("$expand")).thenReturn(operationUntyped);
when(operationUntyped.withNoParameters(Parameters.class)).thenReturn(operationUntypedWithInput);

// mock response
IBaseOperationOutcome operationOutcome = OperationOutcomeUtil.newInstance(fhirContext);
OperationOutcomeUtil.addIssue(
fhirContext, operationOutcome, "warning", "Resource not found", null, null);
ResourceNotFoundException resourceNotFoundException = new ResourceNotFoundException(idType);
resourceNotFoundException.setOperationOutcome(operationOutcome);
doThrow(resourceNotFoundException).when(operationUntypedWithInput).execute();

Exception ex =
Assertions.assertThrows(
HapiOperationException.class, () -> service.getValueSetExpansionById(valueSetId));
assertThat(ex.getMessage(), is(equalTo("Resource not found")));
}
}

0 comments on commit 8c22f59

Please sign in to comment.