Skip to content

Commit

Permalink
chore: refactor messaging service to be used at the usecase level
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwellgithinji committed Sep 9, 2021
1 parent 636492c commit 8adb40b
Show file tree
Hide file tree
Showing 8 changed files with 923 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/savannahghi/feedlib"
"github.com/savannahghi/firebasetools"
)

// FakeServiceMessaging is a mock implementation of the "messaging" service
Expand All @@ -23,6 +24,11 @@ type FakeServiceMessaging struct {
SubscriptionIDsFn func() map[string]string

ReverseSubscriptionIDsFn func() map[string]string
PushFn func(
ctx context.Context,
sender string,
payload firebasetools.SendNotificationPayload,
) error
}

// Notify Sends a message to a topic
Expand All @@ -37,17 +43,26 @@ func (f *FakeServiceMessaging) Notify(
return f.NotifyFn(ctx, topicID, uid, flavour, payload, metadata)
}

// TopicIDs ...
// TopicIDs gets topic IDs
func (f *FakeServiceMessaging) TopicIDs() []string {
return f.TopicIDsFn()
}

// SubscriptionIDs ...
// SubscriptionIDs gets subscription IDs
func (f *FakeServiceMessaging) SubscriptionIDs() map[string]string {
return f.SubscriptionIDsFn()
}

// ReverseSubscriptionIDs ...
// ReverseSubscriptionIDs get Reverse Subscription IDs
func (f *FakeServiceMessaging) ReverseSubscriptionIDs() map[string]string {
return f.ReverseSubscriptionIDsFn()
}

// Push sends push notifications
func (f *FakeServiceMessaging) Push(
ctx context.Context,
sender string,
payload firebasetools.SendNotificationPayload,
) error {
return f.PushFn(ctx, sender, payload)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package messaging_test

import (
"context"
"reflect"
"testing"
"time"

Expand Down Expand Up @@ -148,6 +149,64 @@ func TestPubSubNotificationService_Notify(t *testing.T) {
}
}

func TestPubSubNotificationService_SubscriptionIDs(t *testing.T) {
ctx := context.Background()
projectID := serverutils.MustGetEnvVar(serverutils.GoogleCloudProjectIDEnvVarName)
srv, err := messaging.NewPubSubNotificationService(ctx, projectID)
if err != nil {
t.Errorf("can't initialize pubsub notification service: %s", err)
return
}

if srv == nil {
t.Errorf("nil pubsub notification service")
return
}

tests := []struct {
name string
want map[string]string
}{
{
name: "default case",
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := srv.SubscriptionIDs(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("PubSubNotificationService.ReverseSubscriptionIDs() = %v, want %v", got, tt.want)
}
})
}
}

func TestPubSubNotificationService_ReverseSubscriptionIDs(t *testing.T) {
ctx := context.Background()
projectID := serverutils.MustGetEnvVar(serverutils.GoogleCloudProjectIDEnvVarName)
srv, err := messaging.NewPubSubNotificationService(ctx, projectID)
if err != nil {
t.Errorf("can't initialize pubsub notification service: %s", err)
return
}
tests := []struct {
name string
want map[string]string
}{
{
name: "default case",
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := srv.ReverseSubscriptionIDs(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("PubSubNotificationService.ReverseSubscriptionIDs() = %v, want %v", got, tt.want)
}
})
}
}

func TestRemotePushService_Push(t *testing.T) {
ctx := context.Background()
projectID := serverutils.MustGetEnvVar(serverutils.GoogleCloudProjectIDEnvVarName)
Expand Down
275 changes: 275 additions & 0 deletions pkg/engagement/infrastructure/services/messaging/pubsub_unit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,275 @@
package messaging_test

import (
"context"
"fmt"
"reflect"
"testing"
"time"

"github.com/google/uuid"
"github.com/savannahghi/engagementcore/pkg/engagement/infrastructure/services/messaging"
messagingMock "github.com/savannahghi/engagementcore/pkg/engagement/infrastructure/services/messaging/mock"
"github.com/savannahghi/feedlib"
"github.com/savannahghi/firebasetools"
"github.com/segmentio/ksuid"
)

var (
fakemessagingService messagingMock.FakeServiceMessaging
)

func getSendNotificationPayload() *firebasetools.SendNotificationPayload {

img := "https://www.wxpr.org/sites/wxpr/files/styles/medium/public/202007/chipmunk-5401165_1920.jpg"
key := uuid.New().String()
fakeToken := uuid.New().String()
pckg := "video"

return &firebasetools.SendNotificationPayload{
RegistrationTokens: []string{fakeToken},
Data: map[string]string{
"some": "data",
},
Notification: &firebasetools.FirebaseSimpleNotificationInput{
Title: "Test Notification",
Body: "From Integration Tests",
ImageURL: &img,
Data: map[string]interface{}{
"more": "data",
},
},
Android: &firebasetools.FirebaseAndroidConfigInput{
Priority: "high",
CollapseKey: &key,
RestrictedPackageName: &pckg,
},
}
}

func TestUnit_Notify(t *testing.T) {
ctx := context.Background()
topicID := ksuid.New().String()
uid := ksuid.New().String()
flavour := feedlib.FlavourConsumer
el := &feedlib.Message{
ID: ksuid.New().String(),
SequenceNumber: 1,
Text: ksuid.New().String(),
ReplyTo: ksuid.New().String(),
PostedByUID: ksuid.New().String(),
PostedByName: ksuid.New().String(),
Timestamp: time.Now(),
}
metadata := map[string]interface{}{
"test": "metadata",
}

var s messaging.NotificationService = &fakemessagingService

type args struct {
ctx context.Context
topicID string
uid string
flavour feedlib.Flavour
el feedlib.Element
metadata map[string]interface{}
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "valid: correct params passed",
args: args{
ctx: ctx,
topicID: topicID,
uid: uid,
flavour: flavour,
el: el,
metadata: metadata,
},
wantErr: false,
},
{
name: "invalid: missing args",
args: args{
ctx: ctx,
},
wantErr: true,
},
}
for _, tt := range tests {
if tt.name == "valid: correct params passed" {
fakemessagingService.NotifyFn = func(
ctx context.Context,
topicID string,
uid string,
flavour feedlib.Flavour,
el feedlib.Element,
metadata map[string]interface{},
) error {
return nil
}
}
if tt.name == "invalid: missing args" {
fakemessagingService.NotifyFn = func(
ctx context.Context,
topicID string,
uid string,
flavour feedlib.Flavour,
el feedlib.Element,
metadata map[string]interface{},
) error {
return fmt.Errorf("test error")
}
}
t.Run(tt.name, func(t *testing.T) {
if err := s.Notify(tt.args.ctx, tt.args.topicID, tt.args.uid, tt.args.flavour, tt.args.el, tt.args.metadata); (err != nil) != tt.wantErr {
t.Errorf("PubSubNotificationService.Notify() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestUnit_TopicIDs(t *testing.T) {

var s messaging.NotificationService = &fakemessagingService

tests := []struct {
name string
want []string
}{
{
name: "default case",
want: []string{"testIDs"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.name == "default case" {
fakemessagingService.TopicIDsFn = func() []string {
return []string{"testIDs"}
}
}
if got := s.TopicIDs(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("PubSubNotificationService.TopicIDs() = %v, want %v", got, tt.want)
}
})
}
}

func TestUnit_SubscriptionIDs(t *testing.T) {

var s messaging.NotificationService = &fakemessagingService

tests := []struct {
name string
want map[string]string
}{
{
name: "default case",
want: map[string]string{"test": "id"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.name == "default case" {
fakemessagingService.SubscriptionIDsFn = func() map[string]string {
return map[string]string{"test": "id"}
}
}
if got := s.SubscriptionIDs(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("PubSubNotificationService.SubscriptionIDs() = %v, want %v", got, tt.want)
}
})
}
}

func TestUnit_ReverseSubscriptionIDs(t *testing.T) {
var s messaging.NotificationService = &fakemessagingService

tests := []struct {
name string
want map[string]string
}{
{
name: "default case",
want: map[string]string{"test": "id"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.name == "default case" {
fakemessagingService.ReverseSubscriptionIDsFn = func() map[string]string {
return map[string]string{"test": "id"}
}
}
if got := s.ReverseSubscriptionIDs(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("PubSubNotificationService.ReverseSubscriptionIDs() = %v, want %v", got, tt.want)
}
})
}
}

func TestUnit_Push(t *testing.T) {
ctx := context.Background()
sender := firebasetools.TestUserEmail
notificationPayload := getSendNotificationPayload()

var s messaging.NotificationService = &fakemessagingService

type args struct {
ctx context.Context
sender string
notificationPayload firebasetools.SendNotificationPayload
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "valid: correct params passed",
args: args{
ctx: ctx,
sender: sender,
notificationPayload: *notificationPayload,
},
wantErr: false,
},
{
name: "invalid: missing args passed",
args: args{
ctx: ctx,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.name == "valid: correct params passed" {
fakemessagingService.PushFn = func(
ctx context.Context,
sender string,
payload firebasetools.SendNotificationPayload,
) error {
return nil
}
}
if tt.name == "invalid: missing args passed" {
fakemessagingService.PushFn = func(
ctx context.Context,
sender string,
payload firebasetools.SendNotificationPayload,
) error {
return fmt.Errorf("test error")
}
}
if err := s.Push(tt.args.ctx, tt.args.sender, tt.args.notificationPayload); (err != nil) != tt.wantErr {
t.Errorf("PubSubNotificationService.Push() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

0 comments on commit 8adb40b

Please sign in to comment.