Skip to content

Commit dc7fc2a

Browse files
authored
Merge pull request #8 from sopt-makers/bug/#7
fix: .env 파일을 로드하도록 수정 완료
2 parents ed577ee + 17ea94d commit dc7fc2a

File tree

3 files changed

+65
-11
lines changed

3 files changed

+65
-11
lines changed

src/main/java/org/sopt/makers/global/util/EnvUtil.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
public final class EnvUtil {
1616
private static final String SLACK_WEBHOOK_PREFIX = "SLACK_WEBHOOK_";
1717
private static final String DISCORD_WEBHOOK_PREFIX = "DISCORD_WEBHOOK_";
18-
private static final Dotenv dotenv = Dotenv.configure()
19-
.directory("src/main/resources") // .env 파일 경로 지정
20-
.load();
18+
private static final Dotenv dotenv = Dotenv.configure().load();
2119

2220
/**
2321
* 서비스 유형에 맞는 웹훅 URL 반환

src/test/java/org/sopt/makers/global/util/EnvUtilTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@
2222
@DisplayName("EnvUtil 테스트")
2323
class EnvUtilTest {
2424

25-
private static final String ENV_FILE_PATH = "src/main/resources/.env";
25+
private static final String ENV_FILE_PATH = "src/test/resources/.env";
2626

2727
@BeforeAll
2828
static void setUp() throws IOException {
2929
String content = """
3030
SLACK_WEBHOOK_CREW_DEV_BE=https://hooks.slack.com/services/crew/dev/be
3131
SLACK_WEBHOOK_APP_PROD_FE=https://hooks.slack.com/services/app/prod/fe
3232
""";
33-
Files.createDirectories(Paths.get("src/main/resources"));
33+
Files.createDirectories(Paths.get("src/test/resources"));
3434
Files.write(Paths.get(ENV_FILE_PATH), content.getBytes());
3535
}
3636

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

4848
assertEquals(expectedUrl, actualUrl);
4949
}
@@ -52,7 +52,7 @@ void testGetWebhookUrl_valid() {
5252
@Test
5353
void testGetWebhookUrl_notFound() {
5454
WebhookUrlNotFoundException exception = assertThrows(WebhookUrlNotFoundException.class, () ->
55-
EnvUtil.getWebhookUrl("slack", "crew", "prod", "be")
55+
TestEnvUtil.getWebhookUrl("slack", "crew", "prod", "be")
5656
);
5757

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

@@ -71,7 +71,7 @@ void testGetWebhookUrl_unsupportedServiceType() {
7171
@MethodSource("provideNullParameters")
7272
void testGetWebhookUrl_withNullParameters_shouldThrow(String service, String team, String stage, String type) {
7373
assertThrows(InvalidEnvParameterException.class, () ->
74-
EnvUtil.getWebhookUrl(service, team, stage, type)
74+
TestEnvUtil.getWebhookUrl(service, team, stage, type)
7575
);
7676
}
7777

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

95-
String actualUrl = EnvUtil.getWebhookUrl(service, team, stage, type);
95+
String actualUrl = TestEnvUtil.getWebhookUrl(service, team, stage, type);
9696
assertEquals("https://hooks.slack.com/services/crew/dev/be", actualUrl);
9797
}
9898
private static Stream<Arguments> provideNullParameters() {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.sopt.makers.global.util;
2+
3+
import org.sopt.makers.global.exception.message.ErrorMessage;
4+
import org.sopt.makers.global.exception.unchecked.InvalidEnvParameterException;
5+
import org.sopt.makers.global.exception.unchecked.UnsupportedServiceTypeException;
6+
import org.sopt.makers.global.exception.unchecked.WebhookUrlNotFoundException;
7+
8+
import io.github.cdimascio.dotenv.Dotenv;
9+
10+
class TestEnvUtil {
11+
private static final String SLACK_WEBHOOK_PREFIX = "SLACK_WEBHOOK_";
12+
private static final String DISCORD_WEBHOOK_PREFIX = "DISCORD_WEBHOOK_";
13+
private static final Dotenv dotenv = Dotenv.configure()
14+
.directory("src/test/resources")
15+
.load();
16+
17+
/**
18+
* 서비스 유형에 맞는 웹훅 URL 반환
19+
*
20+
* @param service 서비스 유형 (slack, discord 등)
21+
* @param team 팀 이름 (crew, app 등)
22+
* @param stage 환경 (dev, prod)
23+
* @param type 서버 유형 (be, fe)
24+
* @return 웹훅 URL
25+
* @throws UnsupportedServiceTypeException 지원하지 않는 서비스 유형인 경우
26+
* @throws WebhookUrlNotFoundException 환경 변수를 찾을 수 없는 경우
27+
*/
28+
public static String getWebhookUrl(String service, String team, String stage, String type) {
29+
if (service == null || team == null || stage == null || type == null) {
30+
throw InvalidEnvParameterException.from(ErrorMessage.INVALID_ENV_PARAMETER);
31+
}
32+
33+
String prefix = resolvePrefix(service.toLowerCase());
34+
String envKey = String.format("%s%s_%s_%s",
35+
prefix,
36+
team.toUpperCase(),
37+
stage.toUpperCase(),
38+
type.toUpperCase());
39+
40+
String webhookUrl = dotenv.get(envKey);
41+
42+
if (webhookUrl == null || webhookUrl.isBlank()) {
43+
throw new WebhookUrlNotFoundException(ErrorMessage.WEBHOOK_URL_NOT_FOUND);
44+
}
45+
46+
return webhookUrl;
47+
}
48+
49+
private static String resolvePrefix(String service) {
50+
return switch (service) {
51+
case "slack" -> SLACK_WEBHOOK_PREFIX;
52+
case "discord" -> DISCORD_WEBHOOK_PREFIX;
53+
default -> throw new UnsupportedServiceTypeException(ErrorMessage.UNSUPPORTED_SERVICE_TYPE);
54+
};
55+
}
56+
}

0 commit comments

Comments
 (0)