Created
October 28, 2023 15:29
-
-
Save giovannicandido/5e97308e3a6d41bcce9afe0a68119423 to your computer and use it in GitHub Desktop.
This enable rest requests to parse pageable objects in a more generic way for cucumber tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.kugelbit.mercado.estoque.infrastructure.rest; | |
import com.fasterxml.jackson.annotation.JsonCreator; | |
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | |
import com.fasterxml.jackson.annotation.JsonProperty; | |
import com.fasterxml.jackson.databind.JsonNode; | |
import org.springframework.data.domain.PageImpl; | |
import org.springframework.data.domain.PageRequest; | |
import org.springframework.data.domain.Pageable; | |
import java.util.ArrayList; | |
import java.util.List; | |
@JsonIgnoreProperties(ignoreUnknown = true) | |
public class CustomPageImpl<T> extends PageImpl<T> { | |
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES) | |
public CustomPageImpl(@JsonProperty("content") List<T> content, | |
@JsonProperty("number") int number, | |
@JsonProperty("size") int size, | |
@JsonProperty("totalElements") Long totalElements, | |
@JsonProperty("pageable") JsonNode pageable, | |
@JsonProperty("last") boolean last, | |
@JsonProperty("totalPages") int totalPages, | |
@JsonProperty("sort") JsonNode sort, | |
@JsonProperty("first") boolean first, | |
@JsonProperty("numberOfElements") int numberOfElements) { | |
super(content == null ? new ArrayList<>() : content, PageRequest.of(number, size >= 1 ? size : 1), | |
totalElements != null ? totalElements : 0L); | |
} | |
public CustomPageImpl(List<T> content, Pageable pageable, long total) { | |
super(content == null ? new ArrayList<>() : content, pageable, total); | |
} | |
public CustomPageImpl(List<T> content) { | |
super(content == null ? new ArrayList<>() : content); | |
} | |
public CustomPageImpl() { | |
super(new ArrayList<>()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.springframework.core.ParameterizedTypeReference | |
object ParameterizedTypeReferenceFactory { | |
private val parameterizedTypes = mapOf( | |
JsBrand::class.java to | |
object : ParameterizedTypeReference<CustomPageImpl<JsBrand?>?>() {}, | |
JsMarket::class.java to | |
object : ParameterizedTypeReference<CustomPageImpl<JsMarket?>?>() {}, | |
JsMainStock::class.java to | |
object : ParameterizedTypeReference<CustomPageImpl<JsMainStock?>?>() {}, | |
JsProduct::class.java to | |
object : ParameterizedTypeReference<CustomPageImpl<JsProduct?>?>() {}, | |
JsProductCategory::class.java to | |
object : ParameterizedTypeReference<CustomPageImpl<JsProductCategory?>?>() {} | |
) | |
private val parameterizedTypeReferences = mapOf( | |
JsBrand::class.java to | |
object : TypeReference<CustomPageImpl<JsBrand?>?>() {}, | |
JsProductCategory::class.java to | |
object : TypeReference<CustomPageImpl<JsProductCategory?>?>() {}, | |
JsProduct::class.java to | |
object : TypeReference<CustomPageImpl<JsProduct?>?>() {}, | |
JsMarket::class.java to | |
object : TypeReference<CustomPageImpl<JsMarket?>?>() {}, | |
JsMainStock::class.java to | |
object : TypeReference<CustomPageImpl<JsMainStock?>?>() {}, | |
JsProduct::class.java to | |
object : TypeReference<CustomPageImpl<JsProduct?>?>() {} | |
) | |
fun <T> buildCustomPageReference(aClass: Class<T>): ParameterizedTypeReference<CustomPageImpl<T?>?> { | |
return if (parameterizedTypes.containsKey(aClass)) { | |
parameterizedTypes[aClass] as ParameterizedTypeReference<CustomPageImpl<T?>?> | |
} else { | |
throw IllegalArgumentException( | |
"Cannot find parametrized type for class %s".format(aClass) | |
) | |
} | |
} | |
@JvmStatic | |
fun <T> buildCustomPageTypeReference(aClass: Class<T>): TypeReference<out CustomPageImpl<T?>?> { | |
return if (parameterizedTypes.containsKey(aClass)) { | |
parameterizedTypeReferences[aClass] as TypeReference<CustomPageImpl<T?>?> | |
} else { | |
throw IllegalArgumentException( | |
"Cannot find type reference for class %s".format(aClass) | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment