diff --git a/asyncapi/v2/decode_test.go b/asyncapi/v2/decode_test.go index a0f4a84..e3f2b27 100644 --- a/asyncapi/v2/decode_test.go +++ b/asyncapi/v2/decode_test.go @@ -127,21 +127,21 @@ components: assert.False(t, s.HasExtension(asyncapi.ExtensionEventGatewayDialMapping)) assert.Empty(t, s.Variables()) - assert.Len(t, doc.ApplicationPublishableChannels(), 1) - assert.Len(t, doc.ApplicationPublishOperations(), 1) - assert.Len(t, doc.ApplicationPublishableMessages(), 2) + assert.Empty(t, doc.ApplicationPublishableChannels()) + assert.Empty(t, doc.ApplicationPublishOperations()) + assert.Empty(t, doc.ApplicationPublishableMessages()) - assert.Empty(t, doc.ApplicationSubscribableChannels()) - assert.Empty(t, doc.ApplicationSubscribeOperations()) - assert.Empty(t, doc.ApplicationSubscribableMessages()) + assert.Len(t, doc.ApplicationSubscribableChannels(), 1) + assert.Len(t, doc.ApplicationSubscribeOperations(), 1) + assert.Len(t, doc.ApplicationSubscribableMessages(), 2) - assert.Len(t, doc.ClientSubscribableChannels(), 1) - assert.Len(t, doc.ClientSubscribeOperations(), 1) - assert.Len(t, doc.ClientSubscribableMessages(), 2) + assert.Empty(t, doc.ClientSubscribableChannels()) + assert.Empty(t, doc.ClientSubscribeOperations()) + assert.Empty(t, doc.ClientSubscribableMessages()) - assert.Empty(t, doc.ClientPublishableChannels()) - assert.Empty(t, doc.ClientPublishOperations()) - assert.Empty(t, doc.ClientPublishableMessages()) + assert.Len(t, doc.ClientPublishableChannels(), 1) + assert.Len(t, doc.ClientPublishOperations(), 1) + assert.Len(t, doc.ClientPublishableMessages(), 2) channels := doc.Channels() require.Len(t, channels, 1) @@ -152,10 +152,10 @@ components: operations := channels[0].Operations() require.Len(t, operations, 1) assert.Equal(t, OperationTypePublish, operations[0].Type()) - assert.True(t, operations[0].IsApplicationPublishing()) - assert.False(t, operations[0].IsApplicationSubscribing()) - assert.True(t, operations[0].IsClientSubscribing()) - assert.False(t, operations[0].IsClientPublishing()) + assert.False(t, operations[0].IsApplicationPublishing()) + assert.True(t, operations[0].IsApplicationSubscribing()) + assert.False(t, operations[0].IsClientSubscribing()) + assert.True(t, operations[0].IsClientPublishing()) assert.False(t, operations[0].HasDescription()) assert.True(t, operations[0].HasSummary()) assert.Equal(t, "Inform about environmental lighting conditions for a particular streetlight.", operations[0].Summary()) diff --git a/asyncapi/v2/v2.go b/asyncapi/v2/v2.go index 7fe0c72..8cda4de 100644 --- a/asyncapi/v2/v2.go +++ b/asyncapi/v2/v2.go @@ -147,7 +147,7 @@ func (d Document) filterMessages(filter func(operation asyncapi.Operation) bool) for _, c := range d.Channels() { for _, o := range c.Operations() { if filter(o) { - messages = append(messages, c.Messages()...) + messages = append(messages, o.Messages()...) } } } @@ -312,19 +312,19 @@ func (o Operation) ID() string { } func (o Operation) IsApplicationPublishing() bool { - return o.Type() == OperationTypePublish + return o.Type() == OperationTypeSubscribe } func (o Operation) IsApplicationSubscribing() bool { - return o.Type() == OperationTypeSubscribe + return o.Type() == OperationTypePublish } func (o Operation) IsClientPublishing() bool { - return o.Type() == OperationTypeSubscribe + return o.Type() == OperationTypePublish } func (o Operation) IsClientSubscribing() bool { - return o.Type() == OperationTypePublish + return o.Type() == OperationTypeSubscribe } func (o Operation) Messages() []asyncapi.Message { diff --git a/asyncapi/v2/v2_test.go b/asyncapi/v2/v2_test.go index 6dcb265..43fe4f2 100644 --- a/asyncapi/v2/v2_test.go +++ b/asyncapi/v2/v2_test.go @@ -4,10 +4,8 @@ import ( "testing" "github.com/asyncapi/event-gateway/asyncapi" - - "github.com/stretchr/testify/require" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestMessage_OneOf(t *testing.T) { @@ -64,6 +62,69 @@ func TestSchema_AdditionalItems(t *testing.T) { assert.Equal(t, field, schema.AdditionalItems().Schema()) } +func TestOperationShortcuts(t *testing.T) { + doc := &Document{ + ChannelsField: map[string]Channel{ + "test-channel-1": { + Subscribe: NewSubscribeOperation(generateTestMessageWithName("test-channel-1_subscribe_message")), + }, + "test-channel-2": { + Publish: NewPublishOperation(generateTestMessageWithName("test-channel-2_publish_message")), + }, + "test-channel-3": { + Subscribe: NewSubscribeOperation(generateTestMessageWithName("test-channel-3_subscribe_message")), + Publish: NewPublishOperation(generateTestMessageWithName("test-channel-3_publish_message")), + }, + }, + } + + // Application publishes messages in channels where the Client subscribes to. Subscribe is the right type of Operation. + assert.ElementsMatch(t, []asyncapi.Channel{doc.ChannelsField["test-channel-1"], doc.ChannelsField["test-channel-3"]}, doc.ApplicationPublishableChannels()) + + // Application subscribes to messages in channels where the Client publishes to. Publish is the right type of Operation. + assert.ElementsMatch(t, []asyncapi.Channel{doc.ChannelsField["test-channel-2"], doc.ChannelsField["test-channel-3"]}, doc.ApplicationSubscribableChannels()) + + // Client publishes messages in channels where the Application subscribes to. Publish is the right type of Operation. + assert.ElementsMatch(t, []asyncapi.Channel{doc.ChannelsField["test-channel-2"], doc.ChannelsField["test-channel-3"]}, doc.ClientPublishableChannels()) + + // Client subscribe to messages in channels where the Application publishes to. Subscribe is the right type of Operation. + assert.ElementsMatch(t, []asyncapi.Channel{doc.ChannelsField["test-channel-1"], doc.ChannelsField["test-channel-3"]}, doc.ClientSubscribableChannels()) + + // Application publishes messages through operations of type Subscribe. + assert.ElementsMatch(t, []asyncapi.Operation{doc.ChannelsField["test-channel-1"].Subscribe, doc.ChannelsField["test-channel-3"].Subscribe}, doc.ApplicationPublishOperations()) + + // Application publishes messages through operations of type Subscribe. + assert.ElementsMatch(t, []asyncapi.Operation{doc.ChannelsField["test-channel-1"].Subscribe, doc.ChannelsField["test-channel-3"].Subscribe}, doc.ApplicationPublishOperations()) + + // Application subscribes to messages through operations of type Publish. + assert.ElementsMatch(t, []asyncapi.Operation{doc.ChannelsField["test-channel-2"].Publish, doc.ChannelsField["test-channel-3"].Publish}, doc.ApplicationSubscribeOperations()) + + // Client publishes messages through operations of type Publish. + assert.ElementsMatch(t, []asyncapi.Operation{doc.ChannelsField["test-channel-2"].Publish, doc.ChannelsField["test-channel-3"].Publish}, doc.ClientPublishOperations()) + + // Client subscribes to messages through operations of type Subscribe. + assert.ElementsMatch(t, []asyncapi.Operation{doc.ChannelsField["test-channel-1"].Subscribe, doc.ChannelsField["test-channel-3"].Subscribe}, doc.ClientSubscribeOperations()) + + // Application publishes messages to channels by using Subscribe operations. + assert.ElementsMatch(t, []asyncapi.Message{doc.ChannelsField["test-channel-1"].Subscribe.Messages()[0], doc.ChannelsField["test-channel-3"].Subscribe.Messages()[0]}, doc.ApplicationPublishableMessages()) + + // Application subscribes to messages by using Publish operations. + assert.ElementsMatch(t, []asyncapi.Message{doc.ChannelsField["test-channel-2"].Publish.Messages()[0], doc.ChannelsField["test-channel-3"].Publish.Messages()[0]}, doc.ApplicationSubscribableMessages()) + + // Client publishes messages to channels by using Publish operations. + assert.ElementsMatch(t, []asyncapi.Message{doc.ChannelsField["test-channel-2"].Publish.Messages()[0], doc.ChannelsField["test-channel-3"].Publish.Messages()[0]}, doc.ClientPublishableMessages()) + + // Client subscribes to messages by using Subscribe operations. + assert.ElementsMatch(t, []asyncapi.Message{doc.ChannelsField["test-channel-1"].Subscribe.Messages()[0], doc.ChannelsField["test-channel-3"].Subscribe.Messages()[0]}, doc.ClientSubscribableMessages()) +} + +func generateTestMessageWithName(name string) *Message { + m := generateTestMessage() + m.NameField = name + + return m +} + func generateTestMessage() *Message { return &Message{ PayloadField: &Schema{ diff --git a/asyncapi/v2/validation_test.go b/asyncapi/v2/validation_test.go index ec868f3..7ca768c 100644 --- a/asyncapi/v2/validation_test.go +++ b/asyncapi/v2/validation_test.go @@ -83,7 +83,7 @@ func TestFromDocJsonSchemaMessageValidator(t *testing.T) { t.Run(test.name, func(t *testing.T) { // Doc generation channel := NewChannel(t.Name()) - channel.Subscribe = NewSubscribeOperation(&Message{PayloadField: test.schema}) + channel.Publish = NewPublishOperation(&Message{PayloadField: test.schema}) doc := Document{ChannelsField: map[string]Channel{t.Name(): *channel}} // Test