|
| 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 | +} |
0 commit comments