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 TestFleetAdd(t *testing.T) {
	client, err := services.GetMongoClient()
	if err != nil {
		t.Error(err)
		return
	}
	mockMongo := mongo.NewFleetsCollection(&mongo.MockCollection{})
	client.SetFleets(mockMongo)

	tests := []th.BasicHttpTest{
		{
			Name:             "No data",
			Request:          th.MakeTestRequest(http.MethodPost, "http://example.com/fleet", nil),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"required required","error":"Bad Request"}`,
		},
		{
			Name:             "Invalid data",
			Request:          th.MakeTestRequest(http.MethodPost, "http://example.com/fleet", mongo.Fleet{}),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"required required","error":"Bad Request"}`,
		},
		{
			Name:             "Valid data",
			Request:          th.MakeTestRequest(http.MethodPost, "http://example.com/fleet", mongo.Fleet{Name: "TEST-FLEET"}),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: `{"name":"TEST-FLEET","log_level":"trace","canbus":{"enabled":false,"data_logger_enabled":false,"dtc_enabled":false},"idps_enabled":false,"tags":null,"vehicles":null,"vehicles_count":0}`,
		},
	}

	th.RunParamHttpTests(t, tests, handlers.HandleFleetAdd, "/fleet")
}
