-
Notifications
You must be signed in to change notification settings - Fork 148
/
EventsPublishingTest.java
76 lines (64 loc) · 2.79 KB
/
EventsPublishingTest.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
68
69
70
71
72
73
74
75
76
package io.dddbyexamples.cqrs;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.dddbyexamples.cqrs.persistence.CreditCardRepository;
import io.dddbyexamples.cqrs.model.CardWithdrawn;
import io.dddbyexamples.cqrs.model.CreditCard;
import io.dddbyexamples.cqrs.ui.WithdrawalCommand;
import org.junit.Before;
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.cloud.stream.messaging.Source;
import org.springframework.cloud.stream.test.binder.MessageCollector;
import org.springframework.messaging.Message;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.UUID;
import java.util.concurrent.BlockingQueue;
import static java.math.BigDecimal.TEN;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.awaitility.Duration.FIVE_SECONDS;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
public class EventsPublishingTest {
@Autowired TestRestTemplate restTemplate;
@Autowired CreditCardRepository creditCardRepository;
@Autowired MessageCollector messageCollector;
@Autowired Source source;
@Autowired ObjectMapper objectMapper;
BlockingQueue<Message<?>> events;
@Before
public void setup() {
events = messageCollector.forChannel(source.output());
}
@Test
public void shouldEventuallySendAnEventAboutCardWithdrawal() throws IOException {
// given
UUID cardUUid = thereIsCreditCardWithLimit(new BigDecimal(100));
// when
clientWantsToWithdraw(TEN, cardUUid);
// then
await().atMost(FIVE_SECONDS).until(() -> eventAboutWithdrawalWasSent(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);
}
boolean eventAboutWithdrawalWasSent(BigDecimal amount, UUID cardId) throws IOException {
Message msg = events.poll();
if (msg == null) {
return false;
}
String payload = msg.getPayload().toString();
CardWithdrawn event = objectMapper.readValue(payload, CardWithdrawn.class);
return event.getAmount().compareTo(amount) == 0 && event.getCardNo().equals(cardId);
}
}