github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/internal/networkmap/data_query_test.go (about) 1 // Copyright © 2021 Kaleido, Inc. 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 package networkmap 18 19 import ( 20 "testing" 21 22 "github.com/kaleido-io/firefly/mocks/databasemocks" 23 "github.com/kaleido-io/firefly/pkg/database" 24 "github.com/kaleido-io/firefly/pkg/fftypes" 25 "github.com/stretchr/testify/assert" 26 "github.com/stretchr/testify/mock" 27 ) 28 29 func uuidMatches(id1 *fftypes.UUID) interface{} { 30 return mock.MatchedBy(func(id2 *fftypes.UUID) bool { return id1.Equals(id2) }) 31 } 32 33 func TestGetOrganizationByIDOk(t *testing.T) { 34 nm, cancel := newTestNetworkmap(t) 35 defer cancel() 36 id := fftypes.NewUUID() 37 nm.database.(*databasemocks.Plugin).On("GetOrganizationByID", nm.ctx, uuidMatches(id)).Return(&fftypes.Organization{ID: id}, nil) 38 res, err := nm.GetOrganizationByID(nm.ctx, id.String()) 39 assert.NoError(t, err) 40 assert.Equal(t, *id, *res.ID) 41 } 42 43 func TestGetOrganizationByIDBadUUID(t *testing.T) { 44 nm, cancel := newTestNetworkmap(t) 45 defer cancel() 46 _, err := nm.GetOrganizationByID(nm.ctx, "bad") 47 assert.Regexp(t, "FF10142", err) 48 } 49 50 func TestGetNodeByIDOk(t *testing.T) { 51 nm, cancel := newTestNetworkmap(t) 52 defer cancel() 53 id := fftypes.NewUUID() 54 nm.database.(*databasemocks.Plugin).On("GetNodeByID", nm.ctx, uuidMatches(id)).Return(&fftypes.Node{ID: id}, nil) 55 res, err := nm.GetNodeByID(nm.ctx, id.String()) 56 assert.NoError(t, err) 57 assert.Equal(t, *id, *res.ID) 58 } 59 60 func TestGetNodeByIDBadUUID(t *testing.T) { 61 nm, cancel := newTestNetworkmap(t) 62 defer cancel() 63 _, err := nm.GetNodeByID(nm.ctx, "bad") 64 assert.Regexp(t, "FF10142", err) 65 } 66 67 func TestGetOrganizations(t *testing.T) { 68 nm, cancel := newTestNetworkmap(t) 69 defer cancel() 70 nm.database.(*databasemocks.Plugin).On("GetOrganizations", nm.ctx, mock.Anything).Return([]*fftypes.Organization{}, nil) 71 res, err := nm.GetOrganizations(nm.ctx, database.OrganizationQueryFactory.NewFilter(nm.ctx).And()) 72 assert.NoError(t, err) 73 assert.Empty(t, res) 74 } 75 76 func TestGetNodes(t *testing.T) { 77 nm, cancel := newTestNetworkmap(t) 78 defer cancel() 79 nm.database.(*databasemocks.Plugin).On("GetNodes", nm.ctx, mock.Anything).Return([]*fftypes.Node{}, nil) 80 res, err := nm.GetNodes(nm.ctx, database.NodeQueryFactory.NewFilter(nm.ctx).And()) 81 assert.NoError(t, err) 82 assert.Empty(t, res) 83 }