Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/main/java/org/sopt/makers/global/util/EnvUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
public final class EnvUtil {
private static final String SLACK_WEBHOOK_PREFIX = "SLACK_WEBHOOK_";
private static final String DISCORD_WEBHOOK_PREFIX = "DISCORD_WEBHOOK_";
private static final Dotenv dotenv = Dotenv.configure()
.directory("src/main/resources") // .env 파일 경로 지정
.load();
private static final Dotenv dotenv = Dotenv.configure().load();

/**
* 서비스 유형에 맞는 웹훅 URL 반환
Expand Down
16 changes: 8 additions & 8 deletions src/test/java/org/sopt/makers/global/util/EnvUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
@DisplayName("EnvUtil 테스트")
class EnvUtilTest {

private static final String ENV_FILE_PATH = "src/main/resources/.env";
private static final String ENV_FILE_PATH = "src/test/resources/.env";

@BeforeAll
static void setUp() throws IOException {
String content = """
SLACK_WEBHOOK_CREW_DEV_BE=https://hooks.slack.com/services/crew/dev/be
SLACK_WEBHOOK_APP_PROD_FE=https://hooks.slack.com/services/app/prod/fe
""";
Files.createDirectories(Paths.get("src/main/resources"));
Files.createDirectories(Paths.get("src/test/resources"));
Files.write(Paths.get(ENV_FILE_PATH), content.getBytes());
}

Expand All @@ -43,7 +43,7 @@ static void tearDown() throws IOException {
@Test
void testGetWebhookUrl_valid() {
String expectedUrl = "https://hooks.slack.com/services/crew/dev/be";
String actualUrl = EnvUtil.getWebhookUrl("slack", "crew", "dev", "be");
String actualUrl = TestEnvUtil.getWebhookUrl("slack", "crew", "dev", "be");

assertEquals(expectedUrl, actualUrl);
}
Expand All @@ -52,7 +52,7 @@ void testGetWebhookUrl_valid() {
@Test
void testGetWebhookUrl_notFound() {
WebhookUrlNotFoundException exception = assertThrows(WebhookUrlNotFoundException.class, () ->
EnvUtil.getWebhookUrl("slack", "crew", "prod", "be")
TestEnvUtil.getWebhookUrl("slack", "crew", "prod", "be")
);

assertTrue(exception.getMessage().contains("Webhook URL을 찾을 수 없습니다."));
Expand All @@ -62,7 +62,7 @@ void testGetWebhookUrl_notFound() {
@Test
void testGetWebhookUrl_unsupportedServiceType() {
assertThrows(UnsupportedServiceTypeException.class, () ->
EnvUtil.getWebhookUrl("telegram", "crew", "dev", "be")
TestEnvUtil.getWebhookUrl("telegram", "crew", "dev", "be")
);
}

Expand All @@ -71,7 +71,7 @@ void testGetWebhookUrl_unsupportedServiceType() {
@MethodSource("provideNullParameters")
void testGetWebhookUrl_withNullParameters_shouldThrow(String service, String team, String stage, String type) {
assertThrows(InvalidEnvParameterException.class, () ->
EnvUtil.getWebhookUrl(service, team, stage, type)
TestEnvUtil.getWebhookUrl(service, team, stage, type)
);
}

Expand All @@ -87,12 +87,12 @@ void testGetWebhookUrl_withNullParameters_shouldThrow(String service, String tea
})
void testGetWebhookUrl_caseInsensitive(String service, String team, String stage, String type) {
if (team.equalsIgnoreCase("app")) {
String actualUrl = EnvUtil.getWebhookUrl(service, team, stage, type);
String actualUrl = TestEnvUtil.getWebhookUrl(service, team, stage, type);
assertEquals("https://hooks.slack.com/services/app/prod/fe", actualUrl);
return;
}

String actualUrl = EnvUtil.getWebhookUrl(service, team, stage, type);
String actualUrl = TestEnvUtil.getWebhookUrl(service, team, stage, type);
assertEquals("https://hooks.slack.com/services/crew/dev/be", actualUrl);
}
private static Stream<Arguments> provideNullParameters() {
Expand Down
56 changes: 56 additions & 0 deletions src/test/java/org/sopt/makers/global/util/TestEnvUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.sopt.makers.global.util;

import org.sopt.makers.global.exception.message.ErrorMessage;
import org.sopt.makers.global.exception.unchecked.InvalidEnvParameterException;
import org.sopt.makers.global.exception.unchecked.UnsupportedServiceTypeException;
import org.sopt.makers.global.exception.unchecked.WebhookUrlNotFoundException;

import io.github.cdimascio.dotenv.Dotenv;

class TestEnvUtil {
private static final String SLACK_WEBHOOK_PREFIX = "SLACK_WEBHOOK_";
private static final String DISCORD_WEBHOOK_PREFIX = "DISCORD_WEBHOOK_";
private static final Dotenv dotenv = Dotenv.configure()
.directory("src/test/resources")
.load();

/**
* 서비스 유형에 맞는 웹훅 URL 반환
*
* @param service 서비스 유형 (slack, discord 등)
* @param team 팀 이름 (crew, app 등)
* @param stage 환경 (dev, prod)
* @param type 서버 유형 (be, fe)
* @return 웹훅 URL
* @throws UnsupportedServiceTypeException 지원하지 않는 서비스 유형인 경우
* @throws WebhookUrlNotFoundException 환경 변수를 찾을 수 없는 경우
*/
public static String getWebhookUrl(String service, String team, String stage, String type) {
if (service == null || team == null || stage == null || type == null) {
throw InvalidEnvParameterException.from(ErrorMessage.INVALID_ENV_PARAMETER);
}

String prefix = resolvePrefix(service.toLowerCase());
String envKey = String.format("%s%s_%s_%s",
prefix,
team.toUpperCase(),
stage.toUpperCase(),
type.toUpperCase());

String webhookUrl = dotenv.get(envKey);

if (webhookUrl == null || webhookUrl.isBlank()) {
throw new WebhookUrlNotFoundException(ErrorMessage.WEBHOOK_URL_NOT_FOUND);
}

return webhookUrl;
}

private static String resolvePrefix(String service) {
return switch (service) {
case "slack" -> SLACK_WEBHOOK_PREFIX;
case "discord" -> DISCORD_WEBHOOK_PREFIX;
default -> throw new UnsupportedServiceTypeException(ErrorMessage.UNSUPPORTED_SERVICE_TYPE);
};
}
}
Loading