package com.fosspowered.peist.respository; import static org.assertj.core.api.Assertions.assertThat; import com.fosspowered.peist.model.dao.Paste; import com.fosspowered.peist.repository.PasteRepository; import java.sql.Date; import java.util.Optional; import org.apache.commons.lang3.RandomStringUtils; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; import org.springframework.test.context.junit.jupiter.SpringExtension; @ExtendWith(SpringExtension.class) @DataJpaTest class PasteRepositoryTest { @Autowired private TestEntityManager entityManager; @Autowired private PasteRepository pasteRepository; @Test void whenFindByUrlId_ofValidId_thenReturnValidPaste() { // given String urlId = "id1"; Paste paste = new Paste( null, urlId, "title", "author", "util", null, true, new Date(2019, 1, 1), RandomStringUtils.randomAlphabetic(100)); entityManager.persist(paste); entityManager.flush(); // when Optional actualPaste = pasteRepository.findByUrlId(urlId); assertThat(actualPaste).isPresent(); assertThat(actualPaste).contains(paste); } @Test void whenFindByUrlId_ofInvalidId_thenReturnEmpty() { Optional invalidPaste = pasteRepository.findByUrlId("invalidId"); assertThat(invalidPaste).isNotPresent(); } }