package controller

import (
	"context"
	"net/http"
	"reflect"
	"testing"
	"time"

	"github.com/spf13/viper"
)

const (
	configHome string = "./configs"
	configName string = "ci-testing-gosdn"
	configType string = "toml"
)

func TestRun(t *testing.T) {
	type args struct {
		request string
	}
	tests := []struct {
		name string
		args args
		want interface{}
	}{
		{
			name: "liveliness indicator",
			args: args{request: apiEndpoint + "/livez"},
			want: http.StatusOK,
		},
		{
			name: "readyness indicator",
			args: args{request: apiEndpoint + "/readyz"},
			want: http.StatusOK,
		},
	}

	ctx, cancel := context.WithCancel(context.Background())

	viper.AddConfigPath(configHome)
	viper.SetConfigName(configName)
	viper.SetConfigType(configType)

	go func() {
		if err := Run(ctx); err != nil {
			t.Errorf("Run() error = %v", err)
		}
	}()
	time.Sleep(time.Second)
	t.Run("Controller Start HTTP API", func(t *testing.T) {
		got, err := http.Get(tests[0].args.request)
		if err != nil {
			t.Error(err)
			return
		}
		if !reflect.DeepEqual(got.StatusCode, tests[0].want) {
			t.Errorf("livez got: %v, want %v", got.StatusCode, tests[0].want)
		}
		got, err = http.Get(tests[1].args.request)
		if err != nil {
			t.Error(err)
			return
		}
		if !reflect.DeepEqual(got.StatusCode, tests[1].want) {
			t.Errorf("readyz got: %v, want %v", got.StatusCode, tests[1].want)
		}
	})

	cancel()
}