Merge pull request #8125 from eugenp/revert-8119-BAEL-3275-2

Revert "BAEL-3275: Using blocking queue for pub-sub"
This commit is contained in:
Eric Martin
2019-10-31 20:43:47 -05:00
committed by GitHub
parent db85c8f275
commit 3225470df5
20543 changed files with 1642750 additions and 0 deletions
@@ -0,0 +1,72 @@
package com.baeldung;
import static org.junit.Assert.assertEquals;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import de.flapdoodle.embedmongo.MongoDBRuntime;
import de.flapdoodle.embedmongo.MongodExecutable;
import de.flapdoodle.embedmongo.MongodProcess;
import de.flapdoodle.embedmongo.config.MongodConfig;
import de.flapdoodle.embedmongo.distribution.Version;
import de.flapdoodle.embedmongo.runtime.Network;
public class AppLiveTest {
private static final String DB_NAME = "myMongoDb";
private MongodExecutable mongodExe;
private MongodProcess mongod;
private Mongo mongo;
private DB db;
private DBCollection collection;
@Before
public void setup() throws Exception {
// Creating Mongodbruntime instance
MongoDBRuntime runtime = MongoDBRuntime.getDefaultInstance();
// Creating MongodbExecutable
mongodExe = runtime.prepare(new MongodConfig(Version.V2_0_1, 12345, Network.localhostIsIPv6()));
// Starting Mongodb
mongod = mongodExe.start();
mongo = new Mongo("localhost", 12345);
// Creating DB
db = mongo.getDB(DB_NAME);
// Creating collection Object and adding values
collection = db.getCollection("customers");
}
@After
public void teardown() throws Exception {
mongod.stop();
mongodExe.cleanup();
}
@Test
public void testAddressPersistance() {
BasicDBObject contact = new BasicDBObject();
contact.put("name", "John");
contact.put("company", "Baeldung");
// Inserting document
collection.insert(contact);
DBCursor cursorDoc = collection.find();
BasicDBObject contact1 = new BasicDBObject();
while (cursorDoc.hasNext()) {
contact1 = (BasicDBObject) cursorDoc.next();
System.out.println(contact1);
}
assertEquals(contact1.get("name"), "John");
}
}
@@ -0,0 +1,110 @@
package com.baeldung.geo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.bson.Document;
import org.junit.Before;
import org.junit.Test;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Indexes;
import com.mongodb.client.model.geojson.Point;
import com.mongodb.client.model.geojson.Polygon;
import com.mongodb.client.model.geojson.Position;
public class MongoGeospatialLiveTest {
private MongoClient mongoClient;
private MongoDatabase db;
private MongoCollection<Document> collection;
@Before
public void setup() {
if (mongoClient == null) {
mongoClient = new MongoClient();
db = mongoClient.getDatabase("myMongoDb");
collection = db.getCollection("places");
collection.deleteMany(new Document());
collection.createIndex(Indexes.geo2dsphere("location"));
collection.insertOne(Document.parse("{'name':'Big Ben','location': {'coordinates':[-0.1268194,51.5007292],'type':'Point'}}"));
collection.insertOne(Document.parse("{'name':'Hyde Park','location': {'coordinates': [[[-0.159381,51.513126],[-0.189615,51.509928],[-0.187373,51.502442], [-0.153019,51.503464],[-0.159381,51.513126]]],'type':'Polygon'}}"));
}
}
@Test
public void givenNearbyLocation_whenSearchNearby_thenFound() {
Point currentLoc = new Point(new Position(-0.126821, 51.495885));
FindIterable<Document> result = collection.find(Filters.near("location", currentLoc, 1000.0, 10.0));
assertNotNull(result.first());
assertEquals("Big Ben", result.first().get("name"));
}
@Test
public void givenFarLocation_whenSearchNearby_thenNotFound() {
Point currentLoc = new Point(new Position(-0.5243333, 51.4700223));
FindIterable<Document> result = collection.find(Filters.near("location", currentLoc, 5000.0, 10.0));
assertNull(result.first());
}
@Test
public void givenNearbyLocation_whenSearchWithinCircleSphere_thenFound() {
double distanceInRad = 5.0 / 6371;
FindIterable<Document> result = collection.find(Filters.geoWithinCenterSphere("location", -0.1435083, 51.4990956, distanceInRad));
assertNotNull(result.first());
assertEquals("Big Ben", result.first().get("name"));
}
@Test
public void givenNearbyLocation_whenSearchWithinBox_thenFound() {
double lowerLeftX = -0.1427638;
double lowerLeftY = 51.4991288;
double upperRightX = -0.1256209;
double upperRightY = 51.5030272;
FindIterable<Document> result = collection.find(Filters.geoWithinBox("location", lowerLeftX, lowerLeftY, upperRightX, upperRightY));
assertNotNull(result.first());
assertEquals("Big Ben", result.first().get("name"));
}
@Test
public void givenNearbyLocation_whenSearchWithinPolygon_thenFound() {
ArrayList<List<Double>> points = new ArrayList<List<Double>>();
points.add(Arrays.asList(-0.1439, 51.4952)); // victoria station
points.add(Arrays.asList(-0.1121, 51.4989));// Lambeth North
points.add(Arrays.asList(-0.13, 51.5163));// Tottenham Court Road
points.add(Arrays.asList(-0.1439, 51.4952)); // victoria station
FindIterable<Document> result = collection.find(Filters.geoWithinPolygon("location", points));
assertNotNull(result.first());
assertEquals("Big Ben", result.first().get("name"));
}
@Test
public void givenNearbyLocation_whenSearchUsingIntersect_thenFound() {
ArrayList<Position> positions = new ArrayList<Position>();
positions.add(new Position(-0.1439, 51.4952));
positions.add(new Position(-0.1346, 51.4978));
positions.add(new Position(-0.2177, 51.5135));
positions.add(new Position(-0.1439, 51.4952));
Polygon geometry = new Polygon(positions);
FindIterable<Document> result = collection.find(Filters.geoIntersects("location", geometry));
assertNotNull(result.first());
assertEquals("Hyde Park", result.first().get("name"));
}
}
@@ -0,0 +1,131 @@
package com.baeldung.morphia;
import static dev.morphia.aggregation.Group.grouping;
import static dev.morphia.aggregation.Group.push;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Iterator;
import java.util.List;
import org.bson.types.ObjectId;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import com.baeldung.morphia.domain.Author;
import com.baeldung.morphia.domain.Book;
import com.baeldung.morphia.domain.Publisher;
import com.mongodb.MongoClient;
import dev.morphia.Datastore;
import dev.morphia.Morphia;
import dev.morphia.query.Query;
import dev.morphia.query.UpdateOperations;
@Ignore
public class MorphiaIntegrationTest {
private static Datastore datastore;
private static ObjectId id = new ObjectId();
@BeforeClass
public static void setUp() {
Morphia morphia = new Morphia();
morphia.mapPackage("com.baeldung.morphia");
datastore = morphia.createDatastore(new MongoClient(), "library");
datastore.ensureIndexes();
}
@Test
public void givenDataSource_whenCreateEntity_thenEntityCreated() {
Publisher publisher = new Publisher(id, "Awsome Publisher");
Book book = new Book("9781565927186", "Learning Java", "Tom Kirkman", 3.95, publisher);
Book companionBook = new Book("9789332575103", "Java Performance Companion", "Tom Kirkman", 1.95, publisher);
book.addCompanionBooks(companionBook);
datastore.save(companionBook);
datastore.save(book);
List<Book> books = datastore.createQuery(Book.class)
.field("title")
.contains("Learning Java")
.find()
.toList();
assertEquals(1, books.size());
assertEquals(book, books.get(0));
}
@Test
public void givenDocument_whenUpdated_thenUpdateReflected() {
Publisher publisher = new Publisher(id, "Awsome Publisher");
Book book = new Book("9781565927186", "Learning Java", "Tom Kirkman", 3.95, publisher);
datastore.save(book);
Query<Book> query = datastore.createQuery(Book.class)
.field("title")
.contains("Learning Java");
UpdateOperations<Book> updates = datastore.createUpdateOperations(Book.class)
.inc("price", 1);
datastore.update(query, updates);
List<Book> books = datastore.createQuery(Book.class)
.field("title")
.contains("Learning Java")
.find()
.toList();
assertEquals(4.95, books.get(0)
.getCost());
}
@Test
public void givenDocument_whenDeleted_thenDeleteReflected() {
Publisher publisher = new Publisher(id, "Awsome Publisher");
Book book = new Book("9781565927186", "Learning Java", "Tom Kirkman", 3.95, publisher);
datastore.save(book);
Query<Book> query = datastore.createQuery(Book.class)
.field("title")
.contains("Learning Java");
datastore.delete(query);
List<Book> books = datastore.createQuery(Book.class)
.field("title")
.contains("Learning Java")
.find()
.toList();
assertEquals(0, books.size());
}
@Test
public void givenDocument_whenAggregated_thenResultsCollected() {
Publisher publisher = new Publisher(id, "Awsome Publisher");
datastore.save(new Book("9781565927186", "Learning Java", "Tom Kirkman", 3.95, publisher));
datastore.save(new Book("9781449313142", "Learning Perl", "Mark Pence", 2.95, publisher));
datastore.save(new Book("9787564100476", "Learning Python", "Mark Pence", 5.95, publisher));
datastore.save(new Book("9781449368814", "Learning Scala", "Mark Pence", 6.95, publisher));
datastore.save(new Book("9781784392338", "Learning Go", "Jonathan Sawyer", 8.95, publisher));
Iterator<Author> authors = datastore.createAggregation(Book.class)
.group("author", grouping("books", push("title")))
.out(Author.class);
assertTrue(authors.hasNext());
}
@Test
public void givenDocument_whenProjected_thenOnlyProjectionReceived() {
Publisher publisher = new Publisher(id, "Awsome Publisher");
Book book = new Book("9781565927186", "Learning Java", "Tom Kirkman", 3.95, publisher);
datastore.save(book);
List<Book> books = datastore.createQuery(Book.class)
.field("title")
.contains("Learning Java")
.project("title", true)
.find()
.toList();
assertEquals(books.size(), 1);
assertEquals("Learning Java", books.get(0)
.getTitle());
assertNull(books.get(0)
.getAuthor());
}
}
@@ -0,0 +1,178 @@
package com.baeldung.tagging;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.StreamSupport;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
/**
* Test for {@link TagRepository}.
*
* @author Donato Rimenti
*
*/
public class TaggingLiveTest {
/**
* Object to test.
* Object to test.
*/
private TagRepository repository;
/**
* Sets the test up by instantiating the object to test.
*/
@Before
public void setup() {
repository = new TagRepository();
}
/**
* Tests {@link TagRepository#postsWithAtLeastOneTag(String...)} with 1 tag.
*/
@Test
public void givenTagRepository_whenPostsWithAtLeastOneTagMongoDB_then3Results() {
List<Post> results = repository.postsWithAtLeastOneTag("MongoDB");
results.forEach(System.out::println);
Assert.assertEquals(3, results.size());
results.forEach(post -> {
Assert.assertTrue(post.getTags().contains("MongoDB"));
});
}
/**
* Tests {@link TagRepository#postsWithAtLeastOneTag(String...)} with 2
* tags.
*/
@Test
public void givenTagRepository_whenPostsWithAtLeastOneTagMongoDBJava8_then4Results() {
List<Post> results = repository.postsWithAtLeastOneTag("MongoDB", "Java 8");
results.forEach(System.out::println);
Assert.assertEquals(4, results.size());
results.forEach(post -> {
Assert.assertTrue(post.getTags().contains("MongoDB") || post.getTags().contains("Java 8"));
});
}
/**
* Tests {@link TagRepository#postsWithAllTags(String...)} with 1 tag.
*/
@Test
public void givenTagRepository_whenPostsWithAllTagsMongoDB_then3Results() {
List<Post> results = repository.postsWithAllTags("MongoDB");
results.forEach(System.out::println);
Assert.assertEquals(3, results.size());
results.forEach(post -> {
Assert.assertTrue(post.getTags().contains("MongoDB"));
});
}
/**
* Tests {@link TagRepository#postsWithAllTags(String...)} with 2 tags.
*/
@Test
public void givenTagRepository_whenPostsWithAllTagsMongoDBJava8_then2Results() {
List<Post> results = repository.postsWithAllTags("MongoDB", "Java 8");
results.forEach(System.out::println);
Assert.assertEquals(2, results.size());
results.forEach(post -> {
Assert.assertTrue(post.getTags().contains("MongoDB"));
Assert.assertTrue(post.getTags().contains("Java 8"));
});
}
/**
* Tests {@link TagRepository#postsWithoutTags(String...)} with 1 tag.
*/
@Test
public void givenTagRepository_whenPostsWithoutTagsMongoDB_then1Result() {
List<Post> results = repository.postsWithoutTags("MongoDB");
results.forEach(System.out::println);
Assert.assertEquals(1, results.size());
results.forEach(post -> {
Assert.assertFalse(post.getTags().contains("MongoDB"));
});
}
/**
* Tests {@link TagRepository#postsWithoutTags(String...)} with 2 tags.
*/
@Test
public void givenTagRepository_whenPostsWithoutTagsMongoDBJava8_then0Results() {
List<Post> results = repository.postsWithoutTags("MongoDB", "Java 8");
results.forEach(System.out::println);
Assert.assertEquals(0, results.size());
results.forEach(post -> {
Assert.assertFalse(post.getTags().contains("MongoDB"));
Assert.assertFalse(post.getTags().contains("Java 8"));
});
}
/**
* Tests {@link TagRepository#addTags(String, List)} and
* {@link TagRepository#removeTags(String, List)}. These tests run together
* to keep the database in a consistent state.
*/
@Test
public void givenTagRepository_whenAddingRemovingElements_thenNoDuplicates() {
// Adds one element and checks the result.
boolean result = repository.addTags("Post 1", Arrays.asList("jUnit", "jUnit5"));
Assert.assertTrue(result);
// We add the same elements again to check that there's no duplication.
result = repository.addTags("Post 1", Arrays.asList("jUnit", "jUnit5"));
Assert.assertFalse(result);
// Fetches the element back to check if the elements have been added.
List<Post> postsAfterAddition = repository.postsWithAllTags("jUnit", "jUnit5");
Assert.assertEquals(1, postsAfterAddition.size());
postsAfterAddition.forEach(post -> {
Assert.assertTrue(post.getTags().contains("jUnit"));
Assert.assertTrue(post.getTags().contains("jUnit5"));
});
// Checks for duplication.
long countDuplicateTags = StreamSupport.stream(postsAfterAddition.get(0).getTags().spliterator(), false)
.filter(x -> x.equals("jUnit5")).count();
Assert.assertEquals(1, countDuplicateTags);
// Tries to remove the tags added.
result = repository.removeTags("Post 1", Arrays.asList("jUnit", "jUnit5"));
Assert.assertTrue(result);
// We remove the same elements again to check for errors.
result = repository.removeTags("Post 1", Arrays.asList("jUnit", "jUnit5"));
Assert.assertFalse(result);
// Fetches the element back to check if the elements have been removed.
List<Post> postsAfterDeletion = repository.postsWithAllTags("jUnit", "jUnit5");
Assert.assertEquals(0, postsAfterDeletion.size());
postsAfterDeletion = repository.postsWithAtLeastOneTag("jUnit");
Assert.assertEquals(0, postsAfterDeletion.size());
postsAfterDeletion = repository.postsWithAtLeastOneTag("jUnit5");
Assert.assertEquals(0, postsAfterDeletion.size());
}
/**
* Cleans up the test by deallocating memory.
*
* @throws IOException
*/
@After
public void teardown() throws IOException {
repository.close();
}
}