Skip to content

Commit

Permalink
Chore more tests (#56)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
pointlander authored and rameshpolishetti committed Nov 2, 2017
1 parent 8d3b4ac commit 572a806
Show file tree
Hide file tree
Showing 7 changed files with 406 additions and 61 deletions.
13 changes: 8 additions & 5 deletions ext/flogo/activity/rest/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ const (
methodPATCH = "PATCH"
methodDELETE = "DELETE"

contentTypeTextPlain = "text/plain; charset=UTF-8"
contentTypeApplicationJSON = "application/json; charset=UTF-8"

ivMethod = "method"
ivURI = "uri"
ivPathParams = "pathParams"
Expand Down Expand Up @@ -132,7 +135,7 @@ func (a *RESTActivity) Eval(context activity.Context) (done bool, err error) {

var reqBody io.Reader

contentType := "application/json; charset=UTF-8"
contentType := contentTypeApplicationJSON

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

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

contentType := "application/json; charset=UTF-8"
contentType := contentTypeApplicationJSON

switch v := replyData.(type) {
case string:
if !strings.HasPrefix(v, "{") && !strings.HasPrefix(v, "[") {
contentType = "text/plain; charset=UTF-8"
contentType = contentTypeTextPlain
}
case int, int64, float64, bool, json.Number:
contentType = "text/plain; charset=UTF-8"
contentType = contentTypeTextPlain
default:
contentType = "application/json; charset=UTF-8"
contentType = contentTypeApplicationJSON
}

return contentType
Expand Down
226 changes: 194 additions & 32 deletions ext/flogo/activity/rest/activity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@
* Copyright © 2017. TIBCO Software Inc.
* This file is subject to the license terms contained
* in the license file that is distributed with this file.
*/
*/
package rest

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"strconv"
"strings"
"testing"

"github.com/TIBCOSoftware/flogo-contrib/action/flow/test"
"github.com/TIBCOSoftware/flogo-lib/core/activity"
"io/ioutil"

opentracing "github.com/opentracing/opentracing-go"
)

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

func TestMain(m *testing.M) {
opentracing.SetGlobalTracer(&opentracing.NoopTracer{})

database := make([]map[string]interface{}, 0, 10)
http.HandleFunc("/v2/pet", func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
var pet map[string]interface{}
err = json.Unmarshal(body, &pet)
if err != nil {
panic(err)
}
pet["id"] = len(database)
database = append(database, pet)
body, err = json.Marshal(pet)
if err != nil {
panic(err)
}

_, err = w.Write(body)
if err != nil {
panic(err)
}
}
})

http.HandleFunc("/v2/pet/", func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
parts := strings.Split(r.URL.Path, "/")
id, err := strconv.Atoi(parts[3])
if err != nil {
panic(err)
}
data, err := json.Marshal(database[id])
if err != nil {
panic(err)
}
_, err = w.Write(data)
if err != nil {
panic(err)
}
}
})

http.HandleFunc("/v2/pet/findByStatus", func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
query := r.URL.Query()
if query["status"][0] != "ava" {
panic("invalid status")
}
data, err := json.Marshal(database[0])
if err != nil {
panic(err)
}
_, err = w.Write(data)
if err != nil {
panic(err)
}
}
})

listener, err := net.Listen("tcp", ":8080")
if err != nil {
panic(err)
}

go func() {
http.Serve(listener, nil)
}()

os.Exit(m.Run())
}

func TestCreate(t *testing.T) {

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

//setup attrs
tc.SetInput("method", "POST")
tc.SetInput("uri", "http://petstore.swagger.io/v2/pet")
tc.SetInput("content", reqPostStr)
tc.SetInput(ivMethod, "POST")
tc.SetInput(ivURI, "http://localhost:8080/v2/pet")
tc.SetInput(ivContent, reqPostStr)

span := opentracing.StartSpan("test")
ctx := opentracing.ContextWithSpan(context.Background(), span)
tc.SetInput(ivTracing, ctx)

//eval
act.Eval(tc)
val := tc.GetOutput("result")
val := tc.GetOutput(ovResult)

fmt.Printf("result: %v\n", val)
t.Logf("result: %v\n", val)

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

petID = res["id"].(json.Number).String()
fmt.Println("petID:", petID)
t.Log("petID:", petID)
if petID != "0" {
t.Fatal("invalid pet id")
}

status := tc.GetOutput(ovStatus)
if status == nil {
t.Error("status is nil")
}

tracing := tc.GetOutput(ovTracing)
if tracing == nil {
t.Error("tracing is nil")
}
}

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

//setup attrs
tc.SetInput("method", "GET")
tc.SetInput("uri", "http://petstore.swagger.io/v2/pet/"+petID)
tc.SetInput(ivMethod, "GET")
tc.SetInput(ivURI, "http://localhost:8080/v2/pet/"+petID)

//eval
act.Eval(tc)

val := tc.GetOutput("result")
fmt.Printf("result: %v\n", val)
}
val := tc.GetOutput(ovResult)
t.Logf("result: %v\n", val)

/*
// TODO fix this test
res := val.(map[string]interface{})

petID = res["id"].(json.Number).String()
t.Log("petID:", petID)
if petID != "0" {
t.Fatal("invalid pet id")
}
}

func TestParamGet(t *testing.T) {

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

//setup attrs
tc.SetInput("method", "GET")
tc.SetInput("uri", "http://petstore.swagger.io/v2/pet/:id")
tc.SetInput(ivMethod, "GET")
tc.SetInput(ivURI, "http://localhost:8080/v2/pet/:id")

pathParams := map[string]string{
"id": petID,
}
tc.SetInput("pathParams", pathParams)
tc.SetInput(ivPathParams, pathParams)

//eval
act.Eval(tc)

val := tc.GetOutput("result")
fmt.Printf("result: %v\n", val)
val := tc.GetOutput(ovResult)
t.Logf("result: %v\n", val)

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

petID = res["id"].(json.Number).String()
t.Log("petID:", petID)
if petID != "0" {
t.Fatal("invalid pet id")
}
}
*/

