From 071289dce7aa1a09ab573ffc2e4fa0e83eefba4e Mon Sep 17 00:00:00 2001 From: Alexander Dammeier Date: Tue, 15 Oct 2024 16:58:53 +0200 Subject: [PATCH] #81 remove duplicate code and add tests --- pkg/domain/config.go | 45 +--------------------------- pkg/domain/config_test.go | 62 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 44 deletions(-) diff --git a/pkg/domain/config.go b/pkg/domain/config.go index 5afe258..a5bf299 100644 --- a/pkg/domain/config.go +++ b/pkg/domain/config.go @@ -25,10 +25,7 @@ type DoguConfig struct { Absent []common.DoguConfigKey } -type SensitiveDoguConfig struct { - Present map[common.SensitiveDoguConfigKey]common.SensitiveDoguConfigValue - Absent []common.SensitiveDoguConfigKey -} +type SensitiveDoguConfig = DoguConfig type GlobalConfig struct { Present map[common.GlobalConfigKey]common.GlobalConfigValue @@ -183,46 +180,6 @@ func (config DoguConfig) validate(referencedDoguName common.SimpleDoguName) erro return errors.Join(errs...) } -func (config SensitiveDoguConfig) validate(referencedDoguName common.SimpleDoguName) error { - var errs []error - for configKey := range config.Present { - err := configKey.Validate() - if err != nil { - errs = append(errs, fmt.Errorf("present dogu config key invalid: %w", err)) - } - - // validate that all keys are of the same dogu - if referencedDoguName != configKey.DoguName { - errs = append(errs, fmt.Errorf("present %s does not match superordinate dogu name %q", configKey, referencedDoguName)) - } - } - - for _, configKey := range config.Absent { - err := configKey.Validate() - if err != nil { - errs = append(errs, fmt.Errorf("absent dogu config key invalid: %w", err)) - } - - // absent keys cannot be present - _, isPresent := config.Present[configKey] - if isPresent { - errs = append(errs, fmt.Errorf("%s cannot be present and absent at the same time", configKey)) - } - - // validate that all keys are of the same dogu - if referencedDoguName != configKey.DoguName { - errs = append(errs, fmt.Errorf("absent %s does not match superordinate dogu name %q", configKey, referencedDoguName)) - } - } - - absentDuplicates := util.GetDuplicates(config.Absent) - if len(absentDuplicates) > 0 { - errs = append(errs, fmt.Errorf("absent dogu config should not contain duplicate keys: %v", absentDuplicates)) - } - - return errors.Join(errs...) -} - func (config GlobalConfig) validate() error { var errs []error for configKey := range config.Present { diff --git a/pkg/domain/config_test.go b/pkg/domain/config_test.go index eb4cdfa..a53e1f1 100644 --- a/pkg/domain/config_test.go +++ b/pkg/domain/config_test.go @@ -406,3 +406,65 @@ func TestCombinedDoguConfig_validate(t *testing.T) { assert.ErrorContains(t, err, "dogu config key key \"my/key1\" of dogu \"dogu1\" cannot be in normal and sensitive configuration at the same time") } + +func TestConfig_GetDogusWithChangedConfig(t *testing.T) { + presentConfig := map[common.DoguConfigKey]common.DoguConfigValue{ + dogu1Key1: "val", + } + AbsentConfig := []common.DoguConfigKey{ + dogu1Key1, + } + + normalConfig := DoguConfig{ + Present: presentConfig, + Absent: AbsentConfig, + } + + combinedConfig := CombinedDoguConfig{ + DoguName: dogu1, + Config: normalConfig, + SensitiveConfig: struct { + Present map[common.DoguConfigKey]common.DoguConfigValue + Absent []common.DoguConfigKey + }{}, + } + + config := Config{ + Dogus: map[common.SimpleDoguName]CombinedDoguConfig{ + dogu1: combinedConfig, + }, + Global: GlobalConfig{}, + } + assert.Equal(t, []common.SimpleDoguName{dogu1}, config.GetDogusWithChangedConfig()) +} + +func TestConfig_GetDogusWithChangedSensitiveConfig(t *testing.T) { + presentConfig := map[common.DoguConfigKey]common.DoguConfigValue{ + dogu1Key1: "val", + } + AbsentConfig := []common.DoguConfigKey{ + dogu1Key1, + } + + normalConfig := DoguConfig{ + Present: presentConfig, + Absent: AbsentConfig, + } + + combinedConfig := CombinedDoguConfig{ + DoguName: dogu1, + Config: normalConfig, + SensitiveConfig: struct { + Present map[common.DoguConfigKey]common.DoguConfigValue + Absent []common.DoguConfigKey + }{}, + } + + config := Config{ + Dogus: map[common.SimpleDoguName]CombinedDoguConfig{ + dogu1: combinedConfig, + }, + Global: GlobalConfig{}, + } + assert.Equal(t, []common.SimpleDoguName{dogu1}, config.GetDogusWithChangedConfig()) +}