Skip to content

Commit

Permalink
pg create controller test - work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
souravroy committed Feb 8, 2024
1 parent b86ec58 commit 49a21f8
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 131 deletions.
192 changes: 61 additions & 131 deletions src/test/java/com/homihq/db2rest/rest/PgCreateControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
import org.hamcrest.Matchers;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;

import com.homihq.db2rest.utils.ITestUtil;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
Expand All @@ -21,28 +24,12 @@ class PgCreateControllerTest extends PostgreSQLBaseIntegrationTest {
@Test
@DisplayName("Create a film.")
void create() throws Exception {

var json = """
{
"title" : "Dunki",
"description" : "Film about illegal immigration" ,
"release_year" : 2023,
"language_id" : 1,
"original_language_id" : null,
"rental_duration" : 6,
"rental_rate" : 0.99 ,
"length" : 150,
"replacement_cost" : 20.99 ,
"rating" : "PG-13" ,
"special_features" : "Commentaries"
}
""";


mockMvc.perform(post("/film").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")

mockMvc.perform(post("/film")
.contentType(APPLICATION_JSON)
.characterEncoding(UTF_8)
.header("Content-Profile", "public")
.content(json).accept(MediaType.APPLICATION_JSON))
.content(ITestUtil.CREATE_FILM_REQUEST).accept(APPLICATION_JSON))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.row", Matchers.equalTo(1)))
.andExpect(jsonPath("$.generated_key").exists())
Expand Down Expand Up @@ -73,9 +60,11 @@ void createError() throws Exception {
""";


mockMvc.perform(post("/film").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
mockMvc.perform(post("/film")
.contentType(APPLICATION_JSON)
.characterEncoding(UTF_8)
.header("Content-Profile", "public")
.content(json).accept(MediaType.APPLICATION_JSON))
.content(json).accept(APPLICATION_JSON))
.andExpect(status().isBadRequest())
.andDo(print())
.andDo(document("pg-create-a-film-error"));
Expand All @@ -86,26 +75,11 @@ void createError() throws Exception {
@DisplayName("Create a film - non existent table.")
void createNonExistentTable() throws Exception {

var json = """
{
"title" : "Dunki",
"description" : "Film about illegal immigration" ,
"release_year" : 2023,
"language_id" : 1,
"original_language_id" : null,
"rental_duration" : 6,
"rental_rate" : 0.99 ,
"length" : 150,
"replacement_cost" : 20.99 ,
"rating" : "PG-13" ,
"special_features" : "Commentaries"
}
""";

mockMvc.perform(post("/films").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
mockMvc.perform(post("/films")
.contentType(APPLICATION_JSON)
.characterEncoding(UTF_8)
.header("Content-Profile", "public")
.content(json).accept(MediaType.APPLICATION_JSON))
.content(ITestUtil.CREATE_FILM_REQUEST).accept(APPLICATION_JSON))
.andExpect(status().isBadRequest())
.andDo(print())
.andDo(document("pg-create-a-film-no-table"));
Expand All @@ -116,18 +90,14 @@ void createNonExistentTable() throws Exception {
@DisplayName("Create a director - number tsid type")
void createDirectorWithGivenTsidType() throws Exception {

var json = """
{
"first_name": "Rajkumar",
"last_name": "Hirani"
}
""";

mockMvc.perform(post("/director").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
mockMvc.perform(post("/director")
.characterEncoding(UTF_8)
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.header("Content-Profile", "public")
.param("tsid", "director_id")
.param("tsidType", "number")
.header("Content-Profile", "public")
.content(json).accept(MediaType.APPLICATION_JSON))
.content(ITestUtil.CREATE_DIRECTOR_REQUEST))
.andExpect(status().isCreated())
.andDo(print())
.andDo(document("pg-create-a-director-with-tsid-given-tsid-type"));
Expand All @@ -137,17 +107,15 @@ void createDirectorWithGivenTsidType() throws Exception {
@Test
@DisplayName("Create a director - with wrong tsid type")
void createDirectorWithWrongTsidType() throws Exception {
var json = """
{
"first_name": "Rohit",
"last_name": "Shetty"
}
""";
mockMvc.perform(post("/director").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")

mockMvc.perform(post("/director")
.characterEncoding(UTF_8)
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.header("Content-Profile", "public")
.param("tsid", "director_id")
.param("tsidType", "float") //only support string, number
.header("Content-Profile", "public")
.content(json).accept(MediaType.APPLICATION_JSON))
.content(ITestUtil.CREATE_DIRECTOR_REQUEST))
.andExpect(status().isBadRequest())
.andDo(print())
.andDo(document("pg-create-a-director-with-wrong-tsid-type"));
Expand All @@ -157,16 +125,14 @@ void createDirectorWithWrongTsidType() throws Exception {
@Test
@DisplayName("Create a director - with default tsid type")
void createDirectorWithDefaultTsidType() throws Exception {
var json = """
{
"first_name": "Anurag",
"last_name": "Kashyap"
}
""";
mockMvc.perform(post("/director").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
.param("tsid", "director_id")

mockMvc.perform(post("/director")
.characterEncoding(UTF_8)
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.header("Content-Profile", "public")
.content(json).accept(MediaType.APPLICATION_JSON))
.param("tsid", "director_id")
.content(ITestUtil.CREATE_DIRECTOR_REQUEST))
.andExpect(status().isCreated())
.andDo(print())
.andDo(document("pg-create-a-director-with-tsid-given-tsid-type"));
Expand All @@ -175,27 +141,13 @@ void createDirectorWithDefaultTsidType() throws Exception {
@Test
@DisplayName("Create a film with subset of columns")
void create_a_film_with_columns_specified_in_query_param() throws Exception {
var json = """
{
"title" : "Dunki",
"description" : "Film about illegal immigration",
"release_year" : 2023,
"language_id" : 1,
"original_language_id" : null,
"rental_duration" : 6,
"rental_rate" : 0.99 ,
"length" : 150,
"replacement_cost" : 20.99 ,
"rating" : "PG-13" ,
"special_features" : "Commentaries"
}
""";

var result = mockMvc.perform(post("/film")
.contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
.characterEncoding(UTF_8)
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.header("Content-Profile", "public")
.queryParam("columns", "title,description,language_id")
.content(json).accept(MediaType.APPLICATION_JSON))
.content(ITestUtil.CREATE_FILM_REQUEST))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.row", equalTo(1)))
.andExpect(jsonPath("$.generated_key").exists())
Expand All @@ -206,7 +158,7 @@ void create_a_film_with_columns_specified_in_query_param() throws Exception {
var primary_key = JsonPath.read(result.getResponse().getContentAsString(), "$.generated_key");

mockMvc.perform(get("/film")
.accept(MediaType.APPLICATION_JSON)
.accept(APPLICATION_JSON)
.queryParam("select", "title,release_year")
.queryParam("filter", String.format("film_id==%s", primary_key)))
.andExpect(status().isOk())
Expand All @@ -215,33 +167,20 @@ void create_a_film_with_columns_specified_in_query_param() throws Exception {
.andDo(print());

// cleanup data
assertTrue(deleteRow("film", "film_id", (int)primary_key));
assertTrue(deleteRow("film", "film_id", (int) primary_key));
}

@Test
@DisplayName("Ignore if columns parameter is blank")
void should_ignore_when_columns_query_param_is_empty() throws Exception {
var json = """
{
"title" : "Dunki",
"description" : "Film about illegal immigration",
"release_year" : 2023,
"language_id" : 1,
"original_language_id" : null,
"rental_duration" : 6,
"rental_rate" : 0.99 ,
"length" : 150,
"replacement_cost" : 20.99 ,
"rating" : "PG-13" ,
"special_features" : "Commentaries"
}
""";

var result = mockMvc.perform(post("/film")
.contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
.characterEncoding(UTF_8)
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.header("Content-Profile", "public")
.queryParam("columns", "")
.content(json).accept(MediaType.APPLICATION_JSON))
.content(ITestUtil.CREATE_FILM_REQUEST))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.row", equalTo(1)))
.andExpect(jsonPath("$.generated_key").exists())
Expand All @@ -252,7 +191,7 @@ void should_ignore_when_columns_query_param_is_empty() throws Exception {
var primary_key = JsonPath.read(result.getResponse().getContentAsString(), "$.generated_key");

mockMvc.perform(get("/film")
.accept(MediaType.APPLICATION_JSON)
.accept(APPLICATION_JSON)
.queryParam("select", "title,release_year")
.queryParam("filter", String.format("film_id==%s", primary_key)))
.andExpect(status().isOk())
Expand All @@ -261,7 +200,7 @@ void should_ignore_when_columns_query_param_is_empty() throws Exception {
.andDo(print());

// cleanup data
assertTrue(deleteRow("film", "film_id", (int)primary_key));
assertTrue(deleteRow("film", "film_id", (int) primary_key));
}

@Test
Expand All @@ -282,10 +221,13 @@ void column_is_present_in_columns_query_param_but_not_in_payload() throws Except
}
""";

var result = mockMvc.perform(post("/film").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
var result = mockMvc.perform(post("/film")
.characterEncoding(UTF_8)
.contentType(APPLICATION_JSON)
.header("Content-Profile", "public")
.accept(APPLICATION_JSON)
.queryParam("columns", "title,description,language_id")
.content(json).accept(MediaType.APPLICATION_JSON))
.content(json))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.row", equalTo(1)))
.andExpect(jsonPath("$.generated_key").exists())
Expand All @@ -296,7 +238,7 @@ void column_is_present_in_columns_query_param_but_not_in_payload() throws Except
var primary_key = JsonPath.read(result.getResponse().getContentAsString(), "$.generated_key");

mockMvc.perform(get("/film")
.accept(MediaType.APPLICATION_JSON)
.accept(APPLICATION_JSON)
.queryParam("select", "title,description")
.queryParam("filter", String.format("film_id==%s", primary_key)))
.andExpect(status().isOk())
Expand All @@ -305,32 +247,20 @@ void column_is_present_in_columns_query_param_but_not_in_payload() throws Except
.andDo(print());

// cleanup data
assertTrue(deleteRow("film", "film_id", (int)primary_key));
assertTrue(deleteRow("film", "film_id", (int) primary_key));
}

@Test
@DisplayName("Column violates not-null constraint")
void column_violates_not_null_constraint() throws Exception {
var json = """
{
"title" : "Dunki",
"description" : "Film about illegal immigration",
"release_year" : 2023,
"language_id" : 1,
"original_language_id" : null,
"rental_duration" : 6,
"rental_rate" : 0.99 ,
"length" : 150,
"replacement_cost" : 20.99 ,
"rating" : "PG-13" ,
"special_features" : "Commentaries"
}
""";

mockMvc.perform(post("/film").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
mockMvc.perform(post("/film")
.characterEncoding(UTF_8)
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.header("Content-Profile", "public")
.queryParam("columns", "title,description")
.content(json).accept(MediaType.APPLICATION_JSON))
.content(ITestUtil.CREATE_FILM_REQUEST))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.detail",
containsString("null value in column \"language_id\" of relation \"film\" violates not-null constraint")))
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/com/homihq/db2rest/utils/ITestUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.homihq.db2rest.utils;

import lombok.experimental.UtilityClass;

@UtilityClass
public class ITestUtil {
public final String CREATE_FILM_REQUEST = """
{
"title" : "Dunki",
"description" : "Film about illegal immigration" ,
"release_year" : 2023,
"language_id" : 1,
"original_language_id" : null,
"rental_duration" : 6,
"rental_rate" : 0.99 ,
"length" : 150,
"replacement_cost" : 20.99 ,
"rating" : "PG-13" ,
"special_features" : "Commentaries"
}
""";

public final String CREATE_DIRECTOR_REQUEST = """
{
"first_name": "Rohit",
"last_name": "Shetty"
}
""";
}

0 comments on commit 49a21f8

Please sign in to comment.