Skip to content

Commit d192cf8

Browse files
committed
Added tags CRUD operations.
1 parent 53ec7a8 commit d192cf8

3 files changed

Lines changed: 87 additions & 6 deletions

File tree

‎persistence-modules/java-mongodb/src/main/java/com/baeldung/tagging/Post.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
public class Post {
1212

1313
/**
14-
* Title of the post.
14+
* Title of the post. Must be unique.
1515
*/
1616
private String title;
1717

‎persistence-modules/java-mongodb/src/main/java/com/baeldung/tagging/TagRepository.java‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@
88

99
import org.bson.Document;
1010

11+
import com.mongodb.BasicDBObject;
12+
import com.mongodb.DBCollection;
1113
import com.mongodb.MongoClient;
1214
import com.mongodb.client.FindIterable;
1315
import com.mongodb.client.MongoCollection;
1416
import com.mongodb.client.MongoDatabase;
1517
import com.mongodb.client.model.Filters;
18+
import com.mongodb.client.model.Updates;
19+
import com.mongodb.client.result.UpdateResult;
1620

1721
/**
1822
* Repository used to manage tags for a blog post.
@@ -89,6 +93,36 @@ public List<Post> postsWithoutTags(String... tags) {
8993
.collect(Collectors.toList());
9094
}
9195

96+
/**
97+
* Adds a list of tags to the blog post with the given title.
98+
*
99+
* @param title
100+
* the title of the blog post
101+
* @param tags
102+
* a list of tags to add
103+
* @return the outcome of the operation
104+
*/
105+
public boolean addTags(String title, List<String> tags) {
106+
UpdateResult result = collection.updateOne(new BasicDBObject(DBCollection.ID_FIELD_NAME, title),
107+
Updates.addEachToSet(TAGS_FIELD, tags));
108+
return result.getModifiedCount() == 1;
109+
}
110+
111+
/**
112+
* Removes a list of tags to the blog post with the given title.
113+
*
114+
* @param title
115+
* the title of the blog post
116+
* @param tags
117+
* a list of tags to remove
118+
* @return the outcome of the operation
119+
*/
120+
public boolean removeTags(String title, List<String> tags) {
121+
UpdateResult result = collection.updateOne(new BasicDBObject(DBCollection.ID_FIELD_NAME, title),
122+
Updates.pullAll(TAGS_FIELD, tags));
123+
return result.getModifiedCount() == 1;
124+
}
125+
92126
/**
93127
* Utility method used to map a MongoDB document into a {@link Post}.
94128
*
@@ -100,9 +134,9 @@ public List<Post> postsWithoutTags(String... tags) {
100134
@SuppressWarnings("unchecked")
101135
private static Post documentToPost(Document document) {
102136
Post post = new Post();
137+
post.setTitle(document.getString(DBCollection.ID_FIELD_NAME));
103138
post.setArticle(document.getString("article"));
104139
post.setAuthor(document.getString("author"));
105-
post.setTitle(document.getString("title"));
106140
post.setTags((List<String>) document.get(TAGS_FIELD));
107141
return post;
108142
}

‎persistence-modules/java-mongodb/src/test/java/com/baeldung/tagging/TaggingIntegrationTest.java‎

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.baeldung.tagging;
22

33
import java.io.IOException;
4+
import java.util.Arrays;
45
import java.util.List;
6+
import java.util.stream.StreamSupport;
57

6-
import org.junit.Before;
7-
import org.junit.Test;
88
import org.junit.After;
99
import org.junit.Assert;
10+
import org.junit.Before;
11+
import org.junit.Test;
1012

1113
/**
1214
* Test for {@link TagRepository}.
@@ -41,7 +43,7 @@ public void givenTagRepository_whenPostsWithAtLeastOneTagMongoDB_then3Results()
4143
results.forEach(post -> {
4244
Assert.assertTrue(post.getTags().contains("MongoDB"));
4345
});
44-
46+
4547
}
4648

4749
/**
@@ -109,14 +111,59 @@ public void givenTagRepository_whenPostsWithoutTagsMongoDB_then1Result() {
109111
public void givenTagRepository_whenPostsWithoutTagsMongoDBJava8_then0Results() {
110112
List<Post> results = repository.postsWithoutTags("MongoDB", "Java 8");
111113
results.forEach(System.out::println);
112-
114+
113115
Assert.assertEquals(0, results.size());
114116
results.forEach(post -> {
115117
Assert.assertFalse(post.getTags().contains("MongoDB"));
116118
Assert.assertFalse(post.getTags().contains("Java 8"));
117119
});
118120
}
119121

122+
/**
123+
* Tests {@link TagRepository#addTags(String, List)} and
124+
* {@link TagRepository#removeTags(String, List)}. These tests run together
125+
* to keep the database in a consistent state.
126+
*/
127+
@Test
128+
public void givenTagRepository_whenAddingRemovingElements_thenNoDuplicates() {
129+
// Adds one element and checks the result.
130+
boolean result = repository.addTags("Post 1", Arrays.asList("jUnit", "jUnit5"));
131+
Assert.assertTrue(result);
132+
133+
// We add the same elements again to check that there's no duplication.
134+
result = repository.addTags("Post 1", Arrays.asList("jUnit", "jUnit5"));
135+
Assert.assertFalse(result);
136+
137+
// Fetches the element back to check if the elements have been added.
138+
List<Post> postsAfterAddition = repository.postsWithAllTags("jUnit", "jUnit5");
139+
Assert.assertEquals(1, postsAfterAddition.size());
140+
postsAfterAddition.forEach(post -> {
141+
Assert.assertTrue(post.getTags().contains("jUnit"));
142+
Assert.assertTrue(post.getTags().contains("jUnit5"));
143+
});
144+
145+
// Checks for duplication.
146+
long countDuplicateTags = StreamSupport.stream(postsAfterAddition.get(0).getTags().spliterator(), false)
147+
.filter(x -> x.equals("jUnit5")).count();
148+
Assert.assertEquals(1, countDuplicateTags);
149+
150+
// Tries to remove the tags added.
151+
result = repository.removeTags("Post 1", Arrays.asList("jUnit", "jUnit5"));
152+
Assert.assertTrue(result);
153+
154+
// We remove the same elements again to check for errors.
155+
result = repository.removeTags("Post 1", Arrays.asList("jUnit", "jUnit5"));
156+
Assert.assertFalse(result);
157+
158+
// Fetches the element back to check if the elements have been removed.
159+
List<Post> postsAfterDeletion = repository.postsWithAllTags("jUnit", "jUnit5");
160+
Assert.assertEquals(0, postsAfterDeletion.size());
161+
postsAfterDeletion = repository.postsWithAtLeastOneTag("jUnit");
162+
Assert.assertEquals(0, postsAfterDeletion.size());
163+
postsAfterDeletion = repository.postsWithAtLeastOneTag("jUnit5");
164+
Assert.assertEquals(0, postsAfterDeletion.size());
165+
}
166+
120167
/**
121168
* Cleans up the test by deallocating memory.
122169
*

0 commit comments

Comments
 (0)