|
| 1 | +// Copyright (c) 2026 Uber Technologies, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package hook |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "errors" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | + basehook "github.com/uber/submitqueue/api/base/hook" |
| 25 | + entityqueue "github.com/uber/submitqueue/platform/base/messagequeue" |
| 26 | + "github.com/uber/submitqueue/platform/consumer" |
| 27 | + mqmock "github.com/uber/submitqueue/platform/extension/messagequeue/mock" |
| 28 | + "go.uber.org/mock/gomock" |
| 29 | +) |
| 30 | + |
| 31 | +const ( |
| 32 | + testEventID = "stovepipe/validation.repository.recorded/request/7/0" |
| 33 | + testPartitionKey = "request/7" |
| 34 | +) |
| 35 | + |
| 36 | +func testEvent() *basehook.HookEvent { |
| 37 | + return &basehook.HookEvent{ |
| 38 | + Id: testEventID, |
| 39 | + Source: "stovepipe", |
| 40 | + Type: "validation.repository.recorded", |
| 41 | + TimestampMs: 1756327200000, |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// registryWithHookTopic returns a registry whose hook topic captures whatever is |
| 46 | +// published to it, and the slot the captured message lands in. |
| 47 | +func registryWithHookTopic(t *testing.T, ctrl *gomock.Controller, publishErr error) (consumer.TopicRegistry, *entityqueue.Message) { |
| 48 | + t.Helper() |
| 49 | + |
| 50 | + var published entityqueue.Message |
| 51 | + publisher := mqmock.NewMockPublisher(ctrl) |
| 52 | + publisher.EXPECT().Publish(gomock.Any(), "domain-hook", gomock.Any()). |
| 53 | + DoAndReturn(func(_ context.Context, _ string, msg entityqueue.Message) error { |
| 54 | + published = msg |
| 55 | + return publishErr |
| 56 | + }).AnyTimes() |
| 57 | + |
| 58 | + queue := mqmock.NewMockQueue(ctrl) |
| 59 | + queue.EXPECT().Publisher().Return(publisher).AnyTimes() |
| 60 | + |
| 61 | + registry, err := consumer.NewTopicRegistry([]consumer.TopicConfig{ |
| 62 | + {Key: basehook.TopicKeyHook, Name: "domain-hook", Queue: queue}, |
| 63 | + }) |
| 64 | + require.NoError(t, err) |
| 65 | + return registry, &published |
| 66 | +} |
| 67 | + |
| 68 | +func TestPublish(t *testing.T) { |
| 69 | + ctrl := gomock.NewController(t) |
| 70 | + registry, published := registryWithHookTopic(t, ctrl, nil) |
| 71 | + |
| 72 | + require.NoError(t, Publish(context.Background(), registry, testEvent(), testPartitionKey)) |
| 73 | + |
| 74 | + // The event id is the message id, so a redelivery republishing the same |
| 75 | + // event dedups into the original message. |
| 76 | + assert.Equal(t, testEventID, published.ID) |
| 77 | + assert.Equal(t, testPartitionKey, published.PartitionKey) |
| 78 | + |
| 79 | + decoded := &basehook.HookEvent{} |
| 80 | + require.NoError(t, basehook.Unmarshal(published.Payload, decoded)) |
| 81 | + assert.Equal(t, testEventID, decoded.GetId()) |
| 82 | + assert.Equal(t, "validation.repository.recorded", decoded.GetType()) |
| 83 | +} |
| 84 | + |
| 85 | +func TestPublish_RejectsMalformedEvent(t *testing.T) { |
| 86 | + tests := []struct { |
| 87 | + name string |
| 88 | + event *basehook.HookEvent |
| 89 | + }{ |
| 90 | + {name: "nil", event: nil}, |
| 91 | + {name: "no id", event: &basehook.HookEvent{Source: "stovepipe", Type: "t"}}, |
| 92 | + {name: "no source", event: &basehook.HookEvent{Id: testEventID, Type: "t"}}, |
| 93 | + {name: "no type", event: &basehook.HookEvent{Id: testEventID, Source: "stovepipe"}}, |
| 94 | + } |
| 95 | + |
| 96 | + for _, tt := range tests { |
| 97 | + t.Run(tt.name, func(t *testing.T) { |
| 98 | + ctrl := gomock.NewController(t) |
| 99 | + publisher := mqmock.NewMockPublisher(ctrl) |
| 100 | + queue := mqmock.NewMockQueue(ctrl) |
| 101 | + queue.EXPECT().Publisher().Return(publisher).AnyTimes() |
| 102 | + registry, err := consumer.NewTopicRegistry([]consumer.TopicConfig{ |
| 103 | + {Key: basehook.TopicKeyHook, Name: "domain-hook", Queue: queue}, |
| 104 | + }) |
| 105 | + require.NoError(t, err) |
| 106 | + |
| 107 | + // No Publish expectation: a malformed event must not reach the queue. |
| 108 | + require.Error(t, Publish(context.Background(), registry, tt.event, testPartitionKey)) |
| 109 | + }) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func TestPublish_PropagatesPublishFailure(t *testing.T) { |
| 114 | + ctrl := gomock.NewController(t) |
| 115 | + registry, _ := registryWithHookTopic(t, ctrl, errors.New("boom")) |
| 116 | + |
| 117 | + require.Error(t, Publish(context.Background(), registry, testEvent(), testPartitionKey)) |
| 118 | +} |
| 119 | + |
| 120 | +func TestPublish_FailsWhenHookTopicIsUnregistered(t *testing.T) { |
| 121 | + registry, err := consumer.NewTopicRegistry(nil) |
| 122 | + require.NoError(t, err) |
| 123 | + |
| 124 | + require.Error(t, Publish(context.Background(), registry, testEvent(), testPartitionKey)) |
| 125 | +} |
0 commit comments