package handlers_test

import (
	"fmt"
	"net/http"
	"otaupdate/handlers"
	"otaupdate/services"
	"testing"

	"github.com/fiskerinc/cloud-services/pkg/common"
	mo "github.com/fiskerinc/cloud-services/pkg/db/queries/mocks"
	th "github.com/fiskerinc/cloud-services/pkg/testhelper"
)

func TestSubscriptionPackageAdd(t *testing.T) {
	mock := mo.MockSubscriptionPackages{}
	services.GetDB().SetSubPackages(&mock)

	tests := []mo.DBHttpTest{
		{
			Name:             "No data",
			Request:          th.MakeTestRequest(http.MethodPost, "http://example.com/subscriptionpackage", nil),
			ExpectedStatus:   http.StatusBadRequest,
			ExpectedResponse: `{"message":"Name required","error":"Bad Request"}`,
		},
		{
			Name: "Valid data",
			Request: th.MakeTestRequest(http.MethodPost, "http://example.com/subscriptionpackage", common.SubscriptionPackage{
				Name: "package_name",
			}),
			ExpectedStatus:   http.StatusOK,
			ExpectedResponse: `{"id":"0557bd1d-76d3-41e5-a44e-13c479e55ab0","name":"package_name"}`,
		},
		{
			Name: "DB error",
			Request: th.MakeTestRequest(http.MethodPost, "http://example.com/subscriptionpackage", common.SubscriptionPackage{
				Name: "package_name",
			}),
			ExpectedStatus:   http.StatusServiceUnavailable,
			ExpectedResponse: `{"message":"something went wrong","error":"Service Unavailable"}`,
			DBTestCase: mo.DBTestCase{
				MockError: fmt.Errorf("something went wrong"),
			},
		},
	}

	mo.RunDBTests(t, tests, handlers.HandleSubscriptionPackageAdd, &mock)
}
