From 61dde0f0ed5163439489afb35832847ee8d94164 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Tue, 31 Dec 2024 16:14:35 +0100 Subject: [PATCH] [FEAT] add capability to check the JPA fields to prevent refacto erors --- .../archidata/annotation/AnnotationTools.java | 13 ++++ .../dataAccess/options/CheckJPA.java | 8 ++- .../archidata/dataAccess/TestJPAChecker.java | 69 +++++++++++++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 test/src/test/kar/archidata/dataAccess/TestJPAChecker.java diff --git a/src/org/kar/archidata/annotation/AnnotationTools.java b/src/org/kar/archidata/annotation/AnnotationTools.java index 00515bc..e655737 100644 --- a/src/org/kar/archidata/annotation/AnnotationTools.java +++ b/src/org/kar/archidata/annotation/AnnotationTools.java @@ -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 getFieldsNames(final Class clazz) { return getFieldsNamesFilter(clazz, false); } diff --git a/src/org/kar/archidata/dataAccess/options/CheckJPA.java b/src/org/kar/archidata/dataAccess/options/CheckJPA.java index a5c7dc3..9b2d7ae 100644 --- a/src/org/kar/archidata/dataAccess/options/CheckJPA.java +++ b/src/org/kar/archidata/dataAccess/options/CheckJPA.java @@ -52,7 +52,13 @@ public class CheckJPA implements CheckFunctionInterface { protected Map>> checking = null; - protected void add(final String field, final CheckInterface checkFunction) { + protected void add(final String field, final CheckInterface 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> actions = this.checking.get(field); if (actions == null) { actions = new ArrayList<>(); diff --git a/test/src/test/kar/archidata/dataAccess/TestJPAChecker.java b/test/src/test/kar/archidata/dataAccess/TestJPAChecker.java new file mode 100644 index 0000000..6c54737 --- /dev/null +++ b/test/src/test/kar/archidata/dataAccess/TestJPAChecker.java @@ -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 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 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(); + }); + } + +}