From ac468bb7e727a56b313992f9d0eb6dfd3ab116ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ilkka=20Sepp=C3=A4l=C3=A4?= Date: Sat, 26 Mar 2016 15:54:40 +0200 Subject: [PATCH] Added high level lottery test --- .../LotteryTicketRepositoryMock.java | 2 +- .../domain/LotteryAdministrationImpl.java | 2 +- .../hexagonal/domain/LotteryServiceImpl.java | 2 +- .../domain/LotteryAdministrationTest.java | 2 +- .../hexagonal/domain/LotteryTest.java | 100 ++++++++++++++++++ .../hexagonal/domain/LotteryTestUtils.java | 13 ++- .../domain/LotteryTicketRepositoryTest.java | 2 +- 7 files changed, 116 insertions(+), 7 deletions(-) rename hexagonal/src/main/java/com/iluwatar/hexagonal/{database => adapter}/LotteryTicketRepositoryMock.java (98%) create mode 100644 hexagonal/src/test/java/com/iluwatar/hexagonal/domain/LotteryTest.java diff --git a/hexagonal/src/main/java/com/iluwatar/hexagonal/database/LotteryTicketRepositoryMock.java b/hexagonal/src/main/java/com/iluwatar/hexagonal/adapter/LotteryTicketRepositoryMock.java similarity index 98% rename from hexagonal/src/main/java/com/iluwatar/hexagonal/database/LotteryTicketRepositoryMock.java rename to hexagonal/src/main/java/com/iluwatar/hexagonal/adapter/LotteryTicketRepositoryMock.java index f297edeea..ba1cc69b9 100644 --- a/hexagonal/src/main/java/com/iluwatar/hexagonal/database/LotteryTicketRepositoryMock.java +++ b/hexagonal/src/main/java/com/iluwatar/hexagonal/adapter/LotteryTicketRepositoryMock.java @@ -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; diff --git a/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryAdministrationImpl.java b/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryAdministrationImpl.java index c4983de4a..30d4e7b8b 100644 --- a/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryAdministrationImpl.java +++ b/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryAdministrationImpl.java @@ -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; /** * diff --git a/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryServiceImpl.java b/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryServiceImpl.java index 2f5191601..4774ea3be 100644 --- a/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryServiceImpl.java +++ b/hexagonal/src/main/java/com/iluwatar/hexagonal/domain/LotteryServiceImpl.java @@ -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; /** diff --git a/hexagonal/src/test/java/com/iluwatar/hexagonal/domain/LotteryAdministrationTest.java b/hexagonal/src/test/java/com/iluwatar/hexagonal/domain/LotteryAdministrationTest.java index eabeecfa5..0bcb34048 100644 --- a/hexagonal/src/test/java/com/iluwatar/hexagonal/domain/LotteryAdministrationTest.java +++ b/hexagonal/src/test/java/com/iluwatar/hexagonal/domain/LotteryAdministrationTest.java @@ -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; /** * diff --git a/hexagonal/src/test/java/com/iluwatar/hexagonal/domain/LotteryTest.java b/hexagonal/src/test/java/com/iluwatar/hexagonal/domain/LotteryTest.java new file mode 100644 index 000000000..6135994ea --- /dev/null +++ b/hexagonal/src/test/java/com/iluwatar/hexagonal/domain/LotteryTest.java @@ -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 ticket1 = service.submitTicket(LotteryTestUtils.createLotteryTicket("cvt@bbb.com", + "123-12312", "+32425255", new HashSet<>(Arrays.asList(1, 2, 3, 4)))); + assertTrue(ticket1.isPresent()); + Optional ticket2 = service.submitTicket(LotteryTestUtils.createLotteryTicket("ant@bac.com", + "123-12345", "+32423455", new HashSet<>(Arrays.asList(11, 12, 13, 14)))); + assertTrue(ticket2.isPresent()); + Optional 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 ticket4 = service.submitTicket(LotteryTestUtils.createLotteryTicket("lucky@orb.com", + "123-12399", "+12421255", winningNumbers.getNumbers())); + assertTrue(ticket4.isPresent()); + assertEquals(admin.getAllSubmittedTickets().size(), 4); + + // check winners + Map 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); + } +} diff --git a/hexagonal/src/test/java/com/iluwatar/hexagonal/domain/LotteryTestUtils.java b/hexagonal/src/test/java/com/iluwatar/hexagonal/domain/LotteryTestUtils.java index 854230116..4a38ff3a2 100644 --- a/hexagonal/src/test/java/com/iluwatar/hexagonal/domain/LotteryTestUtils.java +++ b/hexagonal/src/test/java/com/iluwatar/hexagonal/domain/LotteryTestUtils.java @@ -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 givenNumbers) { + PlayerDetails details = PlayerDetails.create(email, account, phone); + LotteryNumbers numbers = LotteryNumbers.create(givenNumbers); return LotteryTicket.create(details, numbers); } } diff --git a/hexagonal/src/test/java/com/iluwatar/hexagonal/domain/LotteryTicketRepositoryTest.java b/hexagonal/src/test/java/com/iluwatar/hexagonal/domain/LotteryTicketRepositoryTest.java index f752d7f84..7bbff9e3f 100644 --- a/hexagonal/src/test/java/com/iluwatar/hexagonal/domain/LotteryTicketRepositoryTest.java +++ b/hexagonal/src/test/java/com/iluwatar/hexagonal/domain/LotteryTicketRepositoryTest.java @@ -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; /** *