From 00e454b20b09b85f218893b93cf5b5c8edcb9681 Mon Sep 17 00:00:00 2001 From: Rebwon Date: Fri, 15 Oct 2021 21:50:06 +0900 Subject: [PATCH] Separate Application Profiles Using Local MySQL DB. --- .gitignore | 2 + build.gradle | 5 +- .../AccountModuleConfiguration.java | 2 +- .../configuration/DatabaseConfiguration.java | 12 -- src/main/resources/application-local.yml | 11 ++ src/main/resources/application.yml | 4 - src/main/resources/schema-mysql.sql | 109 ++++++++++++++++++ .../presentation/AccountCommandApiTests.java | 2 + .../presentation/AccountQueryApiTest.java | 2 + .../presentation/ArticleCommandApiTests.java | 2 + .../presentation/CommentCommandApiTest.java | 2 + .../integration/AbstractIntegrationTests.java | 21 ++++ src/test/resources/application-test.yml | 5 + src/{main => test}/resources/sql/ddl.sql | 5 +- src/{main => test}/resources/sql/dml.sql | 17 +-- 15 files changed, 168 insertions(+), 33 deletions(-) create mode 100644 src/main/resources/application-local.yml create mode 100644 src/main/resources/schema-mysql.sql create mode 100644 src/test/resources/application-test.yml rename src/{main => test}/resources/sql/ddl.sql (98%) rename src/{main => test}/resources/sql/dml.sql (78%) diff --git a/.gitignore b/.gitignore index bee2ab5..327bf4c 100644 --- a/.gitignore +++ b/.gitignore @@ -38,3 +38,5 @@ out/ /logs /logs/*.log + +/src/main/resources/application-prod.yml diff --git a/build.gradle b/build.gradle index 4c6cdae..e9c9e51 100644 --- a/build.gradle +++ b/build.gradle @@ -42,7 +42,8 @@ dependencies { annotationProcessor 'org.projectlombok:lombok' implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.2.0' - runtimeOnly 'com.h2database:h2' + testImplementation 'com.h2database:h2' + runtimeOnly 'mysql:mysql-connector-java' implementation 'com.lmax:disruptor:3.4.4' implementation 'org.springframework.boot:spring-boot-starter-log4j2' @@ -60,7 +61,7 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-redis' implementation 'org.springframework.session:spring-session-data-redis' - testImplementation 'io.github.javaunit:autoparams:0.2.12' + testImplementation 'io.github.javaunit:autoparams:0.3.0' testImplementation 'com.tngtech.archunit:archunit-junit5:0.20.1' testImplementation 'org.springframework.boot:spring-boot-starter-test' diff --git a/src/main/java/com/yam/app/account/infrastructure/AccountModuleConfiguration.java b/src/main/java/com/yam/app/account/infrastructure/AccountModuleConfiguration.java index c0a9953..e9afc50 100644 --- a/src/main/java/com/yam/app/account/infrastructure/AccountModuleConfiguration.java +++ b/src/main/java/com/yam/app/account/infrastructure/AccountModuleConfiguration.java @@ -21,7 +21,7 @@ import org.springframework.security.crypto.password.PasswordEncoder; public class AccountModuleConfiguration { @Bean - @Profile("prod") + @Profile(value = {"local", "prod"}) public MailDispatcher mailDispatcher(JavaMailSender javaMailSender) { return new SmtpMailDispatcher(javaMailSender); } diff --git a/src/main/java/com/yam/app/common/configuration/DatabaseConfiguration.java b/src/main/java/com/yam/app/common/configuration/DatabaseConfiguration.java index dbd7f0c..49ade3e 100644 --- a/src/main/java/com/yam/app/common/configuration/DatabaseConfiguration.java +++ b/src/main/java/com/yam/app/common/configuration/DatabaseConfiguration.java @@ -6,22 +6,10 @@ import org.mybatis.spring.SqlSessionFactoryBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; -import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; -import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; @Configuration public class DatabaseConfiguration { - @Bean - public DataSource dataSource() { - return new EmbeddedDatabaseBuilder() - .setType(EmbeddedDatabaseType.H2) - .setName("testdb;mode=MySQL") - .addScript("classpath:sql/ddl.sql") - .addScript("classpath:sql/dml.sql") - .build(); - } - @Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { var factoryBean = new SqlSessionFactoryBean(); diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml new file mode 100644 index 0000000..eef3d1a --- /dev/null +++ b/src/main/resources/application-local.yml @@ -0,0 +1,11 @@ +spring: + datasource: + url: jdbc:mysql://localhost:3307/yam?useSSL=false&serverTimezone=UTC&useCursors=false&sendStringParametersAsUnicode=false&characterEncoding=UTF8 + username: root + password: pass + driver-class-name: com.mysql.cj.jdbc.Driver + + redis: + host: localhost + password: + port: 6379 diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index a0fce92..f379d0c 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -12,10 +12,6 @@ spring: enable: true session: store-type: redis - redis: - host: localhost - password: - port: 6379 app: mail: diff --git a/src/main/resources/schema-mysql.sql b/src/main/resources/schema-mysql.sql new file mode 100644 index 0000000..ab5865b --- /dev/null +++ b/src/main/resources/schema-mysql.sql @@ -0,0 +1,109 @@ +create table account +( + id bigint not null auto_increment, + email varchar(255) not null, + email_check_token varchar(255), + email_check_token_generated_at timestamp, + email_verified boolean not null, + joined_at timestamp, + last_modified_at timestamp, + role varchar(255) not null, + password varchar(255) not null, + withdraw boolean not null, + withdrawal_at timestamp, + member_id bigint, + status varchar(255) not null, + primary key (id) +) engine=InnoDB; + +create table member +( + id bigint not null auto_increment, + nickname varchar(255) not null, + image varchar(255) not null, + status varchar(255) not null, + primary key (id) +) engine=InnoDB; + +create table article +( + id bigint not null auto_increment, + content varchar(255) not null, + created_at timestamp, + image varchar(255) not null, + modified_at timestamp, + status varchar(255) not null, + title varchar(255) not null, + member_id bigint, + primary key (id) +) engine=InnoDB; + +create table article_likes +( + id bigint not null auto_increment, + article_id bigint, + member_id bigint, + primary key (id) +) engine=InnoDB; + +create table article_tag +( + id bigint not null auto_increment, + article_id bigint not null, + tag_id bigint not null, + primary key (id) +) engine=InnoDB; + +create table comment +( + id bigint not null auto_increment, + content varchar(255) not null, + created_at timestamp, + modified_at timestamp, + status varchar(255), + article_id bigint, + member_id bigint, + primary key (id) +) engine=InnoDB; + +create table comment_likes +( + id bigint not null auto_increment, + comment_id bigint, + member_id bigint, + primary key (id) +) engine=InnoDB; + +create table tag +( + id bigint not null auto_increment, + name varchar(255) not null, + primary key (id) +) engine=InnoDB; + +alter table account + add constraint UK_q0uja26qgu1atulenwup9rxyr unique (email); +alter table account + add constraint FKr5j0huynd7nsv1s7e9vb8qvwo foreign key (member_id) references member; +alter table tag + add constraint UK_1wdpsed5kna2y38hnbgrnhi5b unique (name); +alter table article + add constraint UK_571gx7oqo5xpmgocegaidlcu9 unique (title); +alter table article + add constraint FK6l9vkfd5ixw8o8kph5rj1k7gu foreign key (member_id) references member; +alter table article_likes + add constraint FK1wt0ww82gfxkuxw3ghxmp55xy foreign key (article_id) references article; +alter table article_likes + add constraint FKkipxs7p8nrjd4537f3k8rexh5 foreign key (member_id) references member; +alter table article_tag + add constraint FKenqeees0y8hkm7x1p1ittuuye foreign key (article_id) references article; +alter table article_tag + add constraint FKesqp7s9jj2wumlnhssbme5ule foreign key (tag_id) references tag; +alter table comment + add constraint FK5yx0uphgjc6ik6hb82kkw501y foreign key (article_id) references article; +alter table comment + add constraint FKmrrrpi513ssu63i2783jyiv9m foreign key (member_id) references member; +alter table comment_likes + add constraint FKd0epu3dcjc57pwe7lt5jgfqsi foreign key (comment_id) references comment; +alter table comment_likes + add constraint FK7mxs5jtimpj71miv2r0fp6r8p foreign key (member_id) references member; diff --git a/src/test/java/com/yam/app/account/presentation/AccountCommandApiTests.java b/src/test/java/com/yam/app/account/presentation/AccountCommandApiTests.java index 5c88e40..6b0cd37 100644 --- a/src/test/java/com/yam/app/account/presentation/AccountCommandApiTests.java +++ b/src/test/java/com/yam/app/account/presentation/AccountCommandApiTests.java @@ -27,11 +27,13 @@ import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; import org.springframework.mock.web.MockHttpSession; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultActions; @DisplayName("Account Command HTTP API") @WebMvcTest(AccountCommandApi.class) +@ActiveProfiles("test") final class AccountCommandApiTests { @Autowired diff --git a/src/test/java/com/yam/app/account/presentation/AccountQueryApiTest.java b/src/test/java/com/yam/app/account/presentation/AccountQueryApiTest.java index 07a3252..86c5b63 100644 --- a/src/test/java/com/yam/app/account/presentation/AccountQueryApiTest.java +++ b/src/test/java/com/yam/app/account/presentation/AccountQueryApiTest.java @@ -14,10 +14,12 @@ import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; import org.springframework.mock.web.MockHttpSession; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; @DisplayName("Account Query HTTP API") @WebMvcTest(AccountQueryApi.class) +@ActiveProfiles("test") class AccountQueryApiTest { @Autowired diff --git a/src/test/java/com/yam/app/article/presentation/ArticleCommandApiTests.java b/src/test/java/com/yam/app/article/presentation/ArticleCommandApiTests.java index 2894a45..869ba8c 100644 --- a/src/test/java/com/yam/app/article/presentation/ArticleCommandApiTests.java +++ b/src/test/java/com/yam/app/article/presentation/ArticleCommandApiTests.java @@ -18,11 +18,13 @@ import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; import org.springframework.mock.web.MockHttpSession; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultActions; @DisplayName("Article Command HTTP API") @WebMvcTest(ArticleCommandApi.class) +@ActiveProfiles("test") final class ArticleCommandApiTests { @Autowired diff --git a/src/test/java/com/yam/app/comment/presentation/CommentCommandApiTest.java b/src/test/java/com/yam/app/comment/presentation/CommentCommandApiTest.java index b843465..d7637f8 100644 --- a/src/test/java/com/yam/app/comment/presentation/CommentCommandApiTest.java +++ b/src/test/java/com/yam/app/comment/presentation/CommentCommandApiTest.java @@ -25,10 +25,12 @@ import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; import org.springframework.mock.web.MockHttpSession; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; @DisplayName("Comment Command HTTP API") @WebMvcTest(CommentCommandApi.class) +@ActiveProfiles("test") final class CommentCommandApiTest { @Autowired diff --git a/src/test/java/com/yam/app/integration/AbstractIntegrationTests.java b/src/test/java/com/yam/app/integration/AbstractIntegrationTests.java index 0ed699a..6dade5e 100644 --- a/src/test/java/com/yam/app/integration/AbstractIntegrationTests.java +++ b/src/test/java/com/yam/app/integration/AbstractIntegrationTests.java @@ -3,10 +3,16 @@ package com.yam.app.integration; import static org.springframework.test.web.servlet.setup.SharedHttpSessionConfigurer.sharedHttpSession; import com.fasterxml.jackson.databind.ObjectMapper; +import javax.sql.DataSource; import org.junit.jupiter.api.BeforeEach; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; @@ -32,4 +38,19 @@ abstract class AbstractIntegrationTests { .apply(sharedHttpSession()) .build(); } + + @Configuration + public static class TestConfiguration { + + @Bean + @Profile("test") + public DataSource dataSource() { + return new EmbeddedDatabaseBuilder() + .setType(EmbeddedDatabaseType.H2) + .setName("testdb;mode=MySQL") + .addScript("classpath:sql/ddl.sql") + .addScript("classpath:sql/dml.sql") + .build(); + } + } } diff --git a/src/test/resources/application-test.yml b/src/test/resources/application-test.yml new file mode 100644 index 0000000..1841d05 --- /dev/null +++ b/src/test/resources/application-test.yml @@ -0,0 +1,5 @@ +spring: + redis: + host: localhost + password: + port: 6379 diff --git a/src/main/resources/sql/ddl.sql b/src/test/resources/sql/ddl.sql similarity index 98% rename from src/main/resources/sql/ddl.sql rename to src/test/resources/sql/ddl.sql index d439407..a5eca54 100644 --- a/src/main/resources/sql/ddl.sql +++ b/src/test/resources/sql/ddl.sql @@ -1,5 +1,4 @@ -create table account -( +create table account ( id bigint generated by default as identity, email varchar(255) not null, email_check_token varchar(255), @@ -16,7 +15,7 @@ create table account primary key (id) ); -create table member( +create table member ( id bigint generated by default as identity, nickname varchar(255) not null, image varchar(255) not null, diff --git a/src/main/resources/sql/dml.sql b/src/test/resources/sql/dml.sql similarity index 78% rename from src/main/resources/sql/dml.sql rename to src/test/resources/sql/dml.sql index f55369d..ab51be1 100644 --- a/src/main/resources/sql/dml.sql +++ b/src/test/resources/sql/dml.sql @@ -1,6 +1,7 @@ insert into member(nickname, image, status) values ('rebwon', 'temp.png', 'ALIVE'), - ('loginCheck', 'temp.png', 'ALIVE'); + ('loginCheck', 'temp.png', 'ALIVE'), + ('comment', 'temp.png', 'ALIVE'); insert into account(email, email_check_token, email_check_token_generated_at, email_verified, joined_at, last_modified_at, password, withdraw, role, status) @@ -14,7 +15,10 @@ values ('rebwon@gmail.com', 'emailchecktoken1', now(), true, now(), now(), false, 'DEFAULT', 1, 'ALIVE'), ('loginCheck@gmail.com', 'emailchecktoken1', now(), true, now(), now(), '$2a$10$EqbMbYB0vcZnuA5CClqa9uiLDnjA6pCjxn208ZchzA2q3ofqnkhcq', - false, 'DEFAULT', 2, 'ALIVE'); + false, 'DEFAULT', 2, 'ALIVE'), + ('comment@gmail.com', 'emailchecktoken1', now(), true, now(), now(), + '$2a$10$EqbMbYB0vcZnuA5CClqa9uiLDnjA6pCjxn208ZchzA2q3ofqnkhcq', + false, 'DEFAULT', 3, 'ALIVE'); insert into article(title, content, image, status, created_at, modified_at, member_id) values ('sample-title', 'sample-content', 'sample.png', 'ALIVE', now(), now(), 1); @@ -29,14 +33,5 @@ values (1, 1), (1, 2), (1, 3); -insert into member(nickname, image, status) -values ('comment', 'temp.png', 'ALIVE'); - -insert into account(email, email_check_token, email_check_token_generated_at, email_verified, - joined_at, last_modified_at, password, withdraw, role, member_id, status) -values ('comment@gmail.com', 'emailchecktoken1', now(), true, now(), now(), - '$2a$10$EqbMbYB0vcZnuA5CClqa9uiLDnjA6pCjxn208ZchzA2q3ofqnkhcq', - false, 'DEFAULT', 3, 'ALIVE'); - INSERT INTO comment(content, created_at, modified_at, status, article_id, member_id) VALUES ('sample content1', now(), now(), 'ALIVE', 1, 3);