// Copyright © 2020 Cisco Systems, Inc. and/or its affiliates
//
// 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.

package kafka

import (
	"testing"

	"github.com/Shopify/sarama"

	properties "github.com/banzaicloud/koperator/properties/pkg"
)

func TestCollectNonIdenticalConfigsFromResponse(t *testing.T) {
	testCases := []struct {
		Description  string
		Response     []*sarama.ConfigEntry
		BrokerConfig string
		Result       bool
	}{
		{
			Description: "matching configs",
			Response: []*sarama.ConfigEntry{
				{
					Name:  "config1",
					Value: "value1",
				},
				{
					Name:  "config2",
					Value: "value2",
				},
			},
			BrokerConfig: `config1=value1
config2=value2
`,
			Result: false,
		},
		{
			Description: "if a config is deleted, do not update the broker",
			Response: []*sarama.ConfigEntry{
				{
					Name:  "config1",
					Value: "value1",
				},
				{
					Name:  "config2",
					Value: "value2",
				},
				{
					Name:  "config3",
					Value: "value3",
				},
			},
			BrokerConfig: `config1=value1
config2=value2
`,
			Result: false,
		},
		{
			Description: "config value updated",
			Response: []*sarama.ConfigEntry{
				{
					Name:  "config1",
					Value: "value1",
				},
				{
					Name:  "config2",
					Value: "value2",
				},
			},
			BrokerConfig: `config1=value1
config2=value3
`,
			Result: true,
		},
	}
	for _, testCase := range testCases {
		brokerConf, err := properties.NewFromString(testCase.BrokerConfig)
		if err != nil {
			t.Errorf("failed parsing Properties from string: %v\n", testCase.BrokerConfig)
		}
		if shouldUpdatePerBrokerConfig(testCase.Response, brokerConf) != testCase.Result {
			t.Errorf("wrong result generated by test case: %s", testCase.Description)
		}
	}
}
