package com.lingniu.ingest.gb32960app.actuator;

import com.lingniu.ingest.protocol.gb32960.inbound.Gb32960ConnectionDiagnostics;
import com.lingniu.ingest.protocol.gb32960.model.CommandType;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;

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

class Gb32960SessionsEndpointTest {

    @Test
    void returnsActiveGb32960Sessions() {
        Gb32960ConnectionDiagnostics diagnostics = new Gb32960ConnectionDiagnostics();
        diagnostics.opened("session-1", "127.0.0.1:65469");
        diagnostics.received("session-1", CommandType.PLATFORM_LOGIN, "");
        diagnostics.platformLoginAccepted("session-1", "Hyundai");

        Gb32960SessionsEndpoint endpoint = new Gb32960SessionsEndpoint(diagnostics);

        Map<String, Object> result = endpoint.sessions();

        assertThat(result).containsEntry("activeCount", 1);
        assertThat(result.get("sessions")).isInstanceOf(List.class);
        assertThat((List<?>) result.get("sessions")).singleElement()
                .hasFieldOrPropertyWithValue("sessionId", "session-1")
                .hasFieldOrPropertyWithValue("peer", "127.0.0.1:65469")
                .hasFieldOrPropertyWithValue("lastCommand", "PLATFORM_LOGIN")
                .hasFieldOrPropertyWithValue("platformAccount", "Hyundai");
    }
}
