-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
303 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...in/java/uk/gov/justice/services/core/featurecontrol/remote/FeatureRefreshTimerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package uk.gov.justice.services.core.featurecontrol.remote; | ||
|
||
import static java.lang.Boolean.parseBoolean; | ||
import static java.lang.Long.parseLong; | ||
|
||
import uk.gov.justice.services.common.configuration.GlobalValue; | ||
|
||
import javax.inject.Inject; | ||
|
||
public class FeatureRefreshTimerConfig { | ||
|
||
private final String TEN_MINUTES_IN_MILLISECONDS = 10 * 60 * 1000 + ""; | ||
|
||
@Inject | ||
@GlobalValue(key = "feature-refresh-rate.timer.start.wait.milliseconds", defaultValue = "0") | ||
private String timerStartWaitMilliseconds; | ||
|
||
@Inject | ||
@GlobalValue(key = "feature-refresh-rate.timer.interval.milliseconds", defaultValue = TEN_MINUTES_IN_MILLISECONDS) | ||
private String timerIntervalMilliseconds; | ||
|
||
public long getTimerStartWaitMilliseconds() { | ||
return parseLong(timerStartWaitMilliseconds); | ||
} | ||
|
||
public long getTimerIntervalMilliseconds() { | ||
return parseLong(timerIntervalMilliseconds); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...c/main/java/uk/gov/justice/services/core/featurecontrol/remote/FeatureStoreTimerBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package uk.gov.justice.services.core.featurecontrol.remote; | ||
|
||
import static java.util.Optional.ofNullable; | ||
|
||
import uk.gov.justice.services.core.featurecontrol.FeatureFetcher; | ||
import uk.gov.justice.services.core.featurecontrol.domain.Feature; | ||
import uk.gov.justice.services.ejb.timer.TimerServiceManager; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import javax.annotation.PostConstruct; | ||
import javax.annotation.Resource; | ||
import javax.ejb.Singleton; | ||
import javax.ejb.Startup; | ||
import javax.ejb.Timeout; | ||
import javax.ejb.TimerService; | ||
import javax.inject.Inject; | ||
|
||
@Singleton | ||
@Startup | ||
public class FeatureStoreTimerBean { | ||
|
||
private static final String TIMER_JOB_NAME = "framework.feature-store-refresh.job"; | ||
|
||
private AtomicReference<Map<String, Feature>> atomicFeatureMapReference = new AtomicReference<>(new HashMap<>()); | ||
|
||
@Inject | ||
private FeatureFetcher featureFetcher; | ||
|
||
@Inject | ||
private TimerServiceManager timerServiceManager; | ||
|
||
@Resource | ||
private TimerService timerService; | ||
|
||
@Inject | ||
private FeatureRefreshTimerConfig featureRefreshTimerConfig; | ||
|
||
@PostConstruct | ||
public void startTimerService() { | ||
|
||
timerServiceManager.createIntervalTimer( | ||
TIMER_JOB_NAME, | ||
featureRefreshTimerConfig.getTimerStartWaitMilliseconds(), | ||
featureRefreshTimerConfig.getTimerIntervalMilliseconds(), | ||
timerService); | ||
} | ||
|
||
@Timeout | ||
public void reloadFeatures() { | ||
|
||
final Map<String, Feature> featureMap = new HashMap<>(); | ||
|
||
featureFetcher | ||
.fetchFeatures() | ||
.forEach(feature -> featureMap.put(feature.getFeatureName(), feature)); | ||
|
||
atomicFeatureMapReference.set(featureMap); | ||
} | ||
|
||
public Optional<Feature> lookup(final String featureName) { | ||
return ofNullable(atomicFeatureMapReference.get().get(featureName)); | ||
} | ||
} |
44 changes: 0 additions & 44 deletions
44
core/src/test/java/uk/gov/justice/services/core/featurecontrol/DefaultFeatureStoreTest.java
This file was deleted.
Oops, something went wrong.
72 changes: 72 additions & 0 deletions
72
core/src/test/java/uk/gov/justice/services/core/featurecontrol/FeatureStoreFacadeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package uk.gov.justice.services.core.featurecontrol; | ||
|
||
import static java.util.Optional.empty; | ||
import static java.util.Optional.of; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verifyZeroInteractions; | ||
import static org.mockito.Mockito.when; | ||
|
||
import uk.gov.justice.services.core.featurecontrol.domain.Feature; | ||
import uk.gov.justice.services.core.featurecontrol.local.LocalFeatureStore; | ||
import uk.gov.justice.services.core.featurecontrol.remote.FeatureStoreTimerBean; | ||
|
||
import java.util.Optional; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class FeatureStoreFacadeTest { | ||
|
||
@Mock | ||
private LocalFeatureStore localFeatureStore; | ||
|
||
@Mock | ||
private FeatureStoreTimerBean featureStoreTimerBean; | ||
|
||
@InjectMocks | ||
private FeatureStoreFacade featureStoreFacade; | ||
|
||
@Test | ||
public void shouldLookupFeaturesInTheFeatureStoreTimerBean() throws Exception { | ||
|
||
final String featureName = "some-feature"; | ||
|
||
final Feature feature = mock(Feature.class); | ||
|
||
when(localFeatureStore.lookup(featureName)).thenReturn(empty()); | ||
when(featureStoreTimerBean.lookup(featureName)).thenReturn(of(feature)); | ||
|
||
assertThat(featureStoreFacade.lookup(featureName), is(of(feature))); | ||
} | ||
|
||
@Test | ||
public void shouldReturnEmptyIfTheFeatureCannotBeFound() throws Exception { | ||
|
||
final String unknownFeatureName = "some-unknown-feature"; | ||
|
||
when(localFeatureStore.lookup(unknownFeatureName)).thenReturn(empty()); | ||
when(featureStoreTimerBean.lookup(unknownFeatureName)).thenReturn(empty()); | ||
|
||
assertThat(featureStoreFacade.lookup(unknownFeatureName).isPresent(), is(false)); | ||
} | ||
|
||
@Test | ||
public void shouldOverrideTheFeatureWithALocalOneIfFound() throws Exception { | ||
|
||
final String featureName = "some-feature"; | ||
|
||
final Feature feature = mock(Feature.class); | ||
|
||
when(localFeatureStore.lookup(featureName)).thenReturn(of(feature)); | ||
|
||
assertThat(featureStoreFacade.lookup(featureName), is(of(feature))); | ||
|
||
verifyZeroInteractions(featureStoreTimerBean); | ||
} | ||
} |
3 changes: 1 addition & 2 deletions
3
...test/java/uk/gov/justice/services/core/featurecontrol/remote/DummyFeatureFetcherTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...ava/uk/gov/justice/services/core/featurecontrol/remote/FeatureRefreshTimerConfigTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package uk.gov.justice.services.core.featurecontrol.remote; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static uk.gov.justice.services.test.utils.core.reflection.ReflectionUtil.setField; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class FeatureRefreshTimerConfigTest { | ||
|
||
@InjectMocks | ||
private FeatureRefreshTimerConfig featureRefreshTimerConfig; | ||
|
||
@Test | ||
public void shouldGetTheFeatureRefreshTimerStartWaitInMilliseconds() throws Exception { | ||
|
||
final long timerStartWait = 98234L; | ||
|
||
setField(featureRefreshTimerConfig, "timerStartWaitMilliseconds", timerStartWait + ""); | ||
|
||
assertThat(featureRefreshTimerConfig.getTimerStartWaitMilliseconds(), is(timerStartWait)); | ||
} | ||
|
||
@Test | ||
public void shouldGetTheFeatureRefreshTimerIntervalInMilliseconds() throws Exception { | ||
|
||
final long timerInterval = 98234L; | ||
|
||
setField(featureRefreshTimerConfig, "timerIntervalMilliseconds", timerInterval + ""); | ||
|
||
assertThat(featureRefreshTimerConfig.getTimerIntervalMilliseconds(), is(timerInterval)); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
...st/java/uk/gov/justice/services/core/featurecontrol/remote/FeatureStoreTimerBeanTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package uk.gov.justice.services.core.featurecontrol.remote; | ||
|
||
import static java.util.Arrays.asList; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import uk.gov.justice.services.core.featurecontrol.FeatureFetcher; | ||
import uk.gov.justice.services.core.featurecontrol.domain.Feature; | ||
import uk.gov.justice.services.ejb.timer.TimerServiceManager; | ||
|
||
import javax.ejb.TimerService; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class FeatureStoreTimerBeanTest { | ||
|
||
@Mock | ||
private FeatureFetcher featureFetcher; | ||
|
||
@Mock | ||
private TimerServiceManager timerServiceManager; | ||
|
||
@Mock | ||
private TimerService timerService; | ||
|
||
@Mock | ||
private FeatureRefreshTimerConfig featureRefreshTimerConfig; | ||
|
||
@InjectMocks | ||
private FeatureStoreTimerBean featureStoreTimerBean; | ||
|
||
@Test | ||
public void shouldStartTheTimerServiceOnStartup() throws Exception { | ||
|
||
final long timerStartWaitMilliseconds = 934L; | ||
final long timerIntervalMilliseconds = 987234L; | ||
|
||
when(featureRefreshTimerConfig.getTimerStartWaitMilliseconds()).thenReturn(timerStartWaitMilliseconds); | ||
when(featureRefreshTimerConfig.getTimerIntervalMilliseconds()).thenReturn(timerIntervalMilliseconds); | ||
|
||
featureStoreTimerBean.startTimerService(); | ||
|
||
verify(timerServiceManager).createIntervalTimer( | ||
"framework.feature-store-refresh.job", | ||
timerStartWaitMilliseconds, | ||
timerIntervalMilliseconds, | ||
timerService); | ||
} | ||
|
||
@Test | ||
public void shouldFetchFeaturesAndStore() throws Exception { | ||
|
||
final Feature feature_1 = new Feature("some-feature-1", "some-description-1", true); | ||
final Feature feature_2 = new Feature("some-feature-2", "some-description-2", true); | ||
final Feature feature_3 = new Feature("some-feature-3", "some-description-3", true); | ||
|
||
assertThat(featureStoreTimerBean.lookup(feature_1.getFeatureName()).isPresent(), is(false)); | ||
assertThat(featureStoreTimerBean.lookup(feature_2.getFeatureName()).isPresent(), is(false)); | ||
assertThat(featureStoreTimerBean.lookup(feature_3.getFeatureName()).isPresent(), is(false)); | ||
|
||
when(featureFetcher.fetchFeatures()).thenReturn(asList(feature_1, feature_2, feature_3)); | ||
featureStoreTimerBean.reloadFeatures(); | ||
|
||
final Feature foundFeature_1 = featureStoreTimerBean.lookup(feature_1.getFeatureName()) | ||
.orElseThrow(AssertionError::new); | ||
final Feature foundFeature_2 = featureStoreTimerBean.lookup(feature_2.getFeatureName()) | ||
.orElseThrow(AssertionError::new); | ||
final Feature foundFeature_3 = featureStoreTimerBean.lookup(feature_3.getFeatureName()) | ||
.orElseThrow(AssertionError::new); | ||
|
||
assertThat(foundFeature_1, is(feature_1)); | ||
assertThat(foundFeature_2, is(feature_2)); | ||
assertThat(foundFeature_3, is(feature_3)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters