Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
ARIES-1881 Mongo based implementation
- Added few unit tests. In order to enable use aries.events.test.mongoUri system property which should point to a running mongodb instance
- Loading branch information
Alexei Krainiouk
committed
Jan 7, 2019
1 parent
d63ee56
commit 222279761f01937af95069d7cf367732a17f3a86
Showing
6 changed files
with
168 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,71 @@ | ||
package org.apache.aries.events.mongo; | ||
|
||
import com.mongodb.MongoClientURI; | ||
import com.mongodb.client.MongoClient; | ||
import com.mongodb.client.MongoClients; | ||
import com.mongodb.client.MongoCollection; | ||
import com.mongodb.client.MongoDatabase; | ||
import org.bson.Document; | ||
import org.junit.rules.ExternalResource; | ||
|
||
import java.util.Optional; | ||
import java.util.logging.Logger; | ||
|
||
import static org.junit.Assume.assumeTrue; | ||
|
||
/** | ||
* Provides connection to an external mongodb instance | ||
* New database gets created for each test and dropped | ||
* afterwards. | ||
* Database URL must be provided by mongoUri system | ||
* property | ||
*/ | ||
public class MongoProvider extends ExternalResource { | ||
|
||
MongoCollection<Document> getCollection(String name) { | ||
return database.getCollection(name); | ||
} | ||
|
||
//********************************************* | ||
// Internals | ||
//********************************************* | ||
|
||
private static final String MONGO_URI_PROP = "aries.events.test.mongoUri"; | ||
private static final String DEFAULT_DB_NAME = "tmp_aries_events_test"; | ||
private MongoDatabase database; | ||
private MongoClient client; | ||
|
||
@Override | ||
protected void before() { | ||
String mongoUri = mongoUri(); | ||
client = MongoClients.create(mongoUri); | ||
String dbName = Optional.ofNullable(new MongoClientURI(mongoUri).getDatabase()) | ||
.orElse(DEFAULT_DB_NAME); | ||
database = client.getDatabase(dbName); | ||
} | ||
|
||
@Override | ||
protected void after() { | ||
if (database != null) { | ||
database.drop(); | ||
} | ||
if (client != null) { | ||
client.close(); | ||
} | ||
} | ||
|
||
private static String mongoUri() { | ||
String result = System.getProperty(MONGO_URI_PROP); | ||
if (result == null) { | ||
String message = "No mongo URI provided.\n" + | ||
" In order to enable mongo tests, define " + MONGO_URI_PROP + " system property\n" + | ||
" to point to a running instance of mongodb.\n" + | ||
" Example:\n" + | ||
" mvn test -D" + MONGO_URI_PROP + "=mongodb://localhost:27017/"; | ||
System.out.println("WARNING: " + message); | ||
assumeTrue(message, false); | ||
} | ||
return result; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,67 @@ | ||
package org.apache.aries.events.mongo; | ||
|
||
import com.mongodb.client.MongoCollection; | ||
import org.apache.aries.events.api.Message; | ||
import org.bson.Document; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
import java.util.AbstractMap.SimpleEntry; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.NoSuchElementException; | ||
|
||
import static java.util.Collections.emptyMap; | ||
import static org.apache.aries.events.mongo.MessageReceiverImpl.messageReceiver; | ||
import static org.apache.aries.events.mongo.MessageSenderImpl.messageSender; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
public class SenderReceiverTest { | ||
|
||
@Test public void testReplicate() throws InterruptedException { | ||
MongoCollection<Document> collection = mongoProvider.getCollection("events"); | ||
MessageSender sender = messageSender(collection, 1000 * 60 * 60 * 24 * 7); | ||
MessageReceiver receiver = messageReceiver(collection); | ||
Message expected = new Message(new byte[]{ 1, 2, 3 }, mapOf( | ||
keyVal("key1", "val1"), | ||
keyVal("key2", "val2")) | ||
); | ||
sender.send(expected); | ||
sender.send(expected); | ||
Message actual = receiver.receive(0); | ||
assertEquals(expected, actual); | ||
} | ||
|
||
@Test(expected = NoSuchElementException.class) | ||
public void testEvicted() throws InterruptedException { | ||
MongoCollection<Document> collection = mongoProvider.getCollection("events"); | ||
MessageSender sender = messageSender(collection, 0); | ||
MessageReceiver receiver = messageReceiver(collection); | ||
Message expected = new Message(new byte[] { 1, 2, 3}, emptyMap()); | ||
sender.send(expected); | ||
sender.send(expected); | ||
receiver.receive(0); | ||
} | ||
|
||
//********************************************* | ||
// Internals | ||
//********************************************* | ||
|
||
private MongoCollection<Document> collection; | ||
|
||
@Rule | ||
public MongoProvider mongoProvider = new MongoProvider(); | ||
|
||
private static Map.Entry<String, String> keyVal(String key, String value) { | ||
return new SimpleEntry<>(key, value); | ||
} | ||
|
||
private static Map<String, String> mapOf(Map.Entry<String, String>... mappings) { | ||
Map<String, String> result = new HashMap<>(); | ||
for (Map.Entry<String, String> entry : mappings) { | ||
result.put(entry.getKey(), entry.getValue()); | ||
} | ||
return result; | ||
} | ||
|
||
} |