Skip to content

Commit ac468bb

Browse files
committed
Added high level lottery test
1 parent 4bb7dda commit ac468bb

File tree

7 files changed

+116
-7
lines changed

7 files changed

+116
-7
lines changed

hexagonal/src/main/java/com/iluwatar/hexagonal/database/LotteryTicketRepositoryMock.java renamed to hexagonal/src/main/java/com/iluwatar/hexagonal/adapter/LotteryTicketRepositoryMock.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
* THE SOFTWARE.
2222
*/
23-
package com.iluwatar.hexagonal.database;
23+
package com.iluwatar.hexagonal.adapter;
2424

2525
import java.util.HashMap;
2626
import java.util.Map;

hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryAdministrationImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import java.util.Map;
2626

27-
import com.iluwatar.hexagonal.database.LotteryTicketRepositoryMock;
27+
import com.iluwatar.hexagonal.adapter.LotteryTicketRepositoryMock;
2828

2929
/**
3030
*

hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import java.util.Optional;
2626

27-
import com.iluwatar.hexagonal.database.LotteryTicketRepositoryMock;
27+
import com.iluwatar.hexagonal.adapter.LotteryTicketRepositoryMock;
2828
import com.iluwatar.hexagonal.domain.LotteryTicketCheckResult.CheckResult;
2929

3030
/**

hexagonal/src/test/java/com/iluwatar/hexagonal/domain/LotteryAdministrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.junit.Before;
2828
import org.junit.Test;
2929

30-
import com.iluwatar.hexagonal.database.LotteryTicketRepositoryMock;
30+
import com.iluwatar.hexagonal.adapter.LotteryTicketRepositoryMock;
3131

3232
/**
3333
*
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.hexagonal.domain;
24+
25+
import static org.junit.Assert.assertEquals;
26+
import static org.junit.Assert.assertTrue;
27+
28+
import java.util.Arrays;
29+
import java.util.HashSet;
30+
import java.util.Map;
31+
import java.util.Optional;
32+
33+
import org.junit.Before;
34+
import org.junit.Test;
35+
36+
import com.iluwatar.hexagonal.adapter.LotteryTicketRepositoryMock;
37+
import com.iluwatar.hexagonal.domain.LotteryTicketCheckResult.CheckResult;
38+
39+
/**
40+
*
41+
* Test the lottery system
42+
*
43+
*/
44+
public class LotteryTest {
45+
46+
private final LotteryAdministration admin = new LotteryAdministrationImpl();
47+
private final LotteryService service = new LotteryServiceImpl();
48+
private final LotteryTicketRepository repository = new LotteryTicketRepositoryMock();
49+
50+
@Before
51+
public void clear() {
52+
repository.deleteAll();
53+
}
54+
55+
@Test
56+
public void testLottery() {
57+
58+
// admin resets the lottery
59+
admin.resetLottery();
60+
assertEquals(admin.getAllSubmittedTickets().size(), 0);
61+
62+
// players submit the lottery tickets
63+
Optional<LotteryTicketId> ticket1 = service.submitTicket(LotteryTestUtils.createLotteryTicket("[email protected]",
64+
"123-12312", "+32425255", new HashSet<>(Arrays.asList(1, 2, 3, 4))));
65+
assertTrue(ticket1.isPresent());
66+
Optional<LotteryTicketId> ticket2 = service.submitTicket(LotteryTestUtils.createLotteryTicket("[email protected]",
67+
"123-12345", "+32423455", new HashSet<>(Arrays.asList(11, 12, 13, 14))));
68+
assertTrue(ticket2.isPresent());
69+
Optional<LotteryTicketId> ticket3 = service.submitTicket(LotteryTestUtils.createLotteryTicket("[email protected]",
70+
"123-12367", "+32421255", new HashSet<>(Arrays.asList(6, 8, 13, 19))));
71+
assertTrue(ticket3.isPresent());
72+
assertEquals(admin.getAllSubmittedTickets().size(), 3);
73+
74+
// perform lottery
75+
LotteryNumbers winningNumbers = admin.performLottery();
76+
77+
// cheat a bit for testing sake, use winning numbers to submit another ticket
78+
Optional<LotteryTicketId> ticket4 = service.submitTicket(LotteryTestUtils.createLotteryTicket("[email protected]",
79+
"123-12399", "+12421255", winningNumbers.getNumbers()));
80+
assertTrue(ticket4.isPresent());
81+
assertEquals(admin.getAllSubmittedTickets().size(), 4);
82+
83+
// check winners
84+
Map<LotteryTicketId, LotteryTicket> tickets = admin.getAllSubmittedTickets();
85+
for (LotteryTicketId id: tickets.keySet()) {
86+
LotteryTicketCheckResult checkResult = service.checkTicketForPrize(id, winningNumbers);
87+
assertTrue(checkResult.getResult() != CheckResult.TICKET_NOT_SUBMITTED);
88+
if (checkResult.getResult().equals(CheckResult.WIN_PRIZE)) {
89+
assertTrue(checkResult.getPrizeAmount() > 0);
90+
} else if (checkResult.getResult().equals(CheckResult.WIN_PRIZE)) {
91+
assertEquals(checkResult.getPrizeAmount(), 0);
92+
}
93+
}
94+
95+
// check another ticket that has not been submitted
96+
LotteryTicketCheckResult checkResult = service.checkTicketForPrize(new LotteryTicketId(), winningNumbers);
97+
assertTrue(checkResult.getResult() == CheckResult.TICKET_NOT_SUBMITTED);
98+
assertEquals(checkResult.getPrizeAmount(), 0);
99+
}
100+
}

hexagonal/src/test/java/com/iluwatar/hexagonal/domain/LotteryTestUtils.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import java.util.Arrays;
2626
import java.util.HashSet;
27+
import java.util.Set;
2728

2829
/**
2930
*
@@ -36,8 +37,16 @@ public class LotteryTestUtils {
3637
* @return lottery ticket
3738
*/
3839
public static LotteryTicket createLotteryTicket() {
39-
PlayerDetails details = PlayerDetails.create("[email protected]", "12231-213132", "+99324554");
40-
LotteryNumbers numbers = LotteryNumbers.create(new HashSet<>(Arrays.asList(1, 2, 3, 4)));
40+
return createLotteryTicket("[email protected]", "12231-213132", "+99324554", new HashSet<>(Arrays.asList(1, 2, 3, 4)));
41+
}
42+
43+
/**
44+
* @return lottery ticket
45+
*/
46+
public static LotteryTicket createLotteryTicket(String email, String account, String phone,
47+
Set<Integer> givenNumbers) {
48+
PlayerDetails details = PlayerDetails.create(email, account, phone);
49+
LotteryNumbers numbers = LotteryNumbers.create(givenNumbers);
4150
return LotteryTicket.create(details, numbers);
4251
}
4352
}

hexagonal/src/test/java/com/iluwatar/hexagonal/domain/LotteryTicketRepositoryTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import org.junit.Before;
3131
import org.junit.Test;
3232

33-
import com.iluwatar.hexagonal.database.LotteryTicketRepositoryMock;
33+
import com.iluwatar.hexagonal.adapter.LotteryTicketRepositoryMock;
3434

3535
/**
3636
*

0 commit comments

Comments
 (0)