github.com/vmware/go-vcloud-director/v2@v2.24.0/govcd/adminorg_unit_test.go (about)

     1  //go:build unit || ALL
     2  
     3  /*
     4  * Copyright 2019 VMware, Inc.  All rights reserved.  Licensed under the Apache v2 License.
     5   */
     6  
     7  package govcd
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/vmware/go-vcloud-director/v2/types/v56"
    13  )
    14  
    15  // Tests that equalIds returns the expected result whether the target reference
    16  // contains or not an ID
    17  func Test_equalIds(t *testing.T) {
    18  
    19  	type testData struct {
    20  		wanted    string
    21  		reference types.Reference
    22  		expected  bool
    23  	}
    24  	var testItems = []testData{
    25  		{
    26  			// Regular case: all values are set in the reference
    27  			wanted: "urn:vcloud:catalog:97384890-180c-4563-b9b7-0dc50a2430b0",
    28  			reference: types.Reference{
    29  				Name: "all_values",
    30  				HREF: "https://vcd.somecompany.org/api/entity/97384890-180c-4563-b9b7-0dc50a2430b0",
    31  				ID:   "urn:vcloud:catalog:97384890-180c-4563-b9b7-0dc50a2430b0",
    32  			},
    33  			expected: true,
    34  		},
    35  		{
    36  			// Catalog Item case: The ID is a simple UUID
    37  			wanted: "urn:vcloud:catalogitem:97384890-180c-4563-b9b7-0dc50a2430b0",
    38  			reference: types.Reference{
    39  				Name: "all_values",
    40  				HREF: "https://vcd.somecompany.org/api/entity/97384890-180c-4563-b9b7-0dc50a2430b0",
    41  				ID:   "97384890-180c-4563-b9b7-0dc50a2430b0",
    42  			},
    43  			expected: true,
    44  		},
    45  		{
    46  			// Regular case: all values are set in the reference but wanted is empty
    47  			wanted: "",
    48  			reference: types.Reference{
    49  				Name: "all_values",
    50  				HREF: "https://vcd.somecompany.org/api/entity/97384890-180c-4563-b9b7-0dc50a2430b0",
    51  				ID:   "urn:vcloud:catalog:97384890-180c-4563-b9b7-0dc50a2430b0",
    52  			},
    53  			expected: false,
    54  		},
    55  		{
    56  			// wanted and ID are different
    57  			wanted: "urn:vcloud:catalog:97384890-180c-4563-b9b7-0dc50a2430b0",
    58  			reference: types.Reference{
    59  				Name: "not_matching",
    60  				HREF: "https://vcd.somecompany.org/api/entity/97384890-180c-4563-b9b7-0dc50a2430b0",
    61  				ID:   "urn:vcloud:catalog:97384890-180c-4563-b9b7-0dc50a2430b1",
    62  			},
    63  			expected: false,
    64  		},
    65  		{
    66  			// Missing ID, the match happens with the HREF (as in VDC)
    67  			wanted: "urn:vcloud:catalog:97384890-180c-4563-b9b7-0dc50a2430b0",
    68  			reference: types.Reference{
    69  				Name: "no_id",
    70  				HREF: "https://vcd.somecompany.org/api/entity/97384890-180c-4563-b9b7-0dc50a2430b0",
    71  				ID:   "",
    72  			},
    73  			expected: true,
    74  		},
    75  		{
    76  			// Missing ID, the UUID in the HREF is different from the one in wanted, will fail
    77  			wanted: "urn:vcloud:catalog:97384890-180c-4563-b9b7-0dc50a2430b0",
    78  			reference: types.Reference{
    79  				Name: "no_id_no_matching",
    80  				HREF: "https://vcd.somecompany.org/api/entity/97384890-180c-4563-b9b7-0dc50a2430b1",
    81  				ID:   "",
    82  			},
    83  			expected: false,
    84  		},
    85  		{
    86  			// all bogus values. Matches by ID
    87  			wanted: "urn:vcloud:catalog:deadbeef-0000-0000-0000-000000000000",
    88  			reference: types.Reference{
    89  				Name: "all_dummy_values",
    90  				HREF: "https://vcd.somecompany.org/api/entity/deadbeef-0000-0000-0000-000000000000",
    91  				ID:   "urn:vcloud:catalog:deadbeef-0000-0000-0000-000000000000",
    92  			},
    93  			expected: true,
    94  		},
    95  		{
    96  			// Missing both ID and HREF, will fail
    97  			wanted: "urn:vcloud:catalog:deadbeef-0000-0000-0000-000000000000",
    98  			reference: types.Reference{
    99  				Name: "missing_ids",
   100  				HREF: "",
   101  				ID:   "",
   102  			},
   103  			expected: false,
   104  		},
   105  		{
   106  			// URL has ID also case
   107  			wanted: "urn:vcloud:catalogitem:97384890-180c-4563-b9b7-0dc50a2430b0",
   108  			reference: types.Reference{
   109  				Name: "url_with_id",
   110  				HREF: "https://vcd-a8bbe9be-13f2-4ce7-9187-d0d075c42531.cds.cloud.vmware.com/api/entity/97384890-180c-4563-b9b7-0dc50a2430b0",
   111  			},
   112  			expected: true,
   113  		},
   114  	}
   115  	for _, item := range testItems {
   116  		result := equalIds(item.wanted, item.reference.ID, item.reference.HREF)
   117  		if result == item.expected {
   118  			if testVerbose {
   119  				t.Logf("Test:     %s\nExpected: %v\nwanted:   '%s'\nID:       '%s'\nHREF:     '%s'",
   120  					item.reference.Name, item.expected, item.wanted, item.reference.ID, item.reference.HREF)
   121  			}
   122  		} else {
   123  			t.Logf("Test:     %s\nExpected: %v\nwanted:   '%s'\nID:       '%s'\nHREF:     '%s'",
   124  				item.reference.Name, item.expected, item.wanted, item.reference.ID, item.reference.HREF)
   125  			t.Fail()
   126  		}
   127  	}
   128  }