Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FM2-212 : Extend allergyIntoleranceResource to support update delete. #212

Merged
merged 1 commit into from Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -14,31 +14,39 @@
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;
import org.hl7.fhir.convertors.conv30_40.AllergyIntolerance30_40;
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;
Expand Down Expand Up @@ -79,6 +87,36 @@ public List<Resource> getAllergyIntoleranceHistoryById(@IdParam @NotNull IdType
.map(Provenance30_40::convertProvenance).collect(Collectors.toList());
}

@Create
@SuppressWarnings("unused")
public MethodOutcome creatAllergyIntolerance(@ResourceParam AllergyIntolerance allergyIntolerance) {
Akayeshmantha marked this conversation as resolved.
Show resolved Hide resolved
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(
Expand Down
Expand Up @@ -14,27 +14,31 @@
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;
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;
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.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;
Expand Down Expand Up @@ -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);
}
}
Expand Up @@ -17,13 +17,16 @@
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;

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;
Expand All @@ -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;
Expand Down Expand Up @@ -385,4 +393,60 @@ public void searchForAllergies_shouldReturnMatchingBundleOfAllergiesByLastUpdate
assertThat(((org.hl7.fhir.r4.model.AllergyIntolerance) resultList.iterator().next()).getId(), equalTo(ALLERGY_UUID));
}

@Test
Akayeshmantha marked this conversation as resolved.
Show resolved Hide resolved
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));
}
}
Expand Up @@ -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;
Expand All @@ -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<org.hl7.fhir.r4.model.Task> {
Expand Down Expand Up @@ -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() {
Expand Down