| 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 jsontestingscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/util/jsontesting`/ |
| 18 | |
| 19 | import ( |
| 20 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.21 `encoding/json`/" |
| 21 | "fmtscip-go gomod github.com/golang/go/src go1.21 fmt/" |
| 22 | "reflectscip-go gomod github.com/golang/go/src go1.21 reflect/" |
| 23 | "stringsscip-go gomod github.com/golang/go/src go1.21 strings/" |
| 24 | |
| 25 | kjsonscip-go gomod sigs.k8s.io/json bc3834ca7abd `sigs.k8s.io/json`/ "sigs.k8s.io/jsonscip-go gomod sigs.k8s.io/json bc3834ca7abd `sigs.k8s.io/json`/" |
| 26 | |
| 27 | "github.com/go-openapi/jsonreferencescip-go gomod github.com/go-openapi/jsonreference v0.20.1 `github.com/go-openapi/jsonreference`/" |
| 28 | "github.com/google/go-cmp/cmpscip-go gomod github.com/google/go-cmp v0.6.0 `github.com/google/go-cmp/cmp`/" |
| 29 | "github.com/google/go-cmp/cmp/cmpoptsscip-go gomod github.com/google/go-cmp v0.6.0 `github.com/google/go-cmp/cmp/cmpopts`/" |
| 30 | ) |
| 31 | |
| 32 | func JsonComparescip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/util/jsontesting`/JsonCompare().(got, want []byte) error { |
| 33 | if d := cmpscip-go gomod github.com/google/go-cmp v0.6.0 `github.com/google/go-cmp/cmp`/.Diffscip-go gomod github.com/google/go-cmp v0.6.0 `github.com/google/go-cmp/cmp`/Diff().(got, want, cmpscip-go gomod github.com/google/go-cmp v0.6.0 `github.com/google/go-cmp/cmp`/.Transformerscip-go gomod github.com/google/go-cmp v0.6.0 `github.com/google/go-cmp/cmp`/Transformer().("JSONBytes", func(in []byte) (out interface{}) { |
| 34 | if strictErrors, err := kjsonscip-go gomod sigs.k8s.io/json bc3834ca7abd `sigs.k8s.io/json`/.UnmarshalStrictscip-go gomod sigs.k8s.io/json bc3834ca7abd `sigs.k8s.io/json`/UnmarshalStrict().(in, &out); strictErrors != nil || err != nil { |
| 35 | return in |
| 36 | } |
| 37 | return out |
| 38 | })); d != "" { |
| 39 | return fmtscip-go gomod github.com/golang/go/src go1.21 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.21 fmt/Errorf().("JSON mismatch (-got +want):\n%s", d) |
| 40 | } |
| 41 | return nil |
| 42 | } |
| 43 | |
| 44 | type RoundTripTestCasescip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/util/jsontesting`/RoundTripTestCase# struct { |
| 45 | Namescip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/util/jsontesting`/RoundTripTestCase#Name. string |
| 46 | JSONscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/util/jsontesting`/RoundTripTestCase#JSON. string |
| 47 | Objectscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/util/jsontesting`/RoundTripTestCase#Object. jsonscip-go gomod github.com/golang/go/src go1.21 `encoding/json`/.Marshalerscip-go gomod github.com/golang/go/src go1.21 `encoding/json`/Marshaler# |
| 48 | |
| 49 | // An error that is expected when `Object` is marshalled to json |
| 50 | // If `Object` does not exist, then it is inferred from the provided JSON |
| 51 | ExpectedMarshalErrorscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/util/jsontesting`/RoundTripTestCase#ExpectedMarshalError. string |
| 52 | |
| 53 | // An error that is expected when the provided JSON is unmarshalled |
| 54 | // If `JSON` does not exist then this it is inferred from the provided `Object` |
| 55 | ExpectedUnmarshalErrorscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/util/jsontesting`/RoundTripTestCase#ExpectedUnmarshalError. string |
| 56 | } |
| 57 | |
| 58 | type MarshalerUnmarshalerscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/util/jsontesting`/MarshalerUnmarshaler# interface { |
| 59 | jsonscip-go gomod github.com/golang/go/src go1.21 `encoding/json`/.Unmarshalerscip-go gomod github.com/golang/go/src go1.21 `encoding/json`/Unmarshaler# |
| 60 | jsonscip-go gomod github.com/golang/go/src go1.21 `encoding/json`/.Marshalerscip-go gomod github.com/golang/go/src go1.21 `encoding/json`/Marshaler# |
| 61 | } |
| 62 | |
| 63 | func (t RoundTripTestCasescip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/util/jsontesting`/RoundTripTestCase#) RoundTripTestscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/util/jsontesting`/RoundTripTestCase#RoundTripTest().(example MarshalerUnmarshalerscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/util/jsontesting`/MarshalerUnmarshaler#) error { |
| 64 | var jsonBytes []byte |
| 65 | var err error |
| 66 | |
| 67 | // Tests whether the provided error matches the given pattern, and says |
| 68 | // whether the test is finished, and the error to return |
| 69 | expectError := func(e error, name string, expected string) (testFinished bool, err error) { |
| 70 | if len(expected) > 0 { |
| 71 | if e == nil || !stringsscip-go gomod github.com/golang/go/src go1.21 strings/.Containsscip-go gomod github.com/golang/go/src go1.21 strings/Contains().(e.Error(), expected) { |
| 72 | return true, fmtscip-go gomod github.com/golang/go/src go1.21 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.21 fmt/Errorf().("expected %v error containing substring: '%s'. but got actual error '%v'", name, expected, e) |
| 73 | } |
| 74 | |
| 75 | // If an error was expected and achieved, we stop the test |
| 76 | // since it cannot be continued. But the return nil error since it |
| 77 | // was expected. |
| 78 | return true, nil |
| 79 | } else if e != nil { |
| 80 | return true, fmtscip-go gomod github.com/golang/go/src go1.21 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.21 fmt/Errorf().("unexpected %v error: %w", name, e) |
| 81 | } |
| 82 | |
| 83 | return false, nil |
| 84 | } |
| 85 | |
| 86 | // If user did not provide JSON and instead provided Object, infer JSON |
| 87 | // from the provided object. |
| 88 | if len(t.JSONscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/util/jsontesting`/RoundTripTestCase#JSON.) == 0 { |
| 89 | jsonBytes, err = jsonscip-go gomod github.com/golang/go/src go1.21 `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src go1.21 `encoding/json`/Marshal().(t.Objectscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/util/jsontesting`/RoundTripTestCase#Object.) |
| 90 | if testFinished, err := expectError(err, "marshal", t.ExpectedMarshalErrorscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/util/jsontesting`/RoundTripTestCase#ExpectedMarshalError.); testFinished { |
| 91 | return err |
| 92 | } |
| 93 | } else { |
| 94 | jsonBytes = []byte(t.JSONscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/util/jsontesting`/RoundTripTestCase#JSON.) |
| 95 | } |
| 96 | |
| 97 | err = example.UnmarshalJSONscip-go gomod github.com/golang/go/src go1.21 `encoding/json`/Unmarshaler#UnmarshalJSON().(jsonBytes) |
| 98 | if testFinished, err := expectError(err, "unmarshal", t.ExpectedUnmarshalErrorscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/util/jsontesting`/RoundTripTestCase#ExpectedUnmarshalError.); testFinished { |
| 99 | return err |
| 100 | } |
| 101 | |
| 102 | if t.Objectscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/util/jsontesting`/RoundTripTestCase#Object. != nil && !reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.21 reflect/DeepEqual().(t.Objectscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/util/jsontesting`/RoundTripTestCase#Object., example) { |
| 103 | return fmtscip-go gomod github.com/golang/go/src go1.21 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.21 fmt/Errorf().("test case expected to unmarshal to specific value: %v", cmpscip-go gomod github.com/google/go-cmp v0.6.0 `github.com/google/go-cmp/cmp`/.Diffscip-go gomod github.com/google/go-cmp v0.6.0 `github.com/google/go-cmp/cmp`/Diff().(t.Objectscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/util/jsontesting`/RoundTripTestCase#Object., example, cmpoptsscip-go gomod github.com/google/go-cmp v0.6.0 `github.com/google/go-cmp/cmp/cmpopts`/.IgnoreUnexportedscip-go gomod github.com/google/go-cmp v0.6.0 `github.com/google/go-cmp/cmp/cmpopts`/IgnoreUnexported().(jsonreferencescip-go gomod github.com/go-openapi/jsonreference v0.20.1 `github.com/go-openapi/jsonreference`/.Refscip-go gomod github.com/go-openapi/jsonreference v0.20.1 `github.com/go-openapi/jsonreference`/Ref#{}))) |
| 104 | } |
| 105 | |
| 106 | reEncoded, err := jsonscip-go gomod github.com/golang/go/src go1.21 `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src go1.21 `encoding/json`/Marshal().(example) |
| 107 | if err != nil { |
| 108 | return fmtscip-go gomod github.com/golang/go/src go1.21 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.21 fmt/Errorf().("failed to marshal decoded value: %w", err) |
| 109 | } |
| 110 | |
| 111 | // Check expected marshal error if it has not yet been checked |
| 112 | // (for case where JSON is provided, and object is not) |
| 113 | if testFinished, err := expectError(err, "marshal", t.ExpectedMarshalErrorscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/util/jsontesting`/RoundTripTestCase#ExpectedMarshalError.); testFinished { |
| 114 | return err |
| 115 | } |
| 116 | // Marshal both re-encoded, and original JSON into interface |
| 117 | // to compare them without ordering issues |
| 118 | var expected map[string]interface{} |
| 119 | var actual map[string]interface{} |
| 120 | |
| 121 | if err = jsonscip-go gomod github.com/golang/go/src go1.21 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.21 `encoding/json`/Unmarshal().(jsonBytes, &expected); err != nil { |
| 122 | return fmtscip-go gomod github.com/golang/go/src go1.21 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.21 fmt/Errorf().("failed to unmarshal test json: %w", err) |
| 123 | } |
| 124 | |
| 125 | if err = jsonscip-go gomod github.com/golang/go/src go1.21 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.21 `encoding/json`/Unmarshal().(reEncoded, &actual); err != nil { |
| 126 | return fmtscip-go gomod github.com/golang/go/src go1.21 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.21 fmt/Errorf().("failed to unmarshal actual data: %w", err) |
| 127 | } |
| 128 | |
| 129 | if !reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.21 reflect/DeepEqual().(expected, actual) { |
| 130 | return fmtscip-go gomod github.com/golang/go/src go1.21 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.21 fmt/Errorf().("expected equal values: %v", cmpscip-go gomod github.com/google/go-cmp v0.6.0 `github.com/google/go-cmp/cmp`/.Diffscip-go gomod github.com/google/go-cmp v0.6.0 `github.com/google/go-cmp/cmp`/Diff().(expected, actual)) |
| 131 | } |
| 132 | |
| 133 | return nil |
| 134 | } |