package com.eactive.eai.rms.onl.manage.rule.layoutsync;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;

import static org.junit.jupiter.api.Assertions.*;

/**
 * LayoutSyncController 통합 테스트 실제 localhost:28080으로 HTTP 요청을 보냅니다.
 */
class LayoutSyncControllerTest {

	private static final String BASE_URL = "http://localhost:28080/monitoring/layoutSync.do";
	private RestTemplate restTemplate;

	@BeforeEach
	void setUp() {
		restTemplate = new RestTemplate();
	}

	@Test
	void testParseMessage() {
		String layoutId = "URP_DRA00357321URP_CBSS";
		String requestXml = "<?xml version=\"1.0\" encoding=\"euc-kr\"?><Message><Header><ServiceName>MessageSync</ServiceName><Command>insert</Command></Header><Body><Layout><id>"
				+ layoutId + "</id><data><![CDATA[<?xml version=\"1.0\" encoding=\"euc-kr\"?>"
				+ "<Format id=\"URP_DRA00357321URP_CBSS\" desc=\"(107300)자동이체 신청/변경_REQ\" author=\"1910272\" usesystem=\"EAI\" group=\"URP\" date=\"2020-08-18 08:58:54\" msgType=\"ASCII\" ioType=\"CBSS\" part=\"data\" createBy=\"1910272\" updateBy=\"1910272\" type=\"business\"><Field id=\"TX_UNQ_NO_S16\" desc=\"거래고유번호_S16\" depth=\"1\" maxCnt=\"0\" no=\"1\" count=\"\" type=\"Varchar\" len=\"16\" scale=\"0\" default=\"\" object=\"\" mask=\"\" maskLength=\"0\" point_yn=\"\" batc_info=\"\" rec_tp_yn=\"\" rec_tp_val=\"\" />"
				+ "<Field id=\"PN_NO_S14\" desc=\"플랜번호_S14\" depth=\"1\" maxCnt=\"0\" no=\"2\" count=\"\" type=\"Varchar\" len=\"14\" scale=\"0\" default=\"\" object=\"\" mask=\"\" maskLength=\"0\" point_yn=\"\" batc_info=\"\" rec_tp_yn=\"\" rec_tp_val=\"\" /><Field id=\"TX_DIV1_S1\" desc=\"거래구분1_S1\" depth=\"1\" maxCnt=\"0\" no=\"3\" count=\"\" type=\"Varchar\" len=\"1\" scale=\"0\" default=\"\" object=\"\" mask=\"\" maskLength=\"0\" point_yn=\"\" batc_info=\"\" rec_tp_yn=\"\" rec_tp_val=\"\" /><Field id=\"REG_DIV_S1\" desc=\"등록구분_S1\" depth=\"1\" maxCnt=\"0\" no=\"4\" count=\"\" type=\"Varchar\" len=\"1\" scale=\"0\" default=\"\" object=\"\" mask=\"\" maskLength=\"0\" point_yn=\"\" batc_info=\"\" rec_tp_yn=\"\" rec_tp_val=\"\" /><Field id=\"SEQNO_S4\" desc=\"일련번호_S4\" depth=\"1\" maxCnt=\"0\" no=\"5\" count=\"\" type=\"Varchar\" len=\"4\" scale=\"0\" default=\"\" object=\"\" mask=\"\" maskLength=\"0\" point_yn=\"\" batc_info=\"\" rec_tp_yn=\"\" rec_tp_val=\"\" /><Field id=\"WDL_ACCO_NO_S16\" desc=\"출금계좌번호_S16\" depth=\"1\" maxCnt=\"0\" no=\"6\" count=\"\" type=\"Varchar\" len=\"16\" scale=\"0\" default=\"\" object=\"\" mask=\"\" maskLength=\"0\" point_yn=\"\" batc_info=\"\" rec_tp_yn=\"\" rec_tp_val=\"\" /><Field id=\"MNRC_ACNO_S16\" desc=\"입금계좌번호_S16\" depth=\"1\" maxCnt=\"0\" no=\"7\" count=\"\" type=\"Varchar\" len=\"16\" scale=\"0\" default=\"\" object=\"\" mask=\"\" maskLength=\"0\" point_yn=\"\" batc_info=\"\" rec_tp_yn=\"\" rec_tp_val=\"\" /><Field id=\"TRSF_DD_S2\" desc=\"이체일_S2\" depth=\"1\" maxCnt=\"0\" no=\"8\" count=\"\" type=\"Varchar\" len=\"2\" scale=\"0\" default=\"\" object=\"\" mask=\"\" maskLength=\"0\" point_yn=\"\" batc_info=\"\" rec_tp_yn=\"\" rec_tp_val=\"\" /><Field id=\"STDD_S8\" desc=\"시작일자_S8\" depth=\"1\" maxCnt=\"0\" no=\"9\" count=\"\" type=\"Varchar\" len=\"8\" scale=\"0\" default=\"\" object=\"\" mask=\"\" maskLength=\"0\" point_yn=\"\" batc_info=\"\" rec_tp_yn=\"\" rec_tp_val=\"\" /><Field id=\"ENDT_S8\" desc=\"종료일자_S8\" depth=\"1\" maxCnt=\"0\" no=\"10\" count=\"\" type=\"Varchar\" len=\"8\" scale=\"0\" default=\"\" object=\"\" mask=\"\" maskLength=\"0\" point_yn=\"\" batc_info=\"\" rec_tp_yn=\"\" rec_tp_val=\"\" /><Field id=\"TRAM_NMCP_INFO1_S14\" desc=\"이체금액부수정보1_S14\" depth=\"1\" maxCnt=\"0\" no=\"11\" count=\"\" type=\"Varchar\" len=\"14\" scale=\"0\" default=\"\" object=\"\" mask=\"\" maskLength=\"0\" point_yn=\"\" batc_info=\"\" rec_tp_yn=\"\" rec_tp_val=\"\" /><Field id=\"CORP_BDMN_S14\" desc=\"기업부담금_S14\" depth=\"1\" maxCnt=\"0\" no=\"12\" count=\"\" type=\"Varchar\" len=\"14\" scale=\"0\" default=\"\" object=\"\" mask=\"\" maskLength=\"0\" point_yn=\"\" batc_info=\"\" rec_tp_yn=\"\" rec_tp_val=\"\" /><Field id=\"IDVD_BDMN_S14\" desc=\"개인부담금_S14\" depth=\"1\" maxCnt=\"0\" no=\"13\" count=\"\" type=\"Varchar\" len=\"14\" scale=\"0\" default=\"\" object=\"\" mask=\"\" maskLength=\"0\" point_yn=\"\" batc_info=\"\" rec_tp_yn=\"\" rec_tp_val=\"\" /><Field id=\"WDL_SUMRY\" desc=\"출금적요\" depth=\"1\" maxCnt=\"0\" no=\"14\" count=\"\" type=\"Varchar\" len=\"50\" scale=\"0\" default=\"\" object=\"\" mask=\"\" maskLength=\"0\" point_yn=\"\" batc_info=\"\" rec_tp_yn=\"\" rec_tp_val=\"\" /><Field id=\"MNRC_SUMRY\" desc=\"입금적요\" depth=\"1\" maxCnt=\"0\" no=\"15\" count=\"\" type=\"Varchar\" len=\"50\" scale=\"0\" default=\"\" object=\"\" mask=\"\" maskLength=\"0\" point_yn=\"\" batc_info=\"\" rec_tp_yn=\"\" rec_tp_val=\"\" /><Field id=\"TX_BRCD\" desc=\"거래부점코드\" depth=\"1\" maxCnt=\"0\" no=\"16\" count=\"\" type=\"Varchar\" len=\"4\" scale=\"0\" default=\"\" object=\"\" mask=\"\" maskLength=\"0\" point_yn=\"\" batc_info=\"\" rec_tp_yn=\"\" rec_tp_val=\"\" /><Field id=\"TX_TLRNO\" desc=\"거래텔러번호\" depth=\"1\" maxCnt=\"0\" no=\"17\" count=\"\" type=\"Varchar\" len=\"7\" scale=\"0\" default=\"\" object=\"\" mask=\"\" maskLength=\"0\" point_yn=\"\" batc_info=\"\" rec_tp_yn=\"\" rec_tp_val=\"\" /></Format>]]>"
				+ "</data></Layout></Body></Message>";

		// =====================================

		String url = BASE_URL + "?cmd=Message&schema=APIGW";

		// HTTP 헤더 설정 (EUC-KR)
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(new MediaType("application", "xml", Charset.forName("EUC-KR")));

		// 요청 생성
		HttpEntity<String> request = new HttpEntity<>(requestXml, headers);

		// POST 요청 전송
		ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, request, String.class);

		// 응답 출력
		System.out.println("=== Status Code: " + response.getStatusCodeValue());
		System.out.println("=== Response Body ===");
		System.out.println(response.getBody());

		// 기본 검증
		assertNotNull(response);
		assertEquals(200, response.getStatusCodeValue());
	}
}
