Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
martinrode committed Jun 6, 2019
2 parents 6d22c95 + 9318b37 commit 6258f5b
Showing 1 changed file with 20 additions and 30 deletions.
50 changes: 20 additions & 30 deletions api_testcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/programmfabrik/fylr-apitest/lib/datastore"
"time"

"github.com/programmfabrik/fylr-apitest/lib/datastore"

"github.com/programmfabrik/fylr-apitest/lib/cjson"

"github.com/programmfabrik/fylr-apitest/lib/api"
Expand All @@ -17,22 +18,19 @@ import (
log "github.com/sirupsen/logrus"
)

const (
defaultTimeout int = 10
)

// Case defines the structure of our single testcase
// It gets read in by our config reader at the moment the mainfest.json gets parsed
type Case struct {
Name string `json:"name"`
RequestData *util.GenericJson `json:"request"`
TypedResponseData *CaseResponse `json:"response"`
responseData util.GenericJson
Name string `json:"name"`
RequestData *util.GenericJson `json:"request"`
ResponseData util.GenericJson `json:"response"`
ContinueOnFailure bool `json:"continue_on_failure"`
Store map[string]interface{} `json:"store"` // init datastore before testrun
StoreResponse map[string]string `json:"store_response_qjson"` // store qjson parsed response in datastore

Timeout int `json:"timeout_ms"`
WaitBefore *int `json:"wait_before_ms"`
WaitAfter *int `json:"wait_after_ms"`
Delay *int `json:"delay_ms"`
BreakResponse []util.GenericJson `json:"break_response"`
CollectResponse util.GenericJson `json:"collect_response"`
Expand Down Expand Up @@ -65,22 +63,6 @@ func (testCase Case) runAPITestCase() (success bool) {
}
log.Infof(" [%2d] '%s'", testCase.index, testCase.Name)

//Marshal external datastructure in our internal used reponseData
jsonResponse, err := json.Marshal(testCase.TypedResponseData)
if err != nil {
err = fmt.Errorf("error marshaling TypedResponseData: %s", err)
r.SaveToReportLog(fmt.Sprintf("Error during execution: %s", err))
log.Errorf(" [%2d] %s", testCase.index, err)
return false
}
err = json.Unmarshal(jsonResponse, &testCase.responseData)
if err != nil {
err = fmt.Errorf("error unmarshaling into responseData: %s", err)
r.SaveToReportLog(fmt.Sprintf("Error during execution: %s", err))
log.Errorf(" [%2d] %s", testCase.index, err)
return false
}

r.NewChild(testCase.Name)

start := time.Now()
Expand All @@ -93,7 +75,7 @@ func (testCase Case) runAPITestCase() (success bool) {

return false
}
err = testCase.dataStore.SetMap(testCase.Store)
err := testCase.dataStore.SetMap(testCase.Store)
if err != nil {
err = fmt.Errorf("error setting datastore map:%s", err)
r.SaveToReportLog(fmt.Sprintf("Error during execution: %s", err))
Expand Down Expand Up @@ -322,13 +304,16 @@ func (testCase Case) run() (success bool, err error) {

collectPresent := testCase.CollectResponse != nil

if testCase.WaitBefore != nil {
log.Infof("wait_before_ms: %d", *testCase.WaitBefore)
time.Sleep(time.Duration(*testCase.WaitBefore) * time.Millisecond)
}

//Poll repeats the request until the right response is found, or a timeout triggers
for {
// delay between repeating a request
if testCase.Delay != nil {
time.Sleep(time.Duration(*testCase.Delay) * time.Millisecond)
} else {
time.Sleep(time.Duration(defaultTimeout) * time.Millisecond)
}

responsesMatch, request, apiResponse, err = testCase.executeRequest(requestCounter)
Expand Down Expand Up @@ -409,6 +394,11 @@ func (testCase Case) run() (success bool, err error) {
return false, nil
}

if testCase.WaitAfter != nil {
log.Infof("wait_after_ms: %d", *testCase.WaitAfter)
time.Sleep(time.Duration(*testCase.WaitAfter) * time.Millisecond)
}

return true, nil
}

Expand All @@ -423,10 +413,10 @@ func (testCase Case) loadRequest() (req api.Request, err error) {

func (testCase Case) loadResponse() (res api.Response, err error) {
// unspecified response is interpreted as status_code 200
if testCase.responseData == nil {
if testCase.ResponseData == nil {
return api.NewResponse(200, nil, bytes.NewReader([]byte("")))
}
spec, err := testCase.loadResponseSerialization(testCase.responseData)
spec, err := testCase.loadResponseSerialization(testCase.ResponseData)
if err != nil {
return res, fmt.Errorf("error loading response spec: %s", err)
}
Expand Down

0 comments on commit 6258f5b

Please sign in to comment.