Skip to content

Commit

Permalink
feat: update annotation operation at array
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 0811d09 commit 9166f0b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,50 +47,46 @@ public Object invoke(Operation operation) {
|| operation.method.getReturnType().equals(Boolean.TYPE);
boolean isInt = operation.method.getReturnType().equals(Integer.class)
|| operation.method.getReturnType().equals(Integer.TYPE);
boolean isSame = operation.method.getReturnType().equals(param.getClass());
if (param instanceof Iterable entities) {
return executeIterable(operation, entities, isVoid, isBoolean, isInt, isSame);
return executeIterable(operation, entities, isVoid, isBoolean, isInt, false, null);
} else if (param.getClass().isArray()) {
List<Object> entities = Arrays.asList((Object[]) param);
return executeIterable(operation, entities, isVoid, isBoolean, isInt, isSame);
return executeIterable(operation, entities, isVoid, isBoolean, isInt, true, param);
} else {
return executeSingleEntity(operation, param, isVoid, isBoolean, isInt, isSame);
return executeSingleEntity(operation, param, isVoid, isBoolean, isInt);
}
}

private static Object executeSingleEntity(Operation operation, Object param, boolean isVoid, boolean isBoolean, boolean isInt, boolean isSame) {
boolean result = operation.repository.update(param);
if(isVoid) {
return Void.TYPE;
} else if(isBoolean) {
return result;
} else if(isInt) {
return 1;
} else if(isSame) {
return param;
} else {
throw new UnsupportedOperationException("We don't have support to this return at update operation, please check the method: "
+ operation.method);
}
}

private static Object executeIterable(Operation operation, Iterable entities, boolean isVoid, boolean isBoolean, boolean isInt, boolean isSame) {
private static Object executeIterable(Operation operation, Iterable entities, boolean isVoid, boolean isBoolean,
boolean isInt, boolean isArray, Object param) {
int count = operation.repository.updateAll(entities);
if (isVoid) {
return Void.TYPE;
} else if (isBoolean) {
return true;
} else if (isInt) {
return count;
} else if (isSame) {
return entities;
} else if(isArray){
return param;
} else {
throw new UnsupportedOperationException("We don't have support to this return at update operation, please check the method: "
+ operation.method);
return entities;
}
}
};

private static Object executeSingleEntity(Operation operation, Object param, boolean isVoid, boolean isBoolean, boolean isInt) {
boolean result = operation.repository.update(param);
if(isVoid) {
return Void.TYPE;
} else if(isBoolean) {
return result;
} else if(isInt) {
return 1;
} else {
return param;
}
}

private static void checkParameterNumber(Operation operation) {
if (operation.params.length != 1) {
throw new UnsupportedOperationException("The method operation requires one parameter, please check the method: "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,37 @@ void shouldInsertArrayParameter() throws Throwable {
}


@Test
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));
Mockito.verify(repository).update(person);
Assertions.assertThat(person).isEqualTo(invoked);
}

@Test
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));
Mockito.verify(repository).updateAll(List.of(person));
Assertions.assertThat(List.of(person)).isEqualTo(invoked);
}


@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}},
repository));
Mockito.verify(repository).updateAll(List.of(person));
Assertions.assertThat(new Person[]{person}).isEqualTo(invoked);
}


interface PersonRepository extends DataRepository<Person, Long>{
Expand Down

0 comments on commit 9166f0b

Please sign in to comment.