@@ -4,12 +4,13 @@ import (
4
4
"context"
5
5
"testing"
6
6
7
+ "github.com/google/go-cmp/cmp"
7
8
"github.com/hashicorp/terraform-plugin-framework/attr"
8
9
"github.com/hashicorp/terraform-plugin-framework/types"
9
10
"github.com/pomerium/enterprise-client-go/pb"
10
11
"github.com/pomerium/enterprise-terraform-provider/internal/provider"
11
- "github.com/stretchr/testify/assert"
12
12
"github.com/stretchr/testify/require"
13
+ "google.golang.org/protobuf/testing/protocmp"
13
14
)
14
15
15
16
func TestConvertRoute (t * testing.T ) {
@@ -18,35 +19,133 @@ func TestConvertRoute(t *testing.T) {
18
19
t .Run ("pb to model" , func (t * testing.T ) {
19
20
t .Parallel ()
20
21
21
- // Test conversion from pb to model
22
- route := & pb.Route {
23
- Id : "route-id" ,
24
- Name : "route-name" ,
25
- From : "from" ,
26
- To : []string {"to" },
22
+ input := & pb.Route {
23
+ Id : "route-id" ,
24
+ Name : "route-name" ,
25
+ From : "from" ,
26
+ To : []string {"to1" , "to2" },
27
+ Prefix : P ("/api" ),
28
+ Path : P ("/v1" ),
29
+ PassIdentityHeaders : P (true ),
30
+ SetRequestHeaders : map [string ]string {
31
+ "X-Custom" : "value" ,
32
+ },
33
+ RemoveRequestHeaders : []string {"Remove-Me" },
34
+ NamespaceId : "namespace-1" ,
35
+ StatName : "stats-name" ,
36
+ PolicyIds : []string {"policy-1" , "policy-2" },
37
+ SetResponseHeaders : map [string ]string {
38
+ "X-Response" : "value" ,
39
+ },
40
+ ShowErrorDetails : true ,
27
41
}
28
42
29
- var plan provider.RouteResourceModel
30
- diag := provider .ConvertRouteFromPB (& plan , route )
43
+ var actual provider.RouteResourceModel
44
+ diag := provider .ConvertRouteFromPB (& actual , input )
31
45
require .False (t , diag .HasError (), diag .Errors ())
46
+
47
+ expected := provider.RouteResourceModel {
48
+ ID : types .StringValue ("route-id" ),
49
+ Name : types .StringValue ("route-name" ),
50
+ From : types .StringValue ("from" ),
51
+ Prefix : types .StringValue ("/api" ),
52
+ Path : types .StringValue ("/v1" ),
53
+ PassIdentityHeaders : types .BoolValue (true ),
54
+ To : types .SetValueMust (types .StringType , []attr.Value {
55
+ types .StringValue ("to1" ),
56
+ types .StringValue ("to2" ),
57
+ }),
58
+ SetRequestHeaders : types .MapValueMust (
59
+ types .StringType ,
60
+ map [string ]attr.Value {
61
+ "X-Custom" : types .StringValue ("value" ),
62
+ },
63
+ ),
64
+ RemoveRequestHeaders : types .SetValueMust (
65
+ types .StringType ,
66
+ []attr.Value {types .StringValue ("Remove-Me" )},
67
+ ),
68
+ NamespaceID : types .StringValue ("namespace-1" ),
69
+ StatName : types .StringValue ("stats-name" ),
70
+ Policies : types .SetValueMust (types .StringType , []attr.Value {
71
+ types .StringValue ("policy-1" ),
72
+ types .StringValue ("policy-2" ),
73
+ }),
74
+ SetResponseHeaders : types .MapValueMust (
75
+ types .StringType ,
76
+ map [string ]attr.Value {
77
+ "X-Response" : types .StringValue ("value" ),
78
+ },
79
+ ),
80
+ ShowErrorDetails : types .BoolValue (true ),
81
+ }
82
+
83
+ if diff := cmp .Diff (expected , actual ); diff != "" {
84
+ t .Errorf ("unexpected difference: %s" , diff )
85
+ }
32
86
})
33
87
34
88
t .Run ("model to pb" , func (t * testing.T ) {
35
89
t .Parallel ()
36
90
37
- // Test conversion from model to pb
38
- plan := provider.RouteResourceModel {
39
- From : types .StringValue ("from" ),
40
- To : types .SetValueMust (types .StringType , []attr.Value {types .StringValue ("to" )}),
41
- Name : types .StringValue ("route-name" ),
91
+ input := provider.RouteResourceModel {
42
92
ID : types .StringValue ("route-id" ),
93
+ Name : types .StringValue ("route-name" ),
94
+ From : types .StringValue ("from" ),
95
+ To : types .SetValueMust (types .StringType , []attr.Value {
96
+ types .StringValue ("to1" ),
97
+ types .StringValue ("to2" ),
98
+ }),
99
+ Prefix : types .StringValue ("/api" ),
100
+ Path : types .StringValue ("/v1" ),
101
+ PassIdentityHeaders : types .BoolValue (true ),
102
+ SetRequestHeaders : types .MapValueMust (
103
+ types .StringType ,
104
+ map [string ]attr.Value {
105
+ "X-Custom" : types .StringValue ("value" ),
106
+ },
107
+ ),
108
+ RemoveRequestHeaders : types .SetValueMust (
109
+ types .StringType ,
110
+ []attr.Value {types .StringValue ("Remove-Me" )},
111
+ ),
112
+ NamespaceID : types .StringValue ("namespace-1" ),
113
+ StatName : types .StringValue ("stats-name" ),
114
+ Policies : types .SetValueMust (types .StringType , []attr.Value {
115
+ types .StringValue ("policy-1" ),
116
+ types .StringValue ("policy-2" ),
117
+ }),
118
+ SetResponseHeaders : types .MapValueMust (
119
+ types .StringType ,
120
+ map [string ]attr.Value {
121
+ "X-Response" : types .StringValue ("value" ),
122
+ },
123
+ ),
124
+ ShowErrorDetails : types .BoolValue (true ),
43
125
}
44
126
45
- route , diag := provider .ConvertRouteToPB (context .Background (), & plan )
127
+ actual , diag := provider .ConvertRouteToPB (context .Background (), & input )
46
128
require .False (t , diag .HasError (), diag .Errors ())
47
- assert .Equal (t , "route-id" , route .Id )
48
- assert .Equal (t , "route-name" , route .Name )
49
- assert .Equal (t , "from" , route .From )
50
- assert .Equal (t , []string {"to" }, route .To )
129
+
130
+ expected := & pb.Route {
131
+ Id : "route-id" ,
132
+ Name : "route-name" ,
133
+ From : "from" ,
134
+ To : []string {"to1" , "to2" },
135
+ Prefix : P ("/api" ),
136
+ Path : P ("/v1" ),
137
+ PassIdentityHeaders : P (true ),
138
+ SetRequestHeaders : map [string ]string {"X-Custom" : "value" },
139
+ RemoveRequestHeaders : []string {"Remove-Me" },
140
+ NamespaceId : "namespace-1" ,
141
+ StatName : "stats-name" ,
142
+ PolicyIds : []string {"policy-1" , "policy-2" },
143
+ SetResponseHeaders : map [string ]string {"X-Response" : "value" },
144
+ ShowErrorDetails : true ,
145
+ }
146
+
147
+ if diff := cmp .Diff (expected , actual , protocmp .Transform ()); diff != "" {
148
+ t .Errorf ("unexpected difference: %s" , diff )
149
+ }
51
150
})
52
151
}
0 commit comments