All Downloads are FREE. Search and download functionalities are using the official Maven repository.

generator.server.pagination.domain.test.AppPageTest.mustache Maven / Gradle / Ivy

There is a newer version: 1.22.0
Show newest version
package {{packageName}}.shared.pagination.domain;

import static org.assertj.core.api.Assertions.*;
import static {{packageName}}.shared.pagination.domain.{{baseName}}PagesFixture.*;

import java.util.List;
import org.junit.jupiter.api.Test;
import {{packageName}}.UnitTest;
import {{packageName}}.shared.error.domain.MissingMandatoryValueException;

@UnitTest
class {{baseName}}PageTest {

  @Test
  void shouldGetEmptySinglePageWithoutContent() {
    {{baseName}}Page page = {{baseName}}Page.singlePage(null);

    assertEmptyPage(page);
  }

  @Test
  void shouldGetEmptySinglePageFromBuilderWithoutContent() {
    {{baseName}}Page page = {{baseName}}Page.builder(null).build();

    assertEmptyPage(page);
  }

  private void assertEmptyPage({{baseName}}Page page) {
    assertThat(page.content()).isEmpty();
    assertThat(page.currentPage()).isZero();
    assertThat(page.pageSize()).isZero();
    assertThat(page.totalElementsCount()).isZero();
  }

  @Test
  void shouldGetSinglePage() {
    {{baseName}}Page page = {{baseName}}Page.singlePage(List.of("test", "dummy"));

    assertSinglePage(page);
  }

  @Test
  void shouldGetSinglePageFromBuilderWithContentOnly() {
    {{baseName}}Page page = {{baseName}}Page.builder(List.of("test", "dummy")).build();

    assertSinglePage(page);
  }

  private void assertSinglePage({{baseName}}Page page) {
    assertThat(page.content()).containsExactly("test", "dummy");
    assertThat(page.currentPage()).isZero();
    assertThat(page.pageSize()).isEqualTo(2);
    assertThat(page.totalElementsCount()).isEqualTo(2);
    assertThat(page.pageCount()).isEqualTo(1);
  }

  @Test
  void shouldGetFullPage() {
    {{baseName}}Page page = pageBuilder().build();

    assertThat(page.content()).containsExactly("test");
    assertThat(page.currentPage()).isEqualTo(2);
    assertThat(page.pageSize()).isEqualTo(10);
    assertThat(page.totalElementsCount()).isEqualTo(21);
    assertThat(page.pageCount()).isEqualTo(3);
  }

  @Test
  void shouldNotMapWithoutMapper() {
    assertThatThrownBy(() -> pageBuilder().build().map(null))
      .isExactlyInstanceOf(MissingMandatoryValueException.class)
      .hasMessageContaining("mapper");
  }

  @Test
  void shouldMapPage() {
    {{baseName}}Page page = pageBuilder().build().map(entry -> "hey");

    assertThat(page.content()).containsExactly("hey");
    assertThat(page.currentPage()).isEqualTo(2);
    assertThat(page.pageSize()).isEqualTo(10);
    assertThat(page.totalElementsCount()).isEqualTo(21);
    assertThat(page.pageCount()).isEqualTo(3);
  }

  @Test
  void shouldNotBeLastForFirstPage() {
    assertThat(pageBuilder().currentPage(0).build().isNotLast()).isTrue();
  }

  @Test
  void shouldBeLastWithOnePage() {
    assertThat({{baseName}}Page.singlePage(List.of("d")).isNotLast()).isFalse();
  }

  @Test
  void shouldBeLastPageWithoutContent() {
    {{baseName}}Page page = {{baseName}}Page.builder(List.of())
      .currentPage(0)
      .pageSize(1)
      .totalElementsCount(0)
      .build();
    assertThat(page.isNotLast()).isFalse();
  }

  @Test
  void shouldBeLastForLastPage() {
    assertThat(pageBuilder().currentPage(2).build().isNotLast()).isFalse();
  }

  @Test
  void shouldGetPageFromElements() {
    {{baseName}}Page page = {{baseName}}Page.of(
      List.of("hello", "java", "world"),
      new {{baseName}}Pageable(1, 1)
    );

    assertThat(page.currentPage()).isEqualTo(1);
    assertThat(page.hasNext()).isTrue();
    assertThat(page.hasPrevious()).isTrue();
    assertThat(page.pageCount()).isEqualTo(3);
    assertThat(page.pageSize()).isEqualTo(1);
    assertThat(page.content()).containsExactly("java");
  }

  @Test
  void shouldGetEmptyPageFromOutOfBoundElements() {
    {{baseName}}Page page = {{baseName}}Page.of(
      List.of("hello", "java", "world"),
      new {{baseName}}Pageable(4, 1)
    );

    assertThat(page.currentPage()).isEqualTo(4);
    assertThat(page.hasNext()).isFalse();
    assertThat(page.hasPrevious()).isTrue();
    assertThat(page.pageCount()).isEqualTo(3);
    assertThat(page.pageSize()).isEqualTo(1);
    assertThat(page.content()).isEmpty();
  }

  @Test
  void shouldGetPageWithLessThanExpectedElements() {
    {{baseName}}Page page = {{baseName}}Page.of(
      List.of("hello", "java", "world"),
      new {{baseName}}Pageable(0, 4)
    );

    assertThat(page.currentPage()).isZero();
    assertThat(page.hasNext()).isFalse();
    assertThat(page.hasPrevious()).isFalse();
    assertThat(page.pageCount()).isEqualTo(1);
    assertThat(page.pageSize()).isEqualTo(4);
    assertThat(page.content()).hasSize(3);
  }
}