Compare commits
1 Commits
HexagonalR
...
Issue#699
Author | SHA1 | Date | |
---|---|---|---|
829df031c3 |
@ -0,0 +1,10 @@
|
|||||||
|
package com.iluwatar.balking;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An interface to simulate delay while executing some work.
|
||||||
|
*/
|
||||||
|
public interface DelayProvider {
|
||||||
|
void executeAfterDelay(long interval, TimeUnit timeUnit, Runnable task);
|
||||||
|
}
|
@ -25,17 +25,38 @@ package com.iluwatar.balking;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Washing machine class
|
* Washing machine class
|
||||||
*/
|
*/
|
||||||
public class WashingMachine {
|
public class WashingMachine {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(WashingMachine.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(WashingMachine.class);
|
||||||
|
private final DelayProvider delayProvider;
|
||||||
private WashingMachineState washingMachineState;
|
private WashingMachineState washingMachineState;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new instance of WashingMachine
|
||||||
|
*/
|
||||||
public WashingMachine() {
|
public WashingMachine() {
|
||||||
washingMachineState = WashingMachineState.ENABLED;
|
this((interval, timeUnit, task) -> {
|
||||||
|
try {
|
||||||
|
Thread.sleep(timeUnit.toMillis(interval));
|
||||||
|
} catch (InterruptedException ie) {
|
||||||
|
ie.printStackTrace();
|
||||||
|
}
|
||||||
|
task.run();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new instance of WashingMachine using provided delayProvider. This constructor is used only for
|
||||||
|
* unit testing purposes.
|
||||||
|
*/
|
||||||
|
public WashingMachine(DelayProvider delayProvider) {
|
||||||
|
this.delayProvider = delayProvider;
|
||||||
|
this.washingMachineState = WashingMachineState.ENABLED;
|
||||||
}
|
}
|
||||||
|
|
||||||
public WashingMachineState getWashingMachineState() {
|
public WashingMachineState getWashingMachineState() {
|
||||||
@ -56,12 +77,8 @@ public class WashingMachine {
|
|||||||
washingMachineState = WashingMachineState.WASHING;
|
washingMachineState = WashingMachineState.WASHING;
|
||||||
}
|
}
|
||||||
LOGGER.info("{}: Doing the washing", Thread.currentThread().getName());
|
LOGGER.info("{}: Doing the washing", Thread.currentThread().getName());
|
||||||
try {
|
|
||||||
Thread.sleep(50);
|
this.delayProvider.executeAfterDelay(50, TimeUnit.MILLISECONDS, this::endOfWashing);
|
||||||
} catch (InterruptedException ie) {
|
|
||||||
ie.printStackTrace();
|
|
||||||
}
|
|
||||||
endOfWashing();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -22,11 +22,8 @@
|
|||||||
*/
|
*/
|
||||||
package com.iluwatar.balking;
|
package com.iluwatar.balking;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Disabled;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.util.concurrent.ExecutorService;
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
@ -36,32 +33,39 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
|||||||
*/
|
*/
|
||||||
public class WashingMachineTest {
|
public class WashingMachineTest {
|
||||||
|
|
||||||
private volatile WashingMachineState machineStateGlobal;
|
private FakeDelayProvider fakeDelayProvider = new FakeDelayProvider();
|
||||||
|
|
||||||
@Disabled
|
|
||||||
@Test
|
@Test
|
||||||
public void wash() throws Exception {
|
public void wash() {
|
||||||
WashingMachine washingMachine = new WashingMachine();
|
WashingMachine washingMachine = new WashingMachine(fakeDelayProvider);
|
||||||
ExecutorService executorService = Executors.newFixedThreadPool(2);
|
|
||||||
executorService.execute(washingMachine::wash);
|
|
||||||
executorService.execute(() -> {
|
|
||||||
washingMachine.wash();
|
washingMachine.wash();
|
||||||
machineStateGlobal = washingMachine.getWashingMachineState();
|
washingMachine.wash();
|
||||||
});
|
|
||||||
executorService.shutdown();
|
WashingMachineState machineStateGlobal = washingMachine.getWashingMachineState();
|
||||||
try {
|
|
||||||
executorService.awaitTermination(10, TimeUnit.SECONDS);
|
fakeDelayProvider.task.run();
|
||||||
} catch (InterruptedException ie) {
|
|
||||||
ie.printStackTrace();
|
// washing machine remains in washing state
|
||||||
}
|
|
||||||
assertEquals(WashingMachineState.WASHING, machineStateGlobal);
|
assertEquals(WashingMachineState.WASHING, machineStateGlobal);
|
||||||
|
|
||||||
|
// washing machine goes back to enabled state
|
||||||
|
assertEquals(WashingMachineState.ENABLED, washingMachine.getWashingMachineState());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void endOfWashing() throws Exception {
|
public void endOfWashing() {
|
||||||
WashingMachine washingMachine = new WashingMachine();
|
WashingMachine washingMachine = new WashingMachine();
|
||||||
washingMachine.wash();
|
washingMachine.wash();
|
||||||
assertEquals(WashingMachineState.ENABLED, washingMachine.getWashingMachineState());
|
assertEquals(WashingMachineState.ENABLED, washingMachine.getWashingMachineState());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class FakeDelayProvider implements DelayProvider {
|
||||||
|
private Runnable task;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void executeAfterDelay(long interval, TimeUnit timeUnit, Runnable task) {
|
||||||
|
this.task = task;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -110,6 +110,21 @@ 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
|
||||||
|
@ -22,13 +22,12 @@
|
|||||||
*/
|
*/
|
||||||
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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -85,7 +84,15 @@ public class LotteryNumbers {
|
|||||||
* @return numbers as comma separated string
|
* @return numbers as comma separated string
|
||||||
*/
|
*/
|
||||||
public String getNumbersAsString() {
|
public String getNumbersAsString() {
|
||||||
return Joiner.on(',').join(numbers);
|
StringBuilder builder = new StringBuilder();
|
||||||
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -22,18 +22,17 @@
|
|||||||
*/
|
*/
|
||||||
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 AtomicInteger numAllocated = new AtomicInteger(0);
|
private static volatile int numAllocated;
|
||||||
private final int id;
|
private final int id;
|
||||||
|
|
||||||
public LotteryTicketId() {
|
public LotteryTicketId() {
|
||||||
this.id = numAllocated.incrementAndGet();
|
this.id = numAllocated + 1;
|
||||||
|
numAllocated++;
|
||||||
}
|
}
|
||||||
|
|
||||||
public LotteryTicketId(int id) {
|
public LotteryTicketId(int id) {
|
||||||
|
@ -27,10 +27,10 @@ import org.junit.jupiter.api.Test;
|
|||||||
/**
|
/**
|
||||||
* Unit test for simple App.
|
* Unit test for simple App.
|
||||||
*/
|
*/
|
||||||
class AppTest {
|
public class AppTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testApp() {
|
public void testApp() {
|
||||||
String[] args = {};
|
String[] args = {};
|
||||||
App.main(args);
|
App.main(args);
|
||||||
}
|
}
|
||||||
|
@ -32,19 +32,19 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
|||||||
* Tests for banking
|
* Tests for banking
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
class InMemoryBankTest {
|
public class InMemoryBankTest {
|
||||||
|
|
||||||
private final WireTransfers bank = new InMemoryBank();
|
private final WireTransfers bank = new InMemoryBank();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testInit() {
|
public void testInit() {
|
||||||
assertEquals(0, bank.getFunds("foo"));
|
assertEquals(bank.getFunds("foo"), 0);
|
||||||
bank.setFunds("foo", 100);
|
bank.setFunds("foo", 100);
|
||||||
assertEquals(100, bank.getFunds("foo"));
|
assertEquals(bank.getFunds("foo"), 100);
|
||||||
bank.setFunds("bar", 150);
|
bank.setFunds("bar", 150);
|
||||||
assertEquals(150, bank.getFunds("bar"));
|
assertEquals(bank.getFunds("bar"), 150);
|
||||||
assertTrue(bank.transferFunds(50, "bar", "foo"));
|
assertTrue(bank.transferFunds(50, "bar", "foo"));
|
||||||
assertEquals(150, bank.getFunds("foo"));
|
assertEquals(bank.getFunds("foo"), 150);
|
||||||
assertEquals(100, bank.getFunds("bar"));
|
assertEquals(bank.getFunds("bar"), 100);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
|||||||
* Tests for Mongo banking adapter
|
* Tests for Mongo banking adapter
|
||||||
*/
|
*/
|
||||||
@Disabled
|
@Disabled
|
||||||
class MongoBankTest {
|
public 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 @@ class MongoBankTest {
|
|||||||
private MongoBank mongoBank;
|
private MongoBank mongoBank;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void init() {
|
public 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 @@ class MongoBankTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testSetup() {
|
public void testSetup() {
|
||||||
assertEquals(0, mongoBank.getAccountsCollection().count());
|
assertEquals(0, mongoBank.getAccountsCollection().count());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testFundTransfers() {
|
public 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"));
|
||||||
|
@ -38,23 +38,23 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
|||||||
* Tests for {@link LotteryTicketRepository}
|
* Tests for {@link LotteryTicketRepository}
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
class InMemoryTicketRepositoryTest {
|
public class InMemoryTicketRepositoryTest {
|
||||||
|
|
||||||
private final LotteryTicketRepository repository = new InMemoryTicketRepository();
|
private final LotteryTicketRepository repository = new InMemoryTicketRepository();
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void clear() {
|
public void clear() {
|
||||||
repository.deleteAll();
|
repository.deleteAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testCrudOperations() {
|
public void testCrudOperations() {
|
||||||
LotteryTicketRepository repository = new InMemoryTicketRepository();
|
LotteryTicketRepository repository = new InMemoryTicketRepository();
|
||||||
assertTrue(repository.findAll().isEmpty());
|
assertEquals(repository.findAll().size(), 0);
|
||||||
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(1, repository.findAll().size());
|
assertEquals(repository.findAll().size(), 1);
|
||||||
Optional<LotteryTicket> optionalTicket = repository.findById(id.get());
|
Optional<LotteryTicket> optionalTicket = repository.findById(id.get());
|
||||||
assertTrue(optionalTicket.isPresent());
|
assertTrue(optionalTicket.isPresent());
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
class MongoTicketRepositoryTest {
|
public 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 @@ class MongoTicketRepositoryTest {
|
|||||||
private MongoTicketRepository repository;
|
private MongoTicketRepository repository;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void init() {
|
public 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 @@ class MongoTicketRepositoryTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testSetup() {
|
public void testSetup() {
|
||||||
assertEquals(1, repository.getCountersCollection().count());
|
assertTrue(repository.getCountersCollection().count() == 1);
|
||||||
assertEquals(0, repository.getTicketsCollection().count());
|
assertTrue(repository.getTicketsCollection().count() == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testNextId() {
|
public 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
|
||||||
void testCrudOperations() {
|
public 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();
|
||||||
|
@ -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.assertNotEquals;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
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}
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
class LotteryNumbersTest {
|
public class LotteryNumbersTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testGivenNumbers() {
|
public 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 @@ class LotteryNumbersTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testNumbersCantBeModified() {
|
public 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 @@ class LotteryNumbersTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testRandomNumbers() {
|
public 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
|
||||||
void testEquals() {
|
public 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)));
|
||||||
assertEquals(numbers1, numbers2);
|
assertTrue(numbers1.equals(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)));
|
||||||
assertNotEquals(numbers1, numbers3);
|
assertFalse(numbers1.equals(numbers3));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,6 @@ 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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -46,7 +45,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
|||||||
* Test the lottery system
|
* Test the lottery system
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
class LotteryTest {
|
public class LotteryTest {
|
||||||
|
|
||||||
private Injector injector;
|
private Injector injector;
|
||||||
@Inject
|
@Inject
|
||||||
@ -56,22 +55,22 @@ class LotteryTest {
|
|||||||
@Inject
|
@Inject
|
||||||
private WireTransfers wireTransfers;
|
private WireTransfers wireTransfers;
|
||||||
|
|
||||||
LotteryTest() {
|
public LotteryTest() {
|
||||||
this.injector = Guice.createInjector(new LotteryTestingModule());
|
this.injector = Guice.createInjector(new LotteryTestingModule());
|
||||||
}
|
}
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setup() {
|
public 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
|
||||||
void testLottery() {
|
public void testLottery() {
|
||||||
// admin resets the lottery
|
// admin resets the lottery
|
||||||
administration.resetLottery();
|
administration.resetLottery();
|
||||||
assertEquals(0, administration.getAllSubmittedTickets().size());
|
assertEquals(administration.getAllSubmittedTickets().size(), 0);
|
||||||
|
|
||||||
// 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",
|
||||||
@ -83,7 +82,7 @@ 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(3, administration.getAllSubmittedTickets().size());
|
assertEquals(administration.getAllSubmittedTickets().size(), 3);
|
||||||
|
|
||||||
// perform lottery
|
// perform lottery
|
||||||
LotteryNumbers winningNumbers = administration.performLottery();
|
LotteryNumbers winningNumbers = administration.performLottery();
|
||||||
@ -92,23 +91,23 @@ 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(4, administration.getAllSubmittedTickets().size());
|
assertEquals(administration.getAllSubmittedTickets().size(), 4);
|
||||||
|
|
||||||
// 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);
|
||||||
assertNotEquals(CheckResult.TICKET_NOT_SUBMITTED, checkResult.getResult());
|
assertTrue(checkResult.getResult() != CheckResult.TICKET_NOT_SUBMITTED);
|
||||||
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(0, checkResult.getPrizeAmount());
|
assertEquals(checkResult.getPrizeAmount(), 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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);
|
||||||
assertEquals(CheckResult.TICKET_NOT_SUBMITTED, checkResult.getResult());
|
assertTrue(checkResult.getResult() == CheckResult.TICKET_NOT_SUBMITTED);
|
||||||
assertEquals(0, checkResult.getPrizeAmount());
|
assertEquals(checkResult.getPrizeAmount(), 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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.assertNotEquals;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Unit tests for {@link LotteryTicketCheckResult}
|
* Unit tests for {@link LotteryTicketCheckResult}
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
class LotteryTicketCheckResultTest {
|
public class LotteryTicketCheckResultTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testEquals() {
|
public 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);
|
||||||
assertNotEquals(result1, result3);
|
assertFalse(result1.equals(result3));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for lottery ticket id
|
* Tests for lottery ticket id
|
||||||
*/
|
*/
|
||||||
class LotteryTicketIdTest {
|
public class LotteryTicketIdTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testEquals() {
|
public 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();
|
||||||
assertNotEquals(ticketId1, ticketId2);
|
assertFalse(ticketId1.equals(ticketId2));
|
||||||
assertNotEquals(ticketId2, ticketId3);
|
assertFalse(ticketId2.equals(ticketId3));
|
||||||
LotteryTicketId ticketId4 = new LotteryTicketId(ticketId1.getId());
|
LotteryTicketId ticketId4 = new LotteryTicketId(ticketId1.getId());
|
||||||
assertEquals(ticketId1, ticketId4);
|
assertTrue(ticketId1.equals(ticketId4));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -29,15 +29,14 @@ 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
|
||||||
*/
|
*/
|
||||||
class LotteryTicketTest {
|
public class LotteryTicketTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testEquals() {
|
public 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);
|
||||||
@ -48,6 +47,6 @@ 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);
|
||||||
assertNotEquals(ticket1, ticket3);
|
assertFalse(ticket1.equals(ticket3));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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.assertNotEquals;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Unit tests for {@link PlayerDetails}
|
* Unit tests for {@link PlayerDetails}
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
class PlayerDetailsTest {
|
public class PlayerDetailsTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testEquals() {
|
public 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");
|
||||||
assertNotEquals(details1, details3);
|
assertFalse(details1.equals(details3));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
|||||||
* Tests for Mongo event log
|
* Tests for Mongo event log
|
||||||
*/
|
*/
|
||||||
@Disabled
|
@Disabled
|
||||||
class MongoEventLogTest {
|
public 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 @@ class MongoEventLogTest {
|
|||||||
private MongoEventLog mongoEventLog;
|
private MongoEventLog mongoEventLog;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void init() {
|
public 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 @@ class MongoEventLogTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testSetup() {
|
public void testSetup() {
|
||||||
assertEquals(0, mongoEventLog.getEventsCollection().count());
|
assertEquals(0, mongoEventLog.getEventsCollection().count());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testFundTransfers() {
|
public 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());
|
||||||
|
Reference in New Issue
Block a user