-
Notifications
You must be signed in to change notification settings - Fork 148
/
CommandQuerySynchronizationTest.java
67 lines (57 loc) · 2.44 KB
/
CommandQuerySynchronizationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package io.dddbyexamples.cqrs;
import io.dddbyexamples.cqrs.model.CreditCard;
import io.dddbyexamples.cqrs.persistence.CreditCardRepository;
import io.dddbyexamples.cqrs.ui.WithdrawalDto;
import io.dddbyexamples.cqrs.ui.WithdrawalCommand;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.test.context.junit4.SpringRunner;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import static java.math.BigDecimal.TEN;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import static org.springframework.http.HttpMethod.GET;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
public class CommandQuerySynchronizationTest {
@Autowired TestRestTemplate restTemplate;
@Autowired CreditCardRepository creditCardRepository;
@Test
public void shouldSynchronizeQuerySideAfterSendingACommand() {
// given
UUID cardUUid = thereIsCreditCardWithLimit(new BigDecimal(100));
// when
clientWantsToWithdraw(TEN, cardUUid);
// then
thereIsOneWithdrawalOf(TEN, cardUUid);
}
UUID thereIsCreditCardWithLimit(BigDecimal limit) {
CreditCard creditCard = new CreditCard(limit);
return creditCardRepository.save(creditCard).getId();
}
void clientWantsToWithdraw(BigDecimal amount, UUID cardId) {
restTemplate.postForEntity("/withdrawals", new WithdrawalCommand(cardId, amount), Void.class);
}
void thereIsOneWithdrawalOf(BigDecimal amount, UUID cardId) {
Map<String, Object> params = new HashMap<>();
params.put("uuid", cardId);
List<WithdrawalDto> withdrawals =
restTemplate.exchange(
"/withdrawals?cardId={uuid}",
GET, null,
new ParameterizedTypeReference<List<WithdrawalDto>>() {
},
params)
.getBody();
assertThat(withdrawals).hasSize(1);
assertThat(withdrawals.get(0).getAmount()).isEqualByComparingTo(amount);
}
}