Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
fix: swap operation types on API shortcuts as they were inverted (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
smoya authored Sep 21, 2021
1 parent bc0e744 commit 497e5d2
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 25 deletions.
32 changes: 16 additions & 16 deletions asyncapi/v2/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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())
Expand Down
10 changes: 5 additions & 5 deletions asyncapi/v2/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()...)
}
}
}
Expand Down Expand Up @@ -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 {
Expand Down
67 changes: 64 additions & 3 deletions asyncapi/v2/v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion asyncapi/v2/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 497e5d2

Please sign in to comment.