diff --git a/store/state/version_config.go b/store/state/version_config.go index a6c7d3f..8ed53aa 100644 --- a/store/state/version_config.go +++ b/store/state/version_config.go @@ -62,11 +62,18 @@ func (s *VersionConfigStore) Delete(id string) { func (s VersionConfigStore) GetByConfigId(id string) []entity.VersionConfig { response, found := s.VersionByConfigId[id] if found { - return response + return s.reverse(response) } return []entity.VersionConfig{} } +func (s VersionConfigStore) reverse(cfg []entity.VersionConfig) []entity.VersionConfig { + for i, j := 0, len(cfg)-1; i < j; i, j = i+1, j-1 { + cfg[i], cfg[j] = cfg[j], cfg[i] + } + return cfg +} + func NewVersionConfigStore() *VersionConfigStore { return &VersionConfigStore{ VersionByConfigId: make(map[string][]entity.VersionConfig), diff --git a/store/state/version_config_test.go b/store/state/version_config_test.go new file mode 100644 index 0000000..bfc2ad7 --- /dev/null +++ b/store/state/version_config_test.go @@ -0,0 +1,38 @@ +package state + +import ( + "fmt" + "testing" + + "github.com/integration-system/isp-lib/v2/config" + "github.com/stretchr/testify/assert" + "isp-config-service/conf" + "isp-config-service/entity" +) + +func TestVersionConfigStore(t *testing.T) { + a := assert.New(t) + + type example struct { + cfg entity.VersionConfig + removedId string + } + + config.UnsafeSet(&conf.Configuration{VersionConfigCount: 0}) + vcs := NewVersionConfigStore() + for _, e := range []example{ + {cfg: entity.VersionConfig{Id: "1", ConfigId: "1"}, removedId: ""}, + {cfg: entity.VersionConfig{Id: "2", ConfigId: "1"}, removedId: ""}, + {cfg: entity.VersionConfig{Id: "3", ConfigId: "1"}, removedId: ""}, + {cfg: entity.VersionConfig{Id: "4", ConfigId: "1"}, removedId: ""}, + {cfg: entity.VersionConfig{Id: "5", ConfigId: "1"}, removedId: ""}, + {cfg: entity.VersionConfig{Id: "6", ConfigId: "1"}, removedId: "1"}, + {cfg: entity.VersionConfig{Id: "7", ConfigId: "1"}, removedId: "2"}, + } { + removedId := vcs.Update(e.cfg) + a.Equal(e.removedId, removedId) + } + for i, versionConfig := range vcs.GetByConfigId("1") { + a.Equal(fmt.Sprintf("%d", 7-i), versionConfig.Id) + } +}