github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/internal/orchestrator/subscriptions_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 orchestrator 18 19 import ( 20 "context" 21 "fmt" 22 "testing" 23 24 "github.com/kaleido-io/firefly/pkg/database" 25 "github.com/kaleido-io/firefly/pkg/fftypes" 26 "github.com/stretchr/testify/assert" 27 "github.com/stretchr/testify/mock" 28 ) 29 30 func uuidMatches(id1 *fftypes.UUID) interface{} { 31 return mock.MatchedBy(func(id2 *fftypes.UUID) bool { return id1.Equals(id2) }) 32 } 33 34 func TestCreateSubscriptionBadNamespace(t *testing.T) { 35 or := newTestOrchestrator() 36 or.mdm.On("VerifyNamespaceExists", mock.Anything, "!wrong").Return(fmt.Errorf("pop")) 37 _, err := or.CreateSubscription(or.ctx, "!wrong", &fftypes.Subscription{ 38 SubscriptionRef: fftypes.SubscriptionRef{ 39 Name: "sub1", 40 }, 41 }) 42 assert.Regexp(t, "pop", err) 43 } 44 45 func TestCreateSubscriptionBadName(t *testing.T) { 46 or := newTestOrchestrator() 47 or.mdm.On("VerifyNamespaceExists", mock.Anything, "ns1").Return(nil) 48 _, err := or.CreateSubscription(or.ctx, "ns1", &fftypes.Subscription{ 49 SubscriptionRef: fftypes.SubscriptionRef{ 50 Name: "!sub1", 51 }, 52 }) 53 assert.Regexp(t, "FF10131", err) 54 } 55 56 func TestCreateSubscriptionOk(t *testing.T) { 57 or := newTestOrchestrator() 58 sub := &fftypes.Subscription{ 59 SubscriptionRef: fftypes.SubscriptionRef{ 60 Name: "sub1", 61 }, 62 } 63 or.mdm.On("VerifyNamespaceExists", mock.Anything, "ns1").Return(nil) 64 or.mem.On("CreateDurableSubscription", mock.Anything, mock.Anything).Return(nil) 65 s1, err := or.CreateSubscription(or.ctx, "ns1", sub) 66 assert.NoError(t, err) 67 assert.Equal(t, s1, sub) 68 assert.Equal(t, "ns1", sub.Namespace) 69 } 70 71 func TestDeleteSubscriptionBadUUID(t *testing.T) { 72 or := newTestOrchestrator() 73 or.mdi.On("GetSubscriptionByID", mock.Anything, mock.Anything).Return(nil, fmt.Errorf("pop")) 74 err := or.DeleteSubscription(or.ctx, "ns2", "! a UUID") 75 assert.Regexp(t, "FF10142", err) 76 } 77 78 func TestDeleteSubscriptionLookupError(t *testing.T) { 79 or := newTestOrchestrator() 80 or.mdi.On("GetSubscriptionByID", mock.Anything, mock.Anything).Return(nil, fmt.Errorf("pop")) 81 err := or.DeleteSubscription(or.ctx, "ns2", fftypes.NewUUID().String()) 82 assert.EqualError(t, err, "pop") 83 } 84 85 func TestDeleteSubscriptionNSMismatch(t *testing.T) { 86 or := newTestOrchestrator() 87 sub := &fftypes.Subscription{ 88 SubscriptionRef: fftypes.SubscriptionRef{ 89 ID: fftypes.NewUUID(), 90 Name: "sub1", 91 Namespace: "ns1", 92 }, 93 } 94 or.mdi.On("GetSubscriptionByID", mock.Anything, sub.ID).Return(sub, nil) 95 err := or.DeleteSubscription(or.ctx, "ns2", sub.ID.String()) 96 assert.Regexp(t, "FF10109", err) 97 } 98 99 func TestDeleteSubscription(t *testing.T) { 100 or := newTestOrchestrator() 101 sub := &fftypes.Subscription{ 102 SubscriptionRef: fftypes.SubscriptionRef{ 103 ID: fftypes.NewUUID(), 104 Name: "sub1", 105 Namespace: "ns1", 106 }, 107 } 108 or.mdi.On("GetSubscriptionByID", mock.Anything, uuidMatches(sub.ID)).Return(sub, nil) 109 or.mem.On("DeleteDurableSubscription", mock.Anything, sub).Return(nil) 110 err := or.DeleteSubscription(or.ctx, "ns1", sub.ID.String()) 111 assert.NoError(t, err) 112 } 113 114 func TestGetSubscriptions(t *testing.T) { 115 or := newTestOrchestrator() 116 u := fftypes.NewUUID() 117 or.mdi.On("GetSubscriptions", mock.Anything, mock.Anything).Return([]*fftypes.Subscription{}, nil) 118 fb := database.SubscriptionQueryFactory.NewFilter(context.Background()) 119 f := fb.And(fb.Eq("id", u)) 120 _, err := or.GetSubscriptions(context.Background(), "ns1", f) 121 assert.NoError(t, err) 122 } 123 124 func TestGetSGetSubscriptionsByID(t *testing.T) { 125 or := newTestOrchestrator() 126 u := fftypes.NewUUID() 127 or.mdi.On("GetSubscriptionByID", mock.Anything, u).Return(nil, nil) 128 _, err := or.GetSubscriptionByID(context.Background(), "ns1", u.String()) 129 assert.NoError(t, err) 130 } 131 132 func TestGetSubscriptionDefsByIDBadID(t *testing.T) { 133 or := newTestOrchestrator() 134 _, err := or.GetSubscriptionByID(context.Background(), "", "") 135 assert.Regexp(t, "FF10142", err) 136 }