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 TestVehicleFleetGetList(t *testing.T) {
	client, err := services.GetMongoClient()
	if err != nil {
		t.Error(err)
		return
	}
	mockMongo := mongo.NewVehiclesCollection(
		&mongo.MockCollection{
			AggregateObject: []struct {
				Fleets []string `bson:"fleets"`
			}{{Fleets: []string{"fleet1", "fleet2"}}}},
	)
	client.SetVehicles(mockMongo)

	tests := []th.BasicHttpTest{
		{
			Name:             "Invalid vin parameter",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/vehicle/TESTVIN123/fleets", nil),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"primary key required","error":"Bad Request"}`,
		},
		{
			Name:             "Valid data",
			Request:          th.MakeTestRequest(http.MethodGet, "http://example.com/vehicle/2D4FV48T95H646760/fleets", nil),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: `{"data":["fleet1","fleet2"]}`,
		},
	}

	th.RunParamHttpTests(t, tests, handlers.HandleVehicleFleetGetList, "/vehicle/:vin/fleets")
}
