-
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
17 changed files
with
776 additions
and
2 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
39 changes: 39 additions & 0 deletions
39
...ice/services/eventstore/management/untrigger/commands/AddRemoveTriggerCommandHandler.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,39 @@ | ||
package uk.gov.justice.services.eventstore.management.untrigger.commands; | ||
|
||
import static java.lang.String.format; | ||
import static uk.gov.justice.services.jmx.api.command.AddTriggerCommand.ADD_TRIGGER; | ||
import static uk.gov.justice.services.jmx.api.command.RemoveTriggerCommand.REMOVE_TRIGGER; | ||
|
||
import uk.gov.justice.services.eventstore.management.untrigger.process.EventLogTriggerManipulator; | ||
import uk.gov.justice.services.jmx.api.command.AddTriggerCommand; | ||
import uk.gov.justice.services.jmx.api.command.RemoveTriggerCommand; | ||
import uk.gov.justice.services.jmx.command.HandlesSystemCommand; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.slf4j.Logger; | ||
|
||
public class AddRemoveTriggerCommandHandler { | ||
|
||
@Inject | ||
private EventLogTriggerManipulator eventLogTriggerManipulator; | ||
|
||
@Inject | ||
private Logger logger; | ||
|
||
@HandlesSystemCommand(ADD_TRIGGER) | ||
public void addTriggerToEventLogTable(final AddTriggerCommand addTriggerCommand) { | ||
|
||
logger.info(format("Received command %s", addTriggerCommand.getName())); | ||
|
||
eventLogTriggerManipulator.addTriggerToEventLogTable(); | ||
} | ||
|
||
@HandlesSystemCommand(REMOVE_TRIGGER) | ||
public void removeTriggerFromEventLogTable(final RemoveTriggerCommand removeTriggerCommand) { | ||
|
||
logger.info(format("Received command %s", removeTriggerCommand.getName())); | ||
|
||
eventLogTriggerManipulator.removeTriggerFromEventLogTable(); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
.../justice/services/eventstore/management/untrigger/process/EventLogTriggerManipulator.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,54 @@ | ||
package uk.gov.justice.services.eventstore.management.untrigger.process; | ||
|
||
import static java.lang.String.format; | ||
|
||
import uk.gov.justice.services.eventsourcing.util.sql.triggers.DatabaseTriggerManipulator; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.slf4j.Logger; | ||
|
||
public class EventLogTriggerManipulator { | ||
|
||
private static final String TRIGGER_NAME = "queue_publish_event"; | ||
private static final String TABLE_NAME = "event_log"; | ||
private static final String ACTION = "EXECUTE PROCEDURE update_publish_queue()"; | ||
|
||
@Inject | ||
private EventStoreTriggerManipulatorProvider eventStoreTriggerManipulatorProvider; | ||
|
||
@Inject | ||
private Logger logger; | ||
|
||
public void addTriggerToEventLogTable() { | ||
|
||
final DatabaseTriggerManipulator databaseTriggerManipulator = eventStoreTriggerManipulatorProvider | ||
.getDatabaseTriggerManipulator(); | ||
|
||
if (databaseTriggerManipulator.findTriggerOnTable(TRIGGER_NAME, TABLE_NAME).isPresent()) { | ||
logger.warn(format("Trigger '%s' already exists on %s table", TRIGGER_NAME, TABLE_NAME)); | ||
} else { | ||
|
||
databaseTriggerManipulator | ||
.addInsertTriggerToTable(TRIGGER_NAME, TABLE_NAME, ACTION); | ||
|
||
logger.info(format("Trigger '%s' successfully added to %s table", TRIGGER_NAME, TABLE_NAME)); | ||
} | ||
} | ||
|
||
public void removeTriggerFromEventLogTable() { | ||
|
||
final DatabaseTriggerManipulator databaseTriggerManipulator = eventStoreTriggerManipulatorProvider | ||
.getDatabaseTriggerManipulator(); | ||
|
||
if (databaseTriggerManipulator.findTriggerOnTable(TRIGGER_NAME, TABLE_NAME).isPresent()) { | ||
|
||
databaseTriggerManipulator | ||
.removeTriggerFromTable(TRIGGER_NAME, TABLE_NAME); | ||
|
||
logger.info(format("Removed trigger '%s' from %s table", TRIGGER_NAME, TABLE_NAME)); | ||
} else { | ||
logger.warn(format("No trigger named '%s' found on %s table", TRIGGER_NAME, TABLE_NAME)); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...ervices/eventstore/management/untrigger/process/EventStoreTriggerManipulatorProvider.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,22 @@ | ||
package uk.gov.justice.services.eventstore.management.untrigger.process; | ||
|
||
import uk.gov.justice.services.eventsourcing.source.core.EventStoreDataSourceProvider; | ||
import uk.gov.justice.services.eventsourcing.util.sql.triggers.DatabaseTriggerManipulator; | ||
import uk.gov.justice.services.eventsourcing.util.sql.triggers.DatabaseTriggerManipulatorFactory; | ||
|
||
import javax.inject.Inject; | ||
import javax.sql.DataSource; | ||
|
||
public class EventStoreTriggerManipulatorProvider { | ||
|
||
@Inject | ||
private EventStoreDataSourceProvider eventStoreDataSourceProvider; | ||
|
||
@Inject | ||
public DatabaseTriggerManipulatorFactory databaseTriggerManipulatorFactory; | ||
|
||
public DatabaseTriggerManipulator getDatabaseTriggerManipulator() { | ||
final DataSource defaultDataSource = eventStoreDataSourceProvider.getDefaultDataSource(); | ||
return databaseTriggerManipulatorFactory.databaseTriggerManipulator(defaultDataSource); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...ice/services/eventstore/management/validation/commands/ValidateCatchupCommandHandler.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,22 @@ | ||
package uk.gov.justice.services.eventstore.management.validation.commands; | ||
|
||
import static java.lang.String.format; | ||
import static uk.gov.justice.services.jmx.api.command.ValidateCatchupCommand.VALIDATE_CATCHUP; | ||
|
||
import uk.gov.justice.services.jmx.api.command.ValidateCatchupCommand; | ||
import uk.gov.justice.services.jmx.command.HandlesSystemCommand; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.slf4j.Logger; | ||
|
||
public class ValidateCatchupCommandHandler { | ||
|
||
@Inject | ||
private Logger logger; | ||
|
||
@HandlesSystemCommand(VALIDATE_CATCHUP) | ||
public void validateCatchup(final ValidateCatchupCommand validateCatchupCommand) { | ||
logger.warn(format("Command %s not yet implemented", validateCatchupCommand.getName())); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...services/eventstore/management/untrigger/commands/AddRemoveTriggerCommandHandlerTest.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,65 @@ | ||
package uk.gov.justice.services.eventstore.management.untrigger.commands; | ||
|
||
import static java.lang.String.format; | ||
import static org.junit.Assert.*; | ||
|
||
import org.junit.runner.RunWith; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.CoreMatchers.sameInstance; | ||
import static org.junit.Assert.assertThat; | ||
import static org.mockito.Mockito.when; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
import org.slf4j.Logger; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.mockito.Mockito.*; | ||
|
||
import uk.gov.justice.services.eventstore.management.untrigger.process.EventLogTriggerManipulator; | ||
import uk.gov.justice.services.jmx.api.command.AddTriggerCommand; | ||
import uk.gov.justice.services.jmx.api.command.RemoveTriggerCommand; | ||
|
||
import javax.inject.Inject; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class AddRemoveTriggerCommandHandlerTest { | ||
|
||
@Mock | ||
private EventLogTriggerManipulator eventLogTriggerManipulator; | ||
|
||
@Mock | ||
private Logger logger; | ||
|
||
@InjectMocks | ||
private AddRemoveTriggerCommandHandler addRemoveTriggerCommandHandler; | ||
|
||
@Test | ||
public void shouldCallTheAddEventLogTriggerProcess() throws Exception { | ||
|
||
final AddTriggerCommand addTriggerCommand = new AddTriggerCommand(); | ||
|
||
addRemoveTriggerCommandHandler.addTriggerToEventLogTable(addTriggerCommand); | ||
|
||
verify(logger).info("Received command ADD_TRIGGER"); | ||
verify(eventLogTriggerManipulator).addTriggerToEventLogTable(); | ||
} | ||
|
||
@Test | ||
public void shouldCallTheRemoveEventLogTriggerProcess() throws Exception { | ||
|
||
final RemoveTriggerCommand removeTriggerCommand = new RemoveTriggerCommand(); | ||
|
||
addRemoveTriggerCommandHandler.removeTriggerFromEventLogTable(removeTriggerCommand); | ||
|
||
verify(logger).info("Received command REMOVE_TRIGGER"); | ||
verify(eventLogTriggerManipulator).removeTriggerFromEventLogTable(); | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
...tice/services/eventstore/management/untrigger/process/EventLogTriggerManipulatorTest.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,91 @@ | ||
package uk.gov.justice.services.eventstore.management.untrigger.process; | ||
|
||
import static java.util.Optional.empty; | ||
import static java.util.Optional.of; | ||
import static org.mockito.Matchers.anyString; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyZeroInteractions; | ||
import static org.mockito.Mockito.when; | ||
|
||
import uk.gov.justice.services.eventsourcing.util.sql.triggers.DatabaseTriggerManipulator; | ||
import uk.gov.justice.services.eventsourcing.util.sql.triggers.TriggerData; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
import org.slf4j.Logger; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class EventLogTriggerManipulatorTest { | ||
|
||
@Mock | ||
private EventStoreTriggerManipulatorProvider eventStoreTriggerManipulatorProvider; | ||
|
||
@Mock | ||
private Logger logger; | ||
|
||
@InjectMocks | ||
private EventLogTriggerManipulator eventLogTriggerManipulator; | ||
|
||
@Test | ||
public void shouldAddTriggerToEventLogTable() throws Exception { | ||
|
||
final DatabaseTriggerManipulator databaseTriggerManipulator = mock(DatabaseTriggerManipulator.class); | ||
|
||
when(eventStoreTriggerManipulatorProvider.getDatabaseTriggerManipulator()).thenReturn(databaseTriggerManipulator); | ||
when(databaseTriggerManipulator.findTriggerOnTable("queue_publish_event", "event_log")).thenReturn(empty()); | ||
|
||
eventLogTriggerManipulator.addTriggerToEventLogTable(); | ||
|
||
verify(databaseTriggerManipulator).addInsertTriggerToTable("queue_publish_event", "event_log", "EXECUTE PROCEDURE update_publish_queue()"); | ||
verify(logger).info("Trigger 'queue_publish_event' successfully added to event_log table"); | ||
} | ||
|
||
@Test | ||
public void shouldNotAddTriggerIfItAlreadyExists() throws Exception { | ||
|
||
final TriggerData triggerData = mock(TriggerData.class); | ||
final DatabaseTriggerManipulator databaseTriggerManipulator = mock(DatabaseTriggerManipulator.class); | ||
|
||
when(eventStoreTriggerManipulatorProvider.getDatabaseTriggerManipulator()).thenReturn(databaseTriggerManipulator); | ||
when(databaseTriggerManipulator.findTriggerOnTable("queue_publish_event", "event_log")).thenReturn(of(triggerData)); | ||
|
||
eventLogTriggerManipulator.addTriggerToEventLogTable(); | ||
|
||
verify(logger).warn("Trigger 'queue_publish_event' already exists on event_log table"); | ||
verify(databaseTriggerManipulator, never()).addInsertTriggerToTable(anyString(), anyString(), anyString()); | ||
} | ||
|
||
@Test | ||
public void shouldRemoveTriggerFromEventLogTable() throws Exception { | ||
|
||
final DatabaseTriggerManipulator databaseTriggerManipulator = mock(DatabaseTriggerManipulator.class); | ||
final TriggerData triggerData = mock(TriggerData.class); | ||
|
||
when(eventStoreTriggerManipulatorProvider.getDatabaseTriggerManipulator()).thenReturn(databaseTriggerManipulator); | ||
when(databaseTriggerManipulator.findTriggerOnTable("queue_publish_event", "event_log")).thenReturn(of(triggerData)); | ||
|
||
eventLogTriggerManipulator.removeTriggerFromEventLogTable(); | ||
|
||
verify(databaseTriggerManipulator).removeTriggerFromTable("queue_publish_event", "event_log"); | ||
verify(logger).info("Removed trigger 'queue_publish_event' from event_log table"); | ||
} | ||
|
||
@Test | ||
public void shouldNotRemoveTriggerIfItNoTriggerExists() throws Exception { | ||
|
||
final DatabaseTriggerManipulator databaseTriggerManipulator = mock(DatabaseTriggerManipulator.class); | ||
|
||
when(eventStoreTriggerManipulatorProvider.getDatabaseTriggerManipulator()).thenReturn(databaseTriggerManipulator); | ||
when(databaseTriggerManipulator.findTriggerOnTable("queue_publish_event", "event_log")).thenReturn(empty()); | ||
|
||
eventLogTriggerManipulator.removeTriggerFromEventLogTable(); | ||
|
||
verify(logger).warn("No trigger named 'queue_publish_event' found on event_log table"); | ||
verify(databaseTriggerManipulator, never()).removeTriggerFromTable(anyString(), anyString()); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...ces/eventstore/management/untrigger/process/EventStoreTriggerManipulatorProviderTest.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,54 @@ | ||
package uk.gov.justice.services.eventstore.management.untrigger.process; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import org.junit.runner.RunWith; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.CoreMatchers.sameInstance; | ||
import static org.junit.Assert.assertThat; | ||
import static org.mockito.Mockito.when; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.mockito.Mockito.*; | ||
|
||
import uk.gov.justice.services.eventsourcing.source.core.EventStoreDataSourceProvider; | ||
import uk.gov.justice.services.eventsourcing.util.sql.triggers.DatabaseTriggerManipulator; | ||
import uk.gov.justice.services.eventsourcing.util.sql.triggers.DatabaseTriggerManipulatorFactory; | ||
|
||
import javax.inject.Inject; | ||
import javax.sql.DataSource; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class EventStoreTriggerManipulatorProviderTest { | ||
|
||
@Mock | ||
private EventStoreDataSourceProvider eventStoreDataSourceProvider; | ||
|
||
@Mock | ||
public DatabaseTriggerManipulatorFactory databaseTriggerManipulatorFactory; | ||
|
||
@InjectMocks | ||
private EventStoreTriggerManipulatorProvider eventStoreTriggerManipulatorProvider; | ||
|
||
@Test | ||
public void shouldCreateATriggerManipulaterWithTheCorrectEventStoreDataSource() throws Exception { | ||
|
||
final DataSource eventStoreDataSource = mock(DataSource.class); | ||
final DatabaseTriggerManipulator databaseTriggerManipulator = mock(DatabaseTriggerManipulator.class); | ||
|
||
when(eventStoreDataSourceProvider.getDefaultDataSource()).thenReturn(eventStoreDataSource); | ||
when(databaseTriggerManipulatorFactory.databaseTriggerManipulator(eventStoreDataSource)).thenReturn(databaseTriggerManipulator); | ||
|
||
assertThat(eventStoreTriggerManipulatorProvider.getDatabaseTriggerManipulator(), is(databaseTriggerManipulator)); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...services/eventstore/management/validation/commands/ValidateCatchupCommandHandlerTest.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,30 @@ | ||
package uk.gov.justice.services.eventstore.management.validation.commands; | ||
|
||
import static org.mockito.Mockito.verify; | ||
|
||
import uk.gov.justice.services.jmx.api.command.ValidateCatchupCommand; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
import org.slf4j.Logger; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class ValidateCatchupCommandHandlerTest { | ||
|
||
@Mock | ||
private Logger logger; | ||
|
||
@InjectMocks | ||
private ValidateCatchupCommandHandler validateCatchupCommandHandler; | ||
|
||
@Test | ||
public void shouldLogNotImplemented() throws Exception { | ||
|
||
validateCatchupCommandHandler.validateCatchup(new ValidateCatchupCommand()); | ||
|
||
verify(logger).warn("Command VALIDATE_CATCHUP not yet implemented"); | ||
} | ||
} |
Oops, something went wrong.