package postgresflexalpha import ( "testing" "github.com/google/go-cmp/cmp" "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/stackitcloud/stackit-sdk-go/core/utils" postgresflex "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha" data "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/user/datasources_gen" ) func TestMapDataSourceFields(t *testing.T) { const testRegion = "region" tests := []struct { description string input *postgresflex.GetUserResponse region string expected dataSourceModel isValid bool }{ { "default_values", &postgresflex.GetUserResponse{}, testRegion, dataSourceModel{ UserModel: data.UserModel{ Id: types.Int64Value(1), UserId: types.Int64Value(1), InstanceId: types.StringValue("iid"), ProjectId: types.StringValue("pid"), Name: types.StringValue(""), Roles: types.List(types.SetNull(types.StringType)), Status: types.StringValue(""), Region: types.StringValue(testRegion), }, TerraformID: types.StringValue("pid,region,iid,1"), }, true, }, { "simple_values", &postgresflex.GetUserResponse{ Roles: &[]postgresflex.UserRole{ "role_1", "role_2", "", }, Name: utils.Ptr("username"), }, testRegion, dataSourceModel{ UserModel: data.UserModel{ Id: types.Int64Value(1), UserId: types.Int64Value(1), InstanceId: types.StringValue("iid"), ProjectId: types.StringValue("pid"), Name: types.StringValue("username"), Roles: types.List( types.SetValueMust( types.StringType, []attr.Value{ types.StringValue("role_1"), types.StringValue("role_2"), types.StringValue(""), }, ), ), Region: types.StringValue(testRegion), Status: types.StringValue(""), }, TerraformID: types.StringValue("pid,region,iid,1"), }, true, }, { "null_fields_and_int_conversions", &postgresflex.GetUserResponse{ Id: utils.Ptr(int64(1)), Roles: &[]postgresflex.UserRole{}, Name: nil, Status: utils.Ptr("status"), }, testRegion, dataSourceModel{ UserModel: data.UserModel{ Id: types.Int64Value(1), UserId: types.Int64Value(1), InstanceId: types.StringValue("iid"), ProjectId: types.StringValue("pid"), Name: types.StringValue(""), Roles: types.List(types.SetValueMust(types.StringType, []attr.Value{})), Region: types.StringValue(testRegion), Status: types.StringValue("status"), }, TerraformID: types.StringValue("pid,region,iid,1"), }, true, }, { "nil_response", nil, testRegion, dataSourceModel{}, false, }, { "nil_response_2", &postgresflex.GetUserResponse{}, testRegion, dataSourceModel{}, false, }, { "no_resource_id", &postgresflex.GetUserResponse{}, testRegion, dataSourceModel{}, false, }, } for _, tt := range tests { t.Run( tt.description, func(t *testing.T) { state := &dataSourceModel{ UserModel: data.UserModel{ ProjectId: tt.expected.ProjectId, InstanceId: tt.expected.InstanceId, UserId: tt.expected.UserId, }, } err := mapDataSourceFields(tt.input, state, tt.region) if !tt.isValid && err == nil { t.Fatalf("Should have failed") } if tt.isValid && err != nil { t.Fatalf("Should not have failed: %v", err) } if tt.isValid { diff := cmp.Diff(state, &tt.expected) if diff != "" { t.Fatalf("Data does not match: %s", diff) } } }, ) } } func TestMapFieldsCreate(t *testing.T) { const testRegion = "region" tests := []struct { description string input *postgresflex.GetUserResponse region string expected resourceModel isValid bool }{ { "default_values", &postgresflex.GetUserResponse{ Id: utils.Ptr(int64(1)), }, testRegion, resourceModel{ Id: types.Int64Value(1), UserId: types.Int64Value(1), InstanceId: types.StringValue("iid"), ProjectId: types.StringValue("pid"), Name: types.StringNull(), Roles: types.List(types.SetNull(types.StringType)), Password: types.StringNull(), Region: types.StringValue(testRegion), Status: types.StringNull(), ConnectionString: types.StringNull(), }, true, }, { "simple_values", &postgresflex.GetUserResponse{ Id: utils.Ptr(int64(1)), Name: utils.Ptr("username"), Status: utils.Ptr("status"), }, testRegion, resourceModel{ Id: types.Int64Value(1), UserId: types.Int64Value(1), InstanceId: types.StringValue("iid"), ProjectId: types.StringValue("pid"), Name: types.StringValue("username"), Roles: types.List(types.SetNull(types.StringType)), Password: types.StringNull(), Region: types.StringValue(testRegion), Status: types.StringValue("status"), ConnectionString: types.StringNull(), }, true, }, { "null_fields_and_int_conversions", &postgresflex.GetUserResponse{ Id: utils.Ptr(int64(1)), Name: nil, Status: nil, }, testRegion, resourceModel{ Id: types.Int64Value(1), UserId: types.Int64Value(1), InstanceId: types.StringValue("iid"), ProjectId: types.StringValue("pid"), Name: types.StringNull(), Roles: types.List(types.SetNull(types.StringType)), Password: types.StringNull(), Region: types.StringValue(testRegion), Status: types.StringNull(), ConnectionString: types.StringNull(), }, true, }, { "nil_response", nil, testRegion, resourceModel{}, false, }, { "nil_response_2", &postgresflex.GetUserResponse{}, testRegion, resourceModel{}, false, }, { "no_resource_id", &postgresflex.GetUserResponse{}, testRegion, resourceModel{}, false, }, } for _, tt := range tests { t.Run( tt.description, func(t *testing.T) { state := &resourceModel{ ProjectId: tt.expected.ProjectId, InstanceId: tt.expected.InstanceId, } err := mapResourceFields(tt.input, state, tt.region) if !tt.isValid && err == nil { t.Fatalf("Should have failed") } if tt.isValid && err != nil { t.Fatalf("Should not have failed: %v", err) } if tt.isValid { diff := cmp.Diff(state, &tt.expected) if diff != "" { t.Fatalf("Data does not match: %s", diff) } } }, ) } } func TestMapFields(t *testing.T) { const testRegion = "region" tests := []struct { description string input *postgresflex.GetUserResponse region string expected resourceModel isValid bool }{ { "default_values", &postgresflex.GetUserResponse{ Id: utils.Ptr(int64(1)), }, testRegion, resourceModel{ Id: types.Int64Value(1), UserId: types.Int64Value(int64(1)), InstanceId: types.StringValue("iid"), ProjectId: types.StringValue("pid"), Name: types.StringNull(), Roles: types.List(types.SetNull(types.StringType)), Region: types.StringValue(testRegion), Status: types.StringNull(), ConnectionString: types.StringNull(), }, true, }, { "simple_values", &postgresflex.GetUserResponse{ Id: utils.Ptr(int64(1)), Roles: &[]postgresflex.UserRole{ "role_1", "role_2", "", }, Name: utils.Ptr("username"), }, testRegion, resourceModel{ Id: types.Int64Value(1), UserId: types.Int64Value(1), InstanceId: types.StringValue("iid"), ProjectId: types.StringValue("pid"), Name: types.StringValue("username"), Roles: types.List( types.SetValueMust( types.StringType, []attr.Value{ types.StringValue("role_1"), types.StringValue("role_2"), types.StringValue(""), }, ), ), Region: types.StringValue(testRegion), Status: types.StringNull(), ConnectionString: types.StringNull(), }, true, }, { "null_fields_and_int_conversions", &postgresflex.GetUserResponse{ Id: utils.Ptr(int64(1)), Name: nil, }, testRegion, resourceModel{ Id: types.Int64Value(1), UserId: types.Int64Value(1), InstanceId: types.StringValue("iid"), ProjectId: types.StringValue("pid"), Name: types.StringNull(), Roles: types.List(types.SetNull(types.StringType)), Region: types.StringValue(testRegion), Status: types.StringNull(), ConnectionString: types.StringNull(), }, true, }, { "nil_response", nil, testRegion, resourceModel{}, false, }, { "nil_response_2", &postgresflex.GetUserResponse{}, testRegion, resourceModel{}, false, }, { "no_resource_id", &postgresflex.GetUserResponse{}, testRegion, resourceModel{}, false, }, } for _, tt := range tests { t.Run( tt.description, func(t *testing.T) { state := &resourceModel{ ProjectId: tt.expected.ProjectId, InstanceId: tt.expected.InstanceId, } err := mapResourceFields(tt.input, state, tt.region) if !tt.isValid && err == nil { t.Fatalf("Should have failed") } if tt.isValid && err != nil { t.Fatalf("Should not have failed: %v", err) } if tt.isValid { diff := cmp.Diff(state, &tt.expected) if diff != "" { t.Fatalf("Data does not match: %s", diff) } } }, ) } } func TestToCreatePayload(t *testing.T) { tests := []struct { description string input *resourceModel inputRoles *[]string expected *postgresflex.CreateUserRequestPayload isValid bool }{ { "default_values", &resourceModel{}, &[]string{}, &postgresflex.CreateUserRequestPayload{ Name: nil, Roles: &[]postgresflex.UserRole{}, }, true, }, { "simple_values", &resourceModel{ Name: types.StringValue("username"), }, &[]string{ "role_1", "role_2", }, &postgresflex.CreateUserRequestPayload{ Name: utils.Ptr("username"), Roles: &[]postgresflex.UserRole{ "role_1", "role_2", }, }, true, }, { "null_fields_and_int_conversions", &resourceModel{ Name: types.StringNull(), }, &[]string{ "", }, &postgresflex.CreateUserRequestPayload{ Roles: &[]postgresflex.UserRole{ "", }, Name: nil, }, true, }, { "nil_model", nil, &[]string{}, nil, false, }, { "nil_roles", &resourceModel{}, nil, nil, false, }, } for _, tt := range tests { t.Run( tt.description, func(t *testing.T) { output, err := toCreatePayload(tt.input, tt.inputRoles) if !tt.isValid && err == nil { t.Fatalf("Should have failed") } if tt.isValid && err != nil { t.Fatalf("Should not have failed: %v", err) } if tt.isValid { diff := cmp.Diff(output, tt.expected) if diff != "" { t.Fatalf("Data does not match: %s", diff) } } }, ) } } func TestToUpdatePayload(t *testing.T) { tests := []struct { description string input *resourceModel inputRoles *[]string expected *postgresflex.UpdateUserRequestPayload isValid bool }{ { "default_values", &resourceModel{}, &[]string{}, &postgresflex.UpdateUserRequestPayload{ Roles: &[]postgresflex.UserRole{}, }, true, }, { "default_values", &resourceModel{ Name: types.StringValue("username"), }, &[]string{ "role_1", "role_2", }, &postgresflex.UpdateUserRequestPayload{ Name: utils.Ptr("username"), Roles: &[]postgresflex.UserRole{ "role_1", "role_2", }, }, true, }, { "null_fields_and_int_conversions", &resourceModel{ Name: types.StringNull(), }, &[]string{ "", }, &postgresflex.UpdateUserRequestPayload{ Roles: &[]postgresflex.UserRole{ "", }, }, true, }, { "nil_model", nil, &[]string{}, nil, false, }, { "nil_roles", &resourceModel{}, nil, nil, false, }, } for _, tt := range tests { t.Run( tt.description, func(t *testing.T) { output, err := toUpdatePayload(tt.input, tt.inputRoles) if !tt.isValid && err == nil { t.Fatalf("Should have failed") } if tt.isValid && err != nil { t.Fatalf("Should not have failed: %v", err) } if tt.isValid { diff := cmp.Diff(output, tt.expected) if diff != "" { t.Fatalf("Data does not match: %s", diff) } } }, ) } }