공공데이타포탈 의약 제품 주성분 상세정보 API 반영

dev
Jonguk. Lim 7 months ago
parent a7c77316b1
commit 95bcc8994a

@ -0,0 +1,24 @@
package cokr.xit.adds.biz.iros.service;
import java.util.List;
import cokr.xit.adds.inf.iros.model.DrugPrdtMcpnDtl;
import cokr.xit.adds.inf.iros.model.DrugPrdtMcpnDtlRequest;
/**
* <pre>
* description :
* packageName : cokr.xit.adds.biz.iros.service
* fileName : BizIrosService
* author : limju
* date : 2024-04-29
* ======================================================================
*
* ----------------------------------------------------------------------
* 2024-04-29 limju
*
* </pre>
*/
public interface BizIrosService {
List<DrugPrdtMcpnDtl> getDrugPrdtMcpnDtls(DrugPrdtMcpnDtlRequest dto);
}

@ -0,0 +1,42 @@
package cokr.xit.adds.biz.iros.service.bean;
import java.util.List;
import org.springframework.stereotype.Service;
import cokr.xit.adds.biz.iros.service.BizIrosService;
import cokr.xit.adds.inf.iros.model.DrugPrdtMcpnDtl;
import cokr.xit.adds.inf.iros.model.DrugPrdtMcpnDtlRequest;
import cokr.xit.adds.inf.iros.model.IrosResponse;
import cokr.xit.adds.inf.iros.service.InfIrosService;
import cokr.xit.foundation.component.AbstractServiceBean;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
/**
* <pre>
* description :
* packageName : cokr.xit.adds.biz.iros.service.bean
* fileName : BizIrosServiceBean
* author : limju
* date : 2024-04-29
* ======================================================================
*
* ----------------------------------------------------------------------
* 2024-04-29 limju
*
* </pre>
*/
@Slf4j
@RequiredArgsConstructor
@Service
public class BizIrosServiceBean extends AbstractServiceBean implements BizIrosService {
private final InfIrosService infIrosService;
@Override
public List<DrugPrdtMcpnDtl> getDrugPrdtMcpnDtls(DrugPrdtMcpnDtlRequest dto) {
IrosResponse<DrugPrdtMcpnDtl> rslt = infIrosService.getDrugPrdtMcpnDtls(dto);
return rslt.getResultOrThrow();
}
}

@ -0,0 +1,46 @@
package cokr.xit.adds.biz.iros.web;
import java.util.List;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cokr.xit.adds.biz.iros.service.BizIrosService;
import cokr.xit.adds.core.model.ApiBaseResponse;
import cokr.xit.adds.inf.iros.model.DrugPrdtMcpnDtl;
import cokr.xit.adds.inf.iros.model.DrugPrdtMcpnDtlRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
/**
* <pre>
* description :
* packageName : cokr.xit.adds.biz.iros.web
* fileName : BizIrosController
* author : limju
* date : 2024-04-29
* ======================================================================
*
* ----------------------------------------------------------------------
* 2024-04-29 limju
*
* </pre>
*/
@Tag(name = "BizIrosController", description = "공공데이타포탈 업무 API")
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/biz/iros/v1")
public class BizIrosController {
private final BizIrosService bizIrosService;
@Operation(summary = "의약 제품 주성분 상세 정보 조회", description = "의약 제품 주성분 상세 정보 조회")
@PostMapping("/getDrugPrdtMcpnDtls")
public ApiBaseResponse<List<DrugPrdtMcpnDtl>> getDrugPrdtMcpnDtls(
@RequestBody DrugPrdtMcpnDtlRequest dto
) {
return ApiBaseResponse.of(bizIrosService.getDrugPrdtMcpnDtls(dto));
}
}

