Skip to content

Commit

Permalink
update library version key
Browse files Browse the repository at this point in the history
  • Loading branch information
lbalmaceda committed Apr 15, 2019
1 parent f54e185 commit cdf8649
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
24 changes: 14 additions & 10 deletions src/main/java/com/auth0/net/Telemetry.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,41 @@ public class Telemetry {

private static final String NAME_KEY = "name";
private static final String VERSION_KEY = "version";
private static final String CORE_KEY = "core";
private static final String LIBRARY_VERSION_KEY = "auth0-java";
private static final String ENV_KEY = "env";
private static final String JAVA_KEY = "java";

private final String name;
private final String version;
private final String core;
private final String libraryVersion;
private final Map<String, String> env;
private final String value;

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;
this.core = core;
this.libraryVersion = libraryVersion;

Map<String, Object> values = new HashMap<>();
if (name != null) {
values.put(NAME_KEY, name);
if (name == null) {
env = Collections.emptyMap();
value = null;
return;
}

Map<String, Object> values = new HashMap<>();
values.put(NAME_KEY, name);
if (version != null) {
values.put(VERSION_KEY, version);
}

HashMap<String, String> tmpEnv = new HashMap<>();
tmpEnv.put(JAVA_KEY, Runtime.class.getPackage().getSpecificationVersion());
if (core != null) {
tmpEnv.put(CORE_KEY, core);
if (libraryVersion != null) {
tmpEnv.put(LIBRARY_VERSION_KEY, libraryVersion);
}
this.env = Collections.unmodifiableMap(tmpEnv);
values.put(ENV_KEY, env);
Expand All @@ -70,7 +74,7 @@ public String getVersion() {

//Visible for testing
String getLibraryVersion() {
return core;
return libraryVersion;
}

//Visible for testing
Expand Down
27 changes: 17 additions & 10 deletions src/test/java/com/auth0/net/TelemetryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,42 @@ public void shouldReturnBasicTelemetryBase64Value() throws Exception {
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(nullValue()));
assertThat((String) completeEnv.get("auth0-java"), is(nullValue()));
assertThat((String) completeEnv.get("java"), is("1.8"));
}

@Test
public void shouldReturnCompleteTelemetryBase64Value() throws Exception {
Telemetry telemetry = new Telemetry("auth0-java", "1.0.0", "2.1.3");
Telemetry telemetry = new Telemetry("lock", "1.0.0", "2.1.3");
String value = telemetry.getValue();
assertThat(value, is(notNullValue()));
assertThat(value, is("eyJuYW1lIjoiYXV0aDAtamF2YSIsImVudiI6eyJjb3JlIjoiMi4xLjMiLCJqYXZhIjoiMS44In0sInZlcnNpb24iOiIxLjAuMCJ9"));
assertThat(value, is("eyJuYW1lIjoibG9jayIsImVudiI6eyJqYXZhIjoiMS44IiwiYXV0aDAtamF2YSI6IjIuMS4zIn0sInZlcnNpb24iOiIxLjAuMCJ9"));

String completeString = new String(Base64.decodeBase64(value), "UTF-8");
TypeReference<Map<String, Object>> mapType = new TypeReference<Map<String, Object>>() {
};
Map<String, Object> complete = new ObjectMapper().readValue(completeString, mapType);
assertThat((String) complete.get("name"), is("auth0-java"));
assertThat((String) complete.get("name"), is("lock"));
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("2.1.3"));
assertThat((String) completeEnv.get("auth0-java"), is("2.1.3"));
assertThat((String) completeEnv.get("java"), is("1.8"));
}

@Test
public void shouldAlwaysIncludeJavaVersion() throws Exception {
Telemetry telemetry = new Telemetry(null, null);
public void shouldAcceptNullVersionAlwaysIncludeJavaVersion() throws Exception {
Telemetry telemetry = new Telemetry("auth0-java", null);
assertThat(telemetry.getValue(), is(notNullValue()));
assertThat(telemetry.getEnvironment(), is(notNullValue()));
}

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

@Test
public void shouldGetName() throws Exception {
Telemetry telemetry = new Telemetry("auth0-java", "1.0.0");
Expand All @@ -68,17 +75,17 @@ public void shouldGetVersion() throws Exception {
}

@Test
public void shouldNotIncludeCoreIfNotProvided() throws Exception {
public void shouldNotIncludeLibraryVersionIfNotProvided() throws Exception {
Telemetry telemetry = new Telemetry("auth0-java", "1.0.0");
assertThat(telemetry.getLibraryVersion(), is(nullValue()));
assertThat(telemetry.getEnvironment().containsKey("core"), is(false));
assertThat(telemetry.getEnvironment().containsKey("auth0-java"), is(false));
}

@Test
public void shouldGetLibraryVersion() throws Exception {
Telemetry telemetry = new Telemetry("auth0-java", "1.0.0", "2.1.3");
assertThat(telemetry.getLibraryVersion(), is("2.1.3"));
assertThat(telemetry.getEnvironment().get("core"), is("2.1.3"));
assertThat(telemetry.getEnvironment().get("auth0-java"), is("2.1.3"));
}

}

0 comments on commit cdf8649

Please sign in to comment.