diff --git a/.github/workflow/ci.yml b/.github/workflow/ci.yml deleted file mode 100644 index e34c64d..0000000 --- a/.github/workflow/ci.yml +++ /dev/null @@ -1,29 +0,0 @@ -name: pr-test -on: pull_request - - -jobs: - pr-test: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@main - - - name: Set up JDK 21 - uses: actions/setup-java@main - with: - java-version: '21' - cache: 'gradle' - distribution: 'temurin' - - - name: Set up Gradle - uses: gradle/gradle-build-action@v3 - with: - gradle-version: 8.5 - - - name: Run Test - uses: gradle/gradle-build-action@v3 - with: - gradle-version: wrapper - arguments: test - - diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..5369e4d --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,53 @@ +name: pr-test +on: pull_request + +env: + SPRING_DATASOURCE_URL: ${{ secrets.SPRING_DATASOURCE_URL }} + DB_PASSWORD: root + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} + +jobs: + pr-test: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@main + + - name: Start Redis + uses: supercharge/redis-github-action@1.1.0 + with: + redis-version: 6 + + - name: Start Mysql Service + run: | + sudo /etc/init.d/mysql start + mysql -e 'CREATE DATABASE shoppingtest;' -uroot -proot + + - name: Set up JDK 21 + uses: actions/setup-java@main + with: + java-version: '21' + cache: 'gradle' + distribution: 'temurin' + + - name: Setup Gradle + uses: gradle/gradle-build-action@v3 + with: + gradle-version: 8.5 + + - name: Run Test + uses: gradle/gradle-build-action@v3 + with: + gradle-version: wrapper + arguments: test + + - name: send slack notification + uses: 8398a7/action-slack@v3 + with: + status: ${{ job.status }} + fields: repo,commit,author,ref,workflow,job,took + author_name: GitHub Actions + if: always() + + + diff --git a/.gitignore b/.gitignore index c2065bc..dd15cc0 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,6 @@ out/ ### VS Code ### .vscode/ + +/mysql_data +/config \ No newline at end of file diff --git a/src/main/java/com/kt/common/profile/LocalProfile.java b/src/main/java/com/kt/common/profile/LocalProfile.java index 9fd7be2..ff970b0 100644 --- a/src/main/java/com/kt/common/profile/LocalProfile.java +++ b/src/main/java/com/kt/common/profile/LocalProfile.java @@ -5,7 +5,7 @@ import org.springframework.context.annotation.Profile; -@Profile({"local", "test"}) +@Profile("local") @Retention(RetentionPolicy.RUNTIME) public @interface LocalProfile { } diff --git a/src/main/java/com/kt/common/profile/ProdProfile.java b/src/main/java/com/kt/common/profile/ProdProfile.java new file mode 100644 index 0000000..cda9167 --- /dev/null +++ b/src/main/java/com/kt/common/profile/ProdProfile.java @@ -0,0 +1,11 @@ +package com.kt.common.profile; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +import org.springframework.context.annotation.Profile; + +@Profile("prod") +@Retention(RetentionPolicy.RUNTIME) +public @interface ProdProfile { +} diff --git a/src/main/java/com/kt/common/profile/TestProfile.java b/src/main/java/com/kt/common/profile/TestProfile.java new file mode 100644 index 0000000..43013f5 --- /dev/null +++ b/src/main/java/com/kt/common/profile/TestProfile.java @@ -0,0 +1,11 @@ +package com.kt.common.profile; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +import org.springframework.context.annotation.Profile; + +@Profile("test") +@Retention(RetentionPolicy.RUNTIME) +public @interface TestProfile { +} diff --git a/src/main/java/com/kt/config/ActiveProfileConfig.java b/src/main/java/com/kt/config/ActiveProfileConfig.java index 1055f6c..2427d67 100644 --- a/src/main/java/com/kt/config/ActiveProfileConfig.java +++ b/src/main/java/com/kt/config/ActiveProfileConfig.java @@ -2,21 +2,23 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Profile; -import org.springframework.core.env.Environment; import lombok.RequiredArgsConstructor; -@Profile("dev") @Configuration @RequiredArgsConstructor public class ActiveProfileConfig { - private final Environment environment; + private final TestProperties testProperties; + + private final CommonProperties commonProperties; @Bean public String init() { - System.out.println("Active Profile : " + environment.getActiveProfiles()[0]); + System.out.println(commonProperties.commonValue()); + System.out.println(commonProperties.commonValueParam()); + System.out.println(testProperties.myValue()); + System.out.println(testProperties.myValueParam()); return ""; } } diff --git a/src/main/java/com/kt/config/CommonProperties.java b/src/main/java/com/kt/config/CommonProperties.java new file mode 100644 index 0000000..6e04435 --- /dev/null +++ b/src/main/java/com/kt/config/CommonProperties.java @@ -0,0 +1,10 @@ +package com.kt.config; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties(prefix = "common") +public record CommonProperties( + String commonValue, + String commonValueParam +) { +} diff --git a/src/main/java/com/kt/config/RedisConfiguration.java b/src/main/java/com/kt/config/RedisConfiguration.java index 18ef170..ab49a45 100644 --- a/src/main/java/com/kt/config/RedisConfiguration.java +++ b/src/main/java/com/kt/config/RedisConfiguration.java @@ -3,16 +3,23 @@ import org.redisson.Redisson; import org.redisson.api.RedissonClient; import org.redisson.config.Config; +import org.springframework.boot.autoconfigure.data.redis.RedisProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import lombok.RequiredArgsConstructor; + @Configuration +@RequiredArgsConstructor public class RedisConfiguration { + private final RedisProperties redisProperties; + @Bean public RedissonClient redisClient() { Config config = new Config(); - config.useSingleServer().setAddress("redis://localhost:6379"); + String redisUrl = String.format("redis://%s:%d", redisProperties.getHost(), redisProperties.getPort()); + config.useSingleServer().setAddress(redisUrl); return Redisson.create(config); } } diff --git a/src/main/java/com/kt/config/TestProperties.java b/src/main/java/com/kt/config/TestProperties.java new file mode 100644 index 0000000..471e095 --- /dev/null +++ b/src/main/java/com/kt/config/TestProperties.java @@ -0,0 +1,10 @@ +package com.kt.config; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties(prefix = "test") +public record TestProperties( + String myValue, + String myValueParam +) { +} diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-local.yml similarity index 83% rename from src/main/resources/application-dev.yml rename to src/main/resources/application-local.yml index 78da433..ddf1e5e 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-local.yml @@ -38,4 +38,12 @@ jwt: slack: bot-token: ${slack.token} - log-channel: ${slack.channel} \ No newline at end of file + log-channel: ${slack.channel} + +common: + common-value: this is common value + common-value-param: this is common value with param - ${COMMON_PARAM:공통} + +test: + my-value: this is test value in local + my-value-param: this is test value with param - ${TEST_PARAM:terry} \ No newline at end of file diff --git a/src/main/resources/application-test.yml b/src/main/resources/application-test.yml deleted file mode 100644 index b8f8a36..0000000 --- a/src/main/resources/application-test.yml +++ /dev/null @@ -1,32 +0,0 @@ -spring: - ### mysql 드라이버 설정 ### - datasource: - url: jdbc:mysql://localhost:3306/shopping-sehyeon - username: root - password: 1234 - driver-class-name: com.mysql.cj.jdbc.Driver - ### jpa 설정 ### - jpa: - hibernate: - # create = 테이블을 만들기만 - # update = 테이블 없으면 만들고, entity랑 다르면 수정 -> 삭제는 안해줌 - # validate = 테이블이랑 entity랑 맞는지 확인만 - # none = 아무것도 안함 - # create-drop = create랑 똑같은데, 애플리케이션 종료시점에 테이블 drop => 테스트코드에서만 쓰세요 - ddl-auto: update - properties: - hibernate: - format_sql: true # SQL문을 보기 좋게 출력 - # dialect: org.hibernate.dialect.MySQL5InnoDBDialect - jdbc: - time_zone: Asia/Seoul - show-sql: true - -jwt: - secret: ${kt.jwt.secret:kt-cloud-shopping-kt-cloud-shopping-kt-cloud-shopping-kt-cloud-shopping} - access-token-expiration: ${kt.jwt.access-token-expiration:3000000} #5분 - refresh-token-expiration: ${kt.jwt.refresh-token-expiration:43200000} #12시간 - -slack: - bot-token: ${slack.token} - log-channel: ${slack.channel} \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 78da433..dac9eed 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,35 +1,30 @@ spring: - ### h2 설정 ### - # h2: - # console: - # enabled: true - # datasource: - # url: jdbc:h2:~/test - # username: sa - # password: - # driver-class-name: org.h2.Driver - ### mysql 드라이버 설정 ### datasource: url: jdbc:mysql://localhost:3306/shopping-sehyeon username: root - password: 1234 + password: ${db.password:1234} driver-class-name: com.mysql.cj.jdbc.Driver - ### jpa 설정 ### jpa: hibernate: - # create = 테이블을 만들기만 - # update = 테이블 없으면 만들고, entity랑 다르면 수정 -> 삭제는 안해줌 - # validate = 테이블이랑 entity랑 맞는지 확인만 - # none = 아무것도 안함 - # create-drop = create랑 똑같은데, 애플리케이션 종료시점에 테이블 drop => 테스트코드에서만 쓰세요 - ddl-auto: update + #create = 테이블을 만들기만 + #update = 테이블 없으면 만들고 있지만 entity랑 다르면 수정 -> 삭제는 안해줌 + #validate = 테이블이랑 entity랑 맞는지 확인만 + #none = 아무것도 안함 + #create-drop = create랑 똑같은데 애플리케이션 종료시점에 테이블을 drop함 => 테스트코드에서만 쓰세요 + ddl-auto: create-drop properties: hibernate: - format_sql: true # SQL문을 보기 좋게 출력 - # dialect: org.hibernate.dialect.MySQL5InnoDBDialect + format_sql: true + dialect: org.hibernate.dialect.MySQLDialect jdbc: time_zone: Asia/Seoul + show_sql: true #로컬에서만 + # default_batch_fetch_size: 2 show-sql: true + data: + redis: + host: ${redis.host:localhost} + port: ${redis.port:6379} jwt: secret: ${kt.jwt.secret:kt-cloud-shopping-kt-cloud-shopping-kt-cloud-shopping-kt-cloud-shopping} @@ -38,4 +33,12 @@ jwt: slack: bot-token: ${slack.token} - log-channel: ${slack.channel} \ No newline at end of file + log-channel: ${slack.channel} + +common: + common-value: this is common value + common-value-param: this is common value with param - ${COMMON_PARAM} + +test: + my-value: this is test value in normal + my-value-param: this is test value with param - ${TEST_PARAM:default} \ No newline at end of file diff --git a/src/test/java/com/kt/ConfigTest.java b/src/test/java/com/kt/ConfigTest.java new file mode 100644 index 0000000..74d7e97 --- /dev/null +++ b/src/test/java/com/kt/ConfigTest.java @@ -0,0 +1,30 @@ +package com.kt; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; + +import com.kt.config.CommonProperties; +import com.kt.config.TestProperties; + +@ActiveProfiles("test") +@SpringBootTest +public class ConfigTest { + + @Autowired + CommonProperties commonProperties + + @Autowired + TestProperties testProperties; + + @Test + public void test() { + + System.out.println(commonProperties.commonValue()); + System.out.println(commonProperties.commonValueParam()); + + System.out.println(testProperties.myValue()); + System.out.println(testProperties.myValueParam()); + } +} diff --git a/src/test/java/com/kt/ShoppingApplicationTests.java b/src/test/java/com/kt/ShoppingApplicationTests.java index e0fd926..1bcec29 100644 --- a/src/test/java/com/kt/ShoppingApplicationTests.java +++ b/src/test/java/com/kt/ShoppingApplicationTests.java @@ -2,7 +2,9 @@ import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; +@ActiveProfiles("test") @SpringBootTest class ShoppingApplicationTests { diff --git a/src/test/java/com/kt/domain/product/ProductTest.java b/src/test/java/com/kt/domain/product/ProductTest.java index 506fdf1..65992e4 100644 --- a/src/test/java/com/kt/domain/product/ProductTest.java +++ b/src/test/java/com/kt/domain/product/ProductTest.java @@ -3,9 +3,11 @@ import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; +import org.springframework.test.context.ActiveProfiles; import com.kt.common.exception.CustomException; +@ActiveProfiles("test") class ProductTest { @Test diff --git a/src/test/java/com/kt/repository/ProductRepositoryTest.java b/src/test/java/com/kt/repository/ProductRepositoryTest.java index 6cbb368..1b15e67 100644 --- a/src/test/java/com/kt/repository/ProductRepositoryTest.java +++ b/src/test/java/com/kt/repository/ProductRepositoryTest.java @@ -8,6 +8,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicInteger; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -46,6 +47,7 @@ class ProductRepositoryTest { } @Test + @Disabled void 동시에_100명_주문() throws InterruptedException { var userList = new ArrayList(); for (int i = 0; i < 100; i++) { diff --git a/src/test/java/com/kt/service/order/OrderServiceTest.java b/src/test/java/com/kt/service/order/OrderServiceTest.java index 73d8338..1743ed2 100644 --- a/src/test/java/com/kt/service/order/OrderServiceTest.java +++ b/src/test/java/com/kt/service/order/OrderServiceTest.java @@ -9,6 +9,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicInteger; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -80,6 +81,7 @@ class OrderServiceTest { } @Test + @Disabled void 동시에_100명_주문() throws Exception { var repeatCount = 500; var userList = new ArrayList(); diff --git a/src/test/resources/application-test.yml b/src/test/resources/application-test.yml new file mode 100644 index 0000000..794632a --- /dev/null +++ b/src/test/resources/application-test.yml @@ -0,0 +1,5 @@ +spring: + datasource: + url: ${SPRING_DATASOURCE_URL:jdbc:mysql://localhost:3306/shoppingtest} + username: root + password: ${db.password:1234} \ No newline at end of file