Skip to content

Commit

Permalink
Add unit tests for json.Canonicalize
Browse files Browse the repository at this point in the history
Signed-off-by: Alper Rifat Ulucinar <[email protected]>
  • Loading branch information
ulucinar committed Feb 14, 2024
1 parent 13e9fc0 commit 3372192
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
67 changes: 67 additions & 0 deletions pkg/resource/json/canonical_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// SPDX-FileCopyrightText: 2024 The Crossplane Authors <https://crossplane.io>
//
// SPDX-License-Identifier: Apache-2.0

package json

import (
"os"
"path/filepath"
"strings"
"testing"

"github.com/crossplane/crossplane-runtime/pkg/test"
"github.com/google/go-cmp/cmp"
"github.com/pkg/errors"
)

func TestCanonicalize(t *testing.T) {
tests := map[string]struct {
inputFile string
expectedFile string
err error
}{
"SuccessfulConversion": {
inputFile: "policy.json",
expectedFile: "policy_canonical.json",
},
"NoopConversion": {
inputFile: "policy_canonical.json",
expectedFile: "policy_canonical.json",
},
"InvalidJSON": {
inputFile: "invalid.json",
err: errors.Wrap(errors.New(`ReadString: expects " or n, but found }, error found in #10 byte of ...|"a": "b",}|..., bigger context ...|{"a": "b",}|...`), `failed to unmarshal the JSON document: {"a": "b",}`),
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
input, err := os.ReadFile(filepath.Join("testdata", tc.inputFile))
if err != nil {
t.Fatalf("Failed to read the input file: %v", err)
}

expectedOutput := ""
if tc.expectedFile != "" {
output, err := os.ReadFile(filepath.Join("testdata", tc.expectedFile))
if err != nil {
t.Fatalf("Failed to read expected the output file: %v", err)
}
expectedOutput = strings.Join(strings.Split(strings.TrimSpace(string(output)), "\n")[3:], "\n")
}

inputJSON := strings.Join(strings.Split(strings.TrimSpace(string(input)), "\n")[3:], "\n")
canonicalJSON, err := Canonicalize(inputJSON)
if err != nil {
if diff := cmp.Diff(tc.err, err, test.EquateErrors()); diff != "" {
t.Fatalf("Canonicalize(...): -wantErr, +gotErr: %s", diff)
}
return
}
if diff := cmp.Diff(expectedOutput, canonicalJSON); diff != "" {
t.Errorf("Canonicalize(...): -want, +got: \n%s", diff)
}
})
}
}
4 changes: 4 additions & 0 deletions pkg/resource/json/testdata/invalid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// SPDX-FileCopyrightText: 2024 The Crossplane Authors <https://crossplane.io>
//
// SPDX-License-Identifier: Apache-2.0
{"a": "b",}
14 changes: 14 additions & 0 deletions pkg/resource/json/testdata/policy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-FileCopyrightText: 2024 The Crossplane Authors <https://crossplane.io>
//
// SPDX-License-Identifier: Apache-2.0
{
"Rules": [
{
"ResourceType": "collection",
"Resource": [
"collection/example-collection-2"
]
}
],
"AWSOwnedKey": true
}
4 changes: 4 additions & 0 deletions pkg/resource/json/testdata/policy_canonical.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// SPDX-FileCopyrightText: 2024 The Crossplane Authors <https://crossplane.io>
//
// SPDX-License-Identifier: Apache-2.0
{"AWSOwnedKey":true,"Rules":[{"Resource":["collection/example-collection-2"],"ResourceType":"collection"}]}

0 comments on commit 3372192

Please sign in to comment.