github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/internal/broadcast/namespace_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 broadcast 18 19 import ( 20 "context" 21 "strings" 22 "testing" 23 24 "github.com/kaleido-io/firefly/mocks/databasemocks" 25 "github.com/kaleido-io/firefly/mocks/datamocks" 26 "github.com/kaleido-io/firefly/pkg/fftypes" 27 "github.com/stretchr/testify/assert" 28 "github.com/stretchr/testify/mock" 29 ) 30 31 func TestBroadcastNamespaceBadName(t *testing.T) { 32 bm, cancel := newTestBroadcast(t) 33 defer cancel() 34 mdi := bm.database.(*databasemocks.Plugin) 35 36 mdi.On("GetNamespace", mock.Anything, mock.Anything).Return(&fftypes.Namespace{Name: "ns1"}, nil) 37 _, err := bm.BroadcastNamespace(context.Background(), &fftypes.Namespace{ 38 Name: "!ns", 39 }) 40 assert.Regexp(t, "FF10131.*name", err) 41 } 42 43 func TestBroadcastNamespaceDescriptionTooLong(t *testing.T) { 44 bm, cancel := newTestBroadcast(t) 45 defer cancel() 46 mdi := bm.database.(*databasemocks.Plugin) 47 48 mdi.On("GetNamespace", mock.Anything, mock.Anything).Return(&fftypes.Namespace{Name: "ns1"}, nil) 49 buff := strings.Builder{} 50 buff.Grow(4097) 51 for i := 0; i < 4097; i++ { 52 buff.WriteByte(byte('a' + i%26)) 53 } 54 _, err := bm.BroadcastNamespace(context.Background(), &fftypes.Namespace{ 55 Name: "ns1", 56 Description: buff.String(), 57 }) 58 assert.Regexp(t, "FF10188.*description", err) 59 } 60 61 func TestBroadcastNamespaceBroadcastOk(t *testing.T) { 62 bm, cancel := newTestBroadcast(t) 63 defer cancel() 64 mdi := bm.database.(*databasemocks.Plugin) 65 mdm := bm.data.(*datamocks.Manager) 66 67 mdi.On("GetNamespace", mock.Anything, mock.Anything).Return(&fftypes.Namespace{Name: "ns1"}, nil) 68 mdi.On("UpsertData", mock.Anything, mock.Anything, true, false).Return(nil) 69 mdm.On("CheckDatatype", mock.Anything, "ns1", mock.Anything).Return(nil) 70 mdi.On("InsertMessageLocal", mock.Anything, mock.Anything).Return(nil) 71 buff := strings.Builder{} 72 buff.Grow(4097) 73 for i := 0; i < 4097; i++ { 74 buff.WriteByte(byte('a' + i%26)) 75 } 76 _, err := bm.BroadcastNamespace(context.Background(), &fftypes.Namespace{ 77 Name: "ns1", 78 Description: "my namespace", 79 }) 80 assert.NoError(t, err) 81 }