package cz.senslog.connector.push.rest.senslog.v1;

import cz.senslog.connector.model.config.HostConfig;
import cz.senslog.connector.tools.http.HttpClient;
import cz.senslog.connector.tools.http.HttpCode;
import cz.senslog.connector.tools.http.HttpRequest;
import cz.senslog.connector.tools.http.HttpResponse;
import cz.senslog.connector.model.v1.Observation;
import cz.senslog.connector.model.v1.Record;
import cz.senslog.connector.model.v1.SenslogV1Model;
import org.junit.jupiter.api.Test;

import java.time.*;
import java.util.ArrayList;
import java.util.List;

import static org.mockito.Mockito.*;

class SenslogV1PusherTest {

    @Test
    void push_FullModel_True() {

        HttpClient httpClient = mock(HttpClient.class);
        when(httpClient.send(any(HttpRequest.class)))
                .thenReturn(HttpResponse.newBuilder()
                        .body("true")
                        .status(HttpCode.OK)
                        .build()
                );

        SenslogV1Config config = mock(SenslogV1Config.class);
        when(config.getBaseUrl()).thenReturn("http://test.com");

        SenslogV1Pusher pusher = new SenslogV1Pusher(config, httpClient);

        List<Record> observations = new ArrayList<>();

        Observation observation = new Observation();

        observation.setUnitId(1L);
        observation.setSensorId(1L);
        observation.setValue(42F);
        observation.setTime(OffsetDateTime.of(LocalDateTime.of(1970, Month.JANUARY, 1, 0,0,0), ZoneOffset.UTC));

        observations.add(observation);
        SenslogV1Model model = new SenslogV1Model(observations, null, null);

        pusher.init();
        pusher.push(model);
    }

    @Test
    void push_ErrorResponse_SaveToQueue() {

        HttpClient httpClient = mock(HttpClient.class);
        when(httpClient.send(any(HttpRequest.class)))
                .thenReturn(HttpResponse.newBuilder()
                        .status(HttpCode.FORBIDDEN)
                        .build()
                );

        SenslogV1Config config = mock(SenslogV1Config.class);
        when(config.getBaseUrl()).thenReturn("http://test.com");

        SenslogV1Pusher pusher = new SenslogV1Pusher(config, httpClient);

        List<Record> observations = new ArrayList<>();

        Observation observation = new Observation();

        observation.setUnitId(1L);
        observation.setSensorId(1L);
        observation.setValue(42F);
        observation.setTime(OffsetDateTime.of(LocalDateTime.of(1970, Month.JANUARY, 1, 0,0,0), ZoneOffset.UTC));

        observations.add(observation);
        SenslogV1Model model = new SenslogV1Model(observations, null, null);

        pusher.init();
        pusher.push(model);
        pusher.push(new SenslogV1Model(new ArrayList<>(), null, null));
    }
}