|
| 1 | +package uk.gov.pay.ledger.pact; |
| 2 | + |
| 3 | +import au.com.dius.pact.consumer.MessagePactBuilder; |
| 4 | +import au.com.dius.pact.consumer.MessagePactProviderRule; |
| 5 | +import au.com.dius.pact.consumer.Pact; |
| 6 | +import au.com.dius.pact.consumer.PactVerification; |
| 7 | +import au.com.dius.pact.model.v3.messaging.MessagePact; |
| 8 | +import org.junit.Rule; |
| 9 | +import org.junit.Test; |
| 10 | +import uk.gov.pay.ledger.rule.AppWithPostgresAndSqsRule; |
| 11 | +import uk.gov.pay.ledger.rule.SqsTestDocker; |
| 12 | +import uk.gov.pay.ledger.transaction.dao.TransactionDao; |
| 13 | +import uk.gov.pay.ledger.transaction.entity.TransactionEntity; |
| 14 | +import uk.gov.pay.ledger.util.fixture.QueuePaymentEventFixture; |
| 15 | + |
| 16 | +import java.time.ZonedDateTime; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.Map; |
| 19 | +import java.util.Optional; |
| 20 | +import java.util.concurrent.TimeUnit; |
| 21 | + |
| 22 | +import static io.dropwizard.testing.ConfigOverride.config; |
| 23 | +import static org.awaitility.Awaitility.await; |
| 24 | +import static org.hamcrest.CoreMatchers.is; |
| 25 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 26 | +import static uk.gov.pay.ledger.util.fixture.QueuePaymentEventFixture.aQueuePaymentEventFixture; |
| 27 | + |
| 28 | +public class UserEmailCollectedEventQueueContractTest { |
| 29 | + @Rule |
| 30 | + public MessagePactProviderRule mockProvider = new MessagePactProviderRule(this); |
| 31 | + |
| 32 | + @Rule |
| 33 | + public AppWithPostgresAndSqsRule appRule = new AppWithPostgresAndSqsRule( |
| 34 | + config("queueMessageReceiverConfig.backgroundProcessingEnabled", "true") |
| 35 | + ); |
| 36 | + |
| 37 | + private byte[] currentMessage; |
| 38 | + private String externalId = "userEmailCol_externalId"; |
| 39 | + private ZonedDateTime eventDate = ZonedDateTime.parse("2018-03-12T16:25:01.123456Z"); |
| 40 | + |
| 41 | + @Pact(provider = "connector", consumer = "ledger") |
| 42 | + public MessagePact createUserEmailCollectedEventPact(MessagePactBuilder builder) { |
| 43 | + String userEmailCollectedEvent = "USER_EMAIL_COLLECTED"; |
| 44 | + QueuePaymentEventFixture paymentDetailsEntered = aQueuePaymentEventFixture() |
| 45 | + .withResourceExternalId(externalId) |
| 46 | + .withEventDate(eventDate) |
| 47 | + .withEventType(userEmailCollectedEvent) |
| 48 | + .withDefaultEventDataForEventType(userEmailCollectedEvent); |
| 49 | + |
| 50 | + Map<String, String> metadata = new HashMap<>(); |
| 51 | + metadata.put("contentType", "application/json"); |
| 52 | + |
| 53 | + return builder |
| 54 | + .expectsToReceive("a user email collected message") |
| 55 | + .withMetadata(metadata) |
| 56 | + .withContent(paymentDetailsEntered.getAsPact()) |
| 57 | + .toPact(); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + @PactVerification({"connector"}) |
| 62 | + public void test() { |
| 63 | + TransactionDao transactionDao = new TransactionDao(appRule.getJdbi()); |
| 64 | + |
| 65 | + appRule.getSqsClient().sendMessage(SqsTestDocker.getQueueUrl("event-queue"), new String(currentMessage)); |
| 66 | + |
| 67 | + await().atMost(1, TimeUnit.SECONDS).until( |
| 68 | + () -> transactionDao.findTransactionByExternalId(externalId).isPresent() |
| 69 | + && transactionDao.findTransactionByExternalId(externalId).get().getEmail() != null |
| 70 | + ); |
| 71 | + |
| 72 | + Optional<TransactionEntity> transaction = transactionDao.findTransactionByExternalId(externalId); |
| 73 | + |
| 74 | + assertThat(transaction.isPresent(), is(true)); |
| 75 | + assertThat(transaction.get().getExternalId(), is(externalId)); |
| 76 | + assertThat(transaction.get().getEmail(), is("test@example.org")); |
| 77 | + } |
| 78 | + |
| 79 | + public void setMessage(byte[] messageContents) { |
| 80 | + currentMessage = messageContents; |
| 81 | + } |
| 82 | +} |
0 commit comments