Skip to content

Commit

Permalink
Report data builder tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Konrad Dysput committed Nov 7, 2024
1 parent 75a94f0 commit 5eab77b
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class DeviceAttributesHelper {
/*
* Current Device id
*/
private static String guid;
private static String uuid;

public DeviceAttributesHelper(Context context) {
this.context = context;
Expand Down Expand Up @@ -262,20 +262,20 @@ private BatteryState getBatteryState() {
* @return unique device identifier
*/
private String generateDeviceId() {
if (!BacktraceStringHelper.isNullOrEmpty(guid)) {
return guid;
if (!BacktraceStringHelper.isNullOrEmpty(uuid)) {
return uuid;
}

String androidId = Settings.Secure.getString(this.context.getContentResolver(),
Settings.Secure.ANDROID_ID);

// if the android id is not defined we want to cache at least guid
// for the current session
guid = TextUtils.isEmpty(androidId)
uuid = TextUtils.isEmpty(androidId)
? UUID.randomUUID().toString()
: UUID.nameUUIDFromBytes(androidId.getBytes()).toString();

return guid;
return uuid;
}

private ActivityManager.MemoryInfo getMemoryInformation() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package backtraceio.library.models.attributes;

import java.util.HashMap;

public class ReportDataAttributes {
private final HashMap<String, String> reportAttributes = new HashMap<>();

private final HashMap<String, Object> reportAnnotations = new HashMap<>();


public void addAnnotation(String key, Object value) {
reportAnnotations.put(key, value);
}

public void addAttribute(String key, String value) {
reportAttributes.put(key, value);
}

public HashMap<String, String> getAttributes() {
return reportAttributes;
}

public HashMap<String, Object> getAnnotations() {
return reportAnnotations;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package backtraceio.library.models.attributes;

import java.util.Map;

import backtraceio.library.common.TypeHelper;

public class ReportDataBuilder {

/**
* Divide custom user attributes into primitive and complex attributes and add to this object. By default nullable values will be included.
*
* @param attributes client's attributes
* @return Report data attributes divided into attributes and annotations
*/
public static ReportDataAttributes getReportAttributes(Map<String, Object> attributes) {
return getReportAttributes(attributes, false);
}

/**
* Divide custom user attributes into primitive and complex attributes and add to this object
*
* @param attributes client's attributes
* @param skipNullable define attributes behavior on nullable value. By default all nullable attributes
* will be included in the report. For some features like metrics, we don't want to send
* nullable values, because they can generate invalid behavior/incorrect information.
* @return Report data attributes divided into attributes and annotations
*/
public static ReportDataAttributes getReportAttributes(Map<String, Object> attributes, boolean skipNullable) {
ReportDataAttributes reportDataAttributes = new ReportDataAttributes();

if (attributes == null) {
return reportDataAttributes;
}

for (Map.Entry<String, Object> entry : attributes.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (value == null) {
if (!skipNullable) {
reportDataAttributes.addAttribute(key, null);
}
continue;
}
if (TypeHelper.isPrimitiveOrPrimitiveWrapperOrString(value.getClass())) {
reportDataAttributes.addAttribute(key, value.toString());
} else {
reportDataAttributes.addAnnotation(key, value);
}
}

return reportDataAttributes;

}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package backtraceio.library.models.json;

import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.os.Build;
import android.provider.Settings;
Expand All @@ -19,7 +18,8 @@
import backtraceio.library.common.BacktraceStringHelper;
import backtraceio.library.common.DeviceAttributesHelper;
import backtraceio.library.enums.ScreenOrientation;
import backtraceio.library.models.Tuple;
import backtraceio.library.models.attributes.ReportDataAttributes;
import backtraceio.library.models.attributes.ReportDataBuilder;

/**
* Class instance to get a built-in attributes from current application
Expand Down Expand Up @@ -204,9 +204,9 @@ private void convertReportAttributes(BacktraceReport report) {
}

private void convertAttributes(Map<String, Object> clientAttributes) {
Tuple<Map<String, String>, Map<String, Object>> reportData = ReportDataBuilder.getReportAttribues(clientAttributes);
this.attributes.putAll(reportData.first);
this.complexAttributes.putAll(reportData.second);
ReportDataAttributes data = ReportDataBuilder.getReportAttributes(clientAttributes);
this.attributes.putAll(data.getAttributes());
this.complexAttributes.putAll(data.getAnnotations());
}


Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import backtraceio.library.logger.BacktraceLogger;
import backtraceio.library.models.BacktraceMetricsSettings;
import backtraceio.library.models.json.BacktraceAttributes;
import backtraceio.library.models.json.ReportDataBuilder;
import backtraceio.library.models.attributes.ReportDataBuilder;
import backtraceio.library.models.metrics.SummedEvent;
import backtraceio.library.models.metrics.UniqueEvent;

Expand Down Expand Up @@ -208,7 +208,7 @@ public void enable(BacktraceMetricsSettings settings, String uniqueEventName) {
* Due to that, we need to have a getter that will always transform attributes to a simple format.
*/
private Map<String, String> getClientMetricsAttributes() {
return ReportDataBuilder.getReportAttribues(customReportAttributes).first;
return ReportDataBuilder.getReportAttributes(customReportAttributes, true).getAttributes();
}


Expand Down Expand Up @@ -278,7 +278,7 @@ public boolean addUniqueEvent(String attributeName, Map<String, Object> attribut
return false;
}

Map<String, String> metricsAttributes = ReportDataBuilder.getReportAttribues(attributes).first;
Map<String, String> metricsAttributes = ReportDataBuilder.getReportAttributes(attributes, true).getAttributes();

Map<String, String> localAttributes = createLocalAttributes(metricsAttributes);

Expand Down Expand Up @@ -355,7 +355,7 @@ public boolean addSummedEvent(String metricGroupName, Map<String, Object> attrib
return false;
}

Map<String, String> metricsAttributes = ReportDataBuilder.getReportAttribues(attributes).first;
Map<String, String> metricsAttributes = ReportDataBuilder.getReportAttributes(attributes, true).getAttributes();

SummedEvent summedEvent = new SummedEvent(metricGroupName);
summedEvent.addAttributes(metricsAttributes);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package backtraceio.library;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;

import static org.junit.Assert.assertEquals;

import backtraceio.library.models.attributes.ReportDataAttributes;
import backtraceio.library.models.attributes.ReportDataBuilder;

@RunWith(Parameterized.class)
public class ReportDataAnnotationBuilderTest {

@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{new HashMap<String, String>() {{
put("add", "value");
}}},
{new HashSet<String>() {{
add("value");
}}},
{new Object()},
});
}

private final Object annotation;

public ReportDataAnnotationBuilderTest(Object annotation) {
this.annotation = annotation;
}

@Test
public void correctlySetComplexObjectAsAnnotation() {
String key = "annotation-key";
ReportDataAttributes data = ReportDataBuilder.getReportAttributes(new HashMap<String, Object>() {
{
put(key, annotation);
}
});
assertEquals(data.getAnnotations().get(key), annotation);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package backtraceio.library;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import backtraceio.library.models.attributes.ReportDataAttributes;
import backtraceio.library.models.attributes.ReportDataBuilder;

@RunWith(Parameterized.class)
public class ReportDataAttributeBuilderTest {

private final String key = "attribute-key";

@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{"String value"},
{123},
{45.67},
{true},
});
}

private final Object primitiveValue;

public ReportDataAttributeBuilderTest(Object primitiveValue) {
this.primitiveValue = primitiveValue;
}

@Test
public void correctlySetPrimitiveValueIntoAttribute() {
ReportDataAttributes data = ReportDataBuilder.getReportAttributes(new HashMap<String, Object>() {
{
put(key, primitiveValue);
}
});
assertEquals(data.getAttributes().get(key), primitiveValue.toString());
}

@Test
public void shouldSetNullableValueAsAttribute() {
ReportDataAttributes data = ReportDataBuilder.getReportAttributes(new HashMap<String, Object>() {
{
put(key, null);
}
});
assertNull(data.getAttributes().get(key));
}

@Test
public void shouldSkipNullableValue() {
ReportDataAttributes data = ReportDataBuilder.getReportAttributes(new HashMap<String, Object>() {
{
put(key, null);
}
}, true);
assertNull(data.getAttributes().get(key));
}
}

0 comments on commit 5eab77b

Please sign in to comment.