forked from SAP/jenkins-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegrationArtifactTriggerIntegrationTest_test.go
180 lines (155 loc) · 5.74 KB
/
integrationArtifactTriggerIntegrationTest_test.go
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package cmd
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/stretchr/testify/assert"
)
type integrationArtifactTriggerIntegrationTestMockUtils struct {
*mock.ExecMockRunner
*mock.FilesMock
}
func newIntegrationArtifactTriggerIntegrationTestTestsUtils() integrationArtifactTriggerIntegrationTestMockUtils {
utils := integrationArtifactTriggerIntegrationTestMockUtils{
ExecMockRunner: &mock.ExecMockRunner{},
FilesMock: &mock.FilesMock{},
}
return utils
}
func TestRunIntegrationArtifactTriggerIntegrationTest(t *testing.T) {
t.Run("MessageBodyPath good but no ContentType (ERROR) callIFlowURL", func(t *testing.T) {
//init
iFlowServiceKey := `{
"oauth": {
"url": "https://demo",
"clientid": "demouser",
"clientsecret": "******",
"tokenurl": "https://demo/oauth/token"
}
}`
config := integrationArtifactTriggerIntegrationTestOptions{
IntegrationFlowServiceKey: iFlowServiceKey,
IntegrationFlowID: "CPI_IFlow_Call_using_Cert",
MessageBodyPath: "/file.txt",
ContentType: "",
}
utils := newIntegrationArtifactTriggerIntegrationTestTestsUtils()
utils.AddFile("file.txt", []byte("dummycontent"))
httpClient := httpMockCpis{CPIFunction: "TriggerIntegrationTest", ResponseBody: ``, TestType: "Positive"}
//test
err := callIFlowURL(&config, nil, utils, &httpClient, "")
//assert
assert.Error(t, err)
})
t.Run("MessageBodyPath and ContentType good but file missing (ERROR) callIFlowURL", func(t *testing.T) {
//init
iFlowServiceKey := `{
"oauth": {
"url": "https://demo",
"clientid": "demouser",
"clientsecret": "******",
"tokenurl": "https://demo/oauth/token"
}
}`
config := integrationArtifactTriggerIntegrationTestOptions{
IntegrationFlowServiceKey: iFlowServiceKey,
IntegrationFlowID: "CPI_IFlow_Call_using_Cert",
MessageBodyPath: "test.txt",
ContentType: "txt",
}
utils := newIntegrationArtifactTriggerIntegrationTestTestsUtils()
//no file created here. error expected
httpClient := httpMockCpis{CPIFunction: "TriggerIntegrationTest", ResponseBody: ``, TestType: "Positive"}
//test
err := callIFlowURL(&config, nil, utils, &httpClient, "")
//assert
assert.Error(t, err)
})
t.Run("MessageBodyPath, ContentType, and file good (SUCCESS) callIFlowURL", func(t *testing.T) {
dir, err := ioutil.TempDir("", "")
defer os.RemoveAll(dir) // clean up
assert.NoError(t, err, "Error when creating temp dir")
//init
iFlowServiceKey := `{
"oauth": {
"url": "https://demo",
"clientid": "demouser",
"clientsecret": "******",
"tokenurl": "https://demo/oauth/token"
}
}`
config := integrationArtifactTriggerIntegrationTestOptions{
IntegrationFlowServiceKey: iFlowServiceKey,
IntegrationFlowID: "CPI_IFlow_Call_using_Cert",
MessageBodyPath: filepath.Join(dir, "test.txt"),
ContentType: "txt",
}
utils := newIntegrationArtifactTriggerIntegrationTestTestsUtils()
utils.AddFile(config.MessageBodyPath, []byte("dummycontent1")) //have to add a file here to see in utils
ioutil.WriteFile(config.MessageBodyPath, []byte("dummycontent2"), 0755)
httpClient := httpMockCpis{CPIFunction: "TriggerIntegrationTest", ResponseBody: ``, TestType: "Positive"}
//test
err = callIFlowURL(&config, nil, utils, &httpClient, "https://my-service.com/endpoint")
//assert
assert.NoError(t, err)
assert.Equal(t, "POST", httpClient.Method)
assert.Equal(t, "https://my-service.com/endpoint", httpClient.URL)
})
t.Run("No MessageBodyPath still works (SUCCESS) callIFlowURL", func(t *testing.T) {
//init
iFlowServiceKey := `{
"oauth": {
"url": "https://demo",
"clientid": "demouser",
"clientsecret": "******",
"tokenurl": "https://demo/oauth/token"
}
}`
config := integrationArtifactTriggerIntegrationTestOptions{
IntegrationFlowServiceKey: iFlowServiceKey,
IntegrationFlowID: "CPI_IFlow_Call_using_Cert",
MessageBodyPath: "",
ContentType: "txt",
}
utils := newIntegrationArtifactTriggerIntegrationTestTestsUtils()
//utils.AddFile(config.MessageBodyPath, []byte("dummycontent1")) //have to add a file here to see in utils
//ioutil.WriteFile(config.MessageBodyPath, []byte("dummycontent2"), 0755)
httpClient := httpMockCpis{CPIFunction: "TriggerIntegrationTest", ResponseBody: ``, TestType: "Positive"}
//test
err := callIFlowURL(&config, nil, utils, &httpClient, "https://my-service.com/endpoint")
//assert
assert.NoError(t, err)
assert.Equal(t, "GET", httpClient.Method)
assert.Equal(t, "https://my-service.com/endpoint", httpClient.URL)
})
t.Run("nil fileBody (SUCCESS) callIFlowURL", func(t *testing.T) {
dir, err := ioutil.TempDir("", "")
defer os.RemoveAll(dir) // clean up
assert.NoError(t, err, "Error when creating temp dir")
//init
iFlowServiceKey := `{
"oauth": {
"url": "https://demo",
"clientid": "demouser",
"clientsecret": "******",
"tokenurl": "https://demo/oauth/token"
}
}`
config := integrationArtifactTriggerIntegrationTestOptions{
IntegrationFlowServiceKey: iFlowServiceKey,
IntegrationFlowID: "CPI_IFlow_Call_using_Cert",
MessageBodyPath: filepath.Join(dir, "test.txt"),
ContentType: "txt",
}
utils := newIntegrationArtifactTriggerIntegrationTestTestsUtils()
utils.AddFile(config.MessageBodyPath, []byte(nil)) //have to add a file here to see in utils
ioutil.WriteFile(config.MessageBodyPath, []byte(nil), 0755)
httpClient := httpMockCpis{CPIFunction: "TriggerIntegrationTest", ResponseBody: ``, TestType: "Positive"}
//test
err = callIFlowURL(&config, nil, utils, &httpClient, "")
//assert
assert.NoError(t, err)
})
}