-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathresource_test.go
More file actions
133 lines (130 loc) · 3.09 KB
/
resource_test.go
File metadata and controls
133 lines (130 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package logme
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/types"
logmeSdk "github.com/stackitcloud/stackit-sdk-go/services/logme/v1api"
)
func TestMapFields(t *testing.T) {
tests := []struct {
description string
input *logmeSdk.CredentialsResponse
expected Model
isValid bool
}{
{
"default_values",
&logmeSdk.CredentialsResponse{
Id: "cid",
Raw: &logmeSdk.RawCredentials{},
},
Model{
Id: types.StringValue("pid,iid,cid"),
CredentialId: types.StringValue("cid"),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Host: types.StringValue(""),
Password: types.StringValue(""),
Port: types.Int32Null(),
Uri: types.StringNull(),
Username: types.StringValue(""),
},
true,
},
{
"simple_values",
&logmeSdk.CredentialsResponse{
Id: "cid",
Raw: &logmeSdk.RawCredentials{
Credentials: logmeSdk.Credentials{
Host: "host",
Password: "password",
Port: new(int32(1234)),
Uri: new("uri"),
Username: "username",
},
},
},
Model{
Id: types.StringValue("pid,iid,cid"),
CredentialId: types.StringValue("cid"),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Host: types.StringValue("host"),
Password: types.StringValue("password"),
Port: types.Int32Value(1234),
Uri: types.StringValue("uri"),
Username: types.StringValue("username"),
},
true,
},
{
"null_fields_and_int_conversions",
&logmeSdk.CredentialsResponse{
Id: "cid",
Raw: &logmeSdk.RawCredentials{
Credentials: logmeSdk.Credentials{
Host: "",
Password: "",
Port: new(int32(2123456789)),
Uri: nil,
Username: "",
},
},
},
Model{
Id: types.StringValue("pid,iid,cid"),
CredentialId: types.StringValue("cid"),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Host: types.StringValue(""),
Password: types.StringValue(""),
Port: types.Int32Value(2123456789),
Uri: types.StringNull(),
Username: types.StringValue(""),
},
true,
},
{
"nil_response",
nil,
Model{},
false,
},
{
"no_resource_id",
&logmeSdk.CredentialsResponse{},
Model{},
false,
},
{
"nil_raw_credential",
&logmeSdk.CredentialsResponse{
Id: "cid",
},
Model{},
false,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
model := &Model{
ProjectId: tt.expected.ProjectId,
InstanceId: tt.expected.InstanceId,
}
err := mapFields(tt.input, model)
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(model, &tt.expected)
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
}
}
})
}
}