Skip to content

Commit

Permalink
pg,mysql read, delete controller test refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
souravroy committed Feb 10, 2024
1 parent c51bd54 commit f111cc9
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.homihq.db2rest.rest;

import com.homihq.db2rest.MySQLBaseIntegrationTest;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.delete;
Expand All @@ -19,11 +19,10 @@ class MySQLDeleteControllerTest extends MySQLBaseIntegrationTest {
@DisplayName("Delete a Director")
void delete_single_record() throws Exception {
mockMvc.perform(delete("/director")
.param("filter", "first_name==\"Alex\"")
// .header("Content-Profile", "sakila")
.accept(APPLICATION_JSON))
.accept(APPLICATION_JSON)
.param("filter", "first_name==\"Alex\""))
.andExpect(status().isOk())
.andExpect(jsonPath("$.rows", Matchers.equalTo(1)))
.andExpect(jsonPath("$.rows", equalTo(1)))
.andDo(print())
.andDo(document("mysql-delete-a-director"));
}
Expand All @@ -32,7 +31,6 @@ void delete_single_record() throws Exception {
@DisplayName("Delete all records while allowSafeDelete=true")
void delete_all_records_with_allow_safe_delete_true() throws Exception {
mockMvc.perform(delete("/director")
// .header("Content-Profile", "sakila")
.accept(APPLICATION_JSON))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.detail",
Expand All @@ -45,9 +43,8 @@ void delete_all_records_with_allow_safe_delete_true() throws Exception {
@DisplayName("Column Does Not Exist")
void column_does_not_exist() throws Exception {
mockMvc.perform(delete("/director")
.param("filter", "_name==\"Alex\"")
//.header("Content-Profile", "sakila")
.accept(APPLICATION_JSON))
.accept(APPLICATION_JSON)
.param("filter", "_name==\"Alex\""))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.detail",
containsString("Invalid column director._name")))
Expand All @@ -59,9 +56,8 @@ void column_does_not_exist() throws Exception {
@DisplayName("Foreign Key Constraint Violation")
void foreign_key_constraint_violation() throws Exception {
mockMvc.perform(delete("/language")
.param("filter", "name==\"ENGLISH\"")
//.header("Content-Profile", "sakila")
.accept(APPLICATION_JSON))
.accept(APPLICATION_JSON)
.param("filter", "name==\"ENGLISH\""))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.detail",
containsString("Cannot delete or update a parent row: a foreign key constraint fails")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,25 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.ResultMatcher;

import static org.hamcrest.Matchers.hasSize;
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.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

class MySQLReadControllerTest extends MySQLBaseIntegrationTest {
@Test
@Disabled // TODO: Need to fix
@DisplayName("Get all fields.")
void findAllFilms() throws Exception {

mockMvc.perform(get("/film").header("Accept-Profile", "sakila")
.accept(MediaType.APPLICATION_JSON))
mockMvc.perform(get("/film")
.accept(MediaType.APPLICATION_JSON)
.header("Accept-Profile", "sakila"))
.andExpect(status().isOk())
//.andDo(print())
.andDo(document("mysql-get-all-films"));
}


@Test
@DisplayName("Get count")
void findFilmCount() throws Exception {
Expand All @@ -41,7 +38,8 @@ void findFilmCount() throws Exception {
@Test
@DisplayName("Get one")
void findOneFilm() throws Exception {
mockMvc.perform(get("/film/one").accept(MediaType.APPLICATION_JSON)
mockMvc.perform(get("/film/one")
.accept(MediaType.APPLICATION_JSON)
.param("select", "description"))
.andExpect(status().isOk())
//.andDo(print())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class PgDeleteControllerTest extends PostgreSQLBaseIntegrationTest {
@DisplayName("Delete a Director")
void delete_single_record() throws Exception {
mockMvc.perform(delete("/director")
.param("filter", "first_name==\"Alex\"")
.accept(APPLICATION_JSON))
.accept(APPLICATION_JSON)
.param("filter", "first_name==\"Alex\""))
.andExpect(status().isOk())
.andExpect(jsonPath("$.rows", Matchers.equalTo(1)))
.andDo(print())
Expand All @@ -43,8 +43,8 @@ void delete_all_records_with_allow_safe_delete_true() throws Exception {
@DisplayName("Column Does Not Exist")
void column_does_not_exist() throws Exception {
mockMvc.perform(delete("/director")
.param("filter", "_name==\"Alex\"")
.accept(APPLICATION_JSON))
.accept(APPLICATION_JSON)
.param("filter", "_name==\"Alex\""))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.detail",
containsString("Invalid column director._name")))
Expand All @@ -56,8 +56,8 @@ void column_does_not_exist() throws Exception {
@DisplayName("Foreign Key Constraint Violation")
void foreign_key_constraint_violation() throws Exception {
mockMvc.perform(delete("/language")
.param("filter", "name==\"English\"")
.accept(APPLICATION_JSON))
.accept(APPLICATION_JSON)
.param("filter", "name==\"English\""))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.detail",
containsString("ERROR: update or delete on table \"language\" violates foreign key " +
Expand Down
76 changes: 31 additions & 45 deletions src/test/java/com/homihq/db2rest/rest/PgReadControllerTest.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package com.homihq.db2rest.rest;

import com.homihq.db2rest.PostgreSQLBaseIntegrationTest;
import com.homihq.db2rest.utils.ITestUtil;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;

import static org.hamcrest.Matchers.*;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
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.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;

Expand All @@ -21,28 +24,23 @@ class PgReadControllerTest extends PostgreSQLBaseIntegrationTest {
@DisplayName("C")
void findAllFilms() throws Exception {

mockMvc.perform(get("/film").header("Accept-Profile", "public")
.accept(MediaType.APPLICATION_JSON))
mockMvc.perform(get("/film")
.accept(APPLICATION_JSON)
.header("Accept-Profile", "public"))
.andExpect(status().isOk())
// .andDo(print())
// .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))
mockMvc.perform(post("/query")
.characterEncoding(UTF_8)
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.content(ITestUtil.SINGLE_RESULT_ACTOR_QUERY))
.andExpect(status().isOk())
.andExpect(jsonPath("$.first_name", equalTo("PENELOPE")))
//.andDo(print())
Expand All @@ -52,19 +50,12 @@ void query_returns_single_result() throws Exception {
@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))
mockMvc.perform(post("/query")
.characterEncoding(UTF_8)
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.content(ITestUtil.BULK_RESULT_ACTOR_QUERY))
.andExpect(status().isOk())
.andExpect(jsonPath("$.*", hasSize(2)))
.andExpect(jsonPath("$[0].first_name", equalTo("PENELOPE")))
Expand All @@ -76,42 +67,37 @@ void query_returns_list_of_results() throws Exception {
@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))
mockMvc.perform(post("/query")
.characterEncoding(UTF_8)
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.content(ITestUtil.EMPTY_ACTOR_QUERY))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.status", is(400)))
// .andDo(print())
// .andDo(print())
.andDo(document("pg-create-a-film"));
}

@Test
@DisplayName("Get count")
void findFilmCount() throws Exception {
mockMvc.perform(get("/film/count")
.accept(MediaType.APPLICATION_JSON))
.accept(APPLICATION_JSON))
.andExpect(status().isOk())
// .andDo(print())
// .andDo(print())
.andDo(document("pg-get-film-count"));

}

@Test
@DisplayName("Get one")
void findOneFilm() throws Exception {
mockMvc.perform(get("/film/one").accept(MediaType.APPLICATION_JSON)
mockMvc.perform(get("/film/one")
.accept(APPLICATION_JSON)
.param("select", "title"))
.andExpect(status().isOk())
// .andDo(print())
// .andDo(print())
.andDo(document("pg-get-on-film"));
}
}
40 changes: 37 additions & 3 deletions src/test/java/com/homihq/db2rest/utils/ITestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@UtilityClass
public class ITestUtil {

/* table: film */
/* CREATE film */

public final String CREATE_FILM_REQUEST = """
{
Expand Down Expand Up @@ -106,7 +106,7 @@ public class ITestUtil {
""";


/* table: director */
/* CREATE director */

public final String CREATE_DIRECTOR_REQUEST = """
{
Expand Down Expand Up @@ -142,7 +142,7 @@ public class ITestUtil {
""";


/* table: review */
/* CREATE review */

public final String BULK_CREATE_REVIEW_REQUEST = """
[
Expand All @@ -159,4 +159,38 @@ public class ITestUtil {
]
""";


/* Query Actor */

public final String SINGLE_RESULT_ACTOR_QUERY = """
{
"sql": "SELECT FIRST_NAME,LAST_NAME FROM ACTOR WHERE ACTOR_ID = :id",
"params" : {
"id" : 1
},
"single" : true
}
""";

public final String BULK_RESULT_ACTOR_QUERY = """
{
"sql": "SELECT FIRST_NAME,LAST_NAME FROM ACTOR WHERE ACTOR_ID IN (:id1, :id2)",
"params" : {
"id1" : 1,
"id2" : 2
},
"single" : false
}
""";

public final String EMPTY_ACTOR_QUERY = """
{
"sql": "",
"params" : {
"id1" : 1
},
"single" : false
}
""";

}

0 comments on commit f111cc9

Please sign in to comment.