|
| 1 | +package streaminstance_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "reflect" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 9 | + "github.com/mongodb/terraform-provider-mongodbatlas/internal/service/streaminstance" |
| 10 | + "go.mongodb.org/atlas-sdk/v20231115002/admin" |
| 11 | +) |
| 12 | + |
| 13 | +type sdkToTFModelTestCase struct { |
| 14 | + SDKResp *admin.StreamsTenant |
| 15 | + expectedTFModel *streaminstance.TFStreamInstanceRSModel |
| 16 | + name string |
| 17 | +} |
| 18 | + |
| 19 | +const ( |
| 20 | + dummyProjectID = "111111111111111111111111" |
| 21 | + dummyStreamInstanceID = "222222222222222222222222" |
| 22 | + cloudProvider = "AWS" |
| 23 | + region = "VIRGINIA_USA" |
| 24 | + instanceName = "InstanceName" |
| 25 | +) |
| 26 | + |
| 27 | +var hostnames = []string{"atlas-stream.virginia-usa.a.query.mongodb-dev.net"} |
| 28 | + |
| 29 | +func TestStreamInstanceSDKToTFModel(t *testing.T) { |
| 30 | + testCases := []sdkToTFModelTestCase{ |
| 31 | + { |
| 32 | + name: "Complete SDK response", |
| 33 | + SDKResp: &admin.StreamsTenant{ |
| 34 | + Id: admin.PtrString(dummyStreamInstanceID), |
| 35 | + DataProcessRegion: &admin.StreamsDataProcessRegion{ |
| 36 | + CloudProvider: cloudProvider, |
| 37 | + Region: region, |
| 38 | + }, |
| 39 | + GroupId: admin.PtrString(dummyProjectID), |
| 40 | + Hostnames: hostnames, |
| 41 | + Name: admin.PtrString(instanceName), |
| 42 | + }, |
| 43 | + expectedTFModel: &streaminstance.TFStreamInstanceRSModel{ |
| 44 | + ID: types.StringValue(dummyStreamInstanceID), |
| 45 | + DataProcessRegion: tfRegionObject(t, cloudProvider, region), |
| 46 | + ProjectID: types.StringValue(dummyProjectID), |
| 47 | + Hostnames: tfHostnamesList(t, hostnames), |
| 48 | + InstanceName: types.StringValue(instanceName), |
| 49 | + }, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "Empty hostnames and dataProcessRegion in response", // should never happen, but verifying it is handled gracefully |
| 53 | + SDKResp: &admin.StreamsTenant{ |
| 54 | + Id: admin.PtrString(dummyStreamInstanceID), |
| 55 | + GroupId: admin.PtrString(dummyProjectID), |
| 56 | + Name: admin.PtrString(instanceName), |
| 57 | + }, |
| 58 | + expectedTFModel: &streaminstance.TFStreamInstanceRSModel{ |
| 59 | + ID: types.StringValue(dummyStreamInstanceID), |
| 60 | + DataProcessRegion: types.ObjectNull(streaminstance.ProcessRegionObjectType.AttrTypes), |
| 61 | + ProjectID: types.StringValue(dummyProjectID), |
| 62 | + Hostnames: types.ListNull(types.StringType), |
| 63 | + InstanceName: types.StringValue(instanceName), |
| 64 | + }, |
| 65 | + }, |
| 66 | + } |
| 67 | + |
| 68 | + for _, tc := range testCases { |
| 69 | + t.Run(tc.name, func(t *testing.T) { |
| 70 | + resultModel, diags := streaminstance.NewTFStreamInstance(context.Background(), tc.SDKResp) |
| 71 | + if diags.HasError() { |
| 72 | + t.Errorf("unexpected errors found: %s", diags.Errors()[0].Summary()) |
| 73 | + } |
| 74 | + if !reflect.DeepEqual(resultModel, tc.expectedTFModel) { |
| 75 | + t.Errorf("created terraform model did not match expected output") |
| 76 | + } |
| 77 | + }) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +type tfToSDKCreateModelTestCase struct { |
| 82 | + tfModel *streaminstance.TFStreamInstanceRSModel |
| 83 | + expectedSDKReq *admin.StreamsTenant |
| 84 | + name string |
| 85 | +} |
| 86 | + |
| 87 | +func TestStreamInstanceTFToSDKCreateModel(t *testing.T) { |
| 88 | + testCases := []tfToSDKCreateModelTestCase{ |
| 89 | + { |
| 90 | + name: "Complete TF state", |
| 91 | + tfModel: &streaminstance.TFStreamInstanceRSModel{ |
| 92 | + DataProcessRegion: tfRegionObject(t, cloudProvider, region), |
| 93 | + ProjectID: types.StringValue(dummyProjectID), |
| 94 | + InstanceName: types.StringValue(instanceName), |
| 95 | + }, |
| 96 | + expectedSDKReq: &admin.StreamsTenant{ |
| 97 | + DataProcessRegion: &admin.StreamsDataProcessRegion{ |
| 98 | + CloudProvider: cloudProvider, |
| 99 | + Region: region, |
| 100 | + }, |
| 101 | + GroupId: admin.PtrString(dummyProjectID), |
| 102 | + Name: admin.PtrString(instanceName), |
| 103 | + }, |
| 104 | + }, |
| 105 | + } |
| 106 | + |
| 107 | + for _, tc := range testCases { |
| 108 | + t.Run(tc.name, func(t *testing.T) { |
| 109 | + apiReqResult, diags := streaminstance.NewStreamInstanceCreateReq(context.Background(), tc.tfModel) |
| 110 | + if diags.HasError() { |
| 111 | + t.Errorf("unexpected errors found: %s", diags.Errors()[0].Summary()) |
| 112 | + } |
| 113 | + if !reflect.DeepEqual(apiReqResult, tc.expectedSDKReq) { |
| 114 | + t.Errorf("created sdk model did not match expected output") |
| 115 | + } |
| 116 | + }) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +type tfToSDKUpdateModelTestCase struct { |
| 121 | + tfModel *streaminstance.TFStreamInstanceRSModel |
| 122 | + expectedSDKReq *admin.StreamsDataProcessRegion |
| 123 | + name string |
| 124 | +} |
| 125 | + |
| 126 | +func TestStreamInstanceTFToSDKUpdateModel(t *testing.T) { |
| 127 | + testCases := []tfToSDKUpdateModelTestCase{ |
| 128 | + { |
| 129 | + name: "Complete TF state", |
| 130 | + tfModel: &streaminstance.TFStreamInstanceRSModel{ |
| 131 | + ID: types.StringValue(dummyStreamInstanceID), |
| 132 | + DataProcessRegion: tfRegionObject(t, cloudProvider, region), |
| 133 | + ProjectID: types.StringValue(dummyProjectID), |
| 134 | + Hostnames: tfHostnamesList(t, hostnames), |
| 135 | + InstanceName: types.StringValue(instanceName), |
| 136 | + }, |
| 137 | + expectedSDKReq: &admin.StreamsDataProcessRegion{ |
| 138 | + CloudProvider: cloudProvider, |
| 139 | + Region: region, |
| 140 | + }, |
| 141 | + }, |
| 142 | + } |
| 143 | + |
| 144 | + for _, tc := range testCases { |
| 145 | + t.Run(tc.name, func(t *testing.T) { |
| 146 | + apiReqResult, diags := streaminstance.NewStreamInstanceUpdateReq(context.Background(), tc.tfModel) |
| 147 | + if diags.HasError() { |
| 148 | + t.Errorf("unexpected errors found: %s", diags.Errors()[0].Summary()) |
| 149 | + } |
| 150 | + if !reflect.DeepEqual(apiReqResult, tc.expectedSDKReq) { |
| 151 | + t.Errorf("created sdk model did not match expected output") |
| 152 | + } |
| 153 | + }) |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +func tfRegionObject(t *testing.T, cloudProvider, region string) types.Object { |
| 158 | + dataProcessRegion, diags := types.ObjectValueFrom(context.Background(), streaminstance.ProcessRegionObjectType.AttrTypes, streaminstance.TFInstanceProcessRegionSpecModel{ |
| 159 | + CloudProvider: types.StringValue(cloudProvider), |
| 160 | + Region: types.StringValue(region), |
| 161 | + }) |
| 162 | + if diags.HasError() { |
| 163 | + t.Errorf("failed to create terraform data process region model: %s", diags.Errors()[0].Summary()) |
| 164 | + } |
| 165 | + return dataProcessRegion |
| 166 | +} |
| 167 | + |
| 168 | +func tfHostnamesList(t *testing.T, hostnames []string) types.List { |
| 169 | + resultList, diags := types.ListValueFrom(context.Background(), types.StringType, hostnames) |
| 170 | + if diags.HasError() { |
| 171 | + t.Errorf("failed to create terraform hostnames list: %s", diags.Errors()[0].Summary()) |
| 172 | + } |
| 173 | + return resultList |
| 174 | +} |
0 commit comments