Skip to content

Commit

Permalink
- fixed tests
Browse files Browse the repository at this point in the history
fix issue cer#24,
fix issue cer#26,
fix issue cer#27,
fix issue cer#28
  • Loading branch information
dartpopikyardo committed Sep 9, 2016
1 parent fd63640 commit 859a01a
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 43 deletions.
1 change: 1 addition & 0 deletions java-spring/customers-query-side-service/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<AccountHistoryResponse>() {
@Override
public CompletableFuture<AccountHistoryResponse> produce() {
return CompletableFuture.completedFuture(getAuthenticatedRestTemplate().getForEntity(baseUrl("/accounts/" + fromAccountId + "/history"),
AccountHistoryResponse.class));
AccountHistoryResponse.class, email, password));
}
},
new Verifier<AccountHistoryResponse>() {
Expand Down Expand Up @@ -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<GetAccountsResponse>() {
@Override
public CompletableFuture<GetAccountsResponse> produce() {
return CompletableFuture.completedFuture(getAuthenticatedRestTemplate().getForEntity(baseUrl("/customers/"+customerId+"/accounts"),
GetAccountsResponse.class));
GetAccountsResponse.class, email, password));
}
},
new Verifier<GetAccountsResponse>() {
Expand All @@ -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<GetAccountResponse>() {
@Override
public CompletableFuture<GetAccountResponse> produce() {
return CompletableFuture.completedFuture(getAuthenticatedRestTemplate().getForEntity(baseUrl("/accounts/" + fromAccountId),
GetAccountResponse.class));
GetAccountResponse.class, email, password));
}
},
new Verifier<GetAccountResponse>() {
Expand All @@ -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<QuerySideCustomer>() {
@Override
public CompletableFuture<QuerySideCustomer> produce() {
return CompletableFuture.completedFuture(getAuthenticatedRestTemplate().getForEntity(baseUrl("/customers/" + customerId),
QuerySideCustomer.class));
QuerySideCustomer.class, email, password));
}
},
new Verifier<QuerySideCustomer>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@ public AuthenticatedRestTemplate(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,
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
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -24,16 +24,16 @@ public static HttpHeaders basicAuthHeaders(String username) {
};
}

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("[email protected]"));
httpEntity = new HttpEntity<>(requestObject, BasicAuthUtils.basicAuthHeaders(email, password));
} else {
httpEntity = new HttpEntity(BasicAuthUtils.basicAuthHeaders("[email protected]"));
httpEntity = new HttpEntity(BasicAuthUtils.basicAuthHeaders(email, password));
}

ResponseEntity<T> responseEntity = restTemplate.exchange(url,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<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>() {
Expand Down

0 comments on commit 859a01a

Please sign in to comment.