|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package fromproto5_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/google/go-cmp/cmp" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/internal/fromproto5" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/internal/fwschema" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/internal/fwserver" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/list" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/list/schema" |
| 17 | + "github.com/hashicorp/terraform-plugin-framework/tfsdk" |
| 18 | + "github.com/hashicorp/terraform-plugin-go/tfprotov5" |
| 19 | + "github.com/hashicorp/terraform-plugin-go/tftypes" |
| 20 | +) |
| 21 | + |
| 22 | +func TestValidateListResourceConfigRequest(t *testing.T) { |
| 23 | + t.Parallel() |
| 24 | + |
| 25 | + testProto5Type := tftypes.Object{ |
| 26 | + AttributeTypes: map[string]tftypes.Type{ |
| 27 | + "test_attribute": tftypes.String, |
| 28 | + }, |
| 29 | + } |
| 30 | + |
| 31 | + testProto5Value := tftypes.NewValue(testProto5Type, map[string]tftypes.Value{ |
| 32 | + "test_attribute": tftypes.NewValue(tftypes.String, "test-value"), |
| 33 | + }) |
| 34 | + |
| 35 | + testProto5DynamicValue, err := tfprotov5.NewDynamicValue(testProto5Type, testProto5Value) |
| 36 | + |
| 37 | + if err != nil { |
| 38 | + t.Fatalf("unexpected error calling tfprotov5.NewDynamicValue(): %s", err) |
| 39 | + } |
| 40 | + |
| 41 | + testFwSchema := schema.Schema{ |
| 42 | + Attributes: map[string]schema.Attribute{ |
| 43 | + "test_attribute": schema.StringAttribute{ |
| 44 | + Required: true, |
| 45 | + }, |
| 46 | + }, |
| 47 | + } |
| 48 | + |
| 49 | + testCases := map[string]struct { |
| 50 | + input *tfprotov5.ValidateListResourceConfigRequest |
| 51 | + listResourceSchema fwschema.Schema |
| 52 | + listResource list.ListResource |
| 53 | + expected *fwserver.ValidateListResourceConfigRequest |
| 54 | + expectedDiagnostics diag.Diagnostics |
| 55 | + }{ |
| 56 | + "nil": { |
| 57 | + input: nil, |
| 58 | + expected: nil, |
| 59 | + }, |
| 60 | + "empty": { |
| 61 | + input: &tfprotov5.ValidateListResourceConfigRequest{}, |
| 62 | + expected: &fwserver.ValidateListResourceConfigRequest{}, |
| 63 | + }, |
| 64 | + "config-missing-schema": { |
| 65 | + input: &tfprotov5.ValidateListResourceConfigRequest{ |
| 66 | + Config: &testProto5DynamicValue, |
| 67 | + }, |
| 68 | + expected: &fwserver.ValidateListResourceConfigRequest{}, |
| 69 | + expectedDiagnostics: diag.Diagnostics{ |
| 70 | + diag.NewErrorDiagnostic( |
| 71 | + "Unable to Convert Configuration", |
| 72 | + "An unexpected error was encountered when converting the configuration from the protocol type. "+ |
| 73 | + "This is always an issue in terraform-plugin-framework used to implement the provider and should be reported to the provider developers.\n\n"+ |
| 74 | + "Please report this to the provider developer:\n\n"+ |
| 75 | + "Missing schema.", |
| 76 | + ), |
| 77 | + }, |
| 78 | + }, |
| 79 | + "config": { |
| 80 | + input: &tfprotov5.ValidateListResourceConfigRequest{ |
| 81 | + Config: &testProto5DynamicValue, |
| 82 | + }, |
| 83 | + listResourceSchema: testFwSchema, |
| 84 | + expected: &fwserver.ValidateListResourceConfigRequest{ |
| 85 | + Config: &tfsdk.Config{ |
| 86 | + Raw: testProto5Value, |
| 87 | + Schema: testFwSchema, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + } |
| 92 | + |
| 93 | + for name, testCase := range testCases { |
| 94 | + t.Run(name, func(t *testing.T) { |
| 95 | + t.Parallel() |
| 96 | + |
| 97 | + got, diags := fromproto5.ValidateListResourceConfigRequest(context.Background(), testCase.input, testCase.listResource, testCase.listResourceSchema) |
| 98 | + |
| 99 | + if diff := cmp.Diff(got, testCase.expected); diff != "" { |
| 100 | + t.Errorf("unexpected difference: %s", diff) |
| 101 | + } |
| 102 | + |
| 103 | + if diff := cmp.Diff(diags, testCase.expectedDiagnostics); diff != "" { |
| 104 | + t.Errorf("unexpected diagnostics difference: %s", diff) |
| 105 | + } |
| 106 | + }) |
| 107 | + } |
| 108 | +} |
0 commit comments