Skip to content

Commit

Permalink
feat: update annotation operation
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio Santana <otaviopolianasantana@gmail.com>
  • Loading branch information
otaviojava committed Dec 28, 2023
1 parent 8fa5eaa commit e1c1e92
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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 +
'}';
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Person, Long>{

void invalid(Person person, Person person2);
Expand Down

0 comments on commit e1c1e92

Please sign in to comment.