Search in sources :

Example 36 with IdType

use of org.hl7.fhir.r4b.model.IdType in project openmrs-module-fhir2 by openmrs.

the class FhirTaskTranslatorImplTest method toFhirResource_shouldAddProvenanceResources.

@Test
public void toFhirResource_shouldAddProvenanceResources() {
    FhirTask task = new FhirTask();
    task.setUuid(TASK_UUID);
    Provenance provenance = new Provenance();
    provenance.setId(new IdType(FhirUtils.newUuid()));
    when(provenanceTranslator.getCreateProvenance(task)).thenReturn(provenance);
    when(provenanceTranslator.getUpdateProvenance(task)).thenReturn(provenance);
    org.hl7.fhir.r4.model.Task result = taskTranslator.toFhirResource(task);
    assertThat(result, notNullValue());
    assertThat(result.getContained(), not(empty()));
    assertThat(result.getContained().size(), greaterThanOrEqualTo(2));
    assertThat(result.getContained().stream().anyMatch(resource -> resource.getResourceType().name().equals(Provenance.class.getSimpleName())), is(true));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Date(java.util.Date) Mock(org.mockito.Mock) Matchers.not(org.hamcrest.Matchers.not) Identifier(org.hl7.fhir.r4.model.Identifier) RunWith(org.junit.runner.RunWith) CodeableConcept(org.hl7.fhir.r4.model.CodeableConcept) Reference(org.hl7.fhir.r4.model.Reference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) FhirReference(org.openmrs.module.fhir2.model.FhirReference) Matchers.hasProperty(org.hamcrest.Matchers.hasProperty) Task(org.hl7.fhir.r4.model.Task) FhirUtils(org.openmrs.module.fhir2.api.util.FhirUtils) StringType(org.hl7.fhir.r4.model.StringType) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) FhirConstants(org.openmrs.module.fhir2.FhirConstants) FhirTask(org.openmrs.module.fhir2.model.FhirTask) Before(org.junit.Before) Matchers.empty(org.hamcrest.Matchers.empty) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) ProvenanceTranslator(org.openmrs.module.fhir2.api.translators.ProvenanceTranslator) Collection(java.util.Collection) Set(java.util.Set) Provenance(org.hl7.fhir.r4.model.Provenance) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) IdType(org.hl7.fhir.r4.model.IdType) Consumer(java.util.function.Consumer) List(java.util.List) Matchers.hasItem(org.hamcrest.Matchers.hasItem) Concept(org.openmrs.Concept) Coding(org.hl7.fhir.r4.model.Coding) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.is(org.hamcrest.Matchers.is) Collections(java.util.Collections) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) FhirTaskInput(org.openmrs.module.fhir2.model.FhirTaskInput) FhirTaskOutput(org.openmrs.module.fhir2.model.FhirTaskOutput) Task(org.hl7.fhir.r4.model.Task) Provenance(org.hl7.fhir.r4.model.Provenance) FhirTask(org.openmrs.module.fhir2.model.FhirTask) IdType(org.hl7.fhir.r4.model.IdType) Test(org.junit.Test)

Example 37 with IdType

use of org.hl7.fhir.r4b.model.IdType in project openmrs-module-fhir2 by openmrs.

the class ConditionTranslatorImplTest method shouldAddProvenanceToConditionResource.

@Test
public void shouldAddProvenanceToConditionResource() {
    Provenance provenance = new Provenance();
    provenance.setId(new IdType(FhirUtils.newUuid()));
    when(provenanceTranslator.getCreateProvenance(openmrsCondition)).thenReturn(provenance);
    when(provenanceTranslator.getUpdateProvenance(openmrsCondition)).thenReturn(provenance);
    org.hl7.fhir.r4.model.Condition result = conditionTranslator.toFhirResource(openmrsCondition);
    List<Resource> resources = result.getContained();
    assertThat(resources, Matchers.notNullValue());
    assertThat(resources, Matchers.not(empty()));
    assertThat(resources.stream().findAny().isPresent(), CoreMatchers.is(true));
    assertThat(resources.stream().findAny().get().isResource(), CoreMatchers.is(true));
    assertThat(resources.stream().findAny().get().getResourceType().name(), Matchers.equalTo(Provenance.class.getSimpleName()));
}
Also used : Provenance(org.hl7.fhir.r4.model.Provenance) Resource(org.hl7.fhir.r4.model.Resource) Condition(org.hl7.fhir.r4.model.Condition) IdType(org.hl7.fhir.r4.model.IdType) Test(org.junit.Test)

Example 38 with IdType

use of org.hl7.fhir.r4b.model.IdType in project elexis-server by elexis.

the class ObservationResourceProvider method createObservation.

@Create
public MethodOutcome createObservation(@ResourceParam Observation observation) {
    MethodOutcome outcome = new MethodOutcome();
    Optional<IObservation> exists = getTransformer().getLocalObject(observation);
    if (exists.isPresent()) {
        outcome.setCreated(false);
        outcome.setId(new IdType(observation.getId()));
    } else {
        Optional<IObservation> created = getTransformer().createLocalObject(observation);
        if (created.isPresent()) {
            outcome.setCreated(true);
            outcome.setId(new IdType(created.get().getId()));
        } else {
            throw new InternalErrorException("Creation failed");
        }
    }
    return outcome;
}
Also used : InternalErrorException(ca.uhn.fhir.rest.server.exceptions.InternalErrorException) MethodOutcome(ca.uhn.fhir.rest.api.MethodOutcome) IObservation(ch.elexis.core.findings.IObservation) IdType(org.hl7.fhir.r4.model.IdType) Create(ca.uhn.fhir.rest.annotation.Create)

