Skip to content

Commit

Permalink
[OING-18] feat: 공통 모듈 구축하기 (#5)
Browse files Browse the repository at this point in the history
* chore: add common module

* chore: add common module to workflow

* feat: add common exception classes

* feat: alter DomainException to non abstract class

* test: add DomainException unit test

* test: add ErrorResponse unit test

* chore: fix workflow annotations
  • Loading branch information
CChuYong authored Nov 22, 2023
1 parent cbf86a9 commit 9984d50
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ on:
branches: [ 'dev' ]
paths:
- 'config/**' # Config 모듈 변경
- 'member/**' # Member 모듈 변뚶
- 'member/**' # Member 모듈 변경
- 'common/**' # Common 모듈 변경
- '.github/workflows/**' # 워크플로우와 관련된 파일이 변경된 경우
- 'build.gradle' # Parent Gradle 모듈 설정이 변경된 경우
- 'settings.gradle' # Parent Gradle 설정이 변경된 경우
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ on:
branches: [ 'main' ]
paths:
- 'config/**' # Config 모듈 변경
- 'member/**' # Member 모듈 변뚶
- 'member/**' # Member 모듈 변경
- 'common/**' # Common 모듈 변경
- '.github/workflows/**' # 워크플로우와 관련된 파일이 변경된 경우
- 'build.gradle' # Parent Gradle 모듈 설정이 변경된 경우
- 'settings.gradle' # Parent Gradle 설정이 변경된 경우
Expand Down
15 changes: 15 additions & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
repositories {
mavenCentral()
}

dependencies {

}

tasks.named('bootJar') {
enabled = false
}

tasks.named('jar') {
enabled = true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.oing.domain.exception;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public class DomainException extends RuntimeException {
private final ErrorCode errorCode;
}
22 changes: 22 additions & 0 deletions common/src/main/java/com/oing/domain/exception/ErrorCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.oing.domain.exception;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum ErrorCode {
/**
* Common Errors
*/
UNKNOWN_SERVER_ERROR("CM0001", "Unknown Server Error"),
INVALID_INPUT_VALUE("CM0002", "Invalid Input Value"),
/**
* Auth Related Errors
*/
AUTHENTICATION_FAILED("AU0001", "Authentication failed"),
AUTHORIZATION_FAILED("AU0002", "No Permission");

private final String code;
private final String message;
}
21 changes: 21 additions & 0 deletions common/src/main/java/com/oing/dto/response/ErrorResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.oing.dto.response;

import com.oing.domain.exception.ErrorCode;
import io.swagger.v3.oas.annotations.media.Schema;

@Schema(description = "에러 응답")
public record ErrorResponse(
@Schema(description = "에러 코드", example = "AU0001")
String code,

@Schema(description = "에러 메시지", example = "Authentication failed")
String message
) {
public static ErrorResponse of(String code, String message) {
return new ErrorResponse(code, message);
}

public static ErrorResponse of(ErrorCode errorCode) {
return new ErrorResponse(errorCode.getCode(), errorCode.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.oing.domain.exception;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;


import static org.junit.jupiter.api.Assertions.*;

public class DomainExceptionTest {
@DisplayName("DomainException 생성자 테스트")
@Test
void testConstructor() {
//given
ErrorCode errorCode = ErrorCode.AUTHENTICATION_FAILED;

//when
DomainException exception = new DomainException(errorCode);

//then
assertEquals(exception.getErrorCode(), errorCode);
}
}
38 changes: 38 additions & 0 deletions common/src/test/java/com/oing/dto/response/ErrorResponseTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.oing.dto.response;

import com.oing.domain.exception.ErrorCode;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class ErrorResponseTest {
@DisplayName("ErrorResponse.of(String, String) 메서드 테스트")
@Test
void testOfMethod() {
//given
String errorCode = "AU0001";
String errorMessage = "Authentication failed";

//when
ErrorResponse errorResponse = ErrorResponse.of(errorCode, errorMessage);

//then
assertEquals(errorResponse.code(), errorCode);
assertEquals(errorResponse.message(), errorMessage);
}

@DisplayName("ErrorResponse.of(ErrorCode) 메서드 테스트")
@Test
void testOfMethod2() {
//given
ErrorCode errorCode = ErrorCode.AUTHENTICATION_FAILED;

//when
ErrorResponse errorResponse = ErrorResponse.of(errorCode);

//then
assertEquals(errorResponse.code(), errorCode.getCode());
assertEquals(errorResponse.message(), errorCode.getMessage());
}
}
1 change: 1 addition & 0 deletions config/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ repositories {
}

dependencies {
implementation project(':common')
implementation project(':member')

implementation 'org.flywaydb:flyway-core'
Expand Down
2 changes: 1 addition & 1 deletion member/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ repositories {
}

dependencies {

implementation project(':common')
}

tasks.named('bootJar') {
Expand Down
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ rootProject.name = 'server'

include 'config'
include 'member'
include 'common'

0 comments on commit 9984d50

Please sign in to comment.