-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: SpringEnvironmentHelper 생성 (#45)
* feat: SpringEnvironmentHelper 생성 * test: SpringEnvironmentHelper 테스트코드 추가 * rename: Util로 클래스명 변경 * fix: 문자열 상수 public static으로 변경 * refactor: SpringEnvironmentUtil 로직 Stream으로 개선 * style: spotless * refactor: 운영환경에 따라 허용되는 도메인 달라지도록 개선 * fix: 문자열 상수 public static final로 변경 * test: DisplayName 어노테이션 제거 * fix: cors origin pattern vite port 삭제 * refactor: SpringEnvironmentUtilTest 문자열 배열 상수로 관리 * style: spotless * refactor: WebSecurityConfig에 RequiredArgsConstructor 어노테이션 사용 * style: spotless
- Loading branch information
Showing
3 changed files
with
163 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/com/depromeet/global/util/SpringEnvironmentUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.depromeet.global.util; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class SpringEnvironmentUtil { | ||
private final Environment environment; | ||
|
||
public static final String PROD = "prod"; | ||
public static final String DEV = "dev"; | ||
public static final String LOCAL = "local"; | ||
|
||
private final List<String> PROD_AND_DEV = List.of(PROD, DEV); | ||
|
||
public String getCurrentProfile() { | ||
return getActiveProfiles() | ||
.filter(profile -> profile.equals(PROD) || profile.equals(DEV)) | ||
.findFirst() | ||
.orElse(LOCAL); | ||
} | ||
|
||
public Boolean isProdProfile() { | ||
return getActiveProfiles().anyMatch(PROD::equals); | ||
} | ||
|
||
public Boolean isDevProfile() { | ||
return getActiveProfiles().anyMatch(DEV::equals); | ||
} | ||
|
||
public Boolean isProdAndDevProfile() { | ||
return getActiveProfiles().anyMatch(PROD_AND_DEV::contains); | ||
} | ||
|
||
private Stream<String> getActiveProfiles() { | ||
return Arrays.stream(environment.getActiveProfiles()); | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
src/test/java/com/depromeet/global/util/SpringEnvironmentUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package com.depromeet.global.util; | ||
|
||
import static com.depromeet.global.util.SpringEnvironmentUtil.*; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.BDDMockito.*; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.core.env.Environment; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class SpringEnvironmentUtilTest { | ||
@Mock private Environment environment; | ||
|
||
@InjectMocks private SpringEnvironmentUtil springEnvironmentUtil; | ||
|
||
private final String[] PROD_ARRAY = new String[] {PROD}; | ||
private final String[] DEV_ARRAY = new String[] {DEV}; | ||
private final String[] LOCAL_ARRAY = new String[] {LOCAL}; | ||
|
||
@Test | ||
void 상용_환경이라면_isProdProfile은_true를_반환한다() { | ||
// given | ||
given(environment.getActiveProfiles()).willReturn(PROD_ARRAY); | ||
|
||
// when | ||
// then | ||
assertTrue(springEnvironmentUtil.isProdProfile()); | ||
} | ||
|
||
@Test | ||
void 상용_환경이_아니라면_isProdProfile은_false를_반환한다() { | ||
// given | ||
given(environment.getActiveProfiles()).willReturn(DEV_ARRAY); | ||
|
||
// when | ||
// then | ||
assertFalse(springEnvironmentUtil.isProdProfile()); | ||
} | ||
|
||
@Test | ||
void 테스트_환경이라면_isDevProfile은_true를_반환한다() { | ||
// given | ||
given(environment.getActiveProfiles()).willReturn(DEV_ARRAY); | ||
|
||
// when | ||
// then | ||
assertTrue(springEnvironmentUtil.isDevProfile()); | ||
} | ||
|
||
@Test | ||
void 테스트_환경이_아니라면_isDevProfile은_false를_반환한다() { | ||
// given | ||
given(environment.getActiveProfiles()).willReturn(LOCAL_ARRAY); | ||
|
||
// when | ||
// then | ||
assertFalse(springEnvironmentUtil.isDevProfile()); | ||
} | ||
|
||
@Test | ||
void 로컬_환경이라면_isProdAndDevProfile은_false를_반환한다() { | ||
// given | ||
given(environment.getActiveProfiles()).willReturn(LOCAL_ARRAY); | ||
|
||
// when | ||
// then | ||
assertFalse(springEnvironmentUtil.isProdAndDevProfile()); | ||
} | ||
|
||
@Test | ||
void 로컬_환경이_아니라면_isProdAndDevProfile은_true를_반환한다() { | ||
// given | ||
given(environment.getActiveProfiles()).willReturn(DEV_ARRAY); | ||
|
||
// when | ||
// then | ||
assertTrue(springEnvironmentUtil.isProdAndDevProfile()); | ||
} | ||
|
||
@Test | ||
void 상용_환경이라면_getCurrentProfile는은_prod를_반환한다() { | ||
// given | ||
given(environment.getActiveProfiles()).willReturn(PROD_ARRAY); | ||
|
||
// when | ||
// then | ||
assertEquals(springEnvironmentUtil.getCurrentProfile(), PROD); | ||
} | ||
|
||
@Test | ||
void 테스트_환경이라면_getCurrentProfile는은_dev를_반환한다() { | ||
// given | ||
given(environment.getActiveProfiles()).willReturn(DEV_ARRAY); | ||
|
||
// when | ||
// then | ||
assertEquals(springEnvironmentUtil.getCurrentProfile(), DEV); | ||
} | ||
|
||
@Test | ||
void 로컬_환경이라면_getCurrentProfile는은_local을_반환한다() { | ||
// given | ||
given(environment.getActiveProfiles()).willReturn(LOCAL_ARRAY); | ||
|
||
// when | ||
// then | ||
assertEquals(springEnvironmentUtil.getCurrentProfile(), LOCAL); | ||
} | ||
} |