Example 39 with IdType

use of org.hl7.fhir.r4b.model.IdType in project elexis-server by elexis.

the class ResourceProviderUtil method updateResource.

protected <T extends BaseResource, U extends Identifiable> MethodOutcome updateResource(IdType theId, IFhirTransformer<T, U> transformer, T fhirObject, Logger log) {
    String versionId = theId.getVersionIdPart();
    Optional<U> localObject = transformer.getLocalObject(fhirObject);
    MethodOutcome outcome = new MethodOutcome();
    if (localObject.isPresent()) {
        if (versionId == null) {
            log.warn("Version agnostic update on {}", localObject.get());
        }
        if (versionId != null && !versionId.equals(localObject.get().getLastupdate().toString())) {
            throw new ResourceVersionConflictException("Expected version " + localObject.get().getLastupdate().toString());
        }
        try {
            transformer.updateLocalObject(fhirObject, localObject.get());
        } catch (IFhirTransformerException e) {
            OperationOutcome opOutcome = generateOperationOutcome(e);
            throw new PreconditionFailedException(e.getMessage(), opOutcome);
        }
        Optional<T> updatedObject = transformer.getFhirObject(localObject.get());
        if (updatedObject.isPresent()) {
            outcome.setId(updatedObject.get().getIdElement());
            outcome.setResource(updatedObject.get());
            return outcome;
        }
        log.warn("Object update failed [{}]", fhirObject);
        throw new InternalErrorException("Object update failed");
    } else {
        OperationOutcome issueOutcome = new OperationOutcome();
        issueOutcome.addIssue().setDiagnostics("No local object found");
        outcome.setOperationOutcome(issueOutcome);
    }
    return outcome;
}
Also used : IFhirTransformerException(ch.elexis.core.findings.util.fhir.IFhirTransformerException) OperationOutcome(org.hl7.fhir.r4.model.OperationOutcome) PreconditionFailedException(ca.uhn.fhir.rest.server.exceptions.PreconditionFailedException) InternalErrorException(ca.uhn.fhir.rest.server.exceptions.InternalErrorException) ResourceVersionConflictException(ca.uhn.fhir.rest.server.exceptions.ResourceVersionConflictException) MethodOutcome(ca.uhn.fhir.rest.api.MethodOutcome)

Example 40 with IdType

use of org.hl7.fhir.r4b.model.IdType in project elexis-server by elexis.

the class ServiceRequestResourceProvider method createProcedureRequest.

@Create
public MethodOutcome createProcedureRequest(@ResourceParam ServiceRequest procedureRequest) {
    MethodOutcome outcome = new MethodOutcome();
    Optional<IProcedureRequest> exists = getTransformer().getLocalObject(procedureRequest);
    if (exists.isPresent()) {
        outcome.setCreated(false);
        outcome.setId(new IdType(procedureRequest.getId()));
    } else {
        Optional<IProcedureRequest> created = getTransformer().createLocalObject(procedureRequest);
        if (created.isPresent()) {
            outcome.setCreated(true);
            outcome.setId(new IdType(created.get().getId()));
        } else {
            throw new InternalErrorException("Creation failed");
        }
    }
    return outcome;
}
Also used : IProcedureRequest(ch.elexis.core.findings.IProcedureRequest) InternalErrorException(ca.uhn.fhir.rest.server.exceptions.InternalErrorException) MethodOutcome(ca.uhn.fhir.rest.api.MethodOutcome) IdType(org.hl7.fhir.r4.model.IdType) Create(ca.uhn.fhir.rest.annotation.Create)

Aggregations

IdType (org.hl7.fhir.r4.model.IdType)240 Test (org.junit.Test)240 IdType (org.hl7.fhir.dstu3.model.IdType)217 BaseFhirProvenanceResourceTest (org.openmrs.module.fhir2.providers.BaseFhirProvenanceResourceTest)87 Test (org.junit.jupiter.api.Test)72 HashMap (java.util.HashMap)70 JsonObject (javax.json.JsonObject)55 Path (javax.ws.rs.Path)55 Produces (javax.ws.rs.Produces)55 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)50 MethodOutcome (ca.uhn.fhir.rest.api.MethodOutcome)49 Bundle (org.hl7.fhir.r4.model.Bundle)45 Date (java.util.Date)44 GET (javax.ws.rs.GET)40 ArrayList (java.util.ArrayList)38 ResourceNotFoundException (ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException)36 Coding (org.hl7.fhir.r4.model.Coding)34 IBaseBundle (org.hl7.fhir.instance.model.api.IBaseBundle)33 Resource (org.hl7.fhir.r4.model.Resource)33 Provenance (org.hl7.fhir.r4.model.Provenance)32