package com.eactive.eai.kjb.errors;

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

import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.junit.jupiter.api.Test;

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

class ExceptionHandlerSuccessTest {

	private final ObjectMapper objectMapper = new ObjectMapper();

	@Test
	void testCase001_정상호출() throws Exception {
		// given
		String url = TestConfig.getBaseUrl("/api/test/case001");

		String requestBody = """
				{
				  "serviceId": "TST10001",
				  "aaa": "aaaa",
				  "bbb": "bbbb"
				}
				""";

		// when
		try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
			HttpPost httpPost = new HttpPost(url);
			httpPost.setHeader("Content-Type", "application/json");
			httpPost.setHeader("Accept", "application/json");
			httpPost.setEntity(new StringEntity(requestBody, ContentType.APPLICATION_JSON));

			String responseBody = httpClient.execute(httpPost, response -> {
				assertEquals(200, response.getCode());
				return EntityUtils.toString(response.getEntity());
			});

			// then
			assertNotNull(responseBody);

			JsonNode jsonNode = objectMapper.readTree(responseBody);
			assertNotNull(jsonNode);

			// validate
			assertNotNull(jsonNode.get("transactionId"));
			assertEquals("approved", jsonNode.get("status").asText());
			String prettyJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonNode);
			System.out.println(prettyJson);
		}
	}
}
