Skip to content
This repository was archived by the owner on Aug 13, 2020. It is now read-only.

Commit d3b64d9

Browse files
amckenziemapingo
authored andcommitted
Storing of commands whilst shuttered is now idempotent
1 parent 54a505a commit d3b64d9

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ on [Keep a CHANGELOG](http://keepachangelog.com/). This project adheres to
55

66
## [Unreleased]
77

8+
## [6.0.17] - 2019-10-04
9+
### Changed
10+
- Storing of Commands whilst shuttered is now idempotent
11+
812
## [6.0.16] - 2018-09-23
913
### Changed
1014
- Renamed ValidateCatchupCommand to VerifyCatchupCommand

framework-system/shuttering-persistence/src/main/java/uk/gov/justice/services/shuttering/persistence/StoredCommandRepository.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,15 @@
1919
import javax.inject.Inject;
2020
import javax.sql.DataSource;
2121

22+
import org.slf4j.Logger;
23+
2224
public class StoredCommandRepository {
2325

2426
private static final String SELECT_STORED_COMMAND_SQL = "SELECT envelope_id, command_json_envelope, destination, date_received FROM stored_command";
25-
private static final String INSERT_STORED_COMMAND_SQL = "INSERT into stored_command (envelope_id, command_json_envelope, destination, date_received) VALUES (?, ?, ?, ?)";
27+
private static final String INSERT_STORED_COMMAND_SQL = "INSERT into stored_command " +
28+
"(envelope_id, command_json_envelope, destination, date_received) " +
29+
"VALUES (?, ?, ?, ?) " +
30+
"ON CONFLICT DO NOTHING";
2631
private static final String TRUNCATE_STORED_COMMAND_SQL = "TRUNCATE stored_command";
2732
private static final String DELETE_STORED_COMMAND_SQL = "DELETE FROM stored_command WHERE envelope_id = ?";
2833

@@ -35,6 +40,9 @@ public class StoredCommandRepository {
3540
@Inject
3641
private JdbcResultSetStreamer jdbcResultSetStreamer;
3742

43+
@Inject
44+
private Logger logger;
45+
3846
public Stream<StoredCommand> streamStoredCommands() {
3947

4048
final DataSource dataSource = systemJdbcDataSourceProvider.getDataSource();
@@ -76,7 +84,11 @@ public void save(final StoredCommand storedCommand) {
7684
preparedStatement.setString(2, storedCommand.getCommandJsonEnvelope());
7785
preparedStatement.setString(3, storedCommand.getDestination());
7886
preparedStatement.setTimestamp(4, toSqlTimestamp(storedCommand.getDateReceived()));
79-
preparedStatement.executeUpdate();
87+
final int rowsChanged = preparedStatement.executeUpdate();
88+
89+
if (rowsChanged == 0) {
90+
logger.warn(format("Command with id '%s' not inserted into stored_command table. Command with that id already exists", storedCommand.getEnvelopeId()));
91+
}
8092

8193
} catch (final SQLException e) {
8294
throw new StoredCommandPersistenceException("Failed to insert stored command", e);

framework-system/shuttering-persistence/src/test/java/uk/gov/justice/services/shuttering/persistence/StoredCommandRepositoryIT.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package uk.gov.justice.services.shuttering.persistence;
22

3+
import static java.util.UUID.fromString;
34
import static java.util.UUID.randomUUID;
45
import static java.util.stream.Collectors.toList;
56
import static org.hamcrest.CoreMatchers.hasItem;
67
import static org.hamcrest.CoreMatchers.is;
78
import static org.junit.Assert.assertThat;
9+
import static org.mockito.Mockito.verify;
810
import static org.mockito.Mockito.when;
911

1012
import uk.gov.justice.services.common.util.UtcClock;
@@ -16,6 +18,7 @@
1618

1719
import java.time.ZonedDateTime;
1820
import java.util.List;
21+
import java.util.UUID;
1922
import java.util.stream.Stream;
2023

2124
import javax.sql.DataSource;
@@ -26,6 +29,7 @@
2629
import org.mockito.Mock;
2730
import org.mockito.Spy;
2831
import org.mockito.runners.MockitoJUnitRunner;
32+
import org.slf4j.Logger;
2933

3034
@RunWith(MockitoJUnitRunner.class)
3135
public class StoredCommandRepositoryIT {
@@ -39,6 +43,9 @@ public class StoredCommandRepositoryIT {
3943
@Spy
4044
private JdbcResultSetStreamer jdbcResultSetStreamer = new JdbcResultSetStreamer();
4145

46+
@Mock
47+
private Logger logger;
48+
4249
@InjectMocks
4350
private StoredCommandRepository storedCommandRepository;
4451

@@ -110,4 +117,31 @@ public void shouldDeleteShutteredCommand() throws Exception {
110117
assertThat(storedCommands, hasItem(storedCommand_1));
111118
assertThat(storedCommands, hasItem(storedCommand_3));
112119
}
120+
121+
@Test
122+
public void shouldBeIdempotentWhenSaving() throws Exception {
123+
124+
final DataSource systemDataSource = new TestJdbcDataSourceProvider().getSystemDataSource("framework");
125+
126+
when(systemJdbcDataSourceProvider.getDataSource()).thenReturn(systemDataSource);
127+
128+
storedCommandRepository.deleteAll();
129+
130+
final UUID envelopeId = fromString("0b305815-f251-481d-9a69-2ff60c3a88ca");
131+
final StoredCommand storedCommand = new StoredCommand(envelopeId, "command envelope 1", "destination 1", new UtcClock().now());
132+
133+
storedCommandRepository.save(storedCommand);
134+
135+
136+
final Stream<StoredCommand> secondShutteredCommandStream = storedCommandRepository.streamStoredCommands();
137+
final List<StoredCommand> storedCommands = secondShutteredCommandStream.collect(toList());
138+
139+
assertThat(storedCommands.size(), is(1));
140+
141+
assertThat(storedCommands, hasItem(storedCommand));
142+
143+
storedCommandRepository.save(storedCommand);
144+
145+
verify(logger).warn("Command with id '0b305815-f251-481d-9a69-2ff60c3a88ca' not inserted into stored_command table. Command with that id already exists");
146+
}
113147
}

0 commit comments

Comments
 (0)