Skip to content

Commit 572a806

Browse files
pointlanderrameshpolishetti
authored andcommitted
Chore more tests (#56)
* Added more tests * Added built in pet store http server to tests * Added test for constructStartRequest * Added more mqtt tests * Added more rest tests * Check rest status * Use constants for settings
1 parent 8d3b4ac commit 572a806

File tree

7 files changed

+406
-61
lines changed

7 files changed

+406
-61
lines changed

ext/flogo/activity/rest/activity.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ const (
3434
methodPATCH = "PATCH"
3535
methodDELETE = "DELETE"
3636

37+
contentTypeTextPlain = "text/plain; charset=UTF-8"
38+
contentTypeApplicationJSON = "application/json; charset=UTF-8"
39+
3740
ivMethod = "method"
3841
ivURI = "uri"
3942
ivPathParams = "pathParams"
@@ -132,7 +135,7 @@ func (a *RESTActivity) Eval(context activity.Context) (done bool, err error) {
132135

133136
var reqBody io.Reader
134137

135-
contentType := "application/json; charset=UTF-8"
138+
contentType := contentTypeApplicationJSON
136139

137140
if method == methodPOST || method == methodPUT || method == methodPATCH {
138141

@@ -240,17 +243,17 @@ func (a *RESTActivity) Eval(context activity.Context) (done bool, err error) {
240243
//todo just make contentType a setting
241244
func getContentType(replyData interface{}) string {
242245

243-
contentType := "application/json; charset=UTF-8"
246+
contentType := contentTypeApplicationJSON
244247

245248
switch v := replyData.(type) {
246249
case string:
247250
if !strings.HasPrefix(v, "{") && !strings.HasPrefix(v, "[") {
248-
contentType = "text/plain; charset=UTF-8"
251+
contentType = contentTypeTextPlain
249252
}
250253
case int, int64, float64, bool, json.Number:
251-
contentType = "text/plain; charset=UTF-8"
254+
contentType = contentTypeTextPlain
252255
default:
253-
contentType = "application/json; charset=UTF-8"
256+
contentType = contentTypeApplicationJSON
254257
}
255258

256259
return contentType

ext/flogo/activity/rest/activity_test.go

+194-32
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@
22
* Copyright © 2017. TIBCO Software Inc.
33
* This file is subject to the license terms contained
44
* in the license file that is distributed with this file.
5-
*/
5+
*/
66
package rest
77

88
import (
9+
"context"
910
"encoding/json"
10-
"fmt"
11+
"io/ioutil"
12+
"net"
13+
"net/http"
14+
"os"
15+
"strconv"
16+
"strings"
1117
"testing"
1218

1319
"github.com/TIBCOSoftware/flogo-contrib/action/flow/test"
1420
"github.com/TIBCOSoftware/flogo-lib/core/activity"
15-
"io/ioutil"
21+
22+
opentracing "github.com/opentracing/opentracing-go"
1623
)
1724

1825
const reqPostStr string = `{
@@ -36,6 +43,82 @@ func getActivityMetadata() *activity.Metadata {
3643
return activityMetadata
3744
}
3845

46+
func TestMain(m *testing.M) {
47+
opentracing.SetGlobalTracer(&opentracing.NoopTracer{})
48+
49+
database := make([]map[string]interface{}, 0, 10)
50+
http.HandleFunc("/v2/pet", func(w http.ResponseWriter, r *http.Request) {
51+
if r.Method == http.MethodPost {
52+
body, err := ioutil.ReadAll(r.Body)
53+
if err != nil {
54+
panic(err)
55+
}
56+
var pet map[string]interface{}
57+
err = json.Unmarshal(body, &pet)
58+
if err != nil {
59+
panic(err)
60+
}
61+
pet["id"] = len(database)
62+
database = append(database, pet)
63+
body, err = json.Marshal(pet)
64+
if err != nil {
65+
panic(err)
66+
}
67+
68+
_, err = w.Write(body)
69+
if err != nil {
70+
panic(err)
71+
}
72+
}
73+
})
74+
75+
http.HandleFunc("/v2/pet/", func(w http.ResponseWriter, r *http.Request) {
76+
if r.Method == http.MethodGet {
77+
parts := strings.Split(r.URL.Path, "/")
78+
id, err := strconv.Atoi(parts[3])
79+
if err != nil {
80+
panic(err)
81+
}
82+
data, err := json.Marshal(database[id])
83+
if err != nil {
84+
panic(err)
85+
}
86+
_, err = w.Write(data)
87+
if err != nil {
88+
panic(err)
89+
}
90+
}
91+
})
92+
93+
http.HandleFunc("/v2/pet/findByStatus", func(w http.ResponseWriter, r *http.Request) {
94+
if r.Method == http.MethodGet {
95+
query := r.URL.Query()
96+
if query["status"][0] != "ava" {
97+
panic("invalid status")
98+
}
99+
data, err := json.Marshal(database[0])
100+
if err != nil {
101+
panic(err)
102+
}
103+
_, err = w.Write(data)
104+
if err != nil {
105+
panic(err)
106+
}
107+
}
108+
})
109+
110+
listener, err := net.Listen("tcp", ":8080")
111+
if err != nil {
112+
panic(err)
113+
}
114+
115+
go func() {
116+
http.Serve(listener, nil)
117+
}()
118+
119+
os.Exit(m.Run())
120+
}
121+
39122
func TestCreate(t *testing.T) {
40123

41124
act := NewActivity(getActivityMetadata())
@@ -55,20 +138,37 @@ func TestSimplePost(t *testing.T) {
55138
tc := test.NewTestActivityContext(getActivityMetadata())
56139

57140
//setup attrs
58-
tc.SetInput("method", "POST")
59-
tc.SetInput("uri", "http://petstore.swagger.io/v2/pet")
60-
tc.SetInput("content", reqPostStr)
141+
tc.SetInput(ivMethod, "POST")
142+
tc.SetInput(ivURI, "http://localhost:8080/v2/pet")
143+
tc.SetInput(ivContent, reqPostStr)
144+
145+
span := opentracing.StartSpan("test")
146+
ctx := opentracing.ContextWithSpan(context.Background(), span)
147+
tc.SetInput(ivTracing, ctx)
61148

62149
//eval
63150
act.Eval(tc)
64-
val := tc.GetOutput("result")
151+
val := tc.GetOutput(ovResult)
65152

66-
fmt.Printf("result: %v\n", val)
153+
t.Logf("result: %v\n", val)
67154

68155
res := val.(map[string]interface{})
69156

70157
petID = res["id"].(json.Number).String()
71-
fmt.Println("petID:", petID)
158+
t.Log("petID:", petID)
159+
if petID != "0" {
160+
t.Fatal("invalid pet id")
161+
}
162+
163+
status := tc.GetOutput(ovStatus)
164+
if status == nil {
165+
t.Error("status is nil")
166+
}
167+
168+
tracing := tc.GetOutput(ovTracing)
169+
if tracing == nil {
170+
t.Error("tracing is nil")
171+
}
72172
}
73173

74174
func TestSimpleGet(t *testing.T) {
@@ -77,60 +177,103 @@ func TestSimpleGet(t *testing.T) {
77177
tc := test.NewTestActivityContext(getActivityMetadata())
78178

79179
//setup attrs
80-
tc.SetInput("method", "GET")
81-
tc.SetInput("uri", "http://petstore.swagger.io/v2/pet/"+petID)
180+
tc.SetInput(ivMethod, "GET")
181+
tc.SetInput(ivURI, "http://localhost:8080/v2/pet/"+petID)
82182

83183
//eval
84184
act.Eval(tc)
85185

86-
val := tc.GetOutput("result")
87-
fmt.Printf("result: %v\n", val)
88-
}
186+
val := tc.GetOutput(ovResult)
187+
t.Logf("result: %v\n", val)
89188

90-
/*
91-
// TODO fix this test
189+
res := val.(map[string]interface{})
190+
191+
petID = res["id"].(json.Number).String()
192+
t.Log("petID:", petID)
193+
if petID != "0" {
194+
t.Fatal("invalid pet id")
195+
}
196+
}
92197

93198
func TestParamGet(t *testing.T) {
94199

95-
act := activity.Get("github.com/TIBCOSoftware/flogo-contrib/activity/rest")
200+
act := NewActivity(getActivityMetadata())
96201
tc := test.NewTestActivityContext(act.Metadata())
97202

98203
//setup attrs
99-
tc.SetInput("method", "GET")
100-
tc.SetInput("uri", "http://petstore.swagger.io/v2/pet/:id")
204+
tc.SetInput(ivMethod, "GET")
205+
tc.SetInput(ivURI, "http://localhost:8080/v2/pet/:id")
101206

102207
pathParams := map[string]string{
103208
"id": petID,
104209
}
105-
tc.SetInput("pathParams", pathParams)
210+
tc.SetInput(ivPathParams, pathParams)
106211

107212
//eval
108213
act.Eval(tc)
109214

110-
val := tc.GetOutput("result")
111-
fmt.Printf("result: %v\n", val)
215+
val := tc.GetOutput(ovResult)
216+
t.Logf("result: %v\n", val)
217+
218+
res := val.(map[string]interface{})
219+
220+
petID = res["id"].(json.Number).String()
221+
t.Log("petID:", petID)
222+
if petID != "0" {
223+
t.Fatal("invalid pet id")
224+
}
112225
}
113-
*/
114226

115227
func TestSimpleGetQP(t *testing.T) {
116228

117229
act := NewActivity(getActivityMetadata())
118230
tc := test.NewTestActivityContext(getActivityMetadata())
119231

120232
//setup attrs
121-
tc.SetInput("method", "GET")
122-
tc.SetInput("uri", "http://petstore.swagger.io/v2/pet/findByStatus")
233+
tc.SetInput(ivMethod, "GET")
234+
tc.SetInput(ivURI, "http://localhost:8080/v2/pet/findByStatus")
123235

124236
queryParams := map[string]string{
125237
"status": "ava",
126238
}
127-
tc.SetInput("queryParams", queryParams)
239+
tc.SetInput(ivQueryParams, queryParams)
128240

129241
//eval
130242
act.Eval(tc)
131243

132-
val := tc.GetOutput("result")
133-
fmt.Printf("result: %v\n", val)
244+
val := tc.GetOutput(ovResult)
245+
t.Logf("result: %v\n", val)
246+
247+
res := val.(map[string]interface{})
248+
249+
petID = res["id"].(json.Number).String()
250+
t.Log("petID:", petID)
251+
if petID != "0" {
252+
t.Fatal("invalid pet id")
253+
}
254+
}
255+
256+
func TestGetContentType(t *testing.T) {
257+
contentType := getContentType("test")
258+
if contentType != contentTypeTextPlain {
259+
t.Error("content type should be ", contentTypeTextPlain)
260+
}
261+
262+
contentType = getContentType(1)
263+
if contentType != contentTypeTextPlain {
264+
t.Error("content type should be ", contentTypeTextPlain)
265+
}
266+
267+
contentType = getContentType(make([]int, 1))
268+
if contentType != contentTypeApplicationJSON {
269+
t.Error("content type should be ", contentTypeApplicationJSON)
270+
}
271+
}
272+
273+
func TestMethodIsValid(t *testing.T) {
274+
if !methodIsValid(methodDELETE) {
275+
t.Error("method should be valid")
276+
}
134277
}
135278

136279
func TestBuildURI(t *testing.T) {
@@ -143,7 +286,10 @@ func TestBuildURI(t *testing.T) {
143286

144287
newURI := BuildURI(uri, params)
145288

146-
fmt.Println(newURI)
289+
t.Log(newURI)
290+
if newURI != "http://localhost:7070/flow/1234" {
291+
t.Fatal("invalid uri")
292+
}
147293
}
148294

149295
func TestBuildURI2(t *testing.T) {
@@ -157,7 +303,10 @@ func TestBuildURI2(t *testing.T) {
157303

158304
newURI := BuildURI(uri, params)
159305

160-
fmt.Println(newURI)
306+
t.Log(newURI)
307+
if newURI != "https://127.0.0.1:7070/flow/1234/test" {
308+
t.Fatal("invalid uri")
309+
}
161310
}
162311

163312
func TestBuildURI3(t *testing.T) {
@@ -170,7 +319,10 @@ func TestBuildURI3(t *testing.T) {
170319

171320
newURI := BuildURI(uri, params)
172321

173-
fmt.Println(newURI)
322+
t.Log(newURI)
323+
if newURI != "http://localhost/flow/1234" {
324+
t.Fatal("invalid uri")
325+
}
174326
}
175327

176328
func TestBuildURI4(t *testing.T) {
@@ -184,5 +336,15 @@ func TestBuildURI4(t *testing.T) {
184336

185337
newURI := BuildURI(uri, params)
186338

187-
fmt.Println(newURI)
339+
t.Log(newURI)
340+
if newURI != "https://127.0.0.1/flow/1234/test" {
341+
t.Fatal("invalid uri")
342+
}
343+
}
344+
345+
func TestGetCerts(t *testing.T) {
346+
_, err := getCerts("./certs")
347+
if err != nil {
348+
t.Error(err)
349+
}
188350
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIDJDCCAgwCCQDR7IyhIrVa0zANBgkqhkiG9w0BAQsFADBUMQswCQYDVQQGEwJV
3+
UzETMBEGA1UECAwKQ2FsaWZvcm5pYTESMBAGA1UEBwwJUGFsbyBBbHRvMRwwGgYD
4+
VQQKDBNUSUJDTyBTb2Z0d2FyZSBJbmMuMB4XDTE3MTAyMzE5MDgyOFoXDTI3MTAy
5+
MTE5MDgyOFowVDELMAkGA1UEBhMCVVMxEzARBgNVBAgMCkNhbGlmb3JuaWExEjAQ
6+
BgNVBAcMCVBhbG8gQWx0bzEcMBoGA1UECgwTVElCQ08gU29mdHdhcmUgSW5jLjCC
7+
ASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBANBeZXB95AIqqeB405QZfbg0
8+
d1djjjCqWqAwtVQrFePIC/VHeu5bmbeiKwmAB5/3waubPWue7mG3xVz0QZJ1SqJ1
9+
OaWhziK9AulU2sw6D+vthMGJ+KDCyvGeDS4BwYR47eDwey7fKFAAtLvXpMRTjiVc
10+
P7YkjPLR/HD+NfFUrWCy4f2bRQlMx1zjqVQjI7WwsSGmtGdfJtIIlx4agEIcB0jf
11+
v0+6V2FIWf3XsaVE/vUX8tjMo4gz0FpKpR/V2bEFKKNs/6MzIpk6pkEGQOmGrsRo
12+
6OyXoJ0eb6absVdmswP4rNkOLK66D9fMxmtst5KlxhUpTntxk17yNSnQJ5XBJZMC
13+
AwEAATANBgkqhkiG9w0BAQsFAAOCAQEABP4217+dVaHbhfAtk7C9JVU+rM8v0G9q
14+
jmF9Ntc5+E1bwJgjpLuKKxXP5zv8rvw/s1WIE5C5BxtLrjfOsKeSLvil5xnjmudd
15+
7BY7pd1QafzylSU4cWK4aNO2VixFsRLkdDWO8WCt/2akiYixb1ulTO77BdA/inqn
16+
dPs2WIwmabw4gC3TzZtND4bzW+O4jPq++mAb67dYdWDsPUpKokrer8+pzSF/R5Yo
17+
FVaKgN7cou62mQ8lIHbIRxvAat1SUNUUHmlM2y/wIYuqZrgMHq3Ra6PCcYHnwR2D
18+
OmZ3E+0oPxaILtIpR+H8E4wOEriGW4jjmluq9uc17Kmsw9QhQeCn2w==
19+
-----END CERTIFICATE-----

0 commit comments

Comments
 (0)