Skip to content

Commit

Permalink
Added proper coverage for event builders
Browse files Browse the repository at this point in the history
  • Loading branch information
jbeemster committed Jun 24, 2016
1 parent 7b734b1 commit cf02bcd
Show file tree
Hide file tree
Showing 28 changed files with 1,046 additions and 517 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2015 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 android.test.AndroidTestCase;

public class DevicePlatformsTest extends AndroidTestCase {

public void testPlatforms() {
assertEquals("web", DevicePlatforms.Web.getValue());
assertEquals("mob", DevicePlatforms.Mobile.getValue());
assertEquals("pc", DevicePlatforms.Desktop.getValue());
assertEquals("srv", DevicePlatforms.ServerSideApp.getValue());
assertEquals("app", DevicePlatforms.General.getValue());
assertEquals("tv", DevicePlatforms.ConnectedTV.getValue());
assertEquals("cnsl", DevicePlatforms.GameConsole.getValue());
assertEquals("iot", DevicePlatforms.InternetOfThings.getValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import android.test.AndroidTestCase;

import com.snowplowanalytics.snowplow.tracker.constants.Parameters;
import com.snowplowanalytics.snowplow.tracker.utils.LogLevel;
import com.snowplowanalytics.snowplow.tracker.utils.Logger;

Expand All @@ -34,6 +35,12 @@ private Subject getSubject() {

// Tests

public void testSubjectWithNullContext() {
Subject subject = new Subject.SubjectBuilder().build();
Map<String, String> map = subject.getSubject();
assertFalse(map.containsKey(Parameters.RESOLUTION));
}

public void testGetSubjectStandardPairs() throws Exception {
Subject subject = getSubject();
Map<String, String> standardPairs = subject.getSubject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ public class TrackerTest extends AndroidTestCase {
// Helper Methods

private Tracker getTracker() {

// Make an emitter
Emitter emitter = new Emitter
.EmitterBuilder("testUrl", getContext())
.tick(0)
Expand All @@ -40,21 +38,36 @@ private Tracker getTracker() {
.context(getContext())
.build();

// Make and return the Tracker object
Tracker.close();
return new Tracker
.TrackerBuilder(emitter, "myNamespace", "myAppId", getContext())
Tracker.init(new Tracker.TrackerBuilder(emitter, "myNamespace", "myAppId", getContext())
.subject(subject)
.platform(DevicePlatforms.InternetOfThings)
.base64(false)
.level(LogLevel.DEBUG)
.threadCount(20)
.level(LogLevel.VERBOSE)
.threadCount(1)
.sessionCheckInterval(15)
.backgroundTimeout(4000)
.foregroundTimeout(20000)
.timeUnit(TimeUnit.MILLISECONDS)
.sessionContext(true)
.build();
.build()
);
return Tracker.instance();
}

// Static Tests

public void testTrackerNotInit() {
Tracker.close();

boolean exception = false;
try {
Tracker.instance();
} catch (Exception e) {
assertEquals("FATAL: Tracker must be initialized first!", e.getMessage());
exception = true;
}
assertTrue(exception);
}

// Tests
Expand Down Expand Up @@ -96,7 +109,7 @@ public void testVersionSet() {

public void testLogLevelSet() {
Tracker tracker = getTracker();
assertEquals(LogLevel.DEBUG, tracker.getLogLevel());
assertEquals(LogLevel.VERBOSE, tracker.getLogLevel());
}

public void testSubjectUpdate() {
Expand Down Expand Up @@ -128,7 +141,7 @@ public void testDataCollectionSwitch() {

public void testThreadCountSet() {
Tracker tracker = getTracker();
assertEquals(20, tracker.getThreadCount());
assertEquals(2, tracker.getThreadCount());
}

public void testTrackerSessionSet() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright (c) 2015 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.events;

import android.test.AndroidTestCase;

import com.snowplowanalytics.snowplow.tracker.constants.Parameters;

import java.util.Map;

public class EcommerceItemTest extends AndroidTestCase {

public void testExpectedForm() {
EcommerceTransactionItem ecommerceTransactionItem = EcommerceTransactionItem.builder()
.itemId("some item id")
.sku("some sku")
.price(123.456)
.quantity(1)
.build();

Map data = ecommerceTransactionItem.getPayload().getMap();

assertNotNull(data);
assertEquals("ti", data.get(Parameters.EVENT));
assertEquals("some item id", data.get(Parameters.TI_ITEM_ID));
assertEquals("some sku", data.get(Parameters.TI_ITEM_SKU));
assertEquals("123.456", data.get(Parameters.TI_ITEM_PRICE));
assertEquals("1", data.get(Parameters.TI_ITEM_QUANTITY));
assertFalse(data.containsKey(Parameters.TI_ITEM_NAME));
assertFalse(data.containsKey(Parameters.TI_ITEM_CATEGORY));
assertFalse(data.containsKey(Parameters.TI_ITEM_CURRENCY));

ecommerceTransactionItem = EcommerceTransactionItem.builder()
.itemId("some item id")
.sku("some sku")
.price(123.456)
.quantity(1)
.name("some name")
.category("some category")
.currency("EUR")
.build();

data = ecommerceTransactionItem.getPayload().getMap();

assertNotNull(data);
assertEquals("ti", data.get(Parameters.EVENT));
assertEquals("some item id", data.get(Parameters.TI_ITEM_ID));
assertEquals("some sku", data.get(Parameters.TI_ITEM_SKU));
assertEquals("123.456", data.get(Parameters.TI_ITEM_PRICE));
assertEquals("1", data.get(Parameters.TI_ITEM_QUANTITY));
assertEquals("some name", data.get(Parameters.TI_ITEM_NAME));
assertEquals("some category", data.get(Parameters.TI_ITEM_CATEGORY));
assertEquals("EUR", data.get(Parameters.TI_ITEM_CURRENCY));
}

public void testBuilderFailures() {
boolean exception = false;
try {
EcommerceTransactionItem.builder().build();
} catch (Exception e) {
assertEquals(null, e.getMessage());
exception = true;
}
assertTrue(exception);

exception = false;
try {
EcommerceTransactionItem.builder().itemId("some item id").build();
} catch (Exception e) {
assertEquals(null, e.getMessage());
exception = true;
}
assertTrue(exception);

exception = false;
try {
EcommerceTransactionItem.builder().itemId("some item id").sku("some sku").build();
} catch (Exception e) {
assertEquals(null, e.getMessage());
exception = true;
}
assertTrue(exception);

exception = false;
try {
EcommerceTransactionItem.builder().itemId("some item id").sku("some sku").price(123.456)
.build();
} catch (Exception e) {
assertEquals(null, e.getMessage());
exception = true;
}
assertTrue(exception);

exception = false;
try {
EcommerceTransactionItem.builder().itemId("").sku("some sku").price(123.456)
.quantity(1).build();
} catch (Exception e) {
assertEquals("itemId cannot be empty", e.getMessage());
exception = true;
}
assertTrue(exception);

exception = false;
try {
EcommerceTransactionItem.builder().itemId("item id").sku("").price(123.456)
.quantity(1).build();
} catch (Exception e) {
assertEquals("sku cannot be empty", e.getMessage());
exception = true;
}
assertTrue(exception);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright (c) 2015 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.events;

import android.test.AndroidTestCase;

import com.snowplowanalytics.snowplow.tracker.constants.Parameters;

import java.util.ArrayList;
import java.util.Map;

public class EcommerceTest extends AndroidTestCase {

public void testExpectedForm() {
EcommerceTransaction ecommerceTransaction = EcommerceTransaction.builder()
.orderId("some order id")
.totalValue(123.456)
.items(new ArrayList<EcommerceTransactionItem>())
.build();

Map data = ecommerceTransaction.getPayload().getMap();

assertNotNull(data);
assertEquals("tr", data.get(Parameters.EVENT));
assertEquals("some order id", data.get(Parameters.TR_ID));
assertEquals("123.456", data.get(Parameters.TR_TOTAL));
assertFalse(data.containsKey(Parameters.TR_AFFILIATION));
assertFalse(data.containsKey(Parameters.TR_TAX));
assertFalse(data.containsKey(Parameters.TR_SHIPPING));
assertFalse(data.containsKey(Parameters.TR_CITY));
assertFalse(data.containsKey(Parameters.TR_STATE));
assertFalse(data.containsKey(Parameters.TR_COUNTRY));
assertFalse(data.containsKey(Parameters.TR_CURRENCY));

EcommerceTransactionItem ecommerceTransactionItem = EcommerceTransactionItem.builder()
.itemId("some item id")
.sku("some sku")
.price(123.456)
.quantity(1)
.build();

ecommerceTransaction = EcommerceTransaction.builder()
.orderId("some order id")
.totalValue(123.456)
.affiliation("some affiliate")
.taxValue(50.6)
.shipping(10.0)
.city("Dijon")
.state("Bourgogne")
.country("France")
.currency("EUR")
.items(ecommerceTransactionItem)
.build();

data = ecommerceTransaction.getPayload().getMap();

assertNotNull(data);
assertEquals("tr", data.get(Parameters.EVENT));
assertEquals("some order id", data.get(Parameters.TR_ID));
assertEquals("123.456", data.get(Parameters.TR_TOTAL));
assertEquals("some affiliate", data.get(Parameters.TR_AFFILIATION));
assertEquals("50.6", data.get(Parameters.TR_TAX));
assertEquals("10.0", data.get(Parameters.TR_SHIPPING));
assertEquals("Dijon", data.get(Parameters.TR_CITY));
assertEquals("Bourgogne", data.get(Parameters.TR_STATE));
assertEquals("France", data.get(Parameters.TR_COUNTRY));
assertEquals("EUR", data.get(Parameters.TR_CURRENCY));
}

public void testBuilderFailures() {
boolean exception = false;
try {
EcommerceTransaction.builder().build();
} catch (Exception e) {
assertEquals(null, e.getMessage());
exception = true;
}
assertTrue(exception);

exception = false;
try {
EcommerceTransaction.builder().orderId("some order id").build();
} catch (Exception e) {
assertEquals(null, e.getMessage());
exception = true;
}
assertTrue(exception);

exception = false;
try {
EcommerceTransaction.builder().orderId("some order id").totalValue(123.456).build();
} catch (Exception e) {
assertEquals(null, e.getMessage());
exception = true;
}
assertTrue(exception);

exception = false;
try {
EcommerceTransaction.builder().orderId("").totalValue(123.456)
.items(new ArrayList<EcommerceTransactionItem>()).build();
} catch (Exception e) {
assertEquals("orderId cannot be empty", e.getMessage());
exception = true;
}
assertTrue(exception);
}
}

0 comments on commit cf02bcd

Please sign in to comment.