|
| 1 | +package projection_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/resolver/projection" |
| 7 | + "github.com/operator-framework/operator-registry/pkg/api" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +func TestPropertiesAnnotationFromPropertyList(t *testing.T) { |
| 12 | + for _, tc := range []struct { |
| 13 | + name string |
| 14 | + properties []*api.Property |
| 15 | + expected string |
| 16 | + error bool |
| 17 | + }{ |
| 18 | + { |
| 19 | + name: "nil property slice", |
| 20 | + properties: nil, |
| 21 | + expected: "{}", |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "empty property slice", |
| 25 | + properties: []*api.Property{}, |
| 26 | + expected: "{}", |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "invalid property value", |
| 30 | + properties: []*api.Property{{ |
| 31 | + Type: "bad", |
| 32 | + Value: `]`, |
| 33 | + }}, |
| 34 | + error: true, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "nonempty property slice", |
| 38 | + properties: []*api.Property{ |
| 39 | + { |
| 40 | + Type: "string", |
| 41 | + Value: `"hello"`, |
| 42 | + }, |
| 43 | + { |
| 44 | + Type: "number", |
| 45 | + Value: `5`, |
| 46 | + }, |
| 47 | + { |
| 48 | + Type: "array", |
| 49 | + Value: `[1,"two",3,"four"]`, |
| 50 | + }, { |
| 51 | + Type: "object", |
| 52 | + Value: `{"hello":{"worl":"d"}}`, |
| 53 | + }, |
| 54 | + }, |
| 55 | + expected: `{"properties":[{"type":"string","value":"hello"},{"type":"number","value":5},{"type":"array","value":[1,"two",3,"four"]},{"type":"object","value":{"hello":{"worl":"d"}}}]}`, |
| 56 | + }, |
| 57 | + } { |
| 58 | + t.Run(tc.name, func(t *testing.T) { |
| 59 | + actual, err := projection.PropertiesAnnotationFromPropertyList(tc.properties) |
| 60 | + assert := assert.New(t) |
| 61 | + assert.Equal(tc.expected, actual) |
| 62 | + if tc.error { |
| 63 | + assert.Error(err) |
| 64 | + } else { |
| 65 | + assert.NoError(err) |
| 66 | + } |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestPropertyListFromPropertiesAnnotation(t *testing.T) { |
| 72 | + for _, tc := range []struct { |
| 73 | + name string |
| 74 | + annotation string |
| 75 | + expected []*api.Property |
| 76 | + error bool |
| 77 | + }{ |
| 78 | + { |
| 79 | + name: "empty", |
| 80 | + annotation: "", |
| 81 | + error: true, |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "invalid json", |
| 85 | + annotation: "]", |
| 86 | + error: true, |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "no properties key", |
| 90 | + annotation: "{}", |
| 91 | + expected: nil, |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "properties value not an array or null", |
| 95 | + annotation: `{"properties":5}`, |
| 96 | + error: true, |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "property element not an object", |
| 100 | + annotation: `{"properties":[42]}`, |
| 101 | + error: true, |
| 102 | + }, |
| 103 | + { |
| 104 | + name: "no properties", |
| 105 | + annotation: `{"properties":[]}`, |
| 106 | + expected: nil, |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "several properties", |
| 110 | + annotation: `{"properties":[{"type":"string","value":"hello"},{"type":"number","value":5},{"type":"array","value":[1,"two",3,"four"]},{"type":"object","value":{"hello":{"worl":"d"}}}]}`, |
| 111 | + expected: []*api.Property{ |
| 112 | + { |
| 113 | + Type: "string", |
| 114 | + Value: `"hello"`, |
| 115 | + }, |
| 116 | + { |
| 117 | + Type: "number", |
| 118 | + Value: `5`, |
| 119 | + }, |
| 120 | + { |
| 121 | + Type: "array", |
| 122 | + Value: `[1,"two",3,"four"]`, |
| 123 | + }, { |
| 124 | + Type: "object", |
| 125 | + Value: `{"hello":{"worl":"d"}}`, |
| 126 | + }, |
| 127 | + }, |
| 128 | + }, |
| 129 | + } { |
| 130 | + t.Run(tc.name, func(t *testing.T) { |
| 131 | + actual, err := projection.PropertyListFromPropertiesAnnotation(tc.annotation) |
| 132 | + assert := assert.New(t) |
| 133 | + assert.Equal(tc.expected, actual) |
| 134 | + if tc.error { |
| 135 | + assert.Error(err) |
| 136 | + } else { |
| 137 | + assert.NoError(err) |
| 138 | + } |
| 139 | + }) |
| 140 | + } |
| 141 | +} |
0 commit comments