github.com/hellofresh/janus@v0.0.0-20230925145208-ce8de8183c67/features/bootstrap/context_api.go (about)

     1  package bootstrap
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/cucumber/godog"
     8  	"github.com/cucumber/messages-go/v10"
     9  
    10  	"github.com/hellofresh/janus/pkg/api"
    11  )
    12  
    13  // RegisterAPIContext registers godog suite context for handling API related steps
    14  func RegisterAPIContext(ctx *godog.ScenarioContext, apiRepo api.Repository, ch chan<- api.ConfigurationMessage) {
    15  	scenarioCtx := &apiContext{apiRepo: apiRepo, ch: ch}
    16  
    17  	ctx.BeforeScenario(scenarioCtx.clearAPI)
    18  }
    19  
    20  type apiContext struct {
    21  	apiRepo api.Repository
    22  	ch      chan<- api.ConfigurationMessage
    23  }
    24  
    25  func (c *apiContext) clearAPI(*messages.Pickle) {
    26  	data, err := c.apiRepo.FindAll()
    27  	if err != nil {
    28  		panic(fmt.Errorf("failed to get all registered route specs: %w", err))
    29  	}
    30  
    31  	for _, definition := range data {
    32  		c.ch <- api.ConfigurationMessage{
    33  			Operation:     api.RemovedOperation,
    34  			Configuration: definition,
    35  		}
    36  	}
    37  
    38  	time.Sleep(time.Second)
    39  }