add schema test

This commit is contained in:
kevin
2019-04-18 17:40:04 +08:00
committed by Łukasz Paczos
parent a66cc608a9
commit 8bbd38093c
13 changed files with 305 additions and 80 deletions

View File

@@ -1 +1,2 @@
dependency-graph-mapbox-libraries.png
src/test/resources/mobile-event*

View File

@@ -17,6 +17,7 @@ dependencies {
testImplementation dependenciesList.mockk
testImplementation dependenciesList.robolectric
testImplementation dependenciesList.kotlinLib
testImplementation dependenciesList.commonsIO
}
android {

View File

@@ -10,16 +10,24 @@ import com.mapbox.android.telemetry.Event;
*/
@SuppressLint("ParcelCreator")
public abstract class MapBaseEvent extends Event {
final String event;
final String created;
private final String event;
private final String created;
MapBaseEvent(PhoneState phoneState) {
event = getEventName();
this.event = getEventName();
this.created = phoneState.getCreated();
}
abstract String getEventName();
public String getEvent() {
return event;
}
public String getCreated() {
return created;
}
@Override
public int describeContents() {
return 0;

View File

@@ -13,8 +13,8 @@ class MapClickEvent extends MapBaseEvent {
private final String cellularNetworkType;
private final String carrier;
private final String orientation;
private final double latitude;
private final double longitude;
private final double lat;
private final double lng;
private final double zoom;
private final int batteryLevel;
private final boolean pluggedIn;
@@ -23,8 +23,8 @@ class MapClickEvent extends MapBaseEvent {
MapClickEvent(PhoneState phoneState, MapState mapState) {
super(phoneState);
this.gesture = mapState.getGesture();
this.latitude = mapState.getLatitude();
this.longitude = mapState.getLongitude();
this.lat = mapState.getLatitude();
this.lng = mapState.getLongitude();
this.zoom = mapState.getZoom();
this.batteryLevel = phoneState.getBatteryLevel();
this.pluggedIn = phoneState.isPluggedIn();
@@ -39,10 +39,6 @@ class MapClickEvent extends MapBaseEvent {
return EVENT_NAME;
}
String getCreated() {
return created;
}
String getGesture() {
return gesture;
}
@@ -59,12 +55,12 @@ class MapClickEvent extends MapBaseEvent {
return orientation;
}
double getLatitude() {
return latitude;
double getLat() {
return lat;
}
double getLongitude() {
return longitude;
double getLng() {
return lng;
}
double getZoom() {
@@ -94,10 +90,10 @@ class MapClickEvent extends MapBaseEvent {
MapClickEvent that = (MapClickEvent) o;
if (Double.compare(that.latitude, latitude) != 0) {
if (Double.compare(that.lat, lat) != 0) {
return false;
}
if (Double.compare(that.longitude, longitude) != 0) {
if (Double.compare(that.lng, lng) != 0) {
return false;
}
if (Double.compare(that.zoom, zoom) != 0) {
@@ -112,9 +108,6 @@ class MapClickEvent extends MapBaseEvent {
if (wifi != that.wifi) {
return false;
}
if (created != null ? !created.equals(that.created) : that.created != null) {
return false;
}
if (gesture != null ? !gesture.equals(that.gesture) : that.gesture != null) {
return false;
}
@@ -132,15 +125,13 @@ class MapClickEvent extends MapBaseEvent {
public int hashCode() {
int result;
long temp;
result = event.hashCode();
result = 31 * result + (created != null ? created.hashCode() : 0);
result = 31 * result + (gesture != null ? gesture.hashCode() : 0);
result = gesture != null ? gesture.hashCode() : 0;
result = 31 * result + (cellularNetworkType != null ? cellularNetworkType.hashCode() : 0);
result = 31 * result + (carrier != null ? carrier.hashCode() : 0);
result = 31 * result + (orientation != null ? orientation.hashCode() : 0);
temp = Double.doubleToLongBits(latitude);
temp = Double.doubleToLongBits(lat);
result = 31 * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(longitude);
temp = Double.doubleToLongBits(lng);
result = 31 * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(zoom);
result = 31 * result + (int) (temp ^ (temp >>> 32));
@@ -153,14 +144,12 @@ class MapClickEvent extends MapBaseEvent {
@Override
public String toString() {
return "MapClickEvent{"
+ "event='" + event + '\''
+ ", created='" + created + '\''
+ ", gesture='" + gesture + '\''
+ ", cellularNetworkType='" + cellularNetworkType + '\''
+ ", carrier='" + carrier + '\''
+ ", orientation='" + orientation + '\''
+ ", latitude=" + latitude
+ ", longitude=" + longitude
+ ", lat=" + lat
+ ", lng=" + lng
+ ", zoom=" + zoom
+ ", batteryLevel=" + batteryLevel
+ ", pluggedIn=" + pluggedIn

View File

@@ -36,10 +36,6 @@ class MapDragendEvent extends MapBaseEvent {
return EVENT_NAME;
}
String getCreated() {
return created;
}
String getOrientation() {
return orientation;
}
@@ -105,9 +101,6 @@ class MapDragendEvent extends MapBaseEvent {
if (wifi != that.wifi) {
return false;
}
if (created != null ? !created.equals(that.created) : that.created != null) {
return false;
}
if (orientation != null ? !orientation.equals(that.orientation) : that.orientation != null) {
return false;
}
@@ -122,9 +115,7 @@ class MapDragendEvent extends MapBaseEvent {
public int hashCode() {
int result;
long temp;
result = event.hashCode();
result = 31 * result + (created != null ? created.hashCode() : 0);
result = 31 * result + (orientation != null ? orientation.hashCode() : 0);
result = orientation != null ? orientation.hashCode() : 0;
result = 31 * result + (carrier != null ? carrier.hashCode() : 0);
result = 31 * result + (cellularNetworkType != null ? cellularNetworkType.hashCode() : 0);
result = 31 * result + batteryLevel;
@@ -142,8 +133,6 @@ class MapDragendEvent extends MapBaseEvent {
@Override
public String toString() {
return "MapDragendEvent{"
+ "event='" + event + '\''
+ ", created='" + created + '\''
+ ", orientation='" + orientation + '\''
+ ", carrier='" + carrier + '\''
+ ", cellularNetworkType='" + cellularNetworkType + '\''

View File

@@ -59,10 +59,6 @@ class MapLoadEvent extends MapBaseEvent {
return model;
}
String getCreated() {
return created;
}
String getUserId() {
return userId;
}
@@ -131,9 +127,6 @@ class MapLoadEvent extends MapBaseEvent {
if (model != null ? !model.equals(that.model) : that.model != null) {
return false;
}
if (created != null ? !created.equals(that.created) : that.created != null) {
return false;
}
if (userId != null ? !userId.equals(that.userId) : that.userId != null) {
return false;
}
@@ -149,12 +142,10 @@ class MapLoadEvent extends MapBaseEvent {
@Override
public int hashCode() {
int result = event.hashCode();
result = 31 * result + operatingSystem.hashCode();
int result = operatingSystem != null ? operatingSystem.hashCode() : 0;
result = 31 * result + sdkIdentifier.hashCode();
result = 31 * result + sdkVersion.hashCode();
result = 31 * result + (model != null ? model.hashCode() : 0);
result = 31 * result + (created != null ? created.hashCode() : 0);
result = 31 * result + (userId != null ? userId.hashCode() : 0);
result = 31 * result + (carrier != null ? carrier.hashCode() : 0);
result = 31 * result + (cellularNetworkType != null ? cellularNetworkType.hashCode() : 0);
@@ -170,12 +161,10 @@ class MapLoadEvent extends MapBaseEvent {
@Override
public String toString() {
return "MapLoadEvent{"
+ "event='" + event + '\''
+ ", operatingSystem='" + operatingSystem + '\''
+ ", sdkIdentifier='" + sdkIdentifier + '\''
+ ", sdkVersion='" + sdkVersion + '\''
+ ", model='" + model + '\''
+ ", created='" + created + '\''
+ ", userId='" + userId + '\''
+ ", carrier='" + carrier + '\''
+ ", cellularNetworkType='" + cellularNetworkType + '\''

View File

@@ -36,10 +36,6 @@ public class OfflineDownloadEndEvent extends MapBaseEvent {
return EVENT_NAME;
}
String getCreated() {
return created;
}
Double getMinZoom() {
return minZoom;
}
@@ -104,9 +100,6 @@ public class OfflineDownloadEndEvent extends MapBaseEvent {
if (state != that.state) {
return false;
}
if (created != null ? !created.equals(that.created) : that.created != null) {
return false;
}
if (minZoom != null ? !minZoom.equals(that.minZoom) : that.minZoom != null) {
return false;
}
@@ -122,9 +115,7 @@ public class OfflineDownloadEndEvent extends MapBaseEvent {
@Override
public int hashCode() {
int result = event.hashCode();
result = 31 * result + (created != null ? created.hashCode() : 0);
result = 31 * result + (minZoom != null ? minZoom.hashCode() : 0);
int result = minZoom != null ? minZoom.hashCode() : 0;
result = 31 * result + (maxZoom != null ? maxZoom.hashCode() : 0);
result = 31 * result + (shapeForOfflineRegion != null ? shapeForOfflineRegion.hashCode() : 0);
result = 31 * result + (styleURL != null ? styleURL.hashCode() : 0);
@@ -137,8 +128,6 @@ public class OfflineDownloadEndEvent extends MapBaseEvent {
@Override
public String toString() {
return "OfflineDownloadEndEvent{"
+ "event='" + event + '\''
+ ", created='" + created + '\''
+ ", minZoom=" + minZoom
+ ", maxZoom=" + maxZoom
+ ", shapeForOfflineRegion='" + shapeForOfflineRegion + '\''

View File

@@ -32,10 +32,6 @@ public class OfflineDownloadStartEvent extends MapBaseEvent {
return EVENT_NAME;
}
String getCreated() {
return created;
}
Double getMinZoom() {
return minZoom;
}
@@ -67,9 +63,6 @@ public class OfflineDownloadStartEvent extends MapBaseEvent {
OfflineDownloadStartEvent that = (OfflineDownloadStartEvent) o;
if (created != null ? !created.equals(that.created) : that.created != null) {
return false;
}
if (minZoom != null ? !minZoom.equals(that.minZoom) : that.minZoom != null) {
return false;
}
@@ -85,9 +78,7 @@ public class OfflineDownloadStartEvent extends MapBaseEvent {
@Override
public int hashCode() {
int result = event.hashCode();
result = 31 * result + (created != null ? created.hashCode() : 0);
result = 31 * result + (minZoom != null ? minZoom.hashCode() : 0);
int result = minZoom != null ? minZoom.hashCode() : 0;
result = 31 * result + (maxZoom != null ? maxZoom.hashCode() : 0);
result = 31 * result + (shapeForOfflineRegion != null ? shapeForOfflineRegion.hashCode() : 0);
result = 31 * result + (styleURL != null ? styleURL.hashCode() : 0);
@@ -97,8 +88,6 @@ public class OfflineDownloadStartEvent extends MapBaseEvent {
@Override
public String toString() {
return "OfflineDownloadStartEvent{"
+ "event='" + event + '\''
+ ", created='" + created + '\''
+ ", minZoom=" + minZoom
+ ", maxZoom=" + maxZoom
+ ", shapeForOfflineRegion='" + shapeForOfflineRegion + '\''

View File

@@ -56,8 +56,8 @@ public class MapEventFactoryTest {
public void testClickEvent() {
mapState.setGesture(TelemetryConstants.DOUBLE_TAP);
MapClickEvent mapClickEvent = MapEventFactory.buildMapClickEvent(phoneState, mapState);
assertEquals(LATITUDE, mapClickEvent.getLatitude(), 0);
assertEquals(LONGITUDE, mapClickEvent.getLongitude(), 0);
assertEquals(LATITUDE, mapClickEvent.getLat(), 0);
assertEquals(LONGITUDE, mapClickEvent.getLng(), 0);
assertEquals(ZOOM, mapClickEvent.getZoom(), 0);
assertEquals(BATTERY_LEVEL, mapClickEvent.getBatteryLevel());
assertEquals(CARRIER, mapClickEvent.getCarrier());

View File

@@ -0,0 +1,261 @@
package com.mapbox.mapboxsdk.module.telemetry;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.google.gson.annotations.SerializedName;
import org.apache.commons.io.IOUtils;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPInputStream;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class SchemaTest {
private static final String MAP_CLICK = "map.click";
private static final String MAP_DRAG = "map.dragend";
private static final String MAP_LOAD = "map.load";
private static ArrayList<JsonObject> schemaArray;
@BeforeClass
public static void downloadSchema() throws Exception {
unpackSchemas();
}
@Test
public void checkMapClickEventSize() throws Exception {
JsonObject schema = grabSchema(MAP_CLICK);
List<Field> fields = grabClassFields(MapClickEvent.class);
assertEquals(schema.size(), fields.size());
}
@Test
public void checkMapClickEventFields() throws Exception {
JsonObject schema = grabSchema(MAP_CLICK);
List<Field> fields = grabClassFields(MapClickEvent.class);
schemaContainsFields(schema, fields);
}
@Test
public void checkMapDragEndEventSize() throws Exception {
JsonObject schema = grabSchema(MAP_DRAG);
List<Field> fields = grabClassFields(MapDragendEvent.class);
assertEquals(schema.size(), fields.size());
}
@Test
public void checkMapDragEndEventFields() throws Exception {
JsonObject schema = grabSchema(MAP_DRAG);
List<Field> fields = grabClassFields(MapDragendEvent.class);
schemaContainsFields(schema, fields);
}
@Test
public void checkMapLoadEventSize() throws Exception {
JsonObject schema = grabSchema(MAP_LOAD);
List<Field> fields = grabClassFields(MapLoadEvent.class);
//FIXME: this assertion is invalid: we should introduce a concept of mandatory/optional field to schema validation
//assertEquals(schema.size(), fields.size());
}
@Test
public void checkMapLoadEventFields() throws Exception {
JsonObject schema = grabSchema(MAP_LOAD);
List<Field> fields = grabClassFields(MapLoadEvent.class);
schemaContainsFields(schema, fields);
}
private void schemaContainsFields(JsonObject schema, List<Field> fields) {
int distanceRemainingCount = 0;
int durationRemainingCount = 0;
for (int i = 0; i < fields.size(); i++) {
String thisField = String.valueOf(fields.get(i));
String[] fieldArray = thisField.split(" ");
String[] typeArray = fieldArray[fieldArray.length - 2].split("\\.");
String type = typeArray[typeArray.length - 1];
String[] nameArray = fieldArray[fieldArray.length - 1].split("\\.");
String field = nameArray[nameArray.length - 1];
SerializedName serializedName = fields.get(i).getAnnotation(SerializedName.class);
if (serializedName != null) {
field = serializedName.value();
}
if (field.equalsIgnoreCase("durationRemaining")) {
durationRemainingCount++;
if (durationRemainingCount > 1) {
field = "step" + field;
}
}
if (field.equalsIgnoreCase("distanceRemaining")) {
distanceRemainingCount++;
if (distanceRemainingCount > 1) {
field = "step" + field;
}
}
JsonObject thisSchema = findSchema(schema, field);
assertNotNull(thisSchema);
if (thisSchema.has("type")) {
typesMatch(thisSchema, type);
}
}
}
private JsonObject findSchema(JsonObject schema, String field) {
JsonObject thisSchema = schema.getAsJsonObject(field);
return thisSchema;
}
private void typesMatch(JsonObject schema, String type) {
if (type.equalsIgnoreCase("int") || type.equalsIgnoreCase("integer")
|| type.equalsIgnoreCase("double") || type.equalsIgnoreCase("float")) {
type = "number";
}
if (type.contains("[]")) {
type = "array";
}
Class<? extends JsonElement> typeClass = schema.get("type").getClass();
JsonElement jsonElement = new JsonParser().parse(type.toLowerCase());
if (typeClass == JsonPrimitive.class) {
JsonElement typePrimitive = schema.get("type");
assertTrue(typePrimitive.equals(jsonElement));
} else {
JsonArray arrayOfTypes = schema.getAsJsonArray("type");
assertTrue(arrayOfTypes.contains(jsonElement));
}
}
private static ByteArrayInputStream getFileBytes() throws IOException {
InputStream inputStream = SchemaTest.class.getClassLoader().getResourceAsStream("mobile-event-schemas.jsonl.gz");
byte[] byteOut = IOUtils.toByteArray(inputStream);
return new ByteArrayInputStream(byteOut);
}
private static void unpackSchemas() throws IOException {
ByteArrayInputStream bais = getFileBytes();
GZIPInputStream gzis = new GZIPInputStream(bais);
InputStreamReader reader = new InputStreamReader(gzis);
BufferedReader in = new BufferedReader(reader);
schemaArray = new ArrayList<>();
Gson gson = new Gson();
String readed;
while ((readed = in.readLine()) != null) {
JsonObject schema = gson.fromJson(readed, JsonObject.class);
schemaArray.add(schema);
}
}
private JsonObject grabSchema(String eventName) {
for (JsonObject thisSchema : schemaArray) {
String name = thisSchema.get("name").getAsString();
if (name.equalsIgnoreCase(eventName)) {
Gson gson = new Gson();
String schemaString = gson.toJson(thisSchema.get("properties"));
JsonObject schema = gson.fromJson(thisSchema.get("properties"), JsonObject.class);
if (schema.has("step")) {
JsonObject stepJson = schema.get("step").getAsJsonObject();
JsonObject stepProperties = stepJson.get("properties").getAsJsonObject();
String stepPropertiesJson = gson.toJson(stepProperties);
schemaString = generateStepSchemaString(stepPropertiesJson, schemaString);
schema = gson.fromJson(schemaString, JsonObject.class);
schema.remove("step");
}
schema.remove("userAgent");
schema.remove("received");
schema.remove("token");
schema.remove("authorization");
schema.remove("owner");
schema.remove("locationAuthorization");
schema.remove("locationEnabled");
//temporary need to work out a solution to include this data
schema.remove("platform");
return schema;
}
}
return null;
}
private List<Field> grabClassFields(Class aClass) {
List<Field> fields = new ArrayList<>();
Field[] allFields = aClass.getDeclaredFields();
for (Field field : allFields) {
if (Modifier.isPrivate(field.getModifiers()) && !Modifier.isStatic(field.getModifiers())) {
fields.add(field);
}
}
Field[] superFields = aClass.getSuperclass().getDeclaredFields();
for (Field field : superFields) {
if (Modifier.isPrivate(field.getModifiers()) && !Modifier.isStatic(field.getModifiers())) {
fields.add(field);
}
}
return fields;
}
private List<Field> removeField(List<Field> fields, String fieldName) {
for (Field field : new ArrayList<>(fields)) {
String thisField = String.valueOf(field);
String[] fieldArray = thisField.split("\\.");
String simpleField = fieldArray[fieldArray.length - 1];
if (simpleField.equalsIgnoreCase(fieldName)) {
fields.remove(field);
}
}
return fields;
}
private String generateStepSchemaString(String stepJson, String schemaString) {
stepJson = stepJson.replace("\"distanceRemaining\"", "\"stepdistanceRemaining\"");
stepJson = stepJson.replace("durationRemaining", "stepdurationRemaining");
stepJson = stepJson.replaceFirst("\\{", ",");
schemaString = schemaString.replaceAll("}$", "");
schemaString = schemaString + stepJson;
return schemaString;
}
}

View File

@@ -35,3 +35,4 @@ subprojects {
// Load build system information. If this file does not exist, run
// `make platform/android/gradle/configuration.gradle`
apply from: "${rootDir}/gradle/configuration.gradle"
apply from: "${rootDir}/gradle/download-schema.gradle"

View File

@@ -52,7 +52,7 @@ ext {
testEspressoIntents : "com.android.support.test.espresso:espresso-intents:${versions.espresso}",
testEspressoContrib : "com.android.support.test.espresso:espresso-contrib:${versions.espresso}",
testUiAutomator : "com.android.support.test.uiautomator:uiautomator-v18:${versions.uiAutomator}",
commonsIO : 'commons-io:commons-io:2.5',
supportAnnotations : "com.android.support:support-annotations:${versions.supportLib}",
supportAppcompatV7 : "com.android.support:appcompat-v7:${versions.supportLib}",
supportFragmentV4 : "com.android.support:support-fragment:${versions.supportLib}",

View File

@@ -0,0 +1,8 @@
task downloadSchema(type: Copy) {
def address='https://mapbox.s3.amazonaws.com/mapbox-gl-native/event-schema/mobile-event-schemas.jsonl.gz'
def target='MapboxGLAndroidSDK/src/test/resources/'
new File(target + "${address.tokenize('/')[-1]}").withOutputStream { out ->
new URL(address).withInputStream { from -> out << from }
}
}