diff --git a/src/test/java/com/project/bumawiki/domain/docs/service/CommandDocsServiceTest.java b/src/test/java/com/project/bumawiki/domain/docs/service/CommandDocsServiceTest.java new file mode 100644 index 00000000..e2618a65 --- /dev/null +++ b/src/test/java/com/project/bumawiki/domain/docs/service/CommandDocsServiceTest.java @@ -0,0 +1,83 @@ +package com.project.bumawiki.domain.docs.service; + +import static org.assertj.core.api.AssertionsForClassTypes.*; +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import com.project.bumawiki.domain.docs.domain.Docs; +import com.project.bumawiki.domain.docs.domain.VersionDocs; +import com.project.bumawiki.domain.docs.domain.repository.DocsRepository; +import com.project.bumawiki.domain.docs.domain.repository.VersionDocsRepository; +import com.project.bumawiki.domain.docs.domain.type.DocsType; +import com.project.bumawiki.domain.user.domain.User; +import com.project.bumawiki.domain.user.domain.authority.Authority; +import com.project.bumawiki.domain.user.domain.repository.UserRepository; +import com.project.bumawiki.global.annotation.ServiceTest; + +@ServiceTest +class CommandDocsServiceTest { + + @Autowired + private UserRepository userRepository; + @Autowired + private CommandDocsService commandDocsService; + @Autowired + private DocsRepository docsRepository; + @Autowired + private VersionDocsRepository versionDocsRepository; + + @Test + void 문서_생성하기() { + // given + User user = User.builder() + .email("2022038@bssm.hs.kr") + .name("마현우") + .enroll(2022) + .nickName("마현우") + .authority(Authority.USER) + .build(); + userRepository.save(user); + + String title = "제목"; + String contents = "본문"; + int enroll = 2024; + Docs docs = new Docs(title, enroll, DocsType.ACCIDENT); + + // when + commandDocsService.create(docs, user, contents); + + // then + assertThat(docsRepository.findByTitle(title).get().getTitle()).isEqualTo(title); + assertThat(versionDocsRepository.findFirstByDocsOrderByVersionDesc(docs).getContents()).isEqualTo(contents); + } + + @Test + void 문서_업데이트하기() { + // given + User user = User.builder() + .email("2022038@bssm.hs.kr") + .name("마현우") + .enroll(2022) + .nickName("마현우") + .authority(Authority.USER) + .build(); + userRepository.save(user); + + String title = "제목"; + String contents = "본문"; + String updatedContents = "본문본문"; + int enroll = 2024; + Docs docs = new Docs(title, enroll, DocsType.ACCIDENT); + commandDocsService.create(docs, user, contents); + + // when + commandDocsService.update(user, title, updatedContents, 0); + + // then + VersionDocs versionDocs = versionDocsRepository.findFirstByDocsOrderByVersionDesc(docs); + assertThat(versionDocs.getContents()).isEqualTo(updatedContents); + assertThat(versionDocs.getVersion()).isEqualTo(1); + } +} \ No newline at end of file diff --git a/src/test/java/com/project/bumawiki/global/annotation/ServiceTest.java b/src/test/java/com/project/bumawiki/global/annotation/ServiceTest.java new file mode 100644 index 00000000..c862aa3b --- /dev/null +++ b/src/test/java/com/project/bumawiki/global/annotation/ServiceTest.java @@ -0,0 +1,20 @@ +package com.project.bumawiki.global.annotation; + +import org.junit.jupiter.api.DisplayNameGeneration; +import org.junit.jupiter.api.DisplayNameGenerator; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.transaction.annotation.Transactional; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.*; +import static java.lang.annotation.RetentionPolicy.*; + +@Target(TYPE) +@Retention(RUNTIME) +@Transactional +@SpringBootTest +@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) +public @interface ServiceTest { +} diff --git a/src/test/java/com/project/bumawiki/global/truncate/Truncate.java b/src/test/java/com/project/bumawiki/global/truncate/Truncate.java new file mode 100644 index 00000000..7f978eec --- /dev/null +++ b/src/test/java/com/project/bumawiki/global/truncate/Truncate.java @@ -0,0 +1,34 @@ +package com.project.bumawiki.global.truncate; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +@Component +public class Truncate { + @Autowired + private JdbcTemplate jdbcTemplate; + + @Transactional + public void beforeEach() { + final List truncateQueries = getTruncateQueries(jdbcTemplate); + execute(jdbcTemplate, "SET REFERENTIAL_INTEGRITY FALSE"); + truncateTables(jdbcTemplate, truncateQueries); + execute(jdbcTemplate, "SET REFERENTIAL_INTEGRITY TRUE"); + } + + private List getTruncateQueries(final JdbcTemplate jdbcTemplate) { + return jdbcTemplate.queryForList("SELECT Concat('TRUNCATE TABLE ', TABLE_NAME, ' RESTART IDENTITY;') AS q FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'PUBLIC'", String.class); + } + + private void truncateTables(final JdbcTemplate jdbcTemplate, final List truncateQueries) { + truncateQueries.forEach(v -> execute(jdbcTemplate, v)); + } + + private void execute(final JdbcTemplate jdbcTemplate, final String query) { + jdbcTemplate.execute(query); + } +} diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index cf5d1250..a9aa1967 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -1,6 +1,6 @@ spring: datasource: - url: jdbc:h2:mem:test + url: jdbc:h2:mem:test;MODE=MYSQL;NON_KEYWORDS=USER driverClassName: org.h2.Driver username: sa password: @@ -14,7 +14,7 @@ spring: jpa: hibernate: - ddl-auto: create-drop + ddl-auto: create database-platform: org.hibernate.dialect.H2Dialect generate-ddl: true @@ -23,6 +23,7 @@ spring: org: hibernate: SQL: debug + show-sql: true servlet: multipart: @@ -35,25 +36,24 @@ decorator: p6spy: enable-logging: true -auth: - jwt: - header: Authorization - secret: whfioydwfgiyodsgfionuasnfiuodsoiweqngfbkcjbwbdgfxhghfxjyuafgxasdfxyuyxfgqweiyrxftyqoifxyqiqwoixfniwoeqxfeiwunfxgiweuhfwqihwkefxhiwhfiuxnewhnfouxw - accessExp: 1800 - refreshExp: 2592000 - prefix: Bearer +jwt: + header: Authorization + secret: whfioydwfgiyodsgfionuasnfiuodsoiweqngfbkcjbwbdgfxhghfxjyuafgxasdfxyuyxfgqweiyrxftyqoifxyqiqwoixfniwoeqxfeiwunfxgiweuhfwqihwkefxhiwhfiuxnewhnfouxw + accessExp: 1800 + refreshExp: 2592000 + prefix: Bearer bsm: - client-id: ${BSM_CLIENT_ID} - secret-key: ${BSM_SECRET_KEY} - redirect-url: ${BSM_REDIRECT_URL} + client-id: test-bsm-client-id + secret-key: test-bsm-client-secret + redirect-url: test-redirect-uri #S3 aws: s3: - bucket: ${S3_BUCKET} - access-key: ${S3_ACCESSKEY} - secret-key: ${S3_SECRET} + bucket: test-bucket + access-key: test-access-key + secret-key: test-secret-key server: port: 8080 \ No newline at end of file