Skip to content

Commit

Permalink
Merge be6e0a1 into 6d2ed46
Browse files Browse the repository at this point in the history
  • Loading branch information
allanmckenzie committed Oct 23, 2019
2 parents 6d2ed46 + be6e0a1 commit 74680d5
Show file tree
Hide file tree
Showing 13 changed files with 366 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ the running of PrePublisherTimerBean and PublisherTimerBean respectively
- Error message of event linking verification now gives more accurate error messages
based on whether the problem is in published_event or processed_event
- Catchup now runs verification on completion and will fail catchup if the verification fails
- New SystemCommands EnablePublishingCommand and DisablePublishingCommand for enabling/disabling the publishing beans

## [2.2.1] - 2019-10-18
### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@ public long getTimerMaxRuntimeMilliseconds() {
public boolean isDisabled() {
return parseBoolean(disablePrePublish);
}

public void setDisabled(final boolean disable) {
this.disablePrePublish = Boolean.toString(disable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import uk.gov.justice.services.common.configuration.GlobalValue;

import javax.inject.Inject;
import javax.inject.Singleton;

@Singleton
public class PublisherTimerConfig {

@Inject
Expand Down Expand Up @@ -40,4 +42,8 @@ public long getTimerMaxRuntimeMilliseconds() {
public boolean isDisabled() {
return parseBoolean(disablePublish);
}

public void setDisabled(final boolean disable) {
this.disablePublish = Boolean.toString(disable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,9 @@ public void shouldReturnTrueIfDisabled() throws Exception {

setField(prePublisherTimerConfig, "disablePrePublish", null);
assertThat(prePublisherTimerConfig.isDisabled(), is(false));

prePublisherTimerConfig.setDisabled(true);

assertThat(prePublisherTimerConfig.isDisabled(), is(true));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,8 @@ public void shouldReturnTrueIfDisabled() throws Exception {

setField(publisherTimerConfig, "disablePublish", null);
assertThat(publisherTimerConfig.isDisabled(), is(false));

publisherTimerConfig.setDisabled(true);
assertThat(publisherTimerConfig.isDisabled(), is(true));
}
}
6 changes: 6 additions & 0 deletions event-store-management/event-store-management-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
<artifactId>event-store-management-events</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>uk.gov.justice.event-store</groupId>
<artifactId>event-publisher-timer</artifactId>
<version>${project.version}</version>
</dependency>


<!--Test Dependencies-->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package uk.gov.justice.services.eventstore.management.publishing;

import static uk.gov.justice.services.jmx.api.command.DisablePublishingCommand.DISABLE_PUBLISHING;
import static uk.gov.justice.services.jmx.api.command.EnablePublishingCommand.ENABLE_PUBLISHING;

import uk.gov.justice.services.jmx.api.command.DisablePublishingCommand;
import uk.gov.justice.services.jmx.api.command.EnablePublishingCommand;
import uk.gov.justice.services.jmx.command.HandlesSystemCommand;
import uk.gov.justice.services.jmx.logging.MdcLoggerInterceptor;

import java.util.UUID;

import javax.inject.Inject;
import javax.interceptor.Interceptors;

public class EnablePublishingCommandHandler {

@Inject
private EnablePublishingProcessor enablePublishingProcessor;

@Interceptors(MdcLoggerInterceptor.class)
@HandlesSystemCommand(ENABLE_PUBLISHING)
public void enablePublishing(final EnablePublishingCommand enablePublishingCommand, final UUID commandId) {
enablePublishingProcessor.enableDisable(enablePublishingCommand, commandId);
}

@Interceptors(MdcLoggerInterceptor.class)
@HandlesSystemCommand(DISABLE_PUBLISHING)
public void disablePublishing(final DisablePublishingCommand disablePublishingCommand, final UUID commandId) {
enablePublishingProcessor.enableDisable(disablePublishingCommand, commandId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package uk.gov.justice.services.eventstore.management.publishing;

import static java.lang.String.format;
import static uk.gov.justice.services.jmx.api.domain.CommandState.COMMAND_COMPLETE;
import static uk.gov.justice.services.jmx.api.domain.CommandState.COMMAND_IN_PROGRESS;

import uk.gov.justice.services.common.util.UtcClock;
import uk.gov.justice.services.jmx.api.command.PublishingCommand;
import uk.gov.justice.services.jmx.state.events.SystemCommandStateChangedEvent;

import java.util.UUID;

import javax.enterprise.event.Event;
import javax.inject.Inject;

import org.slf4j.Logger;

public class EnablePublishingProcessor {

@Inject
private PublishingEnabler publishingEnabler;

@Inject
private Event<SystemCommandStateChangedEvent> systemCommandStateChangedEventFirer;

@Inject
private UtcClock clock;

@Inject
private Logger logger;

public void enableDisable(final PublishingCommand publishingCommand, final UUID commandId) {

final String commandName = publishingCommand.getName();
final String startMessage = format("Running %s", commandName);

systemCommandStateChangedEventFirer.fire(new SystemCommandStateChangedEvent(
commandId,
publishingCommand,
COMMAND_IN_PROGRESS,
clock.now(),
startMessage
));

logger.info(startMessage);

publishingEnabler.enableOrDisable(publishingCommand);

final String completeMessage = format("%s complete", commandName);
systemCommandStateChangedEventFirer.fire(new SystemCommandStateChangedEvent(
commandId,
publishingCommand,
COMMAND_COMPLETE,
clock.now(),
completeMessage
));

logger.info(completeMessage);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package uk.gov.justice.services.eventstore.management.publishing;

import static uk.gov.justice.services.jmx.api.command.EnablePublishingCommand.ENABLE_PUBLISHING;

import uk.gov.justice.services.eventsourcing.publishedevent.prepublish.PrePublisherTimerConfig;
import uk.gov.justice.services.eventsourcing.publishedevent.publishing.PublisherTimerConfig;
import uk.gov.justice.services.jmx.api.command.PublishingCommand;

import javax.inject.Inject;

import org.slf4j.Logger;

public class PublishingEnabler {

@Inject
private PublisherTimerConfig publishTimerConfig;

@Inject
private PrePublisherTimerConfig prePublisherTimerConfig;

@Inject
private Logger logger;

public void enableOrDisable(final PublishingCommand publishingCommand) {

if (publishingCommand.getName().equals(ENABLE_PUBLISHING)) {
enable();
} else {
disable();
}
}

private void enable() {
prePublisherTimerConfig.setDisabled(false);
publishTimerConfig.setDisabled(false);

logger.info("Publishing of events enabled");
}

private void disable() {
prePublisherTimerConfig.setDisabled(true);
publishTimerConfig.setDisabled(true);

logger.info("Publishing of events disabled");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package uk.gov.justice.services.eventstore.management.publishing;

import static java.util.UUID.randomUUID;
import static org.mockito.Mockito.verify;

import uk.gov.justice.services.jmx.api.command.DisablePublishingCommand;
import uk.gov.justice.services.jmx.api.command.EnablePublishingCommand;

import java.util.UUID;

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 EnablePublishingCommandHandlerTest {

@Mock
private EnablePublishingProcessor enablePublishingProcessor;

@InjectMocks
private EnablePublishingCommandHandler enablePublishingCommandHandler;

@Test
public void shouldEnablePublishing() throws Exception {

final EnablePublishingCommand enablePublishingCommand = new EnablePublishingCommand();
final UUID commandId = randomUUID();

enablePublishingCommandHandler.enablePublishing(enablePublishingCommand, commandId);

verify(enablePublishingProcessor).enableDisable(enablePublishingCommand, commandId);
}

@Test
public void shouldDisablePublishing() throws Exception {

final DisablePublishingCommand disablePublishingCommand = new DisablePublishingCommand();
final UUID commandId = randomUUID();

enablePublishingCommandHandler.disablePublishing(disablePublishingCommand, commandId);

verify(enablePublishingProcessor).enableDisable(disablePublishingCommand, commandId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package uk.gov.justice.services.eventstore.management.publishing;

import static java.util.UUID.randomUUID;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.when;
import static uk.gov.justice.services.jmx.api.domain.CommandState.COMMAND_COMPLETE;
import static uk.gov.justice.services.jmx.api.domain.CommandState.COMMAND_IN_PROGRESS;

import uk.gov.justice.services.common.util.UtcClock;
import uk.gov.justice.services.jmx.api.command.EnablePublishingCommand;
import uk.gov.justice.services.jmx.api.command.PublishingCommand;
import uk.gov.justice.services.jmx.state.events.SystemCommandStateChangedEvent;

import java.time.ZonedDateTime;
import java.util.List;
import java.util.UUID;

import javax.enterprise.event.Event;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InOrder;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.slf4j.Logger;


@RunWith(MockitoJUnitRunner.class)
public class EnablePublishingProcessorTest {

@Mock
private PublishingEnabler publishingEnabler;

@Mock
private Event<SystemCommandStateChangedEvent> systemCommandStateChangedEventFirer;

@Mock
private UtcClock clock;

@Mock
private Logger logger;

@InjectMocks
private EnablePublishingProcessor enablePublishingProcessor;

@Captor
private ArgumentCaptor<SystemCommandStateChangedEvent> systemCommandStateChangedEventCaptor;

@Test
public void shouldEnableOrDisablePublishingAndFireTheCorrectCommandStateEvents() throws Exception {

final UUID commandId = randomUUID();
final PublishingCommand publishingCommand = new EnablePublishingCommand();

final ZonedDateTime startTime = new UtcClock().now();
final ZonedDateTime completeTime = startTime.plusSeconds(1);

when(clock.now()).thenReturn(startTime, completeTime);

enablePublishingProcessor.enableDisable(publishingCommand, commandId);

final InOrder inOrder = inOrder(systemCommandStateChangedEventFirer, logger, publishingEnabler);

inOrder.verify(systemCommandStateChangedEventFirer).fire(systemCommandStateChangedEventCaptor.capture());
inOrder.verify(logger).info("Running ENABLE_PUBLISHING");
inOrder.verify(publishingEnabler).enableOrDisable(publishingCommand);
inOrder.verify(systemCommandStateChangedEventFirer).fire(systemCommandStateChangedEventCaptor.capture());
inOrder.verify(logger).info("ENABLE_PUBLISHING complete");

final List<SystemCommandStateChangedEvent> allValues = systemCommandStateChangedEventCaptor.getAllValues();

final SystemCommandStateChangedEvent startEvent = allValues.get(0);

assertThat(startEvent.getCommandState(), is(COMMAND_IN_PROGRESS));
assertThat(startEvent.getCommandId(), is(commandId));
assertThat(startEvent.getSystemCommand(), is(publishingCommand));
assertThat(startEvent.getStatusChangedAt(), is(startTime));
assertThat(startEvent.getMessage(), is("Running ENABLE_PUBLISHING"));

final SystemCommandStateChangedEvent completeEvent = allValues.get(1);

assertThat(completeEvent.getCommandState(), is(COMMAND_COMPLETE));
assertThat(completeEvent.getCommandId(), is(commandId));
assertThat(completeEvent.getSystemCommand(), is(publishingCommand));
assertThat(completeEvent.getStatusChangedAt(), is(completeTime));
assertThat(completeEvent.getMessage(), is("ENABLE_PUBLISHING complete"));
}
}
Loading

0 comments on commit 74680d5

Please sign in to comment.