Added high level lottery test
This commit is contained in:
parent
4bb7ddaec1
commit
ac468bb7e7
@ -20,7 +20,7 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.hexagonal.database;
|
||||
package com.iluwatar.hexagonal.adapter;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
@ -24,7 +24,7 @@ package com.iluwatar.hexagonal.domain;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import com.iluwatar.hexagonal.database.LotteryTicketRepositoryMock;
|
||||
import com.iluwatar.hexagonal.adapter.LotteryTicketRepositoryMock;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -24,7 +24,7 @@ package com.iluwatar.hexagonal.domain;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import com.iluwatar.hexagonal.database.LotteryTicketRepositoryMock;
|
||||
import com.iluwatar.hexagonal.adapter.LotteryTicketRepositoryMock;
|
||||
import com.iluwatar.hexagonal.domain.LotteryTicketCheckResult.CheckResult;
|
||||
|
||||
/**
|
||||
|
@ -27,7 +27,7 @@ import static org.junit.Assert.assertEquals;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.iluwatar.hexagonal.database.LotteryTicketRepositoryMock;
|
||||
import com.iluwatar.hexagonal.adapter.LotteryTicketRepositoryMock;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -0,0 +1,100 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.hexagonal.domain;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.iluwatar.hexagonal.adapter.LotteryTicketRepositoryMock;
|
||||
import com.iluwatar.hexagonal.domain.LotteryTicketCheckResult.CheckResult;
|
||||
|
||||
/**
|
||||
*
|
||||
* Test the lottery system
|
||||
*
|
||||
*/
|
||||
public class LotteryTest {
|
||||
|
||||
private final LotteryAdministration admin = new LotteryAdministrationImpl();
|
||||
private final LotteryService service = new LotteryServiceImpl();
|
||||
private final LotteryTicketRepository repository = new LotteryTicketRepositoryMock();
|
||||
|
||||
@Before
|
||||
public void clear() {
|
||||
repository.deleteAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLottery() {
|
||||
|
||||
// admin resets the lottery
|
||||
admin.resetLottery();
|
||||
assertEquals(admin.getAllSubmittedTickets().size(), 0);
|
||||
|
||||
// players submit the lottery tickets
|
||||
Optional<LotteryTicketId> ticket1 = service.submitTicket(LotteryTestUtils.createLotteryTicket("cvt@bbb.com",
|
||||
"123-12312", "+32425255", new HashSet<>(Arrays.asList(1, 2, 3, 4))));
|
||||
assertTrue(ticket1.isPresent());
|
||||
Optional<LotteryTicketId> ticket2 = service.submitTicket(LotteryTestUtils.createLotteryTicket("ant@bac.com",
|
||||
"123-12345", "+32423455", new HashSet<>(Arrays.asList(11, 12, 13, 14))));
|
||||
assertTrue(ticket2.isPresent());
|
||||
Optional<LotteryTicketId> ticket3 = service.submitTicket(LotteryTestUtils.createLotteryTicket("arg@boo.com",
|
||||
"123-12367", "+32421255", new HashSet<>(Arrays.asList(6, 8, 13, 19))));
|
||||
assertTrue(ticket3.isPresent());
|
||||
assertEquals(admin.getAllSubmittedTickets().size(), 3);
|
||||
|
||||
// perform lottery
|
||||
LotteryNumbers winningNumbers = admin.performLottery();
|
||||
|
||||
// cheat a bit for testing sake, use winning numbers to submit another ticket
|
||||
Optional<LotteryTicketId> ticket4 = service.submitTicket(LotteryTestUtils.createLotteryTicket("lucky@orb.com",
|
||||
"123-12399", "+12421255", winningNumbers.getNumbers()));
|
||||
assertTrue(ticket4.isPresent());
|
||||
assertEquals(admin.getAllSubmittedTickets().size(), 4);
|
||||
|
||||
// check winners
|
||||
Map<LotteryTicketId, LotteryTicket> tickets = admin.getAllSubmittedTickets();
|
||||
for (LotteryTicketId id: tickets.keySet()) {
|
||||
LotteryTicketCheckResult checkResult = service.checkTicketForPrize(id, winningNumbers);
|
||||
assertTrue(checkResult.getResult() != CheckResult.TICKET_NOT_SUBMITTED);
|
||||
if (checkResult.getResult().equals(CheckResult.WIN_PRIZE)) {
|
||||
assertTrue(checkResult.getPrizeAmount() > 0);
|
||||
} else if (checkResult.getResult().equals(CheckResult.WIN_PRIZE)) {
|
||||
assertEquals(checkResult.getPrizeAmount(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
// check another ticket that has not been submitted
|
||||
LotteryTicketCheckResult checkResult = service.checkTicketForPrize(new LotteryTicketId(), winningNumbers);
|
||||
assertTrue(checkResult.getResult() == CheckResult.TICKET_NOT_SUBMITTED);
|
||||
assertEquals(checkResult.getPrizeAmount(), 0);
|
||||
}
|
||||
}
|
@ -24,6 +24,7 @@ package com.iluwatar.hexagonal.domain;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -36,8 +37,16 @@ public class LotteryTestUtils {
|
||||
* @return lottery ticket
|
||||
*/
|
||||
public static LotteryTicket createLotteryTicket() {
|
||||
PlayerDetails details = PlayerDetails.create("foo@bar.com", "12231-213132", "+99324554");
|
||||
LotteryNumbers numbers = LotteryNumbers.create(new HashSet<>(Arrays.asList(1, 2, 3, 4)));
|
||||
return createLotteryTicket("foo@bar.com", "12231-213132", "+99324554", new HashSet<>(Arrays.asList(1, 2, 3, 4)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return lottery ticket
|
||||
*/
|
||||
public static LotteryTicket createLotteryTicket(String email, String account, String phone,
|
||||
Set<Integer> givenNumbers) {
|
||||
PlayerDetails details = PlayerDetails.create(email, account, phone);
|
||||
LotteryNumbers numbers = LotteryNumbers.create(givenNumbers);
|
||||
return LotteryTicket.create(details, numbers);
|
||||
}
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ import java.util.Optional;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.iluwatar.hexagonal.database.LotteryTicketRepositoryMock;
|
||||
import com.iluwatar.hexagonal.adapter.LotteryTicketRepositoryMock;
|
||||
|
||||
/**
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user