package godog import ( "context" "encoding/json" "fmt" "github.com/cucumber/godog" "github.com/ohler55/ojg/jp" "net/http/httptest" ) func (gtc *genericTestCenter) theResourceIsCreated(ctx context.Context) (context.Context, error) { return gtc.theResponseHasStatus(ctx, 201) } func (gtc *genericTestCenter) theResourceAlreadyExists(ctx context.Context) (context.Context, error) { return gtc.theResponseHasStatus(ctx, 409) } func (gtc *genericTestCenter) theUserMissRight(ctx context.Context) (context.Context, error) { return gtc.theResponseHasStatus(ctx, 403) } func (gtc *genericTestCenter) badRequest(ctx context.Context) (context.Context, error) { return gtc.theResponseHasStatus(ctx, 400) } func (gtc *genericTestCenter) forbiddenRequest(ctx context.Context) (context.Context, error) { return gtc.theResponseHasStatus(ctx, 403) } func (gtc *genericTestCenter) theResponseHasStatus(ctx context.Context, status int) (context.Context, error) { httpRecorder := ctx.Value("recorder").(*httptest.ResponseRecorder) if httpRecorder.Code != status { return ctx, fmt.Errorf("got code %d with body %s", httpRecorder.Code, httpRecorder.Body.String()) } ctx = context.WithValue(ctx, "OBJECT_RESPONSE", httpRecorder.Body.String()) return ctx, nil } func (gtc *genericTestCenter) theFieldHasValue(ctx context.Context, fieldName, value string) (context.Context, error) { compiledPath, err := jp.ParseString(fieldName) body := ctx.Value("OBJECT_RESPONSE").(string) if err != nil { return ctx, err } var resourceAsMap interface{} err = json.Unmarshal([]byte(body), &resourceAsMap) if err != nil { return ctx, err } datas := compiledPath.Get(resourceAsMap) if len(datas) != 1 { return ctx, fmt.Errorf("Found %v data. Expected only one", len(datas)) } if datas[0] != value { return ctx, fmt.Errorf("The field %s has value %s . Expected %s", fieldName, datas[0], value) } return ctx, nil } func (gtc *genericTestCenter) resourceDoesntExist(ctx context.Context, resourceType string, resourceName string) (context.Context, error) { handler := GetResourceHandler(resourceType) ctx, _ = handler.Delete(ctx, resourceName) return ctx, nil } func (gtc *genericTestCenter) theUserCreatesAResourcesWithTheFollowingData(ctx context.Context, resourceType string, content *godog.DocString) (context.Context, error) { handler := GetResourceHandler(resourceType) ctx, _ = handler.Create(ctx, content.Content) return ctx, nil } func (gtc *genericTestCenter) theUserPatchesAResourcesWithTheFollowingData(ctx context.Context, resourceType string, content *godog.DocString) (context.Context, error) { handler := GetResourceHandler(resourceType) ctx, _ = handler.Patch(ctx, content.Content) return ctx, nil } func (gtc *genericTestCenter) theUserUpdatesAResourcesWithTheFollowingData(ctx context.Context, resourceType string, content *godog.DocString) (context.Context, error) { handler := GetResourceHandler(resourceType) ctx, _ = handler.Update(ctx, content.Content) return ctx, nil } func (gtc *genericTestCenter) theResourceExistWithTheFollowingData(ctx context.Context, resourceType string, content *godog.DocString) (context.Context, error) { handler := GetResourceHandler(resourceType) ctx, _ = handler.Create(ctx, content.Content) return ctx, nil }