KeyInformation { public static KeyInformation parse(String input) { List<String> manipulations = new ArrayList<>(); KeyInformation key; int questionMarkIndex = input.indexOf('?'); String withoutManipulation = questionMarkIndex == -1 ? input : input.substring(0, questionMarkIndex); String allManipulations = questionMarkIndex == -1 ? null : input.substring(questionMarkIndex + 1); if (allManipulations != null) { manipulations.addAll(Arrays.asList(allManipulations.split("\\?"))); } int prefixIndex = withoutManipulation.indexOf('.'); String prefix = prefixIndex != -1 ? withoutManipulation.substring(0, prefixIndex) : ""; withoutManipulation = withoutManipulation.substring(prefixIndex + 1); if (prefix.equalsIgnoreCase(TRIGGER_PREFIX)) { key = new KeyInformation(input, prefix, withoutManipulation, manipulations); } else if (prefix.equalsIgnoreCase(ADDITIONAL_DATA_PREFIX)) { Pattern pattern = Pattern.compile("([a-zA-Z0-9\\-_]+)\\.([\\.a-zA-Z0-9\\-_]+)#([a-zA-Z0-9]+)\\.(.+)"); Matcher matcher = pattern.matcher(withoutManipulation); if (matcher.matches()) { String dataProviderName = matcher.group(DATA_PROVIDER_NAME_IDX); String objectType = matcher.group(OBJECT_TYPE_IDX); Long objectId = Long.valueOf(matcher.group(OBJECT_ID_IDX)); String eventKey = matcher.group(EVENT_KEY_IDX); key = new KeyInformation(input, prefix, dataProviderName, objectType, objectId, eventKey, manipulations); } else { throw new IllegalArgumentException("Incorrect format for key from additional data"); } } else if (prefix.equalsIgnoreCase(POST_ACTION_PARAMETER_PREFIX)) { Pattern pattern = Pattern.compile("([a-zA-Z0-9\\-_]+)\\.(.+)"); Matcher matcher = pattern.matcher(withoutManipulation); if (matcher.matches()) { Long objectId = Long.valueOf(matcher.group(POST_ACTION_PARAM_ID_IDX)); String fieldKey = matcher.group(POST_ACTION_PARAM_KEY_IDX); key = new KeyInformation(input, prefix, objectId, fieldKey, manipulations); } else { throw new IllegalArgumentException("Incorrect format for key from post action parameter."); } } else { throw new IllegalArgumentException("Key must be from trigger, additional data or post action parameter."); } return key; } private  KeyInformation(String originalKey, String prefix, String key, List<String> manipulations); private  KeyInformation(String originalKey, String prefix, Long objectId, String key, List<String> manipulations); private  KeyInformation(String originalKey, String prefix, String dataProviderName, String objectType,
                           Long objectId, String key, List<String> manipulations); static KeyInformation parse(String input); static List<KeyInformation> parseAll(String input); boolean fromTrigger(); boolean fromAdditionalData(); boolean fromPostActionParameter(); String getPrefix(); String getOriginalKey(); String getDataProviderName(); String getObjectType(); Long getObjectId(); String getKey(); boolean hasManipulations(); List<String> getManipulations(); @Override int hashCode(); @Override boolean equals(Object obj); static final String TRIGGER_PREFIX; static final String ADDITIONAL_DATA_PREFIX; static final String POST_ACTION_PARAMETER_PREFIX; }
@Test public void shouldGetInformationFromTriggerKey() { String original = String.format("%s.%s", TRIGGER_PREFIX, KEY_VALUE); KeyInformation key = KeyInformation.parse(original); assertKeyFromTrigger(original, key); }
@Test public void shouldGetInformationFromTriggerKeyWithManipulations() { String original = String.format("%s.%s?toupper?join(-)", TRIGGER_PREFIX, KEY_VALUE); KeyInformation key = KeyInformation.parse(original); assertKeyFromTrigger(original, key); }
@Test public void shouldGetInformationFromAdditionalDataKey() { String original = String.format("%s.%s.%s#%d.%s", ADDITIONAL_DATA_PREFIX, DATA_PROVIDER_NAME, OBJECT_TYPE, OBJECT_ID, KEY_VALUE); KeyInformation key = KeyInformation.parse(original); assertKeyFromAdditionalData(original, key); original = String.format("%s.%s.%s#%d.%s", ADDITIONAL_DATA_PREFIX, DATA_PROVIDER_NAME, OBJECT_TYPE_2, OBJECT_ID, KEY_VALUE); key = KeyInformation.parse(original); assertKeyFromAdditionalData(original, key, OBJECT_TYPE_2); }
@Test public void shouldGetInformationFromAdditionalDataKeyWithManipulations() { String original = String.format("%s.%s.%s#%d.%s?toupper?join(-)", ADDITIONAL_DATA_PREFIX, DATA_PROVIDER_NAME, OBJECT_TYPE, OBJECT_ID, KEY_VALUE); KeyInformation key = KeyInformation.parse(original); assertKeyFromAdditionalData(original, key); original = String.format("%s.%s.%s#%d.%s?toupper?join(-)", ADDITIONAL_DATA_PREFIX, DATA_PROVIDER_NAME, OBJECT_TYPE_2, OBJECT_ID, KEY_VALUE); key = KeyInformation.parse(original); assertKeyFromAdditionalData(original, key, OBJECT_TYPE_2); }
@Test public void shouldGetInformationFromPostActionParameterKey() { String original = String.format("%s.%s.%s", POST_ACTION_PARAMETER_PREFIX, OBJECT_ID, KEY_VALUE); KeyInformation key = KeyInformation.parse(original); assertKeyFromPostActionParameter(original, key); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionWhenKeyIsFromUnknownSource() { KeyInformation.parse("test"); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionWhenAdditionalDataKeyHasIncorrectFormat() { String original = String.format("%s.%s.%s#.%s?toupper?join(-)", ADDITIONAL_DATA_PREFIX, DATA_PROVIDER_NAME, OBJECT_TYPE, KEY_VALUE); KeyInformation.parse(original); }