@Test(expected = NaturalKeyValidationException.class) public void shouldThrowExceptionWhenMissingRequiredKeyField() throws NoNaturalKeysDefinedException { Entity e = setup(); e.getBody().remove("someField"); @SuppressWarnings("unused") Map<String, String> naturalKeys = naturalKeyExtractor.getNaturalKeys(e); }
@Override public Map<String, String> getNaturalKeys(Entity entity) throws NoNaturalKeysDefinedException { Map<String, String> map = new HashMap<String, String>(); List<String> missingKeys = new ArrayList<String>(); Map<String, Boolean> naturalKeyFields = getNaturalKeyFields(entity); if (naturalKeyFields == null) { return null; } for (Entry<String, Boolean> keyField : naturalKeyFields.entrySet()) { Object value = null; try { value = PropertyUtils.getProperty(entity.getBody(), keyField.getKey()); } catch (IllegalAccessException e) { handleFieldAccessException(keyField.getKey(), entity); } catch (InvocationTargetException e) { handleFieldAccessException(keyField.getKey(), entity); } catch (NoSuchMethodException e) { handleFieldAccessException(keyField.getKey(), entity); } if (value == null) { if (keyField.getValue().booleanValue()) { map.put(keyField.getKey(), ""); } else { missingKeys.add(keyField.getKey()); } } else { String strValue = value.toString(); map.put(keyField.getKey(), strValue); } } if (!missingKeys.isEmpty()) { throw new NaturalKeyValidationException(entity.getType(), missingKeys); } return map; }
NaturalKeyExtractor implements INaturalKeyExtractor { @Override public Map<String, String> getNaturalKeys(Entity entity) throws NoNaturalKeysDefinedException { Map<String, String> map = new HashMap<String, String>(); List<String> missingKeys = new ArrayList<String>(); Map<String, Boolean> naturalKeyFields = getNaturalKeyFields(entity); if (naturalKeyFields == null) { return null; } for (Entry<String, Boolean> keyField : naturalKeyFields.entrySet()) { Object value = null; try { value = PropertyUtils.getProperty(entity.getBody(), keyField.getKey()); } catch (IllegalAccessException e) { handleFieldAccessException(keyField.getKey(), entity); } catch (InvocationTargetException e) { handleFieldAccessException(keyField.getKey(), entity); } catch (NoSuchMethodException e) { handleFieldAccessException(keyField.getKey(), entity); } if (value == null) { if (keyField.getValue().booleanValue()) { map.put(keyField.getKey(), ""); } else { missingKeys.add(keyField.getKey()); } } else { String strValue = value.toString(); map.put(keyField.getKey(), strValue); } } if (!missingKeys.isEmpty()) { throw new NaturalKeyValidationException(entity.getType(), missingKeys); } return map; } }
NaturalKeyExtractor implements INaturalKeyExtractor { @Override public Map<String, String> getNaturalKeys(Entity entity) throws NoNaturalKeysDefinedException { Map<String, String> map = new HashMap<String, String>(); List<String> missingKeys = new ArrayList<String>(); Map<String, Boolean> naturalKeyFields = getNaturalKeyFields(entity); if (naturalKeyFields == null) { return null; } for (Entry<String, Boolean> keyField : naturalKeyFields.entrySet()) { Object value = null; try { value = PropertyUtils.getProperty(entity.getBody(), keyField.getKey()); } catch (IllegalAccessException e) { handleFieldAccessException(keyField.getKey(), entity); } catch (InvocationTargetException e) { handleFieldAccessException(keyField.getKey(), entity); } catch (NoSuchMethodException e) { handleFieldAccessException(keyField.getKey(), entity); } if (value == null) { if (keyField.getValue().booleanValue()) { map.put(keyField.getKey(), ""); } else { missingKeys.add(keyField.getKey()); } } else { String strValue = value.toString(); map.put(keyField.getKey(), strValue); } } if (!missingKeys.isEmpty()) { throw new NaturalKeyValidationException(entity.getType(), missingKeys); } return map; }  }
NaturalKeyExtractor implements INaturalKeyExtractor { @Override public Map<String, String> getNaturalKeys(Entity entity) throws NoNaturalKeysDefinedException { Map<String, String> map = new HashMap<String, String>(); List<String> missingKeys = new ArrayList<String>(); Map<String, Boolean> naturalKeyFields = getNaturalKeyFields(entity); if (naturalKeyFields == null) { return null; } for (Entry<String, Boolean> keyField : naturalKeyFields.entrySet()) { Object value = null; try { value = PropertyUtils.getProperty(entity.getBody(), keyField.getKey()); } catch (IllegalAccessException e) { handleFieldAccessException(keyField.getKey(), entity); } catch (InvocationTargetException e) { handleFieldAccessException(keyField.getKey(), entity); } catch (NoSuchMethodException e) { handleFieldAccessException(keyField.getKey(), entity); } if (value == null) { if (keyField.getValue().booleanValue()) { map.put(keyField.getKey(), ""); } else { missingKeys.add(keyField.getKey()); } } else { String strValue = value.toString(); map.put(keyField.getKey(), strValue); } } if (!missingKeys.isEmpty()) { throw new NaturalKeyValidationException(entity.getType(), missingKeys); } return map; }  @Override Map<String, String> getNaturalKeys(Entity entity); @Override Map<String, Boolean> getNaturalKeyFields(Entity entity); @Override NaturalKeyDescriptor getNaturalKeyDescriptor(Entity entity); String getCollectionName(Entity entity); }
NaturalKeyExtractor implements INaturalKeyExtractor { @Override public Map<String, String> getNaturalKeys(Entity entity) throws NoNaturalKeysDefinedException { Map<String, String> map = new HashMap<String, String>(); List<String> missingKeys = new ArrayList<String>(); Map<String, Boolean> naturalKeyFields = getNaturalKeyFields(entity); if (naturalKeyFields == null) { return null; } for (Entry<String, Boolean> keyField : naturalKeyFields.entrySet()) { Object value = null; try { value = PropertyUtils.getProperty(entity.getBody(), keyField.getKey()); } catch (IllegalAccessException e) { handleFieldAccessException(keyField.getKey(), entity); } catch (InvocationTargetException e) { handleFieldAccessException(keyField.getKey(), entity); } catch (NoSuchMethodException e) { handleFieldAccessException(keyField.getKey(), entity); } if (value == null) { if (keyField.getValue().booleanValue()) { map.put(keyField.getKey(), ""); } else { missingKeys.add(keyField.getKey()); } } else { String strValue = value.toString(); map.put(keyField.getKey(), strValue); } } if (!missingKeys.isEmpty()) { throw new NaturalKeyValidationException(entity.getType(), missingKeys); } return map; }  @Override Map<String, String> getNaturalKeys(Entity entity); @Override Map<String, Boolean> getNaturalKeyFields(Entity entity); @Override NaturalKeyDescriptor getNaturalKeyDescriptor(Entity entity); String getCollectionName(Entity entity);  }