Skip to content

Commit e685512

Browse files
committed
Hexagonal pattern: Added Mongo based banking adapter and bound it in Guice production module
1 parent 59e6a0a commit e685512

File tree

5 files changed

+208
-6
lines changed

5 files changed

+208
-6
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.hexagonal.banking;
24+
25+
import com.mongodb.MongoClient;
26+
import com.mongodb.client.MongoCollection;
27+
import com.mongodb.client.MongoDatabase;
28+
import com.mongodb.client.model.UpdateOptions;
29+
import org.bson.Document;
30+
31+
import java.util.ArrayList;
32+
33+
/**
34+
* Mongo based banking adapter
35+
*/
36+
public class MongoBank implements WireTransfers {
37+
38+
private static final String DEFAULT_HOST = "localhost";
39+
private static final int DEFAULT_PORT = 27017;
40+
private static final String DEFAULT_DB = "lotteryDB";
41+
private static final String DEFAULT_ACCOUNTS_COLLECTION = "accounts";
42+
43+
private MongoClient mongoClient;
44+
private MongoDatabase database;
45+
private MongoCollection<Document> accountsCollection;
46+
47+
/**
48+
* Constructor
49+
*/
50+
public MongoBank() {
51+
connect();
52+
}
53+
54+
/**
55+
* Constructor accepting parameters
56+
*/
57+
public MongoBank(String host, int port, String dbName, String accountsCollectionName) {
58+
connect(host, port, dbName, accountsCollectionName);
59+
}
60+
61+
/**
62+
* Connect to database with default parameters
63+
*/
64+
public void connect() {
65+
connect(DEFAULT_HOST, DEFAULT_PORT, DEFAULT_DB, DEFAULT_ACCOUNTS_COLLECTION);
66+
}
67+
68+
/**
69+
* Connect to database with given parameters
70+
*/
71+
public void connect(String host, int port, String dbName, String accountsCollectionName) {
72+
if (mongoClient != null) {
73+
mongoClient.close();
74+
}
75+
mongoClient = new MongoClient(host , port);
76+
database = mongoClient.getDatabase(dbName);
77+
accountsCollection = database.getCollection(accountsCollectionName);
78+
}
79+
80+
/**
81+
* @return mongo client
82+
*/
83+
public MongoClient getMongoClient() {
84+
return mongoClient;
85+
}
86+
87+
/**
88+
*
89+
* @return mongo database
90+
*/
91+
public MongoDatabase getMongoDatabase() {
92+
return database;
93+
}
94+
95+
/**
96+
*
97+
* @return accounts collection
98+
*/
99+
public MongoCollection<Document> getAccountsCollection() {
100+
return accountsCollection;
101+
}
102+
103+
104+
@Override
105+
public void setFunds(String bankAccount, int amount) {
106+
Document search = new Document("_id", bankAccount);
107+
Document update = new Document("_id", bankAccount).append("funds", amount);
108+
accountsCollection.updateOne(search, new Document("$set", update), new UpdateOptions().upsert(true));
109+
}
110+
111+
@Override
112+
public int getFunds(String bankAccount) {
113+
Document search = new Document("_id", bankAccount);
114+
ArrayList<Document> results = accountsCollection.find(search).limit(1).into(new ArrayList<Document>());
115+
if (results.size() > 0) {
116+
return results.get(0).getInteger("funds");
117+
} else {
118+
return 0;
119+
}
120+
}
121+
122+
@Override
123+
public boolean transferFunds(int amount, String sourceBackAccount, String destinationBankAccount) {
124+
int sourceFunds = getFunds(sourceBackAccount);
125+
if (sourceFunds < amount) {
126+
return false;
127+
} else {
128+
int destFunds = getFunds(destinationBankAccount);
129+
setFunds(sourceBackAccount, sourceFunds - amount);
130+
setFunds(destinationBankAccount, destFunds + amount);
131+
return true;
132+
}
133+
}
134+
}

