Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ZonedDateTime inside DBSetup
package lan.dk.podcastserver.repository;
import com.ninja_squad.dbsetup.DbSetup;
import com.ninja_squad.dbsetup.DbSetupTracker;
import com.ninja_squad.dbsetup.destination.DataSourceDestination;
import com.ninja_squad.dbsetup.destination.Destination;
import com.ninja_squad.dbsetup.operation.Operation;
import lan.dk.podcastserver.entity.Item;
import lan.dk.podcastserver.entity.Status;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.sql.DataSource;
import javax.transaction.Transactional;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import static com.ninja_squad.dbsetup.Operations.insertInto;
import static com.ninja_squad.dbsetup.operation.CompositeOperation.sequenceOf;
import static lan.dk.podcastserver.repository.dsl.ItemDSL.isDownloaded;
import static lan.dk.podcastserver.repository.dsl.ItemDSL.isNewerThan;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Created by kevin on 17/08/15 for Podcast Server
*/
@Transactional
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {DatabaseConfiguraitonTest.class, HibernateJpaAutoConfiguration.class})
public class ItemRepositoryTest {
@Autowired DataSource dataSource;
@Autowired ItemRepository itemRepository;
@Autowired Destination destination;
public static final DateTimeFormatter formatter =
new DateTimeFormatterBuilder().append(DateTimeFormatter.ISO_LOCAL_DATE).appendLiteral(" ").append(DateTimeFormatter.ISO_LOCAL_TIME).toFormatter();
private final static DbSetupTracker dbSetupTracker = new DbSetupTracker();
public static final Operation INSERT_REFERENCE_DATA = sequenceOf(
insertInto("PODCAST")
.columns("ID", "TITLE", "URL")
.values(1, "AppLoad", null)
.values(2, "Geek Inc HD", "http://fake.url.com/rss")
.build(),
insertInto("ITEM")
.columns("ID", "TITLE", "URL", "PODCAST_ID", "STATUS", "PUBDATE")
.values(1L, "Appload 1", "http://fakeurl.com/appload.1.mp3", 1, Status.FINISH.value(), ZonedDateTime.now().minusDays(15).format(formatter))
.values(2L, "Appload 2", "http://fakeurl.com/appload.2.mp3", 1, null, ZonedDateTime.now().minusDays(30).format(formatter))
.values(3L, "Appload 3", "http://fakeurl.com/appload.3.mp3", 1, Status.NOT_DOWNLOADED.value(), ZonedDateTime.now().format(formatter))
.build()
);
@Test
public void should_find_downloaded() {
/* When */
dbSetupTracker.skipNextLaunch();
Iterable<Item> one = itemRepository.findAll(isDownloaded(Boolean.TRUE));
/* Then */
assertThat(one)
.hasSize(1)
.extracting(Item::getTitle)
.contains("Appload 1");
}
@Test
public void should_find_not_downloaded() {
/* When */
dbSetupTracker.skipNextLaunch();
Iterable<Item> items = itemRepository.findAll(isDownloaded(Boolean.FALSE));
/* Then */
assertThat(items)
.hasSize(2)
.extracting(Item::getTitle)
.contains("Appload 2", "Appload 3");
}
@Test
public void should_find_newer_than_16_days() {
/* Given */
dbSetupTracker.skipNextLaunch();
/* When */
Iterable<Item> items = itemRepository.findAll(isNewerThan(ZonedDateTime.now().minusDays(16)));
/* Then */
assertThat(items)
.hasSize(2);
}
@Before
public void prepare() throws Exception {
Operation operation = sequenceOf(DatabaseConfiguraitonTest.DELETE_ALL, INSERT_REFERENCE_DATA);
DbSetup dbSetup = new DbSetup(new DataSourceDestination(dataSource), operation);
dbSetupTracker.launchIfNecessary(dbSetup);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.