This repository has been archived by the owner on Aug 13, 2022. It is now read-only.
-
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.
- Loading branch information
Showing
12 changed files
with
220 additions
and
42 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
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
2 changes: 2 additions & 0 deletions
2
src/main/java/com/flab/doorrush/global/dto/response/kakao/Address.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
2 changes: 2 additions & 0 deletions
2
src/main/java/com/flab/doorrush/global/dto/response/kakao/GetAddressInfo.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
11 changes: 11 additions & 0 deletions
11
src/main/java/com/flab/doorrush/global/dto/response/kakao/KakaoApiGetAddressResponse.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
13 changes: 10 additions & 3 deletions
13
src/main/java/com/flab/doorrush/global/dto/response/kakao/Meta.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 |
---|---|---|
@@ -1,15 +1,22 @@ | ||
package com.flab.doorrush.global.dto.response.kakao; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@AllArgsConstructor | ||
@Getter | ||
@Builder | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class Meta { | ||
|
||
@JsonProperty("total_count") | ||
private String totalCount; | ||
private int totalCount; | ||
|
||
public boolean isExist() { | ||
return !totalCount.equals("0"); | ||
return !(totalCount == 0); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
src/main/java/com/flab/doorrush/global/dto/response/kakao/RoadAddress.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
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
109 changes: 95 additions & 14 deletions
109
src/test/java/com/flab/doorrush/global/api/KakaoAddressApiTest.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 |
---|---|---|
@@ -1,35 +1,116 @@ | ||
package com.flab.doorrush.global.api; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static com.flab.doorrush.global.api.mock.KakaoMockObject.createKakaoApiFailResponse; | ||
import static com.flab.doorrush.global.api.mock.KakaoMockObject.createKakaoApiRequest; | ||
import static com.flab.doorrush.global.api.mock.KakaoMockObject.createKakaoApiSuccessResponse; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.get; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import com.flab.doorrush.global.dto.request.KakaoApiGetAddressRequest; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.flab.doorrush.global.dto.response.kakao.KakaoApiGetAddressResponse; | ||
import io.github.resilience4j.circuitbreaker.CircuitBreaker; | ||
import io.github.resilience4j.circuitbreaker.CircuitBreaker.State; | ||
import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry; | ||
import java.util.Optional; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; | ||
import org.springframework.cloud.contract.spec.internal.HttpStatus; | ||
import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.test.context.ActiveProfiles; | ||
|
||
|
||
@SpringBootTest | ||
@ActiveProfiles("test") | ||
@SpringBootTest(webEnvironment = WebEnvironment.NONE) | ||
@AutoConfigureWireMock(port = 0) | ||
class KakaoAddressApiTest { | ||
|
||
|
||
@Autowired | ||
KakaoAddressApi kakaoAddressApi; | ||
|
||
@Autowired | ||
ObjectMapper objectMapper; | ||
|
||
@Autowired | ||
CircuitBreakerRegistry CircuitBreakerRegistry; | ||
|
||
@Test | ||
@DisplayName("좌표로 주소 받아오는 API 성공 테스트") | ||
public void getAddressSuccessTest() { | ||
ResponseEntity<KakaoApiGetAddressResponse> result = kakaoAddressApi.getAddressBySpot(KakaoApiGetAddressRequest.builder() | ||
.x("127.423084873712") | ||
.y("37.0789561558879") | ||
.build()); | ||
|
||
KakaoApiGetAddressResponse response = result.getBody(); | ||
assertThat(response.getMainAddress().getRoadAddress().getAddressName()).isEqualTo("경기도 안성시 죽산면 죽산초교길 69-4"); | ||
assertThat(response.getMainAddress().getAddress().getAddressName()).isEqualTo("경기 안성시 죽산면 죽산리 343-1"); | ||
assertTrue(response.getMeta().isExist()); | ||
public void getAddressSuccessTest() throws JsonProcessingException { | ||
|
||
// Given | ||
stubFor(get(urlPathEqualTo("/v2/local/geo/coord2address.json")) | ||
.willReturn(aResponse() | ||
.withStatus(200) | ||
.withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) | ||
.withBody(objectMapper.writeValueAsString(createKakaoApiSuccessResponse())))); | ||
|
||
// When | ||
ResponseEntity<KakaoApiGetAddressResponse> response = kakaoAddressApi.getAddressBySpot( | ||
createKakaoApiRequest()); | ||
|
||
// Then | ||
assertEquals(objectMapper.writeValueAsString(response.getBody()), | ||
objectMapper.writeValueAsString(createKakaoApiSuccessResponse())); | ||
assertEquals(HttpStatus.OK, response.getStatusCode().value()); | ||
} | ||
|
||
@Test | ||
@DisplayName("좌표로 주소 받아오는 API 실패 테스트 - fallback 메소드 작동") | ||
public void getAddressRunningFallbackTest() throws JsonProcessingException { | ||
|
||
// Given | ||
stubFor(get(urlPathEqualTo("/v2/local/geo/coord2address.json")) | ||
.willReturn(aResponse() | ||
.withStatus(500) | ||
.withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) | ||
.withBody(objectMapper.writeValueAsString(createKakaoApiFailResponse())))); | ||
|
||
// When | ||
ResponseEntity<KakaoApiGetAddressResponse> response = kakaoAddressApi.getAddressBySpot( | ||
createKakaoApiRequest()); | ||
|
||
// Then | ||
assertEquals(objectMapper.writeValueAsString(response.getBody()), | ||
objectMapper.writeValueAsString(createKakaoApiFailResponse())); | ||
assertEquals(HttpStatus.OK, response.getStatusCode().value()); | ||
} | ||
|
||
@Test | ||
@DisplayName("minimumNumberOfCalls에 도달 시 CircuitBreaker OPEN 상태로 변경") | ||
public void shouldCircuitBreakerOpenStatus() throws JsonProcessingException { | ||
// Given | ||
Optional<CircuitBreaker> circuitBreaker = CircuitBreakerRegistry.find( | ||
"kakaoAddressApiCircuitBreaker"); | ||
int minimumNumberOfCalls = circuitBreaker.get().getCircuitBreakerConfig() | ||
.getMinimumNumberOfCalls(); | ||
|
||
stubFor(get(urlPathEqualTo("/v2/local/geo/coord2address.json")) | ||
.willReturn(aResponse() | ||
.withStatus(500) | ||
.withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) | ||
.withBody(objectMapper.writeValueAsString(createKakaoApiFailResponse())))); | ||
|
||
circuitBreaker.get().reset(); | ||
// When | ||
for (int i = 1; i < minimumNumberOfCalls + 10; i++) { | ||
kakaoAddressApi.getAddressBySpot(createKakaoApiRequest()); | ||
// Then | ||
if (i < minimumNumberOfCalls) { | ||
assertEquals(State.CLOSED, circuitBreaker.get().getState()); | ||
} else { | ||
assertEquals(State.OPEN, circuitBreaker.get().getState()); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.