From fdcf5f5d73c02188cb35e456d27e1f34ef63e544 Mon Sep 17 00:00:00 2001 From: James Agnew Date: Tue, 15 Oct 2019 17:58:06 -0400 Subject: [PATCH] Add a test --- .../dao/r4/SearchParamExtractorR4Test.java | 3 +- .../registry/BaseSearchParamRegistry.java | 42 ++++++++----- .../registry/ISearchParamRegistry.java | 2 +- .../SearchParamExtractorDstu3Test.java | 4 +- .../registry/BaseSearchParamRegistryTest.java | 62 +++++++++++++++++++ 5 files changed, 93 insertions(+), 20 deletions(-) create mode 100644 hapi-fhir-jpaserver-searchparam/src/test/java/ca/uhn/fhir/jpa/searchparam/registry/BaseSearchParamRegistryTest.java diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/SearchParamExtractorR4Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/SearchParamExtractorR4Test.java index 7a3b3e00ab1..c9fb6c42ff9 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/SearchParamExtractorR4Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/SearchParamExtractorR4Test.java @@ -49,8 +49,9 @@ public class SearchParamExtractorR4Test { } @Override - public void refreshCacheIfNecessary() { + public boolean refreshCacheIfNecessary() { // nothing + return false; } @Override diff --git a/hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/registry/BaseSearchParamRegistry.java b/hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/registry/BaseSearchParamRegistry.java index e6669c6f49e..76fce440806 100644 --- a/hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/registry/BaseSearchParamRegistry.java +++ b/hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/registry/BaseSearchParamRegistry.java @@ -93,7 +93,7 @@ public abstract class BaseSearchParamRegistry implemen @Override public Map getActiveSearchParams(String theResourceName) { requiresActiveSearchParams(); - return myActiveSearchParams.get(theResourceName); + return getActiveSearchParams().get(theResourceName); } private void requiresActiveSearchParams() { @@ -207,12 +207,8 @@ public abstract class BaseSearchParamRegistry implemen if (next.getCompositeOf() != null) { next.getCompositeOf().sort((theO1, theO2) -> StringUtils.compare(theO1.getName(), theO2.getName())); for (String nextBase : next.getBase()) { - if (!activeParamNamesToUniqueSearchParams.containsKey(nextBase)) { - activeParamNamesToUniqueSearchParams.put(nextBase, new HashMap<>()); - } - if (!activeParamNamesToUniqueSearchParams.get(nextBase).containsKey(paramNames)) { - activeParamNamesToUniqueSearchParams.get(nextBase).put(paramNames, new ArrayList<>()); - } + activeParamNamesToUniqueSearchParams.computeIfAbsent(nextBase, v -> new HashMap<>()); + activeParamNamesToUniqueSearchParams.get(nextBase).computeIfAbsent(paramNames, t -> new ArrayList<>()); activeParamNamesToUniqueSearchParams.get(nextBase).get(paramNames).add(next); } } @@ -224,11 +220,21 @@ public abstract class BaseSearchParamRegistry implemen myActiveParamNamesToUniqueSearchParams = activeParamNamesToUniqueSearchParams; } + @VisibleForTesting + void setFhirContextForUnitTest(FhirContext theFhirContext) { + myFhirContext = theFhirContext; + } + @PostConstruct public void postConstruct() { myBuiltInSearchParams = createBuiltInSearchParamMap(myFhirContext); } + @VisibleForTesting + void setSearchParamProviderForUnitTest(ISearchParamProvider theSearchParamProvider) { + mySearchParamProvider = theSearchParamProvider; + } + public int doRefresh(long theRefreshInterval) { if (System.currentTimeMillis() - theRefreshInterval > myLastRefresh) { StopWatch sw = new StopWatch(); @@ -314,8 +320,10 @@ public abstract class BaseSearchParamRegistry implemen myLastRefresh = System.currentTimeMillis(); ourLog.info("Refreshed search parameter cache in {}ms", sw.getMillis()); + return myActiveSearchParams.size(); + } else { + return 0; } - return myActiveSearchParams.size(); } protected abstract RuntimeSearchParam toRuntimeSp(SP theNextSp); @@ -344,11 +352,6 @@ public abstract class BaseSearchParamRegistry implemen refreshCacheWithRetry(); } - @VisibleForTesting - public void setSearchParamProviderForUnitTest(ISearchParamProvider theSearchParamProvider) { - mySearchParamProvider = theSearchParamProvider; - } - int refreshCacheWithRetry() { Retrier refreshCacheRetrier = new Retrier<>(() -> { synchronized (BaseSearchParamRegistry.this) { @@ -367,10 +370,12 @@ public abstract class BaseSearchParamRegistry implemen } @Override - public void refreshCacheIfNecessary() { - if (myActiveSearchParams == null || - System.currentTimeMillis() - REFRESH_INTERVAL > myLastRefresh) { + public boolean refreshCacheIfNecessary() { + if (myActiveSearchParams == null || System.currentTimeMillis() - REFRESH_INTERVAL > myLastRefresh) { refreshCacheWithRetry(); + return true; + } else { + return false; } } @@ -380,6 +385,11 @@ public abstract class BaseSearchParamRegistry implemen return Collections.unmodifiableMap(myActiveSearchParams); } + @VisibleForTesting + void setSchedulerServiceForUnitTest(ISchedulerService theSchedulerService) { + mySchedulerService = theSchedulerService; + } + public static class SubmitJob implements Job { @Autowired private ISearchParamRegistry myTarget; diff --git a/hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/registry/ISearchParamRegistry.java b/hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/registry/ISearchParamRegistry.java index c927186ffda..064bbc4d936 100644 --- a/hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/registry/ISearchParamRegistry.java +++ b/hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/registry/ISearchParamRegistry.java @@ -41,7 +41,7 @@ public interface ISearchParamRegistry { */ RuntimeSearchParam getActiveSearchParam(String theResourceName, String theParamName); - void refreshCacheIfNecessary(); + boolean refreshCacheIfNecessary(); Map> getActiveSearchParams(); diff --git a/hapi-fhir-jpaserver-searchparam/src/test/java/ca/uhn/fhir/jpa/searchparam/extractor/SearchParamExtractorDstu3Test.java b/hapi-fhir-jpaserver-searchparam/src/test/java/ca/uhn/fhir/jpa/searchparam/extractor/SearchParamExtractorDstu3Test.java index 7309fe39e80..af4793b6ec3 100644 --- a/hapi-fhir-jpaserver-searchparam/src/test/java/ca/uhn/fhir/jpa/searchparam/extractor/SearchParamExtractorDstu3Test.java +++ b/hapi-fhir-jpaserver-searchparam/src/test/java/ca/uhn/fhir/jpa/searchparam/extractor/SearchParamExtractorDstu3Test.java @@ -8,7 +8,6 @@ import ca.uhn.fhir.jpa.model.entity.ModelConfig; import ca.uhn.fhir.jpa.model.entity.ResourceIndexedSearchParamToken; import ca.uhn.fhir.jpa.model.entity.ResourceTable; import ca.uhn.fhir.jpa.searchparam.JpaRuntimeSearchParam; -import ca.uhn.fhir.jpa.searchparam.extractor.SearchParamExtractorDstu3; import ca.uhn.fhir.jpa.searchparam.registry.ISearchParamRegistry; import ca.uhn.fhir.util.TestUtil; import org.hl7.fhir.dstu3.hapi.ctx.DefaultProfileValidationSupport; @@ -44,8 +43,9 @@ public class SearchParamExtractorDstu3Test { } @Override - public void refreshCacheIfNecessary() { + public boolean refreshCacheIfNecessary() { // nothing + return false; } @Override diff --git a/hapi-fhir-jpaserver-searchparam/src/test/java/ca/uhn/fhir/jpa/searchparam/registry/BaseSearchParamRegistryTest.java b/hapi-fhir-jpaserver-searchparam/src/test/java/ca/uhn/fhir/jpa/searchparam/registry/BaseSearchParamRegistryTest.java new file mode 100644 index 00000000000..8d9e440ab3c --- /dev/null +++ b/hapi-fhir-jpaserver-searchparam/src/test/java/ca/uhn/fhir/jpa/searchparam/registry/BaseSearchParamRegistryTest.java @@ -0,0 +1,62 @@ +package ca.uhn.fhir.jpa.searchparam.registry; + +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.jpa.model.sched.ISchedulerService; +import ca.uhn.fhir.rest.server.SimpleBundleProvider; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +import static org.junit.Assert.*; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.*; + +@RunWith(MockitoJUnitRunner.class) +public class BaseSearchParamRegistryTest { + + @Mock + private ISchedulerService mySchedulerService; + @Mock + private ISearchParamProvider mySearchParamProvider; + + @Test + public void testRefreshAfterExpiry() { + when(mySearchParamProvider.search(any())).thenReturn(new SimpleBundleProvider()); + + SearchParamRegistryR4 registry = new SearchParamRegistryR4(); + registry.setSchedulerServiceForUnitTest(mySchedulerService); + registry.setFhirContextForUnitTest(FhirContext.forR4()); + registry.setSearchParamProviderForUnitTest(mySearchParamProvider); + registry.postConstruct(); + + registry.requestRefresh(); + assertEquals(146, registry.doRefresh(100000)); + + // Second time we don't need to run because we ran recently + assertEquals(0, registry.doRefresh(100000)); + + assertEquals(146, registry.getActiveSearchParams().size()); + } + + @Test + public void testRefreshCacheIfNeccessary() { + SearchParamRegistryR4 registry = new SearchParamRegistryR4(); + + when(mySearchParamProvider.search(any())).thenReturn(new SimpleBundleProvider()); + when(mySearchParamProvider.refreshCache(any(), anyLong())).thenAnswer(t->{ + registry.doRefresh(t.getArgument(1, Long.class)); + return 0; + }); + + registry.setSchedulerServiceForUnitTest(mySchedulerService); + registry.setFhirContextForUnitTest(FhirContext.forR4()); + registry.setSearchParamProviderForUnitTest(mySearchParamProvider); + registry.postConstruct(); + registry.requestRefresh(); + + assertTrue(registry.refreshCacheIfNecessary()); + assertFalse(registry.refreshCacheIfNecessary()); + } + +}