@Test public void testProcessReference() { MetaClass metaClass = raptorMetaService.getMetaClass(APPLICATION_SERVICE); String found = "{ \"name\": \"appName-StringArray\", \"_type\": \"ApplicationService\", \"services\": [{\"_oid\":\"4fbb314fc681caf13e283a6a\"}, {\"_oid\":\"4fbb314fc681caf13e283a6c\"}]}"; String source= "{ \"name\": \"appName-StringArray\", \"_type\": \"ApplicationService\", \"services\": [{\"_oid\":\"4fbb314fc681caf13e283a6a\"}, {\"_oid\":\"4fbb314fc681caf13e283a6c\"}]}"; IEntity foundEntity = new JsonEntity(metaClass, found); IEntity sourceEntity = new JsonEntity(metaClass, source); EntityComparator comparator = new EntityComparator(sourceEntity); MetaRelationship metaRelationship = (MetaRelationship)metaClass.getFieldByName("services"); comparator.processReference(foundEntity, metaRelationship); Assert.assertFalse(comparator.getDiffResult()); found = "{ \"name\": \"appName-StringArray\", \"_type\": \"ApplicationService\", \"services\": [{\"_oid\":\"4fbb314fc681caf13e283a6a\"}, {\"_oid\":\"4fbb314fc681caf13e283a6e\"}]}"; foundEntity = new JsonEntity(metaClass, found); comparator.processReference(foundEntity, metaRelationship); Assert.assertTrue(comparator.getDiffResult()); found = "{ \"name\": \"appName-StringArray\", \"_type\": \"ApplicationService\", \"services\": [{\"_oid\":\"4fbb314fc681caf13e283a6a\"}, {\"_oid\":\"4fbb314fc681caf13e283a6b\"}, {\"_oid\":\"4fbb314fc681caf13e283a6c\"}]}"; foundEntity = new JsonEntity(metaClass, found); comparator.processReference(foundEntity, metaRelationship); Assert.assertTrue(comparator.getDiffResult()); }
@Override public void processReference(IEntity currentEntity, MetaRelationship metaRelationship) { if (diff && !isRoot) { return; } String fieldName = metaRelationship.getName(); List<?> sourceValues = sourceEntity.getFieldValues(fieldName); List<?> destValues = currentEntity.getFieldValues(fieldName); boolean isEmebedReference = metaRelationship.getRelationType().equals(RelationTypeEnum.Embedded); if (sourceValues.size() != destValues.size()) { diff = true; return; } boolean flag = false; int len = sourceValues.size(); for (int i = 0; i < len; i++) { IEntity sourceRef = (IEntity) sourceValues.get(i); IEntity destRef = (IEntity) destValues.get(i); if ((sourceRef.getId() == null && destRef.getId() != null) || (sourceRef.getId() != null && destRef.getId() == null)) { flag = true; break; } if (isEmebedReference) { EntityComparator childComparator = new EntityComparator(sourceRef, false); destRef.traverse(childComparator); if (childComparator.getDiffResult()) { flag = true; break; } } else if (!StringUtils.equals(sourceRef.getId(), destRef.getId()) || !StringUtils.equals(sourceRef.getType(), destRef.getType())) { flag = true; break; } } if (flag) { diff = true; } if (isRoot && !flag) { sourceEntity.removeField(metaRelationship.getName()); } }
EntityComparator implements IEntityVisitor { @Override public void processReference(IEntity currentEntity, MetaRelationship metaRelationship) { if (diff && !isRoot) { return; } String fieldName = metaRelationship.getName(); List<?> sourceValues = sourceEntity.getFieldValues(fieldName); List<?> destValues = currentEntity.getFieldValues(fieldName); boolean isEmebedReference = metaRelationship.getRelationType().equals(RelationTypeEnum.Embedded); if (sourceValues.size() != destValues.size()) { diff = true; return; } boolean flag = false; int len = sourceValues.size(); for (int i = 0; i < len; i++) { IEntity sourceRef = (IEntity) sourceValues.get(i); IEntity destRef = (IEntity) destValues.get(i); if ((sourceRef.getId() == null && destRef.getId() != null) || (sourceRef.getId() != null && destRef.getId() == null)) { flag = true; break; } if (isEmebedReference) { EntityComparator childComparator = new EntityComparator(sourceRef, false); destRef.traverse(childComparator); if (childComparator.getDiffResult()) { flag = true; break; } } else if (!StringUtils.equals(sourceRef.getId(), destRef.getId()) || !StringUtils.equals(sourceRef.getType(), destRef.getType())) { flag = true; break; } } if (flag) { diff = true; } if (isRoot && !flag) { sourceEntity.removeField(metaRelationship.getName()); } } }
EntityComparator implements IEntityVisitor { @Override public void processReference(IEntity currentEntity, MetaRelationship metaRelationship) { if (diff && !isRoot) { return; } String fieldName = metaRelationship.getName(); List<?> sourceValues = sourceEntity.getFieldValues(fieldName); List<?> destValues = currentEntity.getFieldValues(fieldName); boolean isEmebedReference = metaRelationship.getRelationType().equals(RelationTypeEnum.Embedded); if (sourceValues.size() != destValues.size()) { diff = true; return; } boolean flag = false; int len = sourceValues.size(); for (int i = 0; i < len; i++) { IEntity sourceRef = (IEntity) sourceValues.get(i); IEntity destRef = (IEntity) destValues.get(i); if ((sourceRef.getId() == null && destRef.getId() != null) || (sourceRef.getId() != null && destRef.getId() == null)) { flag = true; break; } if (isEmebedReference) { EntityComparator childComparator = new EntityComparator(sourceRef, false); destRef.traverse(childComparator); if (childComparator.getDiffResult()) { flag = true; break; } } else if (!StringUtils.equals(sourceRef.getId(), destRef.getId()) || !StringUtils.equals(sourceRef.getType(), destRef.getType())) { flag = true; break; } } if (flag) { diff = true; } if (isRoot && !flag) { sourceEntity.removeField(metaRelationship.getName()); } } EntityComparator(IEntity sourceEntity); protected  EntityComparator(IEntity sourceEntity, boolean isRoot); }
EntityComparator implements IEntityVisitor { @Override public void processReference(IEntity currentEntity, MetaRelationship metaRelationship) { if (diff && !isRoot) { return; } String fieldName = metaRelationship.getName(); List<?> sourceValues = sourceEntity.getFieldValues(fieldName); List<?> destValues = currentEntity.getFieldValues(fieldName); boolean isEmebedReference = metaRelationship.getRelationType().equals(RelationTypeEnum.Embedded); if (sourceValues.size() != destValues.size()) { diff = true; return; } boolean flag = false; int len = sourceValues.size(); for (int i = 0; i < len; i++) { IEntity sourceRef = (IEntity) sourceValues.get(i); IEntity destRef = (IEntity) destValues.get(i); if ((sourceRef.getId() == null && destRef.getId() != null) || (sourceRef.getId() != null && destRef.getId() == null)) { flag = true; break; } if (isEmebedReference) { EntityComparator childComparator = new EntityComparator(sourceRef, false); destRef.traverse(childComparator); if (childComparator.getDiffResult()) { flag = true; break; } } else if (!StringUtils.equals(sourceRef.getId(), destRef.getId()) || !StringUtils.equals(sourceRef.getType(), destRef.getType())) { flag = true; break; } } if (flag) { diff = true; } if (isRoot && !flag) { sourceEntity.removeField(metaRelationship.getName()); } } EntityComparator(IEntity sourceEntity); protected  EntityComparator(IEntity sourceEntity, boolean isRoot); @Override Collection<String> getVisitFields(IEntity currentEntity); @Override void processAttribute(IEntity currentEntity, MetaField metaField); @Override void processReference(IEntity currentEntity, MetaRelationship metaRelationship); boolean getDiffResult(); void setRoot(boolean isRoot); }
EntityComparator implements IEntityVisitor { @Override public void processReference(IEntity currentEntity, MetaRelationship metaRelationship) { if (diff && !isRoot) { return; } String fieldName = metaRelationship.getName(); List<?> sourceValues = sourceEntity.getFieldValues(fieldName); List<?> destValues = currentEntity.getFieldValues(fieldName); boolean isEmebedReference = metaRelationship.getRelationType().equals(RelationTypeEnum.Embedded); if (sourceValues.size() != destValues.size()) { diff = true; return; } boolean flag = false; int len = sourceValues.size(); for (int i = 0; i < len; i++) { IEntity sourceRef = (IEntity) sourceValues.get(i); IEntity destRef = (IEntity) destValues.get(i); if ((sourceRef.getId() == null && destRef.getId() != null) || (sourceRef.getId() != null && destRef.getId() == null)) { flag = true; break; } if (isEmebedReference) { EntityComparator childComparator = new EntityComparator(sourceRef, false); destRef.traverse(childComparator); if (childComparator.getDiffResult()) { flag = true; break; } } else if (!StringUtils.equals(sourceRef.getId(), destRef.getId()) || !StringUtils.equals(sourceRef.getType(), destRef.getType())) { flag = true; break; } } if (flag) { diff = true; } if (isRoot && !flag) { sourceEntity.removeField(metaRelationship.getName()); } } EntityComparator(IEntity sourceEntity); protected  EntityComparator(IEntity sourceEntity, boolean isRoot); @Override Collection<String> getVisitFields(IEntity currentEntity); @Override void processAttribute(IEntity currentEntity, MetaField metaField); @Override void processReference(IEntity currentEntity, MetaRelationship metaRelationship); boolean getDiffResult(); void setRoot(boolean isRoot);  }