-
Notifications
You must be signed in to change notification settings - Fork 0
/
midt_test.go
138 lines (112 loc) · 3.63 KB
/
midt_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package midt_test
import (
"context"
"testing"
"github.com/ssengalanto/midt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNew(t *testing.T) {
t.Run("it should create a new midt instance", func(t *testing.T) {
m := midt.New()
assert.NotNil(t, m, "new midt instance shouldn't be nil")
})
}
func TestMidt_RegisterRequestHandler(t *testing.T) {
t.Run("it should register all request handlers successfully", func(t *testing.T) {
m := midt.New()
hdlr := &CommandHandler{}
err := m.RegisterRequestHandler(hdlr)
require.NoError(t, err)
})
t.Run("it should return an error when trying to register an already existing request handler", func(t *testing.T) {
m := midt.New()
hdlr1 := &CommandHandler{}
err := m.RegisterRequestHandler(hdlr1)
require.NoError(t, err)
hdlr2 := &CommandHandler{}
err = m.RegisterRequestHandler(hdlr2)
require.Error(t, err)
})
}
func TestMidt_RegisterNotificationHandler(t *testing.T) {
t.Run("it should register the notification handler successfully", func(t *testing.T) {
m := midt.New()
hdlr := &NotificationHandler{}
err := m.RegisterNotificationHandler(hdlr)
require.NoError(t, err)
})
t.Run("it should return an error when trying to register an already existing notification handler", func(t *testing.T) {
m := midt.New()
hdlr1 := &NotificationHandler{}
err := m.RegisterNotificationHandler(hdlr1)
require.NoError(t, err)
hdlr2 := &NotificationHandler{}
err = m.RegisterNotificationHandler(hdlr2)
require.Error(t, err)
})
}
func TestMidt_RegisterPipelineBehaviour(t *testing.T) {
t.Run("it should register the pipeline behaviour successfully", func(t *testing.T) {
m := midt.New()
pb := &PipelineBehaviourHandler{}
err := m.RegisterPipelineBehaviour(pb)
require.NoError(t, err)
})
t.Run("it should return an error when trying to register an already existing pipeline behaviour",
func(t *testing.T) {
m := midt.New()
pb1 := &PipelineBehaviourHandler{}
err := m.RegisterPipelineBehaviour(pb1)
require.NoError(t, err)
pb2 := &PipelineBehaviourHandler{}
err = m.RegisterPipelineBehaviour(pb2)
require.Error(t, err)
})
}
func TestMidt_Send(t *testing.T) {
t.Run("it should execute the request handler",
func(t *testing.T) {
m := midt.New()
hdlr := &CommandHandler{}
err := m.RegisterRequestHandler(hdlr)
require.NoError(t, err)
_, err = m.Send(context.Background(), &CommandRequest{})
require.NoError(t, err)
})
t.Run("it should execute the pipeline behaviours",
func(t *testing.T) {
m := midt.New()
pb := &PipelineBehaviourHandler{}
err := m.RegisterPipelineBehaviour(pb)
require.NoError(t, err)
hdlr := &CommandHandler{}
err = m.RegisterRequestHandler(hdlr)
require.NoError(t, err)
_, err = m.Send(context.Background(), &CommandRequest{})
require.NoError(t, err)
})
t.Run("it should return an error if there are no registered request handlers in the registry",
func(t *testing.T) {
m := midt.New()
_, err := m.Send(context.Background(), &CommandRequest{})
require.Error(t, err)
})
}
func TestMidt_Publish(t *testing.T) {
t.Run("it should execute the notification handler",
func(t *testing.T) {
m := midt.New()
hdlr := &NotificationHandler{}
err := m.RegisterNotificationHandler(hdlr)
require.NoError(t, err)
err = m.Publish(context.Background(), &NotificationRequest{})
require.NoError(t, err)
})
t.Run("it should return an error if there are no registered notification handlers in the registry",
func(t *testing.T) {
m := midt.New()
err := m.Publish(context.Background(), &NotificationRequest{})
require.Error(t, err)
})
}