package com.eactive.eai.kjb.errors;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * MessageUtil.makeJsonErrorMessage 기본 포맷 검증 유틸리티
 * 기본 포맷: {"error":"에러코드","error_description":"에러메시지"}
 */
public class ErrorResponseValidator {

	private static final ObjectMapper objectMapper = new ObjectMapper();

	/**
	 * MessageUtil.makeJsonErrorMessage 포맷 검증
	 * @param responseBody JSON 응답 문자열
	 * @throws Exception JSON 파싱 실패 시
	 */
	public static void validateErrorFormat(String responseBody) throws Exception {
		JsonNode jsonNode = objectMapper.readTree(responseBody);
		assertNotNull(jsonNode, "JSON 응답이 null입니다");

		assertTrue(jsonNode.has("error"), "error 필드가 존재해야 합니다");
		assertTrue(jsonNode.has("error_description"), "error_description 필드가 존재해야 합니다");

		assertNotNull(jsonNode.get("error").asText(), "error 값이 null입니다");
		assertNotNull(jsonNode.get("error_description").asText(), "error_description 값이 null입니다");

		// 검증 성공 시 Pretty Print
		String prettyJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonNode);
		System.out.println("Validated Error Format:");
		System.out.println(prettyJson);
	}
}
