[FEAT] add capability to check the JPA fields to prevent refacto erors

This commit is contained in:
Edouard DUPIN 2024-12-31 16:14:35 +01:00
parent e3dd58910e
commit 61dde0f0ed
3 changed files with 89 additions and 1 deletions

View File

@ -380,6 +380,19 @@ public class AnnotationTools {
return null;
}
public static boolean hasFieldsName(final Class<?> clazz, final String name) {
for (final Field field : clazz.getFields()) {
// static field is only for internal global declaration ==> remove it ..
if (java.lang.reflect.Modifier.isStatic(field.getModifiers())) {
continue;
}
if (AnnotationTools.getFieldName(field).equals(name)) {
return true;
}
}
return false;
}
public static List<String> getFieldsNames(final Class<?> clazz) {
return getFieldsNamesFilter(clazz, false);
}

View File

@ -52,7 +52,13 @@ public class CheckJPA<T> implements CheckFunctionInterface {
protected Map<String, List<CheckInterface<T>>> checking = null;
protected void add(final String field, final CheckInterface<T> checkFunction) {
protected void add(final String field, final CheckInterface<T> checkFunction) throws DataAccessException {
if (!AnnotationTools.hasFieldsName(this.clazz, field)) {
LOGGER.error("Try to add a JPA Filter on an inexistant Field: '{}' not in {}", field,
AnnotationTools.getAllFieldsNames(this.clazz));
throw new DataAccessException("Try to add a JPA Filter on an inexistant Field: '" + field + "' not in "
+ AnnotationTools.getAllFieldsNames(this.clazz));
}
List<CheckInterface<T>> actions = this.checking.get(field);
if (actions == null) {
actions = new ArrayList<>();

View File

@ -0,0 +1,69 @@
package test.kar.archidata.dataAccess;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.kar.archidata.dataAccess.DBAccess;
import org.kar.archidata.dataAccess.QueryOptions;
import org.kar.archidata.exception.DataAccessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import test.kar.archidata.StepwiseExtension;
import test.kar.archidata.dataAccess.model.DataInJson;
import test.kar.archidata.dataAccess.model.DataInJson.DataInJsonChecker;
@ExtendWith(StepwiseExtension.class)
public class TestJPAChecker {
final static private Logger LOGGER = LoggerFactory.getLogger(TestJPAChecker.class);
class DataInJsonCheckerTest extends DataInJsonChecker {
public void testWithCorrectFieldName() throws Exception {
initialize();
add("data",
(
final DBAccess ioDb,
final String baseName,
final DataInJson data,
final List<String> modifiedValue,
final QueryOptions options) -> {
// nothing to do...
});
}
public void testWithWrongFieldName() throws Exception {
initialize();
add("dqsdfqsdfqsdfqsdfata",
(
final DBAccess ioDb,
final String baseName,
final DataInJson data,
final List<String> modifiedValue,
final QueryOptions options) -> {
// nothing to do...
});
}
}
@Test
public void testThrowWhenFieldDoesNotExist() throws Exception {
final DataInJsonCheckerTest checker = new DataInJsonCheckerTest();
final DataAccessException res = Assertions.assertThrows(DataAccessException.class, () -> {
checker.testWithWrongFieldName();
});
Assertions.assertEquals(res.getMessage(),
"Try to add a JPA Filter on an inexistant Field: 'dqsdfqsdfqsdfqsdfata' not in [data]");
}
@Test
public void testThrowWhenFieldThatExist() throws Exception {
final DataInJsonCheckerTest checker = new DataInJsonCheckerTest();
Assertions.assertDoesNotThrow(() -> {
checker.testWithCorrectFieldName();
});
}
}