github.com/aiven/aiven-go-client@v1.36.0/service_user_test.go (about)

     1  package aiven
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"reflect"
     7  	"testing"
     8  )
     9  
    10  // TestAccessControlMarshalJSON test AccessControl JSON marshalling process
    11  func TestAccessControlMarshalJSON(t *testing.T) {
    12  	strPtr := func(s string) *string {
    13  		return &s
    14  	}
    15  	type notEmpty struct {
    16  		M3Group      string   `json:"m3_group"`
    17  		RedisACLKeys []string `json:"redis_acl_keys"`
    18  	}
    19  
    20  	tests := []struct {
    21  		name     string
    22  		input    *AccessControl
    23  		wantType interface{}
    24  		want     interface{}
    25  	}{
    26  		{
    27  			name:     "empty",
    28  			input:    &AccessControl{},
    29  			wantType: &map[string]interface{}{},
    30  			want:     &map[string]interface{}{},
    31  		},
    32  		{
    33  			name: "notempty",
    34  			input: &AccessControl{
    35  				M3Group:      strPtr("foo"),
    36  				RedisACLKeys: []string{"bar"},
    37  			},
    38  			wantType: &notEmpty{},
    39  			want: &notEmpty{
    40  				M3Group:      "foo",
    41  				RedisACLKeys: []string{"bar"},
    42  			},
    43  		},
    44  	}
    45  	for _, tt := range tests {
    46  		t.Run(tt.name, func(t *testing.T) {
    47  			b, err := json.Marshal(tt.input)
    48  			if err != nil {
    49  				t.Errorf("MarshalJSON() error = %v", err)
    50  				return
    51  			}
    52  
    53  			dec := json.NewDecoder(bytes.NewReader(b))
    54  			dec.DisallowUnknownFields()
    55  			if err := dec.Decode(tt.wantType); err != nil {
    56  				t.Errorf("Decode() error = %v", err)
    57  			}
    58  			if !reflect.DeepEqual(tt.wantType, tt.want) {
    59  				t.Errorf("DeepEqual() got = %+v, want = %+v", tt.wantType, tt.want)
    60  			}
    61  		})
    62  	}
    63  }