Skip to content

Commit

Permalink
pg bul create controller test - work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
souravroy committed Feb 10, 2024
1 parent d42f5a8 commit 8cf2497
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 140 deletions.
175 changes: 40 additions & 135 deletions src/test/java/com/homihq/db2rest/rest/PgBulkCreateControllerTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
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 java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.Matchers.*;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
Expand All @@ -18,44 +20,12 @@ class PgBulkCreateControllerTest extends PostgreSQLBaseIntegrationTest {
@DisplayName("Create many films.")
void create() throws Exception {

var json = """
[
{
"title" : "Dunki",
"description" : "Film about illegal immigration" ,
"release_year" : 2023,
"language_id" : 6,
"original_language_id" : 6,
"rental_duration" : 6,
"rental_rate" : 0.99 ,
"length" : 150,
"replacement_cost" : 20.99 ,
"rating" : "PG-13" ,
"special_features" : "Commentaries"
},
{
"title" : "Jawan",
"description" : "Socio-econmic problems and corruption" ,
"release_year" : 2023,
"language_id" : 6,
"original_language_id" : 6,
"rental_duration" : 5,
"rental_rate" : 0.99 ,
"length" : 160,
"replacement_cost" : 20.99 ,
"rating" : "PG-13" ,
"special_features" : "Commentaries"
}
]
""";


mockMvc.perform(post("/film/bulk").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
mockMvc.perform(post("/film/bulk")
.characterEncoding(UTF_8)
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.header("Content-Profile", "public")
.content(json).accept(MediaType.APPLICATION_JSON))
.content(ITestUtil.BULK_CREATE_FILM_REQUEST))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.rows").isArray())
.andExpect(jsonPath("$.rows", hasSize(2)))
Expand All @@ -69,27 +39,18 @@ void create() throws Exception {

}


@Test
@DisplayName("Create many films with CSV type.")
void createCSV() throws Exception {

String csv = """
title,description,release_year,language_id,original_language_id,rental_duration,rental_rate,length,replacement_cost,rating,special_features
Dunki2,Film about illegal immigration,2023,6,6,6,0.99,150,20.99,PG-13,Commentaries
Jawan2,Socio-econmic problems and corruption,2023,6,6,6,0.99,160,20.99,PG-13,Commentaries
""";

mockMvc.perform(post("/film/bulk").characterEncoding("utf-8")
.content(csv)
mockMvc.perform(post("/film/bulk")
.characterEncoding(UTF_8)
.contentType("text/csv")
.accept(MediaType.APPLICATION_JSON))
.accept(APPLICATION_JSON)
.content(ITestUtil.CREATE_FILM_REQUEST_CSV))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.rows").isArray())
.andExpect(jsonPath("$.rows", hasSize(2)))
.andExpect(jsonPath("$.rows", hasItem(1)))
.andExpect(jsonPath("$.rows", hasItem(1)))
.andExpect(jsonPath("$.generated_keys").isArray())
.andExpect(jsonPath("$.generated_keys", hasSize(2)))
.andExpect(jsonPath("$.generated_keys", allOf(notNullValue())))
//.andDo(print())
Expand All @@ -102,94 +63,45 @@ void createCSV() throws Exception {
@DisplayName("Create many films with CSV type resulting error.")
void createCSVWithError() throws Exception {

String csv = """
title,description,release_year,language_id,original_language_id,rental_duration,rental_rate,length,replacement_cost,rating,special_features,country
Dunki2,Film about illegal immigration,2023,6,6,6,0.99,150,20.99,PG-13,Commentaries,India
Jawan2,Socio-econmic problems and corruption,2023,6,6,6,0.99,160,20.99,PG-13,Commentaries,India
""";

mockMvc.perform(post("/film/bulk").characterEncoding("utf-8")
.content(csv)
mockMvc.perform(post("/film/bulk")
.characterEncoding(UTF_8)
.contentType("text/csv")
.accept(MediaType.APPLICATION_JSON))
.accept(APPLICATION_JSON)
.content(ITestUtil.CREATE_FILM_BAD_REQUEST_CSV))
.andExpect(status().isBadRequest())
//.andDo(print())
.andDo(document("pg-bulk-create-films-csv-error"));

}


@Test
@DisplayName("Create many films with failure.")
void createError() throws Exception {

var json = """
[
{
"title" : "Dunki",
"description" : "Film about illegal immigration" ,
"release_year" : 2023,
"language_id" : 6,
"original_language_id" : 6,
"rental_duration" : 6,
"rental_rate" : 0.99 ,
"length" : 150,
"replacement_cost" : 20.99 ,
"rating" : "PG-13" ,
"special_features" : "Commentaries",
"country" : "INDIA"
},
{
"title" : "Jawan",
"description" : "Socio-econmic problems and corruption" ,
"release_year" : 2023,
"language_id" : 6,
"original_language_id" : 6,
"rental_duration" : 5,
"rental_rate" : 0.99 ,
"length" : 160,
"replacement_cost" : 20.99 ,
"rating" : "PG-13" ,
"special_features" : "Commentaries"
}
]
""";


mockMvc.perform(post("/film/bulk").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
mockMvc.perform(post("/film/bulk")
.characterEncoding(UTF_8)
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.header("Content-Profile", "public")
.content(json).accept(MediaType.APPLICATION_JSON))
.content(ITestUtil.BULK_CREATE_FILM_BAD_REQUEST))
.andExpect(status().isBadRequest())
// .andDo(print())
// .andDo(print())
.andDo(document("pg-bulk-create-films-error"));

}


@Test
@DisplayName("Create many directors.")
void createDirector() throws Exception {
var json = """
[
{
"first_name": "Anurag",
"last_name": "Kashyap"
},
{
"first_name": "Rajkumar",
"last_name": "Hirani"
}
]
""";

mockMvc.perform(post("/director/bulk").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
mockMvc.perform(post("/director/bulk")
.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.BULK_CREATE_DIRECTOR_REQUEST))
.andExpect(status().isCreated())
//.andDo(print())
.andDo(document("pg-bulk-create-directors"));
Expand All @@ -199,24 +111,15 @@ void createDirector() throws Exception {
@Test
@DisplayName("Create many directors with wrong tsid type.")
void createDirectorWithWrongTsidType() throws Exception {
var json = """
[
{
"first_name": "Anurag",
"last_name": "Kashyap"
},
{
"first_name": "Rajkumar",
"last_name": "Hirani"
}
]
""";

mockMvc.perform(post("/director/bulk").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
mockMvc.perform(post("/director/bulk")
.characterEncoding(UTF_8)
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.param("tsid", "director_id")
.param("tsidType", "string")
.header("Content-Profile", "public")
.content(json).accept(MediaType.APPLICATION_JSON))
.content(ITestUtil.BULK_CREATE_DIRECTOR_BAD_REQUEST))
.andExpect(status().isBadRequest())
//.andDo(print())
.andDo(document("pg-bulk-create-directors-with-wrong-tsid-type"));
Expand All @@ -241,15 +144,17 @@ void createReviewWithDefaultTsidType() throws Exception {
]
""";

mockMvc.perform(post("/review/bulk").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
.param("tsid", "review_id")
mockMvc.perform(post("/review/bulk")
.characterEncoding(UTF_8)
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.header("Content-Profile", "public")
.content(json).accept(MediaType.APPLICATION_JSON))
.param("tsid", "review_id")
.content(json))
.andExpect(status().isCreated())
//.andDo(print())
.andDo(document("pg-bulk-create-reviews-with-default-tsid-type"));

}



}
118 changes: 113 additions & 5 deletions src/test/java/com/homihq/db2rest/utils/ITestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,118 @@ public class ITestUtil {
}
""";

public final String BULK_CREATE_FILM_REQUEST = """
[
{
"title" : "Dunki",
"description" : "Film about illegal immigration" ,
"release_year" : 2023,
"language_id" : 6,
"original_language_id" : 6,
"rental_duration" : 6,
"rental_rate" : 0.99 ,
"length" : 150,
"replacement_cost" : 20.99 ,
"rating" : "PG-13" ,
"special_features" : "Commentaries"
},
{
"title" : "Jawan",
"description" : "Socio-econmic problems and corruption" ,
"release_year" : 2023,
"language_id" : 6,
"original_language_id" : 6,
"rental_duration" : 5,
"rental_rate" : 0.99 ,
"length" : 160,
"replacement_cost" : 20.99 ,
"rating" : "PG-13" ,
"special_features" : "Commentaries"
}
]
""";

public final String BULK_CREATE_FILM_BAD_REQUEST = """
[
{
"title" : "Dunki",
"description" : "Film about illegal immigration" ,
"release_year" : 2023,
"language_id" : 6,
"original_language_id" : 6,
"rental_duration" : 6,
"rental_rate" : 0.99 ,
"length" : 150,
"replacement_cost" : 20.99 ,
"rating" : "PG-13" ,
"special_features" : "Commentaries",
"country" : "INDIA"
},
{
"title" : "Jawan",
"description" : "Socio-econmic problems and corruption" ,
"release_year" : 2023,
"language_id" : 6,
"original_language_id" : 6,
"rental_duration" : 5,
"rental_rate" : 0.99 ,
"length" : 160,
"replacement_cost" : 20.99 ,
"rating" : "PG-13" ,
"special_features" : "Commentaries"
}
]
""";

public final String CREATE_FILM_REQUEST_CSV = """
title,description,release_year,language_id,original_language_id,rental_duration,rental_rate,length,replacement_cost,rating,special_features
Dunki2,Film about illegal immigration,2023,6,6,6,0.99,150,20.99,PG-13,Commentaries
Jawan2,Socio-econmic problems and corruption,2023,6,6,6,0.99,160,20.99,PG-13,Commentaries
""";

public final String CREATE_FILM_BAD_REQUEST_CSV = """
title,description,release_year,language_id,original_language_id,rental_duration,rental_rate,length,replacement_cost,rating,special_features,country
Dunki2,Film about illegal immigration,2023,6,6,6,0.99,150,20.99,PG-13,Commentaries,India
Jawan2,Socio-econmic problems and corruption,2023,6,6,6,0.99,160,20.99,PG-13,Commentaries,India
""";

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

public final String BULK_CREATE_DIRECTOR_REQUEST = """
[
{
"first_name": "Anurag",
"last_name": "Kashyap"
},
{
"first_name": "Rajkumar",
"last_name": "Hirani"
}
]
""";

public final String BULK_CREATE_DIRECTOR_BAD_REQUEST = """
[
{
"first_name": "Anurag",
"last_name": "Kashyap"
},
{
"first_name": "Rajkumar",
"last_name": "Hirani"
}
]
""";

}

0 comments on commit 8cf2497

Please sign in to comment.