-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathLottoServiceTest.java
More file actions
133 lines (110 loc) · 4.87 KB
/
LottoServiceTest.java
File metadata and controls
133 lines (110 loc) · 4.87 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package lotto.domain;
import lotto.domain.model.game.LottoGameResult;
import lotto.domain.model.lotto.*;
import lotto.domain.model.game.Rank;
import lotto.domain.model.lotto.BonusNumber;
import lotto.domain.model.lotto.LottoNumber;
import lotto.domain.model.lotto.LottoTicket;
import lotto.domain.model.lotto.WinningLottoTicket;
import lotto.domain.model.lotto.TotalTicketCount;
import lotto.domain.model.lotto.TicketCount;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
class LottoServiceTest {
private final LottoService lottoService = new LottoService();
@DisplayName("자동 로또 티켓 구매 테스트")
@Test
void purchaseOnlyAutoTickets() {
List<LottoTicket> manualTickets = new ArrayList<>();
int totalCount = 5;
TotalTicketCount totalTicketCount = new TotalTicketCount(
new TicketCount(totalCount),
new TicketCount(0)
);
List<LottoTicket> tickets = lottoService.purchaseTickets(manualTickets, totalTicketCount);
assertThat(tickets).hasSize(totalCount);
}
@DisplayName("로또 티켓 구매 수량 계산 테스트")
@ParameterizedTest
@ValueSource(ints = {1, 2, 5, 10})
void calculateTicketCount(int count) {
List<LottoTicket> manualTickets = new ArrayList<>();
TotalTicketCount totalTicketCount = new TotalTicketCount(
new TicketCount(count),
new TicketCount(0)
);
List<LottoTicket> tickets = lottoService.purchaseTickets(manualTickets, totalTicketCount);
assertThat(tickets).hasSize(count);
}
@DisplayName("로또 게임 결과 계산 테스트")
@Test
void calculateLottoGameResult() {
int purchaseAmount = 6000;
PurchaseAmount purchaseAmountObj = new PurchaseAmount(purchaseAmount);
List<LottoTicket> tickets = createLottoTickets();
WinningLottoTicket winningTicket = new WinningLottoTicket(numbers(1, 2, 3, 4, 5, 6), new BonusNumber(7));
LottoGameResult result = lottoService.draw(tickets, winningTicket);
Map<Rank, Integer> expectedRanks = Map.of(
Rank.FIRST, 1, Rank.SECOND, 1, Rank.THIRD, 1,
Rank.FOURTH, 1, Rank.FIFTH, 1, Rank.MISS, 1);
assertThat(result.getRankCountMap()).isEqualTo(expectedRanks);
double expectedYieldValue = 338_592.5;
assertThat(result.getYield(purchaseAmountObj).getValue()).isEqualTo(expectedYieldValue);
}
private List<LottoTicket> createLottoTickets() {
return List.of(
new LottoTicket(numbers(1, 2, 3, 4, 5, 6)), // 1등
new LottoTicket(numbers(1, 2, 3, 4, 5, 7)), // 2등
new LottoTicket(numbers(1, 2, 3, 4, 5, 8)), // 3등
new LottoTicket(numbers(1, 2, 3, 4, 9, 10)), // 4등
new LottoTicket(numbers(1, 2, 3, 11, 12, 13)), // 5등
new LottoTicket(numbers(1, 2, 14, 15, 16, 17)) // 낙첨
);
}
private Set<LottoNumber> numbers(final int... values) {
Set<LottoNumber> lottoNumbers = new TreeSet<>();
for (int value : values) {
lottoNumbers.add(new LottoNumber(value));
}
return lottoNumbers;
}
private Set<LottoNumber> createLottoNumbers(final int... numbers) {
Set<LottoNumber> lottoNumbers = new TreeSet<>();
for (int number : numbers) {
lottoNumbers.add(new LottoNumber(number));
}
return lottoNumbers;
}
@DisplayName("수동 티켓과 자동 티켓 함께 구매 테스트")
@Test
void purchaseTicketsWithManualAndAuto() {
List<LottoTicket> manualTickets = List.of(
new LottoTicket(numbers(1, 2, 3, 4, 5, 6)),
new LottoTicket(numbers(10, 20, 30, 40, 41, 42))
);
int manualCount = manualTickets.size();
int totalCount = 5;
int autoCount = totalCount - manualCount;
TotalTicketCount totalTicketCount = new TotalTicketCount(
new TicketCount(totalCount),
new TicketCount(manualCount)
);
List<LottoTicket> allTickets = lottoService.purchaseTickets(manualTickets, totalTicketCount);
assertThat(allTickets).hasSize(totalCount);
assertThat(allTickets.subList(0, manualCount)).containsExactlyElementsOf(manualTickets);
List<LottoTicket> autoTickets = allTickets.subList(manualCount, totalCount);
assertThat(autoTickets).hasSize(autoCount);
for (LottoTicket ticket : autoTickets) {
assertThat(ticket.getNumbers()).hasSize(6);
}
}
}