@@ -35,6 +35,8 @@ import (
35
35
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/resource/automation"
36
36
)
37
37
38
+ var idResponseData = []byte (`{ "id": "config-id" }` )
39
+
38
40
func TestDeployAutomation_WrongType (t * testing.T ) {
39
41
client := & client.DummyAutomationClient {}
40
42
@@ -60,10 +62,10 @@ func TestDeployAutomation_UnknownResourceType(t *testing.T) {
60
62
assert .NotEmpty (t , errs )
61
63
}
62
64
63
- func TestDeployAutomation_ClientUpsertFails (t * testing.T ) {
64
- t .Run ("TestDeployAutomation - Workflow Upsert fails" , func (t * testing.T ) {
65
+ func TestDeployAutomation_ClientUpdateFails (t * testing.T ) {
66
+ t .Run ("TestDeployAutomation - Workflow Update fails" , func (t * testing.T ) {
65
67
client := automation .NewMockDeploySource (gomock .NewController (t ))
66
- client .EXPECT ().Upsert (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Times (1 ).Return (api.Response {}, errors .New ("UPSERT_FAIL " ))
68
+ client .EXPECT ().Update (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Times (1 ).Return (api.Response {}, errors .New ("UPDATE_FAIL " ))
67
69
68
70
conf := & config.Config {
69
71
Type : config.AutomationType {
@@ -76,9 +78,9 @@ func TestDeployAutomation_ClientUpsertFails(t *testing.T) {
76
78
assert .Zero (t , resp )
77
79
assert .Error (t , err )
78
80
})
79
- t .Run ("TestDeployAutomation - Workflow Upsert fails - HTTP Err" , func (t * testing.T ) {
81
+ t .Run ("TestDeployAutomation - Workflow Update fails - HTTP Err" , func (t * testing.T ) {
80
82
client := automation .NewMockDeploySource (gomock .NewController (t ))
81
- client .EXPECT ().Upsert (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Times (1 ).Return (api.Response {StatusCode : 400 }, nil )
83
+ client .EXPECT ().Update (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Times (1 ).Return (api.Response {}, api. APIError { StatusCode : 400 })
82
84
83
85
conf := & config.Config {
84
86
Type : config.AutomationType {
@@ -90,9 +92,9 @@ func TestDeployAutomation_ClientUpsertFails(t *testing.T) {
90
92
_ , err := automation .NewDeployAPI (client ).Deploy (t .Context (), nil , "" , conf )
91
93
assert .Error (t , err )
92
94
})
93
- t .Run ("TestDeployAutomation - BusinessCalendar Upsert fails" , func (t * testing.T ) {
95
+ t .Run ("TestDeployAutomation - BusinessCalendar Update fails" , func (t * testing.T ) {
94
96
client := automation .NewMockDeploySource (gomock .NewController (t ))
95
- client .EXPECT ().Upsert (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Times (1 ).Return (api.Response {}, errors .New ("UPSERT_FAIL " ))
97
+ client .EXPECT ().Update (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Times (1 ).Return (api.Response {}, errors .New ("UPDATE_FAIL " ))
96
98
97
99
conf := & config.Config {
98
100
Type : config.AutomationType {
@@ -104,9 +106,9 @@ func TestDeployAutomation_ClientUpsertFails(t *testing.T) {
104
106
_ , err := automation .NewDeployAPI (client ).Deploy (t .Context (), nil , "" , conf )
105
107
assert .Error (t , err )
106
108
})
107
- t .Run ("TestDeployAutomation - BusinessCalendar Upsert fails - HTTP Error" , func (t * testing.T ) {
109
+ t .Run ("TestDeployAutomation - BusinessCalendar Update fails - HTTP Error" , func (t * testing.T ) {
108
110
client := automation .NewMockDeploySource (gomock .NewController (t ))
109
- client .EXPECT ().Upsert (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Times (1 ).Return (api.Response {StatusCode : 400 }, nil )
111
+ client .EXPECT ().Update (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Times (1 ).Return (api.Response {}, api. APIError { StatusCode : 400 })
110
112
111
113
conf := & config.Config {
112
114
Type : config.AutomationType {
@@ -118,9 +120,9 @@ func TestDeployAutomation_ClientUpsertFails(t *testing.T) {
118
120
_ , err := automation .NewDeployAPI (client ).Deploy (t .Context (), nil , "" , conf )
119
121
assert .Error (t , err )
120
122
})
121
- t .Run ("TestDeployAutomation - Scheduling Rule Upsert fails" , func (t * testing.T ) {
123
+ t .Run ("TestDeployAutomation - Scheduling Rule Update fails" , func (t * testing.T ) {
122
124
client := automation .NewMockDeploySource (gomock .NewController (t ))
123
- client .EXPECT ().Upsert (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Times (1 ).Return (api.Response {}, errors .New ("UPSERT_FAIL " ))
125
+ client .EXPECT ().Update (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Times (1 ).Return (api.Response {}, errors .New ("UPDATE_FAIL " ))
124
126
125
127
conf := & config.Config {
126
128
Type : config.AutomationType {
@@ -132,9 +134,9 @@ func TestDeployAutomation_ClientUpsertFails(t *testing.T) {
132
134
_ , err := automation .NewDeployAPI (client ).Deploy (t .Context (), nil , "" , conf )
133
135
assert .Error (t , err )
134
136
})
135
- t .Run ("TestDeployAutomation - Scheduling Rule Upsert fails - HTTP Error" , func (t * testing.T ) {
137
+ t .Run ("TestDeployAutomation - Scheduling Rule Update fails - HTTP Error" , func (t * testing.T ) {
136
138
client := automation .NewMockDeploySource (gomock .NewController (t ))
137
- client .EXPECT ().Upsert (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Times (1 ).Return (api.Response {StatusCode : 400 }, nil )
139
+ client .EXPECT ().Update (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Times (1 ).Return (api.Response {}, api. APIError { StatusCode : 400 })
138
140
139
141
conf := & config.Config {
140
142
Type : config.AutomationType {
@@ -150,9 +152,9 @@ func TestDeployAutomation_ClientUpsertFails(t *testing.T) {
150
152
151
153
func TestDeployAutomation (t * testing.T ) {
152
154
client := automation .NewMockDeploySource (gomock .NewController (t ))
153
- client .EXPECT ().Upsert (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Times (1 ).Return (api.Response {
155
+ client .EXPECT ().Update (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Times (1 ).Return (api.Response {
154
156
StatusCode : 200 ,
155
- Data : [] byte ( "{ \" id \" : \" config-id \" }" ) ,
157
+ Data : idResponseData ,
156
158
}, nil )
157
159
conf := & config.Config {
158
160
Coordinate : coordinate.Coordinate {
@@ -174,9 +176,9 @@ func TestDeployAutomation(t *testing.T) {
174
176
func TestDeployAutomation_WithGivenObjectId (t * testing.T ) {
175
177
client := automation .NewMockDeploySource (gomock .NewController (t ))
176
178
objectId := "custom-object-id"
177
- client .EXPECT ().Upsert (gomock .Any (), gomock .Any (), objectId , gomock .Any ()).Times (1 ).Return (api.Response {
179
+ client .EXPECT ().Update (gomock .Any (), gomock .Any (), objectId , gomock .Any ()).Times (1 ).Return (api.Response {
178
180
StatusCode : 200 ,
179
- Data : [] byte ( `{ "id": "config-id" }` ) ,
181
+ Data : idResponseData ,
180
182
}, nil )
181
183
conf := & config.Config {
182
184
OriginObjectId : objectId ,
@@ -191,3 +193,49 @@ func TestDeployAutomation_WithGivenObjectId(t *testing.T) {
191
193
require .NoError (t , errs )
192
194
assert .Equal (t , "config-id" , resolvedEntity .Properties [config .IdParameter ])
193
195
}
196
+
197
+ func TestDeploy_WithCreate (t * testing.T ) {
198
+ client := automation .NewMockDeploySource (gomock .NewController (t ))
199
+ client .EXPECT ().Update (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Times (1 ).Return (api.Response {}, api.APIError {StatusCode : 404 })
200
+ client .EXPECT ().Create (gomock .Any (), gomock .Any (), gomock .Any ()).Times (1 ).Return (api.Response {StatusCode : 200 , Data : idResponseData }, nil )
201
+ conf := & config.Config {
202
+ Coordinate : coordinate.Coordinate {
203
+ ConfigId : "config-id" ,
204
+ },
205
+ Type : config.AutomationType {
206
+ Resource : config .Workflow ,
207
+ },
208
+ }
209
+ _ , errs := automation .NewDeployAPI (client ).Deploy (t .Context (), parameter.Properties {}, "{}" , conf )
210
+ assert .NoError (t , errs )
211
+ }
212
+
213
+ func TestDeploy_FailsIfPayloadIsInvalid (t * testing.T ) {
214
+ client := automation .NewMockDeploySource (gomock .NewController (t ))
215
+ client .EXPECT ().Update (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Times (1 ).Return (api.Response {}, api.APIError {StatusCode : 404 })
216
+ conf := & config.Config {
217
+ Coordinate : coordinate.Coordinate {
218
+ ConfigId : "config-id" ,
219
+ },
220
+ Type : config.AutomationType {
221
+ Resource : config .Workflow ,
222
+ },
223
+ }
224
+ _ , errs := automation .NewDeployAPI (client ).Deploy (t .Context (), parameter.Properties {}, "" , conf )
225
+ assert .ErrorContains (t , errs , "unable to set the id field" )
226
+ }
227
+
228
+ func TestDeploy_FailsIfResponseIsMissingID (t * testing.T ) {
229
+ client := automation .NewMockDeploySource (gomock .NewController (t ))
230
+ client .EXPECT ().Update (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Times (1 ).Return (api.Response {StatusCode : 200 , Data : []byte ("{}" )}, nil )
231
+ conf := & config.Config {
232
+ Coordinate : coordinate.Coordinate {
233
+ ConfigId : "config-id" ,
234
+ },
235
+ Type : config.AutomationType {
236
+ Resource : config .Workflow ,
237
+ },
238
+ }
239
+ _ , errs := automation .NewDeployAPI (client ).Deploy (t .Context (), parameter.Properties {}, "{}" , conf )
240
+ assert .ErrorContains (t , errs , "id field missing" )
241
+ }
0 commit comments