[FEAT] add DataJson(checker ...) availlable in JPA

This commit is contained in:
Edouard DUPIN 2024-12-31 15:59:14 +01:00
parent 841514935c
commit e3dd58910e
4 changed files with 110 additions and 3 deletions

View File

@ -465,6 +465,22 @@ public class CheckJPA<T> implements CheckFunctionInterface {
} else if (type.isEnum()) {
// nothing to do.
}
final DataJson dataJson = AnnotationTools.getDataJson(field);
if (dataJson != null && dataJson.checker() != null) {
final CheckFunctionInterface checkerInstance = dataJson.checker().getDeclaredConstructor()
.newInstance();
add(fieldName,
(
final DBAccess ioDb,
final String baseName,
final T data,
final List<String> modifiedValue,
final QueryOptions options) -> {
// get the field of the specific element
final Object tmpData = field.get(data);
checkerInstance.check(ioDb, baseName, tmpData, null, options);
});
}
// keep this is last ==> take more time...
if (AnnotationTools.isUnique(field)) {
// Create the request ...
@ -498,13 +514,21 @@ public class CheckJPA<T> implements CheckFunctionInterface {
}
}
public void check(final Object data) throws Exception {
check(null, "", data, null, null);
}
public void check(final String baseName, final Object data) throws Exception {
check(null, baseName, data, null, null);
}
public void check(final DBAccess ioDb, final String baseName, final Object data) throws Exception {
check(ioDb, baseName, data, null, null);
}
public void check(final DBAccess ioDb, final String baseName, final Object data, final List<String> filterValue)
public void check(final DBAccess ioDb, final String baseName, final Object data, final List<String> modifiedValue)
throws Exception {
check(ioDb, baseName, data, filterValue, null);
check(ioDb, baseName, data, modifiedValue, null);
}
@Override
@ -512,11 +536,14 @@ public class CheckJPA<T> implements CheckFunctionInterface {
final DBAccess ioDb,
final String baseName,
final Object data,
final List<String> modifiedValue,
List<String> modifiedValue,
final QueryOptions options) throws Exception {
if (this.checking == null) {
initialize();
}
if (modifiedValue == null) {
modifiedValue = AnnotationTools.getAllFieldsNames(this.clazz);
}
if (!(this.clazz.isAssignableFrom(data.getClass()))) {
throw new DataAccessException("Incompatatyble type of Object" + data.getClass().getCanonicalName());
}

View File

@ -0,0 +1,44 @@
package test.kar.archidata.dataAccess;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.kar.archidata.exception.InputException;
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.DataWithSubJson;
import test.kar.archidata.dataAccess.model.DataWithSubJson.DataWithSubJsonChecker;
@ExtendWith(StepwiseExtension.class)
public class TestDataJson {
final static private Logger LOGGER = LoggerFactory.getLogger(TestDataJson.class);
@Test
public void testCheckerDoesNotThrow() throws Exception {
final DataWithSubJson data = new DataWithSubJson();
data.dataSerialized = new DataInJson();
data.dataSerialized.data = "65454";
final DataWithSubJsonChecker checker = new DataWithSubJsonChecker();
Assertions.assertDoesNotThrow(() -> {
checker.check(null, "", data);
});
}
@Test
public void testCheckerDoesThrow() throws Exception {
final DataWithSubJson data = new DataWithSubJson();
data.dataSerialized = new DataInJson();
data.dataSerialized.data = "lqksjdflkjqsdf";
final DataWithSubJsonChecker checker = new DataWithSubJsonChecker();
final InputException res = Assertions.assertThrows(InputException.class, () -> {
checker.check(null, "", data);
});
Assertions.assertEquals(res.getMessage(),
"does not match the required pattern (constraints) must be '^[0-9]+$'");
}
}

View File

@ -0,0 +1,20 @@
package test.kar.archidata.dataAccess.model;
import org.kar.archidata.dataAccess.options.CheckJPA;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
public class DataInJson {
// Simple checker declaration
public static class DataInJsonChecker extends CheckJPA<DataInJson> {
public DataInJsonChecker() {
super(DataInJson.class);
}
}
// Simple data to verify if the checker is active
@Size(min = 3, max = 128)
@Pattern(regexp = "^[0-9]+$")
public String data;
}

View File

@ -0,0 +1,16 @@
package test.kar.archidata.dataAccess.model;
import org.kar.archidata.annotation.DataJson;
import org.kar.archidata.dataAccess.options.CheckJPA;
public class DataWithSubJson {
// Simple checker declaration
public static class DataWithSubJsonChecker extends CheckJPA<DataWithSubJson> {
public DataWithSubJsonChecker() {
super(DataWithSubJson.class);
}
}
@DataJson(checker = DataInJson.DataInJsonChecker.class)
public DataInJson dataSerialized;
}