forked from stoewer/go-nakadi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscriptions_integration_test.go
155 lines (125 loc) · 5.37 KB
/
subscriptions_integration_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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// +build integration
package nakadi
import (
"bytes"
"encoding/json"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestIntegrationSubscriptionAPI_Get(t *testing.T) {
eventType := &EventType{}
helperLoadTestData(t, "event-type-create.json", eventType)
subscriptions := helperCreateSubscriptions(t, eventType, &Subscription{OwningApplication: "test-app", EventTypes: []string{eventType.Name}})
defer helperDeleteSubscriptions(t, eventType, subscriptions...)
client := New(defaultNakadiURL, &ClientOptions{ConnectionTimeout: time.Second})
subAPI := NewSubscriptionAPI(client, &SubscriptionOptions{Retry: true})
t.Run("fail not found", func(t *testing.T) {
_, err := subAPI.Get("does-not-exist")
require.Error(t, err)
assert.Regexp(t, "does not exist", err)
})
t.Run("success", func(t *testing.T) {
subscription, err := subAPI.Get(subscriptions[0].ID)
require.NoError(t, err)
assert.Equal(t, subscriptions[0].EventTypes, subscription.EventTypes)
assert.Equal(t, subscriptions[0].OwningApplication, subscription.OwningApplication)
assert.Equal(t, "default", subscription.ConsumerGroup)
})
}
func TestIntegrationSubscriptionAPI_List(t *testing.T) {
eventType := &EventType{}
helperLoadTestData(t, "event-type-create.json", eventType)
subscriptions := []*Subscription{
{OwningApplication: "test-app", EventTypes: []string{eventType.Name}},
{OwningApplication: "test-app2", EventTypes: []string{eventType.Name}}}
subscriptions = helperCreateSubscriptions(t, eventType, subscriptions...)
defer helperDeleteSubscriptions(t, eventType, subscriptions...)
client := New(defaultNakadiURL, &ClientOptions{ConnectionTimeout: time.Second})
subAPI := NewSubscriptionAPI(client, nil)
fetched, err := subAPI.List()
require.NoError(t, err)
assert.Len(t, fetched, 2)
}
func TestIntegrationSubscriptionAPI_Create(t *testing.T) {
eventType := &EventType{}
helperLoadTestData(t, "event-type-create.json", eventType)
helperCreateEventTypes(t, eventType)
client := New(defaultNakadiURL, &ClientOptions{ConnectionTimeout: time.Second})
subAPI := NewSubscriptionAPI(client, nil)
t.Run("fail invalid subscription", func(t *testing.T) {
_, err := subAPI.Create(&Subscription{OwningApplication: "test-api"})
require.Error(t, err)
assert.Regexp(t, "unable to create subscription", err)
})
t.Run("success", func(t *testing.T) {
created, err := subAPI.Create(&Subscription{OwningApplication: "test-app", EventTypes: []string{eventType.Name}})
require.NoError(t, err)
assert.Equal(t, "test-app", created.OwningApplication)
assert.Equal(t, []string{eventType.Name}, created.EventTypes)
helperDeleteSubscriptions(t, eventType, created)
})
}
func TestIntegrationSubscriptionAPI_Delete(t *testing.T) {
eventType := &EventType{}
helperLoadTestData(t, "event-type-create.json", eventType)
subscriptions := helperCreateSubscriptions(t, eventType, &Subscription{OwningApplication: "test-app", EventTypes: []string{eventType.Name}})
defer helperDeleteSubscriptions(t, eventType, subscriptions...)
client := New(defaultNakadiURL, &ClientOptions{ConnectionTimeout: time.Second})
subAPI := NewSubscriptionAPI(client, nil)
t.Run("fail not found", func(t *testing.T) {
err := subAPI.Delete("does-not-exist")
require.Error(t, err)
assert.Regexp(t, "does not exist", err)
})
t.Run("success", func(t *testing.T) {
err := subAPI.Delete(subscriptions[0].ID)
require.NoError(t, err)
})
}
func TestIntegrationSubscriptionAPI_GetStats(t *testing.T) {
eventType := &EventType{}
helperLoadTestData(t, "event-type-create.json", eventType)
expected := helperLoadTestData(t, "subscription-stats-integration.json", nil)
helperCreateEventTypes(t, eventType)
client := New(defaultNakadiURL, &ClientOptions{ConnectionTimeout: time.Second})
subAPI := NewSubscriptionAPI(client, &SubscriptionOptions{Retry: true})
subscription := &Subscription{OwningApplication: "test-app-stats", EventTypes: []string{eventType.Name}}
subscription, err := subAPI.Create(subscription)
require.NoError(t, err)
defer subAPI.Delete(subscription.ID)
stats, err := subAPI.GetStats(subscription.ID)
require.NotNil(t, stats)
require.NoError(t, err)
assert.Equal(t, subscription.EventTypes[0], stats[0].EventType)
actual, _ := json.Marshal(stats)
t.Log(string(actual))
assert.JSONEq(t, string(expected), string(actual))
}
func helperCreateSubscriptions(t *testing.T, eventType *EventType, subscription ...*Subscription) []*Subscription {
helperCreateEventTypes(t, eventType)
var subscriptions []*Subscription
for _, eventType := range subscription {
serialized, err := json.Marshal(eventType)
require.NoError(t, err)
response, err := http.DefaultClient.Post(defaultNakadiURL+"/subscriptions", "application/json", bytes.NewReader(serialized))
require.NoError(t, err)
require.Equal(t, http.StatusCreated, response.StatusCode)
created := &Subscription{}
err = json.NewDecoder(response.Body).Decode(created)
require.NoError(t, err)
subscriptions = append(subscriptions, created)
}
return subscriptions
}
func helperDeleteSubscriptions(t *testing.T, eventType *EventType, subscriptions ...*Subscription) {
for _, sub := range subscriptions {
request, err := http.NewRequest("DELETE", defaultNakadiURL+"/subscriptions/"+sub.ID, nil)
require.NoError(t, err)
_, err = http.DefaultClient.Do(request)
require.NoError(t, err)
}
helperDeleteEventTypes(t, eventType)
}