Skip to content

Commit

Permalink
Merge pull request #133 from kdhrubo/feature/114_integration-test-insert
Browse files Browse the repository at this point in the history
Integration test for CreateController with MySQL database
  • Loading branch information
souravroy authored Jan 12, 2024
2 parents 292b2d3 + dcee533 commit 230bf8d
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 464 deletions.
20 changes: 20 additions & 0 deletions src/test/java/com/homihq/db2rest/MySQLContainerConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.homihq.db2rest;

import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.testcontainers.containers.MySQLContainer;

@TestConfiguration(proxyBeanMethods = false)
public class MySQLContainerConfiguration {
@Bean
@ServiceConnection
public MySQLContainer<?> mySQLContainer() {
var mySQLContainer = new MySQLContainer("mysql:8.2");
mySQLContainer.withUsername("mysql")
.withPassword("mysql")
.withInitScript("mysql/mysql-sakila.sql")
.withDatabaseName("sakila").withReuse(true);
return mySQLContainer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.homihq.db2rest.rest;

import com.homihq.db2rest.PostgreSQLBaseIntegrationTest;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;

import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
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;

class MySQLCreateControllerTest 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")
.header("Content-Profile", "public")
.content(json).accept(MediaType.APPLICATION_JSON))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.rows", Matchers.equalTo(1)))
.andDo(print())
.andDo(document("create-a-film"));

}

@Test
@DisplayName("Create a film with error.")
void createError() 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",
"country" : "USA"
}
""";

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

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.homihq.db2rest.rest;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.homihq.db2rest.PostgreSQLBaseIntegrationTest;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -15,8 +14,6 @@

class PgCreateControllerTest extends PostgreSQLBaseIntegrationTest {

private static ObjectMapper mapper = new ObjectMapper();

@Test
@DisplayName("Create a film.")
void create() throws Exception {
Expand Down
Loading

0 comments on commit 230bf8d

Please sign in to comment.