Skip to content

Commit

Permalink
test: create update scenario at 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 b9ca8ba commit ec9f811
Showing 1 changed file with 60 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.lang.reflect.Method;
import java.util.List;

import static org.eclipse.jnosql.mapping.core.query.AnnotationOperation.INSERT;
import static org.eclipse.jnosql.mapping.core.query.AnnotationOperation.UPDATE;
import static org.junit.jupiter.api.Assertions.*;

@ExtendWith(MockitoExtension.class)
Expand All @@ -35,12 +37,21 @@ class AnnotationOperationTest {
private AbstractRepository<Person, Long> repository;


@Test
void shouldReturnInvalidParameter() throws Throwable {
Method method = PersonRepository.class.getDeclaredMethod("invalid", Person.class, Person.class);
Person person = Person.builder().build();
Assertions.assertThatThrownBy(() -> INSERT.invoke(new AnnotationOperation.Operation(method, new Object[]{person, person}, repository)))
.isInstanceOf(UnsupportedOperationException.class);
Assertions.assertThatThrownBy(() -> UPDATE.invoke(new AnnotationOperation.Operation(method, new Object[]{person, person}, repository)))
.isInstanceOf(UnsupportedOperationException.class);
}
@Test
void shouldInsertSingleParameter() throws Throwable {
Method method = PersonRepository.class.getDeclaredMethod("same", Person.class);
Person person = Person.builder().build();
Mockito.when(repository.insert(person)).thenReturn(person);
Object invoked = AnnotationOperation.INSERT.invoke(new AnnotationOperation.Operation(method, new Object[]{person}, repository));
Object invoked = INSERT.invoke(new AnnotationOperation.Operation(method, new Object[]{person}, repository));
Mockito.verify(repository).insert(person);
Assertions.assertThat(person).isEqualTo(invoked);
}
Expand All @@ -50,7 +61,7 @@ void shouldInsertIterableParameter() throws Throwable {
Method method = PersonRepository.class.getDeclaredMethod("iterable", List.class);
Person person = Person.builder().build();
Mockito.when(repository.insertAll(List.of(person))).thenReturn(List.of(person));
Object invoked = AnnotationOperation.INSERT.invoke(new AnnotationOperation.Operation(method, new Object[]{List.of(person)}, repository));
Object invoked = INSERT.invoke(new AnnotationOperation.Operation(method, new Object[]{List.of(person)}, repository));
Mockito.verify(repository).insertAll(List.of(person));
Assertions.assertThat(List.of(person)).isEqualTo(invoked);
}
Expand All @@ -61,7 +72,7 @@ void shouldInsertArrayParameter() throws Throwable {
Method method = PersonRepository.class.getDeclaredMethod("array", Person[].class);
Person person = Person.builder().build();
Mockito.when(repository.insertAll(List.of(person))).thenReturn(List.of(person));
Object invoked = AnnotationOperation.INSERT.invoke(new AnnotationOperation.Operation(method, new Object[]{new Person[]{person}},
Object invoked = INSERT.invoke(new AnnotationOperation.Operation(method, new Object[]{new Person[]{person}},
repository));
Mockito.verify(repository).insertAll(List.of(person));
Assertions.assertThat(List.of(person)).isEqualTo(invoked);
Expand All @@ -73,7 +84,7 @@ void shouldUpdateSingleParameter() throws Throwable {
Method method = PersonRepository.class.getDeclaredMethod("same", Person.class);
Person person = Person.builder().build();
Mockito.when(repository.update(person)).thenReturn(true);
Object invoked = AnnotationOperation.UPDATE.invoke(new AnnotationOperation.Operation(method, new Object[]{person}, repository));
Object invoked = UPDATE.invoke(new AnnotationOperation.Operation(method, new Object[]{person}, repository));
Mockito.verify(repository).update(person);
Assertions.assertThat(person).isEqualTo(invoked);
}
Expand All @@ -83,7 +94,7 @@ void shouldUpdateSingleParameterBoolean() throws Throwable {
Method method = PersonRepository.class.getDeclaredMethod("sameBoolean", Person.class);
Person person = Person.builder().build();
Mockito.when(repository.update(person)).thenReturn(true);
Object invoked = AnnotationOperation.UPDATE.invoke(new AnnotationOperation.Operation(method, new Object[]{person}, repository));
Object invoked = UPDATE.invoke(new AnnotationOperation.Operation(method, new Object[]{person}, repository));
Mockito.verify(repository).update(person);
Assertions.assertThat(invoked).isEqualTo(true);
}
Expand All @@ -93,7 +104,7 @@ void shouldUpdateSingleParameterVoid() throws Throwable {
Method method = PersonRepository.class.getDeclaredMethod("sameVoid", Person.class);
Person person = Person.builder().build();
Mockito.when(repository.update(person)).thenReturn(true);
Object invoked = AnnotationOperation.UPDATE.invoke(new AnnotationOperation.Operation(method, new Object[]{person}, repository));
Object invoked = UPDATE.invoke(new AnnotationOperation.Operation(method, new Object[]{person}, repository));
Mockito.verify(repository).update(person);
Assertions.assertThat(invoked).isEqualTo(Void.TYPE);
}
Expand All @@ -103,7 +114,7 @@ void shouldUpdateSingleParameterInt() throws Throwable {
Method method = PersonRepository.class.getDeclaredMethod("sameInt", Person.class);
Person person = Person.builder().build();
Mockito.when(repository.update(person)).thenReturn(true);
Object invoked = AnnotationOperation.UPDATE.invoke(new AnnotationOperation.Operation(method, new Object[]{person}, repository));
Object invoked = UPDATE.invoke(new AnnotationOperation.Operation(method, new Object[]{person}, repository));
Mockito.verify(repository).update(person);
Assertions.assertThat(invoked).isEqualTo(1);
}
Expand All @@ -113,18 +124,48 @@ void shouldUpdateIterableParameter() throws Throwable {
Method method = PersonRepository.class.getDeclaredMethod("iterable", List.class);
Person person = Person.builder().build();
Mockito.when(repository.updateAll(List.of(person))).thenReturn(1);
Object invoked = AnnotationOperation.UPDATE.invoke(new AnnotationOperation.Operation(method, new Object[]{List.of(person)}, repository));
Object invoked = UPDATE.invoke(new AnnotationOperation.Operation(method, new Object[]{List.of(person)}, repository));
Mockito.verify(repository).updateAll(List.of(person));
Assertions.assertThat(List.of(person)).isEqualTo(invoked);
}

@Test
void shouldUpdateIterableParameterVoid() throws Throwable {
Method method = PersonRepository.class.getDeclaredMethod("iterableVoid", List.class);
Person person = Person.builder().build();
Mockito.when(repository.updateAll(List.of(person))).thenReturn(1);
Object invoked = UPDATE.invoke(new AnnotationOperation.Operation(method, new Object[]{List.of(person)}, repository));
Mockito.verify(repository).updateAll(List.of(person));
Assertions.assertThat(invoked).isEqualTo(Void.TYPE);
}

@Test
void shouldUpdateIterableParameterInt() throws Throwable {
Method method = PersonRepository.class.getDeclaredMethod("iterableInt", List.class);
Person person = Person.builder().build();
Mockito.when(repository.updateAll(List.of(person))).thenReturn(1);
Object invoked = UPDATE.invoke(new AnnotationOperation.Operation(method, new Object[]{List.of(person)}, repository));
Mockito.verify(repository).updateAll(List.of(person));
Assertions.assertThat(invoked).isEqualTo(1);
}

@Test
void shouldUpdateIterableParameterBoolean() throws Throwable {
Method method = PersonRepository.class.getDeclaredMethod("iterableBoolean", List.class);
Person person = Person.builder().build();
Mockito.when(repository.updateAll(List.of(person))).thenReturn(1);
Object invoked = UPDATE.invoke(new AnnotationOperation.Operation(method, new Object[]{List.of(person)}, repository));
Mockito.verify(repository).updateAll(List.of(person));
Assertions.assertThat(invoked).isEqualTo(true);
}


@Test
void shouldUpdateArrayParameter() throws Throwable {
Method method = PersonRepository.class.getDeclaredMethod("array", Person[].class);
Person person = Person.builder().build();
Mockito.when(repository.updateAll(List.of(person))).thenReturn(1);
Object invoked = AnnotationOperation.UPDATE.invoke(new AnnotationOperation.Operation(method, new Object[]{new Person[]{person}},
Object invoked = UPDATE.invoke(new AnnotationOperation.Operation(method, new Object[]{new Person[]{person}},
repository));
Mockito.verify(repository).updateAll(List.of(person));
Assertions.assertThat(new Person[]{person}).isEqualTo(invoked);
Expand All @@ -135,7 +176,7 @@ void shouldUpdateArrayParameterBoolean() throws Throwable {
Method method = PersonRepository.class.getDeclaredMethod("arrayBoolean", Person[].class);
Person person = Person.builder().build();
Mockito.when(repository.updateAll(List.of(person))).thenReturn(1);
Object invoked = AnnotationOperation.UPDATE.invoke(new AnnotationOperation.Operation(method, new Object[]{new Person[]{person}},
Object invoked = UPDATE.invoke(new AnnotationOperation.Operation(method, new Object[]{new Person[]{person}},
repository));
Mockito.verify(repository).updateAll(List.of(person));
Assertions.assertThat(invoked).isEqualTo(true);
Expand All @@ -146,7 +187,7 @@ void shouldUpdateArrayParameterInt() throws Throwable {
Method method = PersonRepository.class.getDeclaredMethod("arrayInt", Person[].class);
Person person = Person.builder().build();
Mockito.when(repository.updateAll(List.of(person))).thenReturn(1);
Object invoked = AnnotationOperation.UPDATE.invoke(new AnnotationOperation.Operation(method, new Object[]{new Person[]{person}},
Object invoked = UPDATE.invoke(new AnnotationOperation.Operation(method, new Object[]{new Person[]{person}},
repository));
Mockito.verify(repository).updateAll(List.of(person));
Assertions.assertThat(invoked).isEqualTo(1);
Expand All @@ -157,7 +198,7 @@ void shouldUpdateArrayParameterVoid() throws Throwable {
Method method = PersonRepository.class.getDeclaredMethod("arrayVoid", Person[].class);
Person person = Person.builder().build();
Mockito.when(repository.updateAll(List.of(person))).thenReturn(1);
Object invoked = AnnotationOperation.UPDATE.invoke(new AnnotationOperation.Operation(method, new Object[]{new Person[]{person}},
Object invoked = UPDATE.invoke(new AnnotationOperation.Operation(method, new Object[]{new Person[]{person}},
repository));
Mockito.verify(repository).updateAll(List.of(person));
Assertions.assertThat(invoked).isEqualTo(Void.TYPE);
Expand All @@ -166,6 +207,7 @@ void shouldUpdateArrayParameterVoid() throws Throwable {

interface PersonRepository extends DataRepository<Person, Long>{

void invalid(Person person, Person person2);
Person same(Person person);

boolean sameBoolean(Person person);
Expand All @@ -183,5 +225,11 @@ interface PersonRepository extends DataRepository<Person, Long>{
int arrayInt(Person[] people);

List<Person> iterable(List<Person> people);

void iterableVoid(List<Person> people);

boolean iterableBoolean(List<Person> people);

int iterableInt(List<Person> people);
}
}

0 comments on commit ec9f811

Please sign in to comment.