Skip to content

Commit

Permalink
Merge pull request #85 from Team-INSERT/test/#79
Browse files Browse the repository at this point in the history
테스트 코드 작성 환경 설정
  • Loading branch information
NameIsUser06 authored Apr 16, 2024
2 parents 3da0c1f + 91d1f14 commit 55dadf8
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -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("[email protected]")
.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("[email protected]")
.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);
}
}
Original file line number Diff line number Diff line change
@@ -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 {
}
34 changes: 34 additions & 0 deletions src/test/java/com/project/bumawiki/global/truncate/Truncate.java
Original file line number Diff line number Diff line change
@@ -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<String> truncateQueries = getTruncateQueries(jdbcTemplate);
execute(jdbcTemplate, "SET REFERENTIAL_INTEGRITY FALSE");
truncateTables(jdbcTemplate, truncateQueries);
execute(jdbcTemplate, "SET REFERENTIAL_INTEGRITY TRUE");
}

private List<String> 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<String> truncateQueries) {
truncateQueries.forEach(v -> execute(jdbcTemplate, v));
}

private void execute(final JdbcTemplate jdbcTemplate, final String query) {
jdbcTemplate.execute(query);
}
}
30 changes: 15 additions & 15 deletions src/test/resources/application.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -14,7 +14,7 @@ spring:

jpa:
hibernate:
ddl-auto: create-drop
ddl-auto: create
database-platform: org.hibernate.dialect.H2Dialect
generate-ddl: true

Expand All @@ -23,6 +23,7 @@ spring:
org:
hibernate:
SQL: debug
show-sql: true

servlet:
multipart:
Expand All @@ -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

0 comments on commit 55dadf8

Please sign in to comment.