added simultaneous token-based and http basic authentication
This commit is contained in:
@@ -5,6 +5,7 @@ import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Cust
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerResponse;
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Name;
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.commonauth.model.AuthRequest;
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.commonauth.utils.BasicAuthUtils;
|
||||
import net.chrisrichardson.eventstorestore.javaexamples.testutil.Producer;
|
||||
import net.chrisrichardson.eventstorestore.javaexamples.testutil.Verifier;
|
||||
import org.junit.Assert;
|
||||
@@ -14,6 +15,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.http.HttpEntity;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
@@ -42,7 +45,8 @@ public class BankingAuthTest {
|
||||
|
||||
@Test
|
||||
public void shouldCreateCustomerAndLogin() {
|
||||
CustomerInfo customerInfo = generateCustomerInfo();
|
||||
String email = uniqueEmail();
|
||||
CustomerInfo customerInfo = generateCustomerInfo(email);
|
||||
|
||||
final CustomerResponse customerResponse = restTemplate.postForEntity(baseUrl("/customers"), customerInfo, CustomerResponse.class).getBody();
|
||||
final String customerId = customerResponse.getId();
|
||||
@@ -52,7 +56,7 @@ public class BankingAuthTest {
|
||||
|
||||
assertCustomerResponse(customerId, customerInfo);
|
||||
|
||||
AuthRequest authRequest = new AuthRequest("current@email.com");
|
||||
AuthRequest authRequest = new AuthRequest(email);
|
||||
|
||||
final CustomerResponse loginCustomerResponse = restTemplate.postForEntity(baseUrl("/login"), authRequest, CustomerResponse.class).getBody();
|
||||
|
||||
@@ -64,7 +68,10 @@ public class BankingAuthTest {
|
||||
new Producer<CustomerResponse>() {
|
||||
@Override
|
||||
public Observable<CustomerResponse> produce() {
|
||||
return Observable.just(restTemplate.getForEntity(baseUrl("/customers/" + customerId), CustomerResponse.class).getBody());
|
||||
return Observable.just(BasicAuthUtils.doRestTemplateRequest(restTemplate,
|
||||
baseUrl("/customers/" + customerId),
|
||||
HttpMethod.GET,
|
||||
CustomerResponse.class));
|
||||
}
|
||||
},
|
||||
new Verifier<CustomerResponse>() {
|
||||
@@ -76,10 +83,10 @@ public class BankingAuthTest {
|
||||
});
|
||||
}
|
||||
|
||||
private CustomerInfo generateCustomerInfo() {
|
||||
private CustomerInfo generateCustomerInfo(String email) {
|
||||
return new CustomerInfo(
|
||||
new Name("John", "Doe"),
|
||||
"current@email.com",
|
||||
email,
|
||||
"000-00-0000",
|
||||
"1-111-111-1111",
|
||||
new Address("street 1",
|
||||
@@ -89,4 +96,8 @@ public class BankingAuthTest {
|
||||
"1111111")
|
||||
);
|
||||
}
|
||||
|
||||
private String uniqueEmail() {
|
||||
return System.currentTimeMillis() + "@email.com";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
package net.chrisrichardson.eventstore.javaexamples.banking.web;
|
||||
|
||||
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;
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.accounts.CreateAccountResponse;
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.transactions.CreateMoneyTransferRequest;
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.transactions.CreateMoneyTransferResponse;
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.web.queryside.accounts.GetAccountResponse;
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.accounts.CreateAccountRequest;
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.accounts.CreateAccountResponse;
|
||||
import net.chrisrichardson.eventstorestore.javaexamples.testutil.Producer;
|
||||
import net.chrisrichardson.eventstorestore.javaexamples.testutil.Verifier;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -13,16 +16,15 @@ 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.http.HttpEntity;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import rx.Observable;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import net.chrisrichardson.eventstorestore.javaexamples.testutil.Producer;
|
||||
import net.chrisrichardson.eventstorestore.javaexamples.testutil.Verifier;
|
||||
import rx.Observable;
|
||||
|
||||
import static net.chrisrichardson.eventstorestore.javaexamples.testutil.TestUtil.eventually;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@@ -31,114 +33,137 @@ import static net.chrisrichardson.eventstorestore.javaexamples.testutil.TestUtil
|
||||
@IntegrationTest({"server.port=0", "management.port=0"})
|
||||
public class BankingWebIntegrationTest {
|
||||
|
||||
@Value("${local.server.port}")
|
||||
private int port;
|
||||
@Value("${local.server.port}")
|
||||
private int port;
|
||||
|
||||
private String baseUrl(String path) {
|
||||
return "http://localhost:" + port + "/" + path;
|
||||
}
|
||||
private String baseUrl(String path) {
|
||||
return "http://localhost:" + port + "/" + path;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
RestTemplate restTemplate;
|
||||
@Autowired
|
||||
RestTemplate restTemplate;
|
||||
|
||||
|
||||
@Test
|
||||
public void shouldCreateAccountsAndTransferMoney() {
|
||||
BigDecimal initialFromAccountBalance = new BigDecimal(500);
|
||||
BigDecimal initialToAccountBalance = new BigDecimal(100);
|
||||
BigDecimal amountToTransfer = new BigDecimal(150);
|
||||
@Test
|
||||
public void shouldCreateAccountsAndTransferMoney() {
|
||||
BigDecimal initialFromAccountBalance = new BigDecimal(500);
|
||||
BigDecimal initialToAccountBalance = new BigDecimal(100);
|
||||
BigDecimal amountToTransfer = new BigDecimal(150);
|
||||
|
||||
BigDecimal finalFromAccountBalance = initialFromAccountBalance.subtract(amountToTransfer);
|
||||
BigDecimal finalToAccountBalance = initialToAccountBalance.add(amountToTransfer);
|
||||
BigDecimal finalFromAccountBalance = initialFromAccountBalance.subtract(amountToTransfer);
|
||||
BigDecimal finalToAccountBalance = initialToAccountBalance.add(amountToTransfer);
|
||||
|
||||
final CreateAccountResponse fromAccount = restTemplate.postForEntity(baseUrl("/accounts"), new CreateAccountRequest("00000000-00000000", "My Account", initialFromAccountBalance), CreateAccountResponse.class).getBody();
|
||||
final String fromAccountId = fromAccount.getAccountId();
|
||||
final CreateAccountResponse fromAccount = BasicAuthUtils.doRestTemplateRequest(restTemplate,
|
||||
baseUrl("/accounts"),
|
||||
HttpMethod.POST,
|
||||
CreateAccountResponse.class,
|
||||
new CreateAccountRequest("00000000-00000000", "My 1 Account", initialFromAccountBalance)
|
||||
);
|
||||
final String fromAccountId = fromAccount.getAccountId();
|
||||
|
||||
CreateAccountResponse toAccount = restTemplate.postForEntity(baseUrl("/accounts"), new CreateAccountRequest("00000000-00000000", "My Account", initialToAccountBalance), CreateAccountResponse.class).getBody();
|
||||
String toAccountId = toAccount.getAccountId();
|
||||
CreateAccountResponse toAccount = BasicAuthUtils.doRestTemplateRequest(restTemplate,
|
||||
baseUrl("/accounts"),
|
||||
HttpMethod.POST,
|
||||
CreateAccountResponse.class,
|
||||
new CreateAccountRequest("00000000-00000000", "My 2 Account", initialToAccountBalance)
|
||||
);
|
||||
|
||||
Assert.assertNotNull(fromAccountId);
|
||||
Assert.assertNotNull(toAccountId);
|
||||
String toAccountId = toAccount.getAccountId();
|
||||
|
||||
assertAccountBalance(fromAccountId, initialFromAccountBalance);
|
||||
assertAccountBalance(toAccountId, initialToAccountBalance);
|
||||
Assert.assertNotNull(fromAccountId);
|
||||
Assert.assertNotNull(toAccountId);
|
||||
|
||||
assertAccountBalance(fromAccountId, initialFromAccountBalance);
|
||||
assertAccountBalance(toAccountId, initialToAccountBalance);
|
||||
|
||||
|
||||
final CreateMoneyTransferResponse moneyTransfer = restTemplate.postForEntity(baseUrl("/transfers"),
|
||||
new CreateMoneyTransferRequest(fromAccountId, toAccountId, amountToTransfer), CreateMoneyTransferResponse.class).getBody();
|
||||
final CreateMoneyTransferResponse moneyTransfer = BasicAuthUtils.doRestTemplateRequest(restTemplate,
|
||||
baseUrl("/transfers"),
|
||||
HttpMethod.POST,
|
||||
CreateMoneyTransferResponse.class,
|
||||
new CreateMoneyTransferRequest(fromAccountId, toAccountId, amountToTransfer)
|
||||
);
|
||||
|
||||
assertAccountBalance(fromAccountId, finalFromAccountBalance);
|
||||
assertAccountBalance(toAccountId, finalToAccountBalance);
|
||||
assertAccountBalance(fromAccountId, finalFromAccountBalance);
|
||||
assertAccountBalance(toAccountId, finalToAccountBalance);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateCustomers() {
|
||||
CustomerInfo customerInfo = generateCustomerInfo();
|
||||
@Test
|
||||
public void shouldCreateCustomers() {
|
||||
CustomerInfo customerInfo = generateCustomerInfo();
|
||||
|
||||
final CustomerResponse customerResponse = restTemplate.postForEntity(baseUrl("/customers"), customerInfo, CustomerResponse.class).getBody();
|
||||
final String customerId = customerResponse.getId();
|
||||
final CustomerResponse customerResponse = restTemplate.postForEntity(baseUrl("/customers"), customerInfo, CustomerResponse.class).getBody();
|
||||
final String customerId = customerResponse.getId();
|
||||
|
||||
Assert.assertNotNull(customerId);
|
||||
Assert.assertEquals(customerInfo, customerResponse.getCustomerInfo());
|
||||
Assert.assertNotNull(customerId);
|
||||
Assert.assertEquals(customerInfo, customerResponse.getCustomerInfo());
|
||||
|
||||
assertCustomerResponse(customerId, customerInfo);
|
||||
}
|
||||
assertCustomerResponse(customerId, customerInfo);
|
||||
}
|
||||
|
||||
private BigDecimal toCents(BigDecimal dollarAmount) {
|
||||
return dollarAmount.multiply(new BigDecimal(100));
|
||||
}
|
||||
private BigDecimal toCents(BigDecimal dollarAmount) {
|
||||
return dollarAmount.multiply(new BigDecimal(100));
|
||||
}
|
||||
|
||||
private void assertAccountBalance(final String fromAccountId, final BigDecimal expectedBalanceInDollars) {
|
||||
final BigDecimal inCents = toCents(expectedBalanceInDollars);
|
||||
eventually(
|
||||
new Producer<GetAccountResponse>() {
|
||||
@Override
|
||||
public Observable<GetAccountResponse> produce() {
|
||||
return Observable.just(restTemplate.getForEntity(baseUrl("/accounts/" + fromAccountId), GetAccountResponse.class).getBody());
|
||||
}
|
||||
},
|
||||
new Verifier<GetAccountResponse>() {
|
||||
@Override
|
||||
public void verify(GetAccountResponse accountInfo) {
|
||||
Assert.assertEquals(fromAccountId, accountInfo.getAccountId());
|
||||
Assert.assertEquals(inCents, accountInfo.getBalance());
|
||||
}
|
||||
});
|
||||
}
|
||||
private void assertAccountBalance(final String fromAccountId, final BigDecimal expectedBalanceInDollars) {
|
||||
final BigDecimal inCents = toCents(expectedBalanceInDollars);
|
||||
eventually(
|
||||
new Producer<GetAccountResponse>() {
|
||||
@Override
|
||||
public Observable<GetAccountResponse> produce() {
|
||||
return Observable.just(BasicAuthUtils.doRestTemplateRequest(restTemplate,
|
||||
baseUrl("/accounts/" + fromAccountId),
|
||||
HttpMethod.GET,
|
||||
GetAccountResponse.class));
|
||||
}
|
||||
},
|
||||
new Verifier<GetAccountResponse>() {
|
||||
@Override
|
||||
public void verify(GetAccountResponse accountInfo) {
|
||||
Assert.assertEquals(fromAccountId, accountInfo.getAccountId());
|
||||
Assert.assertEquals(inCents, accountInfo.getBalance());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void assertCustomerResponse(final String customerId, final CustomerInfo customerInfo) {
|
||||
eventually(
|
||||
new Producer<CustomerResponse>() {
|
||||
@Override
|
||||
public Observable<CustomerResponse> produce() {
|
||||
return Observable.just(restTemplate.getForEntity(baseUrl("/customers/" + customerId), CustomerResponse.class).getBody());
|
||||
}
|
||||
},
|
||||
new Verifier<CustomerResponse>() {
|
||||
@Override
|
||||
public void verify(CustomerResponse customerResponse) {
|
||||
Assert.assertEquals(customerId, customerResponse.getId());
|
||||
Assert.assertEquals(customerInfo, customerResponse.getCustomerInfo());
|
||||
}
|
||||
});
|
||||
}
|
||||
private void assertCustomerResponse(final String customerId, final CustomerInfo customerInfo) {
|
||||
eventually(
|
||||
new Producer<CustomerResponse>() {
|
||||
@Override
|
||||
public Observable<CustomerResponse> produce() {
|
||||
return Observable.just(BasicAuthUtils.doRestTemplateRequest(restTemplate,
|
||||
baseUrl("/customers/" + customerId),
|
||||
HttpMethod.GET,
|
||||
CustomerResponse.class));
|
||||
}
|
||||
},
|
||||
new Verifier<CustomerResponse>() {
|
||||
@Override
|
||||
public void verify(CustomerResponse customerResponse) {
|
||||
Assert.assertEquals(customerId, customerResponse.getId());
|
||||
Assert.assertEquals(customerInfo, customerResponse.getCustomerInfo());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private CustomerInfo generateCustomerInfo() {
|
||||
return new CustomerInfo(
|
||||
new Name("John", "Doe"),
|
||||
"current@email.com",
|
||||
"000-00-0000",
|
||||
"1-111-111-1111",
|
||||
new Address("street 1",
|
||||
"street 2",
|
||||
"City",
|
||||
"State",
|
||||
"1111111")
|
||||
);
|
||||
}
|
||||
|
||||
private ToAccountInfo generateToAccountInfo() {
|
||||
return new ToAccountInfo("11111111-11111111", "New Account", "John Doe");
|
||||
}
|
||||
|
||||
private CustomerInfo generateCustomerInfo() {
|
||||
return new CustomerInfo(
|
||||
new Name("John", "Doe"),
|
||||
"current@email.com",
|
||||
"000-00-0000",
|
||||
"1-111-111-1111",
|
||||
new Address("street 1",
|
||||
"street 2",
|
||||
"City",
|
||||
"State",
|
||||
"1111111")
|
||||
);
|
||||
}
|
||||
|
||||
private ToAccountInfo generateToAccountInfo() {
|
||||
return new ToAccountInfo("11111111-11111111", "New Account", "John Doe");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user