|
| 1 | +package com.baeldung.repository; |
| 2 | + |
| 3 | +import com.baeldung.domain.Article; |
| 4 | +import org.junit.Test; |
| 5 | +import org.junit.runner.RunWith; |
| 6 | +import org.springframework.beans.factory.annotation.Autowired; |
| 7 | +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; |
| 8 | +import org.springframework.test.context.junit4.SpringRunner; |
| 9 | + |
| 10 | +import java.text.SimpleDateFormat; |
| 11 | +import java.util.Arrays; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +import static org.junit.Assert.assertEquals; |
| 15 | +import static org.junit.Assert.assertTrue; |
| 16 | + |
| 17 | +@RunWith(SpringRunner.class) |
| 18 | +@DataJpaTest |
| 19 | +public class ArticleRepositoryIntegrationTest { |
| 20 | + |
| 21 | + @Autowired |
| 22 | + private ArticleRepository repository; |
| 23 | + |
| 24 | + @Test |
| 25 | + public void givenImportedArticlesWhenFindAllByPublicationDateThenArticles1And2Returned() |
| 26 | + throws Exception { |
| 27 | + List<Article> result = repository.findAllByPublicationDate( |
| 28 | + new SimpleDateFormat("yyyy-MM-dd").parse("2018-01-01") |
| 29 | + ); |
| 30 | + |
| 31 | + assertEquals(2, result.size()); |
| 32 | + assertTrue(result.stream() |
| 33 | + .map(Article::getId) |
| 34 | + .allMatch(id -> Arrays.asList(1, 2).contains(id)) |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void givenImportedArticlesWhenFindAllByPublicationTimeBetweenThenArticles2And3Returned() |
| 40 | + throws Exception { |
| 41 | + List<Article> result = repository.findAllByPublicationTimeBetween( |
| 42 | + new SimpleDateFormat("HH:mm").parse("15:15"), |
| 43 | + new SimpleDateFormat("HH:mm").parse("16:30") |
| 44 | + ); |
| 45 | + |
| 46 | + assertEquals(2, result.size()); |
| 47 | + assertTrue(result.stream() |
| 48 | + .map(Article::getId) |
| 49 | + .allMatch(id -> Arrays.asList(2, 3).contains(id)) |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void givenImportedArticlesWhenFindAllWithCreationDateTimeBeforeThenArticles2And3Returned() throws Exception { |
| 55 | + List<Article> result = repository.findAllWithCreationDateTimeBefore( |
| 56 | + new SimpleDateFormat("yyyy-MM-dd HH:mm").parse("2017-12-15 10:00") |
| 57 | + ); |
| 58 | + |
| 59 | + assertEquals(2, result.size()); |
| 60 | + assertTrue(result.stream() |
| 61 | + .map(Article::getId) |
| 62 | + .allMatch(id -> Arrays.asList(2, 3).contains(id)) |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | +} |
0 commit comments