-
Notifications
You must be signed in to change notification settings - Fork 3
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
13 changed files
with
366 additions
and
3 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
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
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
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
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
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
32 changes: 32 additions & 0 deletions
32
...gov/justice/services/eventstore/management/publishing/EnablePublishingCommandHandler.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,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); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...a/uk/gov/justice/services/eventstore/management/publishing/EnablePublishingProcessor.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,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); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...main/java/uk/gov/justice/services/eventstore/management/publishing/PublishingEnabler.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,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"); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...justice/services/eventstore/management/publishing/EnablePublishingCommandHandlerTest.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,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); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
.../gov/justice/services/eventstore/management/publishing/EnablePublishingProcessorTest.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,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")); | ||
} | ||
} |
Oops, something went wrong.