@ -14,7 +14,6 @@ import cokr.xit.adds.biz.nims.service.BizNimsService;
import cokr.xit.adds.core.model.ApiBaseResponse;
import cokr.xit.adds.inf.nims.model.NimsApiDto;
import cokr.xit.adds.inf.nims.model.NimsApiRequest;
import cokr.xit.adds.inf.nims.service.InfNimsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
@ -42,7 +41,6 @@ import lombok.RequiredArgsConstructor;
@RequestMapping("/api/biz/nims/v1")
public class BizNimsController {
private final BizNimsService bizNimsService;
private final InfNimsService infNimsService;
//------------------------------------------------------------------------------------------------------
// NIMS API CALL

@ -94,7 +94,8 @@ public class SpringDocsConfig {
return GroupedOpenApi.builder()
.group("2. BIZ API interface")
.pathsToMatch(
"/api/biz/nims/**"
"/api/biz/nims/**",
"/api/biz/iros/**"
)
.build();
}
@ -104,7 +105,8 @@ public class SpringDocsConfig {
return GroupedOpenApi.builder()
.group("3. Interface API")
.pathsToMatch(
"/api/inf/nims/**"
"/api/inf/nims/**",
"/api/inf/iros/**"
)
.build();
}

@ -13,7 +13,9 @@ import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.json.simple.JSONObject;
import org.springframework.http.HttpHeaders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
@ -96,7 +98,7 @@ public class ApiUtil {
}
public static String getUtf8UrlEncoding(final String str) {
if(!org.springframework.util.StringUtils.hasText(str)) return str;
if(!org.springframework.util.StringUtils.hasText(str)) return StringUtils.EMPTY;
try {
return URLEncoder.encode(
@ -124,6 +126,21 @@ public class ApiUtil {
return rslt.body();
}
/**
* irosApi - x-www-form-urlencoded
* @param uri String
* @param param String
* @return String
*/
public static String callIrosApi(String uri, String param) {
HttpResponse<String> rslt = new WebClient().get(request -> {
request.header(HttpHeaders.CONTENT_TYPE, "application/json");
request.contentType(WebClient.Request.ContentType.FORM);
request.uri(uri + param);
});
return rslt.body();
}
/**
* Object -> class
* @param obj Object

@ -1,7 +1,6 @@
package cokr.xit.adds.inf.iros.model;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonAlias;
/**
* <pre>
@ -18,31 +17,78 @@ import com.fasterxml.jackson.annotation.JsonProperty;
*
* </pre>
*/
@JsonInclude(JsonInclude.Include.NON_EMPTY)
//@JsonInclude(JsonInclude.Include.NON_EMPTY)
public record DrugPrdtMcpnDtl(
@JsonProperty("ENTRPS_PRMISN_NO")
/**
*
*/
@JsonAlias("ENTRPS_PRMISN_NO")
String entrpsPrmisnNo,
@JsonProperty("ENTRPS")
/**
*
*/
@JsonAlias("ENTRPS")
String entrps,
@JsonProperty("PRDUCT")
/**
* ()
*/
@JsonAlias("PRDUCT")
String prduct,
@JsonProperty("MTRAL_SN")
/**
*
*/
@JsonAlias("MTRAL_SN")
String mtralSn,
@JsonProperty("MTRAL_CODE")
/**
*
*/
@JsonAlias("MTRAL_CODE")
String mtralCode,
@JsonProperty("MTRAL_NM")
/**
*
*/
@JsonAlias("MTRAL_NM")
String mtralNm,
@JsonProperty("QNT")
/**
*
*/
@JsonAlias("QNT")
String qnt,
@JsonProperty("INGD_UNIT_CD")
/**
*
*/
@JsonAlias("INGD_UNIT_CD")
String ingdUnitCd,
@JsonProperty("ITEM_SEQ")
/**
*
*/
@JsonAlias("ITEM_SEQ")
String itemSeq,
@JsonProperty("MAIN_INGR_ENG")
/**
*
*/
@JsonAlias("MAIN_INGR_ENG")
String mainIngrEng,
@JsonProperty("BIZRNO")
/**
*
*/
@JsonAlias("BIZRNO")
String bizrno,
@JsonProperty("CPNT_CTNT_CONT")
/**
*
*/
@JsonAlias("CPNT_CTNT_CONT")
String cpntCtntCont
) {
}

@ -1,12 +1,17 @@
package cokr.xit.adds.inf.iros.model;
import java.util.Objects;
import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.*;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import cokr.xit.adds.core.util.ApiUtil;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* <pre>
@ -23,108 +28,58 @@ import lombok.Builder;
*
* </pre>
*/
@Schema(name = "DrugPrdtMcpnDtlRequest", description = "의약 제품 주성분 상세 정보 조회 request")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class DrugPrdtMcpnDtlRequest {
@Schema(requiredMode = REQUIRED, title = "서비스 키", description = "서비스 키", example = "0fTkYnZU6XjaAPAp6GzKW9Q6fYq8iaoSH9wKUZwz2PBjRXGM04aUgtO3a61xYGjL8nFZn4fjLGS9XErhMSXq%2Bw%3D%3D")
@NotEmpty(message = "서비스 키는 필수 입니다")
private String serviceKey;
private Integer pageNo;
private Integer numOfRows;
private String type;
private String entrpsPrmisnNo;
private String prduct;
private String entrps;
private String bizrno;
public DrugPrdtMcpnDtlRequest() {
}
public DrugPrdtMcpnDtlRequest(
String serviceKey,
Integer pageNo,
Integer numOfRows,
String type,
String entrpsPrmisnNo,
String prduct,
String entrps,
String bizrno
) {
this.serviceKey = serviceKey;
this.pageNo = pageNo;
this.numOfRows = numOfRows;
this.type = type;
this.entrpsPrmisnNo = ApiUtil.getUtf8UrlEncoding(entrpsPrmisnNo);
this.prduct = ApiUtil.getUtf8UrlEncoding(prduct);
this.entrps = ApiUtil.getUtf8UrlEncoding(entrps);
this.bizrno = bizrno;
}
public String serviceKey() {
return serviceKey;
}
public Integer pageNo() {
return pageNo;
}
@Schema(requiredMode = REQUIRED, title = "페이지 번호", description = "페이지 번호", example = "1")
@Pattern(regexp = "[0-9]{1,}", message = "페이지 번호는 필수 입니다")
private String pageNo;
public Integer numOfRows() {
return numOfRows;
}
@Schema(requiredMode = REQUIRED, title = "페이지당 갯수", description = "페이지당 갯수", example = "10")
@Pattern(regexp = "[0-9]{1,}", message = "페이지당 갯수는 필수 입니다")
private String numOfRows;
public String type() {
return type;
}
@JsonProperty("Entrps_prmisn_no")
public String entrpsPrmisnNo() {
return entrpsPrmisnNo;
}
@JsonProperty("Prduct")
public String prduct() {
return prduct;
}
@Schema(requiredMode = REQUIRED, title = "타입", description = "타입(json | xml)", allowableValues = {"json", "xml"}, example = "json")
@NotEmpty(message = "타입(json | xml)은 필수 입니다")
private String type;
@JsonProperty("Entrps")
public String entrps() {
return entrps;
}
@Schema(title = "업체허가번호", description = "업체허가번호", example = " ")
private String entrpsPrmisnNo;
@JsonProperty("Bizrno")
public String bizrno() {
return bizrno;
}
@Schema(title = "제품명", description = "제품명", example = "포도당주사액")
private String prduct;
@Override
public boolean equals(Object obj) {
if (obj == this)
return true;
if (obj == null || obj.getClass() != this.getClass())
return false;
var that = (DrugPrdtMcpnDtlRequest)obj;
return Objects.equals(this.serviceKey, that.serviceKey) &&
Objects.equals(this.pageNo, that.pageNo) &&
Objects.equals(this.numOfRows, that.numOfRows) &&
Objects.equals(this.type, that.type) &&
Objects.equals(this.entrpsPrmisnNo, that.entrpsPrmisnNo) &&
Objects.equals(this.prduct, that.prduct) &&
Objects.equals(this.entrps, that.entrps) &&
Objects.equals(this.bizrno, that.bizrno);
}
@Schema(title = "업체명", description = "업체명", example = " ")
private String entrps;
@Override
public int hashCode() {
return Objects.hash(serviceKey, pageNo, numOfRows, type, entrpsPrmisnNo, prduct, entrps, bizrno);
}
@Schema(title = "사업자등록번호", description = "사업자등록번호", example = " ")
private String bizrno;
@Override
public String toString() {
return "DrugPrdtMcpnDtlRequest[" +
"serviceKey=" + serviceKey + ", " +
"pageNo=" + pageNo + ", " +
"numOfRows=" + numOfRows + ", " +
"type=" + type + ", " +
"entrpsPrmisnNo=" + entrpsPrmisnNo + ", " +
"prduct=" + prduct + ", " +
"entrps=" + entrps + ", " +
"bizrno=" + bizrno + ']';
}
// public DrugPrdtMcpnDtlRequest(
// String serviceKey,
// String pageNo,
// String numOfRows,
// String type,
// String entrpsPrmisnNo,
// String prduct,
// String entrps,
// String bizrno
// ) {
// this.serviceKey = serviceKey;
// this.pageNo = pageNo;
// this.numOfRows = numOfRows;
// this.type = type;
// this.entrpsPrmisnNo = ApiUtil.getUtf8UrlEncoding(entrpsPrmisnNo);
// this.prduct = ApiUtil.getUtf8UrlEncoding(prduct);
// this.entrps = ApiUtil.getUtf8UrlEncoding(entrps);
// this.bizrno = bizrno;
// }
}

@ -1,6 +1,18 @@
package cokr.xit.adds.inf.iros.model;
import java.util.List;
import java.util.Objects;
import org.apache.commons.lang3.ObjectUtils;
import com.fasterxml.jackson.annotation.JsonIgnore;
import cokr.xit.adds.core.model.ResultCode;
import cokr.xit.adds.core.spring.exception.ApiCustomException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
/**
* <pre>
@ -17,21 +29,82 @@ import java.util.List;
*
* </pre>
*/
public record IrosResponse<T>(
Header header,
Body<T> body
) {
public static record Header(
String resultCode,
String resultMsg
) {
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class IrosResponse<T> {
Header header;
Body<T> body;
/**
* API call return
* ApiCustomException throw
* : 0-, 1-, 8-, 9-
* @return List<T> or throw ApiCustomException
*/
@JsonIgnore
public List<T> getResultOrThrow() {
if(!ObjectUtils.isEmpty(getResult())) return getResult();
if("00".equals(header.resultCode)){
throw ApiCustomException.create(ResultCode.NO_CONTENT);
}
throw Objects.requireNonNull(ApiCustomException.of(header.resultCode, header.resultMsg));
}
/**
* API call return
* null return
* : 0-, 1-, 8-, 9-
* @return List<T> or null
*/
@JsonIgnore
public List<T> getResultOrNull() {
return getResult();
}
private List<T> getResult() {
if("00".equals(header.resultCode)) {
return body.items;
}
return null;
}
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public static class Header {
String resultCode;
String resultMsg;
}
public static record Body<T>(
String pageNo,
String numOfRows,
String totalCount,
List<T> items
) {
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public static class Body<T> {
String pageNo;
String numOfRows;
String totalCount;
List<T> items;
}
}
// public record IrosResponse<T>(
// Header header,
// Body<T> body
// ) {
// public static record Header(
// String resultCode,
// String resultMsg
// ) {
// }
//
// public static record Body<T>(
// String pageNo,
// String numOfRows,
// String totalCount,
// List<T> items
// ) {
// }
// }

@ -1,5 +1,9 @@
package cokr.xit.adds.inf.iros.service;
import cokr.xit.adds.inf.iros.model.DrugPrdtMcpnDtl;
import cokr.xit.adds.inf.iros.model.DrugPrdtMcpnDtlRequest;
import cokr.xit.adds.inf.iros.model.IrosResponse;
/**
* <pre>
* description :
@ -15,5 +19,6 @@ package cokr.xit.adds.inf.iros.service;
*
* </pre>
*/
public class InfIrosService {
public interface InfIrosService {
IrosResponse<DrugPrdtMcpnDtl> getDrugPrdtMcpnDtls(DrugPrdtMcpnDtlRequest dto);
}

@ -0,0 +1,117 @@
package cokr.xit.adds.inf.iros.service.bean;
import java.util.Map;
import javax.validation.Validation;
import javax.validation.Validator;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.core.type.TypeReference;
import cokr.xit.adds.core.spring.exception.ApiCustomException;
import cokr.xit.adds.core.util.ApiUtil;
import cokr.xit.adds.inf.iros.model.DrugPrdtMcpnDtl;
import cokr.xit.adds.inf.iros.model.DrugPrdtMcpnDtlRequest;
import cokr.xit.adds.inf.iros.model.IrosResponse;
import cokr.xit.adds.inf.iros.service.InfIrosService;
import cokr.xit.foundation.component.AbstractServiceBean;
import cokr.xit.foundation.data.JSON;
import cokr.xit.foundation.data.XML;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
/**
* <pre>
* description :
* packageName : cokr.xit.adds.inf.iros.service.bean
* fileName : InfIrosServiceBean
* author : limju
* date : 2024-04-29
* ======================================================================
*
* ----------------------------------------------------------------------
* 2024-04-29 limju
*
* </pre>
*/
@Slf4j
@RequiredArgsConstructor
@Service
public class InfIrosServiceBean extends AbstractServiceBean implements InfIrosService {
private static final Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
private static JSON json = new JSON();
@Value("${app.inf.iros.url}")
private String irosUrl;
@Value("${app.inf.iros.api-key}")
private String irosApiKey;
@Value("${app.inf.iros.api.drugPrdtMcpnDtlInq}")
private String drugPrdtMcpnDtlInq;
@Override
public IrosResponse<DrugPrdtMcpnDtl> getDrugPrdtMcpnDtls(DrugPrdtMcpnDtlRequest dto) {
dto.setServiceKey(irosApiKey);
ApiUtil.validate(dto, null, validator);
if(isEmpty(dto.getEntrpsPrmisnNo())
&& isEmpty(dto.getPrduct())
&& isEmpty(dto.getEntrps())
&& isEmpty(dto.getBizrno())
) throw ApiCustomException.create("업체허가번호, 제품명, 업체명, 사업자번호 중 하나는 필수 입니다");
String params = String.format(
"?serviceKey=%s&pageNo=%s&numOfRows=%s&type=%s&Entrps_prmisn_no=%s&Prduct=%s&Entrps=%s&Bizrno=%s",
dto.getServiceKey(),
dto.getPageNo(),
dto.getNumOfRows(),
dto.getType(),
dto.getEntrpsPrmisnNo(),
dto.getPrduct(),
dto.getEntrps(),
dto.getBizrno());
String rslt = ApiUtil.callIrosApi(irosUrl + drugPrdtMcpnDtlInq, params);
IrosResponse<DrugPrdtMcpnDtl> result = null;
try {
result = json.parse(rslt, new TypeReference<IrosResponse<DrugPrdtMcpnDtl>>() {});
} catch (Exception e) {
if(!isEmpty(e.getCause())){
//System.out.println("JsonParseException" + je.getMessage());
gatewayErrorParsing(rslt);
}
throw ApiCustomException.create(e.getMessage());
}
return result;
}
/**
* <pre>
* Gateway Error Parsing
*
* <OpenAPI ServiceResponse>
* <cmmMsgHeader>
* <errMsg>SERVICE ERROR</errMsg>
* <returnAuthMsg>SERVICE_KEY_IS_NOT_REGISTERED_ERROR</returnAuthMsg>
* <returnReasonCode>30</returnReasonCode>
* </cmmMsgHeader>
* <OpenAPI ServiceResponse>
* @param rslt
* </pre>
*/
private static void gatewayErrorParsing(String rslt) {
XML xml = new XML();
Map<String, Map<String, String>> map = xml.parse(rslt, Map.class);
Map<String, String> hMap = map.get("cmmMsgHeader");
if(!isEmpty(hMap)) {
throw ApiCustomException.create(Integer.valueOf(hMap.get("returnReasonCode")),
String.format("%s[%s]", hMap.get("errMsg"), hMap.get("returnAuthMsg")));
}
}
}

@ -1,5 +1,19 @@
package cokr.xit.adds.inf.iros.web;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cokr.xit.adds.core.model.ApiBaseResponse;
import cokr.xit.adds.inf.iros.model.DrugPrdtMcpnDtl;
import cokr.xit.adds.inf.iros.model.DrugPrdtMcpnDtlRequest;
import cokr.xit.adds.inf.iros.model.IrosResponse;
import cokr.xit.adds.inf.iros.service.InfIrosService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
/**
* <pre>
* description :
@ -15,6 +29,18 @@ package cokr.xit.adds.inf.iros.web;
*
* </pre>
*/
public class InfIrosController
{
@Tag(name = "InfIrosController", description = "공공데이타포탈 Interface API - Api Interface call Test(테스트용)")
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/inf/iros/v1")
public class InfIrosController {
private final InfIrosService infIrosService;
@Operation(summary = "의약 제품 주성분 상세 정보 조회", description = "의약 제품 주성분 상세 정보 조회")
@PostMapping("/getDrugPrdtMcpnDtls")
public ApiBaseResponse<IrosResponse<DrugPrdtMcpnDtl>> getDrugPrdtMcpnDtls(
@RequestBody DrugPrdtMcpnDtlRequest dto
) {
return ApiBaseResponse.of(infIrosService.getDrugPrdtMcpnDtls(dto));
}
}

@ -15,3 +15,8 @@ app:
officeinfo: /api/officeinfo.do
placeinfoV1: /api/placeinfo_v1.do
reportinfo: /api/reportinfo_ggg.do
iros:
url: https://apis.data.go.kr
api-key: 0fTkYnZU6XjaAPAp6GzKW9Q6fYq8iaoSH9wKUZwz2PBjRXGM04aUgtO3a61xYGjL8nFZn4fjLGS9XErhMSXq%2Bw%3D%3D
api:
drugPrdtMcpnDtlInq: /1471000/DrugPrdtPrmsnInfoService05/getDrugPrdtMcpnDtlInq04

@ -1,26 +1,21 @@
package cokr.xit.adds.inf.iros;
import java.util.List;
import java.net.http.HttpResponse;
import org.apache.http.Header;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.assertj.core.api.Assertions;
import org.assertj.core.util.Lists;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import com.fasterxml.jackson.core.type.TypeReference;
import cokr.xit.adds.inf.iros.model.DrugPrdtMcpnDtl;
import cokr.xit.adds.inf.iros.model.DrugPrdtMcpnDtlRequest;
import cokr.xit.adds.inf.iros.model.IrosResponse;
import cokr.xit.foundation.data.JSON;
import cokr.xit.foundation.web.WebClient;
/**
* <pre>
* description :
@ -39,43 +34,39 @@ import cokr.xit.adds.inf.iros.model.IrosResponse;
@ExtendWith(SpringExtension.class)
@AutoConfigureMockMvc
public class IrosRestfulTest {
private static final String url = "http://apis.data.go.kr/1471000/DrugPrdtPrmsnInfoService05/getDrugPrdtMcpnDtlInq04";
private static final String serviceKey = "0fTkYnZU6XjaAPAp6GzKW9Q6fYq8iaoSH9wKUZwz2PBjRXGM04aUgtO3a61xYGjL8nFZn4fjLGS9XErhMSXq+w==";
TestRestTemplate testRestTemplate = new TestRestTemplate();
private static final String uri = "http://apis.data.go.kr/1471000/DrugPrdtPrmsnInfoService05/getDrugPrdtMcpnDtlInq04";
private static final String serviceKey = "0fTkYnZU6XjaAPAp6GzKW9Q6fYq8iaoSH9wKUZwz2PBjRXGM04aUgtO3a61xYGjL8nFZn4fjLGS9XErhMSXq%2Bw%3D%3D";
@DisplayName("의약품 성분조회 테스트")
@Test
public void testDrugPrdtMcpnDtl() {
final List<Header> headers = Lists.newArrayList(new BasicHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE));
final CloseableHttpClient httpClient = HttpClients.custom().setDefaultHeaders(headers).build();
testRestTemplate.getRestTemplate().setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient));
final DrugPrdtMcpnDtlRequest request = DrugPrdtMcpnDtlRequest.builder()
final DrugPrdtMcpnDtlRequest param = DrugPrdtMcpnDtlRequest.builder()
.serviceKey(serviceKey)
.pageNo(1)
.numOfRows(10)
.pageNo("1")
.numOfRows("10")
.type("json")
.prduct("중외20%포도당주사액")
.prduct("포도당주사액")
.build();
final String urlParam = String.format(
"?serviceKey=%s&pageNo=%d&numOfRows=%d&type=%s&Prduct=%s",
request.serviceKey(),
request.pageNo(),
request.numOfRows(),
request.type(),
request.prduct());
final IrosResponse<DrugPrdtMcpnDtl> irosResponse = testRestTemplate.getForObject(url + urlParam, IrosResponse.class);
Assertions.assertThat(irosResponse).isNotNull();
Assertions.assertThat(irosResponse.header().resultCode()).isEqualTo("00");
Assertions.assertThat(irosResponse.header().resultMsg()).isEqualTo("NORMAL SERVICE.");
Assertions.assertThat(irosResponse.body().pageNo()).isEqualTo("1");
Assertions.assertThat(irosResponse.body().numOfRows()).isEqualTo("10");
Assertions.assertThat(irosResponse.body().totalCount()).isEqualTo("1");
//HttpEntity<DrugPrdtMcpnDtlRequest> requestEntity = new HttpEntity<>(request);
//ResponseEntity<IrosResponse<DrugPrdtMcpnDtl>> forEntity = testRestTemplate.exchange(url, HttpMethod.GET, requestEntity, new ParameterizedTypeReference<IrosResponse<DrugPrdtMcpnDtl>>() {});
//Assertions.assertThat(forEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
HttpResponse<String> rslt = new WebClient().get(request -> {
String urlParam = String.format(
"?serviceKey=%s&pageNo=%s&numOfRows=%s&type=%s&Prduct=%s",
param.getServiceKey(),
param.getPageNo(),
param.getNumOfRows(),
param.getType(),
param.getPrduct()
);
request.header("Content-Type", "application/json");
request.contentType(WebClient.Request.ContentType.FORM);
request.uri(uri + urlParam);
});
IrosResponse<DrugPrdtMcpnDtl> parse = new JSON().parse(rslt.body(),
new TypeReference<IrosResponse<DrugPrdtMcpnDtl>>() {
});
System.out.println(parse);
}
}

Loading…
Cancel
Save