github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/event/targetid_test.go (about) 1 // Copyright (c) 2015-2021 MinIO, Inc. 2 // 3 // This file is part of MinIO Object Storage stack 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package event 19 20 import ( 21 "reflect" 22 "testing" 23 ) 24 25 func TestTargetDString(t *testing.T) { 26 testCases := []struct { 27 tid TargetID 28 expectedResult string 29 }{ 30 {TargetID{}, ":"}, 31 {TargetID{"1", "webhook"}, "1:webhook"}, 32 {TargetID{"httpclient+2e33cdee-fbec-4bdd-917e-7d8e3c5a2531", "localhost:55638"}, "httpclient+2e33cdee-fbec-4bdd-917e-7d8e3c5a2531:localhost:55638"}, 33 } 34 35 for i, testCase := range testCases { 36 result := testCase.tid.String() 37 38 if result != testCase.expectedResult { 39 t.Fatalf("test %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result) 40 } 41 } 42 } 43 44 func TestTargetDToARN(t *testing.T) { 45 tid := TargetID{"1", "webhook"} 46 testCases := []struct { 47 tid TargetID 48 region string 49 expectedARN ARN 50 }{ 51 {tid, "", ARN{TargetID: tid, region: ""}}, 52 {tid, "us-east-1", ARN{TargetID: tid, region: "us-east-1"}}, 53 } 54 55 for i, testCase := range testCases { 56 arn := testCase.tid.ToARN(testCase.region) 57 58 if arn != testCase.expectedARN { 59 t.Fatalf("test %v: ARN: expected: %v, got: %v", i+1, testCase.expectedARN, arn) 60 } 61 } 62 } 63 64 func TestTargetDMarshalJSON(t *testing.T) { 65 testCases := []struct { 66 tid TargetID 67 expectedData []byte 68 expectErr bool 69 }{ 70 {TargetID{}, []byte(`":"`), false}, 71 {TargetID{"1", "webhook"}, []byte(`"1:webhook"`), false}, 72 {TargetID{"httpclient+2e33cdee-fbec-4bdd-917e-7d8e3c5a2531", "localhost:55638"}, []byte(`"httpclient+2e33cdee-fbec-4bdd-917e-7d8e3c5a2531:localhost:55638"`), false}, 73 } 74 75 for i, testCase := range testCases { 76 data, err := testCase.tid.MarshalJSON() 77 expectErr := (err != nil) 78 79 if expectErr != testCase.expectErr { 80 t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) 81 } 82 83 if !testCase.expectErr { 84 if !reflect.DeepEqual(data, testCase.expectedData) { 85 t.Fatalf("test %v: data: expected: %v, got: %v", i+1, string(testCase.expectedData), string(data)) 86 } 87 } 88 } 89 } 90 91 func TestTargetDUnmarshalJSON(t *testing.T) { 92 testCases := []struct { 93 data []byte 94 expectedTargetID *TargetID 95 expectErr bool 96 }{ 97 {[]byte(`""`), nil, true}, 98 {[]byte(`"httpclient+2e33cdee-fbec-4bdd-917e-7d8e3c5a2531:localhost:55638"`), nil, true}, 99 {[]byte(`":"`), &TargetID{}, false}, 100 {[]byte(`"1:webhook"`), &TargetID{"1", "webhook"}, false}, 101 } 102 103 for i, testCase := range testCases { 104 targetID := &TargetID{} 105 err := targetID.UnmarshalJSON(testCase.data) 106 expectErr := (err != nil) 107 108 if expectErr != testCase.expectErr { 109 t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) 110 } 111 112 if !testCase.expectErr { 113 if *targetID != *testCase.expectedTargetID { 114 t.Fatalf("test %v: TargetID: expected: %v, got: %v", i+1, testCase.expectedTargetID, targetID) 115 } 116 } 117 } 118 }