diff --git a/api/src/main/java/org/openmrs/module/fhir2/providers/r3/AllergyIntoleranceFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/AllergyIntoleranceFhirResourceProvider.java index 61857edf8..ff304656e 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/providers/r3/AllergyIntoleranceFhirResourceProvider.java +++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/AllergyIntoleranceFhirResourceProvider.java @@ -14,18 +14,24 @@ import java.util.List; import java.util.stream.Collectors; +import ca.uhn.fhir.rest.annotation.Create; +import ca.uhn.fhir.rest.annotation.Delete; import ca.uhn.fhir.rest.annotation.History; import ca.uhn.fhir.rest.annotation.IdParam; import ca.uhn.fhir.rest.annotation.OptionalParam; import ca.uhn.fhir.rest.annotation.Read; +import ca.uhn.fhir.rest.annotation.ResourceParam; import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.annotation.Sort; +import ca.uhn.fhir.rest.annotation.Update; +import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.api.SortSpec; import ca.uhn.fhir.rest.api.server.IBundleProvider; import ca.uhn.fhir.rest.param.DateRangeParam; import ca.uhn.fhir.rest.param.ReferenceAndListParam; import ca.uhn.fhir.rest.param.TokenAndListParam; import ca.uhn.fhir.rest.server.IResourceProvider; +import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException; import lombok.AccessLevel; import lombok.Setter; @@ -33,12 +39,14 @@ import org.hl7.fhir.convertors.conv30_40.Provenance30_40; import org.hl7.fhir.dstu3.model.AllergyIntolerance; import org.hl7.fhir.dstu3.model.IdType; +import org.hl7.fhir.dstu3.model.OperationOutcome; import org.hl7.fhir.dstu3.model.Patient; import org.hl7.fhir.dstu3.model.Resource; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.r4.model.Observation; import org.hl7.fhir.r4.model.Provenance; import org.openmrs.module.fhir2.api.FhirAllergyIntoleranceService; +import org.openmrs.module.fhir2.providers.util.FhirProviderUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; @@ -79,6 +87,36 @@ public List getAllergyIntoleranceHistoryById(@IdParam @NotNull IdType .map(Provenance30_40::convertProvenance).collect(Collectors.toList()); } + @Create + @SuppressWarnings("unused") + public MethodOutcome creatAllergyIntolerance(@ResourceParam AllergyIntolerance allergyIntolerance) { + return FhirProviderUtils.buildCreate( + allergyIntoleranceService.create(AllergyIntolerance30_40.convertAllergyIntolerance(allergyIntolerance))); + } + + @Update + @SuppressWarnings("unused") + public MethodOutcome updateAllergyIntolerance(@IdParam IdType id, @ResourceParam AllergyIntolerance allergyIntolerance) { + if (id == null || id.getIdPart() == null) { + throw new InvalidRequestException("id must be specified to update"); + } + + allergyIntolerance.setId(id.getIdPart()); + + return FhirProviderUtils.buildUpdate(allergyIntoleranceService.update(id.getIdPart(), + AllergyIntolerance30_40.convertAllergyIntolerance(allergyIntolerance))); + } + + @Delete + @SuppressWarnings("unused") + public OperationOutcome deleteAllergyIntolerance(@IdParam @NotNull IdType id) { + org.hl7.fhir.r4.model.AllergyIntolerance allergyIntolerance = allergyIntoleranceService.delete(id.getIdPart()); + if (allergyIntolerance == null) { + throw new ResourceNotFoundException("Could not find allergyIntolerance to delete with id " + id.getIdPart()); + } + return FhirProviderUtils.buildDelete(AllergyIntolerance30_40.convertAllergyIntolerance(allergyIntolerance)); + } + @Search @SuppressWarnings("unused") public IBundleProvider searchForAllergies( diff --git a/api/src/main/java/org/openmrs/module/fhir2/providers/r4/AllergyIntoleranceFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/AllergyIntoleranceFhirResourceProvider.java index 400816550..77bdbdf1e 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/providers/r4/AllergyIntoleranceFhirResourceProvider.java +++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/AllergyIntoleranceFhirResourceProvider.java @@ -14,6 +14,7 @@ import java.util.List; import ca.uhn.fhir.rest.annotation.Create; +import ca.uhn.fhir.rest.annotation.Delete; import ca.uhn.fhir.rest.annotation.History; import ca.uhn.fhir.rest.annotation.IdParam; import ca.uhn.fhir.rest.annotation.OptionalParam; @@ -21,6 +22,7 @@ import ca.uhn.fhir.rest.annotation.ResourceParam; import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.annotation.Sort; +import ca.uhn.fhir.rest.annotation.Update; import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.api.SortSpec; import ca.uhn.fhir.rest.api.server.IBundleProvider; @@ -28,6 +30,7 @@ import ca.uhn.fhir.rest.param.ReferenceAndListParam; import ca.uhn.fhir.rest.param.TokenAndListParam; import ca.uhn.fhir.rest.server.IResourceProvider; +import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException; import lombok.AccessLevel; import lombok.Setter; @@ -35,6 +38,7 @@ import org.hl7.fhir.r4.model.AllergyIntolerance; import org.hl7.fhir.r4.model.IdType; import org.hl7.fhir.r4.model.Observation; +import org.hl7.fhir.r4.model.OperationOutcome; import org.hl7.fhir.r4.model.Patient; import org.hl7.fhir.r4.model.Resource; import org.openmrs.module.fhir2.api.FhirAllergyIntoleranceService; @@ -106,4 +110,26 @@ public MethodOutcome createAllergy(@ResourceParam AllergyIntolerance allergy) { return FhirProviderUtils.buildCreate(fhirAllergyIntoleranceService.create(allergy)); } + @Update + @SuppressWarnings("unused") + public MethodOutcome updateAllergy(@IdParam IdType id, @ResourceParam AllergyIntolerance allergy) { + if (id == null || id.getIdPart() == null) { + throw new InvalidRequestException("id must be specified to update"); + } + + allergy.setId(id.getIdPart()); + + return FhirProviderUtils.buildUpdate(fhirAllergyIntoleranceService.update(id.getIdPart(), allergy)); + } + + @Delete + @SuppressWarnings("unused") + public OperationOutcome deleteAllergy(@IdParam @NotNull IdType id) { + AllergyIntolerance allergyIntolerance = fhirAllergyIntoleranceService.delete(id.getIdPart()); + if (allergyIntolerance == null) { + throw new ResourceNotFoundException("Could not find allergy to delete with id " + id.getIdPart()); + } + + return FhirProviderUtils.buildDelete(allergyIntolerance); + } } diff --git a/api/src/test/java/org/openmrs/module/fhir2/providers/r3/AllergyIntoleranceFhirR3ResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/AllergyIntoleranceFhirR3ResourceProviderTest.java index 55f4d69bf..388c380c2 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/providers/r3/AllergyIntoleranceFhirR3ResourceProviderTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/AllergyIntoleranceFhirR3ResourceProviderTest.java @@ -17,6 +17,8 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.notNullValue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.Mockito.when; import static org.mockito.hamcrest.MockitoHamcrest.argThat; @@ -24,6 +26,7 @@ import java.util.Collections; import java.util.List; +import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.api.server.IBundleProvider; import ca.uhn.fhir.rest.param.DateRangeParam; import ca.uhn.fhir.rest.param.ReferenceAndListParam; @@ -32,9 +35,14 @@ import ca.uhn.fhir.rest.param.TokenAndListParam; import ca.uhn.fhir.rest.param.TokenOrListParam; import ca.uhn.fhir.rest.param.TokenParam; +import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; +import ca.uhn.fhir.rest.server.exceptions.MethodNotAllowedException; import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException; +import org.hamcrest.CoreMatchers; +import org.hl7.fhir.convertors.conv30_40.AllergyIntolerance30_40; import org.hl7.fhir.dstu3.model.AllergyIntolerance; import org.hl7.fhir.dstu3.model.IdType; +import org.hl7.fhir.dstu3.model.OperationOutcome; import org.hl7.fhir.dstu3.model.Patient; import org.hl7.fhir.dstu3.model.Provenance; import org.hl7.fhir.dstu3.model.Resource; @@ -385,4 +393,60 @@ public void searchForAllergies_shouldReturnMatchingBundleOfAllergiesByLastUpdate assertThat(((org.hl7.fhir.r4.model.AllergyIntolerance) resultList.iterator().next()).getId(), equalTo(ALLERGY_UUID)); } + @Test + public void createAllergyIntolerance_shouldCreateNewAllergyIntolerance() { + when(service.create(any(org.hl7.fhir.r4.model.AllergyIntolerance.class))).thenReturn(allergyIntolerance); + + MethodOutcome result = resourceProvider + .creatAllergyIntolerance(AllergyIntolerance30_40.convertAllergyIntolerance(allergyIntolerance)); + assertThat(result, CoreMatchers.notNullValue()); + assertThat(result.getCreated(), is(true)); + assertThat(result.getResource(), CoreMatchers.equalTo(allergyIntolerance)); + } + + @Test + public void updateAllergyIntolerance_shouldUpdateAllergyIntolerance() { + when(service.update(eq(ALLERGY_UUID), any(org.hl7.fhir.r4.model.AllergyIntolerance.class))) + .thenReturn(allergyIntolerance); + + MethodOutcome result = resourceProvider.updateAllergyIntolerance(new IdType().setValue(ALLERGY_UUID), + AllergyIntolerance30_40.convertAllergyIntolerance(allergyIntolerance)); + assertThat(result, CoreMatchers.notNullValue()); + assertThat(result.getResource(), CoreMatchers.equalTo(allergyIntolerance)); + } + + @Test(expected = InvalidRequestException.class) + public void updateAllergyIntolerance_shouldThrowInvalidRequestForUuidMismatch() { + when(service.update(eq(WRONG_ALLERGY_UUID), any(org.hl7.fhir.r4.model.AllergyIntolerance.class))) + .thenThrow(InvalidRequestException.class); + + resourceProvider.updateAllergyIntolerance(new IdType().setValue(WRONG_ALLERGY_UUID), + AllergyIntolerance30_40.convertAllergyIntolerance(allergyIntolerance)); + } + + @Test(expected = MethodNotAllowedException.class) + public void updateAllergyIntolerance_shouldThrowMethodNotAllowedIfDoesNotExist() { + when(service.update(eq(WRONG_ALLERGY_UUID), any(org.hl7.fhir.r4.model.AllergyIntolerance.class))) + .thenThrow(MethodNotAllowedException.class); + + resourceProvider.updateAllergyIntolerance(new IdType().setValue(WRONG_ALLERGY_UUID), + AllergyIntolerance30_40.convertAllergyIntolerance(allergyIntolerance)); + } + + @Test + public void deleteAllergyIntolerance_shouldDeleteRequestedAllergyIntolerance() { + when(service.delete(ALLERGY_UUID)).thenReturn(allergyIntolerance); + + OperationOutcome result = resourceProvider.deleteAllergyIntolerance(new IdType().setValue(ALLERGY_UUID)); + assertThat(result, CoreMatchers.notNullValue()); + assertThat(result.getIssue(), notNullValue()); + assertThat(result.getIssueFirstRep().getSeverity(), equalTo(OperationOutcome.IssueSeverity.INFORMATION)); + assertThat(result.getIssueFirstRep().getDetails().getCodingFirstRep().getCode(), equalTo("MSG_DELETED")); + } + + @Test(expected = ResourceNotFoundException.class) + public void deleteAllergyIntolerance_shouldThrowResourceNotFoundExceptionWhenIdRefersToNonExistantAllergyIntolerance() { + when(service.delete(WRONG_ALLERGY_UUID)).thenReturn(null); + resourceProvider.deleteAllergyIntolerance(new IdType().setValue(WRONG_ALLERGY_UUID)); + } } diff --git a/api/src/test/java/org/openmrs/module/fhir2/providers/r3/TaskFhirResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/TaskFhirResourceProviderTest.java index 60564de82..a6463af3c 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/providers/r3/TaskFhirResourceProviderTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r3/TaskFhirResourceProviderTest.java @@ -18,18 +18,23 @@ import static org.hamcrest.Matchers.greaterThanOrEqualTo; import static org.hamcrest.Matchers.not; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.when; import java.util.ArrayList; import java.util.List; +import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.api.server.IBundleProvider; import ca.uhn.fhir.rest.param.TokenAndListParam; import ca.uhn.fhir.rest.param.TokenOrListParam; import ca.uhn.fhir.rest.param.TokenParam; +import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; +import ca.uhn.fhir.rest.server.exceptions.MethodNotAllowedException; import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException; import lombok.AccessLevel; import lombok.Getter; +import org.hamcrest.CoreMatchers; import org.hamcrest.Matchers; import org.hl7.fhir.dstu3.model.IdType; import org.hl7.fhir.dstu3.model.OperationOutcome; @@ -45,6 +50,7 @@ import org.openmrs.module.fhir2.FhirConstants; import org.openmrs.module.fhir2.api.FhirTaskService; import org.openmrs.module.fhir2.providers.r4.MockIBundleProvider; +import org.openmrs.module.fhir2.providers.util.TaskVersionConverter; @RunWith(MockitoJUnitRunner.class) public class TaskFhirResourceProviderTest extends BaseFhirR3ProvenanceResourceTest { @@ -144,50 +150,43 @@ public void getTaskHistoryByWithWrongId_shouldThrowResourceNotFoundException() { assertThat(resourceProvider.getTaskHistoryById(idType).size(), Matchers.equalTo(0)); } - // @Test - // public void createTask_shouldCreateNewTask() { - // when(taskService.saveTask(task)).thenReturn(task); - // - // MethodOutcome result = resourceProvider.createTask(TaskVersionConverter.convertTask(task)); - // assertThat(result.getResource(), equalTo(task)); - // } - // - // @Test - // public void updateTask_shouldUpdateTask() { - // when(taskService.updateTask(TASK_UUID, task)).thenReturn(task); - // - // IdType uuid = new IdType(); - // uuid.setValue(TASK_UUID); - // - // MethodOutcome result = resourceProvider.updateTask(uuid, TaskVersionConverter.convertTask(task)); - // assertThat(result.getResource(), equalTo(task)); - // } - // - // @Test(expected = InvalidRequestException.class) - // public void updateTask_shouldThrowInvalidRequestForTaskUuidMismatch() { - // when(taskService.updateTask(WRONG_TASK_UUID, task)).thenThrow(InvalidRequestException.class); - // - // resourceProvider.updateTask(new IdType().setValue(WRONG_TASK_UUID), TaskVersionConverter.convertTask(task)); - // } - // - // @Test(expected = InvalidRequestException.class) - // public void updateTask_shouldThrowInvalidRequestIfTaskHasNoUuid() { - // Task noIdTask = new Task(); - // - // when(taskService.updateTask(TASK_UUID, TaskVersionConverter.convertTask(noIdTask))).thenThrow(InvalidRequestException.class); - // - // resourceProvider.updateTask(new IdType().setValue(TASK_UUID), noIdTask); - // } - // - // @Test(expected = MethodNotAllowedException.class) - // public void updateTask_shouldThrowMethodNotAllowedIfTaskDoesNotExist() { - // Task wrongTask = new Task(); - // wrongTask.setId(WRONG_TASK_UUID); - // - // when(taskService.updateTask(WRONG_TASK_UUID, TaskVersionConverter.convertTask(wrongTask))).thenThrow(MethodNotAllowedException.class); - // - // resourceProvider.updateTask(new IdType().setValue(WRONG_TASK_UUID), wrongTask); - // } + @Test + public void createTask_shouldCreateNewTask() { + when(taskService.create(any(org.hl7.fhir.r4.model.Task.class))).thenReturn(task); + + MethodOutcome result = resourceProvider.createTask(TaskVersionConverter.convertTask(task)); + + assertThat(result, CoreMatchers.notNullValue()); + assertThat(result.getCreated(), is(true)); + assertThat(result.getResource(), CoreMatchers.equalTo(task)); + } + + @Test + public void updateTask_shouldUpdateTask() { + when(taskService.update(eq(TASK_UUID), any(org.hl7.fhir.r4.model.Task.class))).thenReturn(task); + + MethodOutcome result = resourceProvider.updateTask(new IdType().setValue(TASK_UUID), + TaskVersionConverter.convertTask(task)); + + assertThat(result, CoreMatchers.notNullValue()); + assertThat(result.getResource(), CoreMatchers.equalTo(task)); + } + + @Test(expected = InvalidRequestException.class) + public void updateTask_shouldThrowInvalidRequestForUuidMismatch() { + when(taskService.update(eq(WRONG_TASK_UUID), any(org.hl7.fhir.r4.model.Task.class))) + .thenThrow(InvalidRequestException.class); + + resourceProvider.updateTask(new IdType().setValue(WRONG_TASK_UUID), TaskVersionConverter.convertTask(task)); + } + + @Test(expected = MethodNotAllowedException.class) + public void updateTask_shouldThrowMethodNotAllowedIfDoesNotExist() { + when(taskService.update(eq(WRONG_TASK_UUID), any(org.hl7.fhir.r4.model.Task.class))) + .thenThrow(MethodNotAllowedException.class); + + resourceProvider.updateTask(new IdType().setValue(WRONG_TASK_UUID), TaskVersionConverter.convertTask(task)); + } @Test public void searchTasks_shouldReturnMatchingTasks() { diff --git a/api/src/test/java/org/openmrs/module/fhir2/providers/r4/AllergyIntoleranceFhirResourceProviderTest.java b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/AllergyIntoleranceFhirResourceProviderTest.java index adc1eb096..bf378a132 100644 --- a/api/src/test/java/org/openmrs/module/fhir2/providers/r4/AllergyIntoleranceFhirResourceProviderTest.java +++ b/api/src/test/java/org/openmrs/module/fhir2/providers/r4/AllergyIntoleranceFhirResourceProviderTest.java @@ -33,11 +33,14 @@ import ca.uhn.fhir.rest.param.TokenAndListParam; import ca.uhn.fhir.rest.param.TokenOrListParam; import ca.uhn.fhir.rest.param.TokenParam; +import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; +import ca.uhn.fhir.rest.server.exceptions.MethodNotAllowedException; import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException; import org.hamcrest.CoreMatchers; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.r4.model.AllergyIntolerance; import org.hl7.fhir.r4.model.IdType; +import org.hl7.fhir.r4.model.OperationOutcome; import org.hl7.fhir.r4.model.Patient; import org.hl7.fhir.r4.model.Provenance; import org.hl7.fhir.r4.model.Resource; @@ -388,7 +391,7 @@ public void searchForAllergies_shouldReturnMatchingBundleOfAllergiesByLastUpdate } @Test - public void createAllergy_shouldCreateAllergyIntolerance() { + public void createAllergyIntolerance_shouldCreateAllergyIntolerance() { when(service.create(allergyIntolerance)).thenReturn(allergyIntolerance); MethodOutcome result = resourceProvider.createAllergy(allergyIntolerance); @@ -397,4 +400,43 @@ public void createAllergy_shouldCreateAllergyIntolerance() { assertThat(result.getResource(), CoreMatchers.equalTo(allergyIntolerance)); } + @Test + public void updateAllergyIntolerance_shouldUpdateRequestedAllergyIntolerance() { + when(service.update(ALLERGY_UUID, allergyIntolerance)).thenReturn(allergyIntolerance); + + MethodOutcome result = resourceProvider.updateAllergy(new IdType().setValue(ALLERGY_UUID), allergyIntolerance); + assertThat(result, CoreMatchers.notNullValue()); + assertThat(result.getResource(), CoreMatchers.equalTo(allergyIntolerance)); + } + + @Test(expected = InvalidRequestException.class) + public void updateAllergyIntolerance_shouldThrowInvalidRequestForUuidMismatchException() { + when(service.update(WRONG_ALLERGY_UUID, allergyIntolerance)).thenThrow(InvalidRequestException.class); + + resourceProvider.updateAllergy(new IdType().setValue(WRONG_ALLERGY_UUID), allergyIntolerance); + } + + @Test(expected = MethodNotAllowedException.class) + public void updateAllergyIntolerance_shouldthrowMethodNotAllowedIfDoesNotExist() { + when(service.update(WRONG_ALLERGY_UUID, allergyIntolerance)).thenThrow(MethodNotAllowedException.class); + + resourceProvider.updateAllergy(new IdType().setValue(WRONG_ALLERGY_UUID), allergyIntolerance); + } + + @Test + public void deleteAllergyIntolerance_shouldDeleteRequestedAllergyIntolerance() { + when(service.delete(ALLERGY_UUID)).thenReturn(allergyIntolerance); + + OperationOutcome result = resourceProvider.deleteAllergy(new IdType().setValue(ALLERGY_UUID)); + assertThat(result, CoreMatchers.notNullValue()); + assertThat(result.getIssue(), notNullValue()); + assertThat(result.getIssueFirstRep().getSeverity(), equalTo(OperationOutcome.IssueSeverity.INFORMATION)); + assertThat(result.getIssueFirstRep().getDetails().getCodingFirstRep().getCode(), equalTo("MSG_DELETED")); + } + + @Test(expected = ResourceNotFoundException.class) + public void deleteAllergyIntolerance_shouldThrowResourceNotFoundExceptionWhenIdRefersToNonExistantAllergyIntolerance() { + when(service.delete(WRONG_ALLERGY_UUID)).thenReturn(null); + resourceProvider.deleteAllergy(new IdType().setValue(WRONG_ALLERGY_UUID)); + } } diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/AllergyIntoleranceFhirR3ResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/AllergyIntoleranceFhirR3ResourceProviderWebTest.java index c185c3a86..5cc950bd3 100644 --- a/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/AllergyIntoleranceFhirR3ResourceProviderWebTest.java +++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r3/AllergyIntoleranceFhirR3ResourceProviderWebTest.java @@ -10,12 +10,14 @@ package org.openmrs.module.fhir2.providers.r3; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsStringIgnoringCase; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.notNullValue; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -25,11 +27,14 @@ import javax.servlet.ServletException; import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; import java.util.Date; import java.util.List; +import java.util.Objects; import ca.uhn.fhir.rest.param.DateRangeParam; import ca.uhn.fhir.rest.param.ReferenceAndListParam; @@ -38,8 +43,10 @@ import ca.uhn.fhir.rest.param.TokenAndListParam; import ca.uhn.fhir.rest.param.TokenOrListParam; import ca.uhn.fhir.rest.param.TokenParam; +import ca.uhn.fhir.rest.server.exceptions.MethodNotAllowedException; import lombok.AccessLevel; import lombok.Getter; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang.time.DateUtils; import org.hl7.fhir.dstu3.model.AllergyIntolerance; import org.hl7.fhir.dstu3.model.Bundle; @@ -67,6 +74,14 @@ public class AllergyIntoleranceFhirR3ResourceProviderWebTest extends BaseFhirR3R private static final String WRONG_ALLERGY_UUID = "2085AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; + private static final String JSON_CREATE_ALLERGY_PATH = "org/openmrs/module/fhir2/providers/AllergyIntoleranceWebTest_create.json"; + + private static final String JSON_UPDATE_ALLERGY_PATH = "org/openmrs/module/fhir2/providers/AllergyIntoleranceWebTest_update.json"; + + private static final String JSON_UPDATE_WITHOUTID_ALLERGY_PATH = "org/openmrs/module/fhir2/providers/AllergyIntoleranceWebTest_UpdateWithoutId.json"; + + private static final String JSON_UPDATE_WITH_WRONGID_ALLERGY_PATH = "org/openmrs/module/fhir2/providers/AllergyIntoleranceWebTest_UpdateWithWrongId.json"; + private static final String LAST_UPDATED_DATE = "2020-09-03"; @Mock @@ -440,4 +455,112 @@ private void verifyUri(String uri) throws Exception { assertThat(results.getEntry().get(0).getResource().getIdElement().getIdPart(), equalTo(ALLERGY_UUID)); } + @Test + public void createAllergyIntolerance_shouldCreateNewAllergyIntolerance() throws Exception { + org.hl7.fhir.r4.model.AllergyIntolerance allergyIntolerance = new org.hl7.fhir.r4.model.AllergyIntolerance(); + allergyIntolerance.setId(ALLERGY_UUID); + String allergyJson; + try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(JSON_CREATE_ALLERGY_PATH)) { + Objects.requireNonNull(is); + allergyJson = IOUtils.toString(is, StandardCharsets.UTF_8); + } + + when(allergyService.create(any(org.hl7.fhir.r4.model.AllergyIntolerance.class))).thenReturn(allergyIntolerance); + + MockHttpServletResponse response = post("/AllergyIntolerance").jsonContent(allergyJson).accept(FhirMediaTypes.JSON) + .go(); + + assertThat(response, isCreated()); + assertThat(response.getStatus(), is(201)); + } + + @Test + public void updateAllergyIntolerance_shouldUpdateRequestedAllergyIntolerance() throws Exception { + org.hl7.fhir.r4.model.AllergyIntolerance allergyIntolerance = new org.hl7.fhir.r4.model.AllergyIntolerance(); + allergyIntolerance.setId(ALLERGY_UUID); + String allergyJson; + try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(JSON_UPDATE_ALLERGY_PATH)) { + Objects.requireNonNull(is); + allergyJson = IOUtils.toString(is, StandardCharsets.UTF_8); + } + + when(allergyService.update(any(String.class), any(org.hl7.fhir.r4.model.AllergyIntolerance.class))) + .thenReturn(allergyIntolerance); + + MockHttpServletResponse response = put("/AllergyIntolerance/" + ALLERGY_UUID).jsonContent(allergyJson) + .accept(FhirMediaTypes.JSON).go(); + + assertThat(response, isOk()); + } + + @Test + public void updateAllergyIntolerance_shouldThrowErrorForIdMismatch() throws Exception { + String allergyJson; + try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(JSON_UPDATE_ALLERGY_PATH)) { + Objects.requireNonNull(is); + allergyJson = IOUtils.toString(is, StandardCharsets.UTF_8); + } + + MockHttpServletResponse response = put("/AllergyIntolerance/" + WRONG_ALLERGY_UUID).jsonContent(allergyJson) + .accept(FhirMediaTypes.JSON).go(); + + assertThat(response, isBadRequest()); + assertThat(response.getContentAsString(), + containsStringIgnoringCase("body must contain an ID element which matches the request URL")); + } + + @Test + public void updateAllergyIntolerance_shouldErrorForNoId() throws Exception { + String allergyJson; + try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(JSON_UPDATE_WITHOUTID_ALLERGY_PATH)) { + Objects.requireNonNull(is); + allergyJson = IOUtils.toString(is, StandardCharsets.UTF_8); + } + + MockHttpServletResponse response = put("/AllergyIntolerance/" + ALLERGY_UUID).jsonContent(allergyJson) + .accept(FhirMediaTypes.JSON).go(); + + assertThat(response, isBadRequest()); + assertThat(response.getContentAsString(), containsStringIgnoringCase("body must contain an ID element for update")); + } + + @Test + public void updateAllergyIntolerance_shouldErrorForNonexistentMedication() throws Exception { + String medicationJson; + try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(JSON_UPDATE_WITH_WRONGID_ALLERGY_PATH)) { + Objects.requireNonNull(is); + medicationJson = IOUtils.toString(is, StandardCharsets.UTF_8); + } + + when(allergyService.update(eq(WRONG_ALLERGY_UUID), any(org.hl7.fhir.r4.model.AllergyIntolerance.class))) + .thenThrow(new MethodNotAllowedException("AllergyIntolerance " + WRONG_ALLERGY_UUID + " does not exist")); + + MockHttpServletResponse response = put("/AllergyIntolerance/" + WRONG_ALLERGY_UUID).jsonContent(medicationJson) + .accept(FhirMediaTypes.JSON).go(); + + assertThat(response, isMethodNotAllowed()); + } + + @Test + public void deleteAllergyIntolerance_shouldDeleteAllergyIntolerance() throws Exception { + org.hl7.fhir.r4.model.AllergyIntolerance allergyIntolerance = new org.hl7.fhir.r4.model.AllergyIntolerance(); + allergyIntolerance.setId(ALLERGY_UUID); + + when(allergyService.delete(any(String.class))).thenReturn(allergyIntolerance); + + MockHttpServletResponse response = delete("/AllergyIntolerance/" + ALLERGY_UUID).accept(FhirMediaTypes.JSON).go(); + + assertThat(response, isOk()); + assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString())); + } + + @Test + public void deleteAllergyIntolerance_shouldReturn404ForNonExistingAllergyIntolerance() throws Exception { + when(allergyService.delete(WRONG_ALLERGY_UUID)).thenReturn(null); + + MockHttpServletResponse response = delete("/AllergyIntolerance/" + WRONG_ALLERGY_UUID).accept(FhirMediaTypes.JSON) + .go(); + + assertThat(response, isNotFound()); + } } diff --git a/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/AllergyIntoleranceFhirResourceProviderWebTest.java b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/AllergyIntoleranceFhirResourceProviderWebTest.java index c418c948a..f724bbd75 100644 --- a/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/AllergyIntoleranceFhirResourceProviderWebTest.java +++ b/omod/src/test/java/org/openmrs/module/fhir2/providers/r4/AllergyIntoleranceFhirResourceProviderWebTest.java @@ -10,12 +10,14 @@ package org.openmrs.module.fhir2.providers.r4; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsStringIgnoringCase; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.notNullValue; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -41,6 +43,7 @@ import ca.uhn.fhir.rest.param.TokenAndListParam; import ca.uhn.fhir.rest.param.TokenOrListParam; import ca.uhn.fhir.rest.param.TokenParam; +import ca.uhn.fhir.rest.server.exceptions.MethodNotAllowedException; import lombok.AccessLevel; import lombok.Getter; import org.apache.commons.io.IOUtils; @@ -73,7 +76,13 @@ public class AllergyIntoleranceFhirResourceProviderWebTest extends BaseFhirR4Res private static final String PATIENT_UUID = "8d703ff2-c3e2-4070-9737-73e713d5a50d"; - private static final String JSON_CREAT_ALLERGY_PATH = "org/openmrs/module/fhir2/providers/AllergyIntoleranceWebTest_create.json"; + private static final String JSON_CREATE_ALLERGY_PATH = "org/openmrs/module/fhir2/providers/AllergyIntoleranceWebTest_create.json"; + + private static final String JSON_UPDATE_ALLERGY_PATH = "org/openmrs/module/fhir2/providers/AllergyIntoleranceWebTest_update.json"; + + private static final String JSON_UPDATE_WITHOUTID_ALLERGY_PATH = "org/openmrs/module/fhir2/providers/AllergyIntoleranceWebTest_UpdateWithoutId.json"; + + private static final String JSON_UPDATE_WITH_WRONGID_ALLERGY_PATH = "org/openmrs/module/fhir2/providers/AllergyIntoleranceWebTest_UpdateWithWrongId.json"; private static final String LAST_UPDATED_DATE = "2020-09-03"; @@ -467,11 +476,11 @@ private void verifyUri(String uri) throws Exception { } @Test - public void createAllergy_shouldCreateAllergyIntolerance() throws Exception { + public void createAllergyIntolerance_shouldCreateAllergyIntolerance() throws Exception { AllergyIntolerance allergy = new AllergyIntolerance(); allergy.setId(ALLERGY_UUID); String createAllergyJson; - try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(JSON_CREAT_ALLERGY_PATH)) { + try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(JSON_CREATE_ALLERGY_PATH)) { Objects.requireNonNull(is); createAllergyJson = IOUtils.toString(is, StandardCharsets.UTF_8); } @@ -484,4 +493,91 @@ public void createAllergy_shouldCreateAllergyIntolerance() throws Exception { assertThat(response, isCreated()); assertThat(response.getStatus(), is(201)); } + + @Test + public void updateAllergyIntolerance_shouldUpdateRequestedAllergyIntolerance() throws Exception { + AllergyIntolerance allergy = new AllergyIntolerance(); + allergy.setId(ALLERGY_UUID); + String createAllergyJson; + try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(JSON_UPDATE_ALLERGY_PATH)) { + Objects.requireNonNull(is); + createAllergyJson = IOUtils.toString(is, StandardCharsets.UTF_8); + } + + when(allergyService.update(any(String.class), any(AllergyIntolerance.class))).thenReturn(allergy); + + MockHttpServletResponse response = put("/AllergyIntolerance/" + ALLERGY_UUID).jsonContent(createAllergyJson) + .accept(FhirMediaTypes.JSON).go(); + + assertThat(response, isOk()); + } + + @Test + public void updateAllergyIntolerance_shouldThrowErrorForIdMismatch() throws Exception { + String createAllergyJson; + try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(JSON_UPDATE_ALLERGY_PATH)) { + Objects.requireNonNull(is); + createAllergyJson = IOUtils.toString(is, StandardCharsets.UTF_8); + } + + MockHttpServletResponse response = put("/AllergyIntolerance/" + WRONG_ALLERGY_UUID).jsonContent(createAllergyJson) + .accept(FhirMediaTypes.JSON).go(); + + assertThat(response, isBadRequest()); + assertThat(response.getContentAsString(), + containsStringIgnoringCase("body must contain an ID element which matches the request URL")); + } + + @Test + public void updateAllergyIntolerance_shouldThrowErrorForNoId() throws Exception { + String createAllergyJson; + try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(JSON_UPDATE_WITHOUTID_ALLERGY_PATH)) { + Objects.requireNonNull(is); + createAllergyJson = IOUtils.toString(is, StandardCharsets.UTF_8); + } + + MockHttpServletResponse response = put("/AllergyIntolerance/" + ALLERGY_UUID).jsonContent(createAllergyJson) + .accept(FhirMediaTypes.JSON).go(); + + assertThat(response, isBadRequest()); + assertThat(response.getContentAsString(), containsStringIgnoringCase("body must contain an ID element for update")); + } + + @Test + public void updateAllergyIntolerance_shouldThrowErrorForNonexistentMedication() throws Exception { + String createAllergyJson; + try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(JSON_UPDATE_WITH_WRONGID_ALLERGY_PATH)) { + Objects.requireNonNull(is); + createAllergyJson = IOUtils.toString(is, StandardCharsets.UTF_8); + } + + when(allergyService.update(eq(WRONG_ALLERGY_UUID), any(AllergyIntolerance.class))) + .thenThrow(new MethodNotAllowedException("AllergyIntolerance " + WRONG_ALLERGY_UUID + " does not exist")); + + MockHttpServletResponse response = put("/AllergyIntolerance/" + WRONG_ALLERGY_UUID).jsonContent(createAllergyJson) + .accept(FhirMediaTypes.JSON).go(); + + assertThat(response, isMethodNotAllowed()); + } + + @Test + public void deleteAllergyIntolerance_shouldDeleteRequestedAllergyIntolerance() throws Exception { + when(allergyService.delete(any(String.class))).thenReturn(allergyIntolerance); + + MockHttpServletResponse response = delete("/AllergyIntolerance/" + ALLERGY_UUID).accept(FhirMediaTypes.JSON).go(); + + assertThat(response, isOk()); + assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString())); + } + + @Test + public void deleteAllergyIntolerance_shouldReturn404ForNonExistingAllergyIntolerance() throws Exception { + when(allergyService.delete(WRONG_ALLERGY_UUID)).thenReturn(null); + + MockHttpServletResponse response = delete("/AllergyIntolerance/" + WRONG_ALLERGY_UUID).accept(FhirMediaTypes.JSON) + .go(); + + assertThat(response, isNotFound()); + } + } diff --git a/omod/src/test/resources/org/openmrs/module/fhir2/providers/AllergyIntoleranceWebTest_UpdateWithWrongId.json b/omod/src/test/resources/org/openmrs/module/fhir2/providers/AllergyIntoleranceWebTest_UpdateWithWrongId.json new file mode 100644 index 000000000..d94926bd4 --- /dev/null +++ b/omod/src/test/resources/org/openmrs/module/fhir2/providers/AllergyIntoleranceWebTest_UpdateWithWrongId.json @@ -0,0 +1,72 @@ +{ + "resourceType": "AllergyIntolerance", + "id": "2085AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", + "clinicalStatus": { + "coding": [ + { + "system": "http://terminology.hl7.org/CodeSystem/allergyintolerance-clinical", + "code": "active", + "display": "Active" + } + ], + "text": "Active" + }, + "type": "allergy", + "category": [ + "medication" + ], + "code": { + "text": "value for allergen non coded. 123456" + }, + "patient": { + "reference": "Patient/dd55571f-1691-11df-97a5-7038c432aabf", + "type": "Patient", + "identifier": { + "id": "dce9740a-1691-11df-97a5-7038c432aabf", + "use": "usual", + "system": "OpenMRS Identification Number", + "value": "35TU-9" + }, + "display": "Kimetto Akimena Ediati(OpenMRS Identification Number:35TU-9)" + }, + "recorder": { + "reference": "Practitioner/842f4bae-1692-11df-97a5-7038c432aabf", + "type": "Practitioner", + "display": "Super User" + }, + "note": [ + { + "text": "allergy comment" + } + ], + "reaction": [ + { + "substance": { + "coding": [ + { + "code": "162298AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + { + "system": "urn:oid:2.16.840.1.113883.3.7201", + "code": "162298" + } + ] + }, + "manifestation": [ + { + "coding": [ + { + "code": "1513AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + { + "system": "urn:oid:2.16.840.1.113883.3.7201", + "code": "1513" + } + ], + "text": "1513AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA text" + } + ], + "severity": "severe" + } + ] +} diff --git a/omod/src/test/resources/org/openmrs/module/fhir2/providers/AllergyIntoleranceWebTest_UpdateWithoutId.json b/omod/src/test/resources/org/openmrs/module/fhir2/providers/AllergyIntoleranceWebTest_UpdateWithoutId.json new file mode 100644 index 000000000..e1b6ea2c2 --- /dev/null +++ b/omod/src/test/resources/org/openmrs/module/fhir2/providers/AllergyIntoleranceWebTest_UpdateWithoutId.json @@ -0,0 +1,71 @@ +{ + "resourceType": "AllergyIntolerance", + "clinicalStatus": { + "coding": [ + { + "system": "http://terminology.hl7.org/CodeSystem/allergyintolerance-clinical", + "code": "active", + "display": "Active" + } + ], + "text": "Active" + }, + "type": "allergy", + "category": [ + "medication" + ], + "code": { + "text": "value for allergen non coded. 123456" + }, + "patient": { + "reference": "Patient/dd55571f-1691-11df-97a5-7038c432aabf", + "type": "Patient", + "identifier": { + "id": "dce9740a-1691-11df-97a5-7038c432aabf", + "use": "usual", + "system": "OpenMRS Identification Number", + "value": "35TU-9" + }, + "display": "Kimetto Akimena Ediati(OpenMRS Identification Number:35TU-9)" + }, + "recorder": { + "reference": "Practitioner/842f4bae-1692-11df-97a5-7038c432aabf", + "type": "Practitioner", + "display": "Super User" + }, + "note": [ + { + "text": "allergy comment" + } + ], + "reaction": [ + { + "substance": { + "coding": [ + { + "code": "162298AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + { + "system": "urn:oid:2.16.840.1.113883.3.7201", + "code": "162298" + } + ] + }, + "manifestation": [ + { + "coding": [ + { + "code": "1513AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + { + "system": "urn:oid:2.16.840.1.113883.3.7201", + "code": "1513" + } + ], + "text": "1513AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA text" + } + ], + "severity": "severe" + } + ] +} \ No newline at end of file diff --git a/omod/src/test/resources/org/openmrs/module/fhir2/providers/AllergyIntoleranceWebTest_update.json b/omod/src/test/resources/org/openmrs/module/fhir2/providers/AllergyIntoleranceWebTest_update.json new file mode 100644 index 000000000..d507aef6e --- /dev/null +++ b/omod/src/test/resources/org/openmrs/module/fhir2/providers/AllergyIntoleranceWebTest_update.json @@ -0,0 +1,72 @@ +{ + "resourceType": "AllergyIntolerance", + "id": "1085AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", + "clinicalStatus": { + "coding": [ + { + "system": "http://terminology.hl7.org/CodeSystem/allergyintolerance-clinical", + "code": "active", + "display": "Active" + } + ], + "text": "Active" + }, + "type": "allergy", + "category": [ + "medication" + ], + "code": { + "text": "value for allergen non coded. 123456" + }, + "patient": { + "reference": "Patient/dd55571f-1691-11df-97a5-7038c432aabf", + "type": "Patient", + "identifier": { + "id": "dce9740a-1691-11df-97a5-7038c432aabf", + "use": "usual", + "system": "OpenMRS Identification Number", + "value": "35TU-9" + }, + "display": "Kimetto Akimena Ediati(OpenMRS Identification Number:35TU-9)" + }, + "recorder": { + "reference": "Practitioner/842f4bae-1692-11df-97a5-7038c432aabf", + "type": "Practitioner", + "display": "Super User" + }, + "note": [ + { + "text": "allergy comment" + } + ], + "reaction": [ + { + "substance": { + "coding": [ + { + "code": "162298AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + { + "system": "urn:oid:2.16.840.1.113883.3.7201", + "code": "162298" + } + ] + }, + "manifestation": [ + { + "coding": [ + { + "code": "1513AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + { + "system": "urn:oid:2.16.840.1.113883.3.7201", + "code": "1513" + } + ], + "text": "1513AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA text" + } + ], + "severity": "severe" + } + ] +}