From e1c1e92ba9c425ad5b96a9bc88ac135a6a18803a Mon Sep 17 00:00:00 2001 From: Otavio Santana Date: Thu, 28 Dec 2023 19:35:59 +0000 Subject: [PATCH] feat: update annotation operation Signed-off-by: Otavio Santana --- .../core/query/AnnotationOperation.java | 29 +++++++++++++ .../core/query/AnnotationOperationTest.java | 41 +++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/jnosql-mapping/jnosql-mapping-core/src/main/java/org/eclipse/jnosql/mapping/core/query/AnnotationOperation.java b/jnosql-mapping/jnosql-mapping-core/src/main/java/org/eclipse/jnosql/mapping/core/query/AnnotationOperation.java index 87f7cb5f7..34027d794 100644 --- a/jnosql-mapping/jnosql-mapping-core/src/main/java/org/eclipse/jnosql/mapping/core/query/AnnotationOperation.java +++ b/jnosql-mapping/jnosql-mapping-core/src/main/java/org/eclipse/jnosql/mapping/core/query/AnnotationOperation.java @@ -17,6 +17,7 @@ import java.lang.reflect.Method; import java.util.Arrays; import java.util.List; +import java.util.Objects; import java.util.stream.StreamSupport; /** @@ -171,7 +172,35 @@ private static void checkParameterNumber(Operation operation) { public abstract Object invoke(Operation operation); public record Operation(Method method, Object[] params, AbstractRepository repository) { + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Operation operation = (Operation) o; + return Objects.equals(method, operation.method) + && Arrays.equals(params, operation.params) + && Objects.equals(repository, operation.repository); + } + + + @Override + public int hashCode() { + return Objects.hash(method, repository) + 31 * Arrays.hashCode(params); + } + + @Override + public String toString() { + return "Operation{" + + "method=" + method + + ", params=" + Arrays.toString(params) + + ", repository=" + repository + + '}'; + } } diff --git a/jnosql-mapping/jnosql-mapping-core/src/test/java/org/eclipse/jnosql/mapping/core/query/AnnotationOperationTest.java b/jnosql-mapping/jnosql-mapping-core/src/test/java/org/eclipse/jnosql/mapping/core/query/AnnotationOperationTest.java index aa6bbd1aa..dca7a2ab6 100644 --- a/jnosql-mapping/jnosql-mapping-core/src/test/java/org/eclipse/jnosql/mapping/core/query/AnnotationOperationTest.java +++ b/jnosql-mapping/jnosql-mapping-core/src/test/java/org/eclipse/jnosql/mapping/core/query/AnnotationOperationTest.java @@ -348,6 +348,47 @@ void shouldSaveArrayParameter() throws Throwable { Assertions.assertThat(List.of(person)).isEqualTo(invoked); } + @Test + void shouldEqualsAnnotationOperation() throws NoSuchMethodException { + Person person = Person.builder().build(); + Method method = PersonRepository.class.getDeclaredMethod("array", Person[].class); + Object[] params = {new Person[]{person}}; + AnnotationOperation.Operation operation = new AnnotationOperation.Operation(method, params, + repository); + + AnnotationOperation.Operation operation2 = new AnnotationOperation.Operation(method, params, + repository); + + Assertions.assertThat(operation).isEqualTo(operation2); + Assertions.assertThat(operation).isEqualTo(operation); + Assertions.assertThat(operation).isNotEqualTo(123); + } + + @Test + void shouldHashcodeAnnotationOperation() throws NoSuchMethodException { + Person person = Person.builder().build(); + Method method = PersonRepository.class.getDeclaredMethod("array", Person[].class); + Object[] params = {new Person[]{person}}; + AnnotationOperation.Operation operation = new AnnotationOperation.Operation(method, params, + repository); + + AnnotationOperation.Operation operation2 = new AnnotationOperation.Operation(method, params, + repository); + + Assertions.assertThat(operation.hashCode()).isEqualTo(operation2.hashCode()); + } + + @Test + void shouldToStringAnnotationOperation() throws NoSuchMethodException { + Person person = Person.builder().build(); + Method method = PersonRepository.class.getDeclaredMethod("array", Person[].class); + Object[] params = {new Person[]{person}}; + AnnotationOperation.Operation operation = new AnnotationOperation.Operation(method, params, + repository); + + Assertions.assertThat(operation.toString()).isNotEmpty(); + } + interface PersonRepository extends DataRepository{ void invalid(Person person, Person person2);