Added tags CRUD operations.
This commit is contained in:
parent
53ec7a8313
commit
d192cf8c9c
|
@ -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;
|
||||
|
||||
|
|
|
@ -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<String> 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<String> 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<String>) document.get(TAGS_FIELD));
|
||||
return post;
|
||||
}
|
||||
|
|
|
@ -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<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"));
|
||||
|
@ -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<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.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue