package dnsprovider

/*
Copyright 2024 The external-dns-infoblox-webhook Contributors.

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.

Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic
*/

import (
	"testing"

	"github.com/AbsaOSS/external-dns-infoblox-webhook/cmd/webhook/init/configuration"

	log "github.com/sirupsen/logrus"
	"github.com/stretchr/testify/assert"
)

func TestInit(t *testing.T) {
	log.SetLevel(log.DebugLevel)

	cases := []struct {
		name          string
		config        configuration.Config
		env           map[string]string
		expectedError string
	}{
		{
			name:   "minimal config for infoblox provider",
			config: configuration.Config{},
			env: map[string]string{
				"INFOBLOX_WAPI_USER":     "user123",
				"INFOBLOX_WAPI_PASSWORD": "password",
				"INFOBLOX_VERSION":       "2.7.1",
			},
		},
		{
			name: "domain filter config for infoblox provider",
			config: configuration.Config{
				DomainFilter:   []string{"domain.com"},
				ExcludeDomains: []string{"sub.domain.com"},
			},
			env: map[string]string{
				"INFOBLOX_WAPI_USER":     "user123",
				"INFOBLOX_WAPI_PASSWORD": "password",
				"INFOBLOX_VERSION":       "2.7.1",
			},
		},
		{
			name:          "empty configuration",
			config:        configuration.Config{},
			expectedError: "expecting error",
		},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			for k, v := range tc.env {
				t.Setenv(k, v)
			}

			dnsProvider, err := Init(tc.config)

			if tc.expectedError != "" {
				assert.Error(t, err, "configuration error, no mandatory Environment variables set")
				return
			}

			assert.NoErrorf(t, err, "error creating provider")
			assert.NotNil(t, dnsProvider)
		})
	}
}
