|
| 1 | +// +build unit |
| 2 | + |
| 3 | +/** |
| 4 | + * @license |
| 5 | + * Copyright 2020 Dynatrace LLC |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package rest |
| 20 | + |
| 21 | +import ( |
| 22 | + "gotest.tools/assert" |
| 23 | + "testing" |
| 24 | +) |
| 25 | + |
| 26 | +func TestTranslateGenericValuesOnStandardResponse(t *testing.T) { |
| 27 | + |
| 28 | + entry := make(map[string]interface{}) |
| 29 | + entry["id"] = "foo" |
| 30 | + entry["name"] = "bar" |
| 31 | + |
| 32 | + response := make([]interface{}, 1) |
| 33 | + response[0] = entry |
| 34 | + |
| 35 | + values, err := translateGenericValues(response, "extensions") |
| 36 | + |
| 37 | + assert.NilError(t, err) |
| 38 | + assert.Check(t, len(values) == 1) |
| 39 | + |
| 40 | + assert.Equal(t, values[0].Id, "foo") |
| 41 | + assert.Equal(t, values[0].Name, "bar") |
| 42 | +} |
| 43 | + |
| 44 | +func TestTranslateGenericValuesOnIdMissing(t *testing.T) { |
| 45 | + |
| 46 | + entry := make(map[string]interface{}) |
| 47 | + entry["name"] = "bar" |
| 48 | + |
| 49 | + response := make([]interface{}, 1) |
| 50 | + response[0] = entry |
| 51 | + |
| 52 | + _, err := translateGenericValues(response, "extensions") |
| 53 | + |
| 54 | + assert.ErrorContains(t, err, "config of type extensions was invalid: No id") |
| 55 | +} |
| 56 | + |
| 57 | +func TestTranslateGenericValuesOnNameMissing(t *testing.T) { |
| 58 | + |
| 59 | + entry := make(map[string]interface{}) |
| 60 | + entry["id"] = "foo" |
| 61 | + |
| 62 | + response := make([]interface{}, 1) |
| 63 | + response[0] = entry |
| 64 | + |
| 65 | + values, err := translateGenericValues(response, "extensions") |
| 66 | + |
| 67 | + assert.NilError(t, err) |
| 68 | + assert.Check(t, len(values) == 1) |
| 69 | + |
| 70 | + assert.Equal(t, values[0].Id, "foo") |
| 71 | + assert.Equal(t, values[0].Name, "foo") |
| 72 | +} |
0 commit comments