func TestSimpleGetQP(t *testing.T) {

act := NewActivity(getActivityMetadata())
tc := test.NewTestActivityContext(getActivityMetadata())

//setup attrs
tc.SetInput("method", "GET")
tc.SetInput("uri", "http://petstore.swagger.io/v2/pet/findByStatus")
tc.SetInput(ivMethod, "GET")
tc.SetInput(ivURI, "http://localhost:8080/v2/pet/findByStatus")

queryParams := map[string]string{
"status": "ava",
}
tc.SetInput("queryParams", queryParams)
tc.SetInput(ivQueryParams, queryParams)

//eval
act.Eval(tc)

val := tc.GetOutput("result")
fmt.Printf("result: %v\n", val)
val := tc.GetOutput(ovResult)
t.Logf("result: %v\n", val)

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

petID = res["id"].(json.Number).String()
t.Log("petID:", petID)
if petID != "0" {
t.Fatal("invalid pet id")
}
}

func TestGetContentType(t *testing.T) {
contentType := getContentType("test")
if contentType != contentTypeTextPlain {
t.Error("content type should be ", contentTypeTextPlain)
}

contentType = getContentType(1)
if contentType != contentTypeTextPlain {
t.Error("content type should be ", contentTypeTextPlain)
}

contentType = getContentType(make([]int, 1))
if contentType != contentTypeApplicationJSON {
t.Error("content type should be ", contentTypeApplicationJSON)
}
}

func TestMethodIsValid(t *testing.T) {
if !methodIsValid(methodDELETE) {
t.Error("method should be valid")
}
}

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

newURI := BuildURI(uri, params)

fmt.Println(newURI)
t.Log(newURI)
if newURI != "http://localhost:7070/flow/1234" {
t.Fatal("invalid uri")
}
}

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

newURI := BuildURI(uri, params)

fmt.Println(newURI)
t.Log(newURI)
if newURI != "https://127.0.0.1:7070/flow/1234/test" {
t.Fatal("invalid uri")
}
}

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

newURI := BuildURI(uri, params)

fmt.Println(newURI)
t.Log(newURI)
if newURI != "http://localhost/flow/1234" {
t.Fatal("invalid uri")
}
}

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

newURI := BuildURI(uri, params)

fmt.Println(newURI)
t.Log(newURI)
if newURI != "https://127.0.0.1/flow/1234/test" {
t.Fatal("invalid uri")
}
}

func TestGetCerts(t *testing.T) {
_, err := getCerts("./certs")
if err != nil {
t.Error(err)
}
}
19 changes: 19 additions & 0 deletions ext/flogo/activity/rest/certs/self.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-----BEGIN CERTIFICATE-----
MIIDJDCCAgwCCQDR7IyhIrVa0zANBgkqhkiG9w0BAQsFADBUMQswCQYDVQQGEwJV
UzETMBEGA1UECAwKQ2FsaWZvcm5pYTESMBAGA1UEBwwJUGFsbyBBbHRvMRwwGgYD
VQQKDBNUSUJDTyBTb2Z0d2FyZSBJbmMuMB4XDTE3MTAyMzE5MDgyOFoXDTI3MTAy
MTE5MDgyOFowVDELMAkGA1UEBhMCVVMxEzARBgNVBAgMCkNhbGlmb3JuaWExEjAQ
BgNVBAcMCVBhbG8gQWx0bzEcMBoGA1UECgwTVElCQ08gU29mdHdhcmUgSW5jLjCC
ASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBANBeZXB95AIqqeB405QZfbg0
d1djjjCqWqAwtVQrFePIC/VHeu5bmbeiKwmAB5/3waubPWue7mG3xVz0QZJ1SqJ1
OaWhziK9AulU2sw6D+vthMGJ+KDCyvGeDS4BwYR47eDwey7fKFAAtLvXpMRTjiVc
P7YkjPLR/HD+NfFUrWCy4f2bRQlMx1zjqVQjI7WwsSGmtGdfJtIIlx4agEIcB0jf
v0+6V2FIWf3XsaVE/vUX8tjMo4gz0FpKpR/V2bEFKKNs/6MzIpk6pkEGQOmGrsRo
6OyXoJ0eb6absVdmswP4rNkOLK66D9fMxmtst5KlxhUpTntxk17yNSnQJ5XBJZMC
AwEAATANBgkqhkiG9w0BAQsFAAOCAQEABP4217+dVaHbhfAtk7C9JVU+rM8v0G9q
jmF9Ntc5+E1bwJgjpLuKKxXP5zv8rvw/s1WIE5C5BxtLrjfOsKeSLvil5xnjmudd
7BY7pd1QafzylSU4cWK4aNO2VixFsRLkdDWO8WCt/2akiYixb1ulTO77BdA/inqn
dPs2WIwmabw4gC3TzZtND4bzW+O4jPq++mAb67dYdWDsPUpKokrer8+pzSF/R5Yo
FVaKgN7cou62mQ8lIHbIRxvAat1SUNUUHmlM2y/wIYuqZrgMHq3Ra6PCcYHnwR2D
OmZ3E+0oPxaILtIpR+H8E4wOEriGW4jjmluq9uc17Kmsw9QhQeCn2w==
-----END CERTIFICATE-----
Loading

0 comments on commit 572a806

Please sign in to comment.