package click.kamil.springstatemachineexporter.analysis.model;

import org.junit.jupiter.api.Test;

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

class FlowStepJsonTest {

    private final com.fasterxml.jackson.databind.ObjectMapper mapper =
            new com.fasterxml.jackson.databind.ObjectMapper();

    @Test
    void shouldDeserializeLegacyStringStep() throws Exception {
        FlowStep step = mapper.readValue("\"PAY\"", FlowStep.class);
        assertThat(step.getEvent()).isEqualTo("PAY");
        assertThat(step.getSource()).isNull();
    }

    @Test
    void shouldDeserializeStructuredStep() throws Exception {
        FlowStep step = mapper.readValue(
                "{\"source\":\"com.example.order.OrderState.PAID\",\"event\":\"com.example.order.OrderEvent.SHIP\"}",
                FlowStep.class);
        assertThat(step.getSource()).isEqualTo("com.example.order.OrderState.PAID");
        assertThat(step.getEvent()).isEqualTo("com.example.order.OrderEvent.SHIP");
    }

    @Test
    void shouldSerializeEventOnlyStepAsString() throws Exception {
        String json = mapper.writeValueAsString(FlowStep.ofEvent("PAY"));
        assertThat(json).isEqualTo("\"PAY\"");
    }

    @Test
    void shouldSerializeStructuredStepAsObject() throws Exception {
        FlowStep step = FlowStep.builder()
                .source("com.example.order.OrderState.PAID")
                .event("com.example.order.OrderEvent.SHIP")
                .linkKey("OrderState_PAID__OrderEvent_SHIP")
                .build();
        String json = mapper.writeValueAsString(step);
        assertThat(json).contains("\"source\"");
        assertThat(json).contains("\"event\"");
        assertThat(json).contains("\"linkKey\"");
    }
}
