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
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ dependencies {
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.springframework.security:spring-security-test")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
testImplementation("com.squareup.okhttp3:mockwebserver:3.14.9") //외부 api 서버 테스트용

// 스프링 문서화
implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.8.13")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,65 +18,36 @@
@RestController
@RequestMapping("/api/v1/location/kakao")
@RequiredArgsConstructor
public class KakaoTestController {
public class KakaoApiController {

private final KakaoLocalService kakaoLocalService;

@Operation(
summary = "주변 음식점 조회(테스트)",
description = "테스트용 하드코딩 좌표(서울 시청 근처)로 카카오 로컬에서 주변 음식점을 조회합니다., 하드코딩 좌표 : lat - 37.5665, lng -126.9780"
)
@ApiResponse(responseCode = "200", description = "조회 성공",
content = @Content(schema = @Schema(implementation = KakaoLocalResponse.Document.class)))
@GetMapping("/restaurants")

public List<KakaoLocalResponse.Document> testKakaoRestaurants() {

double lat = 37.5665;
double lng = 126.9780;

return kakaoLocalService.searchNearbyRestaurants(lat, lng);
}

@Operation(
summary = "주변 음식점 조회(테스트)",
description = "테스트용 좌표(서울 시청 근처)로 카카오 로컬에서 주변 음식점을 조회합니다, 좌표는 입력하면 됩니다." +
"예시 : http://localhost:8080/api/v1/location/kakao/restaurant?lat=37.5665&lng=126.9780"
summary = "주변 음식점 조회",
description = "좌표(서울 시청 근처)로 카카오 로컬에서 주변 음식점을 조회합니다, 좌표는 입력하면 됩니다." +
"예시 : http://localhost:8080/api/v1/location/kakao/restaurant?lat=37.5665&lon=126.9780"
)
@PostMapping("/restaurant")
public List<KakaoLocalResponse.Document> KakaoRestaurants(
@RequestParam double lat,
@RequestParam double lng
@RequestParam double lon
){
return kakaoLocalService.searchNearbyRestaurants(lat, lng);
return kakaoLocalService.searchNearbyRestaurants(lat, lon);
}

@Operation(
summary = "주변 카페 조회(테스트)",
description = "테스트용 하드코딩 좌표(서울 시청 근처)로 카카오 로컬에서 주변 카페를 조회합니다., 하드코딩 좌표 : lat - 37.5665, lng -126.9780"
)
@ApiResponse(responseCode = "200", description = "조회 성공",
content = @Content(schema = @Schema(implementation = KakaoLocalResponse.Document.class)))
@GetMapping("/cafes")
public List<KakaoLocalResponse.Document> testKakaoCafes() {

double lat = 37.5665;
double lng = 126.9780;

return kakaoLocalService.searchNearbyCafes(lat, lng);
}

@Operation(
summary = "주변 카페 조회(테스트)",
description = "테스트용 좌표(서울 시청 근처)로 카카오 로컬에서 주변 카페를 조회합니다, 좌표는 입력하면 됩니다." +
"예시 : http://localhost:8080/api/v1/location/kakao/cafes?lat=37.5665&lng=126.9780"
summary = "주변 카페 조회",
description = "좌표(서울 시청 근처)로 카카오 로컬에서 주변 카페를 조회합니다, 좌표는 입력하면 됩니다." +
"예시 : http://localhost:8080/api/v1/location/kakao/cafes?lat=37.5665&lon=126.9780"
)
@PostMapping("/cafes")
public List<KakaoLocalResponse.Document> KakaoCafes(
@RequestParam double lat,
@RequestParam double lng
@RequestParam double lon
){
return kakaoLocalService.searchNearbyCafes(lat, lng);
return kakaoLocalService.searchNearbyCafes(lat, lon);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/location")
public class TmapController {
public class TmapApiController {

private final TmapService tmapService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public class WebClientConfig {
@Value("${kakao.restapi-key}")
private String kakaomapApiKey;

@Value("${kakao.base-url}")
private String kakaoBaseUrl;

@Value("${tmap.base-url}")
private String tmapBaseUrl;
@Bean
public WebClient mailgunClient() {
String auth = "api:" + mailgunApiKey;
Expand Down Expand Up @@ -58,8 +63,9 @@ public RestTemplate restTemplate() {

@Bean
public WebClient kakaoWebClient() {

return WebClient.builder()
.baseUrl("https://dapi.kakao.com")
.baseUrl(kakaoBaseUrl)
.defaultHeader("Authorization", kakaomapApiKey)
.build();
}
Expand All @@ -68,7 +74,7 @@ public WebClient kakaoWebClient() {
@Bean
public WebClient TmapClient(){
return WebClient.builder()
.baseUrl("https://apis.openapi.sk.com")
.baseUrl(tmapBaseUrl)
.defaultHeader("appKey", tmapApiKey)
.build();
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ jwt:
refresh-token-expiration: 1209600 # 14일

tmap: #Tmap 대중교통 추천 api 키
base-url: https://apis.openapi.sk.com
api-key: ${TMAP_API_KEY}

kakao: #Kakao map REST API 키
base-url: https://dapi.kakao.com
restapi-key: ${KAKAOMAP_API_KEY}

kopis:
api-key: ${KOPIST_API_KEY}
spotify:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
//package com.back.web7_9_codecrete_be.domain.location.controller;
//
//import okhttp3.mockwebserver.MockResponse;
//import okhttp3.mockwebserver.MockWebServer;
//import org.junit.jupiter.api.*;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
//import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
//import org.springframework.context.annotation.Import;
//import org.springframework.test.context.ActiveProfiles;
//import org.springframework.test.context.DynamicPropertyRegistry;
//import org.springframework.test.context.DynamicPropertySource;
//import org.springframework.test.web.servlet.MockMvc;
//
//import com.back.web7_9_codecrete_be.domain.location.service.KakaoLocalService;
//import com.back.web7_9_codecrete_be.global.config.WebClientConfig; // 네 프로젝트 패키지에 맞게 import 수정
//
//import java.io.IOException;
//import java.net.URLEncoder;
//import java.nio.charset.StandardCharsets;
//
//import static org.assertj.core.api.Assertions.assertThat;
//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
//@AutoConfigureMockMvc(addFilters = false)
//@ActiveProfiles("test")@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
//@WebMvcTest(controllers = KakaoApiController.class)
//@Import({KakaoLocalService.class, WebClientConfig.class})
//@TestInstance(TestInstance.Lifecycle.PER_CLASS)
//class KakaoApiControllerTest {
//
// private static MockWebServer server;
//
// @Autowired
// private MockMvc mockMvc;
//
//
// @AfterAll
// static void stopServer() throws IOException {
// if (server != null) server.shutdown();
// }
//
// @DynamicPropertySource
// static void overrideProps(DynamicPropertyRegistry r) {
// try {
// if (server == null) {
// server = new MockWebServer();
// server.start();
// }
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
//
// r.add("kakao.base-url", () -> server.url("/").toString());
// r.add("kakao.restapi-key", () -> "KakaoAK test-key");
// }
//
// @Test
// void POST_restaurant_우리API호출하면_카카오연동을거쳐_응답이온다() throws Exception {
// server.enqueue(new MockResponse()
// .setResponseCode(200)
// .addHeader("Content-Type", "application/json; charset=UTF-8")
// .setBody("""
// {
// "documents": [
// {
// "place_name": "테스트식당",
// "x": "127.0",
// "y": "37.5",
// "road_address_name": "서울 ...",
// "address_name": "서울 ...",
// "place_url": "https://place.map.kakao.com/111"
// }
// ]
// }
// """));
//
// mockMvc.perform(post("/api/v1/location/kakao/restaurant")
// .param("lat", "37.5")
// .param("lon", "127.0"))
// .andExpect(status().isOk())
// .andExpect(jsonPath("$[0].place_name").value("테스트식당"))
// .andExpect(jsonPath("$[0].x").value("127.0"))
// .andExpect(jsonPath("$[0].y").value("37.5"));
//
// var req = server.takeRequest();
// assertThat(req.getMethod()).isEqualTo("GET");
// assertThat(req.getHeader("Authorization")).isEqualTo("KakaoAK test-key");
//
// assertThat(req.getPath()).startsWith("/v2/local/search/keyword.json?");
// String encoded = URLEncoder.encode("음식점", StandardCharsets.UTF_8);
// assertThat(req.getPath()).contains("query=" + encoded);
// assertThat(req.getPath()).contains("y=37.5");
// assertThat(req.getPath()).contains("x=127.0");
// assertThat(req.getPath()).contains("radius=1000");
// assertThat(req.getPath()).contains("sort=distance");
// }
//
// @Test
// void POST_cafes_우리API호출하면_카카오연동을거쳐_응답이온다() throws Exception {
// server.enqueue(new MockResponse()
// .setResponseCode(200)
// .addHeader("Content-Type", "application/json; charset=UTF-8")
// .setBody("""
// {
// "documents": [
// {
// "place_name": "테스트카페",
// "x": "126.9780",
// "y": "37.5665",
// "road_address_name": "서울 ...",
// "address_name": "서울 ...",
// "place_url": "https://place.map.kakao.com/222"
// }
// ]
// }
// """));
//
// mockMvc.perform(post("/api/v1/location/kakao/cafes")
// .param("lat", "37.5665")
// .param("lon", "126.9780"))
// .andExpect(status().isOk())
// .andExpect(jsonPath("$[0].place_name").value("테스트카페"))
// .andExpect(jsonPath("$[0].x").value("126.9780"))
// .andExpect(jsonPath("$[0].y").value("37.5665"));
//
// var req = server.takeRequest();
// assertThat(req.getMethod()).isEqualTo("GET");
// assertThat(req.getHeader("Authorization")).isEqualTo("KakaoAK test-key");
//
// assertThat(req.getPath()).startsWith("/v2/local/search/keyword.json?");
// String encoded = URLEncoder.encode("카페", StandardCharsets.UTF_8);
// assertThat(req.getPath()).contains("query=" + encoded);
// assertThat(req.getPath()).contains("category_group_code=CE7");
// assertThat(req.getPath()).contains("y=37.5665");
// assertThat(req.getPath()).contains("x=126.9780");
// assertThat(req.getPath()).contains("radius=1000");
// assertThat(req.getPath()).contains("sort=distance");
// }
//
// @Test
// void GET_coord2address_우리API호출하면_RsData로주소가온다() throws Exception {
// server.enqueue(new MockResponse()
// .setResponseCode(200)
// .addHeader("Content-Type", "application/json; charset=UTF-8")
// .setBody("""
// {
// "documents": [
// {
// "road_address": { "address_name": "서울특별시 중구 세종대로 110" },
// "address": { "address_name": "서울특별시 중구 태평로1가 31" }
// }
// ]
// }
// """));
//
// mockMvc.perform(get("/api/v1/location/kakao/coord2address")
// .param("lat", "37.5665")
// .param("lon", "126.9780"))
// .andExpect(status().isOk())
// .andExpect(jsonPath("$.status").value(200))
// .andExpect(jsonPath("$.resultCode").value("OK"))
// .andExpect(jsonPath("$.msg").value("좌표를 주소로 변환했습니다."))
// .andExpect(jsonPath("$.data").value("서울특별시 중구 세종대로 110"));
//
// var req = server.takeRequest();
// assertThat(req.getMethod()).isEqualTo("GET");
// assertThat(req.getHeader("Authorization")).isEqualTo("KakaoAK test-key");
//
// assertThat(req.getPath()).startsWith("/v2/local/geo/coord2address.json?");
// assertThat(req.getPath()).contains("x=126.978");
// assertThat(req.getPath()).contains("y=37.5665");
// }
//}
Loading