Merge commit '9276b1bdc839d42b39f4bbc866e54835d4469ade' into wip-customer
* commit '9276b1bdc839d42b39f4bbc866e54835d4469ade': changed return result for empty accounts case fixed tests fixed tests added new fields to TransferDetails added new fields to TransferDetails -added tests for new endpoints
This commit is contained in:
@@ -1,9 +1,6 @@
|
||||
package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.accounts;
|
||||
|
||||
import net.chrisrichardson.eventstore.Aggregate;
|
||||
import net.chrisrichardson.eventstore.EntityIdentifier;
|
||||
import net.chrisrichardson.eventstore.EntityNotFoundException;
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import rx.Observable;
|
||||
|
||||
import java.util.List;
|
||||
@@ -25,10 +22,6 @@ public class AccountQueryService {
|
||||
}
|
||||
|
||||
public Observable<List<AccountInfo>> findByCustomerId(String customerId) {
|
||||
List<AccountInfo> result = accountInfoRepository.findByCustomerId(customerId);
|
||||
if(result.isEmpty())
|
||||
return Observable.error(new EmptyResultDataAccessException(1));
|
||||
else
|
||||
return Observable.just(result);
|
||||
return Observable.just(accountInfoRepository.findByCustomerId(customerId));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,7 +53,12 @@ public class AccountQueryWorkflow implements CompoundEventHandler {
|
||||
logger.info("**************** account version=" + fromAccountId + ", " + de.eventId().asString());
|
||||
logger.info("**************** account version=" + toAccountId + ", " + de.eventId().asString());
|
||||
|
||||
AccountTransactionInfo ti = new AccountTransactionInfo(moneyTransferId, fromAccountId, toAccountId, toIntegerRepr(de.event().getDetails().getAmount()));
|
||||
AccountTransactionInfo ti = new AccountTransactionInfo(moneyTransferId,
|
||||
fromAccountId,
|
||||
toAccountId,
|
||||
toIntegerRepr(de.event().getDetails().getAmount()),
|
||||
de.event().getDetails().getDate(),
|
||||
de.event().getDetails().getDescription());
|
||||
|
||||
|
||||
accountInfoUpdateService.addTransaction(eventId, fromAccountId, ti);
|
||||
|
||||
@@ -1,17 +1,33 @@
|
||||
package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.accounts;
|
||||
|
||||
import org.apache.commons.lang.builder.EqualsBuilder;
|
||||
import org.apache.commons.lang.builder.HashCodeBuilder;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class AccountTransactionInfo {
|
||||
|
||||
private String transactionId;
|
||||
private String fromAccountId;
|
||||
private String toAccountId;
|
||||
private long amount;
|
||||
private Date date;
|
||||
private String description;
|
||||
|
||||
public AccountTransactionInfo() {
|
||||
}
|
||||
|
||||
public AccountTransactionInfo(String transactionId, String fromAccountId, String toAccountId, long amount) {
|
||||
this(transactionId, fromAccountId, toAccountId, amount, new Date(), "");
|
||||
}
|
||||
|
||||
public AccountTransactionInfo(String transactionId, String fromAccountId, String toAccountId, long amount, Date date, String description) {
|
||||
this.transactionId = transactionId;
|
||||
this.fromAccountId = fromAccountId;
|
||||
this.toAccountId = toAccountId;
|
||||
this.amount = amount;
|
||||
this.date = date;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getTransactionId() {
|
||||
@@ -45,4 +61,30 @@ public class AccountTransactionInfo {
|
||||
public void setAmount(long amount) {
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
public Date getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(Date date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return EqualsBuilder.reflectionEquals(this, o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return HashCodeBuilder.reflectionHashCode(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,20 +7,29 @@ case class TransferDetails(fromAccountId : EntityIdentifier, toAccountId : Entit
|
||||
import net.chrisrichardson.eventstore.EntityIdentifier;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
|
||||
public class TransferDetails {
|
||||
|
||||
private EntityIdentifier fromAccountId;
|
||||
private EntityIdentifier toAccountId;
|
||||
private BigDecimal amount;
|
||||
private Date date;
|
||||
private String description;
|
||||
|
||||
private TransferDetails() {
|
||||
}
|
||||
|
||||
public TransferDetails(EntityIdentifier fromAccountId, EntityIdentifier toAccountId, BigDecimal amount) {
|
||||
this(fromAccountId, toAccountId, amount, new Date(), "");
|
||||
}
|
||||
|
||||
public TransferDetails(EntityIdentifier fromAccountId, EntityIdentifier toAccountId, BigDecimal amount, Date date, String description) {
|
||||
this.fromAccountId = fromAccountId;
|
||||
this.toAccountId = toAccountId;
|
||||
this.amount = amount;
|
||||
this.date = date;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public EntityIdentifier getFromAccountId() {
|
||||
@@ -34,4 +43,12 @@ public class TransferDetails {
|
||||
public BigDecimal getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
public Date getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -115,13 +120,56 @@ public class EndToEndTest {
|
||||
transactionsCommandSideBaseUrl("/transfers"),
|
||||
HttpMethod.POST,
|
||||
CreateMoneyTransferResponse.class,
|
||||
new CreateMoneyTransferRequest(fromAccountId, toAccountId, amountToTransfer)
|
||||
new CreateMoneyTransferRequest(fromAccountId, toAccountId, amountToTransfer, "")
|
||||
);
|
||||
|
||||
assertAccountBalance(fromAccountId, finalFromAccountBalance);
|
||||
assertAccountBalance(toAccountId, finalToAccountBalance);
|
||||
|
||||
// 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();
|
||||
|
||||
assertTrue(transactionInfoList.stream().filter(ti -> ti.getTransactionId().equals(moneyTransfer.getMoneyTransferId()) &&
|
||||
ti.getFromAccountId().equals(fromAccountId) &&
|
||||
ti.getToAccountId().equals(toAccountId) &&
|
||||
ti.getAmount() == toCents(amountToTransfer).longValue()).findFirst().isPresent());
|
||||
}
|
||||
|
||||
@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) {
|
||||
|
||||
@@ -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)
|
||||
@@ -91,12 +96,55 @@ public class BankingWebIntegrationTest {
|
||||
baseUrl("/transfers"),
|
||||
HttpMethod.POST,
|
||||
CreateMoneyTransferResponse.class,
|
||||
new CreateMoneyTransferRequest(fromAccountId, toAccountId, amountToTransfer)
|
||||
new CreateMoneyTransferRequest(fromAccountId, toAccountId, amountToTransfer, "")
|
||||
);
|
||||
|
||||
assertAccountBalance(fromAccountId, finalFromAccountBalance);
|
||||
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();
|
||||
|
||||
|
||||
assertTrue(transactionInfoList.stream().filter(ti -> ti.getTransactionId().equals(moneyTransfer.getMoneyTransferId()) &&
|
||||
ti.getFromAccountId().equals(fromAccountId) &&
|
||||
ti.getToAccountId().equals(toAccountId) &&
|
||||
ti.getAmount() == toCents(amountToTransfer).longValue()).findFirst().isPresent());
|
||||
}
|
||||
|
||||
@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
|
||||
@@ -163,7 +211,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)));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -15,13 +15,16 @@ public class CreateMoneyTransferRequest {
|
||||
@DecimalMin("0.01")
|
||||
private BigDecimal amount;
|
||||
|
||||
private String description;
|
||||
|
||||
public CreateMoneyTransferRequest() {
|
||||
}
|
||||
|
||||
public CreateMoneyTransferRequest(String fromAccountId, String toAccountId, BigDecimal amount) {
|
||||
public CreateMoneyTransferRequest(String fromAccountId, String toAccountId, BigDecimal amount, String description) {
|
||||
this.fromAccountId = fromAccountId;
|
||||
this.toAccountId = toAccountId;
|
||||
this.amount = amount;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public void setFromAccountId(String fromAccountId) {
|
||||
@@ -36,6 +39,10 @@ public class CreateMoneyTransferRequest {
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getFromAccountId() {
|
||||
return fromAccountId;
|
||||
}
|
||||
@@ -47,4 +54,8 @@ public class CreateMoneyTransferRequest {
|
||||
public BigDecimal getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,8 @@ import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import rx.Observable;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/transfers")
|
||||
public class MoneyTransferController {
|
||||
@@ -27,7 +29,9 @@ public class MoneyTransferController {
|
||||
TransferDetails transferDetails = new TransferDetails(
|
||||
new EntityIdentifier(request.getFromAccountId()),
|
||||
new EntityIdentifier(request.getToAccountId()),
|
||||
request.getAmount());
|
||||
request.getAmount(),
|
||||
new Date(),
|
||||
request.getDescription());
|
||||
return moneyTransferService.transferMoney(transferDetails)
|
||||
.map(entityAndEventInfo -> new CreateMoneyTransferResponse(entityAndEventInfo.getEntityIdentifier().getId()));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user