-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
201 additions
and
2 deletions.
There are no files selected for viewing
159 changes: 159 additions & 0 deletions
159
api-rest/src/test/java/com/homihq/db2rest/rest/mariadb/MariaDBTemplateControllerTest.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
package com.homihq.db2rest.rest.mariadb; | ||
|
||
import com.homihq.db2rest.MariaDBBaseIntegrationTest; | ||
import org.junit.jupiter.api.*; | ||
|
||
import static com.homihq.db2rest.jdbc.rest.RdbmsRestApi.VERSION; | ||
import static org.hamcrest.Matchers.hasSize; | ||
import static org.hamcrest.core.AnyOf.anyOf; | ||
import static org.springframework.http.MediaType.APPLICATION_JSON; | ||
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; | ||
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get; | ||
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; | ||
|
||
@TestClassOrder(ClassOrderer.OrderAnnotation.class) | ||
@Order(309) | ||
@DisplayName("Parameterized SQL template") | ||
class MariaDBTemplateControllerTest extends MariaDBBaseIntegrationTest { | ||
|
||
public final static int ID = 1; | ||
|
||
@Test | ||
@DisplayName("Test find all films with sql template") | ||
void findAllFilms() throws Exception { | ||
var tableName = "film"; | ||
|
||
mockMvc.perform(get(VERSION + "/mariadb/sql/select_all") | ||
.contentType(APPLICATION_JSON).accept(APPLICATION_JSON)) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.*").isArray()) | ||
.andExpect(jsonPath("$.*", anyOf(hasSize(4), hasSize(9), hasSize(8)))) | ||
.andExpect(jsonPath("$[0].*", hasSize(14))) | ||
.andDo(document("mariadb-template-get-all-films-all-columns")); | ||
} | ||
|
||
@Test | ||
@DisplayName("Find film by id with request param") | ||
void findFilmByID() throws Exception { | ||
mockMvc.perform(get(VERSION + "/mariadb/sql/select_by_id") | ||
.param("film_id", String.valueOf(ID)) | ||
.contentType(APPLICATION_JSON).accept(APPLICATION_JSON)) | ||
.andExpect(status().isOk()) | ||
.andDo(print()) | ||
.andExpect(jsonPath("$.*").isArray()) | ||
.andExpect(jsonPath("$.*", anyOf(hasSize(1)))) | ||
.andDo(document("mariadb-template-get-film-by-id-with-params")); | ||
} | ||
|
||
@Test | ||
@DisplayName("Find film by id with headers") | ||
void findFilmByIDWithHeader() throws Exception { | ||
mockMvc.perform(get(VERSION + "/mariadb/sql/select_by_id") | ||
.header("film_id", String.valueOf(ID)) | ||
.contentType(APPLICATION_JSON).accept(APPLICATION_JSON)) | ||
.andExpect(status().isOk()) | ||
.andDo(print()) | ||
.andExpect(jsonPath("$.*").isArray()) | ||
.andExpect(jsonPath("$.*", anyOf(hasSize(1)))) | ||
.andDo(document("mariadb-template-get-film-by-id-with-headers")); | ||
} | ||
|
||
@Test | ||
@DisplayName("Find film by id with custom path") | ||
void findFilmByIDWithPath() throws Exception { | ||
mockMvc.perform(get(VERSION + "/mariadb/sql/select_by_id/"+ ID) | ||
.header("paths", "film_id") | ||
.contentType(APPLICATION_JSON).accept(APPLICATION_JSON)) | ||
.andExpect(status().isOk()) | ||
.andDo(print()) | ||
.andExpect(jsonPath("$.*").isArray()) | ||
.andExpect(jsonPath("$.*", anyOf(hasSize(1)))) | ||
.andDo(document("mariadb-template-get-film-by-id-with-user-path")); | ||
} | ||
|
||
@Test | ||
@DisplayName("Failed to find film by id without provided param") | ||
void findFilmByIDWithRequiredConstraint() throws Exception { | ||
mockMvc.perform(get(VERSION + "/mariadb/sql/select_by_id_is_required") | ||
.contentType(APPLICATION_JSON).accept(APPLICATION_JSON)) | ||
.andExpect(status().is4xxClientError()) | ||
.andDo(document("mariadb-template-get-film-by-id-with-required-constraint")); | ||
} | ||
|
||
@Test | ||
@DisplayName("Conditional render: select all when no request params") | ||
void selectWithConditionalRenderNoRequestParams() throws Exception { | ||
mockMvc.perform(get(VERSION + "/mariadb/sql/conditional_render_and_op") | ||
.contentType(APPLICATION_JSON).accept(APPLICATION_JSON)) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.*").isArray()) | ||
.andExpect(jsonPath("$.*", anyOf(hasSize(4), hasSize(9), hasSize(8)))) | ||
.andExpect(jsonPath("$[0].*", hasSize(14))) | ||
.andDo(document("mariadb-template-conditional-render-no-request-params")); | ||
} | ||
|
||
@Test | ||
@DisplayName("Conditional render: select render one condition") | ||
void selectWithConditionalRenderWithID() throws Exception { | ||
var id = 4; | ||
mockMvc.perform(get(VERSION + "/mariadb/sql/conditional_render_and_op") | ||
.param("film_id", String.valueOf(id)) | ||
.contentType(APPLICATION_JSON).accept(APPLICATION_JSON)) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.*").isArray()) | ||
.andExpect(jsonPath("$.*", anyOf(hasSize(1)))) | ||
.andExpect(jsonPath("$[0].film_id").value(id)) | ||
.andDo(document("mariadb-template-conditional-render-with-id-condition")); | ||
} | ||
|
||
@Test | ||
@DisplayName("Conditional render: render both and operations") | ||
void selectWithConditionalRenderWithField() throws Exception { | ||
var id = 4; | ||
var rating = "G"; | ||
mockMvc.perform(get(VERSION + "/mariadb/sql/conditional_render_and_op") | ||
.param("film_id", String.valueOf(id)) | ||
.param("rating", rating) | ||
.contentType(APPLICATION_JSON).accept(APPLICATION_JSON)) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.*").isArray()) | ||
.andExpect(jsonPath("$.*", anyOf(hasSize(1)))) | ||
.andExpect(jsonPath("$[0].film_id").value(id)) | ||
.andExpect(jsonPath("$[0].rating").value(rating)) | ||
.andDo(document("mariadb-template-conditional-render-render-both-and-operations")); | ||
} | ||
|
||
|
||
@Test | ||
@DisplayName("Conditional render join: skip render join, select film by id") | ||
void selectWithConditionalRenderJoinWithoutInput() throws Exception { | ||
var film_id = 1; | ||
mockMvc.perform(get(VERSION + "/mariadb/sql/conditional_render_join") | ||
.param("film_id", String.valueOf(film_id)) | ||
.contentType(APPLICATION_JSON).accept(APPLICATION_JSON)) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.*").isArray()) | ||
.andExpect(jsonPath("$.*", anyOf(hasSize(1)))) | ||
.andExpect(jsonPath("$[0].film_id").value(film_id)) | ||
.andDo(document("mariadb-template-conditional-render-join-skip-render")); | ||
} | ||
|
||
@Test | ||
@DisplayName("Conditional render join: execute rendered join") | ||
void selectWithConditionalRenderJoin() throws Exception { | ||
var film_id = 1; | ||
var language_id = 1; | ||
mockMvc.perform(get(VERSION + "/mariadb/sql/conditional_render_join") | ||
.param("film_id", String.valueOf(film_id)) | ||
.param("language_id", String.valueOf(language_id)) | ||
.contentType(APPLICATION_JSON).accept(APPLICATION_JSON)) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.*").isArray()) | ||
.andExpect(jsonPath("$.*", anyOf(hasSize(1)))) | ||
.andExpect(jsonPath("$[0].film_id").value(film_id)) | ||
.andExpect(jsonPath("$[0].language_name").value("English")) | ||
.andDo(document("mariadb-template-conditional-render-join")); | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
SQL_TEMPLATE_PATH: classpath:testdata/sql-mariadb | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
SQL_TEMPLATE_PATH: classpath:testdata/sql | ||
SQL_TEMPLATE_PATH: classpath:testdata/sql-mysql |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions
9
api-rest/src/test/resources/testdata/sql-mysql/conditional_render_and_op.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
SELECT * | ||
FROM sakila.film f | ||
WHERE 1 = 1 | ||
{% if params.film_id %} | ||
AND f.film_id = {{ params.film_id }} | ||
{% endif %} | ||
{% if params.rating %} | ||
AND f.rating = {{ params.rating }} | ||
{% endif %} |
17 changes: 17 additions & 0 deletions
17
api-rest/src/test/resources/testdata/sql-mysql/conditional_render_join.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
SELECT | ||
{% if params.film_id and params.language_id %} | ||
f.film_id, f.title, l.name as language_name | ||
{% else %} | ||
* | ||
{% endif %} | ||
FROM sakila.film f | ||
|
||
{% if params.film_id and params.language_id %} | ||
JOIN | ||
sakila.language l | ||
ON f.language_id = l.language_id | ||
{% endif %} | ||
|
||
{% if params.film_id %} | ||
WHERE f.film_id = {{ params.film_id | is_required }} | ||
{% endif %} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
SELECT * FROM sakila.film; |
11 changes: 11 additions & 0 deletions
11
api-rest/src/test/resources/testdata/sql-mysql/select_by_id.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
SELECT * | ||
FROM sakila.film f | ||
WHERE 1 = 1 | ||
AND f.film_id = | ||
{% if params.film_id %} | ||
{{ params.film_id }} | ||
{% elif paths.film_id %} | ||
{{ paths.film_id }} | ||
{% elif headers.film_id %} | ||
{{ headers.film_id }} | ||
{% endif %} |
2 changes: 2 additions & 0 deletions
2
api-rest/src/test/resources/testdata/sql-mysql/select_by_id_is_required.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
SELECT * FROM sakila.film f | ||
WHERE f.film_id = {{ params.film_id | is_required }} |