2022-04-11 04:31:10 -04:00
|
|
|
package role
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMarshal(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
role Role
|
|
|
|
wantJson string
|
|
|
|
wantErr bool
|
2022-04-11 04:31:10 -04:00
|
|
|
}{
|
|
|
|
"coordinator role": {
|
2022-04-26 10:54:05 -04:00
|
|
|
role: Coordinator,
|
|
|
|
wantJson: `"Coordinator"`,
|
2022-04-11 04:31:10 -04:00
|
|
|
},
|
|
|
|
"node role": {
|
2022-04-26 10:54:05 -04:00
|
|
|
role: Node,
|
|
|
|
wantJson: `"Node"`,
|
2022-04-11 04:31:10 -04:00
|
|
|
},
|
2022-04-13 06:39:55 -04:00
|
|
|
"admin role": {
|
2022-04-26 10:54:05 -04:00
|
|
|
role: Admin,
|
|
|
|
wantJson: `"Admin"`,
|
2022-04-13 06:39:55 -04:00
|
|
|
},
|
2022-04-11 04:31:10 -04:00
|
|
|
"unknown role": {
|
2022-04-26 10:54:05 -04:00
|
|
|
role: Unknown,
|
|
|
|
wantJson: `"Unknown"`,
|
2022-04-11 04:31:10 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
jsonRole, err := tc.role.MarshalJSON()
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-04-11 04:31:10 -04:00
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(err)
|
2022-04-26 10:54:05 -04:00
|
|
|
assert.Equal(tc.wantJson, string(jsonRole))
|
2022-04-11 04:31:10 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshal(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
json string
|
|
|
|
wantRole Role
|
|
|
|
wantErr bool
|
2022-04-11 04:31:10 -04:00
|
|
|
}{
|
|
|
|
"Coordinator can be unmarshaled": {
|
2022-04-26 10:54:05 -04:00
|
|
|
json: `"Coordinator"`,
|
|
|
|
wantRole: Coordinator,
|
2022-04-11 04:31:10 -04:00
|
|
|
},
|
|
|
|
"lowercase coordinator can be unmarshaled": {
|
2022-04-26 10:54:05 -04:00
|
|
|
json: `"coordinator"`,
|
|
|
|
wantRole: Coordinator,
|
2022-04-11 04:31:10 -04:00
|
|
|
},
|
|
|
|
"Node can be unmarshaled": {
|
2022-04-26 10:54:05 -04:00
|
|
|
json: `"Node"`,
|
|
|
|
wantRole: Node,
|
2022-04-11 04:31:10 -04:00
|
|
|
},
|
|
|
|
"lowercase node can be unmarshaled": {
|
2022-04-26 10:54:05 -04:00
|
|
|
json: `"node"`,
|
|
|
|
wantRole: Node,
|
2022-04-11 04:31:10 -04:00
|
|
|
},
|
2022-04-13 06:39:55 -04:00
|
|
|
"Admin can be unmarshaled": {
|
2022-04-26 10:54:05 -04:00
|
|
|
json: `"Admin"`,
|
|
|
|
wantRole: Admin,
|
2022-04-13 06:39:55 -04:00
|
|
|
},
|
|
|
|
"lowercase admin can be unmarshaled": {
|
2022-04-26 10:54:05 -04:00
|
|
|
json: `"admin"`,
|
|
|
|
wantRole: Admin,
|
2022-04-13 06:39:55 -04:00
|
|
|
},
|
2022-04-11 04:31:10 -04:00
|
|
|
"other strings unmarshal to the unknown role": {
|
2022-04-26 10:54:05 -04:00
|
|
|
json: `"anything"`,
|
|
|
|
wantRole: Unknown,
|
2022-04-11 04:31:10 -04:00
|
|
|
},
|
|
|
|
"invalid json fails": {
|
2022-04-26 10:54:05 -04:00
|
|
|
json: `"unterminated string literal`,
|
|
|
|
wantErr: true,
|
2022-04-11 04:31:10 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
var role Role
|
|
|
|
err := role.UnmarshalJSON([]byte(tc.json))
|
|
|
|
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-04-11 04:31:10 -04:00
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(err)
|
2022-04-26 10:54:05 -04:00
|
|
|
assert.Equal(tc.wantRole, role)
|
2022-04-11 04:31:10 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|