Compare commits

..

2 Commits

19 changed files with 118 additions and 158 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 23 KiB

After

Width:  |  Height:  |  Size: 17 KiB

View File

@ -47,15 +47,13 @@ public class App {
* @throws IOException when there is a problem with the audio file loading * @throws IOException when there is a problem with the audio file loading
* @throws UnsupportedAudioFileException when the loaded audio file is unsupported * @throws UnsupportedAudioFileException when the loaded audio file is unsupported
*/ */
public static void main(String[] args) throws UnsupportedAudioFileException, IOException, InterruptedException { public static void main(String[] args) throws UnsupportedAudioFileException, IOException {
Audio audio = Audio.getInstance(); Audio.playSound(Audio.getAudioStream("./etc/Bass-Drum-1.wav"), -10.0f);
audio.playSound(audio.getAudioStream("./etc/Bass-Drum-1.wav"), -10.0f); Audio.playSound(Audio.getAudioStream("./etc/Closed-Hi-Hat-1.wav"), -8.0f);
audio.playSound(audio.getAudioStream("./etc/Closed-Hi-Hat-1.wav"), -8.0f);
System.out.println("Press Enter key to stop the program..."); System.out.println("Press Enter key to stop the program...");
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
br.read(); br.read();
} Audio.stopService();
audio.stopService();
} }
} }

View File

@ -23,9 +23,6 @@
package com.iluwatar.event.queue; package com.iluwatar.event.queue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -41,56 +38,49 @@ import javax.sound.sampled.UnsupportedAudioFileException;
* *
*/ */
public class Audio { public class Audio {
private static final Logger LOGGER = LoggerFactory.getLogger(Audio.class);
private static final Audio INSTANCE = new Audio();
private static final int MAX_PENDING = 16; private static final int MAX_PENDING = 16;
private int headIndex; private static int headIndex;
private int tailIndex; private static int tailIndex;
private volatile Thread updateThread = null; private static Thread updateThread = null;
private PlayMessage[] pendingAudio = new PlayMessage[MAX_PENDING]; private static PlayMessage[] pendingAudio = new PlayMessage[MAX_PENDING];
// Visible only for testing purposes
Audio() {
}
public static Audio getInstance() {
return INSTANCE;
}
/** /**
* This method stops the Update Method's thread and waits till service stops. * This method stops the Update Method's thread.
*/ */
public synchronized void stopService() throws InterruptedException { public static synchronized void stopService() {
if (updateThread != null) { if (updateThread != null) {
updateThread.interrupt(); updateThread.interrupt();
} }
updateThread.join();
updateThread = null;
} }
/** /**
* This method check the Update Method's thread is started. * This method check the Update Method's thread is started.
* @return boolean * @return boolean
*/ */
public synchronized boolean isServiceRunning() { public static synchronized boolean isServiceRunning() {
return updateThread != null && updateThread.isAlive(); if (updateThread != null && updateThread.isAlive() ) {
return true;
} else {
return false;
}
} }
/** /**
* Starts the thread for the Update Method pattern if it was not started previously. * Starts the thread for the Update Method pattern if it was not started previously.
* Also when the thread is is ready initializes the indexes of the queue * Also when the thread is is ready initializes the indexes of the queue
*/ */
public void init() { public static void init() {
if (updateThread == null) { if (updateThread == null) {
updateThread = new Thread(() -> { updateThread = new Thread(new Runnable() {
while (!Thread.currentThread().isInterrupted()) { public void run() {
update(); while (!Thread.currentThread().isInterrupted()) {
Audio.update();
}
} }
}); });
} }
@ -100,7 +90,7 @@ public class Audio {
/** /**
* This is a synchronized thread starter * This is a synchronized thread starter
*/ */
private synchronized void startThread() { public static synchronized void startThread() {
if (!updateThread.isAlive()) { if (!updateThread.isAlive()) {
updateThread.start(); updateThread.start();
headIndex = 0; headIndex = 0;
@ -113,7 +103,7 @@ public class Audio {
* @param stream is the AudioInputStream for the method * @param stream is the AudioInputStream for the method
* @param volume is the level of the audio's volume * @param volume is the level of the audio's volume
*/ */
public void playSound(AudioInputStream stream, float volume) { public static void playSound(AudioInputStream stream, float volume) {
init(); init();
// Walk the pending requests. // Walk the pending requests.
for (int i = headIndex; i != tailIndex; i = (i + 1) % MAX_PENDING) { for (int i = headIndex; i != tailIndex; i = (i + 1) % MAX_PENDING) {
@ -133,7 +123,7 @@ public class Audio {
* This method uses the Update Method pattern. * This method uses the Update Method pattern.
* It takes the audio from the queue and plays it * It takes the audio from the queue and plays it
*/ */
private void update() { public static void update() {
// If there are no pending requests, do nothing. // If there are no pending requests, do nothing.
if (headIndex == tailIndex) { if (headIndex == tailIndex) {
return; return;
@ -146,11 +136,13 @@ public class Audio {
clip.open(audioStream); clip.open(audioStream);
clip.start(); clip.start();
} catch (LineUnavailableException e) { } catch (LineUnavailableException e) {
LOGGER.trace("Error occoured while loading the audio: The line is unavailable", e); System.err.println("Error occoured while loading the audio: The line is unavailable");
e.printStackTrace();
} catch (IOException e) { } catch (IOException e) {
LOGGER.trace("Input/Output error while loading the audio", e); System.err.println("Input/Output error while loading the audio");
e.printStackTrace();
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
LOGGER.trace("The system doesn't support the sound: " + e.getMessage(), e); System.err.println("The system doesn't support the sound: " + e.getMessage());
} }
} }
@ -161,7 +153,7 @@ public class Audio {
* @throws UnsupportedAudioFileException when the audio file is not supported * @throws UnsupportedAudioFileException when the audio file is not supported
* @throws IOException when the file is not readable * @throws IOException when the file is not readable
*/ */
public AudioInputStream getAudioStream(String filePath) public static AudioInputStream getAudioStream(String filePath)
throws UnsupportedAudioFileException, IOException { throws UnsupportedAudioFileException, IOException {
return AudioSystem.getAudioInputStream(new File(filePath).getAbsoluteFile()); return AudioSystem.getAudioInputStream(new File(filePath).getAbsoluteFile());
} }
@ -170,7 +162,7 @@ public class Audio {
* Returns with the message array of the queue * Returns with the message array of the queue
* @return PlayMessage[] * @return PlayMessage[]
*/ */
public PlayMessage[] getPendingAudio() { public static PlayMessage[] getPendingAudio() {
return pendingAudio; return pendingAudio;
} }

View File

@ -23,7 +23,6 @@
package com.iluwatar.event.queue; package com.iluwatar.event.queue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import javax.sound.sampled.UnsupportedAudioFileException; import javax.sound.sampled.UnsupportedAudioFileException;
@ -40,12 +39,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
*/ */
public class AudioTest { public class AudioTest {
private Audio audio;
@BeforeEach
void createAudioInstance() {
audio = new Audio();
}
/** /**
* Test here that the playSound method works correctly * Test here that the playSound method works correctly
* @throws UnsupportedAudioFileException when the audio file is not supported * @throws UnsupportedAudioFileException when the audio file is not supported
@ -54,15 +47,13 @@ public class AudioTest {
*/ */
@Test @Test
public void testPlaySound() throws UnsupportedAudioFileException, IOException, InterruptedException { public void testPlaySound() throws UnsupportedAudioFileException, IOException, InterruptedException {
audio.playSound(audio.getAudioStream("./etc/Bass-Drum-1.wav"), -10.0f); Audio.playSound(Audio.getAudioStream("./etc/Bass-Drum-1.wav"), -10.0f);
// test that service is started // test that service is started
assertTrue(audio.isServiceRunning()); assertTrue(Audio.isServiceRunning());
// adding a small pause to be sure that the sound is ended // adding a small pause to be sure that the sound is ended
Thread.sleep(5000); Thread.sleep(5000);
audio.stopService();
// test that service is finished // test that service is finished
assertFalse(audio.isServiceRunning()); assertFalse(!Audio.isServiceRunning());
} }
/** /**
@ -73,18 +64,16 @@ public class AudioTest {
*/ */
@Test @Test
public void testQueue() throws UnsupportedAudioFileException, IOException, InterruptedException { public void testQueue() throws UnsupportedAudioFileException, IOException, InterruptedException {
audio.playSound(audio.getAudioStream("./etc/Bass-Drum-1.aif"), -10.0f); Audio.playSound(Audio.getAudioStream("./etc/Bass-Drum-1.aif"), -10.0f);
audio.playSound(audio.getAudioStream("./etc/Bass-Drum-1.aif"), -10.0f); Audio.playSound(Audio.getAudioStream("./etc/Bass-Drum-1.aif"), -10.0f);
audio.playSound(audio.getAudioStream("./etc/Bass-Drum-1.aif"), -10.0f); Audio.playSound(Audio.getAudioStream("./etc/Bass-Drum-1.aif"), -10.0f);
assertTrue(audio.getPendingAudio().length > 0); assertTrue(Audio.getPendingAudio().length > 0);
// test that service is started // test that service is started
assertTrue(audio.isServiceRunning()); assertTrue(Audio.isServiceRunning());
// adding a small pause to be sure that the sound is ended // adding a small pause to be sure that the sound is ended
Thread.sleep(10000); Thread.sleep(10000);
audio.stopService();
// test that service is finished // test that service is finished
assertFalse(audio.isServiceRunning()); assertFalse(!Audio.isServiceRunning());
} }
} }

View File

@ -110,21 +110,6 @@ public class MongoTicketRepository implements LotteryTicketRepository {
return result.getInteger("seq"); return result.getInteger("seq");
} }
/**
* @return mongo client
*/
public MongoClient getMongoClient() {
return mongoClient;
}
/**
*
* @return mongo database
*/
public MongoDatabase getMongoDatabase() {
return database;
}
/** /**
* *
* @return tickets collection * @return tickets collection

View File

@ -22,12 +22,13 @@
*/ */
package com.iluwatar.hexagonal.domain; package com.iluwatar.hexagonal.domain;
import com.google.common.base.Joiner;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.PrimitiveIterator; import java.util.PrimitiveIterator;
import java.util.Random; import java.util.Random;
import java.util.Set; import java.util.Set;
import java.util.Iterator;
/** /**
* *
@ -84,15 +85,7 @@ public class LotteryNumbers {
* @return numbers as comma separated string * @return numbers as comma separated string
*/ */
public String getNumbersAsString() { public String getNumbersAsString() {
StringBuilder builder = new StringBuilder(); return Joiner.on(',').join(numbers);
Iterator<Integer> iterator = numbers.iterator();
for (int i = 0; i < NUM_NUMBERS; i++) {
builder.append(iterator.next());
if (i < NUM_NUMBERS - 1) {
builder.append(",");
}
}
return builder.toString();
} }
/** /**

View File

@ -22,17 +22,18 @@
*/ */
package com.iluwatar.hexagonal.domain; package com.iluwatar.hexagonal.domain;
import java.util.concurrent.atomic.AtomicInteger;
/** /**
* Lottery ticked id * Lottery ticked id
*/ */
public class LotteryTicketId { public class LotteryTicketId {
private static volatile int numAllocated; private static AtomicInteger numAllocated = new AtomicInteger(0);
private final int id; private final int id;
public LotteryTicketId() { public LotteryTicketId() {
this.id = numAllocated + 1; this.id = numAllocated.incrementAndGet();
numAllocated++;
} }
public LotteryTicketId(int id) { public LotteryTicketId(int id) {

View File

@ -27,10 +27,10 @@ import org.junit.jupiter.api.Test;
/** /**
* Unit test for simple App. * Unit test for simple App.
*/ */
public class AppTest { class AppTest {
@Test @Test
public void testApp() { void testApp() {
String[] args = {}; String[] args = {};
App.main(args); App.main(args);
} }

View File

@ -32,19 +32,19 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
* Tests for banking * Tests for banking
* *
*/ */
public class InMemoryBankTest { class InMemoryBankTest {
private final WireTransfers bank = new InMemoryBank(); private final WireTransfers bank = new InMemoryBank();
@Test @Test
public void testInit() { void testInit() {
assertEquals(bank.getFunds("foo"), 0); assertEquals(0, bank.getFunds("foo"));
bank.setFunds("foo", 100); bank.setFunds("foo", 100);
assertEquals(bank.getFunds("foo"), 100); assertEquals(100, bank.getFunds("foo"));
bank.setFunds("bar", 150); bank.setFunds("bar", 150);
assertEquals(bank.getFunds("bar"), 150); assertEquals(150, bank.getFunds("bar"));
assertTrue(bank.transferFunds(50, "bar", "foo")); assertTrue(bank.transferFunds(50, "bar", "foo"));
assertEquals(bank.getFunds("foo"), 150); assertEquals(150, bank.getFunds("foo"));
assertEquals(bank.getFunds("bar"), 100); assertEquals(100, bank.getFunds("bar"));
} }
} }

View File

@ -34,7 +34,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
* Tests for Mongo banking adapter * Tests for Mongo banking adapter
*/ */
@Disabled @Disabled
public class MongoBankTest { class MongoBankTest {
private static final String TEST_DB = "lotteryDBTest"; private static final String TEST_DB = "lotteryDBTest";
private static final String TEST_ACCOUNTS_COLLECTION = "testAccounts"; private static final String TEST_ACCOUNTS_COLLECTION = "testAccounts";
@ -42,7 +42,7 @@ public class MongoBankTest {
private MongoBank mongoBank; private MongoBank mongoBank;
@BeforeEach @BeforeEach
public void init() { void init() {
MongoConnectionPropertiesLoader.load(); MongoConnectionPropertiesLoader.load();
MongoClient mongoClient = new MongoClient(System.getProperty("mongo-host"), MongoClient mongoClient = new MongoClient(System.getProperty("mongo-host"),
Integer.parseInt(System.getProperty("mongo-port"))); Integer.parseInt(System.getProperty("mongo-port")));
@ -52,12 +52,12 @@ public class MongoBankTest {
} }
@Test @Test
public void testSetup() { void testSetup() {
assertEquals(0, mongoBank.getAccountsCollection().count()); assertEquals(0, mongoBank.getAccountsCollection().count());
} }
@Test @Test
public void testFundTransfers() { void testFundTransfers() {
assertEquals(0, mongoBank.getFunds("000-000")); assertEquals(0, mongoBank.getFunds("000-000"));
mongoBank.setFunds("000-000", 10); mongoBank.setFunds("000-000", 10);
assertEquals(10, mongoBank.getFunds("000-000")); assertEquals(10, mongoBank.getFunds("000-000"));

View File

@ -38,23 +38,23 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
* Tests for {@link LotteryTicketRepository} * Tests for {@link LotteryTicketRepository}
* *
*/ */
public class InMemoryTicketRepositoryTest { class InMemoryTicketRepositoryTest {
private final LotteryTicketRepository repository = new InMemoryTicketRepository(); private final LotteryTicketRepository repository = new InMemoryTicketRepository();
@BeforeEach @BeforeEach
public void clear() { void clear() {
repository.deleteAll(); repository.deleteAll();
} }
@Test @Test
public void testCrudOperations() { void testCrudOperations() {
LotteryTicketRepository repository = new InMemoryTicketRepository(); LotteryTicketRepository repository = new InMemoryTicketRepository();
assertEquals(repository.findAll().size(), 0); assertTrue(repository.findAll().isEmpty());
LotteryTicket ticket = LotteryTestUtils.createLotteryTicket(); LotteryTicket ticket = LotteryTestUtils.createLotteryTicket();
Optional<LotteryTicketId> id = repository.save(ticket); Optional<LotteryTicketId> id = repository.save(ticket);
assertTrue(id.isPresent()); assertTrue(id.isPresent());
assertEquals(repository.findAll().size(), 1); assertEquals(1, repository.findAll().size());
Optional<LotteryTicket> optionalTicket = repository.findById(id.get()); Optional<LotteryTicket> optionalTicket = repository.findById(id.get());
assertTrue(optionalTicket.isPresent()); assertTrue(optionalTicket.isPresent());
} }

View File

@ -41,7 +41,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
* Tests for Mongo based ticket repository * Tests for Mongo based ticket repository
*/ */
@Disabled @Disabled
public class MongoTicketRepositoryTest { class MongoTicketRepositoryTest {
private static final String TEST_DB = "lotteryTestDB"; private static final String TEST_DB = "lotteryTestDB";
private static final String TEST_TICKETS_COLLECTION = "lotteryTestTickets"; private static final String TEST_TICKETS_COLLECTION = "lotteryTestTickets";
@ -50,7 +50,7 @@ public class MongoTicketRepositoryTest {
private MongoTicketRepository repository; private MongoTicketRepository repository;
@BeforeEach @BeforeEach
public void init() { void init() {
MongoConnectionPropertiesLoader.load(); MongoConnectionPropertiesLoader.load();
MongoClient mongoClient = new MongoClient(System.getProperty("mongo-host"), MongoClient mongoClient = new MongoClient(System.getProperty("mongo-host"),
Integer.parseInt(System.getProperty("mongo-port"))); Integer.parseInt(System.getProperty("mongo-port")));
@ -61,20 +61,20 @@ public class MongoTicketRepositoryTest {
} }
@Test @Test
public void testSetup() { void testSetup() {
assertTrue(repository.getCountersCollection().count() == 1); assertEquals(1, repository.getCountersCollection().count());
assertTrue(repository.getTicketsCollection().count() == 0); assertEquals(0, repository.getTicketsCollection().count());
} }
@Test @Test
public void testNextId() { void testNextId() {
assertEquals(1, repository.getNextId()); assertEquals(1, repository.getNextId());
assertEquals(2, repository.getNextId()); assertEquals(2, repository.getNextId());
assertEquals(3, repository.getNextId()); assertEquals(3, repository.getNextId());
} }
@Test @Test
public void testCrudOperations() { void testCrudOperations() {
// create new lottery ticket and save it // create new lottery ticket and save it
PlayerDetails details = new PlayerDetails("foo@bar.com", "123-123", "07001234"); PlayerDetails details = new PlayerDetails("foo@bar.com", "123-123", "07001234");
LotteryNumbers random = LotteryNumbers.createRandom(); LotteryNumbers random = LotteryNumbers.createRandom();

View File

@ -28,7 +28,7 @@ import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@ -37,10 +37,10 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
* Unit tests for {@link LotteryNumbers} * Unit tests for {@link LotteryNumbers}
* *
*/ */
public class LotteryNumbersTest { class LotteryNumbersTest {
@Test @Test
public void testGivenNumbers() { void testGivenNumbers() {
LotteryNumbers numbers = LotteryNumbers.create( LotteryNumbers numbers = LotteryNumbers.create(
new HashSet<>(Arrays.asList(1, 2, 3, 4))); new HashSet<>(Arrays.asList(1, 2, 3, 4)));
assertEquals(numbers.getNumbers().size(), 4); assertEquals(numbers.getNumbers().size(), 4);
@ -51,7 +51,7 @@ public class LotteryNumbersTest {
} }
@Test @Test
public void testNumbersCantBeModified() { void testNumbersCantBeModified() {
LotteryNumbers numbers = LotteryNumbers.create( LotteryNumbers numbers = LotteryNumbers.create(
new HashSet<>(Arrays.asList(1, 2, 3, 4))); new HashSet<>(Arrays.asList(1, 2, 3, 4)));
assertThrows(UnsupportedOperationException.class, () -> { assertThrows(UnsupportedOperationException.class, () -> {
@ -60,20 +60,20 @@ public class LotteryNumbersTest {
} }
@Test @Test
public void testRandomNumbers() { void testRandomNumbers() {
LotteryNumbers numbers = LotteryNumbers.createRandom(); LotteryNumbers numbers = LotteryNumbers.createRandom();
assertEquals(numbers.getNumbers().size(), LotteryNumbers.NUM_NUMBERS); assertEquals(numbers.getNumbers().size(), LotteryNumbers.NUM_NUMBERS);
} }
@Test @Test
public void testEquals() { void testEquals() {
LotteryNumbers numbers1 = LotteryNumbers.create( LotteryNumbers numbers1 = LotteryNumbers.create(
new HashSet<>(Arrays.asList(1, 2, 3, 4))); new HashSet<>(Arrays.asList(1, 2, 3, 4)));
LotteryNumbers numbers2 = LotteryNumbers.create( LotteryNumbers numbers2 = LotteryNumbers.create(
new HashSet<>(Arrays.asList(1, 2, 3, 4))); new HashSet<>(Arrays.asList(1, 2, 3, 4)));
assertTrue(numbers1.equals(numbers2)); assertEquals(numbers1, numbers2);
LotteryNumbers numbers3 = LotteryNumbers.create( LotteryNumbers numbers3 = LotteryNumbers.create(
new HashSet<>(Arrays.asList(11, 12, 13, 14))); new HashSet<>(Arrays.asList(11, 12, 13, 14)));
assertFalse(numbers1.equals(numbers3)); assertNotEquals(numbers1, numbers3);
} }
} }

View File

@ -38,6 +38,7 @@ import java.util.Map;
import java.util.Optional; import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
@ -45,7 +46,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
* Test the lottery system * Test the lottery system
* *
*/ */
public class LotteryTest { class LotteryTest {
private Injector injector; private Injector injector;
@Inject @Inject
@ -55,22 +56,22 @@ public class LotteryTest {
@Inject @Inject
private WireTransfers wireTransfers; private WireTransfers wireTransfers;
public LotteryTest() { LotteryTest() {
this.injector = Guice.createInjector(new LotteryTestingModule()); this.injector = Guice.createInjector(new LotteryTestingModule());
} }
@BeforeEach @BeforeEach
public void setup() { void setup() {
injector.injectMembers(this); injector.injectMembers(this);
// add funds to the test player's bank account // add funds to the test player's bank account
wireTransfers.setFunds("123-12312", 100); wireTransfers.setFunds("123-12312", 100);
} }
@Test @Test
public void testLottery() { void testLottery() {
// admin resets the lottery // admin resets the lottery
administration.resetLottery(); administration.resetLottery();
assertEquals(administration.getAllSubmittedTickets().size(), 0); assertEquals(0, administration.getAllSubmittedTickets().size());
// players submit the lottery tickets // players submit the lottery tickets
Optional<LotteryTicketId> ticket1 = service.submitTicket(LotteryTestUtils.createLotteryTicket("cvt@bbb.com", Optional<LotteryTicketId> ticket1 = service.submitTicket(LotteryTestUtils.createLotteryTicket("cvt@bbb.com",
@ -82,7 +83,7 @@ public class LotteryTest {
Optional<LotteryTicketId> ticket3 = service.submitTicket(LotteryTestUtils.createLotteryTicket("arg@boo.com", Optional<LotteryTicketId> ticket3 = service.submitTicket(LotteryTestUtils.createLotteryTicket("arg@boo.com",
"123-12312", "+32421255", new HashSet<>(Arrays.asList(6, 8, 13, 19)))); "123-12312", "+32421255", new HashSet<>(Arrays.asList(6, 8, 13, 19))));
assertTrue(ticket3.isPresent()); assertTrue(ticket3.isPresent());
assertEquals(administration.getAllSubmittedTickets().size(), 3); assertEquals(3, administration.getAllSubmittedTickets().size());
// perform lottery // perform lottery
LotteryNumbers winningNumbers = administration.performLottery(); LotteryNumbers winningNumbers = administration.performLottery();
@ -91,23 +92,23 @@ public class LotteryTest {
Optional<LotteryTicketId> ticket4 = service.submitTicket(LotteryTestUtils.createLotteryTicket("lucky@orb.com", Optional<LotteryTicketId> ticket4 = service.submitTicket(LotteryTestUtils.createLotteryTicket("lucky@orb.com",
"123-12312", "+12421255", winningNumbers.getNumbers())); "123-12312", "+12421255", winningNumbers.getNumbers()));
assertTrue(ticket4.isPresent()); assertTrue(ticket4.isPresent());
assertEquals(administration.getAllSubmittedTickets().size(), 4); assertEquals(4, administration.getAllSubmittedTickets().size());
// check winners // check winners
Map<LotteryTicketId, LotteryTicket> tickets = administration.getAllSubmittedTickets(); Map<LotteryTicketId, LotteryTicket> tickets = administration.getAllSubmittedTickets();
for (LotteryTicketId id: tickets.keySet()) { for (LotteryTicketId id: tickets.keySet()) {
LotteryTicketCheckResult checkResult = service.checkTicketForPrize(id, winningNumbers); LotteryTicketCheckResult checkResult = service.checkTicketForPrize(id, winningNumbers);
assertTrue(checkResult.getResult() != CheckResult.TICKET_NOT_SUBMITTED); assertNotEquals(CheckResult.TICKET_NOT_SUBMITTED, checkResult.getResult());
if (checkResult.getResult().equals(CheckResult.WIN_PRIZE)) { if (checkResult.getResult().equals(CheckResult.WIN_PRIZE)) {
assertTrue(checkResult.getPrizeAmount() > 0); assertTrue(checkResult.getPrizeAmount() > 0);
} else if (checkResult.getResult().equals(CheckResult.WIN_PRIZE)) { } else if (checkResult.getResult().equals(CheckResult.WIN_PRIZE)) {
assertEquals(checkResult.getPrizeAmount(), 0); assertEquals(0, checkResult.getPrizeAmount());
} }
} }
// check another ticket that has not been submitted // check another ticket that has not been submitted
LotteryTicketCheckResult checkResult = service.checkTicketForPrize(new LotteryTicketId(), winningNumbers); LotteryTicketCheckResult checkResult = service.checkTicketForPrize(new LotteryTicketId(), winningNumbers);
assertTrue(checkResult.getResult() == CheckResult.TICKET_NOT_SUBMITTED); assertEquals(CheckResult.TICKET_NOT_SUBMITTED, checkResult.getResult());
assertEquals(checkResult.getPrizeAmount(), 0); assertEquals(0, checkResult.getPrizeAmount());
} }
} }

View File

@ -26,21 +26,21 @@ import com.iluwatar.hexagonal.domain.LotteryTicketCheckResult.CheckResult;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals;
/** /**
* *
* Unit tests for {@link LotteryTicketCheckResult} * Unit tests for {@link LotteryTicketCheckResult}
* *
*/ */
public class LotteryTicketCheckResultTest { class LotteryTicketCheckResultTest {
@Test @Test
public void testEquals() { void testEquals() {
LotteryTicketCheckResult result1 = new LotteryTicketCheckResult(CheckResult.NO_PRIZE); LotteryTicketCheckResult result1 = new LotteryTicketCheckResult(CheckResult.NO_PRIZE);
LotteryTicketCheckResult result2 = new LotteryTicketCheckResult(CheckResult.NO_PRIZE); LotteryTicketCheckResult result2 = new LotteryTicketCheckResult(CheckResult.NO_PRIZE);
assertEquals(result1, result2); assertEquals(result1, result2);
LotteryTicketCheckResult result3 = new LotteryTicketCheckResult(CheckResult.WIN_PRIZE, 300000); LotteryTicketCheckResult result3 = new LotteryTicketCheckResult(CheckResult.WIN_PRIZE, 300000);
assertFalse(result1.equals(result3)); assertNotEquals(result1, result3);
} }
} }

View File

@ -24,22 +24,22 @@ package com.iluwatar.hexagonal.domain;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertNotEquals;
/** /**
* Tests for lottery ticket id * Tests for lottery ticket id
*/ */
public class LotteryTicketIdTest { class LotteryTicketIdTest {
@Test @Test
public void testEquals() { void testEquals() {
LotteryTicketId ticketId1 = new LotteryTicketId(); LotteryTicketId ticketId1 = new LotteryTicketId();
LotteryTicketId ticketId2 = new LotteryTicketId(); LotteryTicketId ticketId2 = new LotteryTicketId();
LotteryTicketId ticketId3 = new LotteryTicketId(); LotteryTicketId ticketId3 = new LotteryTicketId();
assertFalse(ticketId1.equals(ticketId2)); assertNotEquals(ticketId1, ticketId2);
assertFalse(ticketId2.equals(ticketId3)); assertNotEquals(ticketId2, ticketId3);
LotteryTicketId ticketId4 = new LotteryTicketId(ticketId1.getId()); LotteryTicketId ticketId4 = new LotteryTicketId(ticketId1.getId());
assertTrue(ticketId1.equals(ticketId4)); assertEquals(ticketId1, ticketId4);
} }
} }

View File

@ -29,14 +29,15 @@ import java.util.HashSet;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
/** /**
* Test Lottery Tickets for equality * Test Lottery Tickets for equality
*/ */
public class LotteryTicketTest { class LotteryTicketTest {
@Test @Test
public void testEquals() { void testEquals() {
PlayerDetails details1 = new PlayerDetails("bob@foo.bar", "1212-121212", "+34332322"); PlayerDetails details1 = new PlayerDetails("bob@foo.bar", "1212-121212", "+34332322");
LotteryNumbers numbers1 = LotteryNumbers.create(new HashSet<Integer>(Arrays.asList(1, 2, 3, 4))); LotteryNumbers numbers1 = LotteryNumbers.create(new HashSet<Integer>(Arrays.asList(1, 2, 3, 4)));
LotteryTicket ticket1 = new LotteryTicket(new LotteryTicketId(), details1, numbers1); LotteryTicket ticket1 = new LotteryTicket(new LotteryTicketId(), details1, numbers1);
@ -47,6 +48,6 @@ public class LotteryTicketTest {
PlayerDetails details3 = new PlayerDetails("elsa@foo.bar", "1223-121212", "+49332322"); PlayerDetails details3 = new PlayerDetails("elsa@foo.bar", "1223-121212", "+49332322");
LotteryNumbers numbers3 = LotteryNumbers.create(new HashSet<Integer>(Arrays.asList(1, 2, 3, 8))); LotteryNumbers numbers3 = LotteryNumbers.create(new HashSet<Integer>(Arrays.asList(1, 2, 3, 8)));
LotteryTicket ticket3 = new LotteryTicket(new LotteryTicketId(), details3, numbers3); LotteryTicket ticket3 = new LotteryTicket(new LotteryTicketId(), details3, numbers3);
assertFalse(ticket1.equals(ticket3)); assertNotEquals(ticket1, ticket3);
} }
} }

View File

@ -25,21 +25,21 @@ package com.iluwatar.hexagonal.domain;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals;
/** /**
* *
* Unit tests for {@link PlayerDetails} * Unit tests for {@link PlayerDetails}
* *
*/ */
public class PlayerDetailsTest { class PlayerDetailsTest {
@Test @Test
public void testEquals() { void testEquals() {
PlayerDetails details1 = new PlayerDetails("tom@foo.bar", "11212-123434", "+12323425"); PlayerDetails details1 = new PlayerDetails("tom@foo.bar", "11212-123434", "+12323425");
PlayerDetails details2 = new PlayerDetails("tom@foo.bar", "11212-123434", "+12323425"); PlayerDetails details2 = new PlayerDetails("tom@foo.bar", "11212-123434", "+12323425");
assertEquals(details1, details2); assertEquals(details1, details2);
PlayerDetails details3 = new PlayerDetails("john@foo.bar", "16412-123439", "+34323432"); PlayerDetails details3 = new PlayerDetails("john@foo.bar", "16412-123439", "+34323432");
assertFalse(details1.equals(details3)); assertNotEquals(details1, details3);
} }
} }

View File

@ -35,7 +35,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
* Tests for Mongo event log * Tests for Mongo event log
*/ */
@Disabled @Disabled
public class MongoEventLogTest { class MongoEventLogTest {
private static final String TEST_DB = "lotteryDBTest"; private static final String TEST_DB = "lotteryDBTest";
private static final String TEST_EVENTS_COLLECTION = "testEvents"; private static final String TEST_EVENTS_COLLECTION = "testEvents";
@ -43,7 +43,7 @@ public class MongoEventLogTest {
private MongoEventLog mongoEventLog; private MongoEventLog mongoEventLog;
@BeforeEach @BeforeEach
public void init() { void init() {
MongoConnectionPropertiesLoader.load(); MongoConnectionPropertiesLoader.load();
MongoClient mongoClient = new MongoClient(System.getProperty("mongo-host"), MongoClient mongoClient = new MongoClient(System.getProperty("mongo-host"),
Integer.parseInt(System.getProperty("mongo-port"))); Integer.parseInt(System.getProperty("mongo-port")));
@ -53,12 +53,12 @@ public class MongoEventLogTest {
} }
@Test @Test
public void testSetup() { void testSetup() {
assertEquals(0, mongoEventLog.getEventsCollection().count()); assertEquals(0, mongoEventLog.getEventsCollection().count());
} }
@Test @Test
public void testFundTransfers() { void testFundTransfers() {
PlayerDetails playerDetails = new PlayerDetails("john@wayne.com", "000-000", "03432534543"); PlayerDetails playerDetails = new PlayerDetails("john@wayne.com", "000-000", "03432534543");
mongoEventLog.prizeError(playerDetails, 1000); mongoEventLog.prizeError(playerDetails, 1000);
assertEquals(1, mongoEventLog.getEventsCollection().count()); assertEquals(1, mongoEventLog.getEventsCollection().count());