Skip to content

Commit

Permalink
FM2-631: HumanName.text should use the NameTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
ibacher committed Jan 12, 2024
1 parent b4dcbe2 commit a229906
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public HumanName toFhirResource(@Nonnull PersonName name) {
addNameExtension(humanName, "familyNameSuffix", name.getFamilyNameSuffix());
addNameExtension(humanName, "degree", name.getDegree());

humanName.setText(name.getFullName());

return humanName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,25 @@
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.Arrays;
import java.util.Collections;

import org.hl7.fhir.r4.model.Extension;
import org.hl7.fhir.r4.model.HumanName;
import org.hl7.fhir.r4.model.StringType;
import org.junit.Before;
import org.junit.Test;
import org.openmrs.PersonName;
import org.openmrs.api.AdministrationService;
import org.openmrs.api.context.ServiceContext;
import org.openmrs.layout.name.NameSupport;
import org.openmrs.layout.name.NameTemplate;
import org.openmrs.module.fhir2.FhirConstants;
import org.openmrs.serialization.SerializationException;
import org.openmrs.serialization.SimpleXStreamSerializer;

public class PersonNameTranslatorImplTest {

Expand Down Expand Up @@ -152,6 +163,44 @@ public void shouldOnlyCreateOneExtensionForExtensibleAttributes() {
assertThat(extension.getExtension().size(), greaterThan(1));
}

@Test
public void shouldUseDefaultNameTemplateToSetNameText() throws SerializationException {
AdministrationService administrationService = mock(AdministrationService.class);
ServiceContext.getInstance().setAdministrationService(administrationService);
when(administrationService.getGlobalProperty("layout.name.format")).thenReturn("test");

NameSupport nameSupportInstance = new NameSupport();
NameTemplate customNameTemplate = new SimpleXStreamSerializer()
.deserialize("<org.openmrs.layout.name.NameTemplate>\n" + " <codeName>test</codeName>\n"
+ " <displayName>Test Name Format</displayName>\n" + " <nameMappings class=\"properties\">\n"
+ " <property name=\"givenName\" value=\"PersonName.givenName\"/>\n"
+ " <property name=\"middleName\" value=\"PersonName.middleName\"/>\n"
+ " <property name=\"familyName\" value=\"PersonName.familyName\"/>\n" + " </nameMappings>\n"
+ " <sizeMappings class=\"properties\">\n" + " <property name=\"givenName\" value=\"25\"/>\n"
+ " <property name=\"middleName\" value=\"25\"/>\n"
+ " <property name=\"familyName\" value=\"25\"/>\n" + " </sizeMappings>\n"
+ " <lineByLineFormat>\n" + " <string>givenName</string>\n" + " <string>familyName</string>\n"
+ " <string>middleName</string>\n" + " </lineByLineFormat>\n" + " <requiredElements>\n"
+ " <string>givenName</string>\n" + " <string>familyName</string>\n"
+ " </requiredElements>\n" + "</org.openmrs.layout.name.NameTemplate>",
NameTemplate.class);
nameSupportInstance.setLayoutTemplates(Collections.singletonList(customNameTemplate));
nameSupportInstance.setSpecialTokens(Arrays.asList("prefix", "givenName", "middleName", "familyNamePrefix",
"familyNameSuffix", "familyName2", "familyName", "degree"));

PersonName name = new PersonName();
name.setGivenName(PERSON_GIVEN_NAME);
name.setMiddleName(PERSON_MIDDLE_NAME);
name.setFamilyName(PERSON_FAMILY_NAME);

HumanName fhirName = personNameTranslator.toFhirResource(name);

assertThat(fhirName, notNullValue());
assertThat(fhirName.getTextElement(), notNullValue());
assertThat(fhirName.getTextElement().getValue(),
equalTo(PERSON_GIVEN_NAME + " " + PERSON_FAMILY_NAME + " " + PERSON_MIDDLE_NAME));
}

@Test
public void shouldConvertHumanNameToPersonName() {
HumanName name = new HumanName();
Expand Down

0 comments on commit a229906

Please sign in to comment.