|
| 1 | +/* |
| 2 | +Copyright 2022 The Kubernetes Authors. |
| 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 jsontesting |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "fmt" |
| 22 | + "reflect" |
| 23 | + "strings" |
| 24 | + |
| 25 | + "github.com/google/go-cmp/cmp" |
| 26 | +) |
| 27 | + |
| 28 | +type RoundTripTestCase struct { |
| 29 | + Name string |
| 30 | + JSON string |
| 31 | + Object json.Marshaler |
| 32 | + |
| 33 | + // An error that is expected when `Object` is marshalled to json |
| 34 | + // If `Object` does not exist, then it is inferred from the provided JSON |
| 35 | + ExpectedMarshalError string |
| 36 | + |
| 37 | + // An error that is expected when the provided JSON is unmarshalled |
| 38 | + // If `JSON` does not exist then this it is inferred from the provided `Object` |
| 39 | + ExpectedUnmarshalError string |
| 40 | +} |
| 41 | + |
| 42 | +func (t RoundTripTestCase) RoundTripTest(example json.Unmarshaler) error { |
| 43 | + var jsonBytes []byte |
| 44 | + var err error |
| 45 | + |
| 46 | + // Tests whether the provided error matches the given pattern, and says |
| 47 | + // whether the test is finished, and the error to return |
| 48 | + expectError := func(e error, name string, expected string) (testFinished bool, err error) { |
| 49 | + if len(expected) > 0 { |
| 50 | + if e == nil || !strings.Contains(e.Error(), expected) { |
| 51 | + return true, fmt.Errorf("expected %v error containing substring: '%s'. but got actual error '%v'", name, expected, e) |
| 52 | + } |
| 53 | + |
| 54 | + // If an error was expected and achieved, we stop the test |
| 55 | + // since it cannot be continued. But the return nil error since it |
| 56 | + // was expected. |
| 57 | + return true, nil |
| 58 | + } else if e != nil { |
| 59 | + return true, fmt.Errorf("unexpected %v error: %w", name, e) |
| 60 | + } |
| 61 | + |
| 62 | + return false, nil |
| 63 | + } |
| 64 | + |
| 65 | + // If user did not provide JSON and instead provided Object, infer JSON |
| 66 | + // from the provided object. |
| 67 | + if len(t.JSON) == 0 { |
| 68 | + jsonBytes, err = json.Marshal(t.Object) |
| 69 | + if testFinished, err := expectError(err, "marshal", t.ExpectedMarshalError); testFinished { |
| 70 | + return err |
| 71 | + } |
| 72 | + } else { |
| 73 | + jsonBytes = []byte(t.JSON) |
| 74 | + } |
| 75 | + |
| 76 | + err = example.UnmarshalJSON(jsonBytes) |
| 77 | + if testFinished, err := expectError(err, "unmarshal", t.ExpectedUnmarshalError); testFinished { |
| 78 | + return err |
| 79 | + } |
| 80 | + |
| 81 | + if t.Object != nil && !reflect.DeepEqual(t.Object, example) { |
| 82 | + return fmt.Errorf("test case expected to unmarshal to specific value: %v", cmp.Diff(t.Object, example)) |
| 83 | + } |
| 84 | + |
| 85 | + reEncoded, err := json.MarshalIndent(example, "", " ") |
| 86 | + if err != nil { |
| 87 | + return fmt.Errorf("failed to marshal decoded value: %w", err) |
| 88 | + } |
| 89 | + |
| 90 | + // Check expected marshal error if it has not yet been checked |
| 91 | + // (for case where JSON is provided, and object is not) |
| 92 | + if testFinished, err := expectError(err, "marshal", t.ExpectedMarshalError); testFinished { |
| 93 | + return err |
| 94 | + } |
| 95 | + // Marshal both re-encoded, and original JSON into interface |
| 96 | + // to compare them without ordering issues |
| 97 | + var expected map[string]interface{} |
| 98 | + var actual map[string]interface{} |
| 99 | + |
| 100 | + if err = json.Unmarshal(jsonBytes, &expected); err != nil { |
| 101 | + return fmt.Errorf("failed to unmarshal test json: %w", err) |
| 102 | + } |
| 103 | + |
| 104 | + if err = json.Unmarshal(reEncoded, &actual); err != nil { |
| 105 | + return fmt.Errorf("failed to unmarshal actual data: %w", err) |
| 106 | + } |
| 107 | + |
| 108 | + if !reflect.DeepEqual(expected, actual) { |
| 109 | + return fmt.Errorf("expected equal values: %v", cmp.Diff(expected, actual)) |
| 110 | + } |
| 111 | + |
| 112 | + return nil |
| 113 | +} |
0 commit comments