-
Notifications
You must be signed in to change notification settings - Fork 84
/
notifications_count.go
48 lines (41 loc) · 1.85 KB
/
notifications_count.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
package asserters
import (
"context"
"fmt"
"net/http"
"testing"
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"
)
type NotificationsCountAsserter struct {
expectedNotificationsCount int
op string
targetObjectID string
externalServicesMockMtlsSecuredURL string
client *http.Client
}
func NewNotificationsCountAsserter(expectedNotificationsCount int, op string, targetObjectID, externalServicesMockMtlsSecuredURL string, client *http.Client) *NotificationsCountAsserter {
return &NotificationsCountAsserter{
expectedNotificationsCount: expectedNotificationsCount,
op: op,
targetObjectID: targetObjectID,
externalServicesMockMtlsSecuredURL: externalServicesMockMtlsSecuredURL,
client: client,
}
}
func (nca *NotificationsCountAsserter) AssertExpectations(t *testing.T, _ context.Context) {
body := getNotificationsFromExternalSvcMock(t, nca.client, nca.externalServicesMockMtlsSecuredURL)
notificationsForTarget := gjson.GetBytes(body, nca.targetObjectID)
notificationsAboutSource := notificationsForTarget.Array()
nca.assertAtLeastNNotificationsOfTypeReceived(t, notificationsAboutSource)
t.Logf("Successfully asserted assignment notification count for: %s", nca.targetObjectID)
}
func (nca *NotificationsCountAsserter) assertAtLeastNNotificationsOfTypeReceived(t *testing.T, notifications []gjson.Result) {
notificationsForOperationCount := 0
for _, notification := range notifications {
if notification.Get("Operation").String() == nca.op {
notificationsForOperationCount++
}
}
require.LessOrEqual(t, nca.expectedNotificationsCount, notificationsForOperationCount, fmt.Sprintf("Mismatched assignment notification count for tenant: %s", nca.targetObjectID))
}