@Test public void testValidate() { Entity entity = Mockito.mock(Entity.class); Map<String, Object> body = new HashMap<String, Object>(); body.put(REFERENCE_FIELD, UUID); Mockito.when(entity.getBody()).thenReturn(body); Mockito.when(entity.getEntityId()).thenReturn(UUID); List<ValidationError> errors = new ArrayList<ValidationError>(); boolean valid = selfReferenceValidator.validate(entity, errors); Assert.assertFalse(valid); Assert.assertEquals(1, errors.size()); }
public boolean validate(Entity entity, List<ValidationError> errors) { String selfReferencePath = selfReferenceExtractor.getSelfReferenceFields(entity); if (selfReferencePath != null) { Map<String, Object> body = entity.getBody(); if (body != null) { String property = (String) body.get(selfReferencePath); String uuid = entity.getEntityId(); if (uuid == null) { uuid = getDeterministicId(entity); } if (property != null && property.equals(uuid)) { Map<String, String> naturalKeys = null; try { naturalKeys = naturalKeyExtractor.getNaturalKeys(entity); if (naturalKeys != null && naturalKeys.size() > 0) { property = naturalKeys.toString(); } } catch (NoNaturalKeysDefinedException e) { LOG.error(e.getMessage(), e); } errors.add(new ValidationError(ErrorType.SELF_REFERENCING_DATA, selfReferencePath, property, new String[] { "Reference to a separate entity" })); return false; } return true; } } return true; }
SelfReferenceValidator { public boolean validate(Entity entity, List<ValidationError> errors) { String selfReferencePath = selfReferenceExtractor.getSelfReferenceFields(entity); if (selfReferencePath != null) { Map<String, Object> body = entity.getBody(); if (body != null) { String property = (String) body.get(selfReferencePath); String uuid = entity.getEntityId(); if (uuid == null) { uuid = getDeterministicId(entity); } if (property != null && property.equals(uuid)) { Map<String, String> naturalKeys = null; try { naturalKeys = naturalKeyExtractor.getNaturalKeys(entity); if (naturalKeys != null && naturalKeys.size() > 0) { property = naturalKeys.toString(); } } catch (NoNaturalKeysDefinedException e) { LOG.error(e.getMessage(), e); } errors.add(new ValidationError(ErrorType.SELF_REFERENCING_DATA, selfReferencePath, property, new String[] { "Reference to a separate entity" })); return false; } return true; } } return true; } }
SelfReferenceValidator { public boolean validate(Entity entity, List<ValidationError> errors) { String selfReferencePath = selfReferenceExtractor.getSelfReferenceFields(entity); if (selfReferencePath != null) { Map<String, Object> body = entity.getBody(); if (body != null) { String property = (String) body.get(selfReferencePath); String uuid = entity.getEntityId(); if (uuid == null) { uuid = getDeterministicId(entity); } if (property != null && property.equals(uuid)) { Map<String, String> naturalKeys = null; try { naturalKeys = naturalKeyExtractor.getNaturalKeys(entity); if (naturalKeys != null && naturalKeys.size() > 0) { property = naturalKeys.toString(); } } catch (NoNaturalKeysDefinedException e) { LOG.error(e.getMessage(), e); } errors.add(new ValidationError(ErrorType.SELF_REFERENCING_DATA, selfReferencePath, property, new String[] { "Reference to a separate entity" })); return false; } return true; } } return true; }  }
SelfReferenceValidator { public boolean validate(Entity entity, List<ValidationError> errors) { String selfReferencePath = selfReferenceExtractor.getSelfReferenceFields(entity); if (selfReferencePath != null) { Map<String, Object> body = entity.getBody(); if (body != null) { String property = (String) body.get(selfReferencePath); String uuid = entity.getEntityId(); if (uuid == null) { uuid = getDeterministicId(entity); } if (property != null && property.equals(uuid)) { Map<String, String> naturalKeys = null; try { naturalKeys = naturalKeyExtractor.getNaturalKeys(entity); if (naturalKeys != null && naturalKeys.size() > 0) { property = naturalKeys.toString(); } } catch (NoNaturalKeysDefinedException e) { LOG.error(e.getMessage(), e); } errors.add(new ValidationError(ErrorType.SELF_REFERENCING_DATA, selfReferencePath, property, new String[] { "Reference to a separate entity" })); return false; } return true; } } return true; }  boolean validate(Entity entity, List<ValidationError> errors); String getDeterministicId(Entity entity); }
SelfReferenceValidator { public boolean validate(Entity entity, List<ValidationError> errors) { String selfReferencePath = selfReferenceExtractor.getSelfReferenceFields(entity); if (selfReferencePath != null) { Map<String, Object> body = entity.getBody(); if (body != null) { String property = (String) body.get(selfReferencePath); String uuid = entity.getEntityId(); if (uuid == null) { uuid = getDeterministicId(entity); } if (property != null && property.equals(uuid)) { Map<String, String> naturalKeys = null; try { naturalKeys = naturalKeyExtractor.getNaturalKeys(entity); if (naturalKeys != null && naturalKeys.size() > 0) { property = naturalKeys.toString(); } } catch (NoNaturalKeysDefinedException e) { LOG.error(e.getMessage(), e); } errors.add(new ValidationError(ErrorType.SELF_REFERENCING_DATA, selfReferencePath, property, new String[] { "Reference to a separate entity" })); return false; } return true; } } return true; }  boolean validate(Entity entity, List<ValidationError> errors); String getDeterministicId(Entity entity);  }