Skip to content

Commit

Permalink
Merge 1bb666c into 309767b
Browse files Browse the repository at this point in the history
  • Loading branch information
allanmckenzie committed Jun 25, 2019
2 parents 309767b + 1bb666c commit b2c0101
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 107 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package uk.gov.justice.services.eventstore.management.shuttercatchup.commands;
package uk.gov.justice.services.eventstore.management.catchup.commands;

import static uk.gov.justice.services.eventstore.management.shuttercatchup.commands.ShutterCatchupCommand.SHUTTER_CATCHUP;
import static uk.gov.justice.services.eventstore.management.catchup.commands.CatchupCommand.CATCHUP;

import uk.gov.justice.services.common.util.UtcClock;
import uk.gov.justice.services.eventstore.management.catchup.events.CatchupCompletedEvent;
Expand All @@ -17,7 +17,7 @@

import org.slf4j.Logger;

public class ShutterCatchupCommandHandler {
public class CatchupCommandHandler {

@Inject
private Event<ShutteringRequestedEvent> shutteringRequestedEventFirer;
Expand All @@ -35,13 +35,13 @@ public class ShutterCatchupCommandHandler {
private Logger logger;


@HandlesSystemCommand(SHUTTER_CATCHUP)
public void doCatchupWhilstShuttered(final ShutterCatchupCommand shutterCatchupCommand) {
@HandlesSystemCommand(CATCHUP)
public void doCatchupWhilstShuttered(final CatchupCommand catchupCommand) {

logger.info("Catchup requested. Shuttering application first");

final ShutteringRequestedEvent shutteringRequestedEvent = new ShutteringRequestedEvent(
shutterCatchupCommand,
catchupCommand,
clock.now());

shutteringRequestedEventFirer.fire(shutteringRequestedEvent);
Expand All @@ -51,7 +51,7 @@ public void onShutteringComplete(@Observes final ShutteringCompleteEvent shutter

final SystemCommand systemCommand = shutteringCompleteEvent.getTarget();

if(systemCommand instanceof ShutterCatchupCommand) {
if(systemCommand instanceof CatchupCommand) {

logger.info("Received ShutteringComplete event. Now firing CatchupRequested event");

Expand All @@ -66,7 +66,7 @@ public void onShutteringComplete(@Observes final ShutteringCompleteEvent shutter
public void onCatchupComplete(@Observes final CatchupCompletedEvent catchupCompletedEvent) {

final SystemCommand systemCommand = catchupCompletedEvent.getTarget();
if(systemCommand instanceof ShutterCatchupCommand) {
if(systemCommand instanceof CatchupCommand) {

logger.info("Received CatchupCompleted event. Now firing UnshutteringRequested event");
final UnshutteringRequestedEvent unshutteringRequestedEvent = new UnshutteringRequestedEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,65 @@
import static uk.gov.justice.services.eventstore.management.rebuild.commands.RebuildCommand.REBUILD;

import uk.gov.justice.services.common.util.UtcClock;
import uk.gov.justice.services.eventstore.management.catchup.commands.CatchupCommand;
import uk.gov.justice.services.eventstore.management.rebuild.events.RebuildCompleteEvent;
import uk.gov.justice.services.eventstore.management.rebuild.events.RebuildRequestedEvent;
import uk.gov.justice.services.jmx.command.HandlesSystemCommand;
import uk.gov.justice.services.jmx.command.SystemCommand;
import uk.gov.justice.services.management.shuttering.events.ShutteringCompleteEvent;
import uk.gov.justice.services.management.shuttering.events.ShutteringRequestedEvent;
import uk.gov.justice.services.management.shuttering.events.UnshutteringRequestedEvent;

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

import org.slf4j.Logger;

public class RebuildCommandHandler {

@Inject
private UtcClock clock;
private Event<ShutteringRequestedEvent> shutteringRequestedEventFirer;

@Inject
private Event<RebuildRequestedEvent> rebuildRequestedEventEventFirer;

@Inject
private Event<UnshutteringRequestedEvent> unshutteringRequestedEventFirer;

@Inject
private UtcClock clock;

@Inject
private Logger logger;

@HandlesSystemCommand(REBUILD)
public void doRebuild(final RebuildCommand rebuildCommand) {
rebuildRequestedEventEventFirer.fire(new RebuildRequestedEvent(clock.now(), rebuildCommand));
shutteringRequestedEventFirer.fire(new ShutteringRequestedEvent(rebuildCommand, clock.now()));
}

public void onShutteringComplete(@Observes final ShutteringCompleteEvent shutteringCompleteEvent) {

final SystemCommand systemCommand = shutteringCompleteEvent.getTarget();

if(systemCommand instanceof RebuildCommand) {

logger.info("Received ShutteringComplete event. Now firing RebuildRequestedEvent");

rebuildRequestedEventEventFirer.fire(new RebuildRequestedEvent(clock.now(), systemCommand));
}
}

public void onRebuildComplete(@Observes final RebuildCompleteEvent rebuildCompleteEvent) {

final SystemCommand systemCommand = rebuildCompleteEvent.getTarget();
if(systemCommand instanceof RebuildCommand) {

logger.info("Received RebuildComplete event. Now firing UnshutteringRequested event");

unshutteringRequestedEventFirer.fire(new UnshutteringRequestedEvent(
systemCommand,
clock.now()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package uk.gov.justice.services.eventstore.management.rebuild.events;

import uk.gov.justice.services.jmx.command.SystemCommand;

import java.time.ZonedDateTime;
import java.util.Objects;

public class RebuildCompleteEvent {

private final SystemCommand target;
private final ZonedDateTime rebuildCompletedAt;

public RebuildCompleteEvent(final SystemCommand target, final ZonedDateTime rebuildCompletedAt) {
this.target = target;
this.rebuildCompletedAt = rebuildCompletedAt;
}

public SystemCommand getTarget() {
return target;
}

public ZonedDateTime getRebuildCompletedAt() {
return rebuildCompletedAt;
}

@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (!(o instanceof RebuildCompleteEvent)) return false;
final RebuildCompleteEvent that = (RebuildCompleteEvent) o;
return Objects.equals(target, that.target) &&
Objects.equals(rebuildCompletedAt, that.rebuildCompletedAt);
}

@Override
public int hashCode() {
return Objects.hash(target, rebuildCompletedAt);
}

@Override
public String toString() {
return "RebuildCompleteEvent{" +
"target=" + target +
", rebuildStartedAt=" + rebuildCompletedAt +
'}';
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

import uk.gov.justice.services.common.util.UtcClock;
import uk.gov.justice.services.eventsourcing.publishedevent.rebuild.PublishedEventRebuilder;
import uk.gov.justice.services.eventstore.management.rebuild.events.RebuildCompleteEvent;
import uk.gov.justice.services.eventstore.management.rebuild.events.RebuildRequestedEvent;
import uk.gov.justice.services.eventstore.management.rebuild.events.RebuildStartedEvent;
import uk.gov.justice.services.jmx.command.SystemCommand;

import java.time.ZonedDateTime;
Expand All @@ -23,7 +23,7 @@ public class RebuildObserver {
private PublishedEventRebuilder publishedEventRebuilder;

@Inject
private Event<RebuildStartedEvent> rebuildStartedEventFirer;
private Event<RebuildCompleteEvent> rebuildCompletedEventFirer;

@Inject
private UtcClock clock;
Expand All @@ -33,20 +33,21 @@ public class RebuildObserver {

public void onRebuildRequested(@Observes final RebuildRequestedEvent rebuildRequestedEvent) {

final SystemCommand cause = rebuildRequestedEvent.getTarget();
final SystemCommand target = rebuildRequestedEvent.getTarget();

final String causeCommandName = cause.getName();
logger.info(format("Rebuild requested by '%s' at %tc", causeCommandName, rebuildRequestedEvent.getRebuildRequestedAt()));
final String commandName = target.getName();
logger.info(format("Rebuild requested by '%s' command at %tc", commandName, rebuildRequestedEvent.getRebuildRequestedAt()));

final ZonedDateTime rebuildStartedAt = clock.now();
logger.info(format("Rebuild for '%s' started at %tc", causeCommandName, rebuildStartedAt));
rebuildStartedEventFirer.fire(new RebuildStartedEvent(cause, rebuildStartedAt));
logger.info(format("Rebuild for '%s' command started at %tc", commandName, rebuildStartedAt));

publishedEventRebuilder.rebuild();

final ZonedDateTime rebuildCompletedAt = clock.now();
final String format = format("Rebuild for '%s' completed at %tc", causeCommandName, rebuildCompletedAt);
final String format = format("Rebuild for '%s' command completed at %tc", commandName, rebuildCompletedAt);
logger.info(format);
logger.info(format("Rebuild took %d milliseconds", MILLIS.between(rebuildStartedAt, rebuildCompletedAt)));

rebuildCompletedEventFirer.fire(new RebuildCompleteEvent(target, rebuildCompletedAt));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package uk.gov.justice.services.eventstore.management.shuttercatchup.commands;
package uk.gov.justice.services.eventstore.management.catchup.commands;

import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
Expand All @@ -17,7 +17,6 @@

import javax.enterprise.event.Event;

import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InOrder;
Expand All @@ -27,7 +26,7 @@
import org.slf4j.Logger;

@RunWith(MockitoJUnitRunner.class)
public class ShutterCatchupCommandHandlerTest {
public class CatchupCommandHandlerTest {

@Mock
private Event<ShutteringRequestedEvent> shutteringRequestedEventFirer;
Expand All @@ -45,41 +44,41 @@ public class ShutterCatchupCommandHandlerTest {
private Logger logger;

@InjectMocks
private ShutterCatchupCommandHandler shutterCatchupCommandHandler;
private CatchupCommandHandler catchupCommandHandler;

@Test
public void shouldCallCatchupOnHandlingCommand() throws Exception {

final ZonedDateTime now = new UtcClock().now();
final ShutterCatchupCommand shutterCatchupCommand = new ShutterCatchupCommand();
final CatchupCommand catchupCommand = new CatchupCommand();

when(clock.now()).thenReturn(now);

shutterCatchupCommandHandler.doCatchupWhilstShuttered(shutterCatchupCommand);
catchupCommandHandler.doCatchupWhilstShuttered(catchupCommand);

final InOrder inOrder = inOrder(logger, shutteringRequestedEventFirer);

inOrder.verify(logger).info("Catchup requested. Shuttering application first");
inOrder.verify(shutteringRequestedEventFirer).fire(new ShutteringRequestedEvent(
shutterCatchupCommand,
catchupCommand,
now));
}

@Test
public void shouldKickOffCatchupWhenShutteringCompleteIfCommandIsShutterCatchupCommand() throws Exception {

final ShutteringCompleteEvent shutteringCompleteEvent = mock(ShutteringCompleteEvent.class);
final ShutterCatchupCommand shutterCatchupCommand = new ShutterCatchupCommand();
final CatchupCommand catchupCommand = new CatchupCommand();

when(shutteringCompleteEvent.getTarget()).thenReturn(shutterCatchupCommand);
when(shutteringCompleteEvent.getTarget()).thenReturn(catchupCommand);

shutterCatchupCommandHandler.onShutteringComplete(shutteringCompleteEvent);
catchupCommandHandler.onShutteringComplete(shutteringCompleteEvent);

final InOrder inOrder = inOrder(logger, catchupRequestedEventFirer);

inOrder.verify(logger).info("Received ShutteringComplete event. Now firing CatchupRequested event");
inOrder.verify(catchupRequestedEventFirer).fire(new CatchupRequestedEvent(
shutterCatchupCommand,
catchupCommand,
clock.now()));
}

Expand All @@ -91,7 +90,7 @@ public void shouldNotKickOffCatchupIfCommandIsNotShutterCatchupCommand() throws

when(shutteringCompleteEvent.getTarget()).thenReturn(notAShutterCatchupCommand);

shutterCatchupCommandHandler.onShutteringComplete(shutteringCompleteEvent);
catchupCommandHandler.onShutteringComplete(shutteringCompleteEvent);

verifyZeroInteractions(logger);
verifyZeroInteractions(catchupRequestedEventFirer);
Expand All @@ -101,17 +100,17 @@ public void shouldNotKickOffCatchupIfCommandIsNotShutterCatchupCommand() throws
public void shouldKickOffUnshutteringWhenCatchupCompleteIfCommandIsShutterCatchupCommand() throws Exception {

final CatchupCompletedEvent catchupCompletedEvent = mock(CatchupCompletedEvent.class);
final ShutterCatchupCommand shutterCatchupCommand = new ShutterCatchupCommand();
final CatchupCommand catchupCommand = new CatchupCommand();

when(catchupCompletedEvent.getTarget()).thenReturn(shutterCatchupCommand);
when(catchupCompletedEvent.getTarget()).thenReturn(catchupCommand);

shutterCatchupCommandHandler.onCatchupComplete(catchupCompletedEvent);
catchupCommandHandler.onCatchupComplete(catchupCompletedEvent);

final InOrder inOrder = inOrder(logger, unshutteringRequestedEventFirer);

inOrder.verify(logger).info("Received CatchupCompleted event. Now firing UnshutteringRequested event");
inOrder.verify(unshutteringRequestedEventFirer).fire(new UnshutteringRequestedEvent(
shutterCatchupCommand,
catchupCommand,
clock.now()));
}
}
Loading

0 comments on commit b2c0101

Please sign in to comment.