github.com/Axway/agent-sdk@v1.1.101/pkg/apic/asyncapi_test.go (about)

     1  package apic
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  type asyncServer struct {
    10  	name            string
    11  	description     string
    12  	url             string
    13  	protocol        string
    14  	protocolVersion string
    15  	useSaslScram    bool
    16  	useSaslPlain    bool
    17  }
    18  
    19  type asyncChannel struct {
    20  	name             string
    21  	publish          bool
    22  	subscribe        bool
    23  	publishMessage   string
    24  	subscribeMessage string
    25  }
    26  
    27  type asyncMessage struct {
    28  	name        string
    29  	format      string
    30  	contentType string
    31  	payload     map[string]interface{}
    32  }
    33  
    34  func TestAsyncAPIGenerator(t *testing.T) {
    35  	tests := map[string]struct {
    36  		expectBuilder bool
    37  		id            string
    38  		title         string
    39  		version       string
    40  		servers       []asyncServer
    41  		messages      []asyncMessage
    42  		channels      []asyncChannel
    43  	}{
    44  		"no id in info": {
    45  			expectBuilder: true,
    46  		},
    47  		"invalid id in info": {
    48  			id:            "test",
    49  			expectBuilder: true,
    50  		},
    51  		"no title in info": {
    52  			id:            "kafka://test",
    53  			expectBuilder: true,
    54  		},
    55  		"no version in info": {
    56  			id:            "kafka://test",
    57  			title:         "test",
    58  			expectBuilder: true,
    59  		},
    60  		"no servers": {
    61  			id:            "kafka://test",
    62  			title:         "test",
    63  			version:       "1.0.0",
    64  			expectBuilder: true,
    65  		},
    66  		"servers with no url": {
    67  			id:      "kafka://test",
    68  			title:   "test",
    69  			version: "1.0.0",
    70  			servers: []asyncServer{
    71  				{
    72  					name:        "test",
    73  					description: "test",
    74  					url:         "",
    75  				},
    76  			},
    77  			expectBuilder: true,
    78  		},
    79  		"servers with no protocol": {
    80  			id:      "kafka://test",
    81  			title:   "test",
    82  			version: "1.0.0",
    83  			servers: []asyncServer{
    84  				{
    85  					name:        "test",
    86  					description: "test",
    87  					url:         "PLAINTEXT://localhost",
    88  				},
    89  			},
    90  			expectBuilder: true,
    91  		},
    92  		"servers with no protocol version": {
    93  			id:      "kafka://test",
    94  			title:   "test",
    95  			version: "1.0.0",
    96  			servers: []asyncServer{
    97  				{
    98  					name:        "test",
    99  					description: "test",
   100  					url:         "PLAINTEXT://localhost",
   101  					protocol:    "kafka",
   102  				},
   103  			},
   104  			expectBuilder: true,
   105  		},
   106  		"no channels": {
   107  			id:      "kafka://test",
   108  			title:   "test",
   109  			version: "1.0.0",
   110  			servers: []asyncServer{
   111  				{
   112  					name:            "test",
   113  					description:     "test",
   114  					url:             "PLAINTEXT://localhost",
   115  					protocol:        "kafka",
   116  					protocolVersion: "1.0.0",
   117  				},
   118  			},
   119  			expectBuilder: true,
   120  		},
   121  		"servers with invalid component message message ref": {
   122  			id:      "kafka://test",
   123  			title:   "test",
   124  			version: "1.0.0",
   125  			servers: []asyncServer{
   126  				{
   127  					name:            "test",
   128  					description:     "test",
   129  					url:             "PLAINTEXT://localhost",
   130  					protocol:        "kafka",
   131  					protocolVersion: "1.0.0",
   132  				},
   133  			},
   134  			channels: []asyncChannel{
   135  				{
   136  					name:           "test",
   137  					publish:        true,
   138  					subscribe:      true,
   139  					publishMessage: "",
   140  				},
   141  			},
   142  			expectBuilder: true,
   143  		},
   144  		"servers with no component message for publish message ref": {
   145  			id:      "kafka://test",
   146  			title:   "test",
   147  			version: "1.0.0",
   148  			servers: []asyncServer{
   149  				{
   150  					name:            "test",
   151  					description:     "test",
   152  					url:             "PLAINTEXT://localhost",
   153  					protocol:        "kafka",
   154  					protocolVersion: "1.0.0",
   155  				},
   156  			},
   157  			channels: []asyncChannel{
   158  				{
   159  					name:             "test",
   160  					publish:          true,
   161  					subscribe:        true,
   162  					publishMessage:   "order",
   163  					subscribeMessage: "order",
   164  				},
   165  			},
   166  			expectBuilder: true,
   167  		},
   168  		"servers with no component message for subscribe message ref": {
   169  			id:      "kafka://test",
   170  			title:   "test",
   171  			version: "1.0.0",
   172  			servers: []asyncServer{
   173  				{
   174  					name:            "test",
   175  					description:     "test",
   176  					url:             "PLAINTEXT://localhost",
   177  					protocol:        "kafka",
   178  					protocolVersion: "1.0.0",
   179  				},
   180  			},
   181  			channels: []asyncChannel{
   182  				{
   183  					name:             "test",
   184  					publish:          true,
   185  					subscribe:        true,
   186  					publishMessage:   "order-pub",
   187  					subscribeMessage: "order-sub",
   188  				},
   189  			},
   190  			messages: []asyncMessage{
   191  				{
   192  					name:    "order-pub",
   193  					format:  "json",
   194  					payload: map[string]interface{}{"schema": map[string]interface{}{}},
   195  				},
   196  			},
   197  			expectBuilder: true,
   198  		},
   199  		"invalid component message name": {
   200  			id:      "kafka://test",
   201  			title:   "test",
   202  			version: "1.0.0",
   203  			servers: []asyncServer{
   204  				{
   205  					name:            "test",
   206  					description:     "test",
   207  					url:             "PLAINTEXT://localhost",
   208  					protocol:        "kafka",
   209  					protocolVersion: "1.0.0",
   210  				},
   211  			},
   212  			channels: []asyncChannel{
   213  				{
   214  					name:             "test",
   215  					publish:          true,
   216  					subscribe:        true,
   217  					publishMessage:   "order-pub",
   218  					subscribeMessage: "order-sub",
   219  				},
   220  			},
   221  			messages: []asyncMessage{
   222  				{
   223  					name:   "order-pub",
   224  					format: "json",
   225  					payload: map[string]interface{}{
   226  						"schema": map[string]interface{}{},
   227  					},
   228  				},
   229  				{
   230  					name:   "order-sub",
   231  					format: "json",
   232  					payload: map[string]interface{}{
   233  						"schema": map[string]interface{}{},
   234  					},
   235  				},
   236  				{
   237  					name:   "",
   238  					format: "json",
   239  					payload: map[string]interface{}{
   240  						"schema": map[string]interface{}{},
   241  					},
   242  				},
   243  			},
   244  			expectBuilder: true,
   245  		},
   246  		"invalid component message payload": {
   247  			id:      "kafka://test",
   248  			title:   "test",
   249  			version: "1.0.0",
   250  			servers: []asyncServer{
   251  				{
   252  					name:            "test",
   253  					description:     "test",
   254  					url:             "PLAINTEXT://localhost",
   255  					protocol:        "kafka",
   256  					protocolVersion: "1.0.0",
   257  				},
   258  			},
   259  			channels: []asyncChannel{
   260  				{
   261  					name:             "test",
   262  					publish:          true,
   263  					subscribe:        true,
   264  					publishMessage:   "order-pub",
   265  					subscribeMessage: "order-sub",
   266  				},
   267  			},
   268  			messages: []asyncMessage{
   269  				{
   270  					name:   "order-pub",
   271  					format: "json",
   272  					payload: map[string]interface{}{
   273  						"schema": map[string]interface{}{},
   274  					},
   275  				},
   276  				{
   277  					name:   "order-sub",
   278  					format: "json",
   279  					payload: map[string]interface{}{
   280  						"schema": map[string]interface{}{},
   281  					},
   282  				},
   283  				{
   284  					name:   "test",
   285  					format: "json",
   286  				},
   287  			},
   288  			expectBuilder: true,
   289  		},
   290  		"valid async api spec": {
   291  			id:      "kafka://test",
   292  			title:   "test",
   293  			version: "1.0.0",
   294  			servers: []asyncServer{
   295  				{
   296  					name:            "test-plain",
   297  					description:     "test",
   298  					url:             "SASL_SSL://localhost:9092",
   299  					protocol:        "kafka",
   300  					protocolVersion: "1.0.0",
   301  					useSaslScram:    false,
   302  					useSaslPlain:    true,
   303  				},
   304  			},
   305  			channels: []asyncChannel{
   306  				{
   307  					name:             "test",
   308  					publish:          true,
   309  					subscribe:        true,
   310  					publishMessage:   "order",
   311  					subscribeMessage: "order",
   312  				},
   313  			},
   314  			messages: []asyncMessage{
   315  				{
   316  					name:        "order",
   317  					format:      "application/vnd.oai.openapi+json;version=3.0.0",
   318  					contentType: "application/json",
   319  					payload: map[string]interface{}{
   320  						"schema": map[string]interface{}{},
   321  					},
   322  				},
   323  			},
   324  		},
   325  	}
   326  
   327  	for name, tt := range tests {
   328  		t.Run(name, func(t *testing.T) {
   329  			builder := CreateAsyncAPIBuilder()
   330  			for _, serverDetail := range tt.servers {
   331  				opts := make([]AsyncAPIServerOpts, 0)
   332  				if serverDetail.useSaslPlain {
   333  					opts = append(opts, WithSaslPlainSecurity("SASL_PLAIN"))
   334  				}
   335  				if serverDetail.useSaslScram {
   336  					opts = append(opts, WithSaslScramSecurity("SCRAM-SHA-512", "SASL_PLAINTEXT"))
   337  				}
   338  				opts = append(opts, WithProtocol(serverDetail.protocol, serverDetail.protocolVersion))
   339  				builder.AddServer(serverDetail.name, serverDetail.description, serverDetail.url, opts...)
   340  			}
   341  
   342  			for _, msg := range tt.messages {
   343  				builder.AddComponentMessage(msg.name, msg.format, msg.contentType, msg.payload)
   344  			}
   345  			for _, channel := range tt.channels {
   346  				opts := make([]asyncAPIChannelOpts, 0)
   347  				if channel.publish {
   348  					opts = append(opts, WithKafkaPublishOperationBinding(true, true))
   349  				}
   350  				if channel.subscribe {
   351  					opts = append(opts, WithKafkaSubscribeOperationBinding(true, true))
   352  				}
   353  				builder.AddChannel(channel.name, channel.name, opts...)
   354  				builder.SetPublishMessageRef(channel.name, channel.publishMessage)
   355  				builder.SetSubscribeMessageRef(channel.name, channel.subscribeMessage)
   356  			}
   357  
   358  			spec, err := builder.Build(tt.id, tt.title, tt.title, tt.version)
   359  			if tt.expectBuilder {
   360  				assert.NotNil(t, err)
   361  				assert.Nil(t, spec)
   362  				return
   363  			}
   364  			assert.Nil(t, err)
   365  			assert.NotNil(t, spec)
   366  
   367  			assert.Equal(t, "asyncapi", spec.GetResourceType())
   368  			assert.Equal(t, "kafka://test", spec.GetID())
   369  			assert.Equal(t, "test", spec.GetTitle())
   370  			assert.Equal(t, "1.0.0", spec.GetVersion())
   371  
   372  			raw := spec.GetSpecBytes()
   373  			assert.NotEmpty(t, raw)
   374  
   375  			endpoints, err := spec.GetEndpoints()
   376  			assert.Nil(t, err)
   377  			assert.NotEmpty(t, endpoints)
   378  
   379  			assert.Equal(t, "localhost", endpoints[0].Host)
   380  			assert.Equal(t, int32(9092), endpoints[0].Port)
   381  			assert.Equal(t, "SASL_SSL", endpoints[0].Protocol)
   382  			assert.Equal(t, "kafka", endpoints[0].Routing.Details[protocol].(string))
   383  		})
   384  	}
   385  }