/******************************************************************************
 * Copyright (c)  2022 PingCAP                                                *
 * Licensed under the Apache License, Version 2.0 (the "License");            *
 * you may not use this file except in compliance with the License.           *
 * You may obtain a copy of the License at                                    *
 *                                                                            *
 * http://www.apache.org/licenses/LICENSE-2.0                                 *
 *                                                                            *
 * Unless required by applicable law or agreed to in writing, software        *
 * distributed under the License is distributed on an "AS IS" BASIS,          *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
 * See the License for the specific language governing permissions and        *
 * limitations under the License.                                             *
 ******************************************************************************/

/*******************************************************************************
 * @File: serviceapi_test.go
 * @Description:
 * @Author: jiangxunyu@pingcap.com
 * @Version: 1.0.0
 * @Date: 2022/6/30 18:19
*******************************************************************************/

package globalapi

import (
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"
	"net/url"
	"testing"

	"github.com/gin-gonic/gin"
	"github.com/tidbcloud/observability/pkg/common/structs"

	"github.com/tidbcloud/observability/pkg/common/constants"
	"github.com/tidbcloud/observability/pkg/common/errors"
	"github.com/tidbcloud/observability/pkg/common/message"

	"github.com/stretchr/testify/assert"
)

func Test_ServiceInfo(t *testing.T) {
	w := httptest.NewRecorder()
	req, _ := http.NewRequest("GET", "/api/v1/", nil)
	testEngine.ServeHTTP(w, req)
	assert.Equal(t, http.StatusOK, w.Code)

	res := w.Result()
	defer res.Body.Close()

	data, err := io.ReadAll(res.Body)
	assert.NoError(t, err)

	result := &message.Result{
		Data: &message.GetServiceInfoResp{},
	}
	err = json.Unmarshal(data, result)
	assert.NoError(t, err)

	assert.Equal(t, errors.SUCCESS, result.Code)
	assert.Equal(t, constants.ObservabilityGlobalServer, result.Data.(*message.GetServiceInfoResp).ServiceName)
	fmt.Println(result.Data.(*message.GetServiceInfoResp))
	assert.Empty(t, result.Data.(*message.GetServiceInfoResp).AvailableSpecs)
}

func Test_ServiceHealth(t *testing.T) {
	w := httptest.NewRecorder()
	c, _ := gin.CreateTestContext(w)

	// set context
	c.Set(structs.GlobalServerRequestParametersKey, requestParameters)

	c.Request = &http.Request{
		Method: http.MethodGet,
		URL: &url.URL{
			Path:     "/api/v1",
			RawQuery: "vendor=aws&region=us-west-2",
		},
	}
	t.Setenv("AWS_ROLE_ARN", "arn:aws:iam::1234567890:role/test")
	t.Setenv("AWS_WEB_IDENTITY_TOKEN_FILE", "./clusterapi_test.go")
	t.Setenv("AWS_REGION", "us-west-2")

	ServiceHealth(c)
	assert.Equal(t, http.StatusOK, w.Code)
	assert.NotEmpty(t, w.Body.String())
}
