Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration test for CreateController with MySQL database #133

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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