Skip to content

Commit

Permalink
#497 - multi schema update and create tests
Browse files Browse the repository at this point in the history
  • Loading branch information
grabdoc committed Apr 26, 2024
1 parent b20b227 commit ce0b348
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.homihq.db2rest.rest.mysql;

import com.adelean.inject.resources.junit.jupiter.GivenJsonResource;
import com.adelean.inject.resources.junit.jupiter.TestWithResources;
import com.adelean.inject.resources.junit.jupiter.WithJacksonMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.homihq.db2rest.MySQLBaseIntegrationTest;
import com.jayway.jsonpath.JsonPath;
import org.junit.jupiter.api.*;

import java.util.Map;

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.get;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@TestClassOrder(ClassOrderer.OrderAnnotation.class)
@Order(82)
@TestWithResources
class CreateTwoTablesSameNameDiffSchemaTest extends MySQLBaseIntegrationTest {
@WithJacksonMapper
ObjectMapper objectMapper = new ObjectMapper()
.registerModule(new JavaTimeModule());

@GivenJsonResource("/testdata/CREATE_EMP_REQUEST.json")
Map<String,Object> CREATE_EMP_REQUEST;



@Test
@DisplayName("Create emp diff schema")
void create() throws Exception {

mockMvc.perform(post("/employee")
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.header("Content-Profile", "sakila")
.content(objectMapper.writeValueAsString(CREATE_EMP_REQUEST))
)
//.andDo(print())
.andExpect(status().isCreated())
.andExpect(jsonPath("$.row", equalTo(1)))
//.andExpect(jsonPath("$.keys.GENERATED_KEY").exists())
//.andExpect(jsonPath("$.keys.GENERATED_KEY", equalTo(5)))
.andDo(document("mysql-create-emp-sakila"));

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.homihq.db2rest.MySQLBaseIntegrationTest;
import org.junit.jupiter.api.*;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
Expand All @@ -14,7 +13,7 @@

@TestClassOrder(ClassOrderer.OrderAnnotation.class)
@Order(92)
class MySQLDeleteSameTableDifferentSchemaControllerTest extends MySQLBaseIntegrationTest {
class DeleteTwoTablesSameNameDiffSchemaTest extends MySQLBaseIntegrationTest {

@Test
@DisplayName("Delete Employee Same table different database")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.homihq.db2rest.MySQLBaseIntegrationTest;
import org.junit.jupiter.api.*;
import org.springframework.http.MediaType;

import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.notNullValue;
Expand All @@ -16,7 +15,7 @@

@TestClassOrder(ClassOrderer.OrderAnnotation.class)
@Order(3)
class MySQLReadTwoTablesSameNameTest extends MySQLBaseIntegrationTest {
class ReadTwoTablesSameNameDiffSchemaTest extends MySQLBaseIntegrationTest {
@Test
@DisplayName("Test find all films - all columns - different schemas.")
void findUsersInTwoSchemas() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.homihq.db2rest.rest.mysql;

import com.adelean.inject.resources.junit.jupiter.GivenJsonResource;
import com.adelean.inject.resources.junit.jupiter.TestWithResources;
import com.adelean.inject.resources.junit.jupiter.WithJacksonMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.homihq.db2rest.MySQLBaseIntegrationTest;
import org.junit.jupiter.api.*;

import java.util.Map;

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.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@TestClassOrder(ClassOrderer.OrderAnnotation.class)
@Order(71)
@TestWithResources
class UpdateTwoTablesSameNameDiffSchemaTest extends MySQLBaseIntegrationTest {

@WithJacksonMapper
ObjectMapper objectMapper = new ObjectMapper()
.registerModule(new JavaTimeModule());


@GivenJsonResource("/testdata/UPDATE_EMPLOYEE_REQUEST.json")
Map<String,Object> UPDATE_EMPLOYEE_REQUEST;

@Test
@DisplayName("Update employee diff schema")
void updateEmployee() throws Exception {

mockMvc.perform(patch("/employee")
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.header("Content-Profile", "sakila")
.param("filter", "emp_id==1")
.content(objectMapper.writeValueAsString(UPDATE_EMPLOYEE_REQUEST))
)
.andExpect(status().isOk())
.andExpect(jsonPath("$.rows", equalTo(1)))
//.andDo(print())
.andDo(document("mysql-update-emp-sakila"));


mockMvc.perform(patch("/employee")
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.header("Content-Profile", "wakila")
.param("filter", "emp_id==1")
.content(objectMapper.writeValueAsString(UPDATE_EMPLOYEE_REQUEST))
)
.andExpect(status().isOk())
.andExpect(jsonPath("$.rows", equalTo(1)))
//.andDo(print())
.andDo(document("mysql-update-emp-wakila"));
}




}
4 changes: 2 additions & 2 deletions src/test/resources/mysql/mysql-sakila-data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ values
(7, 7, 'Ivan', 'Levchenko', '[email protected]','878511311054');


Insert into employee (emp_id, first_name, last_name, create_date, is_active) values (1, 'Ivan', 'Levchenko', curdate(), 1);
Insert into employee (emp_id, first_name, last_name, create_date, is_active) values (2, 'Roger', 'Federer', curdate(), 1);
Insert into employee (first_name, last_name) values ('Ivan', 'Levchenko');
Insert into employee (first_name, last_name) values ('Roger', 'Federer');


-- language
Expand Down
11 changes: 6 additions & 5 deletions src/test/resources/mysql/mysql-sakila.sql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions src/test/resources/mysql/mysql-wakila-all.sql
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,14 @@ values
--

CREATE TABLE employee (
emp_id int(10) UNSIGNED NOT NULL,
first_name varchar(100) NOT NULL,
last_name varchar(150) NOT NULL,
create_date datetime NOT NULL,
is_active tinyint(1) NOT NULL
emp_id int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
first_name varchar(100) NOT NULL,
last_name varchar(150) NOT NULL,
create_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
is_active tinyint(1) DEFAULT 1,
PRIMARY KEY (emp_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;


Insert into employee (emp_id, first_name, last_name, create_date, is_active) values (1, 'Ivan', 'Levchenko', curdate(), 1);
Insert into employee (emp_id, first_name, last_name, create_date, is_active) values (2, 'Roger', 'Federer', curdate(), 1);
Insert into employee (first_name, last_name) values ('Ivan', 'Levchenko');
Insert into employee (first_name, last_name) values ('Roger', 'Federer');
6 changes: 6 additions & 0 deletions src/test/resources/testdata/CREATE_EMP_REQUEST.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"emp_id" : "3",
"first_name" : "Johny" ,
"last_name" : "Liver"

}
3 changes: 3 additions & 0 deletions src/test/resources/testdata/UPDATE_EMPLOYEE_REQUEST.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"last_name": "Santana"
}

0 comments on commit ce0b348

Please sign in to comment.