Skip to content

Commit

Permalink
add test for appendSetting
Browse files Browse the repository at this point in the history
Signed-off-by: jkoberg <jkoberg@owncloud.com>
  • Loading branch information
kobergj committed Feb 28, 2022
1 parent e23448f commit 4c22c07
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 3 deletions.
4 changes: 3 additions & 1 deletion settings/pkg/store/metadata/assignments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package store

import (
"log"
"sync"
"testing"

olog "github.com/owncloud/ocis/ocis-pkg/log"
Expand All @@ -16,6 +17,7 @@ var (
s = Store{
Logger: logger,
mdc: mdc,
l: &sync.Mutex{},
}

logger = olog.NewLogger(
Expand Down Expand Up @@ -85,7 +87,7 @@ func init() {
func setupRoles() {
for i := range bundles {
if _, err := s.WriteBundle(bundles[i]); err != nil {
log.Fatal(err)
log.Fatal("error initializing ", err)
}
}
}
Expand Down
18 changes: 17 additions & 1 deletion settings/pkg/store/metadata/bundles.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"

"github.com/gofrs/uuid"
settingsmsg "github.com/owncloud/ocis/protogen/gen/ocis/messages/settings/v0"
)

Expand Down Expand Up @@ -86,7 +87,22 @@ func (s Store) WriteBundle(record *settingsmsg.Bundle) (*settingsmsg.Bundle, err

// AddSettingToBundle adds the given setting to the bundle with the given bundleID.
func (s Store) AddSettingToBundle(bundleID string, setting *settingsmsg.Setting) (*settingsmsg.Setting, error) {
return nil, errors.New("not implemented")
s.Init()
b, err := s.ReadBundle(bundleID)
if err != nil {
// TODO: How to differentiate 'not found'?
b = new(settingsmsg.Bundle)
b.Id = bundleID
b.Type = settingsmsg.Bundle_TYPE_DEFAULT
}

if setting.Id == "" {
setting.Id = uuid.Must(uuid.NewV4()).String()
}

b.Settings = append(b.Settings, setting)
_, err = s.WriteBundle(b)
return setting, err
}

// RemoveSettingFromBundle removes the setting from the bundle with the given ids.
Expand Down
68 changes: 68 additions & 0 deletions settings/pkg/store/metadata/bundles_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package store

import (
"sync"
"testing"

olog "github.com/owncloud/ocis/ocis-pkg/log"
Expand Down Expand Up @@ -101,6 +102,42 @@ var bundleScenarios = []struct {
},
}

var (
appendTestBundleID = "append-test-bundle"

appendTestSetting1 = &settingsmsg.Setting{
Id: "append-test-setting-1",
Description: "test-desc-3",
DisplayName: "test-displayname-3",
Resource: &settingsmsg.Resource{
Type: settingsmsg.Resource_TYPE_SETTING,
Id: setting1,
},
Value: &settingsmsg.Setting_PermissionValue{
PermissionValue: &settingsmsg.Permission{
Operation: settingsmsg.Permission_OPERATION_READ,
Constraint: settingsmsg.Permission_CONSTRAINT_OWN,
},
},
}

appendTestSetting2 = &settingsmsg.Setting{
Id: "append-test-setting-2",
Description: "test-desc-3",
DisplayName: "test-displayname-3",
Resource: &settingsmsg.Resource{
Type: settingsmsg.Resource_TYPE_SETTING,
Id: setting1,
},
Value: &settingsmsg.Setting_PermissionValue{
PermissionValue: &settingsmsg.Permission{
Operation: settingsmsg.Permission_OPERATION_READ,
Constraint: settingsmsg.Permission_CONSTRAINT_OWN,
},
},
}
)

func TestBundles(t *testing.T) {
mdc := NewMDC()
s := Store{
Expand All @@ -110,6 +147,7 @@ func TestBundles(t *testing.T) {
olog.Level("info"),
),

l: &sync.Mutex{},
mdc: mdc,
}

Expand Down Expand Up @@ -147,3 +185,33 @@ func TestBundles(t *testing.T) {
require.Equal(t, settingsmsg.Bundle_TYPE_ROLE, roles[i].Type)
}
}

func TestAppendSetting(t *testing.T) {
mdc := NewMDC()
s := Store{
Logger: olog.NewLogger(
olog.Color(true),
olog.Pretty(true),
olog.Level("info"),
),

l: &sync.Mutex{},
mdc: mdc,
}

// appending to non existing bundle creates new
_, err := s.AddSettingToBundle(appendTestBundleID, appendTestSetting1)
require.NoError(t, err)

b, err := s.ReadBundle(appendTestBundleID)
require.NoError(t, err)
require.Len(t, b.Settings, 1)

_, err = s.AddSettingToBundle(appendTestBundleID, appendTestSetting2)
require.NoError(t, err)

b, err = s.ReadBundle(appendTestBundleID)
require.NoError(t, err)
require.Len(t, b.Settings, 2)

}
4 changes: 3 additions & 1 deletion settings/pkg/store/metadata/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ func (s Store) initMetadataClient() error {
}
}

s.initStore(s)
if s.initStore != nil {
s.initStore(s)
}
return nil
}

Expand Down

0 comments on commit 4c22c07

Please sign in to comment.