Skip to content

Commit

Permalink
Refactor telemetry to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
lbalmaceda committed Dec 5, 2018
1 parent 7c64a3b commit 9e1ba28
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
8 changes: 7 additions & 1 deletion auth0/src/main/java/com/auth0/android/util/Base64.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

/**
* Helper class originally used to generate URL safe Base64 encoded strings with padding.
*
* @deprecated this class is no longer used and will be removed soon. See {@link android.util.Base64} as an alternative.
*/
@Deprecated
public class Base64 {

private static final byte[] ENCODE_MAP = new byte[] {
private static final byte[] ENCODE_MAP = new byte[]{
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4',
Expand Down
36 changes: 23 additions & 13 deletions auth0/src/main/java/com/auth0/android/util/Telemetry.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.auth0.android.util;

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

import com.google.gson.Gson;

import java.nio.charset.Charset;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;


public class Telemetry {
public static final String HEADER_NAME = "Auth0-Client";

Expand All @@ -19,6 +23,7 @@ public class Telemetry {
private final String name;
private final String version;
private final Map<String, String> env;
private final String value;

public Telemetry(String name, String version) {
this(name, version, null);
Expand All @@ -27,11 +32,25 @@ public Telemetry(String name, String version) {
public Telemetry(String name, String version, String core) {
this.name = name;
this.version = version;
this.env = new HashMap<>();
env.put(ANDROID_KEY, String.valueOf(android.os.Build.VERSION.SDK_INT));
Map<String, String> tmpEnv = new HashMap<>();
tmpEnv.put(ANDROID_KEY, String.valueOf(android.os.Build.VERSION.SDK_INT));
if (core != null) {
env.put(CORE_KEY, core);
tmpEnv.put(CORE_KEY, core);
}
this.env = Collections.unmodifiableMap(tmpEnv);

Map<String, Object> values = new HashMap<>();
if (name != null) {
values.put(NAME_KEY, name);
}
if (version != null) {
values.put(VERSION_KEY, version);
}
values.put(ENV_KEY, env);
String json = new Gson().toJson(values);
Charset utf8 = Charset.forName("UTF-8");
byte[] bytes = json.getBytes(utf8);
value = new String(Base64.encode(bytes, Base64.URL_SAFE | Base64.NO_WRAP), utf8);
}

public String getName() {
Expand All @@ -52,15 +71,6 @@ Map<String, String> getEnvironment() {
}

public String getValue() {
Map<String, Object> values = new HashMap<>();
if (name != null) {
values.put(NAME_KEY, name);
}
if (version != null) {
values.put(VERSION_KEY, version);
}
values.put(ENV_KEY, env);
String json = new Gson().toJson(values);
return Base64.encodeUrlSafe(json);
return value;
}
}
28 changes: 9 additions & 19 deletions auth0/src/test/java/com/auth0/android/util/TelemetryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
Expand All @@ -22,62 +21,53 @@
@RunWith(RobolectricTestRunner.class)
public class TelemetryTest {

private Telemetry telemetry;

@Before
public void setUp() throws Exception {
telemetry = new Telemetry("auth0-java", "1.0.0", "1.2.3");
}

@Test
public void shouldReturnBase64() throws Exception {
assertThat(telemetry.getValue(), is(notNullValue()));
}

//Testing Android version only for a few SDKs

@Test
@Config(sdk = 21)
public void shouldAlwaysIncludeAndroidVersionAPI21() throws Exception {
telemetry = new Telemetry(null, null);
Telemetry telemetry = new Telemetry(null, null);
assertThat(telemetry.getEnvironment(), is(notNullValue()));
assertThat(telemetry.getEnvironment().get("android"), is("21"));
}

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

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

@Test
public void shouldGetName() throws Exception {
Telemetry telemetry = new Telemetry("auth0-java", "1.0.0", "1.2.3");
assertThat(telemetry.getName(), is("auth0-java"));
}

@Test
public void shouldGetVersion() throws Exception {
Telemetry telemetry = new Telemetry("auth0-java", "1.0.0", "1.2.3");
assertThat(telemetry.getVersion(), is("1.0.0"));
}

@Test
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"));
}

@Test
@Config(sdk = 23)
public void shouldGenerateValidCompleteTelemetryValue() throws Exception {
public void shouldGenerateCompleteTelemetryBase64Value() throws Exception {
Gson gson = new Gson();
Type mapType = new TypeToken<Map<String, Object>>() {
}.getType();
Expand All @@ -96,7 +86,7 @@ public void shouldGenerateValidCompleteTelemetryValue() throws Exception {

@Test
@Config(sdk = 23)
public void shouldGenerateValidBasicTelemetryValue() throws Exception {
public void shouldGenerateBasicTelemetryBase64Value() throws Exception {
Gson gson = new Gson();
Type mapType = new TypeToken<Map<String, Object>>() {
}.getType();
Expand All @@ -115,7 +105,7 @@ public void shouldGenerateValidBasicTelemetryValue() throws Exception {

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

0 comments on commit 9e1ba28

Please sign in to comment.