|
| 1 | +//package com.back.web7_9_codecrete_be.domain.location.controller; |
| 2 | +// |
| 3 | +//import okhttp3.mockwebserver.MockResponse; |
| 4 | +//import okhttp3.mockwebserver.MockWebServer; |
| 5 | +//import org.junit.jupiter.api.*; |
| 6 | +//import org.springframework.beans.factory.annotation.Autowired; |
| 7 | +//import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 8 | +//import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
| 9 | +//import org.springframework.context.annotation.Import; |
| 10 | +//import org.springframework.test.context.ActiveProfiles; |
| 11 | +//import org.springframework.test.context.DynamicPropertyRegistry; |
| 12 | +//import org.springframework.test.context.DynamicPropertySource; |
| 13 | +//import org.springframework.test.web.servlet.MockMvc; |
| 14 | +// |
| 15 | +//import com.back.web7_9_codecrete_be.domain.location.service.KakaoLocalService; |
| 16 | +//import com.back.web7_9_codecrete_be.global.config.WebClientConfig; // 네 프로젝트 패키지에 맞게 import 수정 |
| 17 | +// |
| 18 | +//import java.io.IOException; |
| 19 | +//import java.net.URLEncoder; |
| 20 | +//import java.nio.charset.StandardCharsets; |
| 21 | +// |
| 22 | +//import static org.assertj.core.api.Assertions.assertThat; |
| 23 | +//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 24 | +//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 25 | +//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; |
| 26 | +//@AutoConfigureMockMvc(addFilters = false) |
| 27 | +//@ActiveProfiles("test")@TestMethodOrder(MethodOrderer.OrderAnnotation.class) |
| 28 | +//@WebMvcTest(controllers = KakaoApiController.class) |
| 29 | +//@Import({KakaoLocalService.class, WebClientConfig.class}) |
| 30 | +//@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 31 | +//class KakaoApiControllerTest { |
| 32 | +// |
| 33 | +// private static MockWebServer server; |
| 34 | +// |
| 35 | +// @Autowired |
| 36 | +// private MockMvc mockMvc; |
| 37 | +// |
| 38 | +// |
| 39 | +// @AfterAll |
| 40 | +// static void stopServer() throws IOException { |
| 41 | +// if (server != null) server.shutdown(); |
| 42 | +// } |
| 43 | +// |
| 44 | +// @DynamicPropertySource |
| 45 | +// static void overrideProps(DynamicPropertyRegistry r) { |
| 46 | +// try { |
| 47 | +// if (server == null) { |
| 48 | +// server = new MockWebServer(); |
| 49 | +// server.start(); |
| 50 | +// } |
| 51 | +// } catch (IOException e) { |
| 52 | +// throw new RuntimeException(e); |
| 53 | +// } |
| 54 | +// |
| 55 | +// r.add("kakao.base-url", () -> server.url("/").toString()); |
| 56 | +// r.add("kakao.restapi-key", () -> "KakaoAK test-key"); |
| 57 | +// } |
| 58 | +// |
| 59 | +// @Test |
| 60 | +// void POST_restaurant_우리API호출하면_카카오연동을거쳐_응답이온다() throws Exception { |
| 61 | +// server.enqueue(new MockResponse() |
| 62 | +// .setResponseCode(200) |
| 63 | +// .addHeader("Content-Type", "application/json; charset=UTF-8") |
| 64 | +// .setBody(""" |
| 65 | +// { |
| 66 | +// "documents": [ |
| 67 | +// { |
| 68 | +// "place_name": "테스트식당", |
| 69 | +// "x": "127.0", |
| 70 | +// "y": "37.5", |
| 71 | +// "road_address_name": "서울 ...", |
| 72 | +// "address_name": "서울 ...", |
| 73 | +// "place_url": "https://place.map.kakao.com/111" |
| 74 | +// } |
| 75 | +// ] |
| 76 | +// } |
| 77 | +// """)); |
| 78 | +// |
| 79 | +// mockMvc.perform(post("/api/v1/location/kakao/restaurant") |
| 80 | +// .param("lat", "37.5") |
| 81 | +// .param("lon", "127.0")) |
| 82 | +// .andExpect(status().isOk()) |
| 83 | +// .andExpect(jsonPath("$[0].place_name").value("테스트식당")) |
| 84 | +// .andExpect(jsonPath("$[0].x").value("127.0")) |
| 85 | +// .andExpect(jsonPath("$[0].y").value("37.5")); |
| 86 | +// |
| 87 | +// var req = server.takeRequest(); |
| 88 | +// assertThat(req.getMethod()).isEqualTo("GET"); |
| 89 | +// assertThat(req.getHeader("Authorization")).isEqualTo("KakaoAK test-key"); |
| 90 | +// |
| 91 | +// assertThat(req.getPath()).startsWith("/v2/local/search/keyword.json?"); |
| 92 | +// String encoded = URLEncoder.encode("음식점", StandardCharsets.UTF_8); |
| 93 | +// assertThat(req.getPath()).contains("query=" + encoded); |
| 94 | +// assertThat(req.getPath()).contains("y=37.5"); |
| 95 | +// assertThat(req.getPath()).contains("x=127.0"); |
| 96 | +// assertThat(req.getPath()).contains("radius=1000"); |
| 97 | +// assertThat(req.getPath()).contains("sort=distance"); |
| 98 | +// } |
| 99 | +// |
| 100 | +// @Test |
| 101 | +// void POST_cafes_우리API호출하면_카카오연동을거쳐_응답이온다() throws Exception { |
| 102 | +// server.enqueue(new MockResponse() |
| 103 | +// .setResponseCode(200) |
| 104 | +// .addHeader("Content-Type", "application/json; charset=UTF-8") |
| 105 | +// .setBody(""" |
| 106 | +// { |
| 107 | +// "documents": [ |
| 108 | +// { |
| 109 | +// "place_name": "테스트카페", |
| 110 | +// "x": "126.9780", |
| 111 | +// "y": "37.5665", |
| 112 | +// "road_address_name": "서울 ...", |
| 113 | +// "address_name": "서울 ...", |
| 114 | +// "place_url": "https://place.map.kakao.com/222" |
| 115 | +// } |
| 116 | +// ] |
| 117 | +// } |
| 118 | +// """)); |
| 119 | +// |
| 120 | +// mockMvc.perform(post("/api/v1/location/kakao/cafes") |
| 121 | +// .param("lat", "37.5665") |
| 122 | +// .param("lon", "126.9780")) |
| 123 | +// .andExpect(status().isOk()) |
| 124 | +// .andExpect(jsonPath("$[0].place_name").value("테스트카페")) |
| 125 | +// .andExpect(jsonPath("$[0].x").value("126.9780")) |
| 126 | +// .andExpect(jsonPath("$[0].y").value("37.5665")); |
| 127 | +// |
| 128 | +// var req = server.takeRequest(); |
| 129 | +// assertThat(req.getMethod()).isEqualTo("GET"); |
| 130 | +// assertThat(req.getHeader("Authorization")).isEqualTo("KakaoAK test-key"); |
| 131 | +// |
| 132 | +// assertThat(req.getPath()).startsWith("/v2/local/search/keyword.json?"); |
| 133 | +// String encoded = URLEncoder.encode("카페", StandardCharsets.UTF_8); |
| 134 | +// assertThat(req.getPath()).contains("query=" + encoded); |
| 135 | +// assertThat(req.getPath()).contains("category_group_code=CE7"); |
| 136 | +// assertThat(req.getPath()).contains("y=37.5665"); |
| 137 | +// assertThat(req.getPath()).contains("x=126.9780"); |
| 138 | +// assertThat(req.getPath()).contains("radius=1000"); |
| 139 | +// assertThat(req.getPath()).contains("sort=distance"); |
| 140 | +// } |
| 141 | +// |
| 142 | +// @Test |
| 143 | +// void GET_coord2address_우리API호출하면_RsData로주소가온다() throws Exception { |
| 144 | +// server.enqueue(new MockResponse() |
| 145 | +// .setResponseCode(200) |
| 146 | +// .addHeader("Content-Type", "application/json; charset=UTF-8") |
| 147 | +// .setBody(""" |
| 148 | +// { |
| 149 | +// "documents": [ |
| 150 | +// { |
| 151 | +// "road_address": { "address_name": "서울특별시 중구 세종대로 110" }, |
| 152 | +// "address": { "address_name": "서울특별시 중구 태평로1가 31" } |
| 153 | +// } |
| 154 | +// ] |
| 155 | +// } |
| 156 | +// """)); |
| 157 | +// |
| 158 | +// mockMvc.perform(get("/api/v1/location/kakao/coord2address") |
| 159 | +// .param("lat", "37.5665") |
| 160 | +// .param("lon", "126.9780")) |
| 161 | +// .andExpect(status().isOk()) |
| 162 | +// .andExpect(jsonPath("$.status").value(200)) |
| 163 | +// .andExpect(jsonPath("$.resultCode").value("OK")) |
| 164 | +// .andExpect(jsonPath("$.msg").value("좌표를 주소로 변환했습니다.")) |
| 165 | +// .andExpect(jsonPath("$.data").value("서울특별시 중구 세종대로 110")); |
| 166 | +// |
| 167 | +// var req = server.takeRequest(); |
| 168 | +// assertThat(req.getMethod()).isEqualTo("GET"); |
| 169 | +// assertThat(req.getHeader("Authorization")).isEqualTo("KakaoAK test-key"); |
| 170 | +// |
| 171 | +// assertThat(req.getPath()).startsWith("/v2/local/geo/coord2address.json?"); |
| 172 | +// assertThat(req.getPath()).contains("x=126.978"); |
| 173 | +// assertThat(req.getPath()).contains("y=37.5665"); |
| 174 | +// } |
| 175 | +//} |
0 commit comments