1717package com .google .cloud .tools .eclipse .usagetracker ;
1818
1919import static org .junit .Assert .assertEquals ;
20+ import static org .junit .Assert .assertFalse ;
2021import static org .junit .Assert .assertTrue ;
2122import static org .mockito .Matchers .any ;
2223import static org .mockito .Matchers .anyBoolean ;
24+ import static org .mockito .Matchers .anyString ;
2325import static org .mockito .Matchers .eq ;
2426import static org .mockito .Mockito .never ;
2527import static org .mockito .Mockito .times ;
2830
2931import com .google .cloud .tools .eclipse .preferences .AnalyticsPreferences ;
3032import com .google .cloud .tools .eclipse .usagetracker .AnalyticsPingManager .PingEvent ;
31- import java .util .Collections ;
32- import java .util .HashMap ;
3333import java .util .Map ;
3434import java .util .concurrent .ConcurrentLinkedQueue ;
3535import org .eclipse .core .runtime .preferences .IEclipsePreferences ;
3636import org .eclipse .swt .widgets .Display ;
37+ import org .junit .Before ;
3738import org .junit .Test ;
3839import org .junit .runner .RunWith ;
3940import org .mockito .Mock ;
4344@ RunWith (MockitoJUnitRunner .class )
4445public class AnalyticsPingManagerTest {
4546
46- private static final String UUID = "bee5d838-c3f8-4940-a944-b56973597e74" ;
47-
48- private static final String EVENT_TYPE = "some-event-type" ;
49- private static final String EVENT_NAME = "some-event-name" ;
50-
51- private static final String VIRTUAL_HOST = "virtual.host" ;
52- private static final String VIRTUAL_DOCUMENT_PAGE =
53- "/virtual/" + EVENT_TYPE + "/" + EVENT_NAME ;
54-
55- private static final String METADATA_KEY = "some-custom-key" ;
56- private static final String METADATA_VALUE = "some-custom-value" ;
57-
58- // TODO(chanseok): add a test to that makes an actual HTTP request (locally) and checks the
59- // incoming request. (https://github.com/GoogleCloudPlatform/google-cloud-eclipse/issues/1437)
60- private static final Map <String , String > RANDOM_PARAMETERS = Collections .unmodifiableMap (
61- new HashMap <String , String >() {
62- {
63- put ("v" , "1" );
64- put ("tid" , "UA-12345678-1" );
65- put ("ni" , "0" );
66- put ("t" , "pageview" );
67- put ("cd21" , "1" );
68- put ("cd16" , "0" );
69- put ("cd17" , "0" );
70- put ("cid" , UUID );
71- put ("cd19" , EVENT_TYPE );
72- put ("cd20" , EVENT_NAME );
73- put ("dh" , VIRTUAL_HOST );
74- put ("dp" , VIRTUAL_DOCUMENT_PAGE );
75- put ("dt" , METADATA_KEY + "=" + METADATA_VALUE );
76- }
77- });
78-
7947 @ Mock private IEclipsePreferences preferences ;
8048 @ Mock private Display display ;
8149 @ Mock private ConcurrentLinkedQueue <PingEvent > pingEventQueue ;
8250
51+ private AnalyticsPingManager pingManager ;
52+
53+ @ Before
54+ public void setUp () {
55+ // Pretend ping event queue is always empty to prevent making actual HTTP requests.
56+ when (pingEventQueue .isEmpty ()).thenReturn (true );
57+
58+ pingManager = new AnalyticsPingManager ("https://non-null-url-to-enable-mananger" ,
59+ "clientId" , preferences , display , pingEventQueue );
60+ }
61+
8362 @ Test
8463 public void testEventTypeEventNameConvention () {
8564 PingEvent event = new PingEvent ("some.event-name" , null , null , null );
@@ -101,6 +80,13 @@ public void testMetadataConvention() {
10180 assertEquals ("times-happened=1234" , parameters .get ("dt" ));
10281 }
10382
83+ @ Test
84+ public void testClientId () {
85+ PingEvent event = new PingEvent ("some.event-name" , null , null , null );
86+ Map <String , String > parameters = AnalyticsPingManager .buildParametersMap ("clientId" , event );
87+ assertEquals ("clientId" , parameters .get ("cid" ));
88+ }
89+
10490 @ Test
10591 public void testOptInDialogShown_optInNotRegisteredAndNotYetOptedIn () {
10692 mockOptIn (false );
@@ -141,9 +127,6 @@ private void mockOptInRegistered(boolean registered) {
141127 }
142128
143129 private void verifyOptInDialogOpen (VerificationMode verificationMode ) {
144- AnalyticsPingManager pingManager =
145- new AnalyticsPingManager (preferences , display , pingEventQueue , true );
146- pingManager .unitTestMode = true ;
147130 pingManager .showOptInDialogIfNeeded (null );
148131 verify (display , verificationMode ).syncExec (any (Runnable .class ));
149132 }
@@ -177,11 +160,60 @@ public void testSendPingScheduled_optInRegisteredAndAlreadyOptedIn() {
177160 }
178161
179162 private void verifyPingQueued (VerificationMode verificationMode ) {
180- when (pingEventQueue .isEmpty ()).thenReturn (true );
181- AnalyticsPingManager pingManager =
182- new AnalyticsPingManager (preferences , display , pingEventQueue , true );
183- pingManager .unitTestMode = true ;
184163 pingManager .sendPing ("eventName" , "metadataKey" , "metadataValue" );
185164 verify (pingEventQueue , verificationMode ).add (any (PingEvent .class ));
186165 }
166+
167+ @ Test
168+ public void testGetAnonymizedClientId_generateNewId () {
169+ when (preferences .get (eq (AnalyticsPreferences .ANALYTICS_CLIENT_ID ), anyString ()))
170+ .thenReturn (null ); // Simulate that client ID has never been generated.
171+ String clientId = AnalyticsPingManager .getAnonymizedClientId (preferences );
172+ assertFalse (clientId .isEmpty ());
173+ verify (preferences ).put (AnalyticsPreferences .ANALYTICS_CLIENT_ID , clientId );
174+ }
175+
176+ @ Test
177+ public void testGetAnonymizedClientId_useSavedId () {
178+ when (preferences .get (eq (AnalyticsPreferences .ANALYTICS_CLIENT_ID ), anyString ()))
179+ .thenReturn ("some-unique-client-id" );
180+ String clientId = AnalyticsPingManager .getAnonymizedClientId (preferences );
181+ assertEquals ("some-unique-client-id" , clientId );
182+ verify (preferences , never ()).put (AnalyticsPreferences .ANALYTICS_CLIENT_ID , clientId );
183+ }
184+
185+ @ Test
186+ public void testSendPingArguments_validArgumentsDoNotThrowException () {
187+ pingManager .sendPing ("eventName" , "metadataKey" , "metadataValue" );
188+ }
189+
190+ @ Test (expected = IllegalArgumentException .class )
191+ public void testSendPingArguments_nullEventName () {
192+ pingManager .sendPing (null , "metadataKey" , "metadataValue" );
193+ }
194+
195+ @ Test (expected = IllegalArgumentException .class )
196+ public void testSendPingArguments_emptyEventName () {
197+ pingManager .sendPing ("" , "metadataKey" , "metadataValue" );
198+ }
199+
200+ @ Test (expected = IllegalArgumentException .class )
201+ public void testSendPingArguments_nullMetadataKeyWithNonNullValue () {
202+ pingManager .sendPing ("eventName" , null , "metadataValue" );
203+ }
204+
205+ @ Test (expected = IllegalArgumentException .class )
206+ public void testSendPingArguments_emptyMetadataKeyWithNonNullValue () {
207+ pingManager .sendPing ("eventName" , "" , "metadataValue" );
208+ }
209+
210+ @ Test
211+ public void testSendPingArguments_nullMetadataValueDoNotThrowException () {
212+ pingManager .sendPing ("eventName" , "metadataKey" , null );
213+ }
214+
215+ @ Test (expected = IllegalArgumentException .class )
216+ public void testSendPingArguments_emptyMetadataValue () {
217+ pingManager .sendPing ("eventName" , "metadataKey" , "" );
218+ }
187219}
0 commit comments