Skip to content

Commit

Permalink
feat(3878): [2.129] rpc retourne un Optional du type de clazz demandé…
Browse files Browse the repository at this point in the history
… au lieu du type demandé par Type (#45)

Co-authored-by: Gregory GERMAIN <[email protected]>
  • Loading branch information
arnaud-thorel-of and gregorygermain authored Oct 9, 2024
1 parent 7938587 commit 6812210
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -210,10 +208,9 @@ void shouldCallRpcResultIsList(MockServerClient client) {
[{"id": 1, "title": "test"}]
"""));

List<Post> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

Expand All @@ -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
Expand All @@ -70,9 +67,8 @@ void shouldCallRpcWithCriteriaResultIsList(MockServerClient client) {

PostRequestWithSelect criteria = new PostRequestWithSelect();
criteria.setUserId(1);
List<Post> 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);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@
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;
import org.mockserver.junit.jupiter.MockServerSettings;
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;
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -68,7 +65,7 @@ void shouldCallRpcWithCriteriaResultIsList(MockServerClient client) {

PostRequestWithSelect criteria = new PostRequestWithSelect();
criteria.setUserId(1);
List<Post> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -23,47 +21,44 @@ public class PostgrestRpcClient {
* Execute a rpc call without body
*
* @param rpcName rpc name
* @param type class of return
* @param <V> type of the return object
* @param clazz class of return
* @param <DTO> type of the return object
* @return response
*/
public <V> V executeRpc(String rpcName, Type type) {
return executeRpc(rpcName, null, type);
public <DTO> Optional<DTO> executeRpc(String rpcName, Class<DTO> clazz) {
return executeRpc(rpcName, null, clazz);
}

/**
* Execute a rpc call
*
* @param rpcName rpc name
* @param body body request to send
* @param type class of return
* @param <V> type of return object
* @param clazz class of return
* @param <DTO> type of return object
* @return response
*/
public <V> V executeRpc(String rpcName, Object body, Type type) {
return executeRpc(rpcName, null, body, type);
public <DTO> Optional<DTO> executeRpc(String rpcName, Object body, Class<DTO> 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 <V> type of return object
* @param clazz class of return
* @param <DTO> type of return object
* @return response
*/
public <V> V executeRpc(String rpcName, Object criteria, Object body, Type type) {
public <DTO> Optional<DTO> executeRpc(String rpcName, Object criteria, Object body, Class<DTO> clazz) {
// List filters
List<Filter> 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
*
Expand All @@ -75,5 +70,4 @@ private Optional<Filter> getSelects(Object criteria) {
.filter(x -> !x.isEmpty())
.map(SelectFilter::only);
}

}

0 comments on commit 6812210

Please sign in to comment.