|
7 | 7 | import org.gluecoders.library.config.Database;
|
8 | 8 | import org.gluecoders.library.dao.config.FakeMongo;
|
9 | 9 | import org.gluecoders.library.models.Book;
|
| 10 | +import org.junit.Ignore; |
10 | 11 | import org.junit.Rule;
|
11 | 12 | import org.junit.Test;
|
12 | 13 | import org.junit.runner.RunWith;
|
|
17 | 18 | import org.springframework.test.context.junit4.SpringRunner;
|
18 | 19 |
|
19 | 20 | import java.time.Month;
|
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Arrays; |
20 | 23 | import java.util.List;
|
| 24 | +import java.util.Optional; |
| 25 | +import java.util.function.Supplier; |
21 | 26 |
|
22 | 27 | import static com.lordofthejars.nosqlunit.mongodb.MongoDbRule.MongoDbRuleBuilder.newMongoDbRule;
|
23 | 28 | import static org.junit.Assert.*;
|
24 | 29 |
|
25 | 30 | /**
|
26 | 31 | * Created by Anand_Rajneesh on 6/14/2017.
|
| 32 | + * TODO FIXME - Text based search queries are not working in test mode, Fongo not able to set up text index ? |
27 | 33 | */
|
28 | 34 | @RunWith(SpringRunner.class)
|
29 | 35 | @Import(value = {FakeMongo.class, Database.class})
|
@@ -114,4 +120,171 @@ public void deleteBook_IdNotPresent(){
|
114 | 120 | bookDao.delete(book);
|
115 | 121 | }
|
116 | 122 |
|
| 123 | + @Test |
| 124 | + @UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT, locations="/books/empty.json") |
| 125 | + public void findBooks_NoBooksAvailable(){ |
| 126 | + List<Book> books = FindTester.test(bookDao).noArgs().get(); |
| 127 | + assertTrue(books.isEmpty()); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + @UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT, locations="/books/manybooks.json") |
| 132 | + public void findBooks_BooksAvailableMatchingCategory1(){ |
| 133 | + List<Book> books = FindTester.test(bookDao).category("Technology").get(); |
| 134 | + assertTrue(books.size() == 1); |
| 135 | + assertTrue(books.get(0).getIsbnCode() == 9780471019947L); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + @UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT, locations="/books/manybooks.json") |
| 140 | + public void findBooks_BooksAvailableMatchingCategory2(){ |
| 141 | + List<Book> books = FindTester.test(bookDao).category("Science", "Java").get(); |
| 142 | + assertTrue(books.size() == 6); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + @UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT, locations="/books/manybooks.json") |
| 147 | + public void findBooks_BooksAvailableNoneMatchingCategory(){ |
| 148 | + List<Book> books = FindTester.test(bookDao).category("NeverMatching").get(); |
| 149 | + assertTrue(books.size() == 0); |
| 150 | + } |
| 151 | + |
| 152 | + @Test @Ignore |
| 153 | + @UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT, locations="/books/manybooks.json") |
| 154 | + public void findBooks_BooksAvailableMatchingAuthor(){ |
| 155 | + List<Book> books = FindTester.test(bookDao).author("Moss").get(); |
| 156 | + assertTrue(books.size() == 1); |
| 157 | + assertTrue(books.get(0).getIsbnCode() == 9780412104909L); |
| 158 | + } |
| 159 | + |
| 160 | + @Test @Ignore |
| 161 | + @UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT, locations="/books/manybooks.json") |
| 162 | + public void findBooks_BooksAvailableNotMatchingAuthor(){ |
| 163 | + List<Book> books = FindTester.test(bookDao).author("MossXYZ").get(); |
| 164 | + assertTrue(books.size() == 0); |
| 165 | + } |
| 166 | + |
| 167 | + @Test @Ignore |
| 168 | + @UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT, locations="/books/manybooks.json") |
| 169 | + public void findBooks_BooksAvailableMatchingTitle(){ |
| 170 | + List<Book> books = FindTester.test(bookDao).title("Advanced").get(); |
| 171 | + assertTrue(books.size() == 4); |
| 172 | + } |
| 173 | + |
| 174 | + @Test @Ignore |
| 175 | + @UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT, locations="/books/manybooks.json") |
| 176 | + public void findBooks_BooksAvailableNotMatchingTitle(){ |
| 177 | + List<Book> books = FindTester.test(bookDao).title("AdvancedSuper").get(); |
| 178 | + assertTrue(books.size() == 0); |
| 179 | + } |
| 180 | + |
| 181 | + @Test |
| 182 | + @UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT, locations="/books/manybooks.json") |
| 183 | + public void findBooks_BooksAvailableMatchingYear(){ |
| 184 | + List<Book> books = FindTester.test(bookDao).publishedYear("2001").get(); |
| 185 | + assertTrue(books.size() == 2); |
| 186 | + } |
| 187 | + |
| 188 | + @Test(expected = NumberFormatException.class) |
| 189 | + @UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT, locations="/books/manybooks.json") |
| 190 | + public void findBooks_BooksAvailableMatchingYear_UnhandledError(){ |
| 191 | + List<Book> books = FindTester.test(bookDao).publishedYear("2001s").get(); |
| 192 | + assertTrue(books.size() == 0); |
| 193 | + } |
| 194 | + |
| 195 | + @Test @Ignore |
| 196 | + @UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT, locations="/books/manybooks.json") |
| 197 | + public void findBooks_BooksAvailableCustom1(){ |
| 198 | + List<Book> books = FindTester.test(bookDao).title("Java").publishedYear("2001").get(); |
| 199 | + assertTrue(books.size() == 2); |
| 200 | + } |
| 201 | + |
| 202 | + @Test @Ignore |
| 203 | + @UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT, locations="/books/manybooks.json") |
| 204 | + public void findBooks_BooksAvailableCustom2(){ |
| 205 | + List<Book> books = FindTester.test(bookDao).category("Science").title("Java").publishedYear("2001").get(); |
| 206 | + assertTrue(books.size() == 0); |
| 207 | + } |
| 208 | + |
| 209 | + @Test @Ignore |
| 210 | + @UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT, locations="/books/manybooks.json") |
| 211 | + public void findBooks_BooksAvailableCustom3(){ |
| 212 | + List<Book> books = FindTester.test(bookDao).category("Science", "Java").title("Java").publishedYear("2001").get(); |
| 213 | + assertTrue(books.size() == 2); |
| 214 | + } |
| 215 | + |
| 216 | + @Test @Ignore |
| 217 | + @UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT, locations="/books/manybooks.json") |
| 218 | + public void findBooks_BooksAvailableCustom4(){ |
| 219 | + List<Book> books = FindTester.test(bookDao).title("Advanced").publishedYear("1996").get(); |
| 220 | + assertTrue(books.size() == 2); |
| 221 | + } |
| 222 | + |
| 223 | + @Test @Ignore |
| 224 | + @UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT, locations="/books/manybooks.json") |
| 225 | + public void findBooks_BooksAvailableCustom5(){ |
| 226 | + List<Book> books = FindTester.test(bookDao).title("Advanced").publishedYear("1999").get(); |
| 227 | + assertTrue(books.size() == 1); |
| 228 | + } |
| 229 | + |
| 230 | + @Test @Ignore |
| 231 | + @UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT, locations="/books/manybooks.json") |
| 232 | + public void findBooks_BooksAvailableCustom6(){ |
| 233 | + List<Book> books = FindTester.test(bookDao).author("William Harvey").get(); |
| 234 | + assertTrue(books.size() == 2); |
| 235 | + } |
| 236 | + |
| 237 | + |
| 238 | + @Test @Ignore |
| 239 | + @UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT, locations="/books/manybooks.json") |
| 240 | + public void findBooks_BooksAvailableCustom7(){ |
| 241 | + List<Book> books = FindTester.test(bookDao).author("William Harvey").category("Java").get(); |
| 242 | + assertTrue(books.size() == 1); |
| 243 | + } |
| 244 | + |
| 245 | + private static class FindTester{ |
| 246 | + |
| 247 | + private BookDao testObject; |
| 248 | + private List<String> categories = null; |
| 249 | + private String author = null; |
| 250 | + private String title = null; |
| 251 | + private String publishedYear = null; |
| 252 | + private List<Book> books = null; |
| 253 | + public static FindTester test(BookDao bookDao){ |
| 254 | + FindTester t = new FindTester(); |
| 255 | + t.testObject = bookDao; |
| 256 | + return t; |
| 257 | + } |
| 258 | + |
| 259 | + public Supplier<List<Book>> noArgs(){ |
| 260 | + return () -> testObject.findBooks(null,null,null,null); |
| 261 | + } |
| 262 | + |
| 263 | + public FindTester category(String ... category){ |
| 264 | + this.categories = Optional.ofNullable(this.categories).orElseGet(ArrayList::new); |
| 265 | + this.categories.addAll(Arrays.asList(category)); |
| 266 | + return this; |
| 267 | + } |
| 268 | + |
| 269 | + public FindTester author(String author){ |
| 270 | + this.author = author; |
| 271 | + return this; |
| 272 | + } |
| 273 | + |
| 274 | + public FindTester title(String title){ |
| 275 | + this.title = title; |
| 276 | + return this; |
| 277 | + } |
| 278 | + |
| 279 | + public FindTester publishedYear(String publishedYear){ |
| 280 | + this.publishedYear = publishedYear; |
| 281 | + return this; |
| 282 | + } |
| 283 | + |
| 284 | + public List<Book> get(){ |
| 285 | + return testObject.findBooks(categories, author, title, publishedYear); |
| 286 | + } |
| 287 | + |
| 288 | + } |
| 289 | + |
117 | 290 | }
|
0 commit comments