Hexagonal pattern: Added Mongo based ticket repository and set production configuration to use that

This commit is contained in:
Ilkka Seppälä 2016-09-11 20:02:34 +03:00
parent a85463470e
commit 626c56730c
11 changed files with 339 additions and 14 deletions

View File

@ -54,7 +54,7 @@ public class ConsoleAdministration {
administartion.getAllSubmittedTickets().forEach((k,v)->System.out.println("Key: " + k + " Value: " + v));
} else if (cmd.equals("2")) {
LotteryNumbers numbers = administartion.performLottery();
System.out.println("The winning numbers: " + numbers);
System.out.println("The winning numbers: " + numbers.getNumbersAsString());
System.out.println("Time to reset the database for next round, eh?");
} else if (cmd.equals("3")) {
administartion.resetLottery();

View File

@ -0,0 +1,194 @@
/**
* 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.database;
import com.iluwatar.hexagonal.domain.LotteryNumbers;
import com.iluwatar.hexagonal.domain.LotteryTicket;
import com.iluwatar.hexagonal.domain.LotteryTicketId;
import com.iluwatar.hexagonal.domain.PlayerDetails;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
/**
* Mongo lottery ticket database
*/
public class MongoTicketRepository implements LotteryTicketRepository {
private static final String DEFAULT_HOST = "localhost";
private static final int DEFAULT_PORT = 27017;
private static final String DEFAULT_DB = "lotteryDB";
private static final String DEFAULT_TICKETS_COLLECTION = "lotteryTickets";
private static final String DEFAULT_COUNTERS_COLLECTION = "counters";
private MongoClient mongoClient;
private MongoDatabase database;
private MongoCollection<Document> ticketsCollection;
private MongoCollection<Document> countersCollection;
/**
* Constructor
*/
public MongoTicketRepository() {
connect();
}
/**
* Constructor accepting parameters
*/
public MongoTicketRepository(String host, int port, String dbName, String ticketsCollectionName,
String countersCollectionName) {
connect(host, port, dbName, ticketsCollectionName, countersCollectionName);
}
/**
* Connect to database with default parameters
*/
public void connect() {
connect(DEFAULT_HOST, DEFAULT_PORT, DEFAULT_DB, DEFAULT_TICKETS_COLLECTION, DEFAULT_COUNTERS_COLLECTION);
}
/**
* Connect to database with given parameters
*/
public void connect(String host, int port, String dbName, String ticketsCollectionName,
String countersCollectionName) {
if (mongoClient != null) {
mongoClient.close();
}
mongoClient = new MongoClient(host , port);
database = mongoClient.getDatabase(dbName);
ticketsCollection = database.getCollection(ticketsCollectionName);
countersCollection = database.getCollection(countersCollectionName);
if (countersCollection.count() <= 0) {
initCounters();
}
}
private void initCounters() {
Document doc = new Document("_id", "ticketId").append("seq", 1);
countersCollection.insertOne(doc);
}
/**
* @return next ticket id
*/
public int getNextId() {
Document find = new Document("_id", "ticketId");
Document increase = new Document("seq", 1);
Document update = new Document("$inc", increase);
Document result = countersCollection.findOneAndUpdate(find, update);
return result.getInteger("seq");
}
/**
* @return mongo client
*/
public MongoClient getMongoClient() {
return mongoClient;
}
/**
*
* @return mongo database
*/
public MongoDatabase getMongoDatabase() {
return database;
}
/**
*
* @return tickets collection
*/
public MongoCollection<Document> getTicketsCollection() {
return ticketsCollection;
}
/**
*
* @return counters collection
*/
public MongoCollection<Document> getCountersCollection() {
return countersCollection;
}
@Override
public Optional<LotteryTicket> findById(LotteryTicketId id) {
Document find = new Document("ticketId", id.getId());
ArrayList<Document> results = ticketsCollection.find(find).limit(1).into(new ArrayList<Document>());
if (results.size() > 0) {
LotteryTicket lotteryTicket = docToTicket(results.get(0));
return Optional.of(lotteryTicket);
} else {
return Optional.empty();
}
}
@Override
public Optional<LotteryTicketId> save(LotteryTicket ticket) {
int ticketId = getNextId();
Document doc = new Document("ticketId", ticketId);
doc.put("email", ticket.getPlayerDetails().getEmail());
doc.put("bank", ticket.getPlayerDetails().getBankAccount());
doc.put("phone", ticket.getPlayerDetails().getPhoneNumber());
doc.put("numbers", ticket.getNumbers().getNumbersAsString());
ticketsCollection.insertOne(doc);
return Optional.of(new LotteryTicketId(ticketId));
}
@Override
public Map<LotteryTicketId, LotteryTicket> findAll() {
Map<LotteryTicketId, LotteryTicket> map = new HashMap<>();
ArrayList<Document> docs = ticketsCollection.find(new Document()).into(new ArrayList<Document>());
for (Document doc: docs) {
LotteryTicket lotteryTicket = docToTicket(doc);
map.put(lotteryTicket.getId(), lotteryTicket);
}
return map;
}
@Override
public void deleteAll() {
ticketsCollection.deleteMany(new Document());
}
private LotteryTicket docToTicket(Document doc) {
PlayerDetails playerDetails = PlayerDetails.create(doc.getString("email"), doc.getString("bank"),
doc.getString("phone"));
int[] numArray = Arrays.asList(doc.getString("numbers").split(",")).stream().mapToInt(Integer::parseInt).toArray();
HashSet<Integer> numbers = new HashSet<>();
for (int num: numArray) {
numbers.add(num);
}
LotteryNumbers lotteryNumbers = LotteryNumbers.create(numbers);
return LotteryTicket.create(new LotteryTicketId(doc.getInteger("ticketId")), playerDetails, lotteryNumbers);
}
}

View File

@ -22,8 +22,10 @@
*/
package com.iluwatar.hexagonal.domain;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.PrimitiveIterator;
import java.util.Random;
import java.util.Set;
@ -78,6 +80,22 @@ public class LotteryNumbers {
public Set<Integer> getNumbers() {
return Collections.unmodifiableSet(numbers);
}
/**
* @return numbers as comma separated string
*/
public String getNumbersAsString() {
List<Integer> list = new ArrayList<>();
list.addAll(numbers);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < NUM_NUMBERS; i++) {
builder.append(list.get(i));
if (i < NUM_NUMBERS - 1) {
builder.append(",");
}
}
return builder.toString();
}
/**
* Generates 4 unique random numbers between 1-20 into numbers set.

View File

@ -29,13 +29,15 @@ package com.iluwatar.hexagonal.domain;
*/
public class LotteryTicket {
private LotteryTicketId id;
private final PlayerDetails playerDetails;
private final LotteryNumbers lotteryNumbers;
/**
* Constructor.
*/
private LotteryTicket(PlayerDetails details, LotteryNumbers numbers) {
private LotteryTicket(LotteryTicketId id, PlayerDetails details, LotteryNumbers numbers) {
this.id = id;
playerDetails = details;
lotteryNumbers = numbers;
}
@ -43,8 +45,8 @@ public class LotteryTicket {
/**
* Factory for creating lottery tickets;
*/
public static LotteryTicket create(PlayerDetails details, LotteryNumbers numbers) {
return new LotteryTicket(details, numbers);
public static LotteryTicket create(LotteryTicketId id, PlayerDetails details, LotteryNumbers numbers) {
return new LotteryTicket(id, details, numbers);
}
/**
@ -61,6 +63,20 @@ public class LotteryTicket {
return lotteryNumbers;
}
/**
* @return id
*/
public LotteryTicketId getId() {
return id;
}
/**
* set id
*/
public void setId(LotteryTicketId id) {
this.id = id;
}
@Override
public String toString() {
return playerDetails.toString() + " " + lotteryNumbers.toString();

View File

@ -25,8 +25,8 @@ package com.iluwatar.hexagonal.module;
import com.google.inject.AbstractModule;
import com.iluwatar.hexagonal.banking.InMemoryBank;
import com.iluwatar.hexagonal.banking.WireTransfers;
import com.iluwatar.hexagonal.database.InMemoryTicketRepository;
import com.iluwatar.hexagonal.database.LotteryTicketRepository;
import com.iluwatar.hexagonal.database.MongoTicketRepository;
import com.iluwatar.hexagonal.notifications.LotteryNotifications;
import com.iluwatar.hexagonal.notifications.StdOutNotifications;
@ -36,7 +36,7 @@ import com.iluwatar.hexagonal.notifications.StdOutNotifications;
public class LotteryModule extends AbstractModule {
@Override
protected void configure() {
bind(LotteryTicketRepository.class).to(InMemoryTicketRepository.class);
bind(LotteryTicketRepository.class).to(MongoTicketRepository.class);
bind(LotteryNotifications.class).to(StdOutNotifications.class);
bind(WireTransfers.class).to(InMemoryBank.class);
}

View File

@ -27,6 +27,7 @@ import com.iluwatar.hexagonal.domain.LotteryConstants;
import com.iluwatar.hexagonal.domain.LotteryNumbers;
import com.iluwatar.hexagonal.domain.LotteryService;
import com.iluwatar.hexagonal.domain.LotteryTicket;
import com.iluwatar.hexagonal.domain.LotteryTicketId;
import com.iluwatar.hexagonal.domain.PlayerDetails;
import java.util.ArrayList;
@ -94,7 +95,8 @@ public class SampleData {
*/
public static void submitTickets(LotteryService lotteryService, int numTickets) {
for (int i = 0; i < numTickets; i++) {
LotteryTicket ticket = LotteryTicket.create(getRandomPlayerDetails(), LotteryNumbers.createRandom());
LotteryTicket ticket = LotteryTicket.create(new LotteryTicketId(),
getRandomPlayerDetails(), LotteryNumbers.createRandom());
lotteryService.submitTicket(ticket);
}
}

View File

@ -83,7 +83,7 @@ public class ConsoleLottery {
chosen.add(Integer.parseInt(parts[i]));
}
LotteryNumbers lotteryNumbers = LotteryNumbers.create(chosen);
LotteryTicket lotteryTicket = LotteryTicket.create(details, lotteryNumbers);
LotteryTicket lotteryTicket = LotteryTicket.create(new LotteryTicketId(), details, lotteryNumbers);
Optional<LotteryTicketId> id = service.submitTicket(lotteryTicket);
if (id.isPresent()) {
System.out.println("Submitted lottery ticket with id: " + id.get());

View File

@ -39,7 +39,7 @@ import com.iluwatar.hexagonal.test.LotteryTestUtils;
* Tests for {@link LotteryTicketRepository}
*
*/
public class LotteryTicketRepositoryTest {
public class InMemoryTicketRepositoryTest {
private final LotteryTicketRepository repository = new InMemoryTicketRepository();

View File

@ -0,0 +1,94 @@
/**
* 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.database;
import com.iluwatar.hexagonal.domain.LotteryNumbers;
import com.iluwatar.hexagonal.domain.LotteryTicket;
import com.iluwatar.hexagonal.domain.LotteryTicketId;
import com.iluwatar.hexagonal.domain.PlayerDetails;
import com.mongodb.MongoClient;
import org.junit.Before;
import org.junit.Test;
import java.util.Optional;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* Tests for Mongo based ticket repository
*/
public class MongoTicketRepositoryTest {
private static final String TEST_HOST = "localhost";
private static final int TEST_PORT = 27017;
private static final String TEST_DB = "lotteryDB";
private static final String TEST_TICKETS_COLLECTION = "lotteryTickets";
private static final String TEST_COUNTERS_COLLECTION = "counters";
private MongoTicketRepository repository;
@Before
public void init() {
MongoClient mongoClient = new MongoClient(TEST_HOST, TEST_PORT);
mongoClient.dropDatabase(TEST_DB);
mongoClient.close();
repository = new MongoTicketRepository(TEST_HOST, TEST_PORT, TEST_DB, TEST_TICKETS_COLLECTION,
TEST_COUNTERS_COLLECTION);
}
@Test
public void testSetup() {
assertTrue(repository.getCountersCollection().count() == 1);
assertTrue(repository.getTicketsCollection().count() == 0);
}
@Test
public void testNextId() {
assertEquals(1, repository.getNextId());
assertEquals(2, repository.getNextId());
assertEquals(3, repository.getNextId());
}
@Test
public void testCrudOperations() {
// create new lottery ticket and save it
PlayerDetails details = PlayerDetails.create("foo@bar.com", "123-123", "07001234");
LotteryNumbers random = LotteryNumbers.createRandom();
LotteryTicket original = LotteryTicket.create(new LotteryTicketId(), details, random);
Optional<LotteryTicketId> saved = repository.save(original);
assertEquals(1, repository.getTicketsCollection().count());
assertTrue(saved.isPresent());
// fetch the saved lottery ticket from database and check its contents
Optional<LotteryTicket> found = repository.findById(saved.get());
assertTrue(found.isPresent());
LotteryTicket ticket = found.get();
assertEquals("foo@bar.com", ticket.getPlayerDetails().getEmail());
assertEquals("123-123", ticket.getPlayerDetails().getBankAccount());
assertEquals("07001234", ticket.getPlayerDetails().getPhoneNumber());
assertEquals(original.getNumbers(), ticket.getNumbers());
// clear the collection
repository.deleteAll();
assertEquals(0, repository.getTicketsCollection().count());
}
}

View File

@ -36,14 +36,14 @@ public class LotteryTicketTest {
public void testEquals() {
PlayerDetails details1 = PlayerDetails.create("bob@foo.bar", "1212-121212", "+34332322");
LotteryNumbers numbers1 = LotteryNumbers.create(new HashSet<Integer>(Arrays.asList(1, 2, 3, 4)));
LotteryTicket ticket1 = LotteryTicket.create(details1, numbers1);
LotteryTicket ticket1 = LotteryTicket.create(new LotteryTicketId(), details1, numbers1);
PlayerDetails details2 = PlayerDetails.create("bob@foo.bar", "1212-121212", "+34332322");
LotteryNumbers numbers2 = LotteryNumbers.create(new HashSet<Integer>(Arrays.asList(1, 2, 3, 4)));
LotteryTicket ticket2 = LotteryTicket.create(details2, numbers2);
LotteryTicket ticket2 = LotteryTicket.create(new LotteryTicketId(), details2, numbers2);
assertEquals(ticket1, ticket2);
PlayerDetails details3 = PlayerDetails.create("elsa@foo.bar", "1223-121212", "+49332322");
LotteryNumbers numbers3 = LotteryNumbers.create(new HashSet<Integer>(Arrays.asList(1, 2, 3, 8)));
LotteryTicket ticket3 = LotteryTicket.create(details3, numbers3);
LotteryTicket ticket3 = LotteryTicket.create(new LotteryTicketId(), details3, numbers3);
assertFalse(ticket1.equals(ticket3));
}
}

View File

@ -28,6 +28,7 @@ import java.util.Set;
import com.iluwatar.hexagonal.domain.LotteryNumbers;
import com.iluwatar.hexagonal.domain.LotteryTicket;
import com.iluwatar.hexagonal.domain.LotteryTicketId;
import com.iluwatar.hexagonal.domain.PlayerDetails;
/**
@ -51,6 +52,6 @@ public class LotteryTestUtils {
Set<Integer> givenNumbers) {
PlayerDetails details = PlayerDetails.create(email, account, phone);
LotteryNumbers numbers = LotteryNumbers.create(givenNumbers);
return LotteryTicket.create(details, numbers);
return LotteryTicket.create(new LotteryTicketId(), details, numbers);
}
}