Skip to content

Commit

Permalink
Merge pull request #978 from alphagov/pp_7276_pact_test
Browse files Browse the repository at this point in the history
PP-7276 Add user email collected pact test
  • Loading branch information
kbottla authored Oct 15, 2020
2 parents 54c1b42 + ec18685 commit 8c58d47
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public static TestSuite suite() {
consumerToJUnitTest.put("connector", new JUnit4TestAdapter(PaymentIncludedInPayoutEventQueueContractTest.class));
consumerToJUnitTest.put("connector", new JUnit4TestAdapter(RefundIncludedInPayoutEventQueueContractTest.class));
consumerToJUnitTest.put("connector", new JUnit4TestAdapter(CancelledByUserEventQueueContractTest.class));
consumerToJUnitTest.put("connector", new JUnit4TestAdapter(UserEmailCollectedEventQueueContractTest.class));
return CreateTestSuite.create(consumerToJUnitTest.build());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package uk.gov.pay.ledger.pact;

import au.com.dius.pact.consumer.MessagePactBuilder;
import au.com.dius.pact.consumer.MessagePactProviderRule;
import au.com.dius.pact.consumer.Pact;
import au.com.dius.pact.consumer.PactVerification;
import au.com.dius.pact.model.v3.messaging.MessagePact;
import org.junit.Rule;
import org.junit.Test;
import uk.gov.pay.ledger.rule.AppWithPostgresAndSqsRule;
import uk.gov.pay.ledger.rule.SqsTestDocker;
import uk.gov.pay.ledger.transaction.dao.TransactionDao;
import uk.gov.pay.ledger.transaction.entity.TransactionEntity;
import uk.gov.pay.ledger.util.fixture.QueuePaymentEventFixture;

import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

import static io.dropwizard.testing.ConfigOverride.config;
import static org.awaitility.Awaitility.await;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static uk.gov.pay.ledger.util.fixture.QueuePaymentEventFixture.aQueuePaymentEventFixture;

public class UserEmailCollectedEventQueueContractTest {
@Rule
public MessagePactProviderRule mockProvider = new MessagePactProviderRule(this);

@Rule
public AppWithPostgresAndSqsRule appRule = new AppWithPostgresAndSqsRule(
config("queueMessageReceiverConfig.backgroundProcessingEnabled", "true")
);

private byte[] currentMessage;
private String externalId = "userEmailCol_externalId";
private ZonedDateTime eventDate = ZonedDateTime.parse("2018-03-12T16:25:01.123456Z");

@Pact(provider = "connector", consumer = "ledger")
public MessagePact createUserEmailCollectedEventPact(MessagePactBuilder builder) {
String userEmailCollectedEvent = "USER_EMAIL_COLLECTED";
QueuePaymentEventFixture paymentDetailsEntered = aQueuePaymentEventFixture()
.withResourceExternalId(externalId)
.withEventDate(eventDate)
.withEventType(userEmailCollectedEvent)
.withDefaultEventDataForEventType(userEmailCollectedEvent);

Map<String, String> metadata = new HashMap<>();
metadata.put("contentType", "application/json");

return builder
.expectsToReceive("a user email collected message")
.withMetadata(metadata)
.withContent(paymentDetailsEntered.getAsPact())
.toPact();
}

@Test
@PactVerification({"connector"})
public void test() {
TransactionDao transactionDao = new TransactionDao(appRule.getJdbi());

appRule.getSqsClient().sendMessage(SqsTestDocker.getQueueUrl("event-queue"), new String(currentMessage));

await().atMost(1, TimeUnit.SECONDS).until(
() -> transactionDao.findTransactionByExternalId(externalId).isPresent()
&& transactionDao.findTransactionByExternalId(externalId).get().getEmail() != null
);

Optional<TransactionEntity> transaction = transactionDao.findTransactionByExternalId(externalId);

assertThat(transaction.isPresent(), is(true));
assertThat(transaction.get().getExternalId(), is(externalId));
assertThat(transaction.get().getEmail(), is("[email protected]"));
}

public void setMessage(byte[] messageContents) {
currentMessage = messageContents;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ public QueuePaymentEventFixture withDefaultEventDataForEventType(String eventTyp
case "CANCELLED_BY_USER":
eventData = gsonBuilder.create().toJson(Map.of("gateway_transaction_id", "validGatewayTransactionId"));
break;
case "USER_EMAIL_COLLECTED":
eventData = gsonBuilder.create().toJson(Map.of("email", "[email protected]"));
break;
default:
eventData = gsonBuilder.create().toJson(Map.of("event_data", "event_data"));
}
Expand Down

0 comments on commit 8c58d47

Please sign in to comment.