summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/module/telemetry/SchemaTest.java
blob: 24a343137a59e72aa3a2c0fa3d5dd49c7b52489e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
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 final String MAP_PERFORMANCE = "mobile.performance_trace";
  private static final String OFFLINE_DOWNLOAD_END = "map.offlineDownload.end";
  private static final String OFFLINE_DOWNLOAD_START = "map.offlineDownload.start";
  private static ArrayList<JsonObject> schemaArray;

  @BeforeClass
  public static void downloadSchema() throws Exception {
    unpackSchemas();
  }

  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);
    }
  }

  @Test
  public void checkMapClickEventSize() {
    JsonObject schema = grabSchema(MAP_CLICK);
    List<Field> fields = grabClassFields(MapClickEvent.class);

    assertEquals(schema.size(), fields.size());
  }

  @Test
  public void checkMapClickEventFields() {
    JsonObject schema = grabSchema(MAP_CLICK);
    List<Field> fields = grabClassFields(MapClickEvent.class);

    schemaContainsFields(schema, fields);
  }

  @Test
  public void checkMapDragEndEventSize() {
    JsonObject schema = grabSchema(MAP_DRAG);
    List<Field> fields = grabClassFields(MapDragendEvent.class);

    assertEquals(schema.size(), fields.size());
  }

  @Test
  public void checkMapDragEndEventFields() {
    JsonObject schema = grabSchema(MAP_DRAG);
    List<Field> fields = grabClassFields(MapDragendEvent.class);

    schemaContainsFields(schema, fields);
  }

  @Test
  public void checkOfflineDownloadEndEventSize() {
    JsonObject schema = grabSchema(OFFLINE_DOWNLOAD_END);
    List<Field> fields = grabClassFields(OfflineDownloadEndEvent.class);

    assertEquals(schema.size(), fields.size());
  }

  @Test
  public void checkOfflineDownloadEndEventFields() {
    JsonObject schema = grabSchema(OFFLINE_DOWNLOAD_END);
    List<Field> fields = grabClassFields(OfflineDownloadEndEvent.class);

    schemaContainsFields(schema, fields);
  }

  @Test
  public void checkOfflineDownloadStartEventSize() {
    JsonObject schema = grabSchema(OFFLINE_DOWNLOAD_START);
    List<Field> fields = grabClassFields(OfflineDownloadStartEvent.class);

    assertEquals(schema.size(), fields.size());
  }

  @Test
  public void checkOfflineDownloadStartEventFields() {
    JsonObject schema = grabSchema(OFFLINE_DOWNLOAD_START);
    List<Field> fields = grabClassFields(OfflineDownloadStartEvent.class);

    schemaContainsFields(schema, fields);
  }

  @Test
  public void checkMapLoadEventSize() {
    JsonObject schema = grabSchema(MAP_LOAD);
    List<Field> fields = grabClassFields(MapLoadEvent.class);

    assertEquals(schema.size(), fields.size());
  }

  @Test
  public void checkMapLoadEventFields() {
    JsonObject schema = grabSchema(MAP_LOAD);
    List<Field> fields = grabClassFields(MapLoadEvent.class);

    schemaContainsFields(schema, fields);
  }

  @Test
  public void checkPerformanceEventSize() {
    JsonObject schema = grabSchema(MAP_PERFORMANCE);
    List<Field> fields = grabClassFields(PerformanceEvent.class);

    assertEquals(schema.size(), fields.size());
  }

  @Test
  public void checkPerformanceEventFields() {
    JsonObject schema = grabSchema(MAP_PERFORMANCE);
    List<Field> fields = grabClassFields(PerformanceEvent.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.equalsIgnoreCase("long")) {
      type = "number";
    }

    if (type.contains("[]") || type.equalsIgnoreCase("list")) {
      type = "array";
    }
    if (type.equalsIgnoreCase("jsonobject")) {
      type = "object";
    }
    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 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;
  }
}