Skip to content

Instantly share code, notes, and snippets.

@robfletcher
Last active February 25, 2019 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robfletcher/97ad78d06f15c92617d734d227218c01 to your computer and use it in GitHub Desktop.
Save robfletcher/97ad78d06f15c92617d734d227218c01 to your computer and use it in GitHub Desktop.
Demonstrates issue with comparing objects with set properties whose elements are > 1 level deep value objects
import groovy.transform.TupleConstructor
import org.javers.core.JaversBuilder
import org.javers.core.diff.changetype.container.SetChange
import org.javers.core.diff.changetype.container.ValueAdded
import org.javers.core.metamodel.annotation.Id
import spock.lang.Specification
import static java.util.UUID.randomUUID
class SetPropertySpec extends Specification {
def javers = JaversBuilder
.javers()
.registerValueObject(IngressRule)
.registerValueObject(Port)
.build()
def "can detect new element added to set property"() {
given:
def a = new Firewall(
randomUUID(),
"foo",
[
new IngressRule("bar", new Port(80)),
new IngressRule("baz", new Port(80))
].toSet()
)
def b = new Firewall(
a.id,
a.name,
a.ingressRules + new IngressRule("bar", new Port(443))
)
when:
def diff = javers.compare(a, b)
then:
diff.hasChanges()
diff.changes.any { change ->
change instanceof SetChange && change.changes.any { elementChange ->
elementChange instanceof ValueAdded
}
}
}
}
@TupleConstructor
class Firewall {
@Id final UUID id
final String name
final Set<IngressRule> ingressRules
}
@TupleConstructor
class IngressRule {
final String name
final Port port
}
@TupleConstructor
class Port {
final int number
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment