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