|
| 1 | +package generic |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/google/go-cmp/cmp" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/tfsdk" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 11 | + "github.com/hashicorp/terraform-plugin-go/tftypes" |
| 12 | + "github.com/hashicorp/terraform-provider-awscc/internal/tfresource" |
| 13 | +) |
| 14 | + |
| 15 | +func TestJSONString(t *testing.T) { |
| 16 | + t.Parallel() |
| 17 | + |
| 18 | + type testCase struct { |
| 19 | + plannedValue attr.Value |
| 20 | + currentValue attr.Value |
| 21 | + expectedValue attr.Value |
| 22 | + expectError bool |
| 23 | + } |
| 24 | + tests := map[string]testCase{ |
| 25 | + "planned not string": { |
| 26 | + plannedValue: types.Int64{Value: 1}, |
| 27 | + currentValue: types.String{Value: `{}`}, |
| 28 | + expectError: true, |
| 29 | + }, |
| 30 | + "current not string": { |
| 31 | + plannedValue: types.String{Value: `{}`}, |
| 32 | + currentValue: types.Int64{Value: 1}, |
| 33 | + expectError: true, |
| 34 | + }, |
| 35 | + "exactly equal": { |
| 36 | + plannedValue: types.String{Value: `{}`}, |
| 37 | + currentValue: types.String{Value: `{}`}, |
| 38 | + expectedValue: types.String{Value: `{}`}, |
| 39 | + }, |
| 40 | + "leading and trailing whitespace": { |
| 41 | + plannedValue: types.String{Value: ` {}`}, |
| 42 | + currentValue: types.String{Value: `{} `}, |
| 43 | + expectedValue: types.String{Value: `{} `}, |
| 44 | + }, |
| 45 | + "not equal": { |
| 46 | + plannedValue: types.String{Value: `{"k1": 42}`}, |
| 47 | + currentValue: types.String{Value: `{"k1": -1}`}, |
| 48 | + expectedValue: types.String{Value: `{"k1": 42}`}, |
| 49 | + }, |
| 50 | + "fields reordered": { |
| 51 | + plannedValue: types.String{Value: `{"k2": ["v2", {"k3": true}], "k1": 42 }`}, |
| 52 | + currentValue: types.String{Value: `{"k1": 42, "k2": ["v2", {"k3": true}]}`}, |
| 53 | + expectedValue: types.String{Value: `{"k1": 42, "k2": ["v2", {"k3": true}]}`}, |
| 54 | + }, |
| 55 | + } |
| 56 | + |
| 57 | + for name, test := range tests { |
| 58 | + name, test := name, test |
| 59 | + t.Run(name, func(t *testing.T) { |
| 60 | + ctx := context.TODO() |
| 61 | + request := tfsdk.ModifyAttributePlanRequest{ |
| 62 | + AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), |
| 63 | + AttributePlan: test.plannedValue, |
| 64 | + AttributeState: test.currentValue, |
| 65 | + } |
| 66 | + response := tfsdk.ModifyAttributePlanResponse{} |
| 67 | + JSONString().Modify(ctx, request, &response) |
| 68 | + |
| 69 | + if !response.Diagnostics.HasError() && test.expectError { |
| 70 | + t.Fatal("expected error, got no error") |
| 71 | + } |
| 72 | + |
| 73 | + if response.Diagnostics.HasError() && !test.expectError { |
| 74 | + t.Fatalf("got unexpected error: %s", tfresource.DiagsError(response.Diagnostics)) |
| 75 | + } |
| 76 | + |
| 77 | + if diff := cmp.Diff(response.AttributePlan, test.expectedValue); diff != "" { |
| 78 | + t.Errorf("unexpected diff (+wanted, -got): %s", diff) |
| 79 | + } |
| 80 | + }) |
| 81 | + } |
| 82 | +} |
0 commit comments