You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by jp...@apache.org on 2018/10/01 14:52:02 UTC

[camel] 01/02: camel-fhir Id elements are optional when updating a resource

This is an automated email from the ASF dual-hosted git repository.

jpoth pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 26e45b859d65af9d51d834e92c52d14baf508fdf
Author: jpoth <po...@gmail.com>
AuthorDate: Fri Sep 21 12:27:06 2018 +0200

    camel-fhir Id elements are optional when updating a resource
---
 .../camel/component/fhir/api/FhirUpdate.java       | 30 +++++++++++++++++++---
 components/camel-fhir/camel-fhir-component/pom.xml |  2 ++
 .../apache/camel/component/fhir/FhirUpdateIT.java  | 30 +++++++++++++++++-----
 3 files changed, 52 insertions(+), 10 deletions(-)

diff --git a/components/camel-fhir/camel-fhir-api/src/main/java/org/apache/camel/component/fhir/api/FhirUpdate.java b/components/camel-fhir/camel-fhir-api/src/main/java/org/apache/camel/component/fhir/api/FhirUpdate.java
index 28e2a85..77a257e 100644
--- a/components/camel-fhir/camel-fhir-api/src/main/java/org/apache/camel/component/fhir/api/FhirUpdate.java
+++ b/components/camel-fhir/camel-fhir-api/src/main/java/org/apache/camel/component/fhir/api/FhirUpdate.java
@@ -21,6 +21,8 @@ import ca.uhn.fhir.rest.api.MethodOutcome;
 import ca.uhn.fhir.rest.api.PreferReturnEnum;
 import ca.uhn.fhir.rest.client.api.IGenericClient;
 import ca.uhn.fhir.rest.gclient.IUpdateExecutable;
+import ca.uhn.fhir.rest.gclient.IUpdateTyped;
+import org.apache.camel.util.ObjectHelper;
 import org.hl7.fhir.instance.model.api.IBaseResource;
 import org.hl7.fhir.instance.model.api.IIdType;
 
@@ -36,25 +38,29 @@ public class FhirUpdate {
     }
 
     public MethodOutcome resource(IBaseResource resource, IIdType id, PreferReturnEnum preferReturn, Map<ExtraParameters, Object> extraParameters) {
-        IUpdateExecutable updateExecutable = client.update().resource(resource).withId(id);
+        IUpdateTyped updateTyped = client.update().resource(resource);
+        IUpdateExecutable updateExecutable = withOptionalId(id, updateTyped);
         ExtraParameters.process(extraParameters, updateExecutable);
         return processOptionalParam(preferReturn, updateExecutable);
     }
 
     public MethodOutcome resource(String resourceAsString, IIdType id, PreferReturnEnum preferReturn, Map<ExtraParameters, Object> extraParameters) {
-        IUpdateExecutable updateExecutable = client.update().resource(resourceAsString).withId(id);
+        IUpdateTyped updateTyped = client.update().resource(resourceAsString);
+        IUpdateExecutable updateExecutable = withOptionalId(id, updateTyped);
         ExtraParameters.process(extraParameters, updateExecutable);
         return processOptionalParam(preferReturn, updateExecutable);
     }
 
     public MethodOutcome resource(IBaseResource resource, String stringId, PreferReturnEnum preferReturn, Map<ExtraParameters, Object> extraParameters) {
-        IUpdateExecutable updateExecutable = client.update().resource(resource).withId(stringId);
+        IUpdateTyped updateTyped = client.update().resource(resource);
+        IUpdateExecutable updateExecutable = withOptionalId(stringId, updateTyped);
         ExtraParameters.process(extraParameters, updateExecutable);
         return processOptionalParam(preferReturn, updateExecutable);
     }
 
     public MethodOutcome resource(String resourceAsString, String stringId, PreferReturnEnum preferReturn, Map<ExtraParameters, Object> extraParameters) {
-        IUpdateExecutable updateExecutable = client.update().resource(resourceAsString).withId(stringId);
+        IUpdateTyped updateTyped = client.update().resource(resourceAsString);
+        IUpdateExecutable updateExecutable = withOptionalId(stringId, updateTyped);
         ExtraParameters.process(extraParameters, updateExecutable);
         return processOptionalParam(preferReturn, updateExecutable);
     }
@@ -77,4 +83,20 @@ public class FhirUpdate {
         }
         return updateExecutable.execute();
     }
+
+    private IUpdateExecutable withOptionalId(IIdType id, IUpdateTyped updateTyped) {
+        if (ObjectHelper.isNotEmpty(id)) {
+            return updateTyped.withId(id);
+        } else {
+            return updateTyped;
+        }
+    }
+
+    private IUpdateExecutable withOptionalId(String stringId, IUpdateTyped updateTyped) {
+        if (ObjectHelper.isNotEmpty(stringId)) {
+            return updateTyped.withId(stringId);
+        } else {
+            return updateTyped;
+        }
+    }
 }
diff --git a/components/camel-fhir/camel-fhir-component/pom.xml b/components/camel-fhir/camel-fhir-component/pom.xml
index 03a39a1..d02e3e7 100644
--- a/components/camel-fhir/camel-fhir-component/pom.xml
+++ b/components/camel-fhir/camel-fhir-component/pom.xml
@@ -261,6 +261,8 @@
                   <proxyClass>org.apache.camel.component.fhir.api.FhirUpdate</proxyClass>
                   <fromJavadoc/>
                   <nullableOptions>
+                    <nullableOption>id</nullableOption>
+                    <nullableOption>stringId</nullableOption>
                     <nullableOption>preferReturn</nullableOption>
                     <nullableOption>extraParameters</nullableOption>
                   </nullableOptions>
diff --git a/components/camel-fhir/camel-fhir-component/src/test/java/org/apache/camel/component/fhir/FhirUpdateIT.java b/components/camel-fhir/camel-fhir-component/src/test/java/org/apache/camel/component/fhir/FhirUpdateIT.java
index 4e97050..e85738a 100644
--- a/components/camel-fhir/camel-fhir-component/src/test/java/org/apache/camel/component/fhir/FhirUpdateIT.java
+++ b/components/camel-fhir/camel-fhir-component/src/test/java/org/apache/camel/component/fhir/FhirUpdateIT.java
@@ -61,6 +61,24 @@ public class FhirUpdateIT extends AbstractFhirTestSupport {
     }
 
     @Test
+    public void testResourceNoId() throws Exception {
+        Date date = new SimpleDateFormat("yyyy-MM-dd").parse("1998-04-29");
+        assertNotEquals(date, patient.getBirthDate());
+        this.patient.setBirthDate(date);
+        final Map<String, Object> headers = new HashMap<>();
+        // parameter type is org.hl7.fhir.instance.model.api.IBaseResource
+        headers.put("CamelFhir.resource", this.patient);
+        // parameter type is ca.uhn.fhir.rest.api.PreferReturnEnum
+        headers.put("CamelFhir.preferReturn", PreferReturnEnum.REPRESENTATION);
+
+        MethodOutcome result = requestBodyAndHeaders("direct://RESOURCE", null, headers);
+
+        assertNotNull("resource result", result);
+        LOG.debug("resource: " + result);
+        assertEquals("Birth date not updated!", date, ((Patient)result.getResource()).getBirthDate());
+    }
+
+    @Test
     public void testResourceStringId() throws Exception {
         Date date = new SimpleDateFormat("yyyy-MM-dd").parse("1998-04-29");
         assertNotEquals(date, patient.getBirthDate());
@@ -168,27 +186,27 @@ public class FhirUpdateIT extends AbstractFhirTestSupport {
             public void configure() {
                 // test route for resource
                 from("direct://RESOURCE")
-                    .to("fhir://" + PATH_PREFIX + "/resource");
+                        .to("fhir://" + PATH_PREFIX + "/resource");
 
                 // test route for resource
                 from("direct://RESOURCE_WITH_STRING_ID")
-                    .to("fhir://" + PATH_PREFIX + "/resource");
+                        .to("fhir://" + PATH_PREFIX + "/resource");
 
                 // test route for resource
                 from("direct://RESOURCE_AS_STRING")
-                    .to("fhir://" + PATH_PREFIX + "/resource");
+                        .to("fhir://" + PATH_PREFIX + "/resource");
 
                 // test route for resource
                 from("direct://RESOURCE_AS_STRING_WITH_STRING_ID")
-                    .to("fhir://" + PATH_PREFIX + "/resource");
+                        .to("fhir://" + PATH_PREFIX + "/resource");
 
                 // test route for resourceBySearchUrl
                 from("direct://RESOURCE_BY_SEARCH_URL")
-                    .to("fhir://" + PATH_PREFIX + "/resourceBySearchUrl");
+                        .to("fhir://" + PATH_PREFIX + "/resourceBySearchUrl");
 
                 // test route for resourceBySearchUrl
                 from("direct://RESOURCE_BY_SEARCH_URL_AND_RESOURCE_AS_STRING")
-                    .to("fhir://" + PATH_PREFIX + "/resourceBySearchUrl");
+                        .to("fhir://" + PATH_PREFIX + "/resourceBySearchUrl");
 
             }
         };