-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'kisamoto-62_add-service-plan-context'
[#164389015] Co-authored-by: Derik Evangelista <devangelista@pivotal.io>
- Loading branch information
Showing
4 changed files
with
141 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package brokerapi | ||
|
||
import "context" | ||
|
||
type contextKey string | ||
|
||
const ( | ||
contextKeyService contextKey = "brokerapi_service" | ||
contextKeyPlan contextKey = "brokerapi_plan" | ||
) | ||
|
||
func AddServiceToContext(ctx context.Context, service *Service) context.Context { | ||
if service != nil { | ||
return context.WithValue(ctx, contextKeyService, service) | ||
} | ||
return ctx | ||
} | ||
|
||
func RetrieveServiceFromContext(ctx context.Context) *Service { | ||
if value := ctx.Value(contextKeyService); value != nil { | ||
return value.(*Service) | ||
} | ||
return nil | ||
} | ||
|
||
func AddServicePlanToContext(ctx context.Context, plan *ServicePlan) context.Context { | ||
if plan != nil { | ||
return context.WithValue(ctx, contextKeyPlan, plan) | ||
} | ||
return ctx | ||
} | ||
|
||
func RetrieveServicePlanFromContext(ctx context.Context) *ServicePlan { | ||
if value := ctx.Value(contextKeyPlan); value != nil { | ||
return value.(*ServicePlan) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package brokerapi_test | ||
|
||
import ( | ||
"context" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/pivotal-cf/brokerapi" | ||
) | ||
|
||
var _ = Describe("Context Utilities", func() { | ||
|
||
type testContextKey string | ||
|
||
var ( | ||
ctx context.Context | ||
contextValidatorKey testContextKey | ||
contextValidatorValue string | ||
) | ||
|
||
BeforeEach(func() { | ||
contextValidatorKey = "context-utilities-test" | ||
contextValidatorValue = "original" | ||
ctx = context.Background() | ||
ctx = context.WithValue(ctx, contextValidatorKey, contextValidatorValue) | ||
}) | ||
|
||
Describe("Service Context", func() { | ||
Context("when the service is nil", func() { | ||
It("returns the original context", func() { | ||
ctx = brokerapi.AddServiceToContext(ctx, nil) | ||
Expect(ctx.Err()).To(BeZero()) | ||
Expect(brokerapi.RetrieveServiceFromContext(ctx)).To(BeZero()) | ||
Expect(ctx.Value(contextValidatorKey).(string)).To(Equal(contextValidatorValue)) | ||
}) | ||
}) | ||
|
||
Context("when the service is valid", func() { | ||
It("sets and receives the service in the context", func() { | ||
service := &brokerapi.Service{ | ||
ID: "9A3095D7-ED3C-45FA-BC9F-592820628723", | ||
Name: "Test Service", | ||
} | ||
ctx = brokerapi.AddServiceToContext(ctx, service) | ||
Expect(ctx.Err()).To(BeZero()) | ||
Expect(ctx.Value(contextValidatorKey).(string)).To(Equal(contextValidatorValue)) | ||
Expect(brokerapi.RetrieveServiceFromContext(ctx).ID).To(Equal(service.ID)) | ||
Expect(brokerapi.RetrieveServiceFromContext(ctx).Name).To(Equal(service.Name)) | ||
Expect(brokerapi.RetrieveServiceFromContext(ctx).Metadata).To(BeZero()) | ||
}) | ||
}) | ||
}) | ||
|
||
Describe("Plan Context", func() { | ||
Context("when the service plan is nil", func() { | ||
It("returns the original context", func() { | ||
ctx = brokerapi.AddServicePlanToContext(ctx, nil) | ||
Expect(ctx.Err()).To(BeZero()) | ||
Expect(brokerapi.RetrieveServicePlanFromContext(ctx)).To(BeZero()) | ||
Expect(ctx.Value(contextValidatorKey).(string)).To(Equal(contextValidatorValue)) | ||
}) | ||
}) | ||
|
||
Context("when the service plan is valid", func() { | ||
It("sets and retrieves the service plan in the context", func() { | ||
plan := &brokerapi.ServicePlan{ | ||
ID: "AC257573-8C62-4B1A-AC34-ECA3863F50EC", | ||
} | ||
ctx = brokerapi.AddServicePlanToContext(ctx, plan) | ||
Expect(ctx.Err()).To(BeZero()) | ||
Expect(ctx.Value(contextValidatorKey).(string)).To(Equal(contextValidatorValue)) | ||
Expect(brokerapi.RetrieveServicePlanFromContext(ctx).ID).To(Equal(plan.ID)) | ||
}) | ||
}) | ||
}) | ||
}) |