package handlers_test

import (
	"net/http"
	"testing"

	"otaupdate/handlers"
	"otaupdate/services"

	"github.com/fiskerinc/cloud-services/pkg/mongo"
	th "github.com/fiskerinc/cloud-services/pkg/testhelper"
)

func TestFleetGetList(t *testing.T) {
	client, err := services.GetMongoClient()
	if err != nil {
		t.Error(err)
		return
	}
	mockMongo := mongo.NewFleetsCollection(
		&mongo.MockCollection{
			FindObject: []mongo.Fleet{
				{
					Name: "TESTFLEET",
				},
			},
		},
	)
	client.SetFleets(mockMongo)

	tests := []th.BasicHttpTest{
		{
			Name:             "Valid data",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/fleets", nil),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: `{"data":[{"name":"TESTFLEET","log_level":"trace","canbus":{"enabled":false,"data_logger_enabled":false,"dtc_enabled":false},"idps_enabled":false,"tags":null,"vehicles":null,"vehicles_count":0}]}`,
		},
		{
			Name:             "Valid data with max limit",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/fleets?limit=100", nil),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: `{"data":[{"name":"TESTFLEET","log_level":"trace","canbus":{"enabled":false,"data_logger_enabled":false,"dtc_enabled":false},"idps_enabled":false,"tags":null,"vehicles":null,"vehicles_count":0}]}`,
		},
		{
			Name:             "Wrong limit, -100",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/fleets?limit=-100", nil),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"Limit less than 0","error":"Bad Request"}`,
		},
		{
			Name:             "Wrong limit, 1000",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/fleets?limit=1000", nil),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"Limit greater than 100","error":"Bad Request"}`,
		},
	}

	th.RunParamHttpTests(t, tests, handlers.HandleFleetGetList, "/fleets")
}
