|
| 1 | +package tech.mappie.testing.scenarios |
| 2 | + |
| 3 | +import org.assertj.core.api.Assertions.assertThat |
| 4 | +import org.junit.jupiter.api.Test |
| 5 | +import tech.mappie.testing.MappieTestCase |
| 6 | +import java.time.Instant |
| 7 | + |
| 8 | +class PaginationTest : MappieTestCase() { |
| 9 | + |
| 10 | + data class GetProjectListResult ( |
| 11 | + val items: List<GetProjectListResultItem>, |
| 12 | + val totalCount: Int, |
| 13 | + val page: Int, |
| 14 | + val pageSize: Int, |
| 15 | + val totalPages: Int, |
| 16 | + val hasNextPage: Boolean, |
| 17 | + val hasPreviousPage: Boolean |
| 18 | + ) |
| 19 | + |
| 20 | + data class GetProjectListResultItem ( |
| 21 | + val createdAt: Instant, |
| 22 | + val lastModifiedAt: Instant? = null, |
| 23 | + val id: String, |
| 24 | + val number: String |
| 25 | + ) |
| 26 | + |
| 27 | + data class PaginationInfo<T>( |
| 28 | + val pageKey: Int, |
| 29 | + val totalPages: Int, |
| 30 | + val isLastPage: Boolean, |
| 31 | + val items: List<T>, |
| 32 | + val totalItems: Int, |
| 33 | + ) { |
| 34 | + val nextPageKey: Int |
| 35 | + get() = pageKey + 1 |
| 36 | + } |
| 37 | + |
| 38 | + data class ProjectListItem( |
| 39 | + val id: String, |
| 40 | + val number: String, |
| 41 | + val date: Instant |
| 42 | + ) |
| 43 | + |
| 44 | + @Test |
| 45 | + fun `mapper without pagination`() { |
| 46 | + compile { |
| 47 | + file("Test.kt", |
| 48 | + """ |
| 49 | + import tech.mappie.api.ObjectMappie |
| 50 | + import tech.mappie.testing.scenarios.PaginationTest.* |
| 51 | +
|
| 52 | + class ProjectListItemMapper: ObjectMappie<GetProjectListResultItem, ProjectListItem>() { |
| 53 | + override fun map(from: GetProjectListResultItem): ProjectListItem = mapping { |
| 54 | + to::date fromProperty from::createdAt |
| 55 | + } |
| 56 | + } |
| 57 | +
|
| 58 | + """) |
| 59 | + } satisfies { |
| 60 | + isOk() |
| 61 | + hasNoWarningsOrErrors() |
| 62 | + |
| 63 | + val mapper = objectMappie<GetProjectListResultItem, ProjectListItem>("ProjectListItemMapper") |
| 64 | + |
| 65 | + assertThat(mapper.map(GetProjectListResultItem(Instant.MIN, null, "id", "number"))) |
| 66 | + .isEqualTo(ProjectListItem("id", "number", Instant.MIN)) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + fun `mapper with pagination`() { |
| 72 | + compile { |
| 73 | + file("Test.kt", |
| 74 | + """ |
| 75 | + import tech.mappie.api.ObjectMappie |
| 76 | + import tech.mappie.testing.scenarios.PaginationTest.* |
| 77 | +
|
| 78 | + class GetProjectListResultMapper: ObjectMappie<GetProjectListResult, PaginationInfo<ProjectListItem>>() { |
| 79 | + override fun map(from: GetProjectListResult) = mapping { |
| 80 | + to::pageKey fromProperty from::page |
| 81 | + to::isLastPage fromValue !from.hasNextPage |
| 82 | + to::totalItems fromProperty from::totalPages |
| 83 | + } |
| 84 | + } |
| 85 | +
|
| 86 | + object ProjectListItemMapper: ObjectMappie<GetProjectListResultItem, ProjectListItem>() { |
| 87 | + override fun map(from: GetProjectListResultItem): ProjectListItem = mapping { |
| 88 | + to::date fromProperty from::createdAt |
| 89 | + } |
| 90 | + } |
| 91 | +
|
| 92 | + """) |
| 93 | + } satisfies { |
| 94 | + isOk() |
| 95 | + hasNoWarningsOrErrors() |
| 96 | + |
| 97 | + val mapper = objectMappie<GetProjectListResult, PaginationInfo<ProjectListItem>>("GetProjectListResultMapper") |
| 98 | + |
| 99 | + val input = GetProjectListResult( |
| 100 | + listOf(GetProjectListResultItem(Instant.MIN, null, "id", "number")), |
| 101 | + 1, |
| 102 | + 2, |
| 103 | + 3, |
| 104 | + 4, |
| 105 | + false, |
| 106 | + false |
| 107 | + ) |
| 108 | + |
| 109 | + assertThat(mapper.map(input)) |
| 110 | + .isEqualTo(PaginationInfo(2, 4, true, listOf(ProjectListItem("id", "number", Instant.MIN)), 4)) |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments