Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
Merge pull request #820 from fqutishat/817
Browse files Browse the repository at this point in the history
feat: Add close method to VDRI
  • Loading branch information
fqutishat authored Nov 15, 2019
2 parents 0f96901 + 658b7b2 commit 3dc7437
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/framework/aries/api/vdri/vdri.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Registry interface {
Resolve(did string, opts ...ResolveOpts) (*did.Doc, error)
Store(doc *did.Doc) error
Create(method string, opts ...DocOpts) (*did.Doc, error)
Close() error
}

// VDRI verifiable data registry interface
Expand All @@ -29,6 +30,7 @@ type VDRI interface {
Store(doc *did.Doc, by *[]ModifiedBy) error
Build(pubKey *PubKey, opts ...DocOpts) (*did.Doc, error)
Accept(method string) bool
Close() error
}

// ResultType input option can be used to request a certain type of result.
Expand Down
10 changes: 10 additions & 0 deletions pkg/framework/aries/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,16 @@ func (a *Aries) Close() error {
}
}

return a.closeVDRI()
}

func (a *Aries) closeVDRI() error {
if a.vdriRegistry != nil {
if err := a.vdriRegistry.Close(); err != nil {
return fmt.Errorf("vdri registry close failed: %w", err)
}
}

return nil
}

Expand Down
15 changes: 15 additions & 0 deletions pkg/framework/aries/framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,21 @@ func TestFramework(t *testing.T) {
require.Contains(t, err.Error(), "create new vdri peer failed")
})

t.Run("test vdri - close error", func(t *testing.T) {
path, cleanup := generateTempDir(t)
defer cleanup()
dbPath = path

vdri := &mockvdri.MockVDRI{CloseErr: fmt.Errorf("close vdri error")}
aries, err := New(WithVDRI(vdri), WithInboundTransport(&mockInboundTransport{}))
require.NoError(t, err)
require.NotEmpty(t, aries)

err = aries.Close()
require.Error(t, err)
require.Contains(t, err.Error(), "close vdri error")
})

t.Run("test vdri - with default vdri", func(t *testing.T) {
// store peer DID in the store
dbprov, err := leveldb.NewProvider(dbPath)
Expand Down
5 changes: 5 additions & 0 deletions pkg/internal/mock/vdri/mock_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ func (m *MockVDRIRegistry) Resolve(didID string, opts ...vdriapi.ResolveOpts) (*
return m.ResolveValue, nil
}

// Close frees resources being maintained by vdri.
func (m *MockVDRIRegistry) Close() error {
return nil
}

func createDefaultDID() *did.Doc {
const (
didContext = "https://w3id.org/did/v1"
Expand Down
6 changes: 6 additions & 0 deletions pkg/internal/mock/vdri/mock_vdri.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type MockVDRI struct {
StoreErr error
ReadFunc func(didID string, opts ...vdriapi.ResolveOpts) (*did.Doc, error)
BuildFunc func(pubKey *vdriapi.PubKey, opts ...vdriapi.DocOpts) (*did.Doc, error)
CloseErr error
}

// Read did
Expand Down Expand Up @@ -47,3 +48,8 @@ func (m *MockVDRI) Build(pubKey *vdriapi.PubKey, opts ...vdriapi.DocOpts) (*did.
func (m *MockVDRI) Accept(method string) bool {
return m.AcceptValue
}

// Close frees resources being maintained by vdri.
func (m *MockVDRI) Close() error {
return m.CloseErr
}
5 changes: 5 additions & 0 deletions pkg/vdri/httpbinding/vdri.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ func (v *VDRI) Build(pubKey *vdriapi.PubKey, opts ...vdriapi.DocOpts) (*did.Doc,
return nil, errors.New("not supported")
}

// Close frees resources being maintained by vdri.
func (v *VDRI) Close() error {
return nil
}

// Option configures the peer vdri
type Option func(opts *VDRI)

Expand Down
41 changes: 41 additions & 0 deletions pkg/vdri/httpbinding/vdri_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package httpbinding

import (
"testing"

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

func TestVDRI_Close(t *testing.T) {
t.Run("test success", func(t *testing.T) {
v, err := New("/did:example:334455")
require.NoError(t, err)
require.NoError(t, v.Close())
})
}

func TestVDRI_Store(t *testing.T) {
t.Run("test success", func(t *testing.T) {
v, err := New("/did:example:334455")
require.NoError(t, err)
err = v.Store(nil, nil)
require.Error(t, err)
require.Contains(t, err.Error(), "not supported")
})
}

func TestVDRI_Build(t *testing.T) {
t.Run("test success", func(t *testing.T) {
v, err := New("/did:example:334455")
require.NoError(t, err)
_, err = v.Build(nil)
require.Error(t, err)
require.Contains(t, err.Error(), "not supported")
})
}
5 changes: 5 additions & 0 deletions pkg/vdri/peer/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ func (v *VDRI) Get(id string) (*did.Doc, error) {
return document, nil
}

// Close frees resources being maintained by vdri.
func (v *VDRI) Close() error {
return nil
}

func (v *VDRI) getDeltas(id string) ([]docDelta, error) {
val, err := v.store.Get(id)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions pkg/vdri/peer/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,11 @@ func TestPeerDIDStore(t *testing.T) {
require.Nil(t, v)
require.Contains(t, err.Error(), "delta data fetch from store failed")
}

func TestVDRI_Close(t *testing.T) {
t.Run("test success", func(t *testing.T) {
v, err := New(&storage.MockStoreProvider{})
require.NoError(t, err)
require.NoError(t, v.Close())
})
}
11 changes: 11 additions & 0 deletions pkg/vdri/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,17 @@ func (r *Registry) Store(doc *diddoc.Doc) error {
return method.Store(doc, nil)
}

// Close frees resources being maintained by vdri.
func (r *Registry) Close() error {
for _, v := range r.vdri {
if err := v.Close(); err != nil {
return fmt.Errorf("close vdri: %w", err)
}
}

return nil
}

func (r *Registry) resolveVDRI(method string) (vdriapi.VDRI, error) {
for _, v := range r.vdri {
if v.Accept(method) {
Expand Down
14 changes: 14 additions & 0 deletions pkg/vdri/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ import (
mockvdri "github.com/hyperledger/aries-framework-go/pkg/internal/mock/vdri"
)

func TestRegistry_Close(t *testing.T) {
t.Run("test success", func(t *testing.T) {
registry := New(&mockprovider.Provider{})
require.NoError(t, registry.Close())
})
t.Run("test error", func(t *testing.T) {
registry := New(&mockprovider.Provider{},
WithVDRI(&mockvdri.MockVDRI{CloseErr: fmt.Errorf("close error")}))
err := registry.Close()
require.Error(t, err)
require.Contains(t, err.Error(), "close error")
})
}

func TestRegistry_Resolve(t *testing.T) {
t.Run("test invalid did input", func(t *testing.T) {
registry := New(&mockprovider.Provider{})
Expand Down

0 comments on commit 3dc7437

Please sign in to comment.