Skip to content

Commit

Permalink
pg,mysql update controller tests refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
souravroy committed Feb 10, 2024
1 parent 7594f8c commit ad40a9a
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 92 deletions.
Original file line number Diff line number Diff line change
@@ -1,96 +1,84 @@
package com.homihq.db2rest.rest;

import com.github.dockerjava.api.command.AuthCmd;
import com.homihq.db2rest.MySQLBaseIntegrationTest;
import org.hamcrest.Matchers;
import com.homihq.db2rest.utils.ITestUtil;
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.equalTo;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.patch;
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.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

public class MySQLUpdateControllerTest extends MySQLBaseIntegrationTest {
class MySQLUpdateControllerTest extends MySQLBaseIntegrationTest {

@Test
@DisplayName("Update an existing film")
public void updateExistingFilm() throws Exception {

var json = """
{
"rating": "NC-17"
}
""";
void updateExistingFilm() throws Exception {

mockMvc.perform(patch("/film")
.param("filter", "title==\"ACADEMY DINOSAUR\"")
.contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
.characterEncoding(UTF_8)
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.header("Content-Profile", "sakila")
.content(json).accept(MediaType.APPLICATION_JSON))
.param("filter", "title==\"ACADEMY DINOSAUR\"")
.content(ITestUtil.UPDATE_FILM_REQUEST))
.andExpect(status().isOk())
.andExpect(jsonPath("$.rows", Matchers.equalTo(1)))
.andExpect(jsonPath("$.rows", equalTo(1)))
.andDo(print())
.andDo(document("mysql-update-existing-film"));
}

@Test
@DisplayName("Update a non-existing film")
public void updateNonExistingFilm() throws Exception {

var json = """
{
"special_features": "CGI,Fantasy,Action"
}
""";
void updateNonExistingFilm() throws Exception {

mockMvc.perform(patch("/film")
.param("filter", "title==\"BAAHUBALI\"")
.contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
.characterEncoding(UTF_8)
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.header("Content-Profile", "sakila")
.content(json).accept(MediaType.APPLICATION_JSON))
.param("filter", "title==\"BAAHUBALI\"")
.content(ITestUtil.UPDATE_NON_EXISTING_FILM_REQUEST))
.andExpect(status().isOk())
.andExpect(jsonPath("$.rows", Matchers.equalTo(0)))
.andExpect(jsonPath("$.rows", equalTo(0)))
.andDo(print())
.andDo(document("mysql-update-non-existing-film"));
}

@Test
@DisplayName("Update non-existing table")
public void updateNonExistingTable() throws Exception {
var json = """
{
"sample_col": "sample value"
}
""";
void updateNonExistingTable() throws Exception {

mockMvc.perform(patch("/unnamed_table")
.param("filter", "sample_col==\"sample value 1\"")
.contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
.header("Content-Profile", "sakila")
.content(json).accept(MediaType.APPLICATION_JSON))
mockMvc.perform(patch("/unknown_table")
.characterEncoding(UTF_8)
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.header("Content-Profile", "sakila")
.param("filter", "sample_col==\"sample value 1\"")
.content(ITestUtil.UPDATE_NON_EXISTING_TABLE))
.andExpect(status().isBadRequest())
.andDo(print())
.andDo(document("mysql-update-non-existing-table"));
}

@Test
@DisplayName("Updating multiple films")
public void updateMultipleColumns() throws Exception {
var json = """
{
"rating": "PG"
}
""";
void updateMultipleColumns() throws Exception {

mockMvc.perform(patch("/film")
.characterEncoding(UTF_8)
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.param("filter", "rating==\"G\"")
.contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
.header("Content-Profile", "sakila")
.content(json).accept(MediaType.APPLICATION_JSON))
.content(ITestUtil.UPDATE_FILMS_REQUEST))
.andExpect(status().isOk())
.andExpect(jsonPath("$.rows", Matchers.equalTo(2)))
.andExpect(jsonPath("$.rows", equalTo(2)))
.andDo(print())
.andDo(document("mysql-update-multiple-films"));
}
Expand Down
79 changes: 34 additions & 45 deletions src/test/java/com/homihq/db2rest/rest/PgUpdateControllerTest.java
Original file line number Diff line number Diff line change
@@ -1,95 +1,84 @@
package com.homihq.db2rest.rest;

import com.homihq.db2rest.PostgreSQLBaseIntegrationTest;
import org.hamcrest.Matchers;
import com.homihq.db2rest.utils.ITestUtil;
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.equalTo;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.patch;
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;

public class PgUpdateControllerTest extends PostgreSQLBaseIntegrationTest {
class PgUpdateControllerTest extends PostgreSQLBaseIntegrationTest {

@Test
@DisplayName("Update an existing film")
public void updateExistingFilm() throws Exception {

var json = """
{
"rating": "NC-17"
}
""";
void updateExistingFilm() throws Exception {

mockMvc.perform(patch("/film")
.characterEncoding(UTF_8)
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.header("Content-Profile", "public")
.param("filter", "title==\"ACADEMY DINOSAUR\"")
.contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
.header("Content-Profile", "sakila")
.content(json).accept(MediaType.APPLICATION_JSON))
.content(ITestUtil.UPDATE_FILM_REQUEST))
.andExpect(status().isOk())
.andExpect(jsonPath("$.rows", Matchers.equalTo(1)))
.andExpect(jsonPath("$.rows", equalTo(1)))
.andDo(print())
.andDo(document("pg-update-existing-film"));
}

@Test
@DisplayName("Update a non-existing film")
public void updateNonExistingFilm() throws Exception {

var json = """
{
"special_features": "CGI,Fantasy,Action"
}
""";
void updateNonExistingFilm() throws Exception {

mockMvc.perform(patch("/film")
.characterEncoding(UTF_8)
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.header("Content-Profile", "public")
.param("filter", "title==\"BAAHUBALI\"")
.contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
.header("Content-Profile", "sakila")
.content(json).accept(MediaType.APPLICATION_JSON))
.content(ITestUtil.UPDATE_NON_EXISTING_FILM_REQUEST))
.andExpect(status().isOk())
.andExpect(jsonPath("$.rows", Matchers.equalTo(0)))
.andExpect(jsonPath("$.rows", equalTo(0)))
.andDo(print())
.andDo(document("pg-update-non-existing-film"));
}

@Test
@DisplayName("Update non-existing table")
public void updateNonExistingTable() throws Exception {
var json = """
{
"sample_col": "sample value"
}
""";
void updateNonExistingTable() throws Exception {

mockMvc.perform(patch("/unnamed_table")
mockMvc.perform(patch("/unknown_table")
.characterEncoding(UTF_8)
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.header("Content-Profile", "public")
.param("filter", "sample_col==\"sample value 1\"")
.contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
.header("Content-Profile", "sakila")
.content(json).accept(MediaType.APPLICATION_JSON))
.content(ITestUtil.UPDATE_NON_EXISTING_TABLE))
.andExpect(status().isBadRequest())
.andDo(print())
.andDo(document("pg-update-non-existing-table"));
}

@Test
@DisplayName("Updating multiple films")
public void updateMultipleColumns() throws Exception {
var json = """
{
"rating": "PG"
}
""";
void updateMultipleColumns() throws Exception {

mockMvc.perform(patch("/film")
.characterEncoding(UTF_8)
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.param("filter", "rating==\"G\"")
.contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
.header("Content-Profile", "sakila")
.content(json).accept(MediaType.APPLICATION_JSON))
.header("Content-Profile", "public")
.content(ITestUtil.UPDATE_FILMS_REQUEST))
.andExpect(status().isOk())
.andExpect(jsonPath("$.rows", Matchers.equalTo(2)))
.andExpect(jsonPath("$.rows", equalTo(2)))
.andDo(print())
.andDo(document("pg-update-multiple-films"));
}
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/com/homihq/db2rest/utils/ITestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,32 @@ public class ITestUtil {
""";


/* Update film */

public final String UPDATE_FILM_REQUEST = """
{
"rating": "NC-17"
}
""";

public final String UPDATE_NON_EXISTING_FILM_REQUEST = """
{
"special_features": "CGI,Fantasy,Action"
}
""";

public final String UPDATE_NON_EXISTING_TABLE = """
{
"sample_col": "sample value"
}
""";

public final String UPDATE_FILMS_REQUEST = """
{
"rating": "PG"
}
""";

/* CREATE director */

public final String CREATE_DIRECTOR_REQUEST = """
Expand Down

0 comments on commit ad40a9a

Please sign in to comment.