Skip to content

Commit

Permalink
Merge c9e1348 into d4ef9c7
Browse files Browse the repository at this point in the history
  • Loading branch information
allanmckenzie committed Dec 30, 2019
2 parents d4ef9c7 + c9e1348 commit 68e50d8
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ on [Keep a CHANGELOG](http://keepachangelog.com/). This project adheres to
[Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added
- Added mechanism to also drop/add trigger to event_log table on SUSPEND/UNSUSPEND commands

## [2.4.3] - 2019-12-06
### Changed
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package uk.gov.justice.services.eventstore.management.shuttering.observers;

import static java.lang.String.format;
import static uk.gov.justice.services.management.suspension.api.SuspensionResult.suspensionFailed;
import static uk.gov.justice.services.management.suspension.api.SuspensionResult.suspensionSucceeded;

import uk.gov.justice.services.eventsourcing.util.sql.triggers.EventLogTriggerManipulator;
import uk.gov.justice.services.management.suspension.api.Suspendable;
import uk.gov.justice.services.management.suspension.api.SuspensionResult;
import uk.gov.justice.services.management.suspension.commands.SuspensionCommand;

import java.util.UUID;

import javax.inject.Inject;

import org.slf4j.Logger;

public class TriggerDropper implements Suspendable {

@Inject
private EventLogTriggerManipulator eventLogTriggerManipulator;

@Inject
private Logger logger;

@Override
public boolean shouldSuspend() {
return true;
}

@Override
public boolean shouldUnsuspend() {
return true;
}

@Override
public SuspensionResult suspend(final UUID commandId, final SuspensionCommand suspensionCommand) {

try {
eventLogTriggerManipulator.removeTriggerFromEventLogTable();

final String message = "Trigger successfully removed from event_log table";
logger.info(message);

return suspensionSucceeded(
getName(),
commandId,
message,
suspensionCommand
);
} catch (final Exception e) {
final String message = format("Failed to remove trigger from event_log table: %s:%s", e.getClass().getSimpleName(), e.getMessage());
logger.error(message, e);

return suspensionFailed(
getName(),
commandId,
message,
suspensionCommand,
e
);
}
}

@Override
public SuspensionResult unsuspend(final UUID commandId, final SuspensionCommand suspensionCommand) {
try {
eventLogTriggerManipulator.addTriggerToEventLogTable();

final String message = "Trigger successfully added to event_log table";
logger.info(message);

return suspensionSucceeded(
getName(),
commandId,
message,
suspensionCommand
);
} catch (final Exception e) {
final String message = format("Failed to add trigger to event_log table: %s:%s", e.getClass().getSimpleName(), e.getMessage());
logger.error(message, e);

return suspensionFailed(
getName(),
commandId,
message,
suspensionCommand,
e
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package uk.gov.justice.services.eventstore.management.shuttering.observers;

import static java.util.Optional.empty;
import static java.util.Optional.of;
import static java.util.UUID.randomUUID;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.inOrder;
import static uk.gov.justice.services.jmx.api.domain.CommandState.COMMAND_COMPLETE;
import static uk.gov.justice.services.jmx.api.domain.CommandState.COMMAND_FAILED;

import uk.gov.justice.services.eventsourcing.util.sql.triggers.EventLogTriggerManipulator;
import uk.gov.justice.services.eventsourcing.util.sql.triggers.TriggerManipulationFailedException;
import uk.gov.justice.services.management.suspension.api.SuspensionResult;
import uk.gov.justice.services.management.suspension.commands.SuspendCommand;
import uk.gov.justice.services.management.suspension.commands.UnsuspendCommand;

import java.util.UUID;

import org.junit.Test;
import org.junit.runner.RunWith;
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 TriggerDropperTest {

@Mock
private EventLogTriggerManipulator eventLogTriggerManipulator;

@Mock
private Logger logger;

@InjectMocks
private TriggerDropper triggerDropper;

@Test
public void shouldBothSuspendAndUnsuspend() throws Exception {

assertThat(triggerDropper.shouldSuspend(), is(true));
assertThat(triggerDropper.shouldUnsuspend(), is(true));
}

@Test
public void shouldRemoveTriggerFromEventLogTableWhenSuspending() throws Exception {

final UUID commandId = randomUUID();
final SuspendCommand suspendCommand = new SuspendCommand();

final SuspensionResult suspensionResult = triggerDropper.suspend(commandId, suspendCommand);

assertThat(suspensionResult.getCommandId(), is(commandId));
assertThat(suspensionResult.getSuspendableName(), is("TriggerDropper"));
assertThat(suspensionResult.getSystemCommand(), is(suspendCommand));
assertThat(suspensionResult.getCommandState(), is(COMMAND_COMPLETE));
assertThat(suspensionResult.getMessage(), is("Trigger successfully removed from event_log table"));
assertThat(suspensionResult.getException(), is(empty()));

final InOrder inOrder = inOrder(eventLogTriggerManipulator, logger);

inOrder.verify(eventLogTriggerManipulator).removeTriggerFromEventLogTable();
inOrder.verify(logger).info("Trigger successfully removed from event_log table");
}

@Test
public void shouldReturnUnsuccessfulResultIfRemovingTheTriggerFails() throws Exception {

final UUID commandId = randomUUID();
final SuspendCommand suspendCommand = new SuspendCommand();

final TriggerManipulationFailedException triggerManipulationFailedException = new TriggerManipulationFailedException("Ooops");

doThrow(triggerManipulationFailedException).when(eventLogTriggerManipulator).removeTriggerFromEventLogTable();

final SuspensionResult suspensionResult = triggerDropper.suspend(commandId, suspendCommand);

assertThat(suspensionResult.getCommandId(), is(commandId));
assertThat(suspensionResult.getSuspendableName(), is("TriggerDropper"));
assertThat(suspensionResult.getSystemCommand(), is(suspendCommand));
assertThat(suspensionResult.getCommandState(), is(COMMAND_FAILED));
assertThat(suspensionResult.getMessage(), is("Failed to remove trigger from event_log table: TriggerManipulationFailedException:Ooops"));
assertThat(suspensionResult.getException(), is(of(triggerManipulationFailedException)));

final InOrder inOrder = inOrder(eventLogTriggerManipulator, logger);

inOrder.verify(eventLogTriggerManipulator).removeTriggerFromEventLogTable();
inOrder.verify(logger).error("Failed to remove trigger from event_log table: TriggerManipulationFailedException:Ooops", triggerManipulationFailedException);
}

@Test
public void shouldAddTriggerToEventLogTableWhenUnsuspending() throws Exception {

final UUID commandId = randomUUID();
final UnsuspendCommand unsuspendCommand = new UnsuspendCommand();

final SuspensionResult suspensionResult = triggerDropper.unsuspend(commandId, unsuspendCommand);

assertThat(suspensionResult.getCommandId(), is(commandId));
assertThat(suspensionResult.getSuspendableName(), is("TriggerDropper"));
assertThat(suspensionResult.getSystemCommand(), is(unsuspendCommand));
assertThat(suspensionResult.getCommandState(), is(COMMAND_COMPLETE));
assertThat(suspensionResult.getMessage(), is("Trigger successfully added to event_log table"));
assertThat(suspensionResult.getException(), is(empty()));

final InOrder inOrder = inOrder(eventLogTriggerManipulator, logger);

inOrder.verify(eventLogTriggerManipulator).addTriggerToEventLogTable();
inOrder.verify(logger).info("Trigger successfully added to event_log table");
}

@Test
public void shouldReturnUnsuccessfulResultIfAddingTheTriggerFails() throws Exception {

final UUID commandId = randomUUID();
final UnsuspendCommand unsuspendCommand = new UnsuspendCommand();
final TriggerManipulationFailedException triggerManipulationFailedException = new TriggerManipulationFailedException("Ooops");

doThrow(triggerManipulationFailedException).when(eventLogTriggerManipulator).addTriggerToEventLogTable();

final SuspensionResult suspensionResult = triggerDropper.unsuspend(commandId, unsuspendCommand);

assertThat(suspensionResult.getCommandId(), is(commandId));
assertThat(suspensionResult.getSuspendableName(), is("TriggerDropper"));
assertThat(suspensionResult.getSystemCommand(), is(unsuspendCommand));
assertThat(suspensionResult.getCommandState(), is(COMMAND_FAILED));
assertThat(suspensionResult.getMessage(), is("Failed to add trigger to event_log table: TriggerManipulationFailedException:Ooops"));
assertThat(suspensionResult.getException(), is(of(triggerManipulationFailedException)));

final InOrder inOrder = inOrder(eventLogTriggerManipulator, logger);

inOrder.verify(eventLogTriggerManipulator).addTriggerToEventLogTable();
inOrder.verify(logger).error("Failed to add trigger to event_log table: TriggerManipulationFailedException:Ooops", triggerManipulationFailedException);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

public class TriggerManipulationFailedException extends RuntimeException {

public TriggerManipulationFailedException(final String message) {
super(message);
}

public TriggerManipulationFailedException(final String message, final Throwable cause) {
super(message, cause);
}
Expand Down

0 comments on commit 68e50d8

Please sign in to comment.