Skip to content

Commit 8c2c849

Browse files
Add tests
1 parent 81a7dcd commit 8c2c849

File tree

4 files changed

+93
-20
lines changed

4 files changed

+93
-20
lines changed

android/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ android {
3737
implementation 'com.segment.analytics.android.integrations:amplitude:+'
3838
}
3939
}
40+
41+
dependencies {
42+
testImplementation 'junit:junit:4.12'
43+
}

android/src/main/java/com/example/flutter_segment/FlutterSegmentPlugin.java

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import android.os.Bundle;
99
import android.util.Log;
1010

11+
import androidx.annotation.VisibleForTesting;
12+
1113
import com.segment.analytics.Analytics;
1214
import com.segment.analytics.AnalyticsContext;
1315
import com.segment.analytics.Properties;
@@ -34,6 +36,7 @@
3436
public class FlutterSegmentPlugin implements MethodCallHandler, FlutterPlugin {
3537
private Context applicationContext;
3638
private MethodChannel methodChannel;
39+
private PropertiesMapper propertiesMapper = new PropertiesMapper();
3740

3841
static HashMap<String, Object> appendToContextMiddleware;
3942

@@ -201,7 +204,7 @@ private void callTrack(
201204
HashMap<String, Object> propertiesData,
202205
HashMap<String, Object> optionsData
203206
) {
204-
Properties properties = buildProperties(propertiesData);
207+
Properties properties = propertiesMapper.buildProperties(propertiesData);
205208
Options options = this.buildOptions(optionsData);
206209

207210
Analytics.with(this.applicationContext).track(eventName, properties, options);
@@ -224,7 +227,7 @@ private void callScreen(
224227
HashMap<String, Object> propertiesData,
225228
HashMap<String, Object> optionsData
226229
) {
227-
Properties properties = buildProperties(propertiesData);
230+
Properties properties = propertiesMapper.buildProperties(propertiesData);
228231
Options options = this.buildOptions(optionsData);
229232

230233
Analytics.with(this.applicationContext).screen(null, screenName, properties, options);
@@ -363,22 +366,4 @@ private static Map deepMerge(Map original, Map newMap) {
363366
}
364367
return original;
365368
}
366-
367-
private Properties buildProperties(Map<String, Object> map) {
368-
Properties properties = new Properties();
369-
370-
for(Map.Entry<String, Object> property : map.entrySet()) {
371-
String key = property.getKey();
372-
Object value = property.getValue();
373-
374-
if (value instanceof Map){
375-
Properties nestedProperties = buildProperties((Map<String, Object>) value);
376-
properties.putValue(key, nestedProperties);
377-
} else {
378-
properties.putValue(key, value);
379-
}
380-
}
381-
382-
return properties;
383-
}
384369
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.example.flutter_segment;
2+
3+
import androidx.annotation.VisibleForTesting;
4+
5+
import com.segment.analytics.Properties;
6+
7+
import java.util.Map;
8+
9+
public class PropertiesMapper {
10+
@VisibleForTesting
11+
protected Properties buildProperties(Map<String, Object> map) {
12+
Properties properties = new Properties();
13+
14+
for(Map.Entry<String, Object> property : map.entrySet()) {
15+
String key = property.getKey();
16+
Object value = property.getValue();
17+
18+
if (value instanceof Map){
19+
Properties nestedProperties = buildProperties((Map<String, Object>) value);
20+
properties.putValue(key, nestedProperties);
21+
} else {
22+
properties.putValue(key, value);
23+
}
24+
}
25+
26+
return properties;
27+
}
28+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.example.flutter_segment;
2+
3+
import com.segment.analytics.Properties;
4+
5+
import org.junit.Test;
6+
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
import static org.junit.Assert.assertEquals;
11+
import static org.junit.Assert.assertNotNull;
12+
13+
public class PropertiesMapperTest {
14+
@Test
15+
public void buildProperties_withoutNestedProperties_returnsIt() {
16+
Map<String, Object> properties = new HashMap();
17+
properties.put("string", "value");
18+
properties.put("int", 1);
19+
properties.put("bool", true);
20+
21+
PropertiesMapper mapper = new PropertiesMapper();
22+
Properties result = mapper.buildProperties(properties);
23+
24+
assertEquals(result.get("string"), "value");
25+
assertEquals(result.get("int"), 1);
26+
assertEquals(result.get("bool"), true);
27+
}
28+
29+
@Test
30+
public void buildProperties_withNestedMap_returnsIt() {
31+
Map<String, Object> properties = new HashMap();
32+
Map<String, Object> nestedProperties = new HashMap();
33+
properties.put("nested", nestedProperties);
34+
35+
properties.put("string", "value");
36+
properties.put("int", 1);
37+
properties.put("bool", true);
38+
39+
nestedProperties.put("string", "value2");
40+
nestedProperties.put("int", 2);
41+
nestedProperties.put("bool", false);
42+
43+
PropertiesMapper mapper = new PropertiesMapper();
44+
Properties result = mapper.buildProperties(properties);
45+
46+
assertEquals(result.get("string"), "value");
47+
assertEquals(result.get("int"), 1);
48+
assertEquals(result.get("bool"), true);
49+
50+
Properties nestedResult = (Properties) result.get("nested");
51+
assertNotNull(nestedResult);
52+
assertEquals(nestedResult.get("string"), "value2");
53+
assertEquals(nestedResult.get("int"), 2);
54+
assertEquals(nestedResult.get("bool"), false);
55+
}
56+
}

0 commit comments

Comments
 (0)