-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: RouteClient 응답 분기를 객체로 캡슐화 (#188)
* refactor: RouteClient 응답 분기를 객체로 캡슐화 * chore: 패키지 디렉토리 변경 domain > dto * refactor : 응답 매핑 로직을 mapper로 이전 * refactor : 불필요한 static 객체 삭제 * refactor : 500에러 처리 로직 추가 * refactor : NPE 예외 처리 * refactor : 롬복으로 private 생성자 구현 * test: OdsayResponseMapperTest 작성 * style: message 개행 변경 Co-authored-by: H <[email protected]> * refactor: OdsayRouteClient에 mapper 로직 반영 * fix: FutureOrPresentDateTimeValidatorTest 오류 개선 --------- Co-authored-by: coli-geonwoo <[email protected]> Co-authored-by: H <[email protected]>
- Loading branch information
1 parent
a08b156
commit 188d651
Showing
7 changed files
with
159 additions
and
31 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
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
60 changes: 60 additions & 0 deletions
60
backend/src/main/java/com/ody/route/mapper/OdsayResponseMapper.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,60 @@ | ||
package com.ody.route.mapper; | ||
|
||
import com.ody.common.exception.OdyBadRequestException; | ||
import com.ody.common.exception.OdyServerErrorException; | ||
import com.ody.route.dto.OdsayResponse; | ||
import java.util.Optional; | ||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class OdsayResponseMapper { | ||
|
||
private static final String CLOSE_LOCATION_CODE = "-98"; //출발지-도착지가 700m 이내일 때 | ||
private static final String ODSAY_SERVER_ERROR = "500"; | ||
private static final String EMPTY_MESSAGE = ""; | ||
private static final long ZERO_TIME = 0L; | ||
|
||
public static long mapMinutes(OdsayResponse response) { | ||
if (response == null) { | ||
throw new OdyServerErrorException("response is null"); | ||
} | ||
|
||
if (isCloseLocation(response)) { | ||
return ZERO_TIME; | ||
} | ||
|
||
if(response.code().isPresent()) { | ||
checkOdsayException(response); | ||
} | ||
|
||
return response.minutes().orElseThrow(() -> { | ||
log.error("OdsayResponse minutes is Empty: {}", response); | ||
return new OdyServerErrorException("서버 에러"); | ||
}); | ||
} | ||
|
||
private static boolean isCloseLocation(OdsayResponse response) { | ||
Optional<String> code = response.code(); | ||
return code.isPresent() && CLOSE_LOCATION_CODE.equals(code.get()); | ||
} | ||
|
||
private static void checkOdsayException(OdsayResponse response) { | ||
if (isServerErrorCode(response)) { | ||
log.error("ODsay 500 에러: {}", response); | ||
throw new OdyServerErrorException("서버 에러"); | ||
} | ||
|
||
throw new OdyBadRequestException( | ||
response.message() | ||
.orElse(EMPTY_MESSAGE) | ||
); | ||
} | ||
|
||
private static boolean isServerErrorCode(OdsayResponse response) { | ||
Optional<String> code = response.code(); | ||
return code.isPresent() && ODSAY_SERVER_ERROR.equals(code.get()); | ||
} | ||
} |
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
Submodule private
updated
from 4a7086 to 753991
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
86 changes: 86 additions & 0 deletions
86
backend/src/test/java/com/ody/route/mapper/OdsayResponseMapperTest.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,86 @@ | ||
package com.ody.route.mapper; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import com.ody.common.exception.OdyBadRequestException; | ||
import com.ody.common.exception.OdyServerErrorException; | ||
import com.ody.route.dto.OdsayResponse; | ||
import java.util.Optional; | ||
import java.util.OptionalLong; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class OdsayResponseMapperTest { | ||
|
||
@DisplayName("소요시간을 반환한다") | ||
@Test | ||
void mapMinutesSuccess() { | ||
OdsayResponse validResponse = new OdsayResponse( | ||
Optional.empty(), | ||
Optional.empty(), | ||
OptionalLong.of(3L) | ||
); | ||
|
||
assertThat(OdsayResponseMapper.mapMinutes(validResponse)) | ||
.isEqualTo(3L); | ||
} | ||
|
||
@DisplayName("출, 도착지가 700m 이내일 때 0분을 반환한다") | ||
@Test | ||
void mapZeroMinutesWhenCloseLocation() { | ||
OdsayResponse closeLocationResponse = new OdsayResponse( | ||
Optional.of("-98"), | ||
Optional.of("출,도착지가 700m 이내 입니다"), | ||
OptionalLong.empty() | ||
); | ||
|
||
assertThat(OdsayResponseMapper.mapMinutes(closeLocationResponse)) | ||
.isEqualTo(0L); | ||
} | ||
|
||
@DisplayName("예외 응답 : 500 에러 코드는 OdyServerErrorException을 던진다") | ||
@Test | ||
void mapMinutes500Fail() { | ||
OdsayResponse serverExceptionResponse = new OdsayResponse( | ||
Optional.of("500"), | ||
Optional.of("서버 에러 발생"), | ||
OptionalLong.empty() | ||
); | ||
|
||
assertThatThrownBy(() -> OdsayResponseMapper.mapMinutes(serverExceptionResponse)) | ||
.isInstanceOf(OdyServerErrorException.class); | ||
} | ||
|
||
@DisplayName("예외 응답 : 그 외 에러 코드는 OdyBadRequestException을 던진다") | ||
@Test | ||
void mapMinutes400Fail() { | ||
OdsayResponse badRequestExceptionException = new OdsayResponse( | ||
Optional.of("1"), | ||
Optional.of("그 외 에러 발생"), | ||
OptionalLong.empty() | ||
); | ||
|
||
assertThatThrownBy(() -> OdsayResponseMapper.mapMinutes(badRequestExceptionException)) | ||
.isInstanceOf(OdyBadRequestException.class); | ||
} | ||
|
||
@DisplayName("response가 null인 경우 OdyServerErrorException을 던진다") | ||
@Test | ||
void nullCheck500Fail() { | ||
assertThatThrownBy(() -> OdsayResponseMapper.mapMinutes(null)) | ||
.isInstanceOf(OdyServerErrorException.class); | ||
} | ||
|
||
@DisplayName("예외 응답 : 에러 코드와 소요시간이 모두 없는 경우 OdyServerException을 던진다") | ||
@Test | ||
void noneDataResponse500Fail() { | ||
OdsayResponse unValidResponse = new OdsayResponse( | ||
Optional.empty(), | ||
Optional.empty(), | ||
OptionalLong.empty() | ||
); | ||
assertThatThrownBy(() -> OdsayResponseMapper.mapMinutes(unValidResponse)) | ||
.isInstanceOf(OdyServerErrorException.class); | ||
} | ||
} |