diff --git a/querydsl-postgrest-resttemplate-adapter/src/test/java/fr/ouestfrance/querydsl/postgrest/PostgrestRestTemplateRepositoryTest.java b/querydsl-postgrest-resttemplate-adapter/src/test/java/fr/ouestfrance/querydsl/postgrest/PostgrestRestTemplateRepositoryTest.java index 4007b8d..4b68a5d 100644 --- a/querydsl-postgrest-resttemplate-adapter/src/test/java/fr/ouestfrance/querydsl/postgrest/PostgrestRestTemplateRepositoryTest.java +++ b/querydsl-postgrest-resttemplate-adapter/src/test/java/fr/ouestfrance/querydsl/postgrest/PostgrestRestTemplateRepositoryTest.java @@ -8,7 +8,6 @@ import fr.ouestfrance.querydsl.postgrest.model.Page; import fr.ouestfrance.querydsl.postgrest.model.Pageable; import lombok.SneakyThrows; -import org.apache.commons.lang3.reflect.TypeUtils; import org.apache.hc.client5.http.impl.classic.HttpClients; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -197,10 +196,9 @@ void shouldCallRpc(MockServerClient client) { {"id": 1, "title": "test"} """)); - Post result = rpcClient.executeRpc("testV1", null, Post.class); + Post result = rpcClient.executeRpc("testV1", null, Post.class).orElse(null); assertNotNull(result); - System.out.println(result); - } + } @Test @@ -210,10 +208,9 @@ void shouldCallRpcResultIsList(MockServerClient client) { [{"id": 1, "title": "test"}] """)); - List result = rpcClient.executeRpc("testV1", null, TypeUtils.parameterize(List.class, Post.class)); + Post[] result = rpcClient.executeRpc("testV1", null, Post[].class).orElse(null); assertNotNull(result); - System.out.println(result); - } + } private HttpResponse jsonResponse(String content) { return HttpResponse.response().withContentType(MediaType.APPLICATION_JSON) diff --git a/querydsl-postgrest-resttemplate-adapter/src/test/java/fr/ouestfrance/querydsl/postgrest/PostgrestRpcTest.java b/querydsl-postgrest-resttemplate-adapter/src/test/java/fr/ouestfrance/querydsl/postgrest/PostgrestRpcTest.java index 418552f..26a8b4c 100644 --- a/querydsl-postgrest-resttemplate-adapter/src/test/java/fr/ouestfrance/querydsl/postgrest/PostgrestRpcTest.java +++ b/querydsl-postgrest-resttemplate-adapter/src/test/java/fr/ouestfrance/querydsl/postgrest/PostgrestRpcTest.java @@ -13,8 +13,6 @@ import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; -import java.util.List; - import static fr.ouestfrance.querydsl.postgrest.TestUtils.jsonResponse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -39,7 +37,7 @@ void shouldCallRpc(MockServerClient client) { .respond(jsonResponse(""" {"id": 1, "title": "test"} """)); - Post result = rpcClient.executeRpc("testV1", Post.class); + Post result = rpcClient.executeRpc("testV1", Post.class).orElse(null); assertNotNull(result); } @@ -54,9 +52,8 @@ void shouldCallRpcWithCriteria(MockServerClient client) { PostRequestWithSelect criteria = new PostRequestWithSelect(); criteria.setUserId(1); - Post result = rpcClient.executeRpc("testV1", criteria,null, Post.class); + Post result = rpcClient.executeRpc("testV1", criteria, null, Post.class).orElse(null); assertNotNull(result); - System.out.println(result); } @Test @@ -70,9 +67,8 @@ void shouldCallRpcWithCriteriaResultIsList(MockServerClient client) { PostRequestWithSelect criteria = new PostRequestWithSelect(); criteria.setUserId(1); - List result = rpcClient.executeRpc("testV1", criteria, null, TypeUtils.parameterize(List.class, Post.class)); + Post[] result = rpcClient.executeRpc("testV1", criteria, null, Post[].class).orElse(null); assertNotNull(result); - System.out.println(result); } diff --git a/querydsl-postgrest-webclient-adapter/src/test/java/fr/ouestfrance/querydsl/postgrest/PostgrestWebClientRpcTest.java b/querydsl-postgrest-webclient-adapter/src/test/java/fr/ouestfrance/querydsl/postgrest/PostgrestWebClientRpcTest.java index f695838..8ec9388 100644 --- a/querydsl-postgrest-webclient-adapter/src/test/java/fr/ouestfrance/querydsl/postgrest/PostgrestWebClientRpcTest.java +++ b/querydsl-postgrest-webclient-adapter/src/test/java/fr/ouestfrance/querydsl/postgrest/PostgrestWebClientRpcTest.java @@ -4,7 +4,6 @@ import fr.ouestfrance.querydsl.postgrest.app.PostRequestWithSelect; import fr.ouestfrance.querydsl.postgrest.model.exceptions.PostgrestRequestException; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.reflect.TypeUtils; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockserver.client.MockServerClient; @@ -12,8 +11,6 @@ import org.mockserver.model.HttpRequest; import org.springframework.web.reactive.function.client.WebClient; -import java.util.List; - import static fr.ouestfrance.querydsl.postgrest.TestUtils.jsonResponse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -38,7 +35,7 @@ void shouldCallRpc(MockServerClient client) { .respond(jsonResponse(""" {"id": 1, "title": "test"} """)); - Post result = rpcClient.executeRpc("testV1", Post.class); + Post result = rpcClient.executeRpc("testV1", Post.class).orElse(null); assertNotNull(result); } @@ -53,7 +50,7 @@ void shouldCallRpcWithCriteria(MockServerClient client) { PostRequestWithSelect criteria = new PostRequestWithSelect(); criteria.setUserId(1); - Post result = rpcClient.executeRpc("testV1", criteria, null, Post.class); + Post result = rpcClient.executeRpc("testV1", criteria, null, Post.class).orElse(null); assertNotNull(result); } @@ -68,7 +65,7 @@ void shouldCallRpcWithCriteriaResultIsList(MockServerClient client) { PostRequestWithSelect criteria = new PostRequestWithSelect(); criteria.setUserId(1); - List result = rpcClient.executeRpc("testV1", criteria, null, TypeUtils.parameterize(List.class, Post.class)); + Post[] result = rpcClient.executeRpc("testV1", criteria, null, Post[].class).orElse(null); assertNotNull(result); } diff --git a/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/PostgrestRpcClient.java b/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/PostgrestRpcClient.java index 994a410..1507e61 100644 --- a/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/PostgrestRpcClient.java +++ b/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/PostgrestRpcClient.java @@ -7,8 +7,6 @@ import fr.ouestfrance.querydsl.service.ext.QueryDslProcessorService; import lombok.RequiredArgsConstructor; -import java.lang.reflect.Type; -import java.util.HashMap; import java.util.List; import java.util.Optional; @@ -23,12 +21,12 @@ public class PostgrestRpcClient { * Execute a rpc call without body * * @param rpcName rpc name - * @param type class of return - * @param type of the return object + * @param clazz class of return + * @param type of the return object * @return response */ - public V executeRpc(String rpcName, Type type) { - return executeRpc(rpcName, null, type); + public Optional executeRpc(String rpcName, Class clazz) { + return executeRpc(rpcName, null, clazz); } /** @@ -36,34 +34,31 @@ public V executeRpc(String rpcName, Type type) { * * @param rpcName rpc name * @param body body request to send - * @param type class of return - * @param type of return object + * @param clazz class of return + * @param type of return object * @return response */ - public V executeRpc(String rpcName, Object body, Type type) { - return executeRpc(rpcName, null, body, type); + public Optional executeRpc(String rpcName, Object body, Class clazz) { + return executeRpc(rpcName, null, body, clazz); } - /** * Execute a rpc call * * @param rpcName rpc name * @param body body request to send - * @param type class of return - * @param type of return object + * @param clazz class of return + * @param type of return object * @return response */ - public V executeRpc(String rpcName, Object criteria, Object body, Type type) { + public Optional executeRpc(String rpcName, Object criteria, Object body, Class clazz) { // List filters List queryParams = processorService.process(criteria); // Extract selection getSelects(criteria).ifPresent(queryParams::add); - - return client.rpc(RPC + rpcName, FilterUtils.toMap(queryParams), body, type); + return Optional.ofNullable(client.rpc(RPC + rpcName, FilterUtils.toMap(queryParams), body, clazz)); } - /** * Extract selection on criteria and class * @@ -75,5 +70,4 @@ private Optional getSelects(Object criteria) { .filter(x -> !x.isEmpty()) .map(SelectFilter::only); } - }