Skip to content
Snippets Groups Projects
Commit a5cccf91 authored by Valentin Yordanov's avatar Valentin Yordanov
Browse files

Create unit tests for notify client

parent 83a2f96d
No related branches found
No related tags found
1 merge request!45"Policy Changes Notification"
Pipeline #66999 passed with stages
in 3 minutes
This diff is collapsed.
......@@ -4,11 +4,7 @@ import (
"context"
)
type EventPolicyChange struct {
Name string `json:"name"`
Version string `json:"version"`
Group string `json:"group"`
}
//go:generate counterfeiter . Events
type Events interface {
Send(ctx context.Context, data any) error
......@@ -18,6 +14,12 @@ type Notifier struct {
events Events
}
type EventPolicyChange struct {
Name string `json:"name"`
Version string `json:"version"`
Group string `json:"group"`
}
// New creates a policy change notifier for interested subscribers.
// It can notify for policy changes both via MessageQueue or Web hooks.
func New(events Events) *Notifier {
......
package notify_test
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"gitlab.eclipse.org/eclipse/xfsc/tsa/policy/internal/notify"
"gitlab.eclipse.org/eclipse/xfsc/tsa/policy/internal/notify/notifyfakes"
)
func TestNotify_PolicyDataChange(t *testing.T) {
tests := []struct {
name string
events notify.Events
eventPolicyChange *notify.EventPolicyChange
errText string
}{
{
name: "error when sending event",
eventPolicyChange: &notify.EventPolicyChange{Name: "exampleName", Version: "exampleVersion", Group: "exampleGroup"},
events: &notifyfakes.FakeEvents{SendStub: func(ctx context.Context, a any) error {
return fmt.Errorf("some error")
}},
errText: "some error",
},
{
name: "sending event is successful",
eventPolicyChange: &notify.EventPolicyChange{Name: "exampleName", Version: "exampleVersion", Group: "exampleGroup"},
events: &notifyfakes.FakeEvents{SendStub: func(ctx context.Context, a any) error {
return nil
}},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
notifier := notify.New(test.events)
err := notifier.PolicyDataChange(context.Background(), test.eventPolicyChange)
if test.errText != "" {
assert.ErrorContains(t, err, test.errText)
} else {
assert.NoError(t, err)
}
})
}
}
// Code generated by counterfeiter. DO NOT EDIT.
package notifyfakes
import (
"context"
"sync"
"gitlab.eclipse.org/eclipse/xfsc/tsa/policy/internal/notify"
)
type FakeEvents struct {
SendStub func(context.Context, any) error
sendMutex sync.RWMutex
sendArgsForCall []struct {
arg1 context.Context
arg2 any
}
sendReturns struct {
result1 error
}
sendReturnsOnCall map[int]struct {
result1 error
}
invocations map[string][][]interface{}
invocationsMutex sync.RWMutex
}
func (fake *FakeEvents) Send(arg1 context.Context, arg2 any) error {
fake.sendMutex.Lock()
ret, specificReturn := fake.sendReturnsOnCall[len(fake.sendArgsForCall)]
fake.sendArgsForCall = append(fake.sendArgsForCall, struct {
arg1 context.Context
arg2 any
}{arg1, arg2})
stub := fake.SendStub
fakeReturns := fake.sendReturns
fake.recordInvocation("Send", []interface{}{arg1, arg2})
fake.sendMutex.Unlock()
if stub != nil {
return stub(arg1, arg2)
}
if specificReturn {
return ret.result1
}
return fakeReturns.result1
}
func (fake *FakeEvents) SendCallCount() int {
fake.sendMutex.RLock()
defer fake.sendMutex.RUnlock()
return len(fake.sendArgsForCall)
}
func (fake *FakeEvents) SendCalls(stub func(context.Context, any) error) {
fake.sendMutex.Lock()
defer fake.sendMutex.Unlock()
fake.SendStub = stub
}
func (fake *FakeEvents) SendArgsForCall(i int) (context.Context, any) {
fake.sendMutex.RLock()
defer fake.sendMutex.RUnlock()
argsForCall := fake.sendArgsForCall[i]
return argsForCall.arg1, argsForCall.arg2
}
func (fake *FakeEvents) SendReturns(result1 error) {
fake.sendMutex.Lock()
defer fake.sendMutex.Unlock()
fake.SendStub = nil
fake.sendReturns = struct {
result1 error
}{result1}
}
func (fake *FakeEvents) SendReturnsOnCall(i int, result1 error) {
fake.sendMutex.Lock()
defer fake.sendMutex.Unlock()
fake.SendStub = nil
if fake.sendReturnsOnCall == nil {
fake.sendReturnsOnCall = make(map[int]struct {
result1 error
})
}
fake.sendReturnsOnCall[i] = struct {
result1 error
}{result1}
}
func (fake *FakeEvents) Invocations() map[string][][]interface{} {
fake.invocationsMutex.RLock()
defer fake.invocationsMutex.RUnlock()
fake.sendMutex.RLock()
defer fake.sendMutex.RUnlock()
copiedInvocations := map[string][][]interface{}{}
for key, value := range fake.invocations {
copiedInvocations[key] = value
}
return copiedInvocations
}
func (fake *FakeEvents) recordInvocation(key string, args []interface{}) {
fake.invocationsMutex.Lock()
defer fake.invocationsMutex.Unlock()
if fake.invocations == nil {
fake.invocations = map[string][][]interface{}{}
}
if fake.invocations[key] == nil {
fake.invocations[key] = [][]interface{}{}
}
fake.invocations[key] = append(fake.invocations[key], args)
}
var _ notify.Events = new(FakeEvents)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment