|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.protocol.response.format; |
| 7 | + |
| 8 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 9 | + |
| 10 | +import java.util.LinkedHashMap; |
| 11 | +import java.util.Map; |
| 12 | +import org.junit.jupiter.api.DisplayNameGeneration; |
| 13 | +import org.junit.jupiter.api.DisplayNameGenerator; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.opensearch.sql.utils.YamlFormatter; |
| 16 | + |
| 17 | +@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) |
| 18 | +class YamlResponseFormatterTest { |
| 19 | + |
| 20 | + private final YamlResponseFormatter<Object> formatter = |
| 21 | + new YamlResponseFormatter<>() { |
| 22 | + @Override |
| 23 | + protected Object buildYamlObject(Object response) { |
| 24 | + // Pass-through for testing: return the response directly |
| 25 | + return response; |
| 26 | + } |
| 27 | + }; |
| 28 | + |
| 29 | + @Test |
| 30 | + void content_type_matches_yaml() { |
| 31 | + assertEquals(YamlResponseFormatter.CONTENT_TYPE, formatter.contentType()); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + void formats_response_via_yaml_formatter() { |
| 36 | + Map<String, Object> payload = new LinkedHashMap<>(); |
| 37 | + payload.put("b", 2); |
| 38 | + payload.put("a", "1"); |
| 39 | + |
| 40 | + String expected = YamlFormatter.formatToYaml(payload); |
| 41 | + String actual = formatter.format(payload); |
| 42 | + |
| 43 | + assertEquals(expected, actual); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void formats_throwable_via_yaml_formatter() { |
| 48 | + Exception e = new Exception("boom", new RuntimeException("root-cause")); |
| 49 | + |
| 50 | + String expected = YamlFormatter.formatToYaml(e); |
| 51 | + String actual = formatter.format(e); |
| 52 | + |
| 53 | + assertEquals(expected, actual); |
| 54 | + } |
| 55 | +} |
0 commit comments