-
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
1 parent
0139711
commit 7006da7
Showing
15 changed files
with
904 additions
and
20 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
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
22 changes: 22 additions & 0 deletions
22
Infra/src/main/java/com/example/oauth/tmdb/client/TmdbClient.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,22 @@ | ||
package com.example.oauth.tmdb.client; | ||
|
||
import com.example.oauth.apple.config.AppleOAuthConfig; | ||
import com.example.oauth.tmdb.config.TmdbClientConfig; | ||
import com.example.oauth.tmdb.dto.TdmbResponseDto; | ||
import feign.Headers; | ||
import feign.Param; | ||
import feign.Response; | ||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestHeader; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
|
||
@FeignClient( | ||
name = "TmdbClient", | ||
url = "http://api.koreafilm.or.kr/openapi-data2/wisenut/search_api/search_json2.jsp?collection=kmdb_new2", | ||
configuration = TmdbClientConfig.class) | ||
public interface TmdbClient { | ||
@GetMapping | ||
String getMovieData(@RequestParam("movieId") String movieId, @RequestParam("movieSeq") String movieSeq, @RequestParam("detail") String detail, @RequestParam("ServiceKey") String ServiceKey); | ||
} |
30 changes: 30 additions & 0 deletions
30
Infra/src/main/java/com/example/oauth/tmdb/config/TmdbClientConfig.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,30 @@ | ||
package com.example.oauth.tmdb.config; | ||
|
||
|
||
import feign.codec.Decoder; | ||
import feign.codec.Encoder; | ||
import feign.codec.ErrorDecoder; | ||
import org.springframework.beans.factory.ObjectFactory; | ||
import org.springframework.beans.factory.ObjectProvider; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.autoconfigure.http.HttpMessageConverters; | ||
import org.springframework.cloud.openfeign.support.HttpMessageConverterCustomizer; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Import; | ||
|
||
@Import({TmdbErrorDecoder.class}) | ||
public class TmdbClientConfig { | ||
|
||
@Bean | ||
@ConditionalOnMissingBean(value = ErrorDecoder.class) | ||
public TmdbErrorDecoder commonFeignErrorDecoder() { | ||
return new TmdbErrorDecoder(); | ||
} | ||
|
||
@Bean | ||
Encoder formEncoder() { | ||
return new feign.form.FormEncoder(); | ||
} | ||
|
||
|
||
} |
30 changes: 30 additions & 0 deletions
30
Infra/src/main/java/com/example/oauth/tmdb/config/TmdbErrorDecoder.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,30 @@ | ||
package com.example.oauth.tmdb.config; | ||
|
||
import com.amazonaws.util.IOUtils; | ||
import com.example.error.BaseRunTimeException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import feign.Response; | ||
import feign.codec.ErrorDecoder; | ||
import lombok.SneakyThrows; | ||
|
||
import java.io.InputStream; | ||
|
||
public class TmdbErrorDecoder implements ErrorDecoder { | ||
@Override | ||
@SneakyThrows | ||
public Exception decode(String methodKey, Response response) { | ||
InputStream inputStream = response.body().asInputStream(); | ||
byte[] byteArray = IOUtils.toByteArray(inputStream); | ||
String responseBody = new String(byteArray); | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
JsonNode jsonNode = objectMapper.readTree(responseBody); | ||
|
||
String error = jsonNode.get("error") == null ? null : jsonNode.get("error").asText(); | ||
String errorDescription = | ||
jsonNode.get("error_description") == null | ||
? null | ||
: jsonNode.get("error_description").asText(); | ||
throw new BaseRunTimeException(response.status(), error, errorDescription); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
Infra/src/main/java/com/example/oauth/tmdb/dto/TdmbResponseDto.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,36 @@ | ||
package com.example.oauth.tmdb.dto; | ||
|
||
import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.List; | ||
|
||
|
||
@NoArgsConstructor | ||
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) | ||
public class TdmbResponseDto { | ||
private String movieId; | ||
private String movieSeq; | ||
private String title; | ||
private String directorNm; | ||
private String plotText; | ||
//private String posterUrls; | ||
public String getMovieId() { | ||
return movieId; | ||
} | ||
public String getMovieSeq() { | ||
return movieSeq; | ||
} | ||
public String getTitle() { | ||
return title; | ||
} | ||
public String getDirectorNm() { | ||
return directorNm; | ||
} | ||
public String getPlotText() { | ||
return plotText; | ||
} | ||
|
||
} |
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
12 changes: 12 additions & 0 deletions
12
Infra/src/test/java/com/example/config/InfraIntegrateProfileResolver.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,12 @@ | ||
package com.example.config; | ||
|
||
import org.springframework.test.context.ActiveProfilesResolver; | ||
|
||
public class InfraIntegrateProfileResolver implements ActiveProfilesResolver { | ||
|
||
@Override | ||
public String[] resolve(Class<?> testClass) { | ||
// some code to find out your active profiles | ||
return new String[] { "local","infra","core"}; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Infra/src/test/java/com/example/config/InfraIntegrateTestConfig.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,11 @@ | ||
package com.example.config; | ||
|
||
import com.example.CoreApplication; | ||
import com.example.InfraApplication; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
@ComponentScan(basePackageClasses = {InfraApplication.class, CoreApplication.class}) | ||
public class InfraIntegrateTestConfig { | ||
} |
104 changes: 104 additions & 0 deletions
104
Infra/src/test/java/com/example/oauth/tmdb/client/TmdbClientTest.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,104 @@ | ||
package com.example.oauth.tmdb.client; | ||
|
||
import com.example.config.InfraIntegrateProfileResolver; | ||
import com.example.config.InfraIntegrateTestConfig; | ||
import com.example.oauth.tmdb.dto.TdmbResponseDto; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.gson.Gson; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParser; | ||
import feign.Response; | ||
import net.minidev.json.JSONObject; | ||
import okhttp3.ResponseBody; | ||
import org.apache.tomcat.util.json.JSONParser; | ||
import org.apache.tomcat.util.json.ParseException; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.MockitoAnnotations; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.client.AutoConfigureMockRestServiceServer; | ||
import org.springframework.boot.test.autoconfigure.web.client.AutoConfigureWebClient; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.context.TestPropertySource; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
import org.springframework.util.ResourceUtils; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.mockito.Mockito.doNothing; | ||
import static org.mockito.Mockito.times; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.*; | ||
import static org.springframework.test.util.AssertionErrors.*; | ||
|
||
@SpringBootTest(classes = InfraIntegrateTestConfig.class) | ||
@AutoConfigureWireMock(port=0) | ||
@ActiveProfiles(resolver = InfraIntegrateProfileResolver.class) | ||
@TestPropertySource(properties = { "spring.thymeleaf.enabled=false","test.port=http://localhost:${wiremock.server.port}"}) | ||
public class TmdbClientTest { | ||
|
||
@Autowired | ||
private TmdbClient tmdbClient; | ||
@Test | ||
public void testGetMovieData() throws IOException, ParseException { | ||
Path file = ResourceUtils.getFile("classpath:pamyo-response.json").toPath(); | ||
// WireMock 설정 | ||
stubFor(get(urlEqualTo("/openapi-data2/wisenut/search_api/search_xml2.jsp")) | ||
.withQueryParam("collection", equalTo("kmdb_new2")) | ||
.withQueryParam("movieId", equalTo("K")) | ||
.withQueryParam("movieSeq", equalTo("35655")) | ||
.withQueryParam("detail", equalTo("Y")) | ||
.withQueryParam("ServiceKey", equalTo("33D62G26J6LHL95UV54Y")) | ||
.willReturn(aResponse() | ||
.withStatus(HttpStatus.OK.value()) | ||
.withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) | ||
.withBody(Files.readAllBytes(file)))); | ||
|
||
// 테스트할 Feign Client 메소드 호출 | ||
try { | ||
|
||
String mmovieData = tmdbClient.getMovieData("K", "35655", "Y","a"); | ||
System.out.println(mmovieData); | ||
|
||
JsonParser jsonParser = new JsonParser(); | ||
|
||
//3. To Object | ||
|
||
//값 처리하는 로직 | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
JsonNode rootNode = objectMapper.readTree(mmovieData); | ||
//4. To JsonObject | ||
|
||
JsonNode movieData = rootNode.path("Data").path(0).path("Result").path(0); | ||
|
||
|
||
String title = movieData.path("title").asText(); | ||
title = title.trim(); | ||
String directorNm = movieData.path("directors").path("director").path(0).path("directorNm").asText(); | ||
String plotText = movieData.path("plots").path("plot").path(0).path("plotText").asText(); | ||
String firstPosterUrl = movieData.path("posters").asText().split("\\|")[0]; | ||
//System.out.println(tes); | ||
|
||
System.out.println(title +directorNm +plotText+ firstPosterUrl); | ||
// 응답이 예상대로 받아졌는지 검증합니다. | ||
//assertNotNull("check",tes); | ||
}catch (Exception e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
//assertEquals("right",test.size(),1); | ||
|
||
} | ||
} |
Oops, something went wrong.