-added tests for new endpoints
This commit is contained in:
@@ -1,5 +1,8 @@
|
|||||||
package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.accounts;
|
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 {
|
public class AccountTransactionInfo {
|
||||||
|
|
||||||
private String transactionId;
|
private String transactionId;
|
||||||
@@ -7,6 +10,9 @@ public class AccountTransactionInfo {
|
|||||||
private String toAccountId;
|
private String toAccountId;
|
||||||
private long amount;
|
private long amount;
|
||||||
|
|
||||||
|
public AccountTransactionInfo() {
|
||||||
|
}
|
||||||
|
|
||||||
public AccountTransactionInfo(String transactionId, String fromAccountId, String toAccountId, long amount) {
|
public AccountTransactionInfo(String transactionId, String fromAccountId, String toAccountId, long amount) {
|
||||||
this.transactionId = transactionId;
|
this.transactionId = transactionId;
|
||||||
this.fromAccountId = fromAccountId;
|
this.fromAccountId = fromAccountId;
|
||||||
@@ -45,4 +51,14 @@ public class AccountTransactionInfo {
|
|||||||
public void setAmount(long amount) {
|
public void setAmount(long amount) {
|
||||||
this.amount = amount;
|
this.amount = amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
return EqualsBuilder.reflectionEquals(this, o);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return HashCodeBuilder.reflectionHashCode(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package net.chrisrichardson.eventstore.examples.bank.web;
|
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.CustomerInfo;
|
||||||
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerResponse;
|
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerResponse;
|
||||||
import net.chrisrichardson.eventstore.javaexamples.banking.commonauth.utils.BasicAuthUtils;
|
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 net.chrisrichardson.eventstorestore.javaexamples.testutil.customers.CustomersTestUtils;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.springframework.core.ParameterizedTypeReference;
|
||||||
|
import org.springframework.http.HttpEntity;
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.http.converter.HttpMessageConverter;
|
import org.springframework.http.converter.HttpMessageConverter;
|
||||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||||
@@ -22,9 +25,11 @@ import org.springframework.web.client.RestTemplate;
|
|||||||
import rx.Observable;
|
import rx.Observable;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import static net.chrisrichardson.eventstorestore.javaexamples.testutil.TestUtil.eventually;
|
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.generateCustomerInfo;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
public class EndToEndTest {
|
public class EndToEndTest {
|
||||||
|
|
||||||
@@ -122,6 +127,48 @@ public class EndToEndTest {
|
|||||||
assertAccountBalance(toAccountId, finalToAccountBalance);
|
assertAccountBalance(toAccountId, finalToAccountBalance);
|
||||||
|
|
||||||
// TOOD - check state of money transfer
|
// TOOD - check state of money transfer
|
||||||
|
|
||||||
|
List<AccountTransactionInfo> transactionInfoList = restTemplate.exchange(accountsQuerySideBaseUrl("/accounts/"+fromAccountId+"/history"),
|
||||||
|
HttpMethod.GET,
|
||||||
|
new HttpEntity(BasicAuthUtils.basicAuthHeaders("test_user@mail.com")),
|
||||||
|
new ParameterizedTypeReference<List<AccountTransactionInfo>>() {}).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<GetAccountResponse> accountResponseList = restTemplate.exchange(accountsQuerySideBaseUrl("/accounts?customerId="+customerId),
|
||||||
|
HttpMethod.GET,
|
||||||
|
new HttpEntity(BasicAuthUtils.basicAuthHeaders("test_user@mail.com")),
|
||||||
|
new ParameterizedTypeReference<List<GetAccountResponse>>() {}).getBody();
|
||||||
|
|
||||||
|
assertTrue(accountResponseList.stream().filter(acc -> acc.getAccountId().equals(accountId)).findFirst().isPresent());
|
||||||
}
|
}
|
||||||
|
|
||||||
private BigDecimal toCents(BigDecimal dollarAmount) {
|
private BigDecimal toCents(BigDecimal dollarAmount) {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package net.chrisrichardson.eventstore.javaexamples.banking.web;
|
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.common.customers.*;
|
||||||
import net.chrisrichardson.eventstore.javaexamples.banking.commonauth.utils.BasicAuthUtils;
|
import net.chrisrichardson.eventstore.javaexamples.banking.commonauth.utils.BasicAuthUtils;
|
||||||
import net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.accounts.CreateAccountRequest;
|
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.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.test.IntegrationTest;
|
import org.springframework.boot.test.IntegrationTest;
|
||||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||||
|
import org.springframework.core.ParameterizedTypeReference;
|
||||||
|
import org.springframework.http.HttpEntity;
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
import org.springframework.test.context.web.WebAppConfiguration;
|
import org.springframework.test.context.web.WebAppConfiguration;
|
||||||
@@ -25,10 +28,12 @@ import rx.Observable;
|
|||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import static net.chrisrichardson.eventstorestore.javaexamples.testutil.TestUtil.eventually;
|
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.generateCustomerInfo;
|
||||||
import static net.chrisrichardson.eventstorestore.javaexamples.testutil.customers.CustomersTestUtils.generateToAccountInfo;
|
import static net.chrisrichardson.eventstorestore.javaexamples.testutil.customers.CustomersTestUtils.generateToAccountInfo;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@SpringApplicationConfiguration(classes = BankingWebTestConfiguration.class)
|
@SpringApplicationConfiguration(classes = BankingWebTestConfiguration.class)
|
||||||
@@ -97,6 +102,47 @@ public class BankingWebIntegrationTest {
|
|||||||
assertAccountBalance(fromAccountId, finalFromAccountBalance);
|
assertAccountBalance(fromAccountId, finalFromAccountBalance);
|
||||||
assertAccountBalance(toAccountId, finalToAccountBalance);
|
assertAccountBalance(toAccountId, finalToAccountBalance);
|
||||||
|
|
||||||
|
List<AccountTransactionInfo> transactionInfoList = restTemplate.exchange(baseUrl("/accounts/"+fromAccountId+"/history"),
|
||||||
|
HttpMethod.GET,
|
||||||
|
new HttpEntity(BasicAuthUtils.basicAuthHeaders("test_user@mail.com")),
|
||||||
|
new ParameterizedTypeReference<List<AccountTransactionInfo>>() {}).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<GetAccountResponse> accountResponseList = restTemplate.exchange(baseUrl("/accounts?customerId="+customerId),
|
||||||
|
HttpMethod.GET,
|
||||||
|
new HttpEntity(BasicAuthUtils.basicAuthHeaders("test_user@mail.com")),
|
||||||
|
new ParameterizedTypeReference<List<GetAccountResponse>>() {}).getBody();
|
||||||
|
|
||||||
|
assertTrue(accountResponseList.stream().filter(acc -> acc.getAccountId().equals(accountId)).findFirst().isPresent());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -163,7 +209,7 @@ public class BankingWebIntegrationTest {
|
|||||||
@Override
|
@Override
|
||||||
public void verify(QuerySideCustomer customerResponse) {
|
public void verify(QuerySideCustomer customerResponse) {
|
||||||
Assert.assertEquals(customerId, customerResponse.getId());
|
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)));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user