Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9191,14 +9191,14 @@ paths:
description: Success
default:
description: ""
/network/nodes/{nid}:
/network/nodes/{nameOrId}:
get:
description: 'TODO: Description'
operationId: getNetworkNode
parameters:
- description: 'TODO: Description'
in: path
name: nid
name: nameOrId
required: true
schema:
type: string
Expand Down Expand Up @@ -9592,14 +9592,14 @@ paths:
description: Success
default:
description: ""
/network/organizations/{oid}:
/network/organizations/{nameOrId}:
get:
description: 'TODO: Description'
operationId: getNetworkOrg
parameters:
- description: 'TODO: Description'
in: path
name: oid
name: nameOrId
required: true
schema:
type: string
Expand Down
6 changes: 3 additions & 3 deletions internal/apiserver/route_get_net_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ import (

var getNetworkNode = &oapispec.Route{
Name: "getNetworkNode",
Path: "network/nodes/{nid}",
Path: "network/nodes/{nameOrId}",
Method: http.MethodGet,
PathParams: []*oapispec.PathParam{
{Name: "nid", Description: i18n.MsgTBD},
{Name: "nameOrId", Description: i18n.MsgTBD},
},
QueryParams: nil,
FilterFactory: nil,
Expand All @@ -38,7 +38,7 @@ var getNetworkNode = &oapispec.Route{
JSONOutputValue: func() interface{} { return &fftypes.Identity{} },
JSONOutputCodes: []int{http.StatusOK},
JSONHandler: func(r *oapispec.APIRequest) (output interface{}, err error) {
output, err = getOr(r.Ctx).NetworkMap().GetNodeByID(r.Ctx, r.PP["nid"])
output, err = getOr(r.Ctx).NetworkMap().GetNodeByNameOrID(r.Ctx, r.PP["nameOrId"])
return output, err
},
}
2 changes: 1 addition & 1 deletion internal/apiserver/route_get_net_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestGetNode(t *testing.T) {
req.Header.Set("Content-Type", "application/json; charset=utf-8")
res := httptest.NewRecorder()

nmn.On("GetNodeByID", mock.Anything, "node12345").
nmn.On("GetNodeByNameOrID", mock.Anything, "node12345").
Return(&fftypes.Identity{}, nil)
r.ServeHTTP(res, req)

Expand Down
6 changes: 3 additions & 3 deletions internal/apiserver/route_get_net_org.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ import (

var getNetworkOrg = &oapispec.Route{
Name: "getNetworkOrg",
Path: "network/organizations/{oid}",
Path: "network/organizations/{nameOrId}",
Method: http.MethodGet,
PathParams: []*oapispec.PathParam{
{Name: "oid", Description: i18n.MsgTBD},
{Name: "nameOrId", Description: i18n.MsgTBD},
},
QueryParams: nil,
FilterFactory: nil,
Expand All @@ -38,7 +38,7 @@ var getNetworkOrg = &oapispec.Route{
JSONOutputValue: func() interface{} { return &fftypes.Identity{} },
JSONOutputCodes: []int{http.StatusOK},
JSONHandler: func(r *oapispec.APIRequest) (output interface{}, err error) {
output, err = getOr(r.Ctx).NetworkMap().GetOrganizationByID(r.Ctx, r.PP["oid"])
output, err = getOr(r.Ctx).NetworkMap().GetOrganizationByNameOrID(r.Ctx, r.PP["nameOrId"])
return output, err
},
}
2 changes: 1 addition & 1 deletion internal/apiserver/route_get_net_org_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestGetOrg(t *testing.T) {
req.Header.Set("Content-Type", "application/json; charset=utf-8")
res := httptest.NewRecorder()

nmn.On("GetOrganizationByID", mock.Anything, "org12345").
nmn.On("GetOrganizationByNameOrID", mock.Anything, "org12345").
Return(&fftypes.Identity{}, nil)
r.ServeHTTP(res, req)

Expand Down
46 changes: 26 additions & 20 deletions internal/networkmap/data_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,26 @@ import (
"github.com/hyperledger/firefly/pkg/fftypes"
)

func (nm *networkMap) GetOrganizationByID(ctx context.Context, id string) (*fftypes.Identity, error) {
u, err := fftypes.ParseUUID(ctx, id)
if err != nil {
return nil, err
}
o, err := nm.database.GetIdentityByID(ctx, u)
func (nm *networkMap) GetOrganizationByNameOrID(ctx context.Context, nameOrID string) (org *fftypes.Identity, err error) {
u, err := fftypes.ParseUUID(ctx, nameOrID)
if err != nil {
if err := fftypes.ValidateFFNameField(ctx, nameOrID, "name"); err != nil {
return nil, err
}
if org, err = nm.database.GetIdentityByName(ctx, fftypes.IdentityTypeOrg, fftypes.SystemNamespace, nameOrID); err != nil {
return nil, err
}
} else if org, err = nm.database.GetIdentityByID(ctx, u); err != nil {
return nil, err
}
if o == nil {
if org == nil {
return nil, i18n.NewError(ctx, i18n.Msg404NotFound)
}
if o.Type != fftypes.IdentityTypeOrg {
log.L(ctx).Warnf("Identity '%s' (%s) is not an org identity", o.DID, o.ID)
if org.Type != fftypes.IdentityTypeOrg {
log.L(ctx).Warnf("Identity '%s' (%s) is not an org identity", org.DID, org.ID)
return nil, nil
}
return o, nil
return org, nil
}

func (nm *networkMap) GetOrganizations(ctx context.Context, filter database.AndFilter) ([]*fftypes.Identity, *database.FilterResult, error) {
Expand All @@ -50,23 +53,26 @@ func (nm *networkMap) GetOrganizations(ctx context.Context, filter database.AndF
return nm.database.GetIdentities(ctx, filter)
}

func (nm *networkMap) GetNodeByID(ctx context.Context, id string) (*fftypes.Identity, error) {
u, err := fftypes.ParseUUID(ctx, id)
if err != nil {
return nil, err
}
n, err := nm.database.GetIdentityByID(ctx, u)
func (nm *networkMap) GetNodeByNameOrID(ctx context.Context, nameOrID string) (node *fftypes.Identity, err error) {
u, err := fftypes.ParseUUID(ctx, nameOrID)
if err != nil {
if err := fftypes.ValidateFFNameField(ctx, nameOrID, "name"); err != nil {
return nil, err
}
if node, err = nm.database.GetIdentityByName(ctx, fftypes.IdentityTypeNode, fftypes.SystemNamespace, nameOrID); err != nil {
return nil, err
}
} else if node, err = nm.database.GetIdentityByID(ctx, u); err != nil {
return nil, err
}
if n == nil {
if node == nil {
return nil, i18n.NewError(ctx, i18n.Msg404NotFound)
}
if n.Type != fftypes.IdentityTypeNode {
log.L(ctx).Warnf("Identity '%s' (%s) is not a node identity", n.DID, n.ID)
if node.Type != fftypes.IdentityTypeNode {
log.L(ctx).Warnf("Identity '%s' (%s) is not a node identity", node.DID, node.ID)
return nil, nil
}
return n, nil
return node, nil
}

func (nm *networkMap) GetNodes(ctx context.Context, filter database.AndFilter) ([]*fftypes.Identity, *database.FilterResult, error) {
Expand Down
60 changes: 38 additions & 22 deletions internal/networkmap/data_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,98 +27,114 @@ import (
"github.com/stretchr/testify/mock"
)

func TestGetOrganizationByIDOk(t *testing.T) {
func TestGetOrganizationByNameOrIDOk(t *testing.T) {
nm, cancel := newTestNetworkmap(t)
defer cancel()
id := fftypes.NewUUID()
nm.database.(*databasemocks.Plugin).On("GetIdentityByID", nm.ctx, id).
Return(&fftypes.Identity{IdentityBase: fftypes.IdentityBase{ID: id, Type: fftypes.IdentityTypeOrg}}, nil)
res, err := nm.GetOrganizationByID(nm.ctx, id.String())
res, err := nm.GetOrganizationByNameOrID(nm.ctx, id.String())
assert.NoError(t, err)
assert.Equal(t, *id, *res.ID)
}

func TestGetOrganizationByIDNotOrg(t *testing.T) {
func TestGetOrganizationByNameOrIDNotOrg(t *testing.T) {
nm, cancel := newTestNetworkmap(t)
defer cancel()
id := fftypes.NewUUID()
nm.database.(*databasemocks.Plugin).On("GetIdentityByID", nm.ctx, id).
Return(&fftypes.Identity{IdentityBase: fftypes.IdentityBase{ID: id, Type: fftypes.IdentityTypeNode}}, nil)
res, err := nm.GetOrganizationByID(nm.ctx, id.String())
res, err := nm.GetOrganizationByNameOrID(nm.ctx, id.String())
assert.NoError(t, err)
assert.Nil(t, res)
}

func TestGetOrganizationByIDNotFound(t *testing.T) {
func TestGetOrganizationByNameOrIDNotFound(t *testing.T) {
nm, cancel := newTestNetworkmap(t)
defer cancel()
id := fftypes.NewUUID()
nm.database.(*databasemocks.Plugin).On("GetIdentityByID", nm.ctx, id).Return(nil, nil)
_, err := nm.GetOrganizationByID(nm.ctx, id.String())
_, err := nm.GetOrganizationByNameOrID(nm.ctx, id.String())
assert.Regexp(t, "FF10109", err)
}

func TestGetOrganizationByIDError(t *testing.T) {
func TestGetOrganizationByNameOrIDError(t *testing.T) {
nm, cancel := newTestNetworkmap(t)
defer cancel()
id := fftypes.NewUUID()
nm.database.(*databasemocks.Plugin).On("GetIdentityByID", nm.ctx, id).Return(nil, fmt.Errorf("pop"))
_, err := nm.GetOrganizationByID(nm.ctx, id.String())
_, err := nm.GetOrganizationByNameOrID(nm.ctx, id.String())
assert.Regexp(t, "pop", err)
}

func TestGetOrganizationByIDBadUUID(t *testing.T) {
func TestGetOrganizationByNameBadName(t *testing.T) {
nm, cancel := newTestNetworkmap(t)
defer cancel()
_, err := nm.GetOrganizationByID(nm.ctx, "bad")
assert.Regexp(t, "FF10142", err)
_, err := nm.GetOrganizationByNameOrID(nm.ctx, "!bad")
assert.Regexp(t, "FF10131", err)
}

func TestGetOrganizationByNameError(t *testing.T) {
nm, cancel := newTestNetworkmap(t)
defer cancel()
nm.database.(*databasemocks.Plugin).On("GetIdentityByName", nm.ctx, fftypes.IdentityTypeOrg, fftypes.SystemNamespace, "bad").Return(nil, fmt.Errorf("pop"))
_, err := nm.GetOrganizationByNameOrID(nm.ctx, "bad")
assert.Regexp(t, "pop", err)
}

func TestGetNodeByIDOk(t *testing.T) {
func TestGetNodeByNameOrIDOk(t *testing.T) {
nm, cancel := newTestNetworkmap(t)
defer cancel()
id := fftypes.NewUUID()
nm.database.(*databasemocks.Plugin).On("GetIdentityByID", nm.ctx, id).
Return(&fftypes.Identity{IdentityBase: fftypes.IdentityBase{ID: id, Type: fftypes.IdentityTypeNode}}, nil)
res, err := nm.GetNodeByID(nm.ctx, id.String())
res, err := nm.GetNodeByNameOrID(nm.ctx, id.String())
assert.NoError(t, err)
assert.Equal(t, *id, *res.ID)
}

func TestGetNodeByIDWrongType(t *testing.T) {
func TestGetNodeByNameOrIDWrongType(t *testing.T) {
nm, cancel := newTestNetworkmap(t)
defer cancel()
id := fftypes.NewUUID()
nm.database.(*databasemocks.Plugin).On("GetIdentityByID", nm.ctx, id).
Return(&fftypes.Identity{IdentityBase: fftypes.IdentityBase{ID: id, Type: fftypes.IdentityTypeOrg}}, nil)
res, err := nm.GetNodeByID(nm.ctx, id.String())
res, err := nm.GetNodeByNameOrID(nm.ctx, id.String())
assert.NoError(t, err)
assert.Nil(t, res)
}

func TestGetNodeByIDNotFound(t *testing.T) {
func TestGetNodeByNameOrIDNotFound(t *testing.T) {
nm, cancel := newTestNetworkmap(t)
defer cancel()
id := fftypes.NewUUID()
nm.database.(*databasemocks.Plugin).On("GetIdentityByID", nm.ctx, id).Return(nil, nil)
_, err := nm.GetNodeByID(nm.ctx, id.String())
_, err := nm.GetNodeByNameOrID(nm.ctx, id.String())
assert.Regexp(t, "FF10109", err)
}

func TestGetNodeByIDError(t *testing.T) {
func TestGetNodeByNameOrIDError(t *testing.T) {
nm, cancel := newTestNetworkmap(t)
defer cancel()
id := fftypes.NewUUID()
nm.database.(*databasemocks.Plugin).On("GetIdentityByID", nm.ctx, id).Return(nil, fmt.Errorf("pop"))
_, err := nm.GetNodeByID(nm.ctx, id.String())
_, err := nm.GetNodeByNameOrID(nm.ctx, id.String())
assert.Regexp(t, "pop", err)
}

func TestGetNodeByIDBadUUID(t *testing.T) {
func TestGetNodeByNameBadName(t *testing.T) {
nm, cancel := newTestNetworkmap(t)
defer cancel()
_, err := nm.GetNodeByID(nm.ctx, "bad")
assert.Regexp(t, "FF10142", err)
_, err := nm.GetNodeByNameOrID(nm.ctx, "!bad")
assert.Regexp(t, "FF10131", err)
}

func TestGetNodeByNameError(t *testing.T) {
nm, cancel := newTestNetworkmap(t)
defer cancel()
nm.database.(*databasemocks.Plugin).On("GetIdentityByName", nm.ctx, fftypes.IdentityTypeNode, fftypes.SystemNamespace, "bad").Return(nil, fmt.Errorf("pop"))
_, err := nm.GetNodeByNameOrID(nm.ctx, "bad")
assert.Regexp(t, "pop", err)
}

func TestGetOrganizations(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions internal/networkmap/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ type Manager interface {
RegisterIdentity(ctx context.Context, ns string, dto *fftypes.IdentityCreateDTO, waitConfirm bool) (identity *fftypes.Identity, err error)
UpdateIdentity(ctx context.Context, ns string, id string, dto *fftypes.IdentityUpdateDTO, waitConfirm bool) (identity *fftypes.Identity, err error)

GetOrganizationByID(ctx context.Context, id string) (*fftypes.Identity, error)
GetOrganizationByNameOrID(ctx context.Context, nameOrID string) (*fftypes.Identity, error)
GetOrganizations(ctx context.Context, filter database.AndFilter) ([]*fftypes.Identity, *database.FilterResult, error)
GetNodeByID(ctx context.Context, id string) (*fftypes.Identity, error)
GetNodeByNameOrID(ctx context.Context, nameOrID string) (*fftypes.Identity, error)
GetNodes(ctx context.Context, filter database.AndFilter) ([]*fftypes.Identity, *database.FilterResult, error)
GetIdentityByID(ctx context.Context, ns string, id string) (*fftypes.Identity, error)
GetIdentities(ctx context.Context, ns string, filter database.AndFilter) ([]*fftypes.Identity, *database.FilterResult, error)
Expand Down
20 changes: 10 additions & 10 deletions mocks/networkmapmocks/manager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.