1
+ package com .clevertap .apns ;
2
+
3
+ import com .clevertap .apns .enums .InterruptionLevel ;
4
+ import org .junit .jupiter .api .Test ;
5
+ import org .junit .jupiter .params .ParameterizedTest ;
6
+ import org .junit .jupiter .params .provider .Arguments ;
7
+ import org .junit .jupiter .params .provider .MethodSource ;
8
+
9
+ import java .util .stream .Stream ;
10
+
11
+ import static org .junit .jupiter .api .Assertions .*;
12
+
13
+ class NotificationTest {
14
+
15
+ @ Test
16
+ void testNotificationBuilder () {
17
+ Notification .Builder builder = new Notification .Builder ("token" );
18
+ builder .relevanceScore (0.1 );
19
+ builder .interruptionLevel (InterruptionLevel .PASSIVE );
20
+ builder .mutableContent (true );
21
+ builder .alertBody ("body" );
22
+ builder .alertTitle ("title" );
23
+ builder .category ("cat1" );
24
+ builder .priority (Notification .Priority .IMMEDIATE );
25
+ Notification notification = builder .build ();
26
+ assertEquals ("{\" aps\" :{\" interruption-level\" :\" passive\" ,\" relevance-score\" :0.1,\" alert\" :{\" body\" :\" body\" ,\" title\" :\" title\" },\" category\" :\" cat1\" ,\" mutable-content\" :1}}" , notification .getPayload ());
27
+ }
28
+
29
+ @ Test
30
+ void testNotificationBuilderWithResetOptions () {
31
+ Notification .Builder builder = new Notification .Builder ("token" );
32
+ builder .relevanceScore (0.1 );
33
+ builder .interruptionLevel (InterruptionLevel .PASSIVE );
34
+ builder .mutableContent (true );
35
+ builder .alertBody ("body" );
36
+ builder .alertTitle ("title" );
37
+ builder .category ("cat1" );
38
+ builder .priority (Notification .Priority .IMMEDIATE );
39
+ builder .resetRelevanceScore ();
40
+ builder .resetInterruptionLevel ();
41
+ Notification notification = builder .build ();
42
+ assertEquals ("{\" aps\" :{\" alert\" :{\" body\" :\" body\" ,\" title\" :\" title\" },\" category\" :\" cat1\" ,\" mutable-content\" :1}}" , notification .getPayload ());
43
+ }
44
+
45
+
46
+ @ ParameterizedTest
47
+ @ MethodSource ("providerForRelevanceScore" )
48
+ void testRelevanceScore (String expected , double relevanceScore ) {
49
+ Notification .Builder builder = new Notification .Builder ("token" );
50
+ builder .relevanceScore (relevanceScore );
51
+ assertEquals (expected , builder .build ().getPayload ());
52
+ }
53
+
54
+ static Stream <Arguments > providerForRelevanceScore () {
55
+ Stream .Builder <Arguments > builder = Stream .builder ();
56
+ builder .add (Arguments .of ("{\" aps\" :{\" relevance-score\" :0.0,\" alert\" :{}}}" , 0.0 ));
57
+ builder .add (Arguments .of ("{\" aps\" :{\" relevance-score\" :1.0,\" alert\" :{}}}" , 1.0 ));
58
+ builder .add (Arguments .of ("{\" aps\" :{\" relevance-score\" :0.75,\" alert\" :{}}}" , 0.75 ));
59
+ builder .add (Arguments .of ("{\" aps\" :{\" alert\" :{}}}" , 5.0 ));
60
+ builder .add (Arguments .of ("{\" aps\" :{\" alert\" :{}}}" , -1.0 ));
61
+ return builder .build ();
62
+ }
63
+
64
+ @ ParameterizedTest
65
+ @ MethodSource ("providerInterruptionLevel" )
66
+ void testInterruptionLevel (String expected , InterruptionLevel interruptionLevel ) {
67
+ Notification .Builder builder = new Notification .Builder ("token" );
68
+ builder .interruptionLevel (interruptionLevel );
69
+ assertEquals (expected , builder .build ().getPayload ());
70
+ }
71
+
72
+ static Stream <Arguments > providerInterruptionLevel () {
73
+ Stream .Builder <Arguments > builder = Stream .builder ();
74
+
75
+ String payloadFormat = "{\" aps\" :{\" interruption-level\" :\" %s\" ,\" alert\" :{}}}" ;
76
+ for (InterruptionLevel interruptionLevel : InterruptionLevel .values ()) {
77
+ builder .add (Arguments .of (String .format (payloadFormat , interruptionLevel .getValue ()), interruptionLevel ));
78
+ }
79
+ builder .add (Arguments .of ("{\" aps\" :{\" alert\" :{}}}" , null ));
80
+ return builder .build ();
81
+ }
82
+
83
+ @ Test
84
+ void resetRelevanceScore () {
85
+ Notification .Builder builder = new Notification .Builder ("token" );
86
+ builder .relevanceScore (0.5 );
87
+ Notification notification = builder .build ();
88
+ assertEquals ("{\" aps\" :{\" relevance-score\" :0.5,\" alert\" :{}}}" , notification .getPayload ());
89
+
90
+ builder .resetRelevanceScore ();
91
+ Notification notification2 = builder .build ();
92
+ assertEquals ("{\" aps\" :{\" alert\" :{}}}" , notification2 .getPayload ());
93
+ }
94
+
95
+ @ Test
96
+ void resetInterruptionLevel () {
97
+ Notification .Builder builder = new Notification .Builder ("token" );
98
+ builder .interruptionLevel (InterruptionLevel .ACTIVE );
99
+ Notification notification = builder .build ();
100
+ assertEquals ("{\" aps\" :{\" interruption-level\" :\" active\" ,\" alert\" :{}}}" , notification .getPayload ());
101
+
102
+ builder .resetInterruptionLevel ();
103
+ Notification notification2 = builder .build ();
104
+ assertEquals ("{\" aps\" :{\" alert\" :{}}}" , notification2 .getPayload ());
105
+ }
106
+
107
+ }
0 commit comments