|
| 1 | +package org.gluecoders.library.dao; |
| 2 | + |
| 3 | +import com.lordofthejars.nosqlunit.annotation.ShouldMatchDataSet; |
| 4 | +import com.lordofthejars.nosqlunit.annotation.UsingDataSet; |
| 5 | +import com.lordofthejars.nosqlunit.core.LoadStrategyEnum; |
| 6 | +import com.lordofthejars.nosqlunit.mongodb.MongoDbRule; |
| 7 | +import org.gluecoders.library.config.Database; |
| 8 | +import org.gluecoders.library.dao.config.FakeMongo; |
| 9 | +import org.gluecoders.library.models.Book; |
| 10 | +import org.junit.Rule; |
| 11 | +import org.junit.Test; |
| 12 | +import org.junit.runner.RunWith; |
| 13 | +import org.springframework.beans.factory.annotation.Autowired; |
| 14 | +import org.springframework.context.ApplicationContext; |
| 15 | +import org.springframework.context.annotation.Import; |
| 16 | +import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; |
| 17 | +import org.springframework.test.context.junit4.SpringRunner; |
| 18 | + |
| 19 | +import java.time.Month; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import static com.lordofthejars.nosqlunit.mongodb.MongoDbRule.MongoDbRuleBuilder.newMongoDbRule; |
| 23 | +import static org.junit.Assert.*; |
| 24 | + |
| 25 | +/** |
| 26 | + * Created by Anand_Rajneesh on 6/14/2017. |
| 27 | + */ |
| 28 | +@RunWith(SpringRunner.class) |
| 29 | +@Import(value = {FakeMongo.class, Database.class}) |
| 30 | +@EnableMongoRepositories(basePackageClasses = {BookDao.class}) |
| 31 | +public class BookDaoTest { |
| 32 | + |
| 33 | + @Autowired |
| 34 | + private ApplicationContext applicationContext; |
| 35 | + |
| 36 | + @Autowired |
| 37 | + private BookDao bookDao; |
| 38 | + |
| 39 | + @Rule |
| 40 | + public MongoDbRule embeddedMongoDbRule = newMongoDbRule().defaultSpringMongoDb("mockDB"); |
| 41 | + |
| 42 | + @Test |
| 43 | + @UsingDataSet(loadStrategy = LoadStrategyEnum.DELETE_ALL) |
| 44 | + public void getAllBooks_NoBooks() { |
| 45 | + List<Book> books = bookDao.findAll(); |
| 46 | + assertTrue("Returned book list should be empty", books.isEmpty()); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + @UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT, locations = "/books/books.json") |
| 51 | + public void getAllBooks_WithBooks() { |
| 52 | + List<Book> books = bookDao.findAll(); |
| 53 | + assertFalse("Returned book list should not be empty", books.isEmpty()); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + @UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT, locations = "/books/books.json") |
| 58 | + public void findDistinctByIsbnCode_IsbnAvailable(){ |
| 59 | + Book book = bookDao.findDistinctByIsbnCode(1234567890L); |
| 60 | + assertNotNull("Book should be returned for 1234567890isbn ", book); |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + @Test |
| 65 | + @UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT, locations = "/books/books.json") |
| 66 | + public void findDistinctByIsbnCode_IsbnNotAvailable(){ |
| 67 | + Book book = bookDao.findDistinctByIsbnCode(1234567892L); |
| 68 | + assertNull("No book should be returned for 1234567892 isbn ", book); |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + @Test |
| 73 | + @UsingDataSet(loadStrategy = LoadStrategyEnum.DELETE_ALL) |
| 74 | + public void findDistinctByIsbnCode_NoDataAvailable(){ |
| 75 | + Book book = bookDao.findDistinctByIsbnCode(1234567890L); |
| 76 | + assertNull("No Book should be returned since there is no data present ", book); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + @UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT, locations = "/books/empty.json") |
| 81 | + @ShouldMatchDataSet(location = "/books/book1.json") |
| 82 | + public void addBook(){ |
| 83 | + Book book = Book.builder() |
| 84 | + .author("Joshua Bloch") |
| 85 | + .categories("Programming", "Java") |
| 86 | + .isbn(1234567890L) |
| 87 | + .publisher("noidea") |
| 88 | + .yearOfPublishing(2011, Month.AUGUST) |
| 89 | + .title("Effective Java") |
| 90 | + .build(); |
| 91 | + bookDao.save(book); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + @UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT, locations = "/books/books.json") |
| 96 | + public void deleteBook_WithId(){ |
| 97 | + Book book = bookDao.findDistinctByIsbnCode(1234567890L); |
| 98 | + bookDao.delete(book); |
| 99 | + } |
| 100 | + |
| 101 | + @Test(expected = IllegalArgumentException.class) |
| 102 | + @UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT, locations = "/books/books.json") |
| 103 | + public void deleteBook_WithoutId(){ |
| 104 | + Book book = bookDao.findDistinctByIsbnCode(1234567890L); |
| 105 | + book.setId(null); |
| 106 | + bookDao.delete(book); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + @UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT, locations = "/books/books.json") |
| 111 | + public void deleteBook_IdNotPresent(){ |
| 112 | + Book book = bookDao.findDistinctByIsbnCode(1234567890L); |
| 113 | + book.setId("12323543453412"); |
| 114 | + bookDao.delete(book); |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments