Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphComparator could not find the new member of ArrayList #46

Open
ihsanhaikalz opened this issue Sep 27, 2019 · 2 comments
Open

GraphComparator could not find the new member of ArrayList #46

ihsanhaikalz opened this issue Sep 27, 2019 · 2 comments

Comments

@ihsanhaikalz
Copy link

ihsanhaikalz commented Sep 27, 2019

I am having a problem while using your great library as I want to compare two ArrayLists for differences using GraphComparator. I deleted an element of one of the lists and when I compared the old list and new list it shows that there is OBJECT_ORPHAN command. But when I reversed it by comparing the new list and the old list, it should show that there is a new object but somehow it could not detect.

My code is like this:

List<UnifiedBucket> ubs= new ArrayList<>(); // Assume I already put elements in the list 
List<UnifiedBucket> ubsCopy = cloner.deepClone(ubs);
ubsCopy.remove(25);
ubsCopy.get(14).setRawPolicy(null);
List<GraphComparator.Delta> deltas = GraphComparator.compare(ubs, ubsCopy, getIdFetcher()); //This works fine as I could find the OBJECT_ORPHAN

for (GraphComparator.Delta delta : deltas) {
	if (delta.getCmd() != GraphComparator.Delta.Command.LIST_SET_ELEMENT) {
		System.out.println(delta);
	}
}

List<GraphComparator.Delta> deltas2 = GraphComparator.compare(ubsCopy, ubs, getIdFetcher()); //This does not work fine as I could not find the new element that is not deleted in the ubs list

for (GraphComparator.Delta delta : deltas2) {
	if (delta.getCmd() != GraphComparator.Delta.Command.LIST_SET_ELEMENT) {
		System.out.println(delta);
	}
}

Any reason why?

@jdereg
Copy link
Owner

jdereg commented Oct 7, 2019

The GraphComparator expects to do delta's on data objects (POJOs), meaning domain object model instances that have some sort of ID on them. The IdFetcher is there to normalize fetching the ID from the domain objects so that the GraphComparator can get the IDs of the domain objects without having to think about 'how' to get it.

So, in your example, if the List contains domain objects, that have unique IDs, and there is a supplied IdFetcher to the compare() method, it should work.

@ihsanhaikalz
Copy link
Author

I am still unable to detect my case even though I already followed getIdFetcher() method from your example in Test classes. I forgot to add the POJO of my class UnifiedBucket as follows:

import dev.morphia.annotations.Embedded;
import dev.morphia.annotations.Entity;
import org.javers.core.metamodel.annotation.Id;

import java.io.Serializable;
import java.util.Date;

@Entity("unified_bucket")
public class UnifiedBucket implements Serializable, Cloneable, HasId {


    String name;
    String type;
    Date creationDate;
    Date discoveredDate;
    String location;
    @Embedded
    BucketConfiguration bucketConfiguration;
    String rawPolicy;
    String rawACL;
    String csp;
    String ownerId;
    Boolean deleted;
    @Id
    String arn;

    @Override
    public Object getId() {
        return arn;
    }

}

Should I implement my own compare() method? Is there something missing from my code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants