Skip to content

Commit ab47817

Browse files
authored
Clean up sfcode setting. (#5)
* Guard against null pointer exceptions if `sfcode` setting is not present.
1 parent e2247a6 commit ab47817

3 files changed

Lines changed: 43 additions & 6 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
GROUP=com.segment.analytics.android.integrations
22

3-
VERSION=0.0.2-SNAPSHOT
3+
VERSION=0.0.3-SNAPSHOT
44

55
POM_ARTIFACT_ID=nielsen-dtvr
66
POM_PACKAGING=jar

src/main/java/com/segment/analytics/android/integrations/nielsendtvr/NielsenDTVRIntegrationFactory.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,14 @@ AppSdk reuseAppSdk(String appId, Map<String, List<AppSdk>> appSdkInstances) {
106106
*/
107107
JSONObject parseAppSdkConfig(ValueMap settings) throws JSONException {
108108
JSONObject appSdkConfig = new JSONObject();
109-
String sfcode = settings.getString(SETTING_SF_CODE_KEY);
110-
if (sfcode.isEmpty()) {
111-
sfcode = "us";
109+
110+
String sfcode = "us";
111+
if (settings.containsKey(SETTING_SF_CODE_KEY)
112+
&& settings.getString(SETTING_SF_CODE_KEY) != null
113+
&& !settings.getString(SETTING_SF_CODE_KEY).isEmpty()) {
114+
sfcode = settings.getString(SETTING_SF_CODE_KEY);
112115
}
116+
113117
appSdkConfig.put("appid", settings.getString(SETTING_APP_ID_KEY)).put("sfcode", sfcode);
114118

115119
if (settings.getBoolean(SETTING_DEBUG_KEY, false)) {

src/test/java/com/segment/analytics/android/integrations/nielsendtvr/NielsenDTVRIntegrationFactoryTest.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class NielsenDTVRIntegrationFactoryTest {
5252
private NielsenDTVRIntegrationFactory factory;
5353

5454
private final String appid = "testappid";
55-
private final String sfcode = "";
55+
private final String sfcode = "us";
5656

5757
@Before
5858
public void init() {
@@ -79,7 +79,7 @@ public void reuseAppSdk() {
7979
public void parseAppSdkConfig() throws JSONException {
8080
JSONObject expectedConfig = new JSONObject()
8181
.put("appid", appid)
82-
.put("sfcode", "us");
82+
.put("sfcode", sfcode);
8383

8484
JSONAssert.assertEquals(expectedConfig, factory.parseAppSdkConfig(settings), JSONCompareMode.STRICT);
8585

@@ -115,6 +115,39 @@ public void fetchAppSdk() throws JSONException {
115115
verify(factoryMock, never()).saveAppSdk(anyString(), (AppSdk) any(), ArgumentMatchers.<String, List<AppSdk>>anyMap());
116116
}
117117

118+
@Test
119+
public void sfcodeKeyMissing() throws JSONException {
120+
settings.remove(SETTING_SF_CODE_KEY);
121+
122+
JSONObject expectedConfig = new JSONObject()
123+
.put("appid", appid)
124+
.put(SETTING_SF_CODE_KEY, "us");
125+
126+
JSONAssert.assertEquals(expectedConfig, factory.parseAppSdkConfig(settings), JSONCompareMode.STRICT);
127+
}
128+
129+
@Test
130+
public void emptySfCodeValue() throws JSONException {
131+
settings.put(SETTING_SF_CODE_KEY, "");
132+
133+
JSONObject expectedConfig = new JSONObject()
134+
.put("appid", appid)
135+
.put("sfcode", sfcode);
136+
137+
JSONAssert.assertEquals(expectedConfig, factory.parseAppSdkConfig(settings), JSONCompareMode.STRICT);
138+
}
139+
140+
@Test
141+
public void nullSfCodeValue() throws JSONException {
142+
settings.put(SETTING_SF_CODE_KEY, null);
143+
144+
JSONObject expectedConfig = new JSONObject()
145+
.put("appid", appid)
146+
.put("sfcode", sfcode);
147+
148+
JSONAssert.assertEquals(expectedConfig, factory.parseAppSdkConfig(settings), JSONCompareMode.STRICT);
149+
}
150+
118151
@Test
119152
public void parseId3EventNames() {
120153
List<String> expected = Arrays.asList("sendid3a", "sendid3b");

0 commit comments

Comments
 (0)