github.com/Axway/agent-sdk@v1.1.101/pkg/util/subresource_test.go (about) 1 package util 2 3 import ( 4 "testing" 5 6 "github.com/Axway/agent-sdk/pkg/apic/definitions" 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestGetAgentDetails(t *testing.T) { 11 tests := []struct { 12 name string 13 ri *mockRI 14 expected map[string]interface{} 15 }{ 16 { 17 name: "should return nil if no agent details are found", 18 ri: &mockRI{subResources: map[string]interface{}{}}, 19 expected: nil, 20 }, 21 { 22 name: "should return nil if the agent-details key is found, but is not a map[string]interface{}", 23 ri: &mockRI{subResources: map[string]interface{}{ 24 definitions.XAgentDetails: map[string]string{}, 25 }}, 26 expected: nil, 27 }, 28 { 29 name: "should return the agent details sub resource when saved as a map[string]interface{}", 30 ri: &mockRI{subResources: map[string]interface{}{ 31 definitions.XAgentDetails: map[string]interface{}{}, 32 }}, 33 expected: map[string]interface{}{}, 34 }, 35 } 36 37 for _, tc := range tests { 38 t.Run(tc.name, func(t *testing.T) { 39 val := GetAgentDetails(tc.ri) 40 assert.Equal(t, tc.expected, val) 41 }) 42 } 43 } 44 45 func TestGetAgentDetailStrings(t *testing.T) { 46 tests := []struct { 47 name string 48 ri *mockRI 49 expected map[string]string 50 }{ 51 { 52 name: "should return nil if no agent details are found", 53 ri: &mockRI{subResources: map[string]interface{}{}}, 54 expected: nil, 55 }, 56 { 57 name: "should return nil when the agent-details key is found, but is not a map[string]interface{}", 58 ri: &mockRI{subResources: map[string]interface{}{ 59 definitions.XAgentDetails: map[string]string{}, 60 }}, 61 expected: nil, 62 }, 63 { 64 name: "should return the agent details sub resource when saved as a map[string]interface{}", 65 ri: &mockRI{subResources: map[string]interface{}{ 66 definitions.XAgentDetails: map[string]interface{}{}, 67 }}, 68 expected: map[string]string{}, 69 }, 70 { 71 name: "should map the agent details sub resource as a map[string]string", 72 ri: &mockRI{subResources: map[string]interface{}{ 73 definitions.XAgentDetails: map[string]interface{}{ 74 "key1": "val1", 75 "key2": []string{"val2a", "val2b"}, 76 }, 77 }}, 78 expected: map[string]string{ 79 "key1": "val1", 80 "key2": "[val2a val2b]", 81 }, 82 }, 83 } 84 85 for _, tc := range tests { 86 t.Run(tc.name, func(t *testing.T) { 87 val := GetAgentDetailStrings(tc.ri) 88 assert.Equal(t, tc.expected, val) 89 }) 90 } 91 } 92 93 func TestGetAgentDetailsValue(t *testing.T) { 94 tests := []struct { 95 name string 96 ri *mockRI 97 inputKey string 98 err error 99 expectedItem string 100 hasError bool 101 }{ 102 { 103 name: "should return an empty string and nil if the resource is nil", 104 ri: nil, 105 inputKey: "id", 106 expectedItem: "", 107 hasError: false, 108 }, 109 { 110 name: "should return an empty string and nil if x-agent-details is not found", 111 ri: &mockRI{subResources: map[string]interface{}{}}, 112 inputKey: "id", 113 expectedItem: "", 114 hasError: false, 115 }, 116 { 117 name: "should return an empty string and an error if x-agent-details is not a map[string]interface{}", 118 ri: &mockRI{subResources: map[string]interface{}{ 119 definitions.XAgentDetails: map[string]string{}, 120 }}, 121 inputKey: "id", 122 expectedItem: "", 123 hasError: true, 124 }, 125 { 126 name: "should return an empty string and an error if x-agent-details is found, but the key is not found", 127 ri: &mockRI{subResources: map[string]interface{}{ 128 definitions.XAgentDetails: map[string]interface{}{}, 129 }}, 130 inputKey: "id", 131 expectedItem: "", 132 hasError: true, 133 }, 134 { 135 name: "should return an empty string and an error if x-agent-details is found, and the key is found, but the value is not the correct type", 136 ri: &mockRI{subResources: map[string]interface{}{ 137 definitions.XAgentDetails: map[string]interface{}{ 138 "id": map[string]interface{}{}, 139 }, 140 }}, 141 expectedItem: "", 142 inputKey: "id", 143 hasError: true, 144 }, 145 { 146 name: "should return the x-agent-details value when the key exists, and the value is a string", 147 ri: &mockRI{subResources: map[string]interface{}{ 148 definitions.XAgentDetails: map[string]interface{}{ 149 "id": "123", 150 }, 151 }}, 152 inputKey: "id", 153 hasError: false, 154 expectedItem: "123", 155 }, 156 { 157 name: "should return the x-agent-details value when the key exists, and the value is an int", 158 ri: &mockRI{subResources: map[string]interface{}{ 159 definitions.XAgentDetails: map[string]interface{}{ 160 "id": 123, 161 }, 162 }}, 163 inputKey: "id", 164 hasError: false, 165 expectedItem: "123", 166 }, 167 } 168 169 for _, tc := range tests { 170 t.Run(tc.name, func(t *testing.T) { 171 v, err := GetAgentDetailsValue(tc.ri, tc.inputKey) 172 assert.Equal(t, tc.expectedItem, v) 173 174 if tc.hasError { 175 assert.Error(t, err) 176 } else { 177 assert.Nil(t, err) 178 } 179 }) 180 } 181 } 182 183 func TestSetAgentDetailsKey(t *testing.T) { 184 tests := []struct { 185 name string 186 ri *mockRI 187 err error 188 hasError bool 189 key string 190 value interface{} 191 }{ 192 { 193 name: "should return an error if x-agent-details is not a map[string]interface{}", 194 ri: &mockRI{subResources: map[string]interface{}{ 195 definitions.XAgentDetails: map[string]string{}, 196 }}, 197 hasError: true, 198 key: "id", 199 value: "123", 200 }, 201 { 202 name: "should create the x-agent-details sub resource if it does not exist", 203 ri: &mockRI{subResources: map[string]interface{}{}}, 204 hasError: false, 205 key: "id", 206 value: "123", 207 }, 208 { 209 name: "should add the key and value to x-agent-details", 210 ri: &mockRI{subResources: map[string]interface{}{ 211 definitions.XAgentDetails: map[string]interface{}{}, 212 }}, 213 hasError: false, 214 key: "id", 215 value: "123", 216 }, 217 } 218 219 for _, tc := range tests { 220 t.Run(tc.name, func(t *testing.T) { 221 err := SetAgentDetailsKey(tc.ri, "id", "123") 222 if tc.hasError { 223 assert.Error(t, err) 224 } else { 225 assert.Nil(t, err) 226 s, _ := GetAgentDetailsValue(tc.ri, tc.key) 227 assert.Equal(t, tc.value, s) 228 } 229 }) 230 } 231 } 232 233 func TestSetAgentDetails(t *testing.T) { 234 ri := &mockRI{subResources: map[string]interface{}{}} 235 SetAgentDetails(ri, map[string]interface{}{}) 236 assert.Contains(t, ri.subResources, definitions.XAgentDetails) 237 } 238 239 type mockRI struct { 240 subResources map[string]interface{} 241 } 242 243 func (m *mockRI) GetSubResource(key string) interface{} { 244 if m == nil || m.subResources == nil { 245 return nil 246 } 247 return m.subResources[key] 248 } 249 250 func (m *mockRI) SetSubResource(key string, resource interface{}) { 251 if m == nil { 252 return 253 } 254 255 if m.subResources == nil { 256 m.subResources = make(map[string]interface{}) 257 } 258 m.subResources[key] = resource 259 }