package configmdl

import (
	"io/ioutil"
	"testing"

	"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/errormdl"

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

type tomlConfig struct {
	Title           string
	Name            string
	AdditionalField string
	Version         string
}

func TestInitConfig(t *testing.T) {
	var config tomlConfig
	InitConfig("../testingdata/testData/config/config.toml", &config)
	assert.True(t, config.Name == "github.com/OneOfOne/xxhash", "this should not throw error")
}

func TestInitConfigData(t *testing.T) {
	var config tomlConfig
	ba, _ := ioutil.ReadFile("../testingdata/testData/config/config.toml")
	InitConfigData(ba, &config)
	assert.True(t, config.Name == "github.com/OneOfOne/xxhash", "this should not throw error")
}

func TestSaveConfig(t *testing.T) {
	var config tomlConfig
	InitConfig("../testingdata/testData/config/config.toml", &config)
	err := SaveConfig("../testingdata/testData/config/config-save.toml", config)
	assert.NoError(t, err, "This should not throw error")
}

func Test1SaveConfig(t *testing.T) {
	var config tomlConfig
	InitConfig("../testingdata/testData/config/config.toml", &config)
	errormdl.IsTestingNegetiveCaseOn = true
	err := SaveConfig("../testingdata/testData/config/config-save.toml", config)
	errormdl.IsTestingNegetiveCaseOn = false
	assert.Error(t, err, "This should throw error")
}

func Test2SaveConfig(t *testing.T) {
	var config tomlConfig
	InitConfig("../testingdata/testData/config/config.toml", &config)
	errormdl.IsTestingNegetiveCaseOn2 = true
	err := SaveConfig("../testingdata/testData/config/config-save.toml", config)
	errormdl.IsTestingNegetiveCaseOn2 = false
	assert.Error(t, err, "This should throw error")
}

func TestSaveConfigSecure(t *testing.T) {
	var config tomlConfig
	InitConfig("../testingdata/testData/config/config.toml", &config)
	key := "1234567891234567"
	err := SaveConfigSecure("../testingdata/testData/config/config-save-secure.toml", config, []byte(key))
	assert.NoError(t, err, "This should not throw error")
}

func Test1SaveConfigSecure(t *testing.T) {
	var config tomlConfig
	InitConfig("../testingdata/testData/config/config.toml", &config)
	key := "1234567891234567"
	errormdl.IsTestingNegetiveCaseOn = true
	err := SaveConfigSecure("../testingdata/testData/config/config-save-secure.toml", config, []byte(key))
	errormdl.IsTestingNegetiveCaseOn = false
	assert.Error(t, err, "This should throw error")
}

func Test2SaveConfigSecure(t *testing.T) {
	var config tomlConfig
	InitConfig("../testingdata/testData/config/config.toml", &config)
	key := "1234567891234567"
	errormdl.IsTestingNegetiveCaseOn1 = true
	err := SaveConfigSecure("../testingdata/testData/config/config-save-secure.toml", config, []byte(key))
	errormdl.IsTestingNegetiveCaseOn1 = false
	assert.Error(t, err, "This should throw error")
}

func Test3SaveConfigSecure(t *testing.T) {
	var config tomlConfig
	InitConfig("../testingdata/testData/config/config.toml", &config)
	key := "1234567891234567"
	errormdl.IsTestingNegetiveCaseOn2 = true
	err := SaveConfigSecure("../testingdata/testData/config/config-save-secure.toml", config, []byte(key))
	errormdl.IsTestingNegetiveCaseOn2 = false
	assert.Error(t, err, "This should throw error")
}

func TestInitConfigSecure(t *testing.T) {
	var config tomlConfig
	InitConfig("../testingdata/testData/config/config.toml", &config)
	key := "1234567891234567"
	SaveConfigSecure("../testingdata/testData/config/config-save-secure.toml", config, []byte(key))

	_, err := InitConfigSecure("../testingdata/testData/config/config-save-secure.toml", &config, []byte(key))
	assert.NoError(t, err, "This should not throw error")
}

func Test1InitConfigSecure(t *testing.T) {
	var config tomlConfig
	InitConfig("../testingdata/testData/config/config.toml", &config)
	key := "1234567891234567"
	SaveConfigSecure("../testingdata/testData/config/config-save-secure.toml", config, []byte(key))

	_, err := InitConfigSecure("../testingdata/testData/config/config-save-secure.error", &config, []byte(key))
	assert.Error(t, err, "This should throw error")
}

func Test2InitConfigSecure(t *testing.T) {
	// errormdl.IsTestingNegetiveCaseOn = false
	var config tomlConfig
	InitConfig("../testingdata/testData/config/config.toml", &config)
	key := "1234567891234567"
	SaveConfigSecure("../testingdata/testData/config/config-save-secure.toml", config, []byte(key))

	errormdl.IsTestingNegetiveCaseOn2 = true
	_, err := InitConfigSecure("../testingdata/testData/config/config-save-secure.toml", &config, []byte(key))
	errormdl.IsTestingNegetiveCaseOn2 = false
	assert.Error(t, err, "This should throw error")
}