package tmobtokengen

import (
	"reflect"
	"testing"
)

func TestEHTSMap_SettersNonEmpty(t *testing.T) {
	m := EHTSMap{}
	setters := []struct {
		key    EhtsKey
		setter func(string) EHTSMap
	}{
		{key: ContentType, setter: m.SetContentType},
		{key: Authorization, setter: m.SetAuthorization},
		{key: B2BClient, setter: m.SetB2BClient},
		{key: Body, setter: m.SetBody},
		{key: HTTPMethod, setter: m.SetHTTPMethod},
		{key: URI, setter: m.SetURI},
	}

	for _, s := range setters {
		rs := GenerateUUID()
		m = s.setter(rs)
		if m[s.key] != rs {
			t.Errorf("set%s() = %v, want %v", s.key, m[s.key], rs)
		}
	}
}

func TestEHTSMap_SettersEmpty(t *testing.T) {
	m := EHTSMap{}
	setters := []struct {
		key    EhtsKey
		setter func(string) EHTSMap
	}{
		{key: ContentType, setter: m.SetContentType},
		{key: Authorization, setter: m.SetAuthorization},
		{key: B2BClient, setter: m.SetB2BClient},
		{key: Body, setter: m.SetBody},
		{key: HTTPMethod, setter: m.SetHTTPMethod},
		{key: URI, setter: m.SetURI},
	}

	for _, s := range setters {
		m = s.setter("")
		if _, ok := m[s.key]; ok {
			t.Errorf("ehtsMap[%s] = %v, want no value set", s.key, m[s.key])
		}
	}
}

func Test_ehtsToString(t *testing.T) {
	type args struct {
		ehts map[EhtsKey]string
	}
	tests := []struct {
		name   string
		args   args
		wantks string
		wantvs string
	}{{
		name:   "empty",
		args:   args{ehts: map[EhtsKey]string{}},
		wantks: "",
		wantvs: "",
	}, {
		name:   "one",
		args:   args{ehts: map[EhtsKey]string{ContentType: "value1"}},
		wantks: string(ContentType),
		wantvs: "value1",
	}, {
		name:   "two",
		args:   args{ehts: map[EhtsKey]string{Authorization: "value1", ContentType: "value2"}},
		wantks: string(Authorization) + ";" + string(ContentType),
		wantvs: "value1value2"},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if gotks, gotvs := ehtsToString(tt.args.ehts); gotks != tt.wantks {
				t.Errorf("ehtsToString() = %v, want %v", gotks, tt.wantks)
			} else if gotvs != tt.wantvs {
				t.Errorf("ehtsToString() = %v, want %v", gotvs, tt.wantvs)
			}
		})
	}
}

func Test_encodedEhts(t *testing.T) {
	wantEncoded := []byte("47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU")
	gotEncoded := b64EncodeEHTS(nil)
	if !reflect.DeepEqual(gotEncoded, wantEncoded) {
		t.Errorf("empty-case: b64EncodeEHTS() = %v, want %v", string(gotEncoded), string(wantEncoded))
	}

	wantEncoded = []byte("ungWv48Bz-pBQUDeXa4iI7ADYaOWF3qctBD_YfIAFa0")
	gotEncoded = b64EncodeEHTS([]byte("abc"))
	if !reflect.DeepEqual(gotEncoded, wantEncoded) {
		t.Errorf("non-empty-case: b64EncodeEHTS() = %v, want %v", string(gotEncoded), string(wantEncoded))
	}
}
