Add validator test and allow migrator to be used for 3_3_0

This commit is contained in:
James Agnew 2018-10-08 21:04:17 -04:00
parent 34e943b49b
commit 34bb70af65
5 changed files with 144 additions and 1 deletions

View File

@ -22,6 +22,7 @@ package ca.uhn.fhir.util;
public enum VersionEnum {
V3_3_0,
V3_4_0,
V3_5_0,
V3_6_0

View File

@ -1400,7 +1400,7 @@ public class SearchBuilder implements ISearchBuilder {
myResourceTableQuery = outerQuery;
myResourceTableRoot = myResourceTableQuery.from(ResourceTable.class);
if (theCount) {
outerQuery.multiselect(myBuilder.countDistinct(myResourceTableRoot.get("myId").as(Long.class)));
outerQuery.multiselect(myBuilder.countDistinct(myResourceTableRoot));
} else {
outerQuery.multiselect(myResourceTableRoot.get("myId").as(Long.class));
}

View File

@ -78,6 +78,8 @@ public class FhirResourceDaoR4SearchOptimizedTest extends BaseJpaR4Test {
p.addName().setFamily("FAM" + leftPad(Integer.toString(201), 5, '0') + "A");
p.addName().setFamily("FAM" + leftPad(Integer.toString(201), 5, '0') + "AA");
p.addName().setFamily("FAM" + leftPad(Integer.toString(201), 5, '0') + "AAA");
p.addName().addGiven("FAMA");
p.addName().addGiven("FAMB");
myPatientDao.update(p);
myDaoConfig.setSearchPreFetchThresholds(Arrays.asList(20, 50, 190));
@ -103,6 +105,18 @@ public class FhirResourceDaoR4SearchOptimizedTest extends BaseJpaR4Test {
ids = toUnqualifiedVersionlessIdValues(results, 0, 10, true);
assertThat(ids, hasSize(10));
assertEquals(201, myDatabaseBackedPagingProvider.retrieveResultList(uuid).size().intValue());
// Seach with count only
params = new SearchParameterMap();
params.add(Patient.SP_NAME, new StringParam().setMissing(false));
params.setSummaryMode(Sets.newHashSet(SummaryEnum.COUNT));
results = myPatientDao.search(params);
uuid = results.getUuid();
assertEquals(201, results.size().intValue());
ids = toUnqualifiedVersionlessIdValues(results, 0, 10, true);
assertThat(ids, empty());
assertEquals(201, myDatabaseBackedPagingProvider.retrieveResultList(uuid).size().intValue());
}

View File

@ -0,0 +1,109 @@
package ca.uhn.fhir.jpa.dao.r4;
import ca.uhn.fhir.rest.api.server.RequestDetails;
import ca.uhn.fhir.rest.server.interceptor.ServerOperationInterceptorAdapter;
import ca.uhn.fhir.util.TestUtil;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IIdType;
import org.hl7.fhir.r4.model.Identifier;
import org.hl7.fhir.r4.model.Patient;
import org.hl7.fhir.r4.model.Resource;
import org.junit.AfterClass;
import org.junit.Test;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.junit.Assert.assertEquals;
public class FhirResourceDaoR4SelectiveUpdateTest extends BaseJpaR4Test {
public static final String EUID_SYSTEM = "http://euid";
@Test
public void testInterceptorPreservesAttribute() throws Exception {
CentralAttributesPreservationInterceptor interceptor = new CentralAttributesPreservationInterceptor();
myDaoConfig.getInterceptors().add(interceptor);
try {
// Create the patient with no additional identifier
Patient p = new Patient();
p.setActive(true);
p.addIdentifier().setSystem("http://foo").setValue("bar");
IIdType id = myPatientDao.create(p).getId().toUnqualified();
assertEquals("1", id.getVersionIdPart());
// Update to add a preserved identifier
p = new Patient();
p.setId(id.toVersionless());
p.setActive(true);
p.addIdentifier(new Identifier().setSystem("http://foo").setValue("bar"));
p.addIdentifier(new Identifier().setSystem(EUID_SYSTEM).setValue("123"));
id = myPatientDao.update(p).getId().toUnqualified();
assertEquals("2", id.getVersionIdPart());
// Update to change something but include the preserved attribute
p = new Patient();
p.setId(id.toVersionless());
p.setActive(false);
p.addIdentifier(new Identifier().setSystem("http://foo").setValue("bar"));
id = myPatientDao.update(p).getId().toUnqualified();
assertEquals("3", id.getVersionIdPart());
// Read it back
p = myPatientDao.read(id);
assertEquals(false, p.getActive());
assertEquals(2, p.getIdentifier().size());
} finally {
myDaoConfig.getInterceptors().remove(interceptor);
}
}
public class CentralAttributesPreservationInterceptor extends ServerOperationInterceptorAdapter {
@Override
public void resourcePreUpdate(RequestDetails theRequest, IBaseResource theOldResource, IBaseResource theNewResource) {
Resource oldResource = (Resource) theOldResource;
Resource newResource = (Resource) theNewResource;
if (theOldResource instanceof Patient) {
Patient oldPt = (Patient) oldResource;
Patient newPt = (Patient) newResource;
Identifier oldPtEuid = getEuidIdentifier(oldPt, false);
if (oldPtEuid == null || isBlank(oldPtEuid.getValue())) {
return;
}
Identifier newPtEuid = getEuidIdentifier(newPt, true);
if (isBlank(newPtEuid.getValue())) {
newPtEuid.setValue(oldPtEuid.getValue());
}
}
}
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
public static Identifier getEuidIdentifier(Patient thePt, boolean theCreate) {
return thePt
.getIdentifier()
.stream()
.filter(t -> t.getSystem().equals("http://euid"))
.findFirst()
.orElseGet(() -> {
if (!theCreate) {
return null;
}
Identifier identifier = new Identifier();
identifier.setSystem(EUID_SYSTEM);
identifier.setUse(Identifier.IdentifierUse.SECONDARY);
thePt.getIdentifier().add(identifier);
return identifier;
});
}
}

View File

@ -223,6 +223,25 @@ public class FhirInstanceValidatorR4Test {
return retVal;
}
@Test
public void testValidateCodeWithTailingSpace() {
Patient p = new Patient();
p
.getMaritalStatus()
.addCoding()
.setSystem("http://foo")
.setCode("AA ");
FhirValidator val = ourCtx.newValidator();
val.registerValidatorModule(new FhirInstanceValidator(myDefaultValidationSupport));
ValidationResult result = val.validateWithResult(p);
List<SingleValidationMessage> all = logResultsAndReturnErrorOnes(result);
assertFalse(result.isSuccessful());
assertEquals("The code 'AA ' is not valid (whitespace rules)", all.get(0).getMessage());
}
/**
* See #938
*/