Hexagonal pattern: Added Mongo based ticket repository and set production configuration to use that
This commit is contained in:
@@ -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();
|
||||
|
@@ -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());
|
||||
}
|
||||
}
|
@@ -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));
|
||||
}
|
||||
}
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user