Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update telemetry format #228

Merged
merged 1 commit into from
Apr 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions auth0/src/main/java/com/auth0/android/util/Telemetry.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.auth0.android.util;

import android.support.annotation.VisibleForTesting;
import android.text.TextUtils;
import android.util.Base64;

import com.google.gson.Gson;
Expand All @@ -17,7 +18,7 @@ public class Telemetry {
private static final String NAME_KEY = "name";
private static final String VERSION_KEY = "version";
private static final String ENV_KEY = "env";
private static final String CORE_KEY = "core";
private static final String LIBRARY_VERSION_KEY = "auth0.android";
private static final String ANDROID_KEY = "android";

private final String name;
Expand All @@ -29,21 +30,24 @@ public Telemetry(String name, String version) {
this(name, version, null);
}

public Telemetry(String name, String version, String core) {
public Telemetry(String name, String version, String libraryVersion) {
this.name = name;
this.version = version;
if (TextUtils.isEmpty(name)) {
env = Collections.emptyMap();
value = null;
return;
}
Map<String, String> tmpEnv = new HashMap<>();
tmpEnv.put(ANDROID_KEY, String.valueOf(android.os.Build.VERSION.SDK_INT));
if (core != null) {
tmpEnv.put(CORE_KEY, core);
if (!TextUtils.isEmpty(libraryVersion)) {
tmpEnv.put(LIBRARY_VERSION_KEY, libraryVersion);
}
this.env = Collections.unmodifiableMap(tmpEnv);

Map<String, Object> values = new HashMap<>();
if (name != null) {
values.put(NAME_KEY, name);
}
if (version != null) {
values.put(NAME_KEY, name);
if (!TextUtils.isEmpty(version)) {
values.put(VERSION_KEY, version);
}
values.put(ENV_KEY, env);
Expand All @@ -62,7 +66,7 @@ public String getVersion() {
}

public String getLibraryVersion() {
return env.get(CORE_KEY);
return env.get(LIBRARY_VERSION_KEY);
}

@VisibleForTesting
Expand Down
42 changes: 15 additions & 27 deletions auth0/src/test/java/com/auth0/android/util/TelemetryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,31 @@ public class TelemetryTest {
@Test
@Config(sdk = 21)
public void shouldAlwaysIncludeAndroidVersionAPI21() throws Exception {
Telemetry telemetry = new Telemetry(null, null);
Telemetry telemetry = new Telemetry("auth0-java", null);
assertThat(telemetry.getEnvironment(), is(notNullValue()));
assertThat(telemetry.getEnvironment().get("android"), is("21"));
}

@Test
@Config(sdk = 23)
public void shouldAlwaysIncludeAndroidVersionAPI23() throws Exception {
Telemetry telemetry = new Telemetry(null, null);
Telemetry telemetry = new Telemetry("auth0-java", null);
assertThat(telemetry.getEnvironment(), is(notNullValue()));
assertThat(telemetry.getEnvironment().get("android"), is("23"));
}

@Test
public void shouldNotIncludeCoreIfNotProvided() throws Exception {
public void shouldNotAcceptNullName() throws Exception {
Telemetry telemetry = new Telemetry(null, null);
assertThat(telemetry.getValue(), is(nullValue()));
assertThat(telemetry.getEnvironment(), is(notNullValue()));
}

@Test
public void shouldNotIncludeLibraryVersionIfNotProvided() throws Exception {
Telemetry telemetry = new Telemetry(null, null);
assertThat(telemetry.getEnvironment(), is(notNullValue()));
assertThat(telemetry.getEnvironment().containsKey("core"), is(false));
assertThat(telemetry.getEnvironment().containsKey("auth0.android"), is(false));
}

@Test
Expand All @@ -62,7 +69,7 @@ public void shouldGetVersion() throws Exception {
public void shouldGetLibraryVersion() throws Exception {
Telemetry telemetry = new Telemetry("auth0-java", "1.0.0", "1.2.3");
assertThat(telemetry.getLibraryVersion(), is("1.2.3"));
assertThat(telemetry.getEnvironment().get("core"), is("1.2.3"));
assertThat(telemetry.getEnvironment().get("auth0.android"), is("1.2.3"));
}

@Test
Expand All @@ -74,13 +81,13 @@ public void shouldGenerateCompleteTelemetryBase64Value() throws Exception {

Telemetry telemetryComplete = new Telemetry("auth0-java", "1.0.0", "1.2.3");
String value = telemetryComplete.getValue();
assertThat(value, is("eyJuYW1lIjoiYXV0aDAtamF2YSIsImVudiI6eyJjb3JlIjoiMS4yLjMiLCJhbmRyb2lkIjoiMjMifSwidmVyc2lvbiI6IjEuMC4wIn0="));
assertThat(value, is("eyJuYW1lIjoiYXV0aDAtamF2YSIsImVudiI6eyJhbmRyb2lkIjoiMjMiLCJhdXRoMC5hbmRyb2lkIjoiMS4yLjMifSwidmVyc2lvbiI6IjEuMC4wIn0="));
String completeString = new String(Base64.decode(value, Base64.URL_SAFE | Base64.NO_WRAP), "UTF-8");
Map<String, Object> complete = gson.fromJson(completeString, mapType);
assertThat((String) complete.get("name"), is("auth0-java"));
assertThat((String) complete.get("version"), is("1.0.0"));
Map<String, Object> completeEnv = (Map<String, Object>) complete.get("env");
assertThat((String) completeEnv.get("core"), is("1.2.3"));
assertThat((String) completeEnv.get("auth0.android"), is("1.2.3"));
assertThat((String) completeEnv.get("android"), is("23"));
}

Expand All @@ -99,26 +106,7 @@ public void shouldGenerateBasicTelemetryBase64Value() throws Exception {
assertThat((String) basic.get("name"), is("auth0-python"));
assertThat((String) basic.get("version"), is("99.3.1"));
Map<String, Object> basicEnv = (Map<String, Object>) basic.get("env");
assertThat(basicEnv.get("core"), is(nullValue()));
assertThat(basicEnv.get("auth0.android"), is(nullValue()));
assertThat((String) basicEnv.get("android"), is("23"));
}

@Test
@Config(sdk = 21)
public void shouldGenerateRegularTelemetryBase64Value() throws Exception {
Gson gson = new Gson();
Type mapType = new TypeToken<Map<String, Object>>() {
}.getType();

Telemetry telemetryRegular = new Telemetry(null, null);
String value = telemetryRegular.getValue();
assertThat(value, is("eyJlbnYiOnsiYW5kcm9pZCI6IjIxIn19"));
String defaultString = new String(Base64.decode(value, Base64.URL_SAFE | Base64.NO_WRAP), "UTF-8");
Map<String, Object> regular = gson.fromJson(defaultString, mapType);
assertThat(regular.get("name"), is(nullValue()));
assertThat(regular.get("version"), is(nullValue()));
Map<String, Object> regularEnv = (Map<String, Object>) regular.get("env");
assertThat(regularEnv.get("core"), is(nullValue()));
assertThat((String) regularEnv.get("android"), is("21"));
}
}