Skip to content

Commit

Permalink
Add JNDI value to enable/disable event catchup
Browse files Browse the repository at this point in the history
  • Loading branch information
amckenzie committed Jan 22, 2019
1 parent 491a408 commit 16431da
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ on [Keep a CHANGELOG](http://keepachangelog.com/). This project adheres to

## [Unreleased]

## [1.1.2] - 2019-01-22
### Added
- JNDI configuration variable to enable/disable Event Catchup


## [1.1.1] - 2019-01-15
### Added
- Event Catchup on startup, where all unknown events are retrieved from the EventSource and played
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package uk.gov.justice.services.event.sourcing.subscription.startup;

import static java.lang.Boolean.valueOf;

import uk.gov.justice.services.common.configuration.Value;

import javax.inject.Inject;

public class EventCatchupConfig {

@Inject
@Value(key = "event.catchup.enabled", defaultValue = "false")
String eventCatchupEnabled;

public boolean isEventCatchupEnabled() {
return valueOf(eventCatchupEnabled);
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,42 @@ public class EventCatchupStartUpBean {
@Resource
ManagedExecutorService managedExecutorService;

@Inject
EventCatchupConfig eventCatchupConfig;

@Inject
Logger logger;

@PostConstruct
public void start() {

if(eventCatchupConfig.isEventCatchupEnabled()) {
runEventCatchup();
} else {
logger.info("Not performing event Event Catchup: Event catchup disabled");
}
}

private void runEventCatchup() {
logger.info("SubscriptionManagerStartUp started");
final Set<SubscriptionsDescriptor> subscriptionsDescriptors =
subscriptionsDescriptorsRegistry.getAll();

subscriptionsDescriptors.forEach(this::startSubscriptions);
subscriptionsDescriptors.forEach(this::runEventCatchupForSubscription);
}

private void startSubscriptions(final SubscriptionsDescriptor subscriptionsDescriptor) {
private void runEventCatchupForSubscription(final SubscriptionsDescriptor subscriptionsDescriptor) {

final String componentName = subscriptionsDescriptor.getServiceComponent();

if (componentName.contains(EVENT_LISTENER)) {
subscriptionsDescriptor
.getSubscriptions()
.forEach(subscription -> startSubscription(componentName, subscription));
.forEach(subscription -> runEventCatchupForComponent(componentName, subscription));
}
}

private void startSubscription(final String componentName, final Subscription subscription) {
private void runEventCatchupForComponent(final String componentName, final Subscription subscription) {

final EventCatchupTask eventCatchupTask = new EventCatchupTask(
componentName,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package uk.gov.justice.services.event.sourcing.subscription.startup;

import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import org.junit.Test;
import org.mockito.InjectMocks;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;


@RunWith(MockitoJUnitRunner.class)
public class EventCatchupConfigTest {

@InjectMocks
private EventCatchupConfig eventCatchupConfig;

@Test
public void shouldReturnTrueIfEventCatchupSetToTrue() throws Exception {

eventCatchupConfig.eventCatchupEnabled = "true";

assertThat(eventCatchupConfig.isEventCatchupEnabled(), is(true));
}

@Test
public void shouldReturnFalseIfEventCatchupSetToFalse() throws Exception {

eventCatchupConfig.eventCatchupEnabled = "false";

assertThat(eventCatchupConfig.isEventCatchupEnabled(), is(false));
}

@Test
public void shouldReturnFalseIfEventCatchupSetToSometingRandom() throws Exception {

eventCatchupConfig.eventCatchupEnabled = "something silly";

assertThat(eventCatchupConfig.isEventCatchupEnabled(), is(false));
}

@Test
public void shouldBeCaseInsensitive() throws Exception {

eventCatchupConfig.eventCatchupEnabled = "TrUe";

assertThat(eventCatchupConfig.isEventCatchupEnabled(), is(true));
}

@Test
public void shouldBeFalseIfNull() throws Exception {

eventCatchupConfig.eventCatchupEnabled = null;

assertThat(eventCatchupConfig.isEventCatchupEnabled(), is(false));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;

import uk.gov.justice.subscription.domain.subscriptiondescriptor.Subscription;
Expand Down Expand Up @@ -34,6 +35,9 @@ public class EventCatchupStartUpBeanTest {
@Mock
private ManagedExecutorService managedExecutorService;

@Mock
private EventCatchupConfig eventCatchupConfig;

@Mock
private Logger logger;

Expand Down Expand Up @@ -62,6 +66,7 @@ public void shouldPerformEventCatchupForEventListenerSubscriptions() {
subscriptionsDescriptor_2,
subscriptionsDescriptor_3);

when(eventCatchupConfig.isEventCatchupEnabled()).thenReturn(true);
when(subscriptionsDescriptorsRegistry.getAll()).thenReturn(subscriptionsDescriptors);

when(subscriptionsDescriptor_1.getServiceComponent()).thenReturn(componentName_1);
Expand All @@ -80,6 +85,17 @@ public void shouldPerformEventCatchupForEventListenerSubscriptions() {

verify(subscriptionsDescriptor_3).getServiceComponent();
verifyNoMoreInteractions(subscriptionsDescriptor_3);
}

@Test
public void shouldNotPerformCatchupIfDisabled() throws Exception {

when(eventCatchupConfig.isEventCatchupEnabled()).thenReturn(false);

eventCatchupStartUpBean.start();

verify(logger).info("Not performing event Event Catchup: Event catchup disabled");
verifyZeroInteractions(subscriptionsDescriptorsRegistry);
verifyZeroInteractions(managedExecutorService);
}
}

0 comments on commit 16431da

Please sign in to comment.