- fixed tests
fix issue #24, fix issue #26, fix issue #27, fix issue #28
This commit is contained in:
@@ -14,6 +14,7 @@ dependencies {
|
||||
testCompile project(":testutil")
|
||||
testCompile project(":customers-command-side-service")
|
||||
testCompile "org.springframework.boot:spring-boot-starter-test"
|
||||
testCompile "io.eventuate.client.java:eventuate-client-java-jdbc:$eventuateClientVersion"
|
||||
}
|
||||
|
||||
test {
|
||||
|
||||
@@ -48,8 +48,10 @@ public class CustomersQuerySideServiceIntegrationTest {
|
||||
|
||||
final CustomerResponse customerResponse = restTemplate.postForEntity(baseUrl("/customers"), customerInfo, CustomerResponse.class).getBody();
|
||||
final String customerId = customerResponse.getId();
|
||||
final String email = customerResponse.getCustomerInfo().getEmail();
|
||||
final String password = customerResponse.getCustomerInfo().getPassword();
|
||||
|
||||
customersTestUtils.assertCustomerResponse(customerId, customerInfo);
|
||||
customersTestUtils.assertCustomerResponse(customerId, email, password, customerInfo);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package net.chrisrichardson.eventstore.javaexamples.banking.web;
|
||||
|
||||
import io.eventuate.javaclient.spring.jdbc.EventuateJdbcEventStoreConfiguration;
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.commonauth.AuthConfiguration;
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.customers.CustomersCommandSideWebConfiguration;
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.web.customers.queryside.CustomersQuerySideWebConfiguration;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -14,7 +17,7 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@Configuration
|
||||
@Import({CustomersCommandSideServiceConfiguration.class, CustomersQuerySideServiceConfiguration.class, AuthConfiguration.class})
|
||||
@Import({CustomersCommandSideWebConfiguration.class, CustomersQuerySideWebConfiguration.class, EventuateJdbcEventStoreConfiguration.class, AuthConfiguration.class})
|
||||
@EnableAutoConfiguration
|
||||
public class CustomersQuerySideServiceTestConfiguration {
|
||||
|
||||
|
||||
@@ -53,19 +53,14 @@ public class BankingAuthTest {
|
||||
|
||||
final CustomerResponse customerResponse = restTemplate.postForEntity(baseUrl("/customers"), customerInfo, CustomerResponse.class).getBody();
|
||||
final String customerId = customerResponse.getId();
|
||||
final String password = customerResponse.getCustomerInfo().getPassword();
|
||||
|
||||
Assert.assertNotNull(customerId);
|
||||
Assert.assertEquals(customerInfo, customerResponse.getCustomerInfo());
|
||||
|
||||
try {
|
||||
Thread.sleep(10000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
customersTestUtils.assertCustomerResponse(customerId, email, password, customerInfo);
|
||||
|
||||
customersTestUtils.assertCustomerResponse(customerId, customerInfo);
|
||||
|
||||
AuthRequest authRequest = new AuthRequest(email);
|
||||
AuthRequest authRequest = new AuthRequest(email, password);
|
||||
|
||||
final QuerySideCustomer loginQuerySideCustomer = restTemplate.postForEntity(baseUrl("/login"), authRequest, QuerySideCustomer.class).getBody();
|
||||
|
||||
|
||||
@@ -26,6 +26,13 @@ public abstract class AbstractRestAPITest {
|
||||
|
||||
@Test
|
||||
public void shouldCreateAccountsAndTransferMoney() {
|
||||
CustomerInfo customerInfo = generateCustomerInfo();
|
||||
|
||||
final CustomerResponse customerResponse = getRestTemplate().postForEntity(baseUrl("/customers"), customerInfo, CustomerResponse.class).getBody();
|
||||
final String customerId = customerResponse.getId();
|
||||
final String email = customerResponse.getCustomerInfo().getEmail();
|
||||
final String password = customerResponse.getCustomerInfo().getPassword();
|
||||
|
||||
BigDecimal initialFromAccountBalance = new BigDecimal(500);
|
||||
BigDecimal initialToAccountBalance = new BigDecimal(100);
|
||||
BigDecimal amountToTransfer = new BigDecimal(150);
|
||||
@@ -34,36 +41,36 @@ public abstract class AbstractRestAPITest {
|
||||
BigDecimal finalToAccountBalance = initialToAccountBalance.add(amountToTransfer);
|
||||
|
||||
final CreateAccountResponse fromAccount = getAuthenticatedRestTemplate().postForEntity(baseUrl("/accounts"),
|
||||
new CreateAccountRequest("00000000-00000000", "My 1 Account", "", initialFromAccountBalance),
|
||||
CreateAccountResponse.class);
|
||||
new CreateAccountRequest(customerId, "My 1 Account", "", initialFromAccountBalance),
|
||||
CreateAccountResponse.class, email, password);
|
||||
|
||||
final String fromAccountId = fromAccount.getAccountId();
|
||||
|
||||
CreateAccountResponse toAccount = getAuthenticatedRestTemplate().postForEntity(baseUrl("/accounts"),
|
||||
new CreateAccountRequest("00000000-00000000", "My 2 Account", "", initialToAccountBalance),
|
||||
CreateAccountResponse.class);
|
||||
CreateAccountResponse.class, email, password);
|
||||
|
||||
String toAccountId = toAccount.getAccountId();
|
||||
|
||||
Assert.assertNotNull(fromAccountId);
|
||||
Assert.assertNotNull(toAccountId);
|
||||
|
||||
assertAccountBalance(fromAccountId, initialFromAccountBalance);
|
||||
assertAccountBalance(toAccountId, initialToAccountBalance);
|
||||
assertAccountBalance(email, password, fromAccountId, initialFromAccountBalance);
|
||||
assertAccountBalance(email, password, toAccountId, initialToAccountBalance);
|
||||
|
||||
final CreateMoneyTransferResponse moneyTransfer = getAuthenticatedRestTemplate().postForEntity(baseUrl("/transfers"),
|
||||
new CreateMoneyTransferRequest(fromAccountId, toAccountId, amountToTransfer, ""),
|
||||
CreateMoneyTransferResponse.class);
|
||||
CreateMoneyTransferResponse.class, email, password);
|
||||
|
||||
assertAccountBalance(fromAccountId, finalFromAccountBalance);
|
||||
assertAccountBalance(toAccountId, finalToAccountBalance);
|
||||
assertAccountBalance(email, password, fromAccountId, finalFromAccountBalance);
|
||||
assertAccountBalance(email, password, toAccountId, finalToAccountBalance);
|
||||
|
||||
eventually(
|
||||
new Producer<AccountHistoryResponse>() {
|
||||
@Override
|
||||
public CompletableFuture<AccountHistoryResponse> produce() {
|
||||
return CompletableFuture.completedFuture(getAuthenticatedRestTemplate().getForEntity(baseUrl("/accounts/" + fromAccountId + "/history"),
|
||||
AccountHistoryResponse.class));
|
||||
AccountHistoryResponse.class, email, password));
|
||||
}
|
||||
},
|
||||
new Verifier<AccountHistoryResponse>() {
|
||||
@@ -91,28 +98,30 @@ public abstract class AbstractRestAPITest {
|
||||
|
||||
final CustomerResponse customerResponse = getRestTemplate().postForEntity(baseUrl("/customers"), customerInfo, CustomerResponse.class).getBody();
|
||||
final String customerId = customerResponse.getId();
|
||||
final String email = customerResponse.getCustomerInfo().getEmail();
|
||||
final String password = customerResponse.getCustomerInfo().getPassword();
|
||||
|
||||
Assert.assertNotNull(customerId);
|
||||
assertEquals(customerInfo, customerResponse.getCustomerInfo());
|
||||
|
||||
getCustomersTestUtils().assertCustomerResponse(customerId, customerInfo);
|
||||
getCustomersTestUtils().assertCustomerResponse(customerId, email, password, customerInfo);
|
||||
|
||||
final CreateAccountResponse account = getAuthenticatedRestTemplate().postForEntity(baseUrl("/accounts"),
|
||||
new CreateAccountRequest(customerId, "My 1 Account", "", initialFromAccountBalance),
|
||||
CreateAccountResponse.class);
|
||||
CreateAccountResponse.class, email, password);
|
||||
|
||||
final String accountId = account.getAccountId();
|
||||
|
||||
Assert.assertNotNull(accountId);
|
||||
|
||||
assertAccountBalance(accountId, initialFromAccountBalance);
|
||||
assertAccountBalance(email, password, accountId, initialFromAccountBalance);
|
||||
|
||||
eventually(
|
||||
new Producer<GetAccountsResponse>() {
|
||||
@Override
|
||||
public CompletableFuture<GetAccountsResponse> produce() {
|
||||
return CompletableFuture.completedFuture(getAuthenticatedRestTemplate().getForEntity(baseUrl("/customers/"+customerId+"/accounts"),
|
||||
GetAccountsResponse.class));
|
||||
GetAccountsResponse.class, email, password));
|
||||
}
|
||||
},
|
||||
new Verifier<GetAccountsResponse>() {
|
||||
@@ -129,33 +138,35 @@ public abstract class AbstractRestAPITest {
|
||||
|
||||
final CustomerResponse customerResponse = getRestTemplate().postForEntity(baseUrl("/customers"), customerInfo, CustomerResponse.class).getBody();
|
||||
final String customerId = customerResponse.getId();
|
||||
final String email = customerResponse.getCustomerInfo().getEmail();
|
||||
final String password = customerResponse.getCustomerInfo().getPassword();
|
||||
|
||||
Assert.assertNotNull(customerId);
|
||||
assertEquals(customerInfo, customerResponse.getCustomerInfo());
|
||||
|
||||
getCustomersTestUtils().assertCustomerResponse(customerId, customerInfo);
|
||||
getCustomersTestUtils().assertCustomerResponse(customerId, email, password, customerInfo);
|
||||
|
||||
ToAccountInfo toAccountInfo = generateToAccountInfo();
|
||||
|
||||
getAuthenticatedRestTemplate().postForEntity(baseUrl("/customers/" + customerId + "/toaccounts"),
|
||||
toAccountInfo,
|
||||
null);
|
||||
null, email, password);
|
||||
|
||||
assertToAccountsContains(customerId, toAccountInfo);
|
||||
assertToAccountsContains(customerId, email, password, toAccountInfo);
|
||||
}
|
||||
|
||||
private BigDecimal toCents(BigDecimal dollarAmount) {
|
||||
return dollarAmount.multiply(new BigDecimal(100));
|
||||
}
|
||||
|
||||
private void assertAccountBalance(final String fromAccountId, final BigDecimal expectedBalanceInDollars) {
|
||||
private void assertAccountBalance(final String email, final String password, final String fromAccountId, final BigDecimal expectedBalanceInDollars) {
|
||||
final BigDecimal inCents = toCents(expectedBalanceInDollars);
|
||||
eventually(
|
||||
new Producer<GetAccountResponse>() {
|
||||
@Override
|
||||
public CompletableFuture<GetAccountResponse> produce() {
|
||||
return CompletableFuture.completedFuture(getAuthenticatedRestTemplate().getForEntity(baseUrl("/accounts/" + fromAccountId),
|
||||
GetAccountResponse.class));
|
||||
GetAccountResponse.class, email, password));
|
||||
}
|
||||
},
|
||||
new Verifier<GetAccountResponse>() {
|
||||
@@ -167,13 +178,13 @@ public abstract class AbstractRestAPITest {
|
||||
});
|
||||
}
|
||||
|
||||
private void assertToAccountsContains(final String customerId, final ToAccountInfo toAccountInfo) {
|
||||
private void assertToAccountsContains(final String customerId, final String email, final String password, final ToAccountInfo toAccountInfo) {
|
||||
eventually(
|
||||
new Producer<QuerySideCustomer>() {
|
||||
@Override
|
||||
public CompletableFuture<QuerySideCustomer> produce() {
|
||||
return CompletableFuture.completedFuture(getAuthenticatedRestTemplate().getForEntity(baseUrl("/customers/" + customerId),
|
||||
QuerySideCustomer.class));
|
||||
QuerySideCustomer.class, email, password));
|
||||
}
|
||||
},
|
||||
new Verifier<QuerySideCustomer>() {
|
||||
|
||||
@@ -11,19 +11,23 @@ public class AuthenticatedRestTemplate {
|
||||
this.restTemplate = restTemplate;
|
||||
}
|
||||
|
||||
public <T> T getForEntity(String url, Class<T> clazz) {
|
||||
public <T> T getForEntity(String url, Class<T> clazz, String email, String password) {
|
||||
return BasicAuthUtils.doBasicAuthenticatedRequest(restTemplate,
|
||||
url,
|
||||
HttpMethod.GET,
|
||||
clazz);
|
||||
clazz,
|
||||
email,
|
||||
password);
|
||||
}
|
||||
|
||||
public <T> T postForEntity(String url, Object requestObject, Class<T> clazz) {
|
||||
public <T> T postForEntity(String url, Object requestObject, Class<T> clazz, String email, String password) {
|
||||
return BasicAuthUtils.doBasicAuthenticatedRequest(restTemplate,
|
||||
url,
|
||||
HttpMethod.POST,
|
||||
clazz,
|
||||
requestObject
|
||||
requestObject,
|
||||
email,
|
||||
password
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,10 +12,10 @@ import java.nio.charset.Charset;
|
||||
*/
|
||||
public class BasicAuthUtils {
|
||||
|
||||
public static HttpHeaders basicAuthHeaders(String username) {
|
||||
public static HttpHeaders basicAuthHeaders(String username, String password) {
|
||||
return new HttpHeaders() {
|
||||
{
|
||||
String auth = username + ":";
|
||||
String auth = username + ":" + password;
|
||||
byte[] encodedAuth = Base64.encodeBase64(
|
||||
auth.getBytes(Charset.forName("US-ASCII")));
|
||||
String authHeader = "Basic " + new String(encodedAuth);
|
||||
@@ -24,16 +24,16 @@ public class BasicAuthUtils {
|
||||
};
|
||||
}
|
||||
|
||||
public static <T> T doBasicAuthenticatedRequest(RestTemplate restTemplate, String url, HttpMethod httpMethod, Class<T> responseType) {
|
||||
return doBasicAuthenticatedRequest(restTemplate, url, httpMethod, responseType, null);
|
||||
public static <T> T doBasicAuthenticatedRequest(RestTemplate restTemplate, String url, HttpMethod httpMethod, Class<T> responseType, String email, String password) {
|
||||
return doBasicAuthenticatedRequest(restTemplate, url, httpMethod, responseType, null, email, password);
|
||||
}
|
||||
|
||||
public static <T> T doBasicAuthenticatedRequest(RestTemplate restTemplate, String url, HttpMethod httpMethod, Class<T> responseType, Object requestObject) {
|
||||
public static <T> T doBasicAuthenticatedRequest(RestTemplate restTemplate, String url, HttpMethod httpMethod, Class<T> responseType, Object requestObject, String email, String password) {
|
||||
HttpEntity httpEntity;
|
||||
if (requestObject != null) {
|
||||
httpEntity = new HttpEntity<>(requestObject, BasicAuthUtils.basicAuthHeaders("test_user@mail.com"));
|
||||
httpEntity = new HttpEntity<>(requestObject, BasicAuthUtils.basicAuthHeaders(email, password));
|
||||
} else {
|
||||
httpEntity = new HttpEntity(BasicAuthUtils.basicAuthHeaders("test_user@mail.com"));
|
||||
httpEntity = new HttpEntity(BasicAuthUtils.basicAuthHeaders(email, password));
|
||||
}
|
||||
|
||||
ResponseEntity<T> responseEntity = restTemplate.exchange(url,
|
||||
|
||||
@@ -21,13 +21,13 @@ public class CustomersTestUtils {
|
||||
this.customersBaseUrl = customersBaseUrl;
|
||||
}
|
||||
|
||||
public void assertCustomerResponse(final String customerId, final CustomerInfo customerInfo) {
|
||||
public void assertCustomerResponse(final String customerId, final String email, final String password, final CustomerInfo customerInfo) {
|
||||
AuthenticatedRestTemplate art = new AuthenticatedRestTemplate(restTemplate);
|
||||
eventually(
|
||||
new Producer<QuerySideCustomer>() {
|
||||
@Override
|
||||
public CompletableFuture<QuerySideCustomer> produce() {
|
||||
return CompletableFuture.completedFuture(art.getForEntity(customersBaseUrl + customerId, QuerySideCustomer.class));
|
||||
return CompletableFuture.completedFuture(art.getForEntity(customersBaseUrl + customerId, QuerySideCustomer.class, email, password));
|
||||
}
|
||||
},
|
||||
new Verifier<QuerySideCustomer>() {
|
||||
|
||||
Reference in New Issue
Block a user