|
1 | 1 | package com.baeldung.tagging; |
2 | 2 |
|
3 | 3 | import java.io.IOException; |
| 4 | +import java.util.Arrays; |
4 | 5 | import java.util.List; |
| 6 | +import java.util.stream.StreamSupport; |
5 | 7 |
|
6 | | -import org.junit.Before; |
7 | | -import org.junit.Test; |
8 | 8 | import org.junit.After; |
9 | 9 | import org.junit.Assert; |
| 10 | +import org.junit.Before; |
| 11 | +import org.junit.Test; |
10 | 12 |
|
11 | 13 | /** |
12 | 14 | * Test for {@link TagRepository}. |
@@ -41,7 +43,7 @@ public void givenTagRepository_whenPostsWithAtLeastOneTagMongoDB_then3Results() |
41 | 43 | results.forEach(post -> { |
42 | 44 | Assert.assertTrue(post.getTags().contains("MongoDB")); |
43 | 45 | }); |
44 | | - |
| 46 | + |
45 | 47 | } |
46 | 48 |
|
47 | 49 | /** |
@@ -109,14 +111,59 @@ public void givenTagRepository_whenPostsWithoutTagsMongoDB_then1Result() { |
109 | 111 | public void givenTagRepository_whenPostsWithoutTagsMongoDBJava8_then0Results() { |
110 | 112 | List<Post> results = repository.postsWithoutTags("MongoDB", "Java 8"); |
111 | 113 | results.forEach(System.out::println); |
112 | | - |
| 114 | + |
113 | 115 | Assert.assertEquals(0, results.size()); |
114 | 116 | results.forEach(post -> { |
115 | 117 | Assert.assertFalse(post.getTags().contains("MongoDB")); |
116 | 118 | Assert.assertFalse(post.getTags().contains("Java 8")); |
117 | 119 | }); |
118 | 120 | } |
119 | 121 |
|
| 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 | + |
120 | 167 | /** |
121 | 168 | * Cleans up the test by deallocating memory. |
122 | 169 | * |
|
0 commit comments