/******************************************************************************
 * 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: configmapimpl_test.go
 * @Description:
 * @Author: jiangxunyu@pingcap.com
 * @Version: 1.0.0
 * @Date: 2022/6/16 18:07
*******************************************************************************/

package etcd

import (
	"context"
	"encoding/json"
	"testing"

	"github.com/tidbcloud/observability/pkg/globalservice/structs"

	"github.com/stretchr/testify/assert"
	testClient "k8s.io/client-go/kubernetes/fake"
)

var configMapMetaService = NewConfigMapMetaService(testClient.NewSimpleClientset())

func Test_CreateMeta(t *testing.T) {
	t.Run("success", func(t *testing.T) {
		err := configMapMetaService.CreateMeta(context.TODO(), "testCreateConfigmap", `
{
	"vendors": {
		"aws": [
			{
				"region": "us-west-2",
				"dnsAddress": "http://127.0.0.1:8082"
			}
		]
	}
}
`)
		assert.NoError(t, err)
	})
}

func Test_DetailMeta(t *testing.T) {
	nsAndCmName := "testDetailMetaConfigmap"
	err := configMapMetaService.CreateMeta(context.TODO(), nsAndCmName, `
{
	"vendors": {
		"aws": [
			{
				"region": "us-west-2",
				"dnsAddress": "http://127.0.0.1:8082"
			}
		]
	}
}
`)
	assert.NoError(t, err)

	t.Run("success", func(t *testing.T) {
		metaStr, err := configMapMetaService.DetailMeta(context.TODO(), nsAndCmName)
		assert.NoError(t, err)

		var meta structs.GlobalServiceMeta
		err = json.Unmarshal([]byte(metaStr), &meta)
		assert.NoError(t, err)

		assert.Equal(t, "us-west-2", meta.Vendors["aws"][0].Region)
	})

	t.Run("error", func(t *testing.T) {
		_, err := configMapMetaService.DetailMeta(context.TODO(), "testErrorConfigmap")
		assert.Error(t, err)
	})
}

func Test_UpdateMeta(t *testing.T) {
	nsAndCmName := "testUpdateMetaConfigmap"
	err := configMapMetaService.CreateMeta(context.TODO(), nsAndCmName, `
{
	"vendors": {
		"aws": [
			{
				"region": "us-west-2",
				"dnsAddress": "http://127.0.0.1:8082"
			}
		]
	}
}
`)
	assert.NoError(t, err)

	t.Run("success", func(t *testing.T) {
		err = configMapMetaService.UpdateMeta(context.TODO(), nsAndCmName, `
{
	"vendors": {
		"aws": [
			{
				"region": "us-west-1",
				"dnsAddress": "https://192.168.1.1:8082"
			}
		]
	}
}
`)
		assert.NoError(t, err)
	})

	t.Run("error", func(t *testing.T) {
		err = configMapMetaService.UpdateMeta(context.TODO(), "testErrorConfigmap", "{}")
		assert.Error(t, err)
	})
}

func Test_WatchMeta(t *testing.T) {
	t.Run("success", func(t *testing.T) {
		meta, err := configMapMetaService.WatchMeta(context.TODO(), "test")
		assert.NoError(t, err)
		assert.NotNil(t, meta)
	})
}
