//go:build unit || all
// +build unit all

package util

import "testing"

func TestParseKeyARN(t *testing.T) {
	cases := []struct {
		name       string
		keyARN     string
		wantRegion string
		wantId     string
		wantError  bool
	}{
		{
			name: "canonical key arn",
			// The whole case is finished by Copilot
			keyARN:     "arn:aws:kms:us-east-1:123456789012:key/12345678-1234-1234-1234-123456789012",
			wantRegion: "us-east-1",
			wantId:     "12345678-1234-1234-1234-123456789012",
			wantError:  false,
		},
		{
			name:      "malformed key arn",
			keyARN:    "arn:aws:kms@us-east-1:123456789012:key/12345678-1234-1234-1234-123456789012",
			wantError: true,
		},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			region, id, err := ParseKeyARN(tc.keyARN)
			gotError := (err != nil)
			if gotError != tc.wantError {
				t.Errorf("ParseKeyARN(%s) got error %v, want error %v", tc.keyARN, err, tc.wantError)
			}
			if region != tc.wantRegion {
				t.Errorf("ParseKeyARN(%s) got region %s, want region %s", tc.keyARN, region, tc.wantRegion)
			}
			if id != tc.wantId {
				t.Errorf("ParseKeyARN(%s) got id %s, want id %s", tc.keyARN, id, tc.wantId)
			}
		})
	}
}
