DimensionalSchema implements Schema { @Override public String getSchemaJSON() { if (!changed && schemaJSON != null) { return schemaJSON; } if (changedSchemaKeys) { changedSchemaKeys = false; if (schemaKeys == null) { schema.remove(Schema.FIELD_SCHEMA_KEYS); } else { try { schema.put(Schema.FIELD_SCHEMA_KEYS, SchemaUtils.createJSONObject(schemaKeys)); } catch (JSONException ex) { throw new RuntimeException(ex); } } } if (changedFromTo) { changedFromTo = false; Preconditions.checkState(!(from == null ^ to == null), "Either both from and to should be set or both should be not set."); if (from != null) { Preconditions.checkState(to >= from, "to {} must be greater than or equal to from {}.", to, from); } if (from == null) { time.remove(FIELD_TIME_FROM); time.remove(FIELD_TIME_TO); } else { try { time.put(FIELD_TIME_FROM, from); time.put(FIELD_TIME_TO, to); } catch (JSONException ex) { throw new RuntimeException(ex); } } } if (this.areEnumsUpdated) { for (int keyIndex = 0; keyIndex < keys.length(); keyIndex++) { JSONObject keyData; String name; try { keyData = keys.getJSONObject(keyIndex); name = keyData.getString(DimensionalConfigurationSchema.FIELD_KEYS_NAME); } catch (JSONException ex) { throw new RuntimeException(ex); } List<Object> enumVals = currentEnumVals.get(name); if (enumVals == null || enumVals.isEmpty()) { keyData.remove(DimensionalConfigurationSchema.FIELD_KEYS_ENUMVALUES); continue; } JSONArray newEnumValues = new JSONArray(); for (Object enumVal : enumVals) { newEnumValues.put(enumVal); } try { keyData.put(DimensionalConfigurationSchema.FIELD_KEYS_ENUMVALUES, newEnumValues); } catch (JSONException ex) { throw new RuntimeException(ex); } } this.areEnumsUpdated = false; } schemaJSON = schema.toString(); return schemaJSON; } private  DimensionalSchema();  DimensionalSchema(String schemaStub,
      DimensionalConfigurationSchema configurationSchema,
      Map<String, String> schemaKeys,
      long responseDelayMillis);  DimensionalSchema(int schemaID,
      String schemaStub,
      DimensionalConfigurationSchema configurationSchema,
      Map<String, String> schemaKeys);  DimensionalSchema(String schemaStub,
      DimensionalConfigurationSchema configurationSchema,
      long responseDelayMillis);  DimensionalSchema(int schemaID,
      String schemaStub,
      DimensionalConfigurationSchema configurationSchema,
      long responseDelayMillis);  DimensionalSchema(DimensionalConfigurationSchema configurationSchema,
      Map<String, String> schemaKeys, long responseDelayMillis);  DimensionalSchema(int schemaID,
      DimensionalConfigurationSchema configurationSchema,
      Map<String, String> schemaKeys);  DimensionalSchema(DimensionalConfigurationSchema configurationSchema);  DimensionalSchema(int schemaID,
      DimensionalConfigurationSchema configurationSchema); AggregatorRegistry getAggregatorRegistry(); @Override final void setSchemaKeys(Map<String, String> schemaKeys); void setFrom(Long from); void setTo(Long to); void setEnumsSet(Map<String, Set<Object>> enums); @SuppressWarnings({"rawtypes", "unchecked"}) void setEnumsSetComparable(Map<String, Set<Comparable>> enums); void setEnumsList(Map<String, List<Object>> enums); @Override String getSchemaJSON(); DimensionalConfigurationSchema getDimensionalConfigurationSchema(); @Override String getSchemaType(); @Override String getSchemaVersion(); @Override Map<String, String> getSchemaKeys(); boolean isPredefinedFromTo(); @Override int getSchemaID(); Map<String, List<Object>> getCurrentEnumVals(); static final String SCHEMA_TYPE; static final String SCHEMA_VERSION; static final String FIELD_TIME_FROM; static final String FIELD_TIME; static final String FIELD_TIME_TO; static final String FIELD_TIME_BUCKETS; static final String FIELD_SLIDING_AGGREGATE_SUPPORTED; static final List<Fields> VALID_KEYS; static final List<Fields> VALID_TIME_KEYS; static final String FIELD_RESPONSE_DELAY_MILLS; }
@Test public void testSchemaTags() throws Exception { List<String> expectedTags = Lists.newArrayList("geo", "bullet"); List<String> expectedKeyTags = Lists.newArrayList("geo.location"); List<String> expectedValueTagsLat = Lists.newArrayList("geo.lattitude"); List<String> expectedValueTagsLong = Lists.newArrayList("geo.longitude"); String eventSchemaJSON = SchemaUtils.jarResourceFileToString("adsGenericEventSchemaTags.json"); DimensionalSchema dimensional = new DimensionalSchema( new DimensionalConfigurationSchema(eventSchemaJSON, AggregatorRegistry.DEFAULT_AGGREGATOR_REGISTRY)); String schemaJSON = dimensional.getSchemaJSON(); JSONObject jo = new JSONObject(schemaJSON); List<String> tags = getStringList(jo.getJSONArray(FIELD_TAGS)); Assert.assertEquals(expectedTags, tags); JSONArray keys = jo.getJSONArray(DimensionalConfigurationSchema.FIELD_KEYS); List<String> keyTags = null; for (int keyIndex = 0; keyIndex < keys.length(); keyIndex++) { JSONObject key = keys.getJSONObject(keyIndex); if (!key.has(FIELD_TAGS)) { continue; } Assert.assertEquals("location", key.get(DimensionalConfigurationSchema.FIELD_KEYS_NAME)); keyTags = getStringList(key.getJSONArray(FIELD_TAGS)); } Assert.assertTrue("No tags found for any key", keyTags != null); Assert.assertEquals(expectedKeyTags, keyTags); JSONArray values = jo.getJSONArray(DimensionalConfigurationSchema.FIELD_VALUES); boolean valueTagsLat = false; boolean valueTagsLong = false; for (int valueIndex = 0; valueIndex < values.length(); valueIndex++) { JSONObject value = values.getJSONObject(valueIndex); if (!value.has(FIELD_TAGS)) { continue; } String valueName = value.getString(DimensionalConfigurationSchema.FIELD_VALUES_NAME); List<String> valueTags = getStringList(value.getJSONArray(FIELD_TAGS)); LOG.debug("value name: {}", valueName); if (valueName.startsWith("impressions")) { Assert.assertEquals(expectedValueTagsLat, valueTags); valueTagsLat = true; } else if (valueName.startsWith("clicks")) { Assert.assertEquals(expectedValueTagsLong, valueTags); valueTagsLong = true; } else { Assert.fail("There should be no tags for " + valueName); } } Assert.assertTrue("No tags found for impressions", valueTagsLat); Assert.assertTrue("No tags found for clicks", valueTagsLong); }