Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: WEOS-1133 Generate and Ensure BDD Test #58

Merged
merged 13 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@ paths:
operationId: Get Blogs
summary: Get List of Blogs
parameters:
- in: query
name: page
schema:
type: integer
- in: query
name: l
x-alias: limit
schema:
type: integer
- in: query
name: filters
schema:
Expand Down
4 changes: 2 additions & 2 deletions controllers/rest/controller_standard.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,8 @@ func (c *StandardControllers) List(app model.Service, spec *openapi3.Swagger, pa
})
}
//gets the limit and page from context
limit := newContext.Value("limit").(int)
page := newContext.Value("page").(int)
limit, _ := newContext.Value("limit").(int)
page, _ := newContext.Value("page").(int)
if page == 0 {
page = 1
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/rest/middleware_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ func parseParams(c echo.Context, cc context.Context, parameter *openapi3.Paramet
return nil, err
}
}
var val interface{}
//if there is an alias name specified use that instead. The value is a json.RawMessage (not a string)
if tcontextName, ok := parameter.Value.ExtensionProps.Extensions[AliasExtension]; ok {
err := json.Unmarshal(tcontextName.(json.RawMessage), &contextName)
if err != nil {
return nil, err
}
}
var val interface{}
switch strings.ToLower(parameter.Value.In) {
//parameter values stored as strings
case "header":
Expand Down
24 changes: 14 additions & 10 deletions controllers/rest/middleware_initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,17 +238,19 @@ func AddStandardController(e *echo.Echo, pathData *openapi3.PathItem, method str
//check for identifiers
if identifiers != nil && len(identifiers) > 0 {
for _, identifier := range identifiers {
foundIdentifier := false
//check the parameters for the identifiers
for _, param := range pathData.Put.Parameters {
cName := param.Value.ExtensionProps.Extensions[ContextNameExtension]
if identifier == param.Value.Name || (cName != nil && identifier == cName.(string)) {
foundIdentifier = true
break
}
if !(identifier == param.Value.Name) && !(cName != nil && identifier == cName.(string)) {
allParam = false
e.Logger.Warnf("unexpected error: a parameter for each part of the identifier must be set")
return autoConfigure, nil
}
}
if !foundIdentifier {
allParam = false
e.Logger.Warnf("unexpected error: a parameter for each part of the identifier must be set")
return autoConfigure, nil
}
}
if allParam {
Expand Down Expand Up @@ -356,17 +358,19 @@ func AddStandardController(e *echo.Echo, pathData *openapi3.PathItem, method str
var contextName string
if identifiers != nil && len(identifiers) > 0 {
for _, identifier := range identifiers {
foundIdentifier := false
//check the parameters
for _, param := range pathData.Get.Parameters {
cName := param.Value.ExtensionProps.Extensions[ContextNameExtension]
if identifier == param.Value.Name || (cName != nil && identifier == cName.(string)) {
foundIdentifier = true
break
}
if !(identifier == param.Value.Name) && !(cName != nil && identifier == cName.(string)) {
allParam = false
e.Logger.Warnf("unexpected error: a parameter for each part of the identifier must be set")
return autoConfigure, nil
}
}
if !foundIdentifier {
allParam = false
e.Logger.Warnf("unexpected error: a parameter for each part of the identifier must be set")
return autoConfigure, nil
}
}
}
Expand Down
26 changes: 13 additions & 13 deletions controllers/rest/weos_mocks_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 90 additions & 2 deletions end2end_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ var e *echo.Echo
var API api.RESTAPI
var openAPI string
var Developer *User
var Content *ContentType
var errs error
var buf bytes.Buffer
var payload ContentType
Expand All @@ -48,6 +47,10 @@ var binary string
var dockerFile string
var binaryMount string
var esContainer testcontainers.Container
var limit int
var page int
var contentType string
var result api.ListApiResponse

type User struct {
Name string
Expand All @@ -73,6 +76,7 @@ func InitializeSuite(ctx *godog.TestSuiteContext) {
requests = map[string]map[string]interface{}{}
contentTypeID = map[string]bool{}
Developer = &User{}
result = api.ListApiResponse{}
e = echo.New()
e.Logger.SetOutput(&buf)
os.Remove("e2e.db")
Expand Down Expand Up @@ -122,6 +126,7 @@ func reset(ctx context.Context, sc *godog.Scenario) (context.Context, error) {
requests = map[string]map[string]interface{}{}
contentTypeID = map[string]bool{}
Developer = &User{}
result = api.ListApiResponse{}
errs = nil
header = make(http.Header)
rec = httptest.NewRecorder()
Expand Down Expand Up @@ -769,6 +774,83 @@ func sojournerIsUpdatingWithId(contentType, id string) error {
return nil
}

func isOnTheListScreen(user, content string) error {
contentType = content
requests[strings.ToLower(contentType+"_list")] = map[string]interface{}{}
currScreen = strings.ToLower(contentType + "_list")
return nil
}

func theItemsPerPageAre(pageLimit int) error {
limit = pageLimit
return nil
}

func theListResultsShouldBe(details *godog.Table) error {
head := details.Rows[0].Cells
compare := map[string]interface{}{}
compareArray := []map[string]interface{}{}

for i := 1; i < len(details.Rows); i++ {
for n, cell := range details.Rows[i].Cells {
compare[head[n].Value] = cell.Value
}
compareArray = append(compareArray, compare)
compare = map[string]interface{}{}
}
foundItems := 0

json.NewDecoder(rec.Body).Decode(&result)
for i, entity := range compareArray {
foundEntity := true
for key, value := range entity {
if result.Items[i][key] != value {
foundEntity = false
break
}
}
if foundEntity {
foundItems++
}
}
if foundItems != len(compareArray) {
return fmt.Errorf("expected to find %d, got %d", len(compareArray), foundItems)
}

return nil
}

func thePageInTheResultShouldBe(pageResult int) error {
if result.Page != pageResult {
return fmt.Errorf("expect page to be %d, got %d", pageResult, result.Page)
}
return nil
}

func thePageNoIs(pageNo int) error {
page = pageNo
return nil
}

func theSearchButtonIsHit() error {
var request *http.Request
request = httptest.NewRequest("GET", "/"+strings.ToLower(contentType)+"?limit="+strconv.Itoa(limit)+"&page="+strconv.Itoa(page), nil)
request = request.WithContext(context.TODO())
header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
request.Header = header
request.Close = true
rec = httptest.NewRecorder()
e.ServeHTTP(rec, request)
return nil
}

func theTotalResultsShouldBe(totalResult int) error {
if result.Total != int64(totalResult) {
return fmt.Errorf("expect page to be %d, got %d", totalResult, result.Total)
}
return nil
}

func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.Before(reset)
//add context steps
Expand Down Expand Up @@ -811,7 +893,13 @@ func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.Step(`^the service is running$`, theServiceIsRunning)
ctx.Step(`^is run on the operating system "([^"]*)" as "([^"]*)"$`, isRunOnTheOperatingSystemAs)
ctx.Step(`^a warning should be output because the endpoint is invalid$`, aWarningShouldBeOutputBecauseTheEndpointIsInvalid)

ctx.Step(`^"([^"]*)" is on the "([^"]*)" list screen$`, isOnTheListScreen)
ctx.Step(`^the items per page are (\d+)$`, theItemsPerPageAre)
ctx.Step(`^the list results should be$`, theListResultsShouldBe)
ctx.Step(`^the page in the result should be (\d+)$`, thePageInTheResultShouldBe)
ctx.Step(`^the page no\. is (\d+)$`, thePageNoIs)
ctx.Step(`^the search button is hit$`, theSearchButtonIsHit)
ctx.Step(`^the total results should be (\d+)$`, theTotalResultsShouldBe)
}

func TestBDD(t *testing.T) {
Expand Down
Loading