|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package fwserver_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "slices" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/google/go-cmp/cmp" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/internal/fwserver" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/internal/testing/testprovider" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/list" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/resource/identityschema" |
| 17 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 18 | + "github.com/hashicorp/terraform-plugin-framework/tfsdk" |
| 19 | + "github.com/hashicorp/terraform-plugin-go/tftypes" |
| 20 | +) |
| 21 | + |
| 22 | +func TestServerListResource(t *testing.T) { |
| 23 | + t.Parallel() |
| 24 | + |
| 25 | + testSchema := schema.Schema{ |
| 26 | + Attributes: map[string]schema.Attribute{ |
| 27 | + "test_computed": schema.StringAttribute{ |
| 28 | + Computed: true, |
| 29 | + }, |
| 30 | + "test_required": schema.StringAttribute{ |
| 31 | + Required: true, |
| 32 | + }, |
| 33 | + }, |
| 34 | + } |
| 35 | + |
| 36 | + testType := tftypes.Object{ |
| 37 | + AttributeTypes: map[string]tftypes.Type{ |
| 38 | + "test_attribute": tftypes.String, |
| 39 | + }, |
| 40 | + } |
| 41 | + |
| 42 | + testResourceValue1 := tftypes.NewValue(testType, map[string]tftypes.Value{ |
| 43 | + "test_attribute": tftypes.NewValue(tftypes.String, "test-value-1"), |
| 44 | + }) |
| 45 | + |
| 46 | + testResourceValue2 := tftypes.NewValue(testType, map[string]tftypes.Value{ |
| 47 | + "test_attribute": tftypes.NewValue(tftypes.String, "test-value-2"), |
| 48 | + }) |
| 49 | + |
| 50 | + testIdentitySchema := identityschema.Schema{ |
| 51 | + Attributes: map[string]identityschema.Attribute{ |
| 52 | + "test_id": identityschema.StringAttribute{ |
| 53 | + RequiredForImport: true, |
| 54 | + }, |
| 55 | + }, |
| 56 | + } |
| 57 | + |
| 58 | + testIdentityType := tftypes.Object{ |
| 59 | + AttributeTypes: map[string]tftypes.Type{ |
| 60 | + "test_id": tftypes.String, |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + testIdentityValue1 := tftypes.NewValue(testIdentityType, map[string]tftypes.Value{ |
| 65 | + "test_id": tftypes.NewValue(tftypes.String, "new-id-123"), |
| 66 | + }) |
| 67 | + |
| 68 | + testIdentityValue2 := tftypes.NewValue(testIdentityType, map[string]tftypes.Value{ |
| 69 | + "test_id": tftypes.NewValue(tftypes.String, "new-id-456"), |
| 70 | + }) |
| 71 | + |
| 72 | + // nilIdentityValue := tftypes.NewValue(testIdentityType, nil) |
| 73 | + |
| 74 | + testCases := map[string]struct { |
| 75 | + server *fwserver.Server |
| 76 | + request *fwserver.ListRequest |
| 77 | + expectedStreamEvents []fwserver.ListResult |
| 78 | + }{ |
| 79 | + "success-with-zero-results": { |
| 80 | + server: &fwserver.Server{ |
| 81 | + Provider: &testprovider.Provider{}, |
| 82 | + }, |
| 83 | + request: &fwserver.ListRequest{ |
| 84 | + ListResource: &testprovider.ListResource{ |
| 85 | + ListMethod: func(ctx context.Context, req list.ListRequest, resp *list.ListResultsStream) { // TODO |
| 86 | + resp.Results = slices.Values([]list.ListResult{}) |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + expectedStreamEvents: []fwserver.ListResult{}, |
| 91 | + }, |
| 92 | + "success-with-nil-results": { |
| 93 | + server: &fwserver.Server{ |
| 94 | + Provider: &testprovider.Provider{}, |
| 95 | + }, |
| 96 | + request: &fwserver.ListRequest{ |
| 97 | + ListResource: &testprovider.ListResource{ |
| 98 | + ListMethod: func(ctx context.Context, req list.ListRequest, resp *list.ListResultsStream) { // TODO |
| 99 | + // Do nothing, so that resp.Results is nil |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + expectedStreamEvents: []fwserver.ListResult{}, |
| 104 | + }, |
| 105 | + |
| 106 | + "success-with-multiple-results": { |
| 107 | + server: &fwserver.Server{ |
| 108 | + Provider: &testprovider.Provider{}, |
| 109 | + }, |
| 110 | + request: &fwserver.ListRequest{ |
| 111 | + ListResource: &testprovider.ListResource{ |
| 112 | + ListMethod: func(ctx context.Context, req list.ListRequest, resp *list.ListResultsStream) { // TODO |
| 113 | + resp.Results = slices.Values([]list.ListResult{ |
| 114 | + { |
| 115 | + Identity: &tfsdk.ResourceIdentity{ |
| 116 | + Schema: testIdentitySchema, |
| 117 | + Raw: testIdentityValue1, |
| 118 | + }, |
| 119 | + Resource: &tfsdk.Resource{ |
| 120 | + Schema: testSchema, |
| 121 | + Raw: testResourceValue1, |
| 122 | + }, |
| 123 | + DisplayName: "Test Resource 1", |
| 124 | + Diagnostics: diag.Diagnostics{}, |
| 125 | + }, |
| 126 | + { |
| 127 | + Identity: &tfsdk.ResourceIdentity{ |
| 128 | + Schema: testIdentitySchema, |
| 129 | + Raw: testIdentityValue2, |
| 130 | + }, |
| 131 | + Resource: &tfsdk.Resource{ |
| 132 | + Schema: testSchema, |
| 133 | + Raw: testResourceValue2, |
| 134 | + }, |
| 135 | + DisplayName: "Test Resource 2", |
| 136 | + Diagnostics: diag.Diagnostics{}, |
| 137 | + }, |
| 138 | + }) |
| 139 | + }, |
| 140 | + }, |
| 141 | + }, |
| 142 | + expectedStreamEvents: []fwserver.ListResult{ |
| 143 | + { |
| 144 | + Identity: &tfsdk.ResourceIdentity{ |
| 145 | + Schema: testIdentitySchema, |
| 146 | + Raw: testIdentityValue1, |
| 147 | + }, |
| 148 | + Resource: &tfsdk.Resource{ |
| 149 | + Schema: testSchema, |
| 150 | + Raw: testResourceValue1, |
| 151 | + }, |
| 152 | + DisplayName: "Test Resource 1", |
| 153 | + Diagnostics: diag.Diagnostics{}, |
| 154 | + }, |
| 155 | + { |
| 156 | + Identity: &tfsdk.ResourceIdentity{ |
| 157 | + Schema: testIdentitySchema, |
| 158 | + Raw: testIdentityValue2, |
| 159 | + }, |
| 160 | + Resource: &tfsdk.Resource{ |
| 161 | + Schema: testSchema, |
| 162 | + Raw: testResourceValue2, |
| 163 | + }, |
| 164 | + DisplayName: "Test Resource 2", |
| 165 | + Diagnostics: diag.Diagnostics{}, |
| 166 | + }, |
| 167 | + }, |
| 168 | + }, |
| 169 | + } |
| 170 | + |
| 171 | + for name, testCase := range testCases { |
| 172 | + t.Run(name, func(t *testing.T) { |
| 173 | + t.Parallel() |
| 174 | + |
| 175 | + response := &fwserver.ListResourceStream{} |
| 176 | + testCase.server.ListResource(context.Background(), testCase.request, response) |
| 177 | + |
| 178 | + events := slices.AppendSeq([]fwserver.ListResult{}, response.Results) |
| 179 | + if diff := cmp.Diff(events, testCase.expectedStreamEvents); diff != "" { |
| 180 | + t.Errorf("unexpected difference: %s", diff) |
| 181 | + } |
| 182 | + }) |
| 183 | + } |
| 184 | +} |
0 commit comments