storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/event/targetid_test.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2018 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package event
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  )
    23  
    24  func TestTargetDString(t *testing.T) {
    25  	testCases := []struct {
    26  		tid            TargetID
    27  		expectedResult string
    28  	}{
    29  		{TargetID{}, ":"},
    30  		{TargetID{"1", "webhook"}, "1:webhook"},
    31  		{TargetID{"httpclient+2e33cdee-fbec-4bdd-917e-7d8e3c5a2531", "localhost:55638"}, "httpclient+2e33cdee-fbec-4bdd-917e-7d8e3c5a2531:localhost:55638"},
    32  	}
    33  
    34  	for i, testCase := range testCases {
    35  		result := testCase.tid.String()
    36  
    37  		if result != testCase.expectedResult {
    38  			t.Fatalf("test %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result)
    39  		}
    40  	}
    41  }
    42  
    43  func TestTargetDToARN(t *testing.T) {
    44  	tid := TargetID{"1", "webhook"}
    45  	testCases := []struct {
    46  		tid         TargetID
    47  		region      string
    48  		expectedARN ARN
    49  	}{
    50  		{tid, "", ARN{TargetID: tid, region: ""}},
    51  		{tid, "us-east-1", ARN{TargetID: tid, region: "us-east-1"}},
    52  	}
    53  
    54  	for i, testCase := range testCases {
    55  		arn := testCase.tid.ToARN(testCase.region)
    56  
    57  		if arn != testCase.expectedARN {
    58  			t.Fatalf("test %v: ARN: expected: %v, got: %v", i+1, testCase.expectedARN, arn)
    59  		}
    60  	}
    61  }
    62  
    63  func TestTargetDMarshalJSON(t *testing.T) {
    64  	testCases := []struct {
    65  		tid          TargetID
    66  		expectedData []byte
    67  		expectErr    bool
    68  	}{
    69  		{TargetID{}, []byte(`":"`), false},
    70  		{TargetID{"1", "webhook"}, []byte(`"1:webhook"`), false},
    71  		{TargetID{"httpclient+2e33cdee-fbec-4bdd-917e-7d8e3c5a2531", "localhost:55638"}, []byte(`"httpclient+2e33cdee-fbec-4bdd-917e-7d8e3c5a2531:localhost:55638"`), false},
    72  	}
    73  
    74  	for i, testCase := range testCases {
    75  		data, err := testCase.tid.MarshalJSON()
    76  		expectErr := (err != nil)
    77  
    78  		if expectErr != testCase.expectErr {
    79  			t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
    80  		}
    81  
    82  		if !testCase.expectErr {
    83  			if !reflect.DeepEqual(data, testCase.expectedData) {
    84  				t.Fatalf("test %v: data: expected: %v, got: %v", i+1, string(testCase.expectedData), string(data))
    85  			}
    86  		}
    87  	}
    88  }
    89  
    90  func TestTargetDUnmarshalJSON(t *testing.T) {
    91  	testCases := []struct {
    92  		data             []byte
    93  		expectedTargetID *TargetID
    94  		expectErr        bool
    95  	}{
    96  		{[]byte(`""`), nil, true},
    97  		{[]byte(`"httpclient+2e33cdee-fbec-4bdd-917e-7d8e3c5a2531:localhost:55638"`), nil, true},
    98  		{[]byte(`":"`), &TargetID{}, false},
    99  		{[]byte(`"1:webhook"`), &TargetID{"1", "webhook"}, false},
   100  	}
   101  
   102  	for i, testCase := range testCases {
   103  		targetID := &TargetID{}
   104  		err := targetID.UnmarshalJSON(testCase.data)
   105  		expectErr := (err != nil)
   106  
   107  		if expectErr != testCase.expectErr {
   108  			t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
   109  		}
   110  
   111  		if !testCase.expectErr {
   112  			if *targetID != *testCase.expectedTargetID {
   113  				t.Fatalf("test %v: TargetID: expected: %v, got: %v", i+1, testCase.expectedTargetID, targetID)
   114  			}
   115  		}
   116  	}
   117  }