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

feat: 종속성 변경 및 테스트 자동화 유틸 생성 #4

Merged
merged 8 commits into from
Jan 7, 2024
6 changes: 2 additions & 4 deletions .github/workflows/backend-test-when-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ name: backend-test
on:
pull_request:
branches:
- main
- develop
paths:
- backend/**
- .github/**
- dev
- release

permissions:
contents: read
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ out/

### VS Code ###
.vscode/

### REST Docs, QueryDSL ###
/src/main/resources/static/docs/
/src/main/generated
13 changes: 10 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ dependencies {

// db
runtimeOnly 'com.h2database:h2'
implementation 'mysql:mysql-connector-java:8.0.32'
runtimeOnly 'com.mysql:mysql-connector-j'

// flyway
implementation 'org.flywaydb:flyway-core'
Expand All @@ -63,16 +63,23 @@ dependencies {
runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-jackson', version: '0.11.2'

// jasypt
implementation "com.github.ulisesbocchio:jasypt-spring-boot-starter:3.0.3"
implementation 'com.github.ulisesbocchio:jasypt-spring-boot-starter:3.0.5'

// QueryDSL Implementation
implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jakarta"
annotationProcessor "jakarta.annotation:jakarta.annotation-api"
annotationProcessor "jakarta.persistence:jakarta.persistence-api"
}

def generated = 'src/main/generated'

sourceSets {
main.java.srcDirs += [generated]
}

tasks.withType(JavaCompile) {
options.annotationProcessorGeneratedSourcesDirectory = file(generated)
options.getGeneratedSourceOutputDirectory().set(file(generated))
}

clean.doLast {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/market/config/JasyptConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@Configuration
public class JasyptConfig {

@Value("${jasypt.encryptor.password")
@Value("${jasypt.encryptor.password}")
private String ENCRYPT_KEY;

@Bean(name = "jasyptEncryptor")
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/market/config/QuerydslConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.market.config;

import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QuerydslConfig {

@PersistenceContext
private EntityManager entityManager;

@Bean
public JPAQueryFactory jpaQueryFactory() {
return new JPAQueryFactory(entityManager);
}
}
11 changes: 7 additions & 4 deletions src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ spring:
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver

flyway:
enabled: true
baseline-on-migrate: true
enabled: false
baseline-on-migrate: false

jpa:
hibernate:
ddl-auto: create

properties:
hibernate:
format_sql: false
show_sql: false
format_sql: true
show_sql: true
43 changes: 43 additions & 0 deletions src/test/java/com/market/helper/IntegrationHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.market.helper;

import io.restassured.RestAssured;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.support.AbstractTestExecutionListener;

import java.util.List;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class IntegrationHelper extends AbstractTestExecutionListener {

@LocalServerPort
private int port;

@Autowired
private JdbcTemplate jdbcTemplate;

@BeforeEach
void init() {
RestAssured.port = this.port;
validateH2Database();
List<String> truncateAllTablesQuery = jdbcTemplate.queryForList("SELECT CONCAT('TRUNCATE TABLE ', TABLE_NAME, ';') AS q FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'PUBLIC'", String.class);
truncateAllTables(truncateAllTablesQuery);
}

private void validateH2Database() {
jdbcTemplate.queryForObject("SELECT H2VERSION() FROM DUAL", String.class);
}

private void truncateAllTables(List<String> truncateAllTablesQuery) {
jdbcTemplate.execute("SET FOREIGN_KEY_CHECKS = 0");

for (String truncateQuery : truncateAllTablesQuery) {
jdbcTemplate.execute(truncateQuery);
}

jdbcTemplate.execute("SET FOREIGN_KEY_CHECKS = 1");
}
}
43 changes: 43 additions & 0 deletions src/test/java/com/market/helper/JdbcTestHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.market.helper;

import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@JdbcTest(includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = Repository.class)})
public class JdbcTestHelper {

@Autowired
private JdbcTemplate jdbcTemplate;

@BeforeEach
void initDatabase() {
validateH2Database();
List<String> truncateQueries = getTruncateQueries();
truncateAllTables(truncateQueries);
}

private void validateH2Database() {
jdbcTemplate.queryForObject("SELECT H2VERSION() FROM DUAL", String.class);
}

private List<String> getTruncateQueries() {
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 truncateAllTables(List<String> truncateQueries) {
jdbcTemplate.execute("SET REFERENTIAL_INTEGRITY FALSE");

for (String truncateQuery : truncateQueries) {
jdbcTemplate.execute(truncateQuery);
}

jdbcTemplate.execute("SET REFERENTIAL_INTEGRITY TRUE");
}
}
11 changes: 11 additions & 0 deletions src/test/java/com/market/helper/MockBeanInjection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.market.helper;

import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.jpa.mapping.JpaMetamodelMappingContext;

@MockBean(JpaMetamodelMappingContext.class)
public class MockBeanInjection {

// @MockBean
// protected Example example;
}
20 changes: 20 additions & 0 deletions src/test/java/com/market/helper/RestDocsHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.market.helper;

import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler;
import org.springframework.restdocs.snippet.Snippet;

import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint;

public class RestDocsHelper {

public static RestDocumentationResultHandler customDocument(String identifier, Snippet... snippets) {
return document("{class-name}/" + identifier,
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint()),
snippets
);
}
}