|
| 1 | +package uk.nhs.adaptors.gp2gp.ehr.mapper; |
| 2 | + |
| 3 | +import org.hl7.fhir.dstu3.model.IdType; |
| 4 | +import org.hl7.fhir.dstu3.model.MedicationRequest; |
| 5 | +import org.hl7.fhir.dstu3.model.Reference; |
| 6 | +import org.junit.jupiter.api.AfterEach; |
| 7 | +import org.junit.jupiter.api.BeforeEach; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 10 | +import org.mockito.InjectMocks; |
| 11 | +import org.mockito.Mock; |
| 12 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 13 | +import org.mockito.junit.jupiter.MockitoSettings; |
| 14 | +import org.mockito.quality.Strictness; |
| 15 | +import java.util.Optional; |
| 16 | +import static org.junit.Assert.assertTrue; |
| 17 | +import static org.mockito.ArgumentMatchers.any; |
| 18 | +import static org.mockito.Mockito.when; |
| 19 | + |
| 20 | +@ExtendWith(MockitoExtension.class) |
| 21 | +@MockitoSettings(strictness = Strictness.LENIENT) |
| 22 | +class EhrExtractResourceMapperTest { |
| 23 | + |
| 24 | + @Mock |
| 25 | + private MessageContext messageContext; |
| 26 | + |
| 27 | + @Mock |
| 28 | + private InputBundle inputBundleHolder; |
| 29 | + |
| 30 | + @Mock |
| 31 | + private IdMapper idMapper; |
| 32 | + |
| 33 | + @InjectMocks |
| 34 | + private NonConsultationResourceMapper resourceMapper; |
| 35 | + |
| 36 | + @BeforeEach |
| 37 | + public void setUp() { |
| 38 | + when(messageContext.getInputBundleHolder()).thenReturn(inputBundleHolder); |
| 39 | + when(messageContext.getIdMapper()).thenReturn(idMapper); |
| 40 | + when(idMapper.hasIdBeenMapped(any(), any())).thenReturn(false); |
| 41 | + } |
| 42 | + |
| 43 | + @AfterEach |
| 44 | + public void tearDown() { |
| 45 | + messageContext.resetMessageContext(); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void When_ReferencedResourceIsEmpty_Expect_MapMedicationRequest() { |
| 50 | + |
| 51 | + MedicationRequest medRequest = new MedicationRequest(); |
| 52 | + medRequest.setId("MedicationRequest/1"); |
| 53 | + medRequest.addBasedOn(new Reference("ServiceRequest/123")); |
| 54 | + when(inputBundleHolder.getResource(new IdType("ServiceRequest/123"))).thenReturn(Optional.empty()); |
| 55 | + |
| 56 | + boolean result = resourceMapper.shouldMapResource(medRequest); |
| 57 | + |
| 58 | + assertTrue(result); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void When_ReferencedResourceHasNotBeenMapped_Expect_MapMedicationRequest() { |
| 63 | + |
| 64 | + MedicationRequest medRequest = new MedicationRequest(); |
| 65 | + medRequest.setId("MedicationRequest/1"); |
| 66 | + medRequest.addBasedOn(new Reference("ServiceRequest/123")); |
| 67 | + when(inputBundleHolder.getResource(new IdType("ServiceRequest/123"))).thenReturn( |
| 68 | + Optional.ofNullable(new MedicationRequest().setId("111"))); |
| 69 | + |
| 70 | + boolean result = resourceMapper.shouldMapResource(medRequest); |
| 71 | + |
| 72 | + assertTrue(result); |
| 73 | + } |
| 74 | + |
| 75 | +} |
0 commit comments