From bdcdae862d7bb846fee967e19a099cdf084e7edc Mon Sep 17 00:00:00 2001 From: dartpopikyardo Date: Tue, 22 Mar 2016 01:09:39 +0300 Subject: [PATCH] -added tests for new endpoints --- .../accounts/AccountTransactionInfo.java | 16 +++++++ .../examples/bank/web/EndToEndTest.java | 47 ++++++++++++++++++ .../web/BankingWebIntegrationTest.java | 48 ++++++++++++++++++- 3 files changed, 110 insertions(+), 1 deletion(-) diff --git a/java-spring/accounts-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountTransactionInfo.java b/java-spring/accounts-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountTransactionInfo.java index bdc0c01..c617635 100644 --- a/java-spring/accounts-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountTransactionInfo.java +++ b/java-spring/accounts-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountTransactionInfo.java @@ -1,5 +1,8 @@ package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.accounts; +import org.apache.commons.lang.builder.EqualsBuilder; +import org.apache.commons.lang.builder.HashCodeBuilder; + public class AccountTransactionInfo { private String transactionId; @@ -7,6 +10,9 @@ public class AccountTransactionInfo { private String toAccountId; private long amount; + public AccountTransactionInfo() { + } + public AccountTransactionInfo(String transactionId, String fromAccountId, String toAccountId, long amount) { this.transactionId = transactionId; this.fromAccountId = fromAccountId; @@ -45,4 +51,14 @@ public class AccountTransactionInfo { public void setAmount(long amount) { this.amount = amount; } + + @Override + public boolean equals(Object o) { + return EqualsBuilder.reflectionEquals(this, o); + } + + @Override + public int hashCode() { + return HashCodeBuilder.reflectionHashCode(this); + } } diff --git a/java-spring/e2e-test/src/test/java/net/chrisrichardson/eventstore/examples/bank/web/EndToEndTest.java b/java-spring/e2e-test/src/test/java/net/chrisrichardson/eventstore/examples/bank/web/EndToEndTest.java index 1f22c81..d21e936 100644 --- a/java-spring/e2e-test/src/test/java/net/chrisrichardson/eventstore/examples/bank/web/EndToEndTest.java +++ b/java-spring/e2e-test/src/test/java/net/chrisrichardson/eventstore/examples/bank/web/EndToEndTest.java @@ -1,6 +1,7 @@ package net.chrisrichardson.eventstore.examples.bank.web; +import net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.accounts.AccountTransactionInfo; import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerResponse; import net.chrisrichardson.eventstore.javaexamples.banking.commonauth.utils.BasicAuthUtils; @@ -15,6 +16,8 @@ import net.chrisrichardson.eventstorestore.javaexamples.testutil.Verifier; import net.chrisrichardson.eventstorestore.javaexamples.testutil.customers.CustomersTestUtils; import org.junit.Assert; import org.junit.Test; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.HttpEntity; import org.springframework.http.HttpMethod; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; @@ -22,9 +25,11 @@ import org.springframework.web.client.RestTemplate; import rx.Observable; import java.math.BigDecimal; +import java.util.List; import static net.chrisrichardson.eventstorestore.javaexamples.testutil.TestUtil.eventually; import static net.chrisrichardson.eventstorestore.javaexamples.testutil.customers.CustomersTestUtils.generateCustomerInfo; +import static org.junit.Assert.assertTrue; public class EndToEndTest { @@ -122,6 +127,48 @@ public class EndToEndTest { assertAccountBalance(toAccountId, finalToAccountBalance); // TOOD - check state of money transfer + + List transactionInfoList = restTemplate.exchange(accountsQuerySideBaseUrl("/accounts/"+fromAccountId+"/history"), + HttpMethod.GET, + new HttpEntity(BasicAuthUtils.basicAuthHeaders("test_user@mail.com")), + new ParameterizedTypeReference>() {}).getBody(); + + AccountTransactionInfo expectedTransactionInfo = new AccountTransactionInfo(moneyTransfer.getMoneyTransferId(), fromAccountId, toAccountId, toCents(amountToTransfer).longValue()); + + assertTrue(transactionInfoList.contains(expectedTransactionInfo)); + } + + @Test + public void shouldCreateAccountsAndGetByCustomer() { + BigDecimal initialFromAccountBalance = new BigDecimal(500); + CustomerInfo customerInfo = generateCustomerInfo(); + + final CustomerResponse customerResponse = restTemplate.postForEntity(customersCommandSideBaseUrl("/customers"), customerInfo, CustomerResponse.class).getBody(); + final String customerId = customerResponse.getId(); + + Assert.assertNotNull(customerId); + Assert.assertEquals(customerInfo, customerResponse.getCustomerInfo()); + + customersTestUtils.assertCustomerResponse(customerId, customerInfo); + + final CreateAccountResponse account = BasicAuthUtils.doBasicAuthenticatedRequest(restTemplate, + accountsCommandSideBaseUrl("/accounts"), + HttpMethod.POST, + CreateAccountResponse.class, + new CreateAccountRequest(customerId, "My 1 Account", "", initialFromAccountBalance) + ); + final String accountId = account.getAccountId(); + + Assert.assertNotNull(accountId); + + assertAccountBalance(accountId, initialFromAccountBalance); + + List accountResponseList = restTemplate.exchange(accountsQuerySideBaseUrl("/accounts?customerId="+customerId), + HttpMethod.GET, + new HttpEntity(BasicAuthUtils.basicAuthHeaders("test_user@mail.com")), + new ParameterizedTypeReference>() {}).getBody(); + + assertTrue(accountResponseList.stream().filter(acc -> acc.getAccountId().equals(accountId)).findFirst().isPresent()); } private BigDecimal toCents(BigDecimal dollarAmount) { diff --git a/java-spring/monolithic-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/BankingWebIntegrationTest.java b/java-spring/monolithic-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/BankingWebIntegrationTest.java index 70352bc..a3276f9 100644 --- a/java-spring/monolithic-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/BankingWebIntegrationTest.java +++ b/java-spring/monolithic-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/BankingWebIntegrationTest.java @@ -1,5 +1,6 @@ package net.chrisrichardson.eventstore.javaexamples.banking.web; +import net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.accounts.AccountTransactionInfo; import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.*; import net.chrisrichardson.eventstore.javaexamples.banking.commonauth.utils.BasicAuthUtils; import net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.accounts.CreateAccountRequest; @@ -17,6 +18,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.HttpEntity; import org.springframework.http.HttpMethod; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; @@ -25,10 +28,12 @@ import rx.Observable; import javax.annotation.PostConstruct; import java.math.BigDecimal; +import java.util.List; import static net.chrisrichardson.eventstorestore.javaexamples.testutil.TestUtil.eventually; import static net.chrisrichardson.eventstorestore.javaexamples.testutil.customers.CustomersTestUtils.generateCustomerInfo; import static net.chrisrichardson.eventstorestore.javaexamples.testutil.customers.CustomersTestUtils.generateToAccountInfo; +import static org.junit.Assert.assertTrue; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = BankingWebTestConfiguration.class) @@ -97,6 +102,47 @@ public class BankingWebIntegrationTest { assertAccountBalance(fromAccountId, finalFromAccountBalance); assertAccountBalance(toAccountId, finalToAccountBalance); + List transactionInfoList = restTemplate.exchange(baseUrl("/accounts/"+fromAccountId+"/history"), + HttpMethod.GET, + new HttpEntity(BasicAuthUtils.basicAuthHeaders("test_user@mail.com")), + new ParameterizedTypeReference>() {}).getBody(); + + AccountTransactionInfo expectedTransactionInfo = new AccountTransactionInfo(moneyTransfer.getMoneyTransferId(), fromAccountId, toAccountId, toCents(amountToTransfer).longValue()); + + assertTrue(transactionInfoList.contains(expectedTransactionInfo)); + } + + @Test + public void shouldCreateAccountsAndGetByCustomer() { + BigDecimal initialFromAccountBalance = new BigDecimal(500); + CustomerInfo customerInfo = generateCustomerInfo(); + + final CustomerResponse customerResponse = restTemplate.postForEntity(baseUrl("/customers"), customerInfo, CustomerResponse.class).getBody(); + final String customerId = customerResponse.getId(); + + Assert.assertNotNull(customerId); + Assert.assertEquals(customerInfo, customerResponse.getCustomerInfo()); + + customersTestUtils.assertCustomerResponse(customerId, customerInfo); + + final CreateAccountResponse account = BasicAuthUtils.doBasicAuthenticatedRequest(restTemplate, + baseUrl("/accounts"), + HttpMethod.POST, + CreateAccountResponse.class, + new CreateAccountRequest(customerId, "My 1 Account", "", initialFromAccountBalance) + ); + final String accountId = account.getAccountId(); + + Assert.assertNotNull(accountId); + + assertAccountBalance(accountId, initialFromAccountBalance); + + List accountResponseList = restTemplate.exchange(baseUrl("/accounts?customerId="+customerId), + HttpMethod.GET, + new HttpEntity(BasicAuthUtils.basicAuthHeaders("test_user@mail.com")), + new ParameterizedTypeReference>() {}).getBody(); + + assertTrue(accountResponseList.stream().filter(acc -> acc.getAccountId().equals(accountId)).findFirst().isPresent()); } @Test @@ -163,7 +209,7 @@ public class BankingWebIntegrationTest { @Override public void verify(QuerySideCustomer customerResponse) { Assert.assertEquals(customerId, customerResponse.getId()); - Assert.assertTrue(customerResponse.getToAccounts().values().stream().anyMatch(t -> t.equals(toAccountInfo))); + assertTrue(customerResponse.getToAccounts().values().stream().anyMatch(t -> t.equals(toAccountInfo))); } }); }