hexagonal/src/main/java/com/iluwatar/hexagonal/module/LotteryModule.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
package com.iluwatar.hexagonal.module;
2424

2525
import com.google.inject.AbstractModule;
26-
import com.iluwatar.hexagonal.banking.InMemoryBank;
26+
import com.iluwatar.hexagonal.banking.MongoBank;
2727
import com.iluwatar.hexagonal.banking.WireTransfers;
2828
import com.iluwatar.hexagonal.database.LotteryTicketRepository;
2929
import com.iluwatar.hexagonal.database.MongoTicketRepository;
@@ -38,6 +38,6 @@ public class LotteryModule extends AbstractModule {
3838
protected void configure() {
3939
bind(LotteryTicketRepository.class).to(MongoTicketRepository.class);
4040
bind(LotteryNotifications.class).to(StdOutNotifications.class);
41-
bind(WireTransfers.class).to(InMemoryBank.class);
41+
bind(WireTransfers.class).to(MongoBank.class);
4242
}
4343
}

hexagonal/src/test/java/com/iluwatar/hexagonal/banking/WireTransfersTest.java renamed to hexagonal/src/test/java/com/iluwatar/hexagonal/banking/InMemoryBankTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* Tests for banking
3333
*
3434
*/
35-
public class WireTransfersTest {
35+
public class InMemoryBankTest {
3636

3737
private final WireTransfers bank = new InMemoryBank();
3838

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.hexagonal.banking;
24+
25+
import com.mongodb.MongoClient;
26+
import org.junit.Before;
27+
import org.junit.Ignore;
28+
import org.junit.Test;
29+
30+
import static org.junit.Assert.assertEquals;
31+
32+
/**
33+
* Tests for Mongo banking adapter
34+
*/
35+
@Ignore
36+
public class MongoBankTest {
37+
38+
private static final String TEST_HOST = "localhost";
39+
private static final int TEST_PORT = 27017;
40+
private static final String TEST_DB = "lotteryDBTest";
41+
private static final String TEST_ACCOUNTS_COLLECTION = "testAccounts";
42+
43+
private MongoBank mongoBank;
44+
45+
@Before
46+
public void init() {
47+
MongoClient mongoClient = new MongoClient(TEST_HOST, TEST_PORT);
48+
mongoClient.dropDatabase(TEST_DB);
49+
mongoClient.close();
50+
mongoBank = new MongoBank(TEST_HOST, TEST_PORT, TEST_DB, TEST_ACCOUNTS_COLLECTION);
51+
}
52+
53+
@Test
54+
public void testSetup() {
55+
assertEquals(0, mongoBank.getAccountsCollection().count());
56+
}
57+
58+
@Test
59+
public void testFundTransfers() {
60+
assertEquals(0, mongoBank.getFunds("000-000"));
61+
mongoBank.setFunds("000-000", 10);
62+
assertEquals(10, mongoBank.getFunds("000-000"));
63+
assertEquals(0, mongoBank.getFunds("111-111"));
64+
mongoBank.transferFunds(9, "000-000", "111-111");
65+
assertEquals(1, mongoBank.getFunds("000-000"));
66+
assertEquals(9, mongoBank.getFunds("111-111"));
67+
}
68+
}

hexagonal/src/test/java/com/iluwatar/hexagonal/database/MongoTicketRepositoryTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public class MongoTicketRepositoryTest {
4444

4545
private static final String TEST_HOST = "localhost";
4646
private static final int TEST_PORT = 27017;
47-
private static final String TEST_DB = "lotteryDB";
48-
private static final String TEST_TICKETS_COLLECTION = "lotteryTickets";
49-
private static final String TEST_COUNTERS_COLLECTION = "counters";
47+
private static final String TEST_DB = "lotteryTestDB";
48+
private static final String TEST_TICKETS_COLLECTION = "lotteryTestTickets";
49+
private static final String TEST_COUNTERS_COLLECTION = "testCounters";
5050

5151
private MongoTicketRepository repository;
5252

0 commit comments

Comments
 (0)