Skip to content

Commit

Permalink
adding integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
souravroy committed Jan 17, 2024
1 parent 7b7c457 commit 3407af5
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public Object findByJoinTable(@PathVariable String tableName,
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.query(queryRequest));
return ResponseEntity.ok(readService.findByCustomQuery(queryRequest));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public Object findAll(String schemaName, String tableName, String select, String

}

Object query(QueryRequest queryRequest) {
Object findByCustomQuery(QueryRequest queryRequest) {
return queryRequest.isSingle() ?
namedParameterJdbcTemplate.queryForMap(queryRequest.getSql(), queryRequest.getParams()) :
namedParameterJdbcTemplate.queryForList(queryRequest.getSql(), queryRequest.getParams());
Expand Down
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"));
}
}

0 comments on commit 3407af5

Please sign in to comment.