Adjust unit tests to deal with asynchronous search parameter registry

This commit is contained in:
jamesagnew 2018-02-15 07:12:30 -05:00
parent e6253b7f22
commit 4f45ad87f3
11 changed files with 668 additions and 627 deletions

View File

@ -632,7 +632,7 @@ public abstract class BaseHapiFhirResourceDao<T extends IBaseResource> extends B
}
}
mySearchParamRegistry.forceRefresh();
mySearchParamRegistry.requestRefresh();
}
@Override

View File

@ -61,12 +61,18 @@ public abstract class BaseSearchParamRegistry<SP extends IBaseResource> implemen
}
@Override
public void forceRefresh() {
public void requestRefresh() {
synchronized (this) {
myLastRefresh = 0;
}
}
@Override
public void forceRefresh() {
requestRefresh();
refreshCacheIfNecessary();
}
@Override
public RuntimeSearchParam getActiveSearchParam(String theResourceName, String theParamName) {
RuntimeSearchParam retVal = null;

View File

@ -29,6 +29,9 @@ import java.util.Set;
public interface ISearchParamRegistry {
/**
* Request that the cache be refreshed now, in the current thread
*/
void forceRefresh();
/**
@ -36,13 +39,18 @@ public interface ISearchParamRegistry {
*/
RuntimeSearchParam getActiveSearchParam(String theResourceName, String theParamName);
Map<String, RuntimeSearchParam> getActiveSearchParams(String theResourceName);
Map<String, Map<String, RuntimeSearchParam>> getActiveSearchParams();
List<JpaRuntimeSearchParam> getActiveUniqueSearchParams(String theResourceName);
Map<String, RuntimeSearchParam> getActiveSearchParams(String theResourceName);
List<JpaRuntimeSearchParam> getActiveUniqueSearchParams(String theResourceName, Set<String> theParamNames);
List<JpaRuntimeSearchParam> getActiveUniqueSearchParams(String theResourceName);
void refreshCacheIfNecessary();
/**
* Request that the cache be refreshed at the next convenient time (in a different thread)
*/
void requestRefresh();
}

View File

@ -34,7 +34,6 @@ import org.springframework.transaction.support.TransactionTemplate;
import javax.persistence.EntityManager;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.Callable;

View File

@ -22,7 +22,6 @@ import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.internal.util.collections.ListUtil;
import org.thymeleaf.util.ListUtils;
import java.util.List;
@ -54,93 +53,6 @@ public class FhirResourceDaoDstu2SearchCustomSearchParamTest extends BaseJpaDstu
}
}
@Test
public void testOverrideAndDisableBuiltInSearchParametersWithOverridingEnabled() {
myDaoConfig.setDefaultSearchParamsCanBeOverridden(true);
SearchParameter memberSp = new SearchParameter();
memberSp.setCode("member");
memberSp.setBase(ResourceTypeEnum.GROUP);
memberSp.setType(SearchParamTypeEnum.REFERENCE);
memberSp.setXpath("Group.member.entity");
memberSp.setXpathUsage(XPathUsageTypeEnum.NORMAL);
memberSp.setStatus(ConformanceResourceStatusEnum.RETIRED);
mySearchParameterDao.create(memberSp, mySrd);
SearchParameter identifierSp = new SearchParameter();
identifierSp.setCode("identifier");
identifierSp.setBase(ResourceTypeEnum.GROUP);
identifierSp.setType(SearchParamTypeEnum.TOKEN);
identifierSp.setXpath("Group.identifier");
identifierSp.setXpathUsage(XPathUsageTypeEnum.NORMAL);
identifierSp.setStatus(ConformanceResourceStatusEnum.RETIRED);
mySearchParameterDao.create(identifierSp, mySrd);
mySearchParamRegsitry.forceRefresh();
Patient p = new Patient();
p.addName().addGiven("G");
IIdType pid = myPatientDao.create(p).getId().toUnqualifiedVersionless();
Group g = new Group();
g.addIdentifier().setSystem("urn:foo").setValue("bar");
g.addMember().getEntity().setReference(pid);
myGroupDao.create(g);
assertThat(myResourceLinkDao.findAll(), empty());
assertThat(ListUtil.filter(myResourceIndexedSearchParamTokenDao.findAll(), new ListUtil.Filter<ResourceIndexedSearchParamToken>() {
@Override
public boolean isOut(ResourceIndexedSearchParamToken object) {
return !object.getResourceType().equals("Group") || object.isMissing();
}
}), empty());
}
@Test
public void testOverrideAndDisableBuiltInSearchParametersWithOverridingDisabled() {
myDaoConfig.setDefaultSearchParamsCanBeOverridden(false);
SearchParameter memberSp = new SearchParameter();
memberSp.setCode("member");
memberSp.setBase(ResourceTypeEnum.GROUP);
memberSp.setType(SearchParamTypeEnum.REFERENCE);
memberSp.setXpath("Group.member.entity");
memberSp.setXpathUsage(XPathUsageTypeEnum.NORMAL);
memberSp.setStatus(ConformanceResourceStatusEnum.RETIRED);
mySearchParameterDao.create(memberSp, mySrd);
SearchParameter identifierSp = new SearchParameter();
identifierSp.setCode("identifier");
identifierSp.setBase(ResourceTypeEnum.GROUP);
identifierSp.setType(SearchParamTypeEnum.TOKEN);
identifierSp.setXpath("Group.identifier");
identifierSp.setXpathUsage(XPathUsageTypeEnum.NORMAL);
identifierSp.setStatus(ConformanceResourceStatusEnum.RETIRED);
mySearchParameterDao.create(identifierSp, mySrd);
mySearchParamRegsitry.forceRefresh();
Patient p = new Patient();
p.addName().addGiven("G");
IIdType pid = myPatientDao.create(p).getId().toUnqualifiedVersionless();
Group g = new Group();
g.addIdentifier().setSystem("urn:foo").setValue("bar");
g.addMember().getEntity().setReference(pid);
myGroupDao.create(g);
assertThat(myResourceLinkDao.findAll(), not(empty()));
assertThat(ListUtil.filter(myResourceIndexedSearchParamTokenDao.findAll(), new ListUtil.Filter<ResourceIndexedSearchParamToken>() {
@Override
public boolean isOut(ResourceIndexedSearchParamToken object) {
return !object.getResourceType().equals("Group") || object.isMissing();
}
}), not(empty()));
}
@Test
public void testCreateInvalidParamInvalidResourceName() {
SearchParameter fooSp = new SearchParameter();
@ -228,7 +140,7 @@ public class FhirResourceDaoDstu2SearchCustomSearchParamTest extends BaseJpaDstu
}
@Test
public void testCustomReferenceParameter() throws Exception {
public void testCustomReferenceParameter() {
SearchParameter sp = new SearchParameter();
sp.setBase(ResourceTypeEnum.PATIENT);
sp.setCode("myDoctor");
@ -238,6 +150,8 @@ public class FhirResourceDaoDstu2SearchCustomSearchParamTest extends BaseJpaDstu
sp.setStatus(ConformanceResourceStatusEnum.ACTIVE);
mySearchParameterDao.create(sp);
mySearchParamRegsitry.forceRefresh();
Practitioner pract = new Practitioner();
pract.setId("A");
pract.getName().addFamily("PRACT");
@ -316,6 +230,90 @@ public class FhirResourceDaoDstu2SearchCustomSearchParamTest extends BaseJpaDstu
}
@Test
public void testOverrideAndDisableBuiltInSearchParametersWithOverridingDisabled() {
myDaoConfig.setDefaultSearchParamsCanBeOverridden(false);
SearchParameter memberSp = new SearchParameter();
memberSp.setCode("member");
memberSp.setBase(ResourceTypeEnum.GROUP);
memberSp.setType(SearchParamTypeEnum.REFERENCE);
memberSp.setXpath("Group.member.entity");
memberSp.setXpathUsage(XPathUsageTypeEnum.NORMAL);
memberSp.setStatus(ConformanceResourceStatusEnum.RETIRED);
mySearchParameterDao.create(memberSp, mySrd);
SearchParameter identifierSp = new SearchParameter();
identifierSp.setCode("identifier");
identifierSp.setBase(ResourceTypeEnum.GROUP);
identifierSp.setType(SearchParamTypeEnum.TOKEN);
identifierSp.setXpath("Group.identifier");
identifierSp.setXpathUsage(XPathUsageTypeEnum.NORMAL);
identifierSp.setStatus(ConformanceResourceStatusEnum.RETIRED);
mySearchParameterDao.create(identifierSp, mySrd);
mySearchParamRegsitry.forceRefresh();
Patient p = new Patient();
p.addName().addGiven("G");
IIdType pid = myPatientDao.create(p).getId().toUnqualifiedVersionless();
Group g = new Group();
g.addIdentifier().setSystem("urn:foo").setValue("bar");
g.addMember().getEntity().setReference(pid);
myGroupDao.create(g);
assertThat(myResourceLinkDao.findAll(), not(empty()));
assertThat(ListUtil.filter(myResourceIndexedSearchParamTokenDao.findAll(), new ListUtil.Filter<ResourceIndexedSearchParamToken>() {
@Override
public boolean isOut(ResourceIndexedSearchParamToken object) {
return !object.getResourceType().equals("Group") || object.isMissing();
}
}), not(empty()));
}
@Test
public void testOverrideAndDisableBuiltInSearchParametersWithOverridingEnabled() {
myDaoConfig.setDefaultSearchParamsCanBeOverridden(true);
SearchParameter memberSp = new SearchParameter();
memberSp.setCode("member");
memberSp.setBase(ResourceTypeEnum.GROUP);
memberSp.setType(SearchParamTypeEnum.REFERENCE);
memberSp.setXpath("Group.member.entity");
memberSp.setXpathUsage(XPathUsageTypeEnum.NORMAL);
memberSp.setStatus(ConformanceResourceStatusEnum.RETIRED);
mySearchParameterDao.create(memberSp, mySrd);
SearchParameter identifierSp = new SearchParameter();
identifierSp.setCode("identifier");
identifierSp.setBase(ResourceTypeEnum.GROUP);
identifierSp.setType(SearchParamTypeEnum.TOKEN);
identifierSp.setXpath("Group.identifier");
identifierSp.setXpathUsage(XPathUsageTypeEnum.NORMAL);
identifierSp.setStatus(ConformanceResourceStatusEnum.RETIRED);
mySearchParameterDao.create(identifierSp, mySrd);
mySearchParamRegsitry.forceRefresh();
Patient p = new Patient();
p.addName().addGiven("G");
IIdType pid = myPatientDao.create(p).getId().toUnqualifiedVersionless();
Group g = new Group();
g.addIdentifier().setSystem("urn:foo").setValue("bar");
g.addMember().getEntity().setReference(pid);
myGroupDao.create(g);
assertThat(myResourceLinkDao.findAll(), empty());
assertThat(ListUtil.filter(myResourceIndexedSearchParamTokenDao.findAll(), new ListUtil.Filter<ResourceIndexedSearchParamToken>() {
@Override
public boolean isOut(ResourceIndexedSearchParamToken object) {
return !object.getResourceType().equals("Group") || object.isMissing();
}
}), empty());
}
@Test
public void testSearchForExtensionReferenceWithNonMatchingTarget() {
SearchParameter siblingSp = new SearchParameter();

View File

@ -29,56 +29,56 @@ public class SearchParamExtractorDstu3Test {
private static FhirContext ourCtx = FhirContext.forDstu3();
private static IValidationSupport ourValidationSupport;
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
public static void beforeClass() {
ourValidationSupport = new DefaultProfileValidationSupport();
}
@Test
public void testParamWithOrInPath() {
Observation obs = new Observation();
obs.addCategory().addCoding().setSystem("SYSTEM").setCode("CODE");
ISearchParamRegistry searchParamRegistry = new ISearchParamRegistry() {
@Override
public Map<String,RuntimeSearchParam> getActiveSearchParams(String theResourceName) {
RuntimeResourceDefinition nextResDef = ourCtx.getResourceDefinition(theResourceName);
Map<String, RuntimeSearchParam> sps = new HashMap<String, RuntimeSearchParam>();
for (RuntimeSearchParam nextSp : nextResDef.getSearchParams()) {
sps.put(nextSp.getName(), nextSp);
}
return sps;
}
@Override
public List<JpaRuntimeSearchParam> getActiveUniqueSearchParams(String theResourceName) {
throw new UnsupportedOperationException();
}
@Override
public List<JpaRuntimeSearchParam> getActiveUniqueSearchParams(String theResourceName, Set<String> theParamNames) {
throw new UnsupportedOperationException();
}
@Override
public void forceRefresh() {
// nothing
}
@Override
public RuntimeSearchParam getActiveSearchParam(String theResourceName, String theParamName) {
throw new UnsupportedOperationException();
}
@Override
public Map<String, Map<String, RuntimeSearchParam>> getActiveSearchParams() {
throw new UnsupportedOperationException();
}
@Override
public RuntimeSearchParam getActiveSearchParam(String theResourceName, String theParamName) {
public Map<String,RuntimeSearchParam> getActiveSearchParams(String theResourceName) {
RuntimeResourceDefinition nextResDef = ourCtx.getResourceDefinition(theResourceName);
Map<String, RuntimeSearchParam> sps = new HashMap<>();
for (RuntimeSearchParam nextSp : nextResDef.getSearchParams()) {
sps.put(nextSp.getName(), nextSp);
}
return sps;
}
@Override
public List<JpaRuntimeSearchParam> getActiveUniqueSearchParams(String theResourceName, Set<String> theParamNames) {
throw new UnsupportedOperationException();
}
@Override
public List<JpaRuntimeSearchParam> getActiveUniqueSearchParams(String theResourceName) {
throw new UnsupportedOperationException();
}
@Override
public void refreshCacheIfNecessary() {
// nothing
}
@Override
public void requestRefresh() {
// nothing
}
};
SearchParamExtractorDstu3 extractor = new SearchParamExtractorDstu3(ourCtx, ourValidationSupport, searchParamRegistry);
@ -90,4 +90,14 @@ public class SearchParamExtractorDstu3Test {
assertEquals("CODE", token.getValue());
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
public static void beforeClass() {
ourValidationSupport = new DefaultProfileValidationSupport();
}
}

View File

@ -156,6 +156,8 @@ public class FhirResourceDaoR4SearchCustomSearchParamTest extends BaseJpaR4Test
sp.setStatus(org.hl7.fhir.r4.model.Enumerations.PublicationStatus.ACTIVE);
mySearchParameterDao.create(sp);
mySearchParamRegsitry.forceRefresh();
org.hl7.fhir.r4.model.Practitioner pract = new org.hl7.fhir.r4.model.Practitioner();
pract.setId("A");
pract.addName().setFamily("PRACT");

View File

@ -1,75 +1,83 @@
package ca.uhn.fhir.jpa.dao.r4;
import static org.junit.Assert.assertEquals;
import java.util.*;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.RuntimeResourceDefinition;
import ca.uhn.fhir.context.RuntimeSearchParam;
import ca.uhn.fhir.jpa.dao.ISearchParamRegistry;
import ca.uhn.fhir.jpa.entity.BaseResourceIndexedSearchParam;
import ca.uhn.fhir.jpa.entity.ResourceIndexedSearchParamToken;
import ca.uhn.fhir.jpa.entity.ResourceTable;
import ca.uhn.fhir.jpa.search.JpaRuntimeSearchParam;
import ca.uhn.fhir.util.TestUtil;
import org.hl7.fhir.r4.hapi.ctx.DefaultProfileValidationSupport;
import org.hl7.fhir.r4.hapi.ctx.IValidationSupport;
import org.hl7.fhir.r4.model.Observation;
import org.junit.*;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import ca.uhn.fhir.context.*;
import ca.uhn.fhir.jpa.dao.ISearchParamRegistry;
import ca.uhn.fhir.jpa.entity.*;
import ca.uhn.fhir.util.TestUtil;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.junit.Assert.*;
public class SearchParamExtractorR4Test {
private static FhirContext ourCtx = FhirContext.forR4();
private static IValidationSupport ourValidationSupport;
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
public static void beforeClass() {
ourValidationSupport = new DefaultProfileValidationSupport();
}
@Test
public void testParamWithOrInPath() {
Observation obs = new Observation();
obs.addCategory().addCoding().setSystem("SYSTEM").setCode("CODE");
ISearchParamRegistry searchParamRegistry = new ISearchParamRegistry() {
@Override
public Map<String,RuntimeSearchParam> getActiveSearchParams(String theResourceName) {
RuntimeResourceDefinition nextResDef = ourCtx.getResourceDefinition(theResourceName);
Map<String, RuntimeSearchParam> sps = new HashMap<String, RuntimeSearchParam>();
for (RuntimeSearchParam nextSp : nextResDef.getSearchParams()) {
sps.put(nextSp.getName(), nextSp);
}
return sps;
}
@Override
public List<JpaRuntimeSearchParam> getActiveUniqueSearchParams(String theResourceName) {
throw new UnsupportedOperationException();
}
@Override
public List<JpaRuntimeSearchParam> getActiveUniqueSearchParams(String theResourceName, Set<String> theParamNames) {
throw new UnsupportedOperationException();
}
@Override
public void forceRefresh() {
// nothing
}
@Override
public RuntimeSearchParam getActiveSearchParam(String theResourceName, String theParamName) {
throw new UnsupportedOperationException();
}
@Override
public Map<String, Map<String, RuntimeSearchParam>> getActiveSearchParams() {
throw new UnsupportedOperationException();
}
@Override
public RuntimeSearchParam getActiveSearchParam(String theResourceName, String theParamName) {
public Map<String, RuntimeSearchParam> getActiveSearchParams(String theResourceName) {
RuntimeResourceDefinition nextResDef = ourCtx.getResourceDefinition(theResourceName);
Map<String, RuntimeSearchParam> sps = new HashMap<>();
for (RuntimeSearchParam nextSp : nextResDef.getSearchParams()) {
sps.put(nextSp.getName(), nextSp);
}
return sps;
}
@Override
public List<JpaRuntimeSearchParam> getActiveUniqueSearchParams(String theResourceName, Set<String> theParamNames) {
throw new UnsupportedOperationException();
}
@Override
public List<JpaRuntimeSearchParam> getActiveUniqueSearchParams(String theResourceName) {
throw new UnsupportedOperationException();
}
@Override
public void refreshCacheIfNecessary() {
// nothing
}
@Override
public void requestRefresh() {
// nothing
}
};
SearchParamExtractorR4 extractor = new SearchParamExtractorR4(ourCtx, ourValidationSupport, searchParamRegistry);
@ -81,4 +89,14 @@ public class SearchParamExtractorR4Test {
assertEquals("CODE", token.getValue());
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
public static void beforeClass() {
ourValidationSupport = new DefaultProfileValidationSupport();
}
}

View File

@ -306,7 +306,7 @@ public class ResourceProviderCustomSearchParamDstu3Test extends BaseResourceProv
.resource(eyeColourSp)
.execute();
// mySearchParamRegsitry.forceRefresh();
mySearchParamRegsitry.forceRefresh();
Patient p1 = new Patient();
p1.setActive(true);

View File

@ -306,7 +306,7 @@ public class ResourceProviderCustomSearchParamR4Test extends BaseResourceProvide
.resource(eyeColourSp)
.execute();
// mySearchParamRegsitry.forceRefresh();
mySearchParamRegsitry.forceRefresh();
Patient p1 = new Patient();
p1.setActive(true);