From b09e53638708f355fe5dfe5982cfafcce32afdbd Mon Sep 17 00:00:00 2001 From: Amir Toosi <54131619+amir-kt@users.noreply.github.com> Date: Thu, 13 Apr 2023 04:31:58 +1000 Subject: [PATCH] Fix HumanName getNameAsSingleString never returning text element (#1207) * Fix HumanName getNameAsSingleString never returning text element * Add test + gentle rename in joinStringsSpaceSeparated * removed unused variable from unit test. added fix for getNameAsSingleString to r5. * changed variable names in joinStringSpaceSeparated to more meaningful names * Add tests and fixes for dstu2016may, dstu3, and r4b + gentle refactor --------- Co-authored-by: dotasek --- .../hl7/fhir/dstu2016may/model/HumanName.java | 22 +++++++------- .../fhir/dstu2016may/model/HumanNameTest.java | 28 +++++++++++++++++ .../org/hl7/fhir/dstu3/model/HumanName.java | 26 ++++++++-------- .../hl7/fhir/dstu3/model/HumanNameTest.java | 28 +++++++++++++++++ .../java/org/hl7/fhir/r4/model/HumanName.java | 18 ++++++----- .../org/hl7/fhir/r4/model/HumanNameTest.java | 30 +++++++++++++++++++ .../org/hl7/fhir/r4b/model/HumanName.java | 20 +++++++------ .../org/hl7/fhir/r4b/model/HumanNameTest.java | 28 +++++++++++++++++ .../java/org/hl7/fhir/r5/model/HumanName.java | 26 ++++++++-------- .../org/hl7/fhir/r5/model/HumanNameTest.java | 30 +++++++++++++++++++ 10 files changed, 204 insertions(+), 52 deletions(-) create mode 100644 org.hl7.fhir.dstu2016may/src/test/java/org/hl7/fhir/dstu2016may/model/HumanNameTest.java create mode 100644 org.hl7.fhir.dstu3/src/test/java/org/hl7/fhir/dstu3/model/HumanNameTest.java create mode 100644 org.hl7.fhir.r4/src/test/java/org/hl7/fhir/r4/model/HumanNameTest.java create mode 100644 org.hl7.fhir.r4b/src/test/java/org/hl7/fhir/r4b/model/HumanNameTest.java create mode 100644 org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/model/HumanNameTest.java diff --git a/org.hl7.fhir.dstu2016may/src/main/java/org/hl7/fhir/dstu2016may/model/HumanName.java b/org.hl7.fhir.dstu2016may/src/main/java/org/hl7/fhir/dstu2016may/model/HumanName.java index a7cb82f47..08aa8c554 100644 --- a/org.hl7.fhir.dstu2016may/src/main/java/org/hl7/fhir/dstu2016may/model/HumanName.java +++ b/org.hl7.fhir.dstu2016may/src/main/java/org/hl7/fhir/dstu2016may/model/HumanName.java @@ -112,7 +112,7 @@ public class HumanName extends Type implements ICompositeType { case ANONYMOUS: return "anonymous"; case OLD: return "old"; case MAIDEN: return "maiden"; - case NULL: return null; + case NULL: return null; default: return "?"; } } @@ -125,7 +125,7 @@ public class HumanName extends Type implements ICompositeType { case ANONYMOUS: return "http://hl7.org/fhir/name-use"; case OLD: return "http://hl7.org/fhir/name-use"; case MAIDEN: return "http://hl7.org/fhir/name-use"; - case NULL: return null; + case NULL: return null; default: return "?"; } } @@ -138,7 +138,7 @@ public class HumanName extends Type implements ICompositeType { case ANONYMOUS: return "Anonymous assigned name, alias, or pseudonym (used to protect a person's identity for privacy reasons)"; case OLD: return "This name is no longer in use (or was never correct, but retained for records)"; case MAIDEN: return "A name used prior to marriage. Marriage naming customs vary greatly around the world. This name use is for use by applications that collect and store \"maiden\" names. Though the concept of maiden name is often gender specific, the use of this term is not gender specific. The use of this term does not imply any particular history for a person's name, nor should the maiden name be determined algorithmically."; - case NULL: return null; + case NULL: return null; default: return "?"; } } @@ -151,7 +151,7 @@ public class HumanName extends Type implements ICompositeType { case ANONYMOUS: return "Anonymous"; case OLD: return "Old"; case MAIDEN: return "Maiden"; - case NULL: return null; + case NULL: return null; default: return "?"; } } @@ -680,17 +680,17 @@ public class HumanName extends Type implements ICompositeType { * TODO: replace with call to ca.uhn.fhir.util.DatatypeUtil.joinStringsSpaceSeparated when HAPI upgrades to 1.4 */ private static String joinStringsSpaceSeparated(List> theStrings) { - StringBuilder b = new StringBuilder(); - for (IPrimitiveType next : theStrings) { - if (next.isEmpty()) { + StringBuilder stringBuilder = new StringBuilder(); + for (IPrimitiveType string : theStrings) { + if (string.isEmpty()) { continue; } - if (b.length() > 0) { - b.append(' '); + if (stringBuilder.length() > 0) { + stringBuilder.append(' '); } - b.append(next.getValue()); + stringBuilder.append(string.getValue()); } - return b.toString(); + return stringBuilder.toString(); } protected void listChildren(List childrenList) { super.listChildren(childrenList); diff --git a/org.hl7.fhir.dstu2016may/src/test/java/org/hl7/fhir/dstu2016may/model/HumanNameTest.java b/org.hl7.fhir.dstu2016may/src/test/java/org/hl7/fhir/dstu2016may/model/HumanNameTest.java new file mode 100644 index 000000000..5e2c06f95 --- /dev/null +++ b/org.hl7.fhir.dstu2016may/src/test/java/org/hl7/fhir/dstu2016may/model/HumanNameTest.java @@ -0,0 +1,28 @@ +package org.hl7.fhir.dstu2016may.model; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class HumanNameTest { + + @Test + public void getNameAsSingleStringWithoutFamilyElement() { + final String expected = "dummy value"; + HumanName humanName = new HumanName() + .setTextElement(new StringType(expected)); + + String actual = humanName.getNameAsSingleString(); + assertEquals(expected, actual); + } + + @Test + public void getNameAsSingleStringWithFamilyElement() { + final String expected = "good value"; + HumanName humanName = new HumanName() + .addFamily(expected); + + String actual = humanName.getNameAsSingleString(); + assertEquals(expected, actual); + } +} \ No newline at end of file diff --git a/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/model/HumanName.java b/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/model/HumanName.java index aaa8e7a0c..814412456 100644 --- a/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/model/HumanName.java +++ b/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/model/HumanName.java @@ -114,7 +114,7 @@ public class HumanName extends Type implements ICompositeType { case ANONYMOUS: return "anonymous"; case OLD: return "old"; case MAIDEN: return "maiden"; - case NULL: return null; + case NULL: return null; default: return "?"; } } @@ -127,7 +127,7 @@ public class HumanName extends Type implements ICompositeType { case ANONYMOUS: return "http://hl7.org/fhir/name-use"; case OLD: return "http://hl7.org/fhir/name-use"; case MAIDEN: return "http://hl7.org/fhir/name-use"; - case NULL: return null; + case NULL: return null; default: return "?"; } } @@ -140,7 +140,7 @@ public class HumanName extends Type implements ICompositeType { case ANONYMOUS: return "Anonymous assigned name, alias, or pseudonym (used to protect a person's identity for privacy reasons)"; case OLD: return "This name is no longer in use (or was never correct, but retained for records)"; case MAIDEN: return "A name used prior to changing name because of marriage. This name use is for use by applications that collect and store names that were used prior to a marriage. Marriage naming customs vary greatly around the world, and are constantly changing. This term is not gender specific. The use of this term does not imply any particular history for a person's name"; - case NULL: return null; + case NULL: return null; default: return "?"; } } @@ -153,7 +153,7 @@ public class HumanName extends Type implements ICompositeType { case ANONYMOUS: return "Anonymous"; case OLD: return "Old"; case MAIDEN: return "Name changed for Marriage"; - case NULL: return null; + case NULL: return null; default: return "?"; } } @@ -678,7 +678,9 @@ public class HumanName extends Type implements ICompositeType { List nameParts = new ArrayList(); nameParts.addAll(getPrefix()); nameParts.addAll(getGiven()); - nameParts.add(getFamilyElement()); + if (hasFamilyElement()) { + nameParts.add(getFamilyElement()); + } nameParts.addAll(getSuffix()); if (nameParts.size() > 0) { return joinStringsSpaceSeparated(nameParts); @@ -693,17 +695,17 @@ public class HumanName extends Type implements ICompositeType { * TODO: replace with call to ca.uhn.fhir.util.DatatypeUtil.joinStringsSpaceSeparated when HAPI upgrades to 1.4 */ private static String joinStringsSpaceSeparated(List> theStrings) { - StringBuilder b = new StringBuilder(); - for (IPrimitiveType next : theStrings) { - if (next.isEmpty()) { + StringBuilder stringBuilder = new StringBuilder(); + for (IPrimitiveType string : theStrings) { + if (string.isEmpty()) { continue; } - if (b.length() > 0) { - b.append(' '); + if (stringBuilder.length() > 0) { + stringBuilder.append(' '); } - b.append(next.getValue()); + stringBuilder.append(string.getValue()); } - return b.toString(); + return stringBuilder.toString(); } protected void listChildren(List children) { super.listChildren(children); diff --git a/org.hl7.fhir.dstu3/src/test/java/org/hl7/fhir/dstu3/model/HumanNameTest.java b/org.hl7.fhir.dstu3/src/test/java/org/hl7/fhir/dstu3/model/HumanNameTest.java new file mode 100644 index 000000000..166fc4ed8 --- /dev/null +++ b/org.hl7.fhir.dstu3/src/test/java/org/hl7/fhir/dstu3/model/HumanNameTest.java @@ -0,0 +1,28 @@ +package org.hl7.fhir.dstu3.model; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class HumanNameTest { + + @Test + public void getNameAsSingleStringWithoutFamilyElement() { + final String expected = "dummy value"; + HumanName humanName = new HumanName() + .setTextElement(new StringType(expected)); + + String actual = humanName.getNameAsSingleString(); + assertEquals(expected, actual); + } + + @Test + public void getNameAsSingleStringWithFamilyElement() { + final String expected = "good value"; + HumanName humanName = new HumanName() + .setFamily(expected); + + String actual = humanName.getNameAsSingleString(); + assertEquals(expected, actual); + } +} \ No newline at end of file diff --git a/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/model/HumanName.java b/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/model/HumanName.java index 0f88f3cc2..0a41b4837 100644 --- a/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/model/HumanName.java +++ b/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/model/HumanName.java @@ -678,7 +678,9 @@ public class HumanName extends Type implements ICompositeType { List nameParts = new ArrayList(); nameParts.addAll(getPrefix()); nameParts.addAll(getGiven()); - nameParts.add(getFamilyElement()); + if (hasFamilyElement()) { + nameParts.add(getFamilyElement()); + } nameParts.addAll(getSuffix()); if (nameParts.size() > 0) { return joinStringsSpaceSeparated(nameParts); @@ -693,17 +695,17 @@ public class HumanName extends Type implements ICompositeType { * TODO: replace with call to ca.uhn.fhir.util.DatatypeUtil.joinStringsSpaceSeparated when HAPI upgrades to 1.4 */ private static String joinStringsSpaceSeparated(List> theStrings) { - StringBuilder b = new StringBuilder(); - for (IPrimitiveType next : theStrings) { - if (next.isEmpty()) { + StringBuilder stringBuilder = new StringBuilder(); + for (IPrimitiveType string : theStrings) { + if (string.isEmpty()) { continue; } - if (b.length() > 0) { - b.append(' '); + if (stringBuilder.length() > 0) { + stringBuilder.append(' '); } - b.append(next.getValue()); + stringBuilder.append(string.getValue()); } - return b.toString(); + return stringBuilder.toString(); } protected void listChildren(List children) { super.listChildren(children); diff --git a/org.hl7.fhir.r4/src/test/java/org/hl7/fhir/r4/model/HumanNameTest.java b/org.hl7.fhir.r4/src/test/java/org/hl7/fhir/r4/model/HumanNameTest.java new file mode 100644 index 000000000..40a754e27 --- /dev/null +++ b/org.hl7.fhir.r4/src/test/java/org/hl7/fhir/r4/model/HumanNameTest.java @@ -0,0 +1,30 @@ +package org.hl7.fhir.r4.model; + +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class HumanNameTest { + + @Test + public void getNameAsSingleStringWithoutFamilyElement() { + final String expected = "dummy value"; + HumanName humanName = new HumanName() + .setTextElement(new StringType(expected)); + + String actual = humanName.getNameAsSingleString(); + assertEquals(expected, actual); + } + + @Test + public void getNameAsSingleStringWithFamilyElement() { + final String expected = "good value"; + HumanName humanName = new HumanName() + .setFamily(expected); + + String actual = humanName.getNameAsSingleString(); + assertEquals(expected, actual); + } +} diff --git a/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/model/HumanName.java b/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/model/HumanName.java index 8056f51ff..6f2b34733 100644 --- a/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/model/HumanName.java +++ b/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/model/HumanName.java @@ -899,8 +899,10 @@ public class HumanName extends DataType implements ICompositeType { public String getNameAsSingleString() { List nameParts = new ArrayList(); nameParts.addAll(getPrefix()); - nameParts.addAll(getGiven()); - nameParts.add(getFamilyElement()); + nameParts.addAll(getGiven()); + if (hasFamilyElement()) { + nameParts.add(getFamilyElement()); + } nameParts.addAll(getSuffix()); if (nameParts.size() > 0) { return joinStringsSpaceSeparated(nameParts); @@ -915,17 +917,17 @@ public class HumanName extends DataType implements ICompositeType { * TODO: replace with call to ca.uhn.fhir.util.DatatypeUtil.joinStringsSpaceSeparated when HAPI upgrades to 1.4 */ private static String joinStringsSpaceSeparated(List> theStrings) { - StringBuilder b = new StringBuilder(); - for (IPrimitiveType next : theStrings) { - if (next.isEmpty()) { + StringBuilder stringBuilder = new StringBuilder(); + for (IPrimitiveType string : theStrings) { + if (string.isEmpty()) { continue; } - if (b.length() > 0) { - b.append(' '); + if (stringBuilder.length() > 0) { + stringBuilder.append(' '); } - b.append(next.getValue()); + stringBuilder.append(string.getValue()); } - return b.toString(); + return stringBuilder.toString(); } // end addition diff --git a/org.hl7.fhir.r4b/src/test/java/org/hl7/fhir/r4b/model/HumanNameTest.java b/org.hl7.fhir.r4b/src/test/java/org/hl7/fhir/r4b/model/HumanNameTest.java new file mode 100644 index 000000000..ef5a7892a --- /dev/null +++ b/org.hl7.fhir.r4b/src/test/java/org/hl7/fhir/r4b/model/HumanNameTest.java @@ -0,0 +1,28 @@ +package org.hl7.fhir.r4b.model; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class HumanNameTest { + + @Test + public void getNameAsSingleStringWithoutFamilyElement() { + final String expected = "dummy value"; + HumanName humanName = new HumanName() + .setTextElement(new StringType(expected)); + + String actual = humanName.getNameAsSingleString(); + assertEquals(expected, actual); + } + + @Test + public void getNameAsSingleStringWithFamilyElement() { + final String expected = "good value"; + HumanName humanName = new HumanName() + .setFamily(expected); + + String actual = humanName.getNameAsSingleString(); + assertEquals(expected, actual); + } +} \ No newline at end of file diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/model/HumanName.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/model/HumanName.java index 80959136c..e68bd237e 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/model/HumanName.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/model/HumanName.java @@ -900,7 +900,9 @@ public class HumanName extends DataType implements ICompositeType { List nameParts = new ArrayList(); nameParts.addAll(getPrefix()); nameParts.addAll(getGiven()); - nameParts.add(getFamilyElement()); + if (hasFamilyElement()) { + nameParts.add(getFamilyElement()); + } nameParts.addAll(getSuffix()); if (nameParts.size() > 0) { return joinStringsSpaceSeparated(nameParts); @@ -915,17 +917,17 @@ public class HumanName extends DataType implements ICompositeType { * TODO: replace with call to ca.uhn.fhir.util.DatatypeUtil.joinStringsSpaceSeparated when HAPI upgrades to 1.4 */ private static String joinStringsSpaceSeparated(List> theStrings) { - StringBuilder b = new StringBuilder(); - for (IPrimitiveType next : theStrings) { - if (next.isEmpty()) { - continue; - } - if (b.length() > 0) { - b.append(' '); - } - b.append(next.getValue()); - } - return b.toString(); + StringBuilder stringBuilder = new StringBuilder(); + for (IPrimitiveType string : theStrings) { + if (string.isEmpty()) { + continue; + } + if (stringBuilder.length() > 0) { + stringBuilder.append(' '); + } + stringBuilder.append(string.getValue()); + } + return stringBuilder.toString(); } // end addition diff --git a/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/model/HumanNameTest.java b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/model/HumanNameTest.java new file mode 100644 index 000000000..a130be4b9 --- /dev/null +++ b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/model/HumanNameTest.java @@ -0,0 +1,30 @@ +package org.hl7.fhir.r5.model; + +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class HumanNameTest { + + @Test + public void getNameAsSingleStringWithoutFamilyElement() { + final String expected = "dummy value"; + HumanName humanName = new HumanName() + .setTextElement(new StringType(expected)); + + String actual = humanName.getNameAsSingleString(); + assertEquals(expected, actual); + } + + @Test + public void getNameAsSingleStringWithFamilyElement() { + final String expected = "good value"; + HumanName humanName = new HumanName() + .setFamily(expected); + + String actual = humanName.getNameAsSingleString(); + assertEquals(expected, actual); + } +}