diff --git a/persistence-modules/java-mongodb/src/main/java/com/baeldung/tagging/Post.java b/persistence-modules/java-mongodb/src/main/java/com/baeldung/tagging/Post.java index 6f054e4c39..739a7e6300 100644 --- a/persistence-modules/java-mongodb/src/main/java/com/baeldung/tagging/Post.java +++ b/persistence-modules/java-mongodb/src/main/java/com/baeldung/tagging/Post.java @@ -11,7 +11,7 @@ import java.util.List; public class Post { /** - * Title of the post. + * Title of the post. Must be unique. */ private String title; diff --git a/persistence-modules/java-mongodb/src/main/java/com/baeldung/tagging/TagRepository.java b/persistence-modules/java-mongodb/src/main/java/com/baeldung/tagging/TagRepository.java index 95332cdaf9..cb65e05f99 100644 --- a/persistence-modules/java-mongodb/src/main/java/com/baeldung/tagging/TagRepository.java +++ b/persistence-modules/java-mongodb/src/main/java/com/baeldung/tagging/TagRepository.java @@ -8,11 +8,15 @@ import java.util.stream.StreamSupport; import org.bson.Document; +import com.mongodb.BasicDBObject; +import com.mongodb.DBCollection; 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.Updates; +import com.mongodb.client.result.UpdateResult; /** * Repository used to manage tags for a blog post. @@ -89,6 +93,36 @@ public class TagRepository implements Closeable { .collect(Collectors.toList()); } + /** + * Adds a list of tags to the blog post with the given title. + * + * @param title + * the title of the blog post + * @param tags + * a list of tags to add + * @return the outcome of the operation + */ + public boolean addTags(String title, List tags) { + UpdateResult result = collection.updateOne(new BasicDBObject(DBCollection.ID_FIELD_NAME, title), + Updates.addEachToSet(TAGS_FIELD, tags)); + return result.getModifiedCount() == 1; + } + + /** + * Removes a list of tags to the blog post with the given title. + * + * @param title + * the title of the blog post + * @param tags + * a list of tags to remove + * @return the outcome of the operation + */ + public boolean removeTags(String title, List tags) { + UpdateResult result = collection.updateOne(new BasicDBObject(DBCollection.ID_FIELD_NAME, title), + Updates.pullAll(TAGS_FIELD, tags)); + return result.getModifiedCount() == 1; + } + /** * Utility method used to map a MongoDB document into a {@link Post}. * @@ -100,9 +134,9 @@ public class TagRepository implements Closeable { @SuppressWarnings("unchecked") private static Post documentToPost(Document document) { Post post = new Post(); + post.setTitle(document.getString(DBCollection.ID_FIELD_NAME)); post.setArticle(document.getString("article")); post.setAuthor(document.getString("author")); - post.setTitle(document.getString("title")); post.setTags((List) document.get(TAGS_FIELD)); return post; } diff --git a/persistence-modules/java-mongodb/src/test/java/com/baeldung/tagging/TaggingIntegrationTest.java b/persistence-modules/java-mongodb/src/test/java/com/baeldung/tagging/TaggingIntegrationTest.java index e169c3ef6f..2796bc60fb 100644 --- a/persistence-modules/java-mongodb/src/test/java/com/baeldung/tagging/TaggingIntegrationTest.java +++ b/persistence-modules/java-mongodb/src/test/java/com/baeldung/tagging/TaggingIntegrationTest.java @@ -1,12 +1,14 @@ package com.baeldung.tagging; import java.io.IOException; +import java.util.Arrays; import java.util.List; +import java.util.stream.StreamSupport; -import org.junit.Before; -import org.junit.Test; import org.junit.After; import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; /** * Test for {@link TagRepository}. @@ -41,7 +43,7 @@ public class TaggingIntegrationTest { results.forEach(post -> { Assert.assertTrue(post.getTags().contains("MongoDB")); }); - + } /** @@ -109,7 +111,7 @@ public class TaggingIntegrationTest { public void givenTagRepository_whenPostsWithoutTagsMongoDBJava8_then0Results() { List 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")); @@ -117,6 +119,51 @@ public class TaggingIntegrationTest { }); } + /** + * 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 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 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. *