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

CustomValueComparator not being invoked #925

Closed
vinnybod opened this issue Dec 6, 2019 · 4 comments
Closed

CustomValueComparator not being invoked #925

vinnybod opened this issue Dec 6, 2019 · 4 comments
Labels

Comments

@vinnybod
Copy link

@vinnybod vinnybod commented Dec 6, 2019

Cross posting from: https://stackoverflow.com/questions/59185469/javers-customvaluecomparator-for-zoneddatetime

I am using JaVers 5.8.5, which is the latest as of this post.

I want to compare two objects that contain ZonedDateTime using JaVers. The issue is that it finds differences when the zones don't match up, but I'd really like it to treat the ZonedDateTimes as instants, since the timezones aren't always guaranteed in these situations.

It just doesn't seem like JaVers is using the custom comparator that I supplied. I provided a slimmed down test below. Note that I've tried the CustomValueComparator class, both as an anonymous class and not, and I've tried using the version of registerValue that takes the two lambdas.

import lombok.Builder;
import lombok.Value;
import org.javers.core.Javers;
import org.javers.core.JaversBuilder;
import org.javers.core.diff.Diff;
import org.javers.core.diff.ListCompareAlgorithm;
import org.javers.core.diff.custom.CustomValueComparator;
import org.junit.Before;
import org.junit.Test;

import java.time.ZoneId;
import java.time.ZonedDateTime;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;

public class JaversTest {

    private Javers javers;

    @Before
    public void setUp() throws Exception {
        javers = JaversBuilder
                .javers()
                // using CustomValueComparator
                .registerValue(ZonedDateTime.class, new CustomValueComparator<ZonedDateTime>() {
                    @Override
                    public boolean equals(ZonedDateTime a, ZonedDateTime b) {
                        return a.toInstant().equals(b.toInstant());
                    }

                    @Override
                    public String toString(ZonedDateTime value) {
                        return String.valueOf(value.toInstant().getEpochSecond());
                    }
                })
                // using lambdas
                .registerValue(ZonedDateTime.class,
                        (a, b) -> a.toInstant().getEpochSecond() == b.toInstant().getEpochSecond(),
                        (a) -> String.valueOf(a.toInstant().getEpochSecond()))
                .build();
    }

    @Test
    public void testZonedDateTime() {
        final ZonedDateTime now = ZonedDateTime.now();
        final ZonedDateTimeHolder a = ZonedDateTimeHolder.builder()
                .id(1L)
                .zonedDateTime(now.withZoneSameInstant(ZoneId.of("UTC"))).build();
        final ZonedDateTimeHolder b = ZonedDateTimeHolder.builder()
                .id(1L)
                .zonedDateTime(now.withZoneSameInstant(ZoneId.of("Z"))).build();
        final Diff compare = javers.compare(a, b);

        assertThat(compare.getChanges().size(), is(0));
    }

    @TypeName("ZonedDateTimeHolder")
    @Builder
    @Value
    public static class ZonedDateTimeHolder {

        @Id
        private long id;

        private ZonedDateTime zonedDateTime;
    }
}

Output of compare.toString()
Diff:
* changes on io.freestar.auditlog.JaversTest$ZonedDateTimeHolder/ :
  - 'zonedDateTime' value changed from '04 Dec 2019, 22:15:04+0000' to '04 Dec 2019, 22:15:04+0000'
@vinnybod

This comment has been minimized.

Copy link
Author

@vinnybod vinnybod commented Dec 6, 2019

Realized now you may have addressed this here

@bartoszwalacik

This comment has been minimized.

Copy link
Member

@bartoszwalacik bartoszwalacik commented Dec 6, 2019

right

@bartoszwalacik

This comment has been minimized.

Copy link
Member

@bartoszwalacik bartoszwalacik commented Dec 6, 2019

fixed in 5.8.6

@vinnybod

This comment has been minimized.

Copy link
Author

@vinnybod vinnybod commented Dec 6, 2019

Just tested it out and it worked. Thanks for the fast turnaround @bartoszwalacik !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.