Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api to execute custom query #156

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>

<dependency>
<groupId>com.ibm.db2</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,40 @@
package com.homihq.db2rest.exception;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import java.time.Instant;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex,
HttpHeaders headers,
HttpStatusCode status, WebRequest request) {
var body = new LinkedHashMap<>();
body.put("type", "https://github.com/kdhrubo/db2rest/invalid-arguments");
body.put("title", "Invalid arguments in the request");
body.put("status", status.value());
var errors = ex.getBindingResult()
.getFieldErrors()
.stream()
.map(error -> Map.of(error.getField(), Objects.requireNonNull(error.getDefaultMessage())))
.toList();
body.put("detail", errors);
body.put("instance", ((ServletWebRequest) request).getRequest().getRequestURI());
body.put("errorCategory", "Invalid-Arguments");
body.put("timestamp", Instant.now());
return new ResponseEntity<>(body, headers, status);
}

}
13 changes: 11 additions & 2 deletions src/main/java/com/homihq/db2rest/rest/read/ReadController.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.homihq.db2rest.rest.read;

import com.homihq.db2rest.rest.read.model.QueryRequest;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import static org.springframework.web.bind.ServletRequestUtils.*;

import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


Expand Down Expand Up @@ -39,7 +44,11 @@ public Object findByJoinTable(@PathVariable String tableName,

return readService.findAll(schemaName, tableName,select, filter, pageable, sort);
}


@PostMapping(value = "/query", consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> findByCustomQuery(@RequestBody @Valid QueryRequest queryRequest) {
log.debug("Execute SQL statement {} with params {}", queryRequest.getSql(), queryRequest.getParams());
return ResponseEntity.ok(readService.findByCustomQuery(queryRequest));
}

}
10 changes: 6 additions & 4 deletions src/main/java/com/homihq/db2rest/rest/read/ReadService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.homihq.db2rest.rest.read;

import com.homihq.db2rest.rest.read.helper.*;
import com.homihq.db2rest.rest.read.model.QueryRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -46,8 +47,9 @@ public Object findAll(String schemaName, String tableName, String select, String

}





Object findByCustomQuery(QueryRequest queryRequest) {
return queryRequest.isSingle() ?
namedParameterJdbcTemplate.queryForMap(queryRequest.getSql(), queryRequest.getParams()) :
namedParameterJdbcTemplate.queryForList(queryRequest.getSql(), queryRequest.getParams());
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/homihq/db2rest/rest/read/model/QueryRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.homihq.db2rest.rest.read.model;

import jakarta.validation.constraints.NotEmpty;
import lombok.Builder;
import lombok.Data;

import java.util.Map;

@Data
@Builder
public class QueryRequest {

@NotEmpty(message = "Sql statement cannot be empty")
private String sql;

private Map<String, Object> params;

private boolean isSingle;
}
84 changes: 74 additions & 10 deletions src/test/java/com/homihq/db2rest/rest/PgReadControllerTest.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package com.homihq.db2rest.rest;

import com.homihq.db2rest.PostgreSQLBaseIntegrationTest;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;

import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName;
import static org.springframework.restdocs.headers.HeaderDocumentation.responseHeaders;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.*;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.*;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.*;
import static org.springframework.restdocs.payload.PayloadDocumentation.*;
import static org.springframework.restdocs.request.RequestDocumentation.*;

import static org.hamcrest.Matchers.*;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

class PgReadControllerTest extends PostgreSQLBaseIntegrationTest {

@Test
Expand All @@ -27,4 +25,70 @@ void findAllFilms() throws Exception {
.andDo(print())
.andDo(document("pg-get-all-films"));
}

@Test
@DisplayName("Query returns single result")
void query_returns_single_result() throws Exception {
var json = """
{
"sql": "SELECT FIRST_NAME,LAST_NAME FROM ACTOR WHERE ACTOR_ID = :id",
"params" : {
"id" : 1
},
"single" : true
}
""";

mockMvc.perform(post("/query").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
.content(json).accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.first_name", equalTo("PENELOPE")))
.andDo(print())
.andDo(document("pg-create-a-film"));
}

@Test
@DisplayName("Query returns list of results")
void query_returns_list_of_results() throws Exception {
var json = """
{
"sql": "SELECT FIRST_NAME,LAST_NAME FROM ACTOR WHERE ACTOR_ID IN (:id1, :id2)",
"params" : {
"id1" : 1,
"id2" : 2
},
"single" : false
}
""";

mockMvc.perform(post("/query").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
.content(json).accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.*", hasSize(2)))
.andExpect(jsonPath("$[0].first_name", equalTo("PENELOPE")))
.andExpect(jsonPath("$[1].last_name", equalTo("WAHLBERG")))
.andDo(print())
.andDo(document("pg-create-a-film"));
}

@Test
@DisplayName("Query returns 400 bad request error")
void query_returns_400_bad_request() throws Exception {
var json = """
{
"sql": "",
"params" : {
"id1" : 1
},
"single" : false
}
""";

mockMvc.perform(post("/query").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
.content(json).accept(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.status", is(400)))
.andDo(print())
.andDo(document("pg-create-a-film"));
}
}