forked from snowplow/snowplow-java-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnowplowTest.java
More file actions
144 lines (114 loc) · 5.74 KB
/
SnowplowTest.java
File metadata and controls
144 lines (114 loc) · 5.74 KB
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
/*
* Copyright (c) 2014-2022 Snowplow Analytics Ltd. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
package com.snowplowanalytics.snowplow.tracker;
import com.snowplowanalytics.snowplow.tracker.configuration.NetworkConfiguration;
import com.snowplowanalytics.snowplow.tracker.configuration.TrackerConfiguration;
import com.snowplowanalytics.snowplow.tracker.emitter.BatchEmitter;
import org.junit.After;
import org.junit.Test;
import static org.junit.Assert.*;
public class SnowplowTest {
@After
public void cleanUp(){
Snowplow.reset();
}
@Test
public void createsAndRetrievesATracker() {
assertTrue(Snowplow.getInstancedTrackerNamespaces().isEmpty());
Tracker tracker = Snowplow.createTracker("namespace", "appId", "http://endpoint");
Tracker retrievedTracker = Snowplow.getTracker("namespace");
assertFalse(Snowplow.getInstancedTrackerNamespaces().isEmpty());
assertEquals(tracker, retrievedTracker);
assertEquals("namespace", tracker.getNamespace());
assertEquals("appId", tracker.getAppId());
assertTrue(tracker.getBase64Encoded());
}
@Test
public void preventsDuplicateNamespaces() {
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
Snowplow.createTracker("namespace", "appId", "http://endpoint");
Snowplow.createTracker("namespace", "appId2", "http://collector");
});
assertEquals("Tracker with this namespace already exists.", exception.getMessage());
}
@Test
public void deletesStoredTracker() {
Snowplow.createTracker("namespace", "appId", "http://endpoint");
boolean result = Snowplow.removeTracker("namespace");
assertTrue(result);
Tracker tracker = Snowplow.createTracker("namespace2", "appId", "http://endpoint");
boolean result2 = Snowplow.removeTracker(tracker);
assertTrue(result2);
}
@Test
public void doesNotDeleteUnregisteredTracker() {
BatchEmitter emitter = new BatchEmitter(new NetworkConfiguration("http://collector"));
Tracker tracker = new Tracker(new TrackerConfiguration("namespace", "appId"), emitter);
boolean result = Snowplow.removeTracker(tracker);
assertFalse(result);
boolean result2 = Snowplow.removeTracker("not registered");
assertFalse(result2);
}
@Test
public void setsDefaultTrackerFromObject() {
assertNull(Snowplow.getDefaultTracker());
Tracker tracker = Snowplow.createTracker("namespace", "appId", "http://endpoint");
assertEquals(tracker, Snowplow.getDefaultTracker());
Tracker tracker2 = Snowplow.createTracker("namespace2", "appId", "http://endpoint");
// The first tracker is still the default
assertEquals(tracker, Snowplow.getDefaultTracker());
Snowplow.setDefaultTracker(tracker2);
assertEquals(tracker2, Snowplow.getDefaultTracker());
assertEquals(2, Snowplow.getInstancedTrackerNamespaces().size());
}
@Test
public void setsDefaultTrackerFromNamespace() {
assertNull(Snowplow.getDefaultTracker());
Snowplow.createTracker("namespace", "appId", "http://endpoint");
Tracker tracker2 = Snowplow.createTracker("namespace2", "appId", "http://endpoint");
boolean result = Snowplow.setDefaultTracker("namespace2");
assertTrue(result);
assertEquals(tracker2, Snowplow.getDefaultTracker());
}
@Test
public void registersATrackerMadeWithoutSnowplowClass() {
BatchEmitter emitter = new BatchEmitter(new NetworkConfiguration("http://collector"));
Tracker tracker = new Tracker(new TrackerConfiguration("namespace", "appId"), emitter);
Snowplow.registerTracker(tracker);
assertEquals(tracker, Snowplow.getDefaultTracker());
assertEquals(1, Snowplow.getInstancedTrackerNamespaces().size());
}
@Test
public void settingNewDefaultTrackerRegistersIt() {
BatchEmitter emitter = new BatchEmitter(new NetworkConfiguration("http://collector"));
Tracker tracker = new Tracker(new TrackerConfiguration("new_tracker", "appId"), emitter);
Snowplow.setDefaultTracker(tracker);
assertEquals(1, Snowplow.getInstancedTrackerNamespaces().size());
assertEquals("new_tracker", Snowplow.getDefaultTracker().getNamespace());
}
@Test
public void createsTrackerFromConfigs() {
TrackerConfiguration trackerConfig = new TrackerConfiguration("namespace", "appId")
.base64Encoded(false)
.platform(DevicePlatform.Desktop);
NetworkConfiguration networkConfig = new NetworkConfiguration("http://collector-endpoint");
Tracker tracker = Snowplow.createTracker(trackerConfig, networkConfig);
Tracker retrievedTracker = Snowplow.getTracker("namespace");
assertFalse(Snowplow.getInstancedTrackerNamespaces().isEmpty());
assertEquals(tracker, retrievedTracker);
assertEquals("namespace", tracker.getNamespace());
assertEquals("appId", tracker.getAppId());
assertEquals(DevicePlatform.Desktop, tracker.getPlatform());
assertFalse(tracker.getBase64Encoded());
}
}