package at.procon.eventhub.persistence;

import static org.assertj.core.api.Assertions.assertThat;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

class EventHubEventReadRepositoryTest {

    private final ObjectMapper objectMapper = new ObjectMapper().findAndRegisterModules();

    @Test
    void parsesRegularJsonObjectColumn() {
        JsonNode node = EventHubEventReadRepository.parseJsonColumn(
                objectMapper,
                "{\"raw\":{\"driverKey\":\"12:123\"},\"details\":{\"k\":1}}"
        );

        assertThat(node.isObject()).isTrue();
        assertThat(node.get("raw").get("driverKey").asText()).isEqualTo("12:123");
        assertThat(node.get("details").get("k").asInt()).isEqualTo(1);
    }

    @Test
    void unwrapsDoubleEncodedJsonObjectColumn() {
        JsonNode node = EventHubEventReadRepository.parseJsonColumn(
                objectMapper,
                "\"{\\\"raw\\\":{\\\"driverKey\\\":\\\"12:123\\\"},\\\"details\\\":{\\\"k\\\":1}}\""
        );

        assertThat(node.isObject()).isTrue();
        assertThat(node.get("raw").get("driverKey").asText()).isEqualTo("12:123");
        assertThat(node.get("details").get("k").asInt()).isEqualTo(1);
    }
}
