From 859a01a4bc2372a11a5377d1206235ca12da4417 Mon Sep 17 00:00:00 2001 From: dartpopikyardo Date: Fri, 9 Sep 2016 16:55:24 +0300 Subject: [PATCH] - fixed tests fix issue #24, fix issue #26, fix issue #27, fix issue #28 --- .../customers-query-side-service/build.gradle | 1 + ...tomersQuerySideServiceIntegrationTest.java | 4 +- ...mersQuerySideServiceTestConfiguration.java | 5 +- .../banking/web/BankingAuthTest.java | 11 ++-- .../testutil/AbstractRestAPITest.java | 51 +++++++++++-------- .../testutil/AuthenticatedRestTemplate.java | 12 +++-- .../javaexamples/testutil/BasicAuthUtils.java | 14 ++--- .../testutil/CustomersTestUtils.java | 4 +- 8 files changed, 59 insertions(+), 43 deletions(-) diff --git a/java-spring/customers-query-side-service/build.gradle b/java-spring/customers-query-side-service/build.gradle index 6b218bb..5f536f1 100644 --- a/java-spring/customers-query-side-service/build.gradle +++ b/java-spring/customers-query-side-service/build.gradle @@ -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 { diff --git a/java-spring/customers-query-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersQuerySideServiceIntegrationTest.java b/java-spring/customers-query-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersQuerySideServiceIntegrationTest.java index f48c868..2993a27 100644 --- a/java-spring/customers-query-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersQuerySideServiceIntegrationTest.java +++ b/java-spring/customers-query-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersQuerySideServiceIntegrationTest.java @@ -48,8 +48,10 @@ public void shouldGetCustomerById() { 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); } } diff --git a/java-spring/customers-query-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersQuerySideServiceTestConfiguration.java b/java-spring/customers-query-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersQuerySideServiceTestConfiguration.java index cb17c0c..8e12c30 100644 --- a/java-spring/customers-query-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersQuerySideServiceTestConfiguration.java +++ b/java-spring/customers-query-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersQuerySideServiceTestConfiguration.java @@ -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.List; @Configuration -@Import({CustomersCommandSideServiceConfiguration.class, CustomersQuerySideServiceConfiguration.class, AuthConfiguration.class}) +@Import({CustomersCommandSideWebConfiguration.class, CustomersQuerySideWebConfiguration.class, EventuateJdbcEventStoreConfiguration.class, AuthConfiguration.class}) @EnableAutoConfiguration public class CustomersQuerySideServiceTestConfiguration { diff --git a/java-spring/monolithic-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/BankingAuthTest.java b/java-spring/monolithic-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/BankingAuthTest.java index 2499229..388a2c5 100644 --- a/java-spring/monolithic-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/BankingAuthTest.java +++ b/java-spring/monolithic-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/BankingAuthTest.java @@ -53,19 +53,14 @@ public void shouldCreateCustomerAndLogin() { 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(); diff --git a/java-spring/testutil/src/main/java/net/chrisrichardson/eventstorestore/javaexamples/testutil/AbstractRestAPITest.java b/java-spring/testutil/src/main/java/net/chrisrichardson/eventstorestore/javaexamples/testutil/AbstractRestAPITest.java index 44942a4..e083def 100644 --- a/java-spring/testutil/src/main/java/net/chrisrichardson/eventstorestore/javaexamples/testutil/AbstractRestAPITest.java +++ b/java-spring/testutil/src/main/java/net/chrisrichardson/eventstorestore/javaexamples/testutil/AbstractRestAPITest.java @@ -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 void shouldCreateAccountsAndTransferMoney() { 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() { @Override public CompletableFuture produce() { return CompletableFuture.completedFuture(getAuthenticatedRestTemplate().getForEntity(baseUrl("/accounts/" + fromAccountId + "/history"), - AccountHistoryResponse.class)); + AccountHistoryResponse.class, email, password)); } }, new Verifier() { @@ -91,28 +98,30 @@ public void shouldCreateAccountsAndGetByCustomer() { 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() { @Override public CompletableFuture produce() { return CompletableFuture.completedFuture(getAuthenticatedRestTemplate().getForEntity(baseUrl("/customers/"+customerId+"/accounts"), - GetAccountsResponse.class)); + GetAccountsResponse.class, email, password)); } }, new Verifier() { @@ -129,33 +138,35 @@ public void shouldCreateCustomersAndAddToAccount() { 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() { @Override public CompletableFuture produce() { return CompletableFuture.completedFuture(getAuthenticatedRestTemplate().getForEntity(baseUrl("/accounts/" + fromAccountId), - GetAccountResponse.class)); + GetAccountResponse.class, email, password)); } }, new Verifier() { @@ -167,13 +178,13 @@ public void verify(GetAccountResponse accountInfo) { }); } - 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() { @Override public CompletableFuture produce() { return CompletableFuture.completedFuture(getAuthenticatedRestTemplate().getForEntity(baseUrl("/customers/" + customerId), - QuerySideCustomer.class)); + QuerySideCustomer.class, email, password)); } }, new Verifier() { diff --git a/java-spring/testutil/src/main/java/net/chrisrichardson/eventstorestore/javaexamples/testutil/AuthenticatedRestTemplate.java b/java-spring/testutil/src/main/java/net/chrisrichardson/eventstorestore/javaexamples/testutil/AuthenticatedRestTemplate.java index 991c18e..15d7433 100644 --- a/java-spring/testutil/src/main/java/net/chrisrichardson/eventstorestore/javaexamples/testutil/AuthenticatedRestTemplate.java +++ b/java-spring/testutil/src/main/java/net/chrisrichardson/eventstorestore/javaexamples/testutil/AuthenticatedRestTemplate.java @@ -11,19 +11,23 @@ public AuthenticatedRestTemplate(RestTemplate restTemplate) { this.restTemplate = restTemplate; } - public T getForEntity(String url, Class clazz) { + public T getForEntity(String url, Class clazz, String email, String password) { return BasicAuthUtils.doBasicAuthenticatedRequest(restTemplate, url, HttpMethod.GET, - clazz); + clazz, + email, + password); } - public T postForEntity(String url, Object requestObject, Class clazz) { + public T postForEntity(String url, Object requestObject, Class clazz, String email, String password) { return BasicAuthUtils.doBasicAuthenticatedRequest(restTemplate, url, HttpMethod.POST, clazz, - requestObject + requestObject, + email, + password ); } } diff --git a/java-spring/testutil/src/main/java/net/chrisrichardson/eventstorestore/javaexamples/testutil/BasicAuthUtils.java b/java-spring/testutil/src/main/java/net/chrisrichardson/eventstorestore/javaexamples/testutil/BasicAuthUtils.java index da496a9..4d5ed0c 100644 --- a/java-spring/testutil/src/main/java/net/chrisrichardson/eventstorestore/javaexamples/testutil/BasicAuthUtils.java +++ b/java-spring/testutil/src/main/java/net/chrisrichardson/eventstorestore/javaexamples/testutil/BasicAuthUtils.java @@ -12,10 +12,10 @@ */ 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 static HttpHeaders basicAuthHeaders(String username) { }; } - public static T doBasicAuthenticatedRequest(RestTemplate restTemplate, String url, HttpMethod httpMethod, Class responseType) { - return doBasicAuthenticatedRequest(restTemplate, url, httpMethod, responseType, null); + public static T doBasicAuthenticatedRequest(RestTemplate restTemplate, String url, HttpMethod httpMethod, Class responseType, String email, String password) { + return doBasicAuthenticatedRequest(restTemplate, url, httpMethod, responseType, null, email, password); } - public static T doBasicAuthenticatedRequest(RestTemplate restTemplate, String url, HttpMethod httpMethod, Class responseType, Object requestObject) { + public static T doBasicAuthenticatedRequest(RestTemplate restTemplate, String url, HttpMethod httpMethod, Class 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 responseEntity = restTemplate.exchange(url, diff --git a/java-spring/testutil/src/main/java/net/chrisrichardson/eventstorestore/javaexamples/testutil/CustomersTestUtils.java b/java-spring/testutil/src/main/java/net/chrisrichardson/eventstorestore/javaexamples/testutil/CustomersTestUtils.java index 288dad7..403a8a0 100644 --- a/java-spring/testutil/src/main/java/net/chrisrichardson/eventstorestore/javaexamples/testutil/CustomersTestUtils.java +++ b/java-spring/testutil/src/main/java/net/chrisrichardson/eventstorestore/javaexamples/testutil/CustomersTestUtils.java @@ -21,13 +21,13 @@ public CustomersTestUtils(RestTemplate restTemplate, String 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); eventually( new Producer() { @Override public CompletableFuture produce() { - return CompletableFuture.completedFuture(art.getForEntity(customersBaseUrl + customerId, QuerySideCustomer.class)); + return CompletableFuture.completedFuture(art.getForEntity(customersBaseUrl + customerId, QuerySideCustomer.class, email, password)); } }, new Verifier() {