-
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
8 changed files
with
180 additions
and
24 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
103 changes: 103 additions & 0 deletions
103
...le-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/EventErrorHandlingIT.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,103 @@ | ||
package uk.gov.justice.services.example.cakeshop.it; | ||
|
||
import static java.util.Optional.empty; | ||
import static java.util.Optional.of; | ||
import static java.util.UUID.randomUUID; | ||
import static javax.json.Json.createArrayBuilder; | ||
import static javax.json.Json.createObjectBuilder; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.junit.Assert.fail; | ||
import static uk.gov.justice.services.messaging.JsonEnvelope.envelopeFrom; | ||
import static uk.gov.justice.services.messaging.JsonEnvelope.metadataBuilder; | ||
|
||
import uk.gov.justice.services.example.cakeshop.it.helpers.EventErrorLogRepositoryFactory; | ||
import uk.gov.justice.services.example.cakeshop.it.helpers.EventSender; | ||
import uk.gov.justice.services.messaging.JsonEnvelope; | ||
import uk.gov.justice.services.system.domain.EventError; | ||
import uk.gov.justice.services.system.persistence.EventErrorLogRepository; | ||
import uk.gov.justice.services.test.utils.core.messaging.Poller; | ||
import uk.gov.justice.services.test.utils.persistence.DatabaseCleaner; | ||
import uk.gov.justice.services.test.utils.persistence.TestJdbcDataSourceProvider; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
import javax.json.JsonObject; | ||
import javax.sql.DataSource; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class EventErrorHandlingIT { | ||
|
||
private static final String TOPIC_NAME = "example.event"; | ||
|
||
private final EventSender eventSender = new EventSender(); | ||
|
||
private final DataSource systemDataSource = new TestJdbcDataSourceProvider().getSystemDataSource("framework"); | ||
private final EventErrorLogRepositoryFactory eventErrorLogRepositoryFactory = new EventErrorLogRepositoryFactory(); | ||
|
||
private final DatabaseCleaner databaseCleaner = new DatabaseCleaner(); | ||
|
||
private final Poller poller = new Poller(); | ||
|
||
@Before | ||
public void cleanDatabase() { | ||
databaseCleaner.cleanSystemTables("framework"); | ||
} | ||
|
||
@Test | ||
public void shouldStoreEventProcessingErrorsInTheDatabase() throws Exception { | ||
|
||
final UUID recipeId = randomUUID(); | ||
final UUID eventId = randomUUID(); | ||
final String eventName = "example.events.recipe-added"; | ||
|
||
final JsonObject jsonObject = createObjectBuilder() | ||
.add("recipeId", recipeId.toString()) | ||
.add("name", "Chocolate muffin") | ||
.add("glutenFree", true) | ||
.add("ingredients", createArrayBuilder() | ||
.add(createObjectBuilder() | ||
.add("name", "someIngredient") | ||
.add("quantity", 1) | ||
).build() | ||
).build(); | ||
|
||
final JsonEnvelope eventWithMissingEventNumberEnvelope = envelopeFrom( | ||
metadataBuilder() | ||
.withId(eventId) | ||
.withName(eventName) | ||
.withStreamId(recipeId) | ||
.withPosition(1L) | ||
.build(), | ||
jsonObject); | ||
|
||
assertThat(eventWithMissingEventNumberEnvelope.metadata().eventNumber(), is(empty())); | ||
|
||
eventSender.sendToTopic(eventWithMissingEventNumberEnvelope, TOPIC_NAME); | ||
|
||
final EventErrorLogRepository eventErrorLogRepository = eventErrorLogRepositoryFactory.create(systemDataSource); | ||
|
||
final Optional<EventError> eventError = poller.pollUntilFound(() -> { | ||
final List<EventError> eventErrors = eventErrorLogRepository.findAll(); | ||
|
||
if (eventErrors.size() > 0) { | ||
return of(eventErrors.get(0)); | ||
} | ||
|
||
return empty(); | ||
}); | ||
|
||
if (eventError.isPresent()) { | ||
assertThat(eventError.get().getEventId(), is(eventId)); | ||
assertThat(eventError.get().getEventName(), is(eventName)); | ||
assertThat(eventError.get().getEventNumber(), is(empty())); | ||
assertThat(eventError.get().getComponent(), is("EVENT_LISTENER")); | ||
} else { | ||
fail(); | ||
} | ||
} | ||
} |
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
31 changes: 31 additions & 0 deletions
31
...a/uk/gov/justice/services/example/cakeshop/it/helpers/EventErrorLogRepositoryFactory.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,31 @@ | ||
package uk.gov.justice.services.example.cakeshop.it.helpers; | ||
|
||
import static org.mockito.Mockito.when; | ||
import static org.mockito.MockitoAnnotations.initMocks; | ||
|
||
import uk.gov.justice.services.jdbc.persistence.SystemJdbcDataSourceProvider; | ||
import uk.gov.justice.services.system.persistence.EventErrorLogRepository; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
|
||
public class EventErrorLogRepositoryFactory { | ||
|
||
@Mock | ||
private SystemJdbcDataSourceProvider systemJdbcDataSourceProvider; | ||
|
||
@InjectMocks | ||
private EventErrorLogRepository eventErrorLogRepository; | ||
|
||
|
||
public EventErrorLogRepository create(final DataSource systemDataSource) { | ||
|
||
initMocks(this); | ||
|
||
when(systemJdbcDataSourceProvider.getDataSource()).thenReturn(systemDataSource); | ||
|
||
return eventErrorLogRepository; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...ple-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/helpers/EventSender.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,31 @@ | ||
package uk.gov.justice.services.example.cakeshop.it.helpers; | ||
|
||
import uk.gov.justice.services.messaging.JsonEnvelope; | ||
|
||
import javax.jms.JMSException; | ||
import javax.jms.MessageProducer; | ||
import javax.jms.Session; | ||
import javax.jms.TextMessage; | ||
import javax.jms.Topic; | ||
|
||
public class EventSender { | ||
|
||
private final JmsBootstrapper jmsBootstrapper = new JmsBootstrapper(); | ||
|
||
public void sendToTopic(final JsonEnvelope jsonEnvelope, final String topicName) throws JMSException { | ||
try (final Session jmsSession = jmsBootstrapper.jmsSession()) { | ||
final Topic topic = jmsSession.createTopic(topicName); | ||
|
||
try (final MessageProducer producer = jmsSession.createProducer(topic);) { | ||
|
||
@SuppressWarnings("deprecation") final String json = jsonEnvelope.toDebugStringPrettyPrint(); | ||
final TextMessage message = jmsSession.createTextMessage(); | ||
|
||
message.setText(json); | ||
message.setStringProperty("CPPNAME", jsonEnvelope.metadata().name()); | ||
|
||
producer.send(message); | ||
} | ||
} | ||
} | ||
} |
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