generator.server.pagination.jpa.test.AppPagesTest.mustache Maven / Gradle / Ivy
package {{packageName}}.shared.pagination.infrastructure.secondary;
import static org.assertj.core.api.Assertions.*;
import java.util.List;
import java.util.function.Function;
import org.junit.jupiter.api.Test;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import {{packageName}}.UnitTest;
import {{packageName}}.shared.error.domain.MissingMandatoryValueException;
import {{packageName}}.shared.pagination.domain.{{baseName}}Page;
import {{packageName}}.shared.pagination.domain.{{baseName}}Pageable;
@UnitTest
class {{baseName}}PagesTest {
@Test
void shouldNotBuildPageableFromNull{{baseName}}Pageable() {
assertThatThrownBy(() -> {{baseName}}Pages.from(null))
.isExactlyInstanceOf(MissingMandatoryValueException.class)
.hasMessageContaining("pagination");
}
@Test
void shouldBuildPageableFrom{{baseName}}Pageable() {
Pageable pagination = {{baseName}}Pages.from(pagination());
assertThat(pagination.getPageNumber()).isEqualTo(2);
assertThat(pagination.getPageSize()).isEqualTo(15);
assertThat(pagination.getSort()).isEqualTo(Sort.unsorted());
}
@Test
void shouldNotBuildWithoutSort() {
assertThatThrownBy(() -> {{baseName}}Pages.from(pagination(), null))
.isExactlyInstanceOf(MissingMandatoryValueException.class)
.hasMessageContaining("sort");
}
@Test
void shouldBuildPageableFrom{{baseName}}PageableAndSort() {
Pageable pagination = {{baseName}}Pages.from(pagination(), Sort.by("dummy"));
assertThat(pagination.getPageNumber()).isEqualTo(2);
assertThat(pagination.getPageSize()).isEqualTo(15);
assertThat(pagination.getSort()).isEqualTo(Sort.by("dummy"));
}
private {{baseName}}Pageable pagination() {
return new {{baseName}}Pageable(2, 15);
}
@Test
void shouldNotConvertFromSpringPageWithoutSpringPage() {
assertThatThrownBy(() -> {{baseName}}Pages.from(null, source -> source))
.isExactlyInstanceOf(MissingMandatoryValueException.class)
.hasMessageContaining("springPage");
}
@Test
void shouldNotConvertFromSpringPageWithoutMapper() {
assertThatThrownBy(() -> {{baseName}}Pages.from(springPage(), null))
.isExactlyInstanceOf(MissingMandatoryValueException.class)
.hasMessageContaining("mapper");
}
@Test
void shouldConvertFromSpringPage() {
{{baseName}}Page page = {{baseName}}Pages.from(springPage(), Function.identity());
assertThat(page.content()).containsExactly("test");
assertThat(page.currentPage()).isEqualTo(2);
assertThat(page.pageSize()).isEqualTo(10);
assertThat(page.totalElementsCount()).isEqualTo(30);
}
private PageImpl springPage() {
return new PageImpl<>(List.of("test"), PageRequest.of(2, 10), 30);
}
}