Skip to content

Commit 0208419

Browse files
committed
include unit tests
1 parent be8a625 commit 0208419

7 files changed

Lines changed: 909 additions & 62 deletions

File tree

platform-api/src/api/generated.go

Lines changed: 14 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

platform-api/src/internal/model/deployment.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ type WebSubAPIDeploymentSpec struct {
125125
Version string `yaml:"version"`
126126
Context string `yaml:"context"`
127127
Vhosts *WebSubAPIDeploymentVhosts `yaml:"vhosts,omitempty"`
128-
AllChannels *WebSubDeployAllChannelPolicies `json:"allChannels,omitempty"`
128+
AllChannels *WebSubDeployAllChannelPolicies `yaml:"allChannels,omitempty"`
129129
Receiver *WebSubDeployReceiver `yaml:"receiver,omitempty"`
130130
Hub *WebSubDeployHub `yaml:"hub,omitempty"`
131131
Delivery *WebSubDeployDelivery `yaml:"delivery,omitempty"`
@@ -153,19 +153,15 @@ type WebSubDeployAllChannelPolicies struct {
153153
OnMessageDelivery *WebSubDeployEventPolicies `yaml:"on_message_delivery,omitempty"`
154154
}
155155

156-
// WebSubDeployChannelPolicies represents per-channel policies in the deployment YAML, organized by event type.
157-
type WebSubDeployChannelPolicies struct {
156+
// WebSubDeployChannel represents a single channel entry in the deployment YAML.
157+
// Event policies are at the top level to match the gateway-controller's WebSubChannel schema.
158+
type WebSubDeployChannel struct {
158159
OnSubscription *WebSubDeployEventPolicies `yaml:"on_subscription,omitempty"`
159160
OnUnsubscription *WebSubDeployEventPolicies `yaml:"on_unsubscription,omitempty"`
160161
OnMessageReceived *WebSubDeployEventPolicies `yaml:"on_message_received,omitempty"`
161162
OnMessageDelivery *WebSubDeployEventPolicies `yaml:"on_message_delivery,omitempty"`
162163
}
163164

164-
// WebSubDeployChannel represents a single channel entry in the deployment YAML.
165-
type WebSubDeployChannel struct {
166-
Policies *WebSubDeployChannelPolicies `yaml:"policies,omitempty"`
167-
}
168-
169165
// WebSubDeployReceiver represents the receiver section in the deployment YAML.
170166
type WebSubDeployReceiver struct {
171167
Policies []Policy `yaml:"policies"`

platform-api/src/internal/service/websub_api.go

Lines changed: 99 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func (s *WebSubAPIService) Create(orgUUID, createdBy string, req *api.WebSubAPI)
144144
Context: req.Context,
145145
Channels: mapWebSubChannelsAPIToModel(req.Channels),
146146
Upstream: *mapUpstreamAPIToModel(req.Upstream),
147-
AllChannels: mapWebSubAllChannelPoliciesAPIToModel(req.AllChannels),
147+
AllChannels: mapWebSubPoliciesAPIToAllChannels(req.Policies),
148148
SubscriptionPlans: subscriptionPlans,
149149
},
150150
}
@@ -259,7 +259,7 @@ func (s *WebSubAPIService) Update(orgUUID, handle string, req *api.WebSubAPI) (*
259259
Context: req.Context,
260260
Channels: mapWebSubChannelsAPIToModel(req.Channels),
261261
Upstream: *mapUpstreamAPIToModel(req.Upstream),
262-
AllChannels: mapWebSubAllChannelPoliciesAPIToModel(req.AllChannels),
262+
AllChannels: mapWebSubPoliciesAPIToAllChannels(req.Policies),
263263
SubscriptionPlans: subscriptionPlans,
264264
}
265265

@@ -415,7 +415,7 @@ func mapWebSubAPIModelToAPI(m *model.WebSubAPI, apiUtil *utils.APIUtil) *api.Web
415415
Context: m.Configuration.Context,
416416
Upstream: mapUpstreamModelToAPI(&m.Configuration.Upstream),
417417
Channels: mapWebSubChannelsModelToAPI(m.Configuration.Channels),
418-
AllChannels: mapWebSubAllChannelPoliciesModelToAPI(m.Configuration.AllChannels),
418+
Policies: mapAllChannelsModelToWebSubPolicies(m.Configuration.AllChannels),
419419
SubscriptionPlans: subscriptionPlans,
420420
CreatedAt: utils.TimePtr(m.CreatedAt),
421421
UpdatedAt: utils.TimePtr(m.UpdatedAt),
@@ -431,11 +431,15 @@ func mapWebSubChannelsAPIToModel(in *map[string]api.WebSubChannel) map[string]mo
431431
}
432432
out := make(map[string]model.WebSubChannel, len(*in))
433433
for name, ch := range *in {
434+
var p *api.WebSubChannelPolicies
435+
if ch.Policies != nil {
436+
p = ch.Policies
437+
}
434438
out[name] = model.WebSubChannel{
435-
OnSubscription: mapEventPoliciesAPIToModel(ch.OnSubscription),
436-
OnUnsubscription: mapEventPoliciesAPIToModel(ch.OnUnsubscription),
437-
OnMessageReceived: mapEventPoliciesAPIToModel(ch.OnMessageReceived),
438-
OnMessageDelivery: mapEventPoliciesAPIToModel(ch.OnMessageDelivery),
439+
OnSubscription: policySlicePtrToEventPolicies(policySlicePtrFromChannelPolicies(p, "on_subscription")),
440+
OnUnsubscription: policySlicePtrToEventPolicies(policySlicePtrFromChannelPolicies(p, "on_unsubscription")),
441+
OnMessageReceived: policySlicePtrToEventPolicies(policySlicePtrFromChannelPolicies(p, "on_message_received")),
442+
OnMessageDelivery: policySlicePtrToEventPolicies(policySlicePtrFromChannelPolicies(p, "on_message_delivery")),
439443
}
440444
}
441445
return out
@@ -457,10 +461,10 @@ func mapWebSubChannelPoliciesAPIToModel(in *api.WebSubChannelPolicies) *model.We
457461
return nil
458462
}
459463
return &model.WebSubChannelPolicies{
460-
OnSubscription: mapEventPoliciesAPIToModel(in.OnSubscription),
461-
OnUnsubscription: mapEventPoliciesAPIToModel(in.OnUnsubscription),
462-
OnMessageReceived: mapEventPoliciesAPIToModel(in.OnMessageReceived),
463-
OnMessageDelivery: mapEventPoliciesAPIToModel(in.OnMessageDelivery),
464+
OnSubscription: policySlicePtrToEventPolicies(in.OnSubscription),
465+
OnUnsubscription: policySlicePtrToEventPolicies(in.OnUnsubscription),
466+
OnMessageReceived: policySlicePtrToEventPolicies(in.OnMessageReceived),
467+
OnMessageDelivery: policySlicePtrToEventPolicies(in.OnMessageDelivery),
464468
}
465469
}
466470

@@ -477,6 +481,80 @@ func mapWebSubAllChannelPoliciesAPIToModel(in *api.WebSubAllChannelPolicies) *mo
477481
}
478482
}
479483

484+
// mapWebSubPoliciesAPIToAllChannels converts flat WebSubChannelPolicies (from API) to model.WebSubAllChannelPolicies (for storage).
485+
func mapWebSubPoliciesAPIToAllChannels(in *api.WebSubChannelPolicies) *model.WebSubAllChannelPolicies {
486+
if in == nil {
487+
return nil
488+
}
489+
return &model.WebSubAllChannelPolicies{
490+
OnSubscription: policySlicePtrToEventPolicies(in.OnSubscription),
491+
OnUnsubscription: policySlicePtrToEventPolicies(in.OnUnsubscription),
492+
OnMessageReceived: policySlicePtrToEventPolicies(in.OnMessageReceived),
493+
OnMessageDelivery: policySlicePtrToEventPolicies(in.OnMessageDelivery),
494+
}
495+
}
496+
497+
// mapAllChannelsModelToWebSubPolicies converts stored model.WebSubAllChannelPolicies to flat WebSubChannelPolicies (for API response).
498+
func mapAllChannelsModelToWebSubPolicies(in *model.WebSubAllChannelPolicies) *api.WebSubChannelPolicies {
499+
if in == nil {
500+
return nil
501+
}
502+
return &api.WebSubChannelPolicies{
503+
OnSubscription: eventPoliciesToPolicySlicePtr(in.OnSubscription),
504+
OnUnsubscription: eventPoliciesToPolicySlicePtr(in.OnUnsubscription),
505+
OnMessageReceived: eventPoliciesToPolicySlicePtr(in.OnMessageReceived),
506+
OnMessageDelivery: eventPoliciesToPolicySlicePtr(in.OnMessageDelivery),
507+
}
508+
}
509+
510+
// policySlicePtrToEventPolicies wraps a flat policy slice pointer into a model.WebSubEventPolicies.
511+
func policySlicePtrToEventPolicies(in *[]api.Policy) *model.WebSubEventPolicies {
512+
if in == nil {
513+
return nil
514+
}
515+
policies := make([]model.Policy, 0, len(*in))
516+
for _, p := range *in {
517+
policy := model.Policy{
518+
Name: p.Name,
519+
Version: p.Version,
520+
}
521+
if p.ExecutionCondition != nil {
522+
policy.ExecutionCondition = p.ExecutionCondition
523+
}
524+
if p.Params != nil {
525+
policy.Params = p.Params
526+
}
527+
policies = append(policies, policy)
528+
}
529+
return &model.WebSubEventPolicies{Policies: policies}
530+
}
531+
532+
// eventPoliciesToPolicySlicePtr converts a model.WebSubEventPolicies to a flat policy slice pointer.
533+
func eventPoliciesToPolicySlicePtr(in *model.WebSubEventPolicies) *[]api.Policy {
534+
if in == nil || len(in.Policies) == 0 {
535+
return nil
536+
}
537+
return mapModelPolicySliceToAPI(in.Policies)
538+
}
539+
540+
// policySlicePtrFromChannelPolicies extracts the policy slice for a given event type from WebSubChannelPolicies.
541+
func policySlicePtrFromChannelPolicies(p *api.WebSubChannelPolicies, event string) *[]api.Policy {
542+
if p == nil {
543+
return nil
544+
}
545+
switch event {
546+
case "on_subscription":
547+
return p.OnSubscription
548+
case "on_unsubscription":
549+
return p.OnUnsubscription
550+
case "on_message_received":
551+
return p.OnMessageReceived
552+
case "on_message_delivery":
553+
return p.OnMessageDelivery
554+
}
555+
return nil
556+
}
557+
480558
// mapWebSubChannelsModelToAPI converts the model channel map to the API channel map.
481559
func mapWebSubChannelsModelToAPI(in map[string]model.WebSubChannel) *map[string]api.WebSubChannel {
482560
if len(in) == 0 {
@@ -485,10 +563,12 @@ func mapWebSubChannelsModelToAPI(in map[string]model.WebSubChannel) *map[string]
485563
out := make(map[string]api.WebSubChannel, len(in))
486564
for name, ch := range in {
487565
out[name] = api.WebSubChannel{
488-
OnSubscription: mapEventPoliciesModelToAPI(ch.OnSubscription),
489-
OnUnsubscription: mapEventPoliciesModelToAPI(ch.OnUnsubscription),
490-
OnMessageReceived: mapEventPoliciesModelToAPI(ch.OnMessageReceived),
491-
OnMessageDelivery: mapEventPoliciesModelToAPI(ch.OnMessageDelivery),
566+
Policies: &api.WebSubChannelPolicies{
567+
OnSubscription: eventPoliciesToPolicySlicePtr(ch.OnSubscription),
568+
OnUnsubscription: eventPoliciesToPolicySlicePtr(ch.OnUnsubscription),
569+
OnMessageReceived: eventPoliciesToPolicySlicePtr(ch.OnMessageReceived),
570+
OnMessageDelivery: eventPoliciesToPolicySlicePtr(ch.OnMessageDelivery),
571+
},
492572
}
493573
}
494574
return &out
@@ -510,10 +590,10 @@ func mapWebSubChannelPoliciesModelToAPI(in *model.WebSubChannelPolicies) *api.We
510590
return nil
511591
}
512592
return &api.WebSubChannelPolicies{
513-
OnSubscription: mapEventPoliciesModelToAPI(in.OnSubscription),
514-
OnUnsubscription: mapEventPoliciesModelToAPI(in.OnUnsubscription),
515-
OnMessageReceived: mapEventPoliciesModelToAPI(in.OnMessageReceived),
516-
OnMessageDelivery: mapEventPoliciesModelToAPI(in.OnMessageDelivery),
593+
OnSubscription: eventPoliciesToPolicySlicePtr(in.OnSubscription),
594+
OnUnsubscription: eventPoliciesToPolicySlicePtr(in.OnUnsubscription),
595+
OnMessageReceived: eventPoliciesToPolicySlicePtr(in.OnMessageReceived),
596+
OnMessageDelivery: eventPoliciesToPolicySlicePtr(in.OnMessageDelivery),
517597
}
518598
}
519599

platform-api/src/internal/service/websub_api_deployment.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -637,25 +637,15 @@ func buildWebSubDeployChannels(channels map[string]model.WebSubChannel) map[stri
637637
result := make(map[string]model.WebSubDeployChannel, len(channels))
638638
for name, ch := range channels {
639639
result[name] = model.WebSubDeployChannel{
640-
Policies: buildWebSubDeployChannelPoliciesFromChannel(ch),
640+
OnSubscription: generateEventPolicyList(ch.OnSubscription),
641+
OnUnsubscription: generateEventPolicyList(ch.OnUnsubscription),
642+
OnMessageReceived: generateEventPolicyList(ch.OnMessageReceived),
643+
OnMessageDelivery: generateEventPolicyList(ch.OnMessageDelivery),
641644
}
642645
}
643646
return result
644647
}
645648

646-
// buildWebSubDeployChannelPoliciesFromChannel converts a model channel to deployment channel policies.
647-
func buildWebSubDeployChannelPoliciesFromChannel(ch model.WebSubChannel) *model.WebSubDeployChannelPolicies {
648-
if ch.OnSubscription == nil && ch.OnUnsubscription == nil && ch.OnMessageReceived == nil && ch.OnMessageDelivery == nil {
649-
return nil
650-
}
651-
return &model.WebSubDeployChannelPolicies{
652-
OnSubscription: generateEventPolicyList(ch.OnSubscription),
653-
OnUnsubscription: generateEventPolicyList(ch.OnUnsubscription),
654-
OnMessageReceived: generateEventPolicyList(ch.OnMessageReceived),
655-
OnMessageDelivery: generateEventPolicyList(ch.OnMessageDelivery),
656-
}
657-
}
658-
659649
func generateEventPolicyList(ep *model.WebSubEventPolicies) *model.WebSubDeployEventPolicies {
660650
if ep == nil {
661651
return nil

0 commit comments

Comments
 (0)