Skip to content

Commit

Permalink
Updated deprecated godog api
Browse files Browse the repository at this point in the history
  • Loading branch information
vgarvardt committed Aug 11, 2020
1 parent 7bb42ef commit 76e7679
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 63 deletions.
6 changes: 3 additions & 3 deletions features/bootstrap/context_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
)

// RegisterAPIContext registers godog suite context for handling API related steps
func RegisterAPIContext(s *godog.Suite, apiRepo api.Repository, ch chan<- api.ConfigurationMessage) {
ctx := &apiContext{apiRepo: apiRepo, ch: ch}
func RegisterAPIContext(ctx *godog.ScenarioContext, apiRepo api.Repository, ch chan<- api.ConfigurationMessage) {
scenarioCtx := &apiContext{apiRepo: apiRepo, ch: ch}

s.BeforeScenario(ctx.clearAPI)
ctx.BeforeScenario(scenarioCtx.clearAPI)
}

type apiContext struct {
Expand Down
8 changes: 4 additions & 4 deletions features/bootstrap/context_misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
const durationAWhile = time.Second

// RegisterMiscContext registers godog suite context for handling misc steps
func RegisterMiscContext(s *godog.Suite) {
ctx := &miscContext{}
func RegisterMiscContext(ctx *godog.ScenarioContext) {
scenarioCtx := &miscContext{}

s.Step(`^I wait for a while$`, ctx.iWaitForAWhile)
s.Step(`^I wait for "([^"]*)"$`, ctx.iWaitFor)
ctx.Step(`^I wait for a while$`, scenarioCtx.iWaitForAWhile)
ctx.Step(`^I wait for "([^"]*)"$`, scenarioCtx.iWaitFor)
}

type miscContext struct{}
Expand Down
40 changes: 20 additions & 20 deletions features/bootstrap/context_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,33 @@ const (
)

// RegisterRequestContext registers godog suite context for handling HTTP-requests related steps
func RegisterRequestContext(s *godog.Suite, port, apiPort, portSecondary, apiPortSecondary int, adminCred config.Credentials) {
ctx := &requestContext{
func RegisterRequestContext(ctx *godog.ScenarioContext, port, apiPort, portSecondary, apiPortSecondary int, adminCred config.Credentials) {
scenarioCtx := &requestContext{
port: port,
apiPort: apiPort,
portSecondary: portSecondary,
apiPortSecondary: apiPortSecondary,
adminCred: adminCred,
}

ctx.requestHeaders = make(http.Header)

s.Step(`^I request "([^"]*)" path with "([^"]*)" method$`, ctx.iRequestPathWithMethod)
s.Step(`^I request "([^"]*)" API path with "([^"]*)" method$`, ctx.iRequestAPIPathWithMethod)
s.Step(`^I request "([^"]*)" secondary path with "([^"]*)" method$`, ctx.iRequestSecondaryPathWithMethod)
s.Step(`^I request "([^"]*)" secondary API path with "([^"]*)" method$`, ctx.iRequestSecondaryAPIPathWithMethod)
s.Step(`^I should receive (\d+) response code$`, ctx.iShouldReceiveResponseCode)
s.Step(`^header "([^"]*)" should be "([^"]*)"$`, ctx.headerShouldBe)
s.Step(`^header "([^"]*)" should start with "([^"]*)"$`, ctx.headerShouldStartWith)
s.Step(`^the response should contain "([^"]*)"$`, ctx.responseShouldContain)
s.Step(`^response JSON body has "([^"]*)" path with value \'([^']*)\'$`, ctx.responseJSONBodyHasPathWithValue)
s.Step(`^response JSON body has "([^"]*)" path and is an array of length (\d+)$`, ctx.responseJSONBodyHasPathIsAnArrayOfLenght)
s.Step(`^response JSON body has "([^"]*)" path`, ctx.responseJSONBodyHasPath)
s.Step(`^response JSON body is an array of length (\d+)$`, ctx.responseJSONBodyIsAnArrayOfLength)
s.Step(`^request JSON payload:$`, ctx.requestJSONPayload)
s.Step(`^request header "([^"]*)" is set to "([^"]*)"$`, ctx.requestHeaderIsSetTo)
s.Step(`^request JWT token is not set$`, ctx.requestJWTTokenIsNotSet)
s.Step(`^request JWT token is valid admin token$`, ctx.requestJWTTokenIsValidAdminToken)
scenarioCtx.requestHeaders = make(http.Header)

ctx.Step(`^I request "([^"]*)" path with "([^"]*)" method$`, scenarioCtx.iRequestPathWithMethod)
ctx.Step(`^I request "([^"]*)" API path with "([^"]*)" method$`, scenarioCtx.iRequestAPIPathWithMethod)
ctx.Step(`^I request "([^"]*)" secondary path with "([^"]*)" method$`, scenarioCtx.iRequestSecondaryPathWithMethod)
ctx.Step(`^I request "([^"]*)" secondary API path with "([^"]*)" method$`, scenarioCtx.iRequestSecondaryAPIPathWithMethod)
ctx.Step(`^I should receive (\d+) response code$`, scenarioCtx.iShouldReceiveResponseCode)
ctx.Step(`^header "([^"]*)" should be "([^"]*)"$`, scenarioCtx.headerShouldBe)
ctx.Step(`^header "([^"]*)" should start with "([^"]*)"$`, scenarioCtx.headerShouldStartWith)
ctx.Step(`^the response should contain "([^"]*)"$`, scenarioCtx.responseShouldContain)
ctx.Step(`^response JSON body has "([^"]*)" path with value \'([^']*)\'$`, scenarioCtx.responseJSONBodyHasPathWithValue)
ctx.Step(`^response JSON body has "([^"]*)" path and is an array of length (\d+)$`, scenarioCtx.responseJSONBodyHasPathIsAnArrayOfLenght)
ctx.Step(`^response JSON body has "([^"]*)" path`, scenarioCtx.responseJSONBodyHasPath)
ctx.Step(`^response JSON body is an array of length (\d+)$`, scenarioCtx.responseJSONBodyIsAnArrayOfLength)
ctx.Step(`^request JSON payload:$`, scenarioCtx.requestJSONPayload)
ctx.Step(`^request header "([^"]*)" is set to "([^"]*)"$`, scenarioCtx.requestHeaderIsSetTo)
ctx.Step(`^request JWT token is not set$`, scenarioCtx.requestJWTTokenIsNotSet)
ctx.Step(`^request JWT token is valid admin token$`, scenarioCtx.requestJWTTokenIsValidAdminToken)
}

type requestContext struct {
Expand Down
84 changes: 48 additions & 36 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,54 +15,66 @@ import (
"github.com/hellofresh/janus/pkg/config"
)

func FeatureContext(s *godog.Suite) {
c, err := config.LoadEnv()
if nil != err {
panic(err)
}

var apiRepo api.Repository
var (
apiRepo api.Repository
cfg *config.Specification
portSecondary int
apiPortSecondary int
cfgChan chan api.ConfigurationMessage
)

dsnURL, err := url.Parse(c.Database.DSN)
if nil != err {
panic(err)
}
func InitializeTestSuite(ctx *godog.TestSuiteContext) {
var err error

switch dsnURL.Scheme {
case "mongodb":
apiRepo, err = api.NewMongoAppRepository(c.Database.DSN, c.BackendFlushInterval)
ctx.BeforeSuite(func() {
cfg, err = config.LoadEnv()
if err != nil {
panic(err)
}
case "file":
var apiPath = dsnURL.Path + "/apis"

apiRepo, err = api.NewFileSystemRepository(apiPath)
if err != nil {
dsnURL, err := url.Parse(cfg.Database.DSN)
if nil != err {
panic(err)
}
default:
panic("invalid database")
}

portSecondary, err := strconv.Atoi(os.Getenv("PORT_SECONDARY"))
if nil != err {
panic(err)
}
switch dsnURL.Scheme {
case "mongodb":
apiRepo, err = api.NewMongoAppRepository(cfg.Database.DSN, cfg.BackendFlushInterval)
if err != nil {
panic(err)
}
case "file":
var apiPath = dsnURL.Path + "/apis"

apiRepo, err = api.NewFileSystemRepository(apiPath)
if err != nil {
panic(err)
}
default:
panic("invalid database")
}

portSecondary, err = strconv.Atoi(os.Getenv("PORT_SECONDARY"))
if err != nil {
panic(err)
}

apiPortSecondary, err := strconv.Atoi(os.Getenv("API_PORT_SECONDARY"))
if nil != err {
panic(err)
}
apiPortSecondary, err = strconv.Atoi(os.Getenv("API_PORT_SECONDARY"))
if err != nil {
panic(err)
}

ch := make(chan api.ConfigurationMessage, 100)
if listener, ok := apiRepo.(api.Listener); ok {
listener.Listen(context.Background(), ch)
}
cfgChan = make(chan api.ConfigurationMessage, 100)
if listener, ok := apiRepo.(api.Listener); ok {
listener.Listen(context.Background(), cfgChan)
}
})
}

bootstrap.RegisterRequestContext(s, c.Port, c.Web.Port, portSecondary, apiPortSecondary, c.Web.Credentials)
bootstrap.RegisterAPIContext(s, apiRepo, ch)
bootstrap.RegisterMiscContext(s)
func InitializeScenario(ctx *godog.ScenarioContext) {
bootstrap.RegisterRequestContext(ctx, cfg.Port, cfg.Web.Port, portSecondary, apiPortSecondary, cfg.Web.Credentials)
bootstrap.RegisterAPIContext(ctx, apiRepo, cfgChan)
bootstrap.RegisterMiscContext(ctx)
}

func Test_Fake(t *testing.T) {
Expand Down

0 comments on commit 76e7679

Please sign in to comment.