github.com/hamba/avro@v1.8.0/protocol_test.go (about)

     1  package avro_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hamba/avro"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestMustParseProtocol(t *testing.T) {
    11  	proto := avro.MustParseProtocol(`{"protocol":"test", "namespace": "org.hamba.avro", "doc": "docs"}`)
    12  
    13  	assert.IsType(t, &avro.Protocol{}, proto)
    14  }
    15  
    16  func TestMustParseProtocol_PanicsOnError(t *testing.T) {
    17  	assert.Panics(t, func() {
    18  		avro.MustParseProtocol("123")
    19  	})
    20  }
    21  
    22  func TestNewProtocol_ValidatesName(t *testing.T) {
    23  	_, err := avro.NewProtocol("0test", "", nil, nil)
    24  
    25  	assert.Error(t, err)
    26  }
    27  
    28  func TestNewMessage(t *testing.T) {
    29  	field, _ := avro.NewField("test", avro.NewPrimitiveSchema(avro.String, nil), nil)
    30  	fields := []*avro.Field{field}
    31  	req, _ := avro.NewRecordSchema("test", "", fields)
    32  	resp := avro.NewPrimitiveSchema(avro.String, nil)
    33  	types := []avro.Schema{avro.NewPrimitiveSchema(avro.String, nil)}
    34  	errs, _ := avro.NewUnionSchema(types)
    35  
    36  	msg := avro.NewMessage(req, resp, errs, false)
    37  
    38  	assert.Equal(t, req, msg.Request())
    39  	assert.Equal(t, resp, msg.Response())
    40  	assert.Equal(t, errs, msg.Errors())
    41  	assert.False(t, msg.OneWay())
    42  }
    43  
    44  func TestParseProtocol(t *testing.T) {
    45  	tests := []struct {
    46  		name    string
    47  		schema  string
    48  		wantErr bool
    49  	}{
    50  		{
    51  			name:    "Valid",
    52  			schema:  `{"protocol":"test", "namespace": "org.hamba.avro", "doc": "docs"}`,
    53  			wantErr: false,
    54  		},
    55  		{
    56  			name:    "Invalid Json",
    57  			schema:  `{`,
    58  			wantErr: true,
    59  		},
    60  		{
    61  			name:    "Invalid Name First Char",
    62  			schema:  `{"protocol":"0test", "namespace": "org.hamba.avro"}`,
    63  			wantErr: true,
    64  		},
    65  		{
    66  			name:    "Invalid Name Other Char",
    67  			schema:  `{"protocol":"test+", "namespace": "org.hamba.avro"}`,
    68  			wantErr: true,
    69  		},
    70  		{
    71  			name:    "Empty Name",
    72  			schema:  `{"protocol":"", "namespace": "org.hamba.avro"}`,
    73  			wantErr: true,
    74  		},
    75  		{
    76  			name:    "No Name",
    77  			schema:  `{"namespace": "org.hamba.avro"}`,
    78  			wantErr: true,
    79  		},
    80  		{
    81  			name:    "Invalid Namespace",
    82  			schema:  `{"protocol":"test", "namespace": "org.hamba.avro+"}`,
    83  			wantErr: true,
    84  		},
    85  		{
    86  			name:    "Empty Namespace",
    87  			schema:  `{"protocol":"test", "namespace": ""}`,
    88  			wantErr: true,
    89  		},
    90  		{
    91  			name:    "Invalid Type Schema",
    92  			schema:  `{"protocol":"test", "namespace": "org.hamba.avro", "types":["test"]}`,
    93  			wantErr: true,
    94  		},
    95  		{
    96  			name:    "Type Not Named Schema",
    97  			schema:  `{"protocol":"test", "namespace": "org.hamba.avro", "types":["string"]}`,
    98  			wantErr: true,
    99  		},
   100  		{
   101  			name:    "Message Not Object",
   102  			schema:  `{"protocol":"test", "namespace": "org.hamba.avro", "messages":{"test":["test"]}}`,
   103  			wantErr: true,
   104  		},
   105  		{
   106  			name:    "Message Request Invalid Request Json",
   107  			schema:  `{"protocol":"test", "namespace": "org.hamba.avro", "messages":{"test":{"request": "test"}}}`,
   108  			wantErr: true,
   109  		},
   110  		{
   111  			name:    "Message Request Invalid Field",
   112  			schema:  `{"protocol":"test", "namespace": "org.hamba.avro", "messages":{"test":{"request": [{"name": "foobar"}]}}}`,
   113  			wantErr: true,
   114  		},
   115  		{
   116  			name:    "Message Response Invalid Schema",
   117  			schema:  `{"protocol":"test", "namespace": "org.hamba.avro", "messages":{"test":{"request": [{"name": "foobar", "type": "string"}], "response": "test"}}}`,
   118  			wantErr: true,
   119  		},
   120  		{
   121  			name:    "Message Errors Invalid Schema",
   122  			schema:  `{"protocol":"test", "namespace": "org.hamba.avro", "messages":{"test":{"request": [{"name": "foobar", "type": "string"}], "errors": ["test"]}}}`,
   123  			wantErr: true,
   124  		},
   125  		{
   126  			name:    "Message Errors Record Not Error Schema",
   127  			schema:  `{"protocol":"test", "namespace": "org.hamba.avro", "messages":{"test":{"request": [{"name": "foobar", "type": "string"}], "errors": [{"type":"record", "name":"test", "fields":[{"name": "field", "type": "int"}]}]}}}`,
   128  			wantErr: true,
   129  		},
   130  		{
   131  			name:    "Message Errors Duplicate Schema",
   132  			schema:  `{"protocol":"test", "namespace": "org.hamba.avro", "messages":{"test":{"request": [{"name": "foobar", "type": "string"}], "errors": ["string"]}}}`,
   133  			wantErr: true,
   134  		},
   135  		{
   136  			name:    "Message One Way Invalid",
   137  			schema:  `{"protocol":"test", "namespace": "org.hamba.avro", "messages":{"test":{"request": [{"name": "foobar", "type": "string"}], "errors": ["int"], "one-way": true}}}`,
   138  			wantErr: true,
   139  		},
   140  	}
   141  
   142  	for _, tt := range tests {
   143  		t.Run(tt.name, func(t *testing.T) {
   144  			_, err := avro.ParseProtocol(tt.schema)
   145  
   146  			if tt.wantErr {
   147  				assert.Error(t, err)
   148  				return
   149  			}
   150  
   151  			assert.NoError(t, err)
   152  		})
   153  	}
   154  }
   155  
   156  func TestParseProtocol_DeterminesOneWayMessage(t *testing.T) {
   157  	schema := `{"protocol":"test", "namespace": "org.hamba.avro", "messages":{"test":{"request": [{"name": "foobar", "type": "string"}]}}}`
   158  
   159  	proto, err := avro.ParseProtocol(schema)
   160  
   161  	assert.NoError(t, err)
   162  
   163  	msg := proto.Message("test")
   164  	assert.NotNil(t, msg)
   165  	assert.True(t, msg.OneWay())
   166  }
   167  
   168  func TestParseProtocolFile(t *testing.T) {
   169  	protocol, err := avro.ParseProtocolFile("testdata/echo.avpr")
   170  
   171  	want := `{"protocol":"Echo","namespace":"org.hamba.avro","types":[{"name":"org.hamba.avro.Ping","type":"record","fields":[{"name":"timestamp","type":"long"},{"name":"text","type":"string"}]},{"name":"org.hamba.avro.Pong","type":"record","fields":[{"name":"timestamp","type":"long"},{"name":"ping","type":"org.hamba.avro.Ping"}]},{"name":"org.hamba.avro.PongError","type":"error","fields":[{"name":"timestamp","type":"long"},{"name":"reason","type":"string"}]}],"messages":{"ping":{"request":[{"name":"ping","type":"org.hamba.avro.Ping"}],"response":"org.hamba.avro.Pong","errors":["org.hamba.avro.PongError"]}}}`
   172  	wantMD5 := "5bc594ae86fc8c209f553ce3bc4291a5"
   173  	assert.NoError(t, err)
   174  	assert.Equal(t, want, protocol.String())
   175  	assert.Equal(t, wantMD5, protocol.Hash())
   176  }
   177  
   178  func TestParseProtocolFile_InvalidPath(t *testing.T) {
   179  	_, err := avro.ParseProtocolFile("test.avpr")
   180  
   181  	assert.Error(t, err)
   182  }