diff --git a/build.gradle b/build.gradle index 22cea029..93953fab 100644 --- a/build.gradle +++ b/build.gradle @@ -1,10 +1,7 @@ -import java.util.stream.Collectors - plugins { id('java') id('maven-publish') - id('com.github.johnrengelman.shadow') version('7.1.2') - id('com.github.breadmoirai.github-release') version('2.4.1') + id('com.github.johnrengelman.shadow') version('5.2.0') } group("$projectGroup") @@ -15,9 +12,14 @@ repositories { } dependencies { + // MongoDB implementation("org.mongodb:mongodb-driver-sync:$mongoDriverVersion") testImplementation("org.mongodb:mongodb-driver-sync:$mongoDriverVersion") + // JetBrains annotations + implementation("org.jetbrains:annotations:$jetbrainsAnnotationsVersion") + testImplementation("org.jetbrains:annotations:$jetbrainsAnnotationsVersion") + // Lombok compileOnly("org.projectlombok:lombok:$lombokVersion") annotationProcessor("org.projectlombok:lombok:$lombokVersion") @@ -35,10 +37,8 @@ test { } java { - sourceCompatibility = JavaVersion.VERSION_17 - targetCompatibility = JavaVersion.VERSION_17 - withJavadocJar() - withSourcesJar() + sourceCompatibility = JavaVersion.VERSION_11 + targetCompatibility = JavaVersion.VERSION_11 } tasks.withType(JavaCompile).configureEach { @@ -46,6 +46,15 @@ tasks.withType(JavaCompile).configureEach { options.encoding = 'UTF-8' } +task sourcesJar(type: Jar) { + from sourceSets.main.allJava + archiveClassifier.set('sources') +} +task javadocJar(type: Jar) { + from javadoc + archiveClassifier.set('javadoc') +} + publishing { repositories { maven { @@ -59,38 +68,12 @@ publishing { publications { maven(MavenPublication) { from components.java + artifact sourcesJar + artifact javadocJar } } } -githubRelease { - token System.getenv('GITHUB_TOKEN') - generateReleaseNotes = true - draft = true - - releaseAssets jar.destinationDir.listFiles() - - body { """\ -## Links to $version - -* [Documentation](https://koboo.gitbook.com/en2do) -* [Maven](https://reposilite.koboo.eu/#/releases/eu/koboo/en2do/$version/) -* [JavaDocs](https://reposilite.koboo.eu/javadoc/releases/eu/koboo/en2do/$version/) -* [Jenkins](https://jenkins.koboo.eu/job/en2do/job/Build%20and%20Publish%20(main)/) - -${ - changelog().call() - .readLines() - .stream() - .limit(10) - .map { "- $it" } - .collect(Collectors.joining('\n', '## Changelog\n', '')) - } -- And more.. -""" } -} - project.tasks.shadowJar.finalizedBy(project.tasks.javadocJar) project.tasks.shadowJar.finalizedBy(project.tasks.sourcesJar) project.tasks.publish.dependsOn(project.tasks.shadowJar) -project.tasks.githubRelease.dependsOn(project.tasks.shadowJar) diff --git a/gradle.properties b/gradle.properties index 6313ad35..c96f485b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,12 +1,13 @@ ### Project properties ### projectGroup=eu.koboo -projectVersion=2.2.0 +projectVersion=2.3.0-SNAPSHOT # ### Dependency versions ### lombokVersion=1.18.24 mongoDriverVersion=4.8.2 jupiterVersion=5.9.2 slf4jVersion=2.0.6 +jetbrainsAnnotationsVersion=24.0.0 # ### Gradle properties ### # diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 070cb702..838e6bc8 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-5.3-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/src/main/java/eu/koboo/en2do/Credentials.java b/src/main/java/eu/koboo/en2do/Credentials.java index a45e567d..4725836f 100644 --- a/src/main/java/eu/koboo/en2do/Credentials.java +++ b/src/main/java/eu/koboo/en2do/Credentials.java @@ -1,5 +1,12 @@ package eu.koboo.en2do; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.experimental.FieldDefaults; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + import java.io.File; import java.io.FileInputStream; import java.io.IOException; @@ -11,34 +18,63 @@ /** * This object is used to simplify creating credentials to the mongodb server. * See documentation: ... - * - * @param connectString The connection string to the mongodb database server - * @param database The database, which should be used */ @SuppressWarnings("unused") -public record Credentials(String connectString, String database) { +@RequiredArgsConstructor +@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true) +@Getter +public class Credentials { + /** + * Empty representation of the credentials object + */ + private static final Credentials EMPTY = new Credentials(null, null); + /** + * The default key of the connection string. + */ private static final String CONNECT_KEY = "en2do.connectstring"; + /** + * The default key of the database. + */ private static final String DATABASE_KEY = "en2do.database"; - + /** + * The default name of the credentials file. + */ private static final String DEFAULT_CREDENTIAL_FILE = "credentials.properties"; - private static Credentials fromStreamProperties(InputStream inputStream) { + /** + * Utility method for reading credentials from an input stream. + * + * @param inputStream The input stream, which should be read. + * @return The new created credentials object. + */ + private static @NotNull Credentials fromStreamProperties(@NotNull InputStream inputStream) { try { Properties properties = new Properties(); properties.load(inputStream); return new Credentials(properties.getProperty(CONNECT_KEY), properties.getProperty(DATABASE_KEY)); } catch (IOException e) { - e.printStackTrace(); + throw new RuntimeException("Error while loading credentials"); } - return null; } - public static Credentials fromResource() { + /** + * Automatically reading credentials from the default resourcePath, which is + * "{applicationJar}/credentials.properties" + * + * @return The new created credentials object. + */ + public static @Nullable Credentials fromResource() { return fromResource("/" + DEFAULT_CREDENTIAL_FILE); } - public static Credentials fromResource(String resourcePath) { + /** + * Automatically reading credentials from a resource file from given resourcePath. + * + * @param resourcePath The resource path with the containing credentials. + * @return The new created credentials object. + */ + public static @Nullable Credentials fromResource(@Nullable String resourcePath) { if (resourcePath == null) { throw new RuntimeException("Couldn't read resource from null path!"); } @@ -48,17 +84,32 @@ public static Credentials fromResource(String resourcePath) { return null; } try (InputStream inputStream = managerClass.getResourceAsStream(resourcePath)) { + if (inputStream == null) { + throw new RuntimeException("Couldn't create a stream from the resource in the path \"" + resourcePath + "\"!"); + } return fromStreamProperties(inputStream); } catch (IOException e) { throw new RuntimeException("Couldn't read resource from path \"" + resourcePath + "\": ", e); } } - public static Credentials fromFile() { + /** + * Automatically reading credentials from the default filePath, which is + * "{applicationDirectory}/credentials.properties" + * + * @return The new created credentials object. + */ + public static @Nullable Credentials fromFile() { return fromFile(DEFAULT_CREDENTIAL_FILE); } - public static Credentials fromFile(String filePath) { + /** + * Automatically reading credentials from a file from given filePath. + * + * @param filePath The file path with the containing credentials. + * @return The new created credentials object. + */ + public static @Nullable Credentials fromFile(@Nullable String filePath) { if (filePath == null) { throw new RuntimeException("Couldn't read file from null path!"); } @@ -73,24 +124,68 @@ public static Credentials fromFile(String filePath) { } } - public static Credentials fromSystemProperties() { + /** + * Automatically reading credentials from the system properties. + * + * @return The new created credentials object. + */ + public static @NotNull Credentials fromSystemProperties() { return fromSystemProperties(CONNECT_KEY, DATABASE_KEY); } - public static Credentials fromSystemProperties(String propertyConnectKey, String propertyDatabaseKey) { + /** + * Automatically reading credentials from the system properties, + * using custom keys for the connectString and database + * + * @param propertyConnectKey The property key for the connection string + * @param propertyDatabaseKey The property key for the database + * @return The new created credentials object. + */ + public static @NotNull Credentials fromSystemProperties(@NotNull String propertyConnectKey, @NotNull String propertyDatabaseKey) { return new Credentials(System.getProperty(propertyConnectKey), System.getProperty(propertyDatabaseKey)); } - public static Credentials fromSystemEnvVars() { + /** + * Automatically reading credentials from the system environmental variables. + * + * @return The new created credentials object. + */ + public static @NotNull Credentials fromSystemEnvVars() { return fromSystemEnvVars(CONNECT_KEY.toUpperCase(Locale.ROOT).replaceFirst("\\.", "_"), DATABASE_KEY.toUpperCase(Locale.ROOT).replaceFirst("\\.", "_")); } - public static Credentials fromSystemEnvVars(String envVarConnectKey, String envVarDatabaseKey) { + /** + * Automatically reading credentials from the system environmental variables. + * using custom keys for the connectString and database + * + * @param envVarConnectKey The environmental variable key for the connection string + * @param envVarDatabaseKey The environmental variable key for the database + * @return The new created credentials object. + */ + public static @NotNull Credentials fromSystemEnvVars(@NotNull String envVarConnectKey, @NotNull String envVarDatabaseKey) { return new Credentials(System.getenv(envVarConnectKey), System.getenv(envVarDatabaseKey)); } - public static Credentials of(String connectString, String database) { + /** + * Create a new credentials object by passing the two values directly. + * + * @param connectString The connection string to the mongodb server. + * @param database The database which should be used. + * @return A new created credentials object. + */ + public static @NotNull Credentials of(@Nullable String connectString, @Nullable String database) { return new Credentials(connectString, database); } + + /** + * The connection string to the mongodb database server + */ + @Nullable + String connectString; + /** + * The database, which should be used + */ + @Nullable + String database; } diff --git a/src/main/java/eu/koboo/en2do/MongoManager.java b/src/main/java/eu/koboo/en2do/MongoManager.java index f5de32a2..0919d235 100644 --- a/src/main/java/eu/koboo/en2do/MongoManager.java +++ b/src/main/java/eu/koboo/en2do/MongoManager.java @@ -31,6 +31,7 @@ import eu.koboo.en2do.repository.entity.compound.Index; import eu.koboo.en2do.repository.entity.ttl.TTLIndex; import eu.koboo.en2do.repository.methods.async.Async; +import eu.koboo.en2do.repository.methods.fields.UpdateBatch; import eu.koboo.en2do.repository.methods.pagination.Pagination; import eu.koboo.en2do.repository.methods.sort.*; import eu.koboo.en2do.repository.methods.transform.Transform; @@ -45,6 +46,8 @@ import org.bson.codecs.pojo.Conventions; import org.bson.codecs.pojo.PojoCodecProvider; import org.bson.conversions.Bson; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.lang.reflect.Field; import java.lang.reflect.Method; @@ -71,21 +74,31 @@ public class MongoManager { // Predefined methods by Java objects // These methods are ignored by our method processing proxy / invocation handler. + @NotNull private static final List IGNORED_DEFAULT_METHODS = Arrays.asList( "notify", "notifyAll", "wait", "finalize", "clone" ); + @NotNull Map, Repository> repositoryRegistry; + + @NotNull Map, RepositoryMeta> repositoryMetaRegistry; + @Nullable ExecutorService executorService; @Getter + @NotNull CodecRegistry codecRegistry; + + @NotNull MongoClient client; + + @NotNull MongoDatabase database; - public MongoManager(Credentials credentials, ExecutorService executorService) { + public MongoManager(@Nullable Credentials credentials, @Nullable ExecutorService executorService) { repositoryRegistry = new ConcurrentHashMap<>(); repositoryMetaRegistry = new ConcurrentHashMap<>(); @@ -105,14 +118,14 @@ public MongoManager(Credentials credentials, ExecutorService executorService) { "accessible credentials."); } - String connectString = credentials.connectString(); + String connectString = credentials.getConnectString(); // If credentials connectString is null, throw exception if (connectString == null) { throw new NullPointerException("No connectString given! Please make sure to provide a " + "accessible connectString."); } // If credentials databaseString is null, throw exception - String databaseString = credentials.database(); + String databaseString = credentials.getDatabase(); if (databaseString == null) { throw new NullPointerException("No databaseString given! Please make sure to provide a " + "accessible databaseString."); @@ -145,7 +158,7 @@ public MongoManager(Credentials credentials, ExecutorService executorService) { database = client.getDatabase(databaseString); } - public MongoManager(Credentials credentials) { + public MongoManager(@Nullable Credentials credentials) { this(credentials, null); } @@ -160,21 +173,15 @@ public boolean close() { public boolean close(boolean shutdownExecutor) { try { - if(executorService != null && shutdownExecutor) { + if (executorService != null && shutdownExecutor) { executorService.shutdown(); } - if (repositoryRegistry != null) { - repositoryRegistry.clear(); - } - if (repositoryMetaRegistry != null) { - for (RepositoryMeta meta : repositoryMetaRegistry.values()) { - meta.destroy(); - } - repositoryMetaRegistry.clear(); - } - if (client != null) { - client.close(); + repositoryRegistry.clear(); + for (RepositoryMeta meta : repositoryMetaRegistry.values()) { + meta.destroy(); } + repositoryMetaRegistry.clear(); + client.close(); return true; } catch (Exception e) { e.printStackTrace(); @@ -183,7 +190,7 @@ public boolean close(boolean shutdownExecutor) { } @SuppressWarnings("unchecked") - public > R create(Class repositoryClass) { + public > @NotNull R create(@NotNull Class repositoryClass) { try { // Check for already created repository to avoid multiply instances of the same repository @@ -288,6 +295,7 @@ public > R create(Class repositoryClass) { repositoryMeta.registerPredefinedMethod(new MethodSaveAll<>(repositoryMeta, entityCollection)); repositoryMeta.registerPredefinedMethod(new MethodSortAll<>(repositoryMeta, entityCollection)); repositoryMeta.registerPredefinedMethod(new MethodToString<>(repositoryMeta, entityCollection)); + repositoryMeta.registerPredefinedMethod(new MethodUpdateAllFields<>(repositoryMeta, entityCollection)); // Iterate through the repository methods for (Method method : repositoryClass.getMethods()) { @@ -363,7 +371,7 @@ public > R create(Class repositoryClass) { for (String filterOperatorString : methodFilterPartArray) { FilterType filterType = createFilterType(entityClass, repositoryClass, method, filterOperatorString, entityFieldSet); - int filterTypeParameterCount = filterType.operator().getExpectedParameterCount(); + int filterTypeParameterCount = filterType.getOperator().getExpectedParameterCount(); for (int i = 0; i < filterTypeParameterCount; i++) { int paramIndex = nextParameterIndex + i; Class paramClass = method.getParameters()[paramIndex].getType(); @@ -372,14 +380,14 @@ public > R create(Class repositoryClass) { method.getParameterCount()); } // Special checks for some operators - Class fieldClass = filterType.field().getType(); - switch (filterType.operator()) { - case REGEX -> { + Class fieldClass = filterType.getField().getType(); + switch (filterType.getOperator()) { + case REGEX: if (GenericUtils.isNotTypeOf(String.class, paramClass) && GenericUtils.isNotTypeOf(Pattern.class, paramClass)) { throw new MethodInvalidRegexParameterException(method, repositoryClass, paramClass); } - } - case IN -> { + break; + case IN: if (GenericUtils.isNotTypeOf(List.class, paramClass)) { throw new MethodMismatchingTypeException(method, repositoryClass, List.class, paramClass); } @@ -387,12 +395,12 @@ public > R create(Class repositoryClass) { if (GenericUtils.isNotTypeOf(fieldClass, listType)) { throw new MethodInvalidListParameterException(method, repositoryClass, fieldClass, listType); } - } - default -> { + break; + default: if (GenericUtils.isNotTypeOf(fieldClass, paramClass)) { throw new MethodMismatchingTypeException(method, repositoryClass, fieldClass, paramClass); } - } + break; } } MethodFilterPart filterPart = new MethodFilterPart(filterType, nextParameterIndex); @@ -430,6 +438,14 @@ public > R create(Class repositoryClass) { throw new MethodParameterCountException(method, repositoryClass, (expectedParameterCount + 1), methodParameterCount); } } + if (lastMethodParameter.isAssignableFrom(UpdateBatch.class)) { + if (methodOperator != MethodOperator.UPDATE_FIELD) { + throw new MethodBatchNotAllowedException(method, repositoryClass); + } + if ((expectedParameterCount + 1) != methodParameterCount) { + throw new MethodParameterCountException(method, repositoryClass, (expectedParameterCount + 1), methodParameterCount); + } + } } else { throw new MethodParameterCountException(method, repositoryClass, expectedParameterCount, methodParameterCount); } @@ -556,12 +572,10 @@ public > R create(Class repositoryClass) { } } - private FilterType createFilterType(Class entityClass, Class repoClass, Method method, - String filterOperatorString, Set fieldSet) throws Exception { + private @NotNull FilterType createFilterType(@NotNull Class entityClass, @NotNull Class repoClass, + @NotNull Method method, @NotNull String filterOperatorString, + @NotNull Set fieldSet) throws Exception { FilterOperator filterOperator = FilterOperator.parseFilterEndsWith(filterOperatorString); - if (filterOperator == null) { - throw new MethodNoFilterOperatorException(method, repoClass); - } String expectedFieldName = filterOperator.removeOperatorFrom(filterOperatorString); boolean notFilter = false; if (expectedFieldName.endsWith("Not")) { diff --git a/src/main/java/eu/koboo/en2do/internal/MethodCallable.java b/src/main/java/eu/koboo/en2do/internal/MethodCallable.java index 82f84100..f3eaedf7 100644 --- a/src/main/java/eu/koboo/en2do/internal/MethodCallable.java +++ b/src/main/java/eu/koboo/en2do/internal/MethodCallable.java @@ -3,10 +3,12 @@ /** * This interface is used to retrieve a return value of the dynamic method */ +@FunctionalInterface public interface MethodCallable { /** * Called to get the object + * * @return The return value of the method * @throws Exception if anything bad happens */ diff --git a/src/main/java/eu/koboo/en2do/internal/RepositoryInvocationHandler.java b/src/main/java/eu/koboo/en2do/internal/RepositoryInvocationHandler.java index 8735ec3b..e69b6fef 100644 --- a/src/main/java/eu/koboo/en2do/internal/RepositoryInvocationHandler.java +++ b/src/main/java/eu/koboo/en2do/internal/RepositoryInvocationHandler.java @@ -2,16 +2,22 @@ import com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; +import com.mongodb.client.model.UpdateOptions; +import com.mongodb.client.result.UpdateResult; import eu.koboo.en2do.internal.exception.methods.MethodUnsupportedException; +import eu.koboo.en2do.internal.exception.repository.RepositoryInvalidCallException; import eu.koboo.en2do.internal.methods.dynamic.DynamicMethod; import eu.koboo.en2do.internal.methods.predefined.PredefinedMethod; import eu.koboo.en2do.repository.Repository; import eu.koboo.en2do.repository.methods.async.Async; +import eu.koboo.en2do.repository.methods.fields.UpdateBatch; import eu.koboo.en2do.repository.methods.transform.Transform; import lombok.AccessLevel; import lombok.AllArgsConstructor; import lombok.experimental.FieldDefaults; import org.bson.conversions.Bson; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; @@ -23,7 +29,10 @@ @AllArgsConstructor public class RepositoryInvocationHandler> implements InvocationHandler { + @NotNull RepositoryMeta repositoryMeta; + + @Nullable ExecutorService executorService; @Override @@ -82,32 +91,40 @@ private Object executeMethod(DynamicMethod dynamicMethod, Object[] arg // Switch-case the method operator to use the correct mongo query. final MongoCollection collection = repositoryMeta.getCollection(); - return switch (dynamicMethod.getMethodOperator()) { - case COUNT -> collection.countDocuments(filter); - case DELETE -> collection.deleteMany(filter).wasAcknowledged(); - case EXISTS -> collection.countDocuments(filter) > 0; - case FIND_MANY -> { - FindIterable findIterable = repositoryMeta.createIterable(filter, methodName); + FindIterable findIterable; + switch (dynamicMethod.getMethodOperator()) { + case COUNT: + return collection.countDocuments(filter); + case DELETE: + return collection.deleteMany(filter).wasAcknowledged(); + case EXISTS: + return collection.countDocuments(filter) > 0; + case FIND_MANY: + findIterable = repositoryMeta.createIterable(filter, methodName); findIterable = repositoryMeta.applySortObject(method, findIterable, arguments); findIterable = repositoryMeta.applySortAnnotations(method, findIterable); - yield findIterable.into(new ArrayList<>()); - } - case FIND_FIRST -> { - FindIterable findIterable = repositoryMeta.createIterable(filter, methodName); + return findIterable.into(new ArrayList<>()); + case FIND_FIRST: + findIterable = repositoryMeta.createIterable(filter, methodName); findIterable = repositoryMeta.applySortObject(method, findIterable, arguments); findIterable = repositoryMeta.applySortAnnotations(method, findIterable); - yield findIterable.limit(1).first(); - } - case PAGE -> { - FindIterable findIterable = repositoryMeta.createIterable(filter, methodName); + return findIterable.limit(1).first(); + case PAGE: + findIterable = repositoryMeta.createIterable(filter, methodName); findIterable = repositoryMeta.applyPageObject(method, findIterable, arguments); - yield findIterable.into(new ArrayList<>()); - } - // Couldn't find any match method operator, but that shouldn't happen - }; + return findIterable.into(new ArrayList<>()); + case UPDATE_FIELD: + UpdateBatch updateBatch = (UpdateBatch) arguments[arguments.length - 1]; + UpdateResult result = collection.updateMany(filter, repositoryMeta.createUpdateDocument(updateBatch), + new UpdateOptions().upsert(false)); + return result.wasAcknowledged(); + default: + // Couldn't find any match method operator, but that shouldn't happen + throw new RepositoryInvalidCallException(method, repositoryMeta.getRepositoryClass()); + } } - private void executeFuture(CompletableFuture future, MethodCallable callable) { + private void executeFuture(@NotNull CompletableFuture future, @NotNull MethodCallable callable) { future.completeAsync(() -> { try { return callable.call(); diff --git a/src/main/java/eu/koboo/en2do/internal/RepositoryMeta.java b/src/main/java/eu/koboo/en2do/internal/RepositoryMeta.java index f8c368d9..b7393855 100644 --- a/src/main/java/eu/koboo/en2do/internal/RepositoryMeta.java +++ b/src/main/java/eu/koboo/en2do/internal/RepositoryMeta.java @@ -12,6 +12,9 @@ import eu.koboo.en2do.repository.AppendMethodAsComment; import eu.koboo.en2do.repository.Repository; import eu.koboo.en2do.repository.SeparateEntityId; +import eu.koboo.en2do.repository.methods.fields.FieldUpdate; +import eu.koboo.en2do.repository.methods.fields.UpdateBatch; +import eu.koboo.en2do.repository.methods.fields.UpdateType; import eu.koboo.en2do.repository.methods.pagination.Pagination; import eu.koboo.en2do.repository.methods.sort.Limit; import eu.koboo.en2do.repository.methods.sort.Skip; @@ -20,7 +23,10 @@ import lombok.AccessLevel; import lombok.Getter; import lombok.experimental.FieldDefaults; +import org.bson.Document; import org.bson.conversions.Bson; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.lang.reflect.Field; import java.lang.reflect.Method; @@ -30,15 +36,25 @@ @Getter public class RepositoryMeta> { + @NotNull String collectionName; + + @NotNull MongoCollection collection; + @NotNull Class repositoryClass; + + @NotNull Class entityClass; + @NotNull Set entityFieldSet; + @NotNull Class entityUniqueIdClass; + + @NotNull Field entityUniqueIdField; @Getter(AccessLevel.NONE) @@ -46,15 +62,17 @@ public class RepositoryMeta> { boolean separateEntityId; @Getter(AccessLevel.NONE) + @NotNull Map> methodRegistry; @Getter(AccessLevel.NONE) + @NotNull Map> dynamicMethodRegistry; - public RepositoryMeta(Class repositoryClass, Class entityClass, - Set entityFieldSet, - Class entityUniqueIdClass, Field entityUniqueIdField, - MongoCollection collection, String collectionName) { + public RepositoryMeta(@NotNull Class repositoryClass, @NotNull Class entityClass, + @NotNull Set entityFieldSet, + @NotNull Class entityUniqueIdClass, @NotNull Field entityUniqueIdField, + @NotNull MongoCollection collection, @NotNull String collectionName) { this.collectionName = collectionName; this.collection = collection; @@ -78,11 +96,11 @@ public void destroy() { dynamicMethodRegistry.clear(); } - public boolean isRepositoryMethod(String methodName) { + public boolean isRepositoryMethod(@NotNull String methodName) { return methodRegistry.containsKey(methodName); } - public void registerPredefinedMethod(PredefinedMethod methodHandler) { + public void registerPredefinedMethod(@NotNull PredefinedMethod methodHandler) { String methodName = methodHandler.getMethodName(); if (methodRegistry.containsKey(methodName)) { throw new RuntimeException("Already registered method with name \"" + methodName + "\"."); @@ -90,11 +108,11 @@ public void registerPredefinedMethod(PredefinedMethod methodHandler) { methodRegistry.put(methodName, methodHandler); } - public PredefinedMethod lookupPredefinedMethod(String methodName) { + public @Nullable PredefinedMethod lookupPredefinedMethod(@NotNull String methodName) { return methodRegistry.get(methodName); } - public void registerDynamicMethod(String methodName, DynamicMethod dynamicMethod) { + public void registerDynamicMethod(@NotNull String methodName, @NotNull DynamicMethod dynamicMethod) { if (dynamicMethodRegistry.containsKey(methodName)) { // Removed regex condition, because the hashmap couldn't handle methods with the same name. throw new RuntimeException("Already registered dynamicMethod with name \"" + methodName + "\"."); @@ -102,12 +120,12 @@ public void registerDynamicMethod(String methodName, DynamicMethod dyn dynamicMethodRegistry.put(methodName, dynamicMethod); } - public DynamicMethod lookupDynamicMethod(String methodName) { + public @Nullable DynamicMethod lookupDynamicMethod(@NotNull String methodName) { return dynamicMethodRegistry.get(methodName); } @SuppressWarnings("unchecked") - public E checkEntity(Method method, Object argument) { + public @NotNull E checkEntity(@NotNull Method method, @Nullable Object argument) { E entity = (E) argument; if (entity == null) { throw new NullPointerException("Entity of type " + entityClass.getName() + " as parameter of method " + @@ -117,7 +135,7 @@ public E checkEntity(Method method, Object argument) { } @SuppressWarnings("unchecked") - public ID checkUniqueId(Method method, Object argument) { + public @NotNull ID checkUniqueId(@NotNull Method method, @Nullable Object argument) { ID uniqueId = (ID) argument; if (uniqueId == null) { throw new NullPointerException("UniqueId of Entity of type " + entityClass.getName() + " as parameter of method " + @@ -127,7 +145,7 @@ public ID checkUniqueId(Method method, Object argument) { } @SuppressWarnings("unchecked") - public List checkEntityList(Method method, Object argument) { + public @NotNull List checkEntityList(@NotNull Method method, @Nullable Object argument) { List entity = (List) argument; if (entity == null) { throw new NullPointerException("List of Entities of type " + entityClass.getName() + " as parameter of method " + @@ -136,11 +154,11 @@ public List checkEntityList(Method method, Object argument) { return entity; } - public ID getUniqueId(E entity) throws IllegalAccessException { + public @Nullable ID getUniqueId(@NotNull E entity) throws IllegalAccessException { return entityUniqueIdClass.cast(entityUniqueIdField.get(entity)); } - public Bson createIdFilter(ID uniqueId) { + public @NotNull Bson createIdFilter(@NotNull ID uniqueId) { if (!separateEntityId) { return Filters.eq("_id", uniqueId); } else { @@ -148,7 +166,15 @@ public Bson createIdFilter(ID uniqueId) { } } - public FindIterable createIterable(Bson filter, String methodName) { + public @NotNull Bson createIdExistsFilter() { + if (!separateEntityId) { + return Filters.exists("_id"); + } else { + return Filters.exists(entityUniqueIdField.getName()); + } + } + + public @NotNull FindIterable createIterable(@Nullable Bson filter, @NotNull String methodName) { FindIterable findIterable; if (filter != null) { findIterable = collection.find(filter); @@ -161,7 +187,9 @@ public FindIterable createIterable(Bson filter, String methodName) { return findIterable; } - public FindIterable applySortObject(Method method, FindIterable findIterable, Object[] args) throws Exception { + public @NotNull FindIterable applySortObject(@NotNull Method method, + @NotNull FindIterable findIterable, + @NotNull Object[] args) throws Exception { int parameterCount = method.getParameterCount(); if (parameterCount <= 0) { return findIterable; @@ -171,9 +199,10 @@ public FindIterable applySortObject(Method method, FindIterable findIterab return findIterable; } Object lastParamObject = args == null ? null : args[args.length - 1]; - if (!(lastParamObject instanceof Sort sortOptions)) { + if (!(lastParamObject instanceof Sort)) { return findIterable; } + Sort sortOptions = (Sort) lastParamObject; if (!sortOptions.getFieldDirectionMap().isEmpty()) { for (Map.Entry byField : sortOptions.getFieldDirectionMap().entrySet()) { findIterable = findIterable.sort(new BasicDBObject(byField.getKey(), byField.getValue())); @@ -197,7 +226,8 @@ public FindIterable applySortObject(Method method, FindIterable findIterab return findIterable; } - public FindIterable applySortAnnotations(Method method, FindIterable findIterable) throws Exception { + public @NotNull FindIterable applySortAnnotations(@NotNull Method method, + @NotNull FindIterable findIterable) throws Exception { SortBy[] sortAnnotations = method.getAnnotationsByType(SortBy.class); if (sortAnnotations != null) { for (SortBy sortBy : sortAnnotations) { @@ -225,35 +255,68 @@ public FindIterable applySortAnnotations(Method method, FindIterable findI return findIterable; } - public FindIterable applyPageObject(Method method, FindIterable findIterable, Object[] args) throws Exception { - int parameterCount = method.getParameterCount(); - if (parameterCount <= 0) { - return findIterable; - } - Class lastParamType = method.getParameterTypes()[method.getParameterCount() - 1]; - if (!lastParamType.isAssignableFrom(Pagination.class)) { - return findIterable; - } - Object lastParamObject = args == null ? null : args[args.length - 1]; - if (!(lastParamObject instanceof Pagination pageObject)) { - return findIterable; - } - if (pageObject.getPage() <= 0) { + public @NotNull FindIterable applyPageObject(@NotNull Method method, + @NotNull FindIterable findIterable, Object[] args) throws Exception { + Pagination pagination = (Pagination) args[args.length - 1]; + if (pagination.getPage() <= 0) { throw new MethodInvalidPageException(method, repositoryClass); } - if (!pageObject.getPageDirectionMap().isEmpty()) { - for (Map.Entry byField : pageObject.getPageDirectionMap().entrySet()) { + if (!pagination.getPageDirectionMap().isEmpty()) { + for (Map.Entry byField : pagination.getPageDirectionMap().entrySet()) { findIterable = findIterable.sort(new BasicDBObject(byField.getKey(), byField.getValue())); } } - int skip = (int) ((pageObject.getPage() - 1) * pageObject.getEntitiesPerPage()); - findIterable = findIterable.limit(pageObject.getEntitiesPerPage()).skip(skip); + int skip = (int) ((pagination.getPage() - 1) * pagination.getEntitiesPerPage()); + findIterable = findIterable.limit(pagination.getEntitiesPerPage()).skip(skip); findIterable.allowDiskUse(true); return findIterable; } - public String getPredefinedNameByAsyncName(String asyncName) { + public @NotNull String getPredefinedNameByAsyncName(@NotNull String asyncName) { String predefinedName = asyncName.replaceFirst("async", ""); return predefinedName.substring(0, 1).toLowerCase(Locale.ROOT) + predefinedName.substring(1); } + + public @NotNull Object getFilterableValue(@NotNull Object object) { + if (object instanceof Enum) { + return ((Enum) object).name(); + } + return object; + } + + public Document createUpdateDocument(UpdateBatch updateBatch) { + Document document = new Document(); + Document fieldSetDocument = new Document(); + Document fieldRenameDocument = new Document(); + Document fieldUnsetDocument = new Document(); + for (FieldUpdate fieldUpdate : updateBatch.getUpdateList()) { + String field = fieldUpdate.getFieldName(); + UpdateType updateType = fieldUpdate.getUpdateType(); + Object filterableValue = null; + if (fieldUpdate.getValue() != null && (updateType == UpdateType.SET || updateType == UpdateType.RENAME)) { + filterableValue = getFilterableValue(fieldUpdate.getValue()); + } + switch (updateType) { + case SET: + fieldSetDocument.append(field, filterableValue); + break; + case RENAME: + fieldRenameDocument.append(field, filterableValue); + break; + case REMOVE: + fieldUnsetDocument.append(field, 0); + break; + } + } + if (!fieldUnsetDocument.isEmpty()) { + document.append("$unset", fieldUnsetDocument); + } + if (!fieldSetDocument.isEmpty()) { + document.append("$set", fieldSetDocument); + } + if (!fieldRenameDocument.isEmpty()) { + document.append("$rename", fieldRenameDocument); + } + return document; + } } diff --git a/src/main/java/eu/koboo/en2do/internal/Validator.java b/src/main/java/eu/koboo/en2do/internal/Validator.java index b02ec4e5..0d84d835 100644 --- a/src/main/java/eu/koboo/en2do/internal/Validator.java +++ b/src/main/java/eu/koboo/en2do/internal/Validator.java @@ -8,6 +8,8 @@ import eu.koboo.en2do.utility.FieldUtils; import lombok.experimental.UtilityClass; import org.bson.codecs.Codec; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.beans.BeanInfo; import java.beans.IntrospectionException; @@ -29,10 +31,11 @@ public class Validator { /** * Returns a codec for the given type class or if no codec is found, it returns null. + * * @param typeClass The type class to search a codec for. * @return The codec if found, otherwise null. */ - private Codec getCodec(Class typeClass) { + private @Nullable Codec getCodec(@NotNull Class typeClass) { try { return MongoClientSettings.getDefaultCodecRegistry().get(typeClass); } catch (Exception ignored) { @@ -42,15 +45,16 @@ private Codec getCodec(Class typeClass) { /** * Validates the compatibility of the given type class + * * @param repositoryClass The class of the repository - * @param typeClass The type class, which should be validated - * @param The generic type of the entity - * @param The generic type of the id of the entity - * @param The generic type of the repository + * @param typeClass The type class, which should be validated + * @param The generic type of the entity + * @param The generic type of the id of the entity + * @param The generic type of the repository * @throws Exception if type class is not valid. */ public > void validateCompatibility( - Class repositoryClass, Class typeClass) throws Exception { + @NotNull Class repositoryClass, @Nullable Class typeClass) throws Exception { if (typeClass == null) { throw new RuntimeException("Class for validation is null! Please open an issue on github!"); } diff --git a/src/main/java/eu/koboo/en2do/internal/codec/InternalPropertyCodecProvider.java b/src/main/java/eu/koboo/en2do/internal/codec/InternalPropertyCodecProvider.java index 142579ef..0e1cd474 100644 --- a/src/main/java/eu/koboo/en2do/internal/codec/InternalPropertyCodecProvider.java +++ b/src/main/java/eu/koboo/en2do/internal/codec/InternalPropertyCodecProvider.java @@ -17,11 +17,11 @@ public class InternalPropertyCodecProvider implements PropertyCodecProvider { /** - * @see PropertyCodecProvider - * @param type the class and bound type parameters for which to get a Codec + * @param type the class and bound type parameters for which to get a Codec * @param registry the registry to use for resolving dependent Codec instances + * @param The type of the codec * @return The codec from the type - * @param The type of the codec + * @see PropertyCodecProvider */ @Override @SuppressWarnings({"rawtypes", "unchecked"}) diff --git a/src/main/java/eu/koboo/en2do/internal/codec/lang/ClassCodec.java b/src/main/java/eu/koboo/en2do/internal/codec/lang/ClassCodec.java index c6345b39..5349fed2 100644 --- a/src/main/java/eu/koboo/en2do/internal/codec/lang/ClassCodec.java +++ b/src/main/java/eu/koboo/en2do/internal/codec/lang/ClassCodec.java @@ -14,10 +14,10 @@ public class ClassCodec implements Codec { /** - * @see org.bson.codecs.Encoder * @param writer the BSON writer to encode into * @param value the value to encode * @param encoderContext the encoder context + * @see org.bson.codecs.Encoder */ @Override public void encode(BsonWriter writer, Class value, EncoderContext encoderContext) { @@ -25,10 +25,10 @@ public void encode(BsonWriter writer, Class value, EncoderContext encoderContext } /** - * @see org.bson.codecs.Decoder * @param reader the BSON reader * @param decoderContext the decoder context * @return the decoded Class + * @see org.bson.codecs.Decoder */ @Override public Class decode(BsonReader reader, DecoderContext decoderContext) { @@ -41,8 +41,8 @@ public Class decode(BsonReader reader, DecoderContext decoderContext) { } /** - * @see org.bson.codecs.Encoder * @return the class of the encoded class + * @see org.bson.codecs.Encoder */ @Override public Class getEncoderClass() { diff --git a/src/main/java/eu/koboo/en2do/internal/codec/map/GenericMapCodec.java b/src/main/java/eu/koboo/en2do/internal/codec/map/GenericMapCodec.java index dc339733..360df95a 100644 --- a/src/main/java/eu/koboo/en2do/internal/codec/map/GenericMapCodec.java +++ b/src/main/java/eu/koboo/en2do/internal/codec/map/GenericMapCodec.java @@ -41,10 +41,10 @@ public GenericMapCodec(Class> encoderClass, Codec keyCodec, Codec map, EncoderContext encoderContext) { @@ -77,10 +77,10 @@ public void encode(BsonWriter writer, Map map, EncoderContext encoderConte } /** - * @see org.bson.codecs.Decoder - * @param reader the BSON reader + * @param reader the BSON reader * @param context the decoder context * @return The decoded map instance + * @see org.bson.codecs.Decoder */ @Override @SuppressWarnings("unchecked") @@ -111,6 +111,7 @@ public Map decode(BsonReader reader, DecoderContext context) { /** * Used to get a new instance of the saved map. + * * @return The new created map instance. */ private Map getInstance() { diff --git a/src/main/java/eu/koboo/en2do/internal/convention/AnnotationConvention.java b/src/main/java/eu/koboo/en2do/internal/convention/AnnotationConvention.java index 6a61acf8..8b0ac5eb 100644 --- a/src/main/java/eu/koboo/en2do/internal/convention/AnnotationConvention.java +++ b/src/main/java/eu/koboo/en2do/internal/convention/AnnotationConvention.java @@ -10,6 +10,8 @@ import org.bson.codecs.pojo.ClassModelBuilder; import org.bson.codecs.pojo.Convention; import org.bson.codecs.pojo.PropertyModelBuilder; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.lang.annotation.Annotation; import java.util.Map; @@ -23,15 +25,17 @@ @RequiredArgsConstructor public class AnnotationConvention implements Convention { + @NotNull Map, RepositoryMeta> repositoryMetaRegistry; /** * This method is used to get the RepositoryMeta object by the given typeClass, * and if non is found, it returns null. + * * @param typeClass The type of RepositoryMeta (should be the entity class) * @return The RepositoryMeta if found, otherwise "null" */ - private RepositoryMeta findRepositoryMetaOf(Class typeClass) { + private @Nullable RepositoryMeta findRepositoryMetaOf(@NotNull Class typeClass) { for (RepositoryMeta meta : repositoryMetaRegistry.values()) { if (!meta.getEntityClass().equals(typeClass)) { continue; @@ -42,8 +46,8 @@ public class AnnotationConvention implements Convention { } /** - * @see Convention * @param classModelBuilder the ClassModelBuilder to apply the convention to + * @see Convention */ @Override public void apply(ClassModelBuilder classModelBuilder) { @@ -53,7 +57,8 @@ public void apply(ClassModelBuilder classModelBuilder) { propertyModelBuilder.readName(null); continue; } - if (readAnnotation instanceof TransformField transformField) { + if (readAnnotation instanceof TransformField) { + TransformField transformField = (TransformField) readAnnotation; propertyModelBuilder.readName(transformField.value()); continue; } @@ -69,7 +74,8 @@ public void apply(ClassModelBuilder classModelBuilder) { propertyModelBuilder.writeName(null); continue; } - if (writeAnnotation instanceof TransformField transformField) { + if (writeAnnotation instanceof TransformField) { + TransformField transformField = (TransformField) writeAnnotation; propertyModelBuilder.writeName(transformField.value()); } } diff --git a/src/main/java/eu/koboo/en2do/internal/exception/methods/MethodBatchNotAllowedException.java b/src/main/java/eu/koboo/en2do/internal/exception/methods/MethodBatchNotAllowedException.java new file mode 100644 index 00000000..32cdd3c7 --- /dev/null +++ b/src/main/java/eu/koboo/en2do/internal/exception/methods/MethodBatchNotAllowedException.java @@ -0,0 +1,13 @@ +package eu.koboo.en2do.internal.exception.methods; + +import eu.koboo.en2do.repository.methods.fields.UpdateBatch; + +import java.lang.reflect.Method; + +public class MethodBatchNotAllowedException extends Exception { + + public MethodBatchNotAllowedException(Method method, Class repoClass) { + super("The method " + method.getName() + " in " + + repoClass.getName() + " is not allowed to have the parameter " + UpdateBatch.class + "!"); + } +} diff --git a/src/main/java/eu/koboo/en2do/internal/exception/methods/MethodBooleanReturnTypeException.java b/src/main/java/eu/koboo/en2do/internal/exception/methods/MethodBooleanReturnTypeException.java index f7655da4..7d1bb03b 100644 --- a/src/main/java/eu/koboo/en2do/internal/exception/methods/MethodBooleanReturnTypeException.java +++ b/src/main/java/eu/koboo/en2do/internal/exception/methods/MethodBooleanReturnTypeException.java @@ -5,7 +5,7 @@ public class MethodBooleanReturnTypeException extends Exception { public MethodBooleanReturnTypeException(Method method, Class repoClass) { - super("Methods, which return " + Boolean.class.getName() + " has to start with keywords \"deleteBy\" or " + - "\"existsBy\"! Please correct the method " + method.getName() + " in " + repoClass.getName() + "."); + super("Methods, which return " + Boolean.class.getName() + " has to start with keywords \"deleteBy\", " + + "\"existsBy\" or \"updateFieldsBy\"! Please correct the method " + method.getName() + " in " + repoClass.getName() + "."); } } diff --git a/src/main/java/eu/koboo/en2do/internal/exception/methods/MethodNoFilterOperatorException.java b/src/main/java/eu/koboo/en2do/internal/exception/methods/MethodNoFilterOperatorException.java deleted file mode 100644 index b628a2f9..00000000 --- a/src/main/java/eu/koboo/en2do/internal/exception/methods/MethodNoFilterOperatorException.java +++ /dev/null @@ -1,10 +0,0 @@ -package eu.koboo.en2do.internal.exception.methods; - -import java.lang.reflect.Method; - -public class MethodNoFilterOperatorException extends Exception { - - public MethodNoFilterOperatorException(Method method, Class repoClass) { - super("Couldn't find filter operator in " + method.getName() + " of " + repoClass.getName() + "!"); - } -} diff --git a/src/main/java/eu/koboo/en2do/internal/methods/dynamic/DynamicMethod.java b/src/main/java/eu/koboo/en2do/internal/methods/dynamic/DynamicMethod.java index 82ebcec5..14c77734 100644 --- a/src/main/java/eu/koboo/en2do/internal/methods/dynamic/DynamicMethod.java +++ b/src/main/java/eu/koboo/en2do/internal/methods/dynamic/DynamicMethod.java @@ -11,6 +11,7 @@ import lombok.RequiredArgsConstructor; import lombok.experimental.FieldDefaults; import org.bson.conversions.Bson; +import org.jetbrains.annotations.NotNull; import java.lang.reflect.Method; import java.util.LinkedList; @@ -21,20 +22,28 @@ @FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true) public class DynamicMethod> { + @NotNull Method method; + + @NotNull RepositoryMeta repositoryMeta; + @Getter + @NotNull MethodOperator methodOperator; + boolean multipleFilter; boolean andFilter; + + @NotNull List filterPartList; - public Bson createBsonFilter(Object[] arguments) throws Exception { + public @NotNull Bson createBsonFilter(@NotNull Object[] arguments) throws Exception { Bson filter; List filterList = new LinkedList<>(); for (MethodFilterPart filterPart : filterPartList) { - FilterType filterType = filterPart.filterType(); - int paramStartIndex = filterPart.nextParameterIndex(); + FilterType filterType = filterPart.getFilterType(); + int paramStartIndex = filterPart.getNextParameterIndex(); Bson processedBsonFilter = processBson(filterType, paramStartIndex, arguments); filterList.add(processedBsonFilter); } @@ -51,70 +60,78 @@ public Bson createBsonFilter(Object[] arguments) throws Exception { } @SuppressWarnings("unchecked") - private Bson processBson(FilterType filterType, int paramsIndexAt, Object[] args) throws Exception { - String fieldName = filterType.field().getName(); + private @NotNull Bson processBson(@NotNull FilterType filterType, int paramsIndexAt, + @NotNull Object[] args) throws Exception { + String fieldName = filterType.getField().getName(); Bson retFilter = null; - switch (filterType.operator()) { - case EQUALS -> retFilter = Filters.eq(fieldName, getFilterableValue(args[paramsIndexAt])); - case EQUALS_IGNORE_CASE -> { - String patternString = "(?i)^" + getFilterableValue(args[paramsIndexAt]) + "$"; - Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE); - retFilter = Filters.regex(fieldName, pattern); - } - case CONTAINS -> { - String patternString = ".*" + getFilterableValue(args[paramsIndexAt]) + ".*"; - Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE); - retFilter = Filters.regex(fieldName, pattern); - } - case GREATER_THAN -> retFilter = Filters.gt(fieldName, getFilterableValue(args[paramsIndexAt])); - case LESS_THAN -> retFilter = Filters.lt(fieldName, getFilterableValue(args[paramsIndexAt])); - case GREATER_EQUALS -> retFilter = Filters.gte(fieldName, getFilterableValue(args[paramsIndexAt])); - case LESS_EQUALS -> retFilter = Filters.lte(fieldName, getFilterableValue(args[paramsIndexAt])); - case REGEX -> { + switch (filterType.getOperator()) { + case EQUALS: + retFilter = Filters.eq(fieldName, repositoryMeta.getFilterableValue(args[paramsIndexAt])); + break; + case EQUALS_IGNORE_CASE: + String ignCasePatternString = "(?i)^" + repositoryMeta.getFilterableValue(args[paramsIndexAt]) + "$"; + Pattern ignCasePattern = Pattern.compile(ignCasePatternString, Pattern.CASE_INSENSITIVE); + retFilter = Filters.regex(fieldName, ignCasePattern); + break; + case CONTAINS: + String containsPatternString = ".*" + repositoryMeta.getFilterableValue(args[paramsIndexAt]) + ".*"; + Pattern containsPattern = Pattern.compile(containsPatternString, Pattern.CASE_INSENSITIVE); + retFilter = Filters.regex(fieldName, containsPattern); + break; + case GREATER_THAN: + retFilter = Filters.gt(fieldName, repositoryMeta.getFilterableValue(args[paramsIndexAt])); + break; + case LESS_THAN: + retFilter = Filters.lt(fieldName, repositoryMeta.getFilterableValue(args[paramsIndexAt])); + break; + case GREATER_EQUALS: + retFilter = Filters.gte(fieldName, repositoryMeta.getFilterableValue(args[paramsIndexAt])); + break; + case LESS_EQUALS: + retFilter = Filters.lte(fieldName, repositoryMeta.getFilterableValue(args[paramsIndexAt])); + break; + case REGEX: // MongoDB supports multiple types of regex filtering, so check which type is provided. - Object value = getFilterableValue(args[paramsIndexAt]); - if (value instanceof String patternString) { - retFilter = Filters.regex(fieldName, patternString); + Object value = repositoryMeta.getFilterableValue(args[paramsIndexAt]); + if (value instanceof String) { + String regexPatternString = (String) value; + retFilter = Filters.regex(fieldName, regexPatternString); } - if (value instanceof Pattern pattern) { - retFilter = Filters.regex(fieldName, pattern); + if (value instanceof Pattern) { + Pattern regexPattern = (Pattern) value; + retFilter = Filters.regex(fieldName, regexPattern); } if (retFilter == null) { throw new MethodInvalidRegexParameterException(method, repositoryMeta.getRepositoryClass(), value.getClass()); } - } - case EXISTS -> retFilter = Filters.exists(fieldName); - case BETWEEN -> { - Object from = getFilterableValue(args[paramsIndexAt]); - Object to = args[paramsIndexAt + 1]; - retFilter = Filters.and(Filters.gt(fieldName, from), Filters.lt(fieldName, to)); - } - case BETWEEN_EQUALS -> { - Object from = getFilterableValue(args[paramsIndexAt]); - Object to = args[paramsIndexAt + 1]; - retFilter = Filters.and(Filters.gte(fieldName, from), Filters.lte(fieldName, to)); - } - case IN -> { + break; + case EXISTS: + retFilter = Filters.exists(fieldName); + break; + case BETWEEN: + Object betweenStart = repositoryMeta.getFilterableValue(args[paramsIndexAt]); + Object betweenEnd = args[paramsIndexAt + 1]; + retFilter = Filters.and(Filters.gt(fieldName, betweenStart), Filters.lt(fieldName, betweenEnd)); + break; + case BETWEEN_EQUALS: + Object betweenEqStart = repositoryMeta.getFilterableValue(args[paramsIndexAt]); + Object betweenEqEnd = args[paramsIndexAt + 1]; + retFilter = Filters.and(Filters.gte(fieldName, betweenEqStart), Filters.lte(fieldName, betweenEqEnd)); + break; + case IN: // MongoDB expects an Array and not a List, but for easier usage // the framework wants a list. So just convert the list to an array and pass it to the filter - List objectList = (List) getFilterableValue(args[paramsIndexAt]); + List objectList = (List) repositoryMeta.getFilterableValue(args[paramsIndexAt]); Object[] objectArray = objectList.toArray(new Object[]{}); retFilter = Filters.in(fieldName, objectArray); - } - default -> // This filter is not supported. Throw exception. - throw new MethodUnsupportedFilterException(method, repositoryMeta.getRepositoryClass()); + break; + default: // This filter is not supported. Throw exception. + throw new MethodUnsupportedFilterException(method, repositoryMeta.getRepositoryClass()); } // Applying negotiating of the filter, if needed - if (filterType.notFilter()) { + if (filterType.isNotFilter()) { return Filters.not(retFilter); } return retFilter; } - - private Object getFilterableValue(Object object) { - if (object instanceof Enum) { - return ((Enum) object).name(); - } - return object; - } -} +} \ No newline at end of file diff --git a/src/main/java/eu/koboo/en2do/internal/methods/dynamic/FilterType.java b/src/main/java/eu/koboo/en2do/internal/methods/dynamic/FilterType.java index c2ba1be2..6690869d 100644 --- a/src/main/java/eu/koboo/en2do/internal/methods/dynamic/FilterType.java +++ b/src/main/java/eu/koboo/en2do/internal/methods/dynamic/FilterType.java @@ -1,14 +1,34 @@ package eu.koboo.en2do.internal.methods.dynamic; import eu.koboo.en2do.internal.methods.operators.FilterOperator; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.experimental.FieldDefaults; +import org.jetbrains.annotations.NotNull; import java.lang.reflect.Field; /** * Represents a segment of a method filter. - * @param field The field, which should be filtered - * @param notFilter is true if the filter is negotiated - * @param operator The operator of the filter */ -public record FilterType(Field field, boolean notFilter, FilterOperator operator) { +@RequiredArgsConstructor +@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true) +@Getter +public class FilterType { + + /** + * The field, which should be filtered + */ + @NotNull + Field field; + /** + * is true if the filter is negotiated + */ + boolean notFilter; + /** + * The operator of the filter + */ + @NotNull + FilterOperator operator; } diff --git a/src/main/java/eu/koboo/en2do/internal/methods/dynamic/MethodFilterPart.java b/src/main/java/eu/koboo/en2do/internal/methods/dynamic/MethodFilterPart.java index 8f0456cb..58180737 100644 --- a/src/main/java/eu/koboo/en2do/internal/methods/dynamic/MethodFilterPart.java +++ b/src/main/java/eu/koboo/en2do/internal/methods/dynamic/MethodFilterPart.java @@ -1,9 +1,26 @@ package eu.koboo.en2do.internal.methods.dynamic; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.experimental.FieldDefaults; +import org.jetbrains.annotations.NotNull; + /** * Represents a filter part of the method. - * @param filterType The type of the filter - * @param nextParameterIndex The parameter index */ -public record MethodFilterPart(FilterType filterType, int nextParameterIndex) { +@RequiredArgsConstructor +@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true) +@Getter +public class MethodFilterPart { + + /** + * The type of the filter + */ + @NotNull + FilterType filterType; + /** + * The parameter index + */ + int nextParameterIndex; } diff --git a/src/main/java/eu/koboo/en2do/internal/methods/operators/FilterOperator.java b/src/main/java/eu/koboo/en2do/internal/methods/operators/FilterOperator.java index 41bce22d..7b9e41be 100644 --- a/src/main/java/eu/koboo/en2do/internal/methods/operators/FilterOperator.java +++ b/src/main/java/eu/koboo/en2do/internal/methods/operators/FilterOperator.java @@ -4,23 +4,92 @@ import lombok.AllArgsConstructor; import lombok.Getter; import lombok.experimental.FieldDefaults; +import org.jetbrains.annotations.NotNull; +import java.util.regex.Pattern; + +/** + * Represents a segment of the whole method filters. + */ @AllArgsConstructor @Getter @FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true) public enum FilterOperator { + /** + * Represents Filters.eq(..) + * + * @see com.mongodb.client.model.Filters#eq(Object) + */ EQUALS("", 1), + /** + * Represents Filters.regex("(?i)^[value]$") + * + * @see com.mongodb.client.model.Filters#regex(String, String) + */ EQUALS_IGNORE_CASE("Ign", 1), + /** + * Represents Filters.gt(..) + * + * @see com.mongodb.client.model.Filters#gt(String, Object) + */ GREATER_THAN("GreaterThan", 1), + /** + * Represents Filters.lt(..) + * + * @see com.mongodb.client.model.Filters#lt(String, Object) + */ LESS_THAN("LessThan", 1), + /** + * Represents Filters.regex(..) + * + * @see com.mongodb.client.model.Filters#regex(String, String) + * @see com.mongodb.client.model.Filters#regex(String, Pattern) + */ REGEX("Regex", 1), + /** + * Represents Filters.gte(..) + * + * @see com.mongodb.client.model.Filters#gte(String, Object) + */ GREATER_EQUALS("GreaterEq", 1), + /** + * Represents Filters.lte(..) + * + * @see com.mongodb.client.model.Filters#lte(String, Object) + */ LESS_EQUALS("LessEq", 1), + /** + * Represents Filters.exists(..) + * + * @see com.mongodb.client.model.Filters#exists(String) + */ EXISTS("Exists", 0), + /** + * Represents Filters.regex(".*[value].*") + * + * @see com.mongodb.client.model.Filters#regex(String, String) + */ CONTAINS("Contains", 1), + /** + * Represents Filters.gt(..) + Filters.lt(..) + * + * @see com.mongodb.client.model.Filters#gt(String, Object) + * @see com.mongodb.client.model.Filters#lt(String, Object) + */ BETWEEN("Between", 2), + /** + * Represents Filters.gte(..) + Filters.lte(..) + * + * @see com.mongodb.client.model.Filters#gte(String, Object) + * @see com.mongodb.client.model.Filters#lte(String, Object) + */ BETWEEN_EQUALS("BetweenEq", 2), + /** + * Represents Filters.in(..) + * + * @see com.mongodb.client.model.Filters#in(String, Object[]) + */ IN("In", 1); public static final FilterOperator[] VALUES = FilterOperator.values(); @@ -28,11 +97,24 @@ public enum FilterOperator { String keyword; int expectedParameterCount; - public String removeOperatorFrom(String textWithOperator) { + /** + * Removes the filter operator from end of the given text and returns it. + * + * @param textWithOperator The text, which has to end with the filter operator. + * @return The text without the filter operator. + */ + public @NotNull String removeOperatorFrom(@NotNull String textWithOperator) { return textWithOperator.substring(0, textWithOperator.length() - getKeyword().length()); } - public static FilterOperator parseFilterEndsWith(String methodNamePart) { + /** + * Parses a string to a filter operator by checking, if the text is ending with the + * keyword of the filter operator, and returns the results. + * + * @param methodNamePart The text, which should be parsed. + * @return The FilterOperator if any found, otherwise it fallbacks to EQUALS. + */ + public static @NotNull FilterOperator parseFilterEndsWith(@NotNull String methodNamePart) { for (FilterOperator operator : VALUES) { if (operator == EQUALS) { continue; diff --git a/src/main/java/eu/koboo/en2do/internal/methods/operators/MethodOperator.java b/src/main/java/eu/koboo/en2do/internal/methods/operators/MethodOperator.java index 70c98bff..2f5c5fda 100644 --- a/src/main/java/eu/koboo/en2do/internal/methods/operators/MethodOperator.java +++ b/src/main/java/eu/koboo/en2do/internal/methods/operators/MethodOperator.java @@ -6,19 +6,30 @@ import lombok.AllArgsConstructor; import lombok.Getter; import lombok.experimental.FieldDefaults; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.lang.reflect.Method; import java.util.List; +/** + * Represents the MethodOperator of a method inside a repository. + */ @AllArgsConstructor @FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true) public enum MethodOperator { + /** + * Searches the first entity with the given filters. + */ FIND_FIRST("findFirstBy", (method, returnType, entityClass, repoClass) -> { if (GenericUtils.isNotTypeOf(entityClass, returnType)) { throw new MethodFindReturnTypeException(method, entityClass, repoClass); } }), + /** + * Searches all entities with the given filters. + */ FIND_MANY("findManyBy", (method, returnType, entityClass, repoClass) -> { if (GenericUtils.isNotTypeOf(List.class, returnType)) { throw new MethodFindListReturnTypeException(method, entityClass, repoClass); @@ -28,21 +39,33 @@ public enum MethodOperator { throw new MethodFindListTypeException(method, repoClass, listType); } }), + /** + * Deletes all entities with the given filters. + */ DELETE("deleteBy", (method, returnType, entityClass, repoClass) -> { if (GenericUtils.isNotTypeOf(Boolean.class, returnType)) { throw new MethodBooleanReturnTypeException(method, repoClass); } }), + /** + * Checks if any entity exists with the given filters. + */ EXISTS("existsBy", (method, returnType, entityClass, repoClass) -> { if (GenericUtils.isNotTypeOf(Boolean.class, returnType)) { throw new MethodBooleanReturnTypeException(method, repoClass); } }), + /** + * Counts all entities with the given filters. + */ COUNT("countBy", (method, returnType, entityClass, repoClass) -> { if (GenericUtils.isNotTypeOf(Long.class, returnType)) { throw new MethodLongReturnTypeException(method, repoClass); } }), + /** + * Creates pagination on all entities with the given filters. + */ PAGE("pageBy", (method, returnType, entityClass, repoClass) -> { if (GenericUtils.isNotTypeOf(List.class, returnType)) { throw new MethodFindListReturnTypeException(method, entityClass, repoClass); @@ -51,6 +74,14 @@ public enum MethodOperator { if (!listType.isAssignableFrom(entityClass)) { throw new MethodFindListTypeException(method, repoClass, listType); } + }), + /** + * Updates specific fields on all entities with the given filters. + */ + UPDATE_FIELD("updateFieldsBy", (method, returnType, entityClass, repoClass) -> { + if (GenericUtils.isNotTypeOf(Boolean.class, returnType)) { + throw new MethodBooleanReturnTypeException(method, repoClass); + } }); public static final MethodOperator[] VALUES = MethodOperator.values(); @@ -59,15 +90,38 @@ public enum MethodOperator { String keyword; ReturnTypeValidator returnTypeValidator; - public String removeOperatorFrom(String textWithOperator) { + /** + * Replaces the method operator keyword from the given text and returns it. + * + * @param textWithOperator The text, with the method operator at the start + * @return The text, without the method operator + */ + public @NotNull String removeOperatorFrom(@NotNull String textWithOperator) { return textWithOperator.replaceFirst(getKeyword(), ""); } - public void validate(Method method, Class returnType, Class entityClass, Class repoClass) throws Exception { + /** + * Validates the return type of the specific method operator, using the given parameters. + * + * @param method The method, which should be validated + * @param returnType, The return type of the method (Could be overridden, due to async methods) + * @param entityClass The entity class of the validated repository + * @param repoClass THe repository classs + * @throws Exception if the validation is unsuccessful. + */ + public void validate(@NotNull Method method, @NotNull Class returnType, + @NotNull Class entityClass, @NotNull Class repoClass) throws Exception { returnTypeValidator.check(method, returnType, entityClass, repoClass); } - public static MethodOperator parseMethodStartsWith(String methodNamePart) { + /** + * Parses the method operator by the name of the method. It just checks if the method is starting + * with any method operator of the enumeration. + * + * @param methodNamePart The name of the method. + * @return The MethodOperator if any is found, otherwise null. + */ + public static @Nullable MethodOperator parseMethodStartsWith(@NotNull String methodNamePart) { for (MethodOperator operator : VALUES) { if (!methodNamePart.startsWith(operator.getKeyword())) { continue; diff --git a/src/main/java/eu/koboo/en2do/internal/methods/operators/ReturnTypeValidator.java b/src/main/java/eu/koboo/en2do/internal/methods/operators/ReturnTypeValidator.java index 56b584c1..f291804d 100644 --- a/src/main/java/eu/koboo/en2do/internal/methods/operators/ReturnTypeValidator.java +++ b/src/main/java/eu/koboo/en2do/internal/methods/operators/ReturnTypeValidator.java @@ -5,14 +5,16 @@ /** * Represents the validation of the return type from a method. */ +@FunctionalInterface public interface ReturnTypeValidator { /** * Gets executed on the validation of the repository. - * @param method The method, which should be validated - * @param returnType The return type, of the method + * + * @param method The method, which should be validated + * @param returnType The return type, of the method * @param entityClass The class of the entity of the repository - * @param repoClass The class of the repository + * @param repoClass The class of the repository * @throws Exception if return type isn't valid */ void check(Method method, Class returnType, Class entityClass, Class repoClass) throws Exception; diff --git a/src/main/java/eu/koboo/en2do/internal/methods/predefined/PredefinedMethod.java b/src/main/java/eu/koboo/en2do/internal/methods/predefined/PredefinedMethod.java index 35fc1092..b04c59ed 100644 --- a/src/main/java/eu/koboo/en2do/internal/methods/predefined/PredefinedMethod.java +++ b/src/main/java/eu/koboo/en2do/internal/methods/predefined/PredefinedMethod.java @@ -7,14 +7,17 @@ import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.experimental.FieldDefaults; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.lang.reflect.Method; /** * This class is a representation of a predefined method from the repository - * @param The generic type of the entity + * + * @param The generic type of the entity * @param The generic type of the id of the entity - * @param The generic type of the repository + * @param The generic type of the repository */ @FieldDefaults(level = AccessLevel.PROTECTED, makeFinal = true) @RequiredArgsConstructor @@ -27,10 +30,11 @@ public abstract class PredefinedMethod> { /** * Invokes the method and returns the created object. - * @param method The method, which should be invoked + * + * @param method The method, which should be invoked * @param arguments The object array, which represents the arguments of the method * @return The object created by the method invocation * @throws Exception any, if something bad happens */ - public abstract Object handle(Method method, Object[] arguments) throws Exception; + public abstract @Nullable Object handle(@NotNull Method method, @NotNull Object[] arguments) throws Exception; } diff --git a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodCountAll.java b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodCountAll.java index 34f8b219..414cc852 100644 --- a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodCountAll.java +++ b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodCountAll.java @@ -4,6 +4,8 @@ import eu.koboo.en2do.internal.RepositoryMeta; import eu.koboo.en2do.internal.methods.predefined.PredefinedMethod; import eu.koboo.en2do.repository.Repository; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.lang.reflect.Method; @@ -14,7 +16,7 @@ public MethodCountAll(RepositoryMeta meta, MongoCollection entityCo } @Override - public Object handle(Method method, Object[] arguments) throws Exception { + public @Nullable Object handle(@NotNull Method method, @NotNull Object[] arguments) throws Exception { return entityCollection.countDocuments(); } } diff --git a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodDelete.java b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodDelete.java index f1309734..9ebadcef 100644 --- a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodDelete.java +++ b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodDelete.java @@ -6,6 +6,8 @@ import eu.koboo.en2do.internal.methods.predefined.PredefinedMethod; import eu.koboo.en2do.repository.Repository; import org.bson.conversions.Bson; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.lang.reflect.Method; @@ -16,7 +18,7 @@ public MethodDelete(RepositoryMeta meta, MongoCollection entityColl } @Override - public Object handle(Method method, Object[] arguments) throws Exception { + public @Nullable Object handle(@NotNull Method method, @NotNull Object[] arguments) throws Exception { E entity = repositoryMeta.checkEntity(method, arguments[0]); ID uniqueId = repositoryMeta.checkUniqueId(method, repositoryMeta.getUniqueId(entity)); Bson idFilter = repositoryMeta.createIdFilter(uniqueId); diff --git a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodDeleteAll.java b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodDeleteAll.java index 73bcb539..4251490a 100644 --- a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodDeleteAll.java +++ b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodDeleteAll.java @@ -5,6 +5,8 @@ import eu.koboo.en2do.internal.methods.predefined.PredefinedMethod; import eu.koboo.en2do.repository.Repository; import org.bson.conversions.Bson; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.lang.reflect.Method; import java.util.List; @@ -16,7 +18,8 @@ public MethodDeleteAll(RepositoryMeta meta, MongoCollection entityC } @Override - public Object handle(Method method, Object[] arguments) throws Exception { + public @Nullable Object handle(@NotNull Method method, @NotNull Object[] arguments) throws Exception { + // TODO: Check for Id in check instead of deleting every single entity itself. List entityList = repositoryMeta.checkEntityList(method, arguments[0]); for (E entity : entityList) { ID uniqueId = repositoryMeta.checkUniqueId(method, repositoryMeta.getUniqueId(entity)); diff --git a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodDeleteById.java b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodDeleteById.java index b7e40926..54ee7074 100644 --- a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodDeleteById.java +++ b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodDeleteById.java @@ -6,6 +6,8 @@ import eu.koboo.en2do.internal.methods.predefined.PredefinedMethod; import eu.koboo.en2do.repository.Repository; import org.bson.conversions.Bson; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.lang.reflect.Method; @@ -16,7 +18,7 @@ public MethodDeleteById(RepositoryMeta meta, MongoCollection entity } @Override - public Object handle(Method method, Object[] arguments) throws Exception { + public @Nullable Object handle(@NotNull Method method, @NotNull Object[] arguments) throws Exception { ID uniqueId = repositoryMeta.checkUniqueId(method, arguments[0]); Bson idFilter = repositoryMeta.createIdFilter(uniqueId); DeleteResult result = entityCollection.deleteOne(idFilter); diff --git a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodDrop.java b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodDrop.java index 495bcd4f..d9deef90 100644 --- a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodDrop.java +++ b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodDrop.java @@ -4,6 +4,8 @@ import eu.koboo.en2do.internal.RepositoryMeta; import eu.koboo.en2do.internal.methods.predefined.PredefinedMethod; import eu.koboo.en2do.repository.Repository; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.lang.reflect.Method; @@ -14,7 +16,7 @@ public MethodDrop(RepositoryMeta meta, MongoCollection entityCollec } @Override - public Object handle(Method method, Object[] arguments) throws Exception { + public @Nullable Object handle(@NotNull Method method, @NotNull Object[] arguments) throws Exception { entityCollection.drop(); return true; } diff --git a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodEquals.java b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodEquals.java index 1db79b7d..293dd01f 100644 --- a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodEquals.java +++ b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodEquals.java @@ -4,6 +4,8 @@ import eu.koboo.en2do.internal.RepositoryMeta; import eu.koboo.en2do.internal.methods.predefined.PredefinedMethod; import eu.koboo.en2do.repository.Repository; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.lang.reflect.Method; @@ -14,14 +16,15 @@ public MethodEquals(RepositoryMeta meta, MongoCollection entityColl } @Override - public Object handle(Method method, Object[] arguments) throws Exception { - Object object = arguments[0]; - if (object == null) { + public @Nullable Object handle(@NotNull Method method, @NotNull Object[] arguments) throws Exception { + if (arguments.length != 1) { return false; } - if (!(object instanceof Repository repository)) { + Object object = arguments[0]; + if (!(object instanceof Repository)) { return false; } + Repository repository = (Repository) object; return repository.getClass().getName().equalsIgnoreCase(repositoryMeta.getClass().getName()); } } diff --git a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodExists.java b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodExists.java index b7ef5a73..f3da75b0 100644 --- a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodExists.java +++ b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodExists.java @@ -5,6 +5,8 @@ import eu.koboo.en2do.internal.methods.predefined.PredefinedMethod; import eu.koboo.en2do.repository.Repository; import org.bson.conversions.Bson; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.lang.reflect.Method; @@ -15,7 +17,7 @@ public MethodExists(RepositoryMeta meta, MongoCollection entityColl } @Override - public Object handle(Method method, Object[] arguments) throws Exception { + public @Nullable Object handle(@NotNull Method method, @NotNull Object[] arguments) throws Exception { E entity = repositoryMeta.checkEntity(method, arguments[0]); ID uniqueId = repositoryMeta.checkUniqueId(method, repositoryMeta.getUniqueId(entity)); Bson idFilter = repositoryMeta.createIdFilter(uniqueId); diff --git a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodExistsById.java b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodExistsById.java index a2710f2d..ae9e4734 100644 --- a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodExistsById.java +++ b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodExistsById.java @@ -5,6 +5,8 @@ import eu.koboo.en2do.internal.methods.predefined.PredefinedMethod; import eu.koboo.en2do.repository.Repository; import org.bson.conversions.Bson; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.lang.reflect.Method; @@ -15,7 +17,7 @@ public MethodExistsById(RepositoryMeta meta, MongoCollection entity } @Override - public Object handle(Method method, Object[] arguments) throws Exception { + public @Nullable Object handle(@NotNull Method method, @NotNull Object[] arguments) throws Exception { ID uniqueId = repositoryMeta.checkUniqueId(method, arguments[0]); Bson idFilter = repositoryMeta.createIdFilter(uniqueId); return entityCollection.countDocuments(idFilter) > 0; diff --git a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodFindAll.java b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodFindAll.java index dae1367d..cfe1bf44 100644 --- a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodFindAll.java +++ b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodFindAll.java @@ -5,6 +5,8 @@ import eu.koboo.en2do.internal.RepositoryMeta; import eu.koboo.en2do.internal.methods.predefined.PredefinedMethod; import eu.koboo.en2do.repository.Repository; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.lang.reflect.Method; import java.util.ArrayList; @@ -16,7 +18,7 @@ public MethodFindAll(RepositoryMeta meta, MongoCollection entityCol } @Override - public Object handle(Method method, Object[] arguments) throws Exception { + public @Nullable Object handle(@NotNull Method method, @NotNull Object[] arguments) throws Exception { FindIterable findIterable = repositoryMeta.createIterable(null, methodName); return findIterable.into(new ArrayList<>()); } diff --git a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodFindFirstById.java b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodFindFirstById.java index 02d629dd..794d78db 100644 --- a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodFindFirstById.java +++ b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodFindFirstById.java @@ -6,6 +6,8 @@ import eu.koboo.en2do.internal.methods.predefined.PredefinedMethod; import eu.koboo.en2do.repository.Repository; import org.bson.conversions.Bson; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.lang.reflect.Method; @@ -16,7 +18,7 @@ public MethodFindFirstById(RepositoryMeta meta, MongoCollection ent } @Override - public Object handle(Method method, Object[] arguments) throws Exception { + public @Nullable Object handle(@NotNull Method method, @NotNull Object[] arguments) throws Exception { ID uniqueId = repositoryMeta.checkUniqueId(method, arguments[0]); Bson idFilter = repositoryMeta.createIdFilter(uniqueId); FindIterable findIterable = repositoryMeta.createIterable(idFilter, methodName); diff --git a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodGetClass.java b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodGetClass.java index 3ebb64c7..313591dd 100644 --- a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodGetClass.java +++ b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodGetClass.java @@ -4,6 +4,8 @@ import eu.koboo.en2do.internal.RepositoryMeta; import eu.koboo.en2do.internal.methods.predefined.PredefinedMethod; import eu.koboo.en2do.repository.Repository; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.lang.reflect.Method; @@ -14,7 +16,7 @@ public MethodGetClass(RepositoryMeta meta, MongoCollection entityCo } @Override - public Object handle(Method method, Object[] arguments) throws Exception { + public @Nullable Object handle(@NotNull Method method, @NotNull Object[] arguments) throws Exception { return repositoryMeta.getRepositoryClass(); } } diff --git a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodGetCollectionName.java b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodGetCollectionName.java index ac81cf35..da991410 100644 --- a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodGetCollectionName.java +++ b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodGetCollectionName.java @@ -4,6 +4,8 @@ import eu.koboo.en2do.internal.RepositoryMeta; import eu.koboo.en2do.internal.methods.predefined.PredefinedMethod; import eu.koboo.en2do.repository.Repository; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.lang.reflect.Method; @@ -14,7 +16,7 @@ public MethodGetCollectionName(RepositoryMeta meta, MongoCollection } @Override - public Object handle(Method method, Object[] arguments) throws Exception { + public @Nullable Object handle(@NotNull Method method, @NotNull Object[] arguments) throws Exception { return repositoryMeta.getCollectionName(); } } diff --git a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodGetEntityClass.java b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodGetEntityClass.java index 5f1d16cf..af7f3e0b 100644 --- a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodGetEntityClass.java +++ b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodGetEntityClass.java @@ -4,6 +4,8 @@ import eu.koboo.en2do.internal.RepositoryMeta; import eu.koboo.en2do.internal.methods.predefined.PredefinedMethod; import eu.koboo.en2do.repository.Repository; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.lang.reflect.Method; @@ -14,7 +16,7 @@ public MethodGetEntityClass(RepositoryMeta meta, MongoCollection en } @Override - public Object handle(Method method, Object[] arguments) throws Exception { + public @Nullable Object handle(@NotNull Method method, @NotNull Object[] arguments) throws Exception { return repositoryMeta.getEntityClass(); } } diff --git a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodGetEntityUniqueIdClass.java b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodGetEntityUniqueIdClass.java index d000f41f..aeaeecaa 100644 --- a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodGetEntityUniqueIdClass.java +++ b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodGetEntityUniqueIdClass.java @@ -4,6 +4,8 @@ import eu.koboo.en2do.internal.RepositoryMeta; import eu.koboo.en2do.internal.methods.predefined.PredefinedMethod; import eu.koboo.en2do.repository.Repository; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.lang.reflect.Method; @@ -14,7 +16,7 @@ public MethodGetEntityUniqueIdClass(RepositoryMeta meta, MongoCollecti } @Override - public Object handle(Method method, Object[] arguments) throws Exception { + public @Nullable Object handle(@NotNull Method method, @NotNull Object[] arguments) throws Exception { return repositoryMeta.getEntityUniqueIdClass(); } } diff --git a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodGetUniqueId.java b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodGetUniqueId.java index 8daeff1f..3ae1b5cf 100644 --- a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodGetUniqueId.java +++ b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodGetUniqueId.java @@ -4,6 +4,8 @@ import eu.koboo.en2do.internal.RepositoryMeta; import eu.koboo.en2do.internal.methods.predefined.PredefinedMethod; import eu.koboo.en2do.repository.Repository; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.lang.reflect.Method; @@ -14,7 +16,7 @@ public MethodGetUniqueId(RepositoryMeta meta, MongoCollection entit } @Override - public Object handle(Method method, Object[] arguments) throws Exception { + public @Nullable Object handle(@NotNull Method method, @NotNull Object[] arguments) throws Exception { E entity = repositoryMeta.checkEntity(method, arguments[0]); Object identifier = repositoryMeta.getEntityUniqueIdField().get(entity); return repositoryMeta.checkUniqueId(method, identifier); diff --git a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodHashCode.java b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodHashCode.java index 8bfb2b43..94594eda 100644 --- a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodHashCode.java +++ b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodHashCode.java @@ -4,6 +4,8 @@ import eu.koboo.en2do.internal.RepositoryMeta; import eu.koboo.en2do.internal.methods.predefined.PredefinedMethod; import eu.koboo.en2do.repository.Repository; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.lang.reflect.Method; @@ -14,7 +16,7 @@ public MethodHashCode(RepositoryMeta meta, MongoCollection entityCo } @Override - public Object handle(Method method, Object[] arguments) throws Exception { + public @Nullable Object handle(@NotNull Method method, @NotNull Object[] arguments) throws Exception { Class repositoryClass = repositoryMeta.getRepositoryClass(); return repositoryClass.getName().hashCode() + repositoryMeta.getCollectionName().hashCode(); } diff --git a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodPageAll.java b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodPageAll.java index eedce96c..d7aac6f5 100644 --- a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodPageAll.java +++ b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodPageAll.java @@ -5,6 +5,8 @@ import eu.koboo.en2do.internal.RepositoryMeta; import eu.koboo.en2do.internal.methods.predefined.PredefinedMethod; import eu.koboo.en2do.repository.Repository; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.lang.reflect.Method; import java.util.ArrayList; @@ -16,7 +18,7 @@ public MethodPageAll(RepositoryMeta meta, MongoCollection entityCol } @Override - public Object handle(Method method, Object[] arguments) throws Exception { + public @Nullable Object handle(@NotNull Method method, @NotNull Object[] arguments) throws Exception { FindIterable findIterable = repositoryMeta.createIterable(null, methodName); findIterable = repositoryMeta.applyPageObject(method, findIterable, arguments); return findIterable.into(new ArrayList<>()); diff --git a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodSave.java b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodSave.java index 3b7b088a..47a72c75 100644 --- a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodSave.java +++ b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodSave.java @@ -9,6 +9,8 @@ import lombok.AccessLevel; import lombok.experimental.FieldDefaults; import org.bson.conversions.Bson; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.lang.reflect.Method; @@ -23,7 +25,7 @@ public MethodSave(RepositoryMeta meta, MongoCollection entityCollec } @Override - public Object handle(Method method, Object[] arguments) throws Exception { + public @Nullable Object handle(@NotNull Method method, @NotNull Object[] arguments) throws Exception { E entity = repositoryMeta.checkEntity(method, arguments[0]); ID uniqueId = repositoryMeta.checkUniqueId(method, repositoryMeta.getUniqueId(entity)); Bson idFilter = repositoryMeta.createIdFilter(uniqueId); diff --git a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodSaveAll.java b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodSaveAll.java index d03e35b4..887aa0f5 100644 --- a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodSaveAll.java +++ b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodSaveAll.java @@ -8,6 +8,8 @@ import lombok.AccessLevel; import lombok.experimental.FieldDefaults; import org.bson.conversions.Bson; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.lang.reflect.Method; import java.util.ArrayList; @@ -24,13 +26,13 @@ public MethodSaveAll(RepositoryMeta meta, MongoCollection entityCol } @Override - public Object handle(Method method, Object[] arguments) throws Exception { + public @Nullable Object handle(@NotNull Method method, @NotNull Object[] arguments) throws Exception { List entityList = repositoryMeta.checkEntityList(method, arguments[0]); if (entityList.isEmpty()) { return true; } List insertList = new ArrayList<>(); - // Iterate through entities and check if it already exists by uniqueidentifier. + // Iterate through entities and check if it already exists by unique identifier. for (E entity : entityList) { ID uniqueId = repositoryMeta.checkUniqueId(method, repositoryMeta.getUniqueId(entity)); Bson idFilter = repositoryMeta.createIdFilter(uniqueId); diff --git a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodSortAll.java b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodSortAll.java index 3821cfa7..b68c5729 100644 --- a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodSortAll.java +++ b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodSortAll.java @@ -5,6 +5,8 @@ import eu.koboo.en2do.internal.RepositoryMeta; import eu.koboo.en2do.internal.methods.predefined.PredefinedMethod; import eu.koboo.en2do.repository.Repository; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.lang.reflect.Method; import java.util.ArrayList; @@ -16,7 +18,7 @@ public MethodSortAll(RepositoryMeta meta, MongoCollection entityCol } @Override - public Object handle(Method method, Object[] arguments) throws Exception { + public @Nullable Object handle(@NotNull Method method, @NotNull Object[] arguments) throws Exception { FindIterable findIterable = repositoryMeta.createIterable(null, methodName); findIterable = repositoryMeta.applySortObject(method, findIterable, arguments); return findIterable.into(new ArrayList<>()); diff --git a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodToString.java b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodToString.java index 58941582..66b66f26 100644 --- a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodToString.java +++ b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodToString.java @@ -4,6 +4,8 @@ import eu.koboo.en2do.internal.RepositoryMeta; import eu.koboo.en2do.internal.methods.predefined.PredefinedMethod; import eu.koboo.en2do.repository.Repository; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.lang.reflect.Method; @@ -14,7 +16,7 @@ public MethodToString(RepositoryMeta meta, MongoCollection entityCo } @Override - public Object handle(Method method, Object[] arguments) throws Exception { + public @Nullable Object handle(@NotNull Method method, @NotNull Object[] arguments) throws Exception { Class repositoryClass = repositoryMeta.getRepositoryClass(); return repositoryClass.getName(); } diff --git a/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodUpdateAllFields.java b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodUpdateAllFields.java new file mode 100644 index 00000000..9cbf3d67 --- /dev/null +++ b/src/main/java/eu/koboo/en2do/internal/methods/predefined/impl/MethodUpdateAllFields.java @@ -0,0 +1,30 @@ +package eu.koboo.en2do.internal.methods.predefined.impl; + +import com.mongodb.client.MongoCollection; +import com.mongodb.client.model.UpdateOptions; +import com.mongodb.client.result.UpdateResult; +import eu.koboo.en2do.internal.RepositoryMeta; +import eu.koboo.en2do.internal.methods.predefined.PredefinedMethod; +import eu.koboo.en2do.repository.Repository; +import eu.koboo.en2do.repository.methods.fields.UpdateBatch; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.lang.reflect.Method; + +public class MethodUpdateAllFields> extends PredefinedMethod { + + public MethodUpdateAllFields(RepositoryMeta meta, MongoCollection entityCollection) { + super("updateAllFields", meta, entityCollection); + } + + @Override + public @Nullable Object handle(@NotNull Method method, @NotNull Object[] arguments) throws Exception { + MongoCollection collection = repositoryMeta.getCollection(); + UpdateBatch updateBatch = (UpdateBatch) arguments[0]; + UpdateResult result = collection.updateMany(repositoryMeta.createIdExistsFilter(), + repositoryMeta.createUpdateDocument(updateBatch), + new UpdateOptions().upsert(false)); + return result.wasAcknowledged(); + } +} \ No newline at end of file diff --git a/src/main/java/eu/koboo/en2do/repository/AsyncRepository.java b/src/main/java/eu/koboo/en2do/repository/AsyncRepository.java index 41337b70..c142d70f 100644 --- a/src/main/java/eu/koboo/en2do/repository/AsyncRepository.java +++ b/src/main/java/eu/koboo/en2do/repository/AsyncRepository.java @@ -1,8 +1,10 @@ package eu.koboo.en2do.repository; import eu.koboo.en2do.repository.methods.async.Async; +import eu.koboo.en2do.repository.methods.fields.UpdateBatch; import eu.koboo.en2do.repository.methods.pagination.Pagination; import eu.koboo.en2do.repository.methods.sort.Sort; +import org.jetbrains.annotations.NotNull; import java.util.List; import java.util.concurrent.CompletableFuture; @@ -20,115 +22,151 @@ public interface AsyncRepository { /** * Async representation - * @see Repository#countAll() + * * @return Future, with the count of all entities + * @see Repository#countAll() */ @Async + @NotNull CompletableFuture asyncCountAll(); /** * Async representation - * @see Repository#delete(Object) + * * @param entity The entity, which should be deleted * @return Future, with a boolean of success + * @see Repository#delete(Object) */ @Async - CompletableFuture asyncDelete(E entity); + @NotNull + CompletableFuture asyncDelete(@NotNull E entity); /** * Async representation - * @see Repository#deleteAll(List) + * * @param entityList The List with entities, which should be deleted * @return Future, with a boolean of success + * @see Repository#deleteAll(List) */ @Async - CompletableFuture asyncDeleteAll(List entityList); + @NotNull + CompletableFuture asyncDeleteAll(@NotNull List entityList); /** * Async representation - * @see Repository#deleteById(Object) + * * @param identifier The identifier of the entity, which should be deleted * @return Future, with a boolean of success + * @see Repository#deleteById(Object) */ @Async - CompletableFuture asyncDeleteById(ID identifier); + @NotNull + CompletableFuture asyncDeleteById(@NotNull ID identifier); /** * Async representation - * @see Repository#drop() + * * @return Future, with a boolean of success + * @see Repository#drop() */ @Async + @NotNull CompletableFuture asyncDrop(); /** * Async representation - * @see Repository#exists(Object) + * * @param entity The entity, which should be checked * @return Future, with a boolean, which indicates if the entity exists + * @see Repository#exists(Object) */ @Async - CompletableFuture asyncExists(E entity); + @NotNull + CompletableFuture asyncExists(@NotNull E entity); /** * Async representation - * @see Repository#existsById(Object) + * * @param identifier The identifier of the entity, which should be checked * @return Future, with a boolean, which indicates if an entity with the id exists + * @see Repository#existsById(Object) */ @Async - CompletableFuture asyncExistsById(ID identifier); + @NotNull + CompletableFuture asyncExistsById(@NotNull ID identifier); /** * Async representation - * @see Repository#findAll() + * * @return Future, with all entities + * @see Repository#findAll() */ @Async + @NotNull CompletableFuture> asyncFindAll(); /** * Async representation - * @see Repository#findFirstById(Object) + * * @param identifier The identifier of the entity, which should be found * @return Future, with the first entity with the id + * @see Repository#findFirstById(Object) */ @Async - CompletableFuture asyncFindFirstById(ID identifier); + @NotNull + CompletableFuture asyncFindFirstById(@NotNull ID identifier); /** * Async representation - * @see Repository#pageAll(Pagination) + * * @param pagination The options, which should be used for pagination * @return Future, with all entities, paged by the Pagination object + * @see Repository#pageAll(Pagination) */ @Async - CompletableFuture> asyncPageAll(Pagination pagination); + @NotNull + CompletableFuture> asyncPageAll(@NotNull Pagination pagination); /** * Async representation - * @see Repository#save(Object) + * * @param entity The entity, which should be saved * @return Future, with a boolean of success + * @see Repository#save(Object) */ @Async - CompletableFuture asyncSave(E entity); + @NotNull + CompletableFuture asyncSave(@NotNull E entity); /** * Async representation - * @see Repository#saveAll(List) + * * @param entityList The List of entities, which should be saved * @return Future, with a boolean of success + * @see Repository#saveAll(List) */ @Async - CompletableFuture asyncSaveAll(List entityList); + @NotNull + CompletableFuture asyncSaveAll(@NotNull List entityList); /** * Async representation - * @see Repository#sortAll(Sort) + * * @param sort The options, which should be used for sorting * @return Future, with all entities, sorted by the Sort object + * @see Repository#sortAll(Sort) + */ + @Async + @NotNull + CompletableFuture> asyncSortAll(@NotNull Sort sort); + + /** + * Async representation + * + * @param updateBatch The UpdateBatch to use. + * @return true, if the operation was successful. + * @see Repository#updateAllFields(UpdateBatch) */ @Async - CompletableFuture> asyncSortAll(Sort sort); + CompletableFuture asyncUpdateAllFields(UpdateBatch updateBatch); } diff --git a/src/main/java/eu/koboo/en2do/repository/Repository.java b/src/main/java/eu/koboo/en2do/repository/Repository.java index f24614df..b885aace 100644 --- a/src/main/java/eu/koboo/en2do/repository/Repository.java +++ b/src/main/java/eu/koboo/en2do/repository/Repository.java @@ -1,7 +1,10 @@ package eu.koboo.en2do.repository; +import eu.koboo.en2do.repository.methods.fields.UpdateBatch; import eu.koboo.en2do.repository.methods.pagination.Pagination; import eu.koboo.en2do.repository.methods.sort.Sort; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.List; @@ -29,7 +32,7 @@ public interface Repository { * @param entity The entity, which should be deleted. * @return true, if the entity was deleted successfully. */ - boolean delete(E entity); + boolean delete(@NotNull E entity); /** * This method deletes all entities of the given list, filtering like the "#delete(E entity)" method. @@ -37,7 +40,7 @@ public interface Repository { * @param entityList The List with the entities, which should be deleted. * @return true, if all entities were deleted successfully. */ - boolean deleteAll(List entityList); + boolean deleteAll(@NotNull List entityList); /** * This method deletes the entity with the given identifier, filtering like the "#delete(E entity)" method. @@ -45,7 +48,7 @@ public interface Repository { * @param identifier The unique identifier of the entity, which should be deleted. * @return true, if the entity was deleted successfully. */ - boolean deleteById(ID identifier); + boolean deleteById(@NotNull ID identifier); /** * Drops / deletes all entities of the repository. @@ -61,7 +64,7 @@ public interface Repository { * @param entity The entity, which should be checked. * @return true, if the entity exists in the collection. */ - boolean exists(E entity); + boolean exists(@NotNull E entity); /** * Checks if an entity with the given unique identifier exists in the repository, like the "#exists(E entity)" method. @@ -69,7 +72,7 @@ public interface Repository { * @param identifier The identifier of the entity, which should be checked. * @return true, if an entity with the given identifier exists in the collection. */ - boolean existsById(ID identifier); + boolean existsById(@NotNull ID identifier); /** * Finds all entities of the collection @@ -85,21 +88,25 @@ public interface Repository { * @param identifier The unique identifier of the entity, which is used to filter. * @return The found entity, if it exists, or "null" if it not exists. */ - E findFirstById(ID identifier); + @Nullable + E findFirstById(@NotNull ID identifier); /** * @return The collection name, defined by the "@Collection" annotation of the repository. */ + @NotNull String getCollectionName(); /** * @return The Class of the entity of the repository. */ + @NotNull Class getEntityClass(); /** * @return The Class of the unique identifier of the entity of the repository. */ + @NotNull Class getEntityUniqueIdClass(); /** @@ -109,7 +116,8 @@ public interface Repository { * @param entity The entity, which unique identifier, should be returned * @return The unique identifier of the given entity. */ - ID getUniqueId(E entity); + @Nullable + ID getUniqueId(@NotNull E entity); /** * This method applies the pagination of all entities of the repository. @@ -117,7 +125,8 @@ public interface Repository { * @param pagination The pagination, which is used to page the entities. * @return A List with the paged entities. */ - List pageAll(Pagination pagination); + @NotNull + List pageAll(@NotNull Pagination pagination); /** * Saves the given entity to the database. @@ -127,7 +136,7 @@ public interface Repository { * @param entity The entity, which should be saved. * @return true, if the entity was successfully saved. */ - boolean save(E entity); + boolean save(@NotNull E entity); /** * Saves all entities of the given List to the database. @@ -135,7 +144,7 @@ public interface Repository { * @param entityList A List of the entities, which should be saved * @return true, if the entities were successfully saved. */ - boolean saveAll(List entityList); + boolean saveAll(@NotNull List entityList); /** * This method applies the Sort object of all entities of the repository. @@ -143,5 +152,14 @@ public interface Repository { * @param sort The Sort object, which should be used to sort all entities. * @return A List with the sorted entities. */ - List sortAll(Sort sort); + @NotNull + List sortAll(@NotNull Sort sort); + + /** + * This method uses the UpdateBatch object to update the fields of all documents. + * + * @param updateBatch The UpdateBatch to use. + * @return true, if the operation was successful. + */ + boolean updateAllFields(UpdateBatch updateBatch); } diff --git a/src/main/java/eu/koboo/en2do/repository/entity/compound/CompoundIndex.java b/src/main/java/eu/koboo/en2do/repository/entity/compound/CompoundIndex.java index d8118146..0fdadb4d 100644 --- a/src/main/java/eu/koboo/en2do/repository/entity/compound/CompoundIndex.java +++ b/src/main/java/eu/koboo/en2do/repository/entity/compound/CompoundIndex.java @@ -16,12 +16,14 @@ /** * An array of all indexed field in this compound index. + * * @return The array with the index annotations. */ Index[] value(); /** * Defines if the index array is unique. This speeds up the usage of queries on the indexed fields drastically. + * * @return true, if the compound index is unique on every document. */ boolean uniqueIndex() default false; diff --git a/src/main/java/eu/koboo/en2do/repository/entity/compound/Index.java b/src/main/java/eu/koboo/en2do/repository/entity/compound/Index.java index 195f70b9..1440ac82 100644 --- a/src/main/java/eu/koboo/en2do/repository/entity/compound/Index.java +++ b/src/main/java/eu/koboo/en2do/repository/entity/compound/Index.java @@ -14,12 +14,14 @@ /** * Sets the name of the field, which should be used to index. + * * @return The field name, which should be indexed. */ String value(); /** * Sets the direction of the index. + * * @return true, if the direction is ascending. false, if the direction is descending. */ boolean ascending() default true; diff --git a/src/main/java/eu/koboo/en2do/repository/entity/ttl/TTLIndex.java b/src/main/java/eu/koboo/en2do/repository/entity/ttl/TTLIndex.java index 76e20ebd..52ba286e 100644 --- a/src/main/java/eu/koboo/en2do/repository/entity/ttl/TTLIndex.java +++ b/src/main/java/eu/koboo/en2do/repository/entity/ttl/TTLIndex.java @@ -17,18 +17,21 @@ /** * Indicates, which field should be checked for the time-to-live index + * * @return The name of the field in the entity */ String value(); /** * Sets the amount of the time unit + * * @return The amount of time */ long ttl() default 0; /** * Sets the unit of the time-to-live index + * * @return The unit of the ttl value */ TimeUnit time() default TimeUnit.SECONDS; diff --git a/src/main/java/eu/koboo/en2do/repository/methods/fields/FieldUpdate.java b/src/main/java/eu/koboo/en2do/repository/methods/fields/FieldUpdate.java new file mode 100644 index 00000000..f322f90a --- /dev/null +++ b/src/main/java/eu/koboo/en2do/repository/methods/fields/FieldUpdate.java @@ -0,0 +1,56 @@ +package eu.koboo.en2do.repository.methods.fields; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.experimental.FieldDefaults; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * Represents a field change in all documents, which match the given filters. + */ +@Getter +@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true) +@RequiredArgsConstructor(access = AccessLevel.PRIVATE) +public class FieldUpdate { + + /** + * Set the value of a specific field. + * + * @param fieldName The field, which should be set + * @param value The value, which gets set into the field. + * @return The new created FieldUpdate instance. + */ + public static @NotNull FieldUpdate set(@NotNull String fieldName, @NotNull Object value) { + return new FieldUpdate(UpdateType.SET, fieldName, value); + } + + /** + * Rename a specific field. + * + * @param fieldName The field, which should be renamed. + * @param value The new field name. + * @return The new created FieldUpdate instance. + */ + public static @NotNull FieldUpdate rename(@NotNull String fieldName, @NotNull Object value) { + return new FieldUpdate(UpdateType.RENAME, fieldName, value); + } + + /** + * Remove a specific field. + * + * @param fieldName The field, which should be removed. + * @return The new created FieldUpdate instance. + */ + public static @NotNull FieldUpdate remove(@NotNull String fieldName) { + return new FieldUpdate(UpdateType.REMOVE, fieldName, null); + } + + @NotNull + UpdateType updateType; + @NotNull + String fieldName; + @Nullable + Object value; +} diff --git a/src/main/java/eu/koboo/en2do/repository/methods/fields/UpdateBatch.java b/src/main/java/eu/koboo/en2do/repository/methods/fields/UpdateBatch.java new file mode 100644 index 00000000..8c0c344e --- /dev/null +++ b/src/main/java/eu/koboo/en2do/repository/methods/fields/UpdateBatch.java @@ -0,0 +1,88 @@ +package eu.koboo.en2do.repository.methods.fields; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.experimental.FieldDefaults; +import org.jetbrains.annotations.NotNull; + +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + +/** + * Represents multiple field updates, like set, rename or remove. + */ +@Getter +@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true) +@RequiredArgsConstructor(access = AccessLevel.PRIVATE) +public class UpdateBatch { + + /** + * Creates a UpdateBatch instance from a List of + * + * @param fieldUpdateList The given list, which gets wrapped. + * @return The new created UpdateBatch instance. + * @see FieldUpdate + */ + public static @NotNull UpdateBatch of(@NotNull List fieldUpdateList) { + return new UpdateBatch(new LinkedList<>()).addAll(fieldUpdateList); + } + + /** + * Creates a UpdateBatch instance from an Array of + * + * @param fieldUpdateArray The given array, which gets wrapped. + * @return The new created UpdateBatch instance. + * @see FieldUpdate + */ + public static @NotNull UpdateBatch of(@NotNull FieldUpdate... fieldUpdateArray) { + return new UpdateBatch(new LinkedList<>()).addAll(fieldUpdateArray); + } + + /** + * Creates a UpdateBatch instance from a single + * + * @param fieldUpdate The given instance, which gets wrapped. + * @return The new created UpdateBatch instance. + * @see FieldUpdate + */ + public static @NotNull UpdateBatch of(@NotNull FieldUpdate fieldUpdate) { + return new UpdateBatch(new LinkedList<>()).add(fieldUpdate); + } + + @NotNull + List updateList; + + /** + * Adds a single FieldUpdate to the list. + * + * @param fieldUpdate The FieldUpdate, which should be added. + * @return The instance of the UpdateBatch + */ + public UpdateBatch add(FieldUpdate fieldUpdate) { + updateList.add(fieldUpdate); + return this; + } + + /** + * Adds an Array of FieldUpdates to the list. + * + * @param fieldUpdateArray The array, which should be added. + * @return The instance of the UpdateBatch + */ + public UpdateBatch addAll(FieldUpdate... fieldUpdateArray) { + return addAll(Arrays.asList(fieldUpdateArray)); + } + + /** + * Adds a List of FieldUpdates to the list. + * + * @param fieldUpdateList The array, which should be added. + * @return The instance of the UpdateBatch + */ + public UpdateBatch addAll(List fieldUpdateList) { + updateList.addAll(fieldUpdateList); + return this; + } +} diff --git a/src/main/java/eu/koboo/en2do/repository/methods/fields/UpdateType.java b/src/main/java/eu/koboo/en2do/repository/methods/fields/UpdateType.java new file mode 100644 index 00000000..b9bde453 --- /dev/null +++ b/src/main/java/eu/koboo/en2do/repository/methods/fields/UpdateType.java @@ -0,0 +1,21 @@ +package eu.koboo.en2do.repository.methods.fields; + +/** + * Represents the type of the update behaviour. + */ +public enum UpdateType { + + /** + * Sets the value of the given field to the given value. + * Note: Overrides the previous values. + */ + SET, + /** + * Renames the given field to the given new field name. + */ + RENAME, + /** + * Removes the field from every document. + */ + REMOVE +} diff --git a/src/main/java/eu/koboo/en2do/repository/methods/pagination/Pagination.java b/src/main/java/eu/koboo/en2do/repository/methods/pagination/Pagination.java index aca46ebd..a72070cd 100644 --- a/src/main/java/eu/koboo/en2do/repository/methods/pagination/Pagination.java +++ b/src/main/java/eu/koboo/en2do/repository/methods/pagination/Pagination.java @@ -4,6 +4,7 @@ import lombok.Getter; import lombok.experimental.FieldDefaults; import lombok.experimental.NonFinal; +import org.jetbrains.annotations.NotNull; import java.util.HashMap; import java.util.Map; @@ -22,7 +23,7 @@ public class Pagination { * @param entitiesPerPage Sets the maximum entities per one page. * @return The created Pagination object */ - public static Pagination of(int entitiesPerPage) { + public static @NotNull Pagination of(int entitiesPerPage) { return new Pagination(entitiesPerPage); } @@ -44,7 +45,7 @@ private Pagination(int entitiesPerPage) { * @param ascending The direction of the sorting * @return The used Pagination object */ - public Pagination order(String fieldName, boolean ascending) { + public @NotNull Pagination order(String fieldName, boolean ascending) { pageDirectionMap.put(fieldName, ascending ? 1 : -1); return this; } @@ -56,7 +57,7 @@ public Pagination order(String fieldName, boolean ascending) { * @param fieldName The field which is used to sort * @return The used Pagination object */ - public Pagination order(String fieldName) { + public @NotNull Pagination order(String fieldName) { return order(fieldName, true); } @@ -67,7 +68,7 @@ public Pagination order(String fieldName) { * @param page The requested page * @return The used Pagination object */ - public Pagination page(long page) { + public @NotNull Pagination page(long page) { this.page = page; return this; } diff --git a/src/main/java/eu/koboo/en2do/repository/methods/sort/Sort.java b/src/main/java/eu/koboo/en2do/repository/methods/sort/Sort.java index 92e5e73f..c8833bbb 100644 --- a/src/main/java/eu/koboo/en2do/repository/methods/sort/Sort.java +++ b/src/main/java/eu/koboo/en2do/repository/methods/sort/Sort.java @@ -3,6 +3,7 @@ import lombok.AccessLevel; import lombok.Getter; import lombok.experimental.FieldDefaults; +import org.jetbrains.annotations.NotNull; import java.util.HashMap; import java.util.Map; @@ -20,7 +21,7 @@ public class Sort { * * @return The new created sort object. */ - public static Sort of() { + public static @NotNull Sort of() { return new Sort(); } @@ -41,7 +42,7 @@ private Sort() { * @param ascending The direction of the sorting * @return The used Sort object */ - public Sort order(String fieldName, boolean ascending) { + public @NotNull Sort order(@NotNull String fieldName, boolean ascending) { fieldDirectionMap.put(fieldName, ascending ? 1 : -1); return this; } @@ -53,7 +54,7 @@ public Sort order(String fieldName, boolean ascending) { * @param fieldName The field which is used to sort * @return The used Sort object */ - public Sort order(String fieldName) { + public @NotNull Sort order(@NotNull String fieldName) { return order(fieldName, true); } @@ -63,7 +64,7 @@ public Sort order(String fieldName) { * @param limit The amount of the entities in the returned List. * @return The used Sort object */ - public Sort limit(int limit) { + public @NotNull Sort limit(int limit) { this.limit = limit; return this; } @@ -74,7 +75,7 @@ public Sort limit(int limit) { * @param skip The amount of the entities, which should be skipped, before creating the List. * @return The used Sort object */ - public Sort skip(int skip) { + public @NotNull Sort skip(int skip) { this.skip = skip; return this; } diff --git a/src/main/java/eu/koboo/en2do/utility/AnnotationUtils.java b/src/main/java/eu/koboo/en2do/utility/AnnotationUtils.java index cc236067..e93d9a0d 100644 --- a/src/main/java/eu/koboo/en2do/utility/AnnotationUtils.java +++ b/src/main/java/eu/koboo/en2do/utility/AnnotationUtils.java @@ -1,6 +1,7 @@ package eu.koboo.en2do.utility; import lombok.experimental.UtilityClass; +import org.jetbrains.annotations.NotNull; import java.lang.annotation.Annotation; import java.util.Arrays; @@ -23,7 +24,8 @@ public class AnnotationUtils { * @param The generic type of the annotation Class * @return The Set with all found annotations of type A */ - public Set collectAnnotations(Class entityClass, Class annotationClass) { + public @NotNull Set collectAnnotations(@NotNull Class entityClass, + @NotNull Class annotationClass) { Set annotationSet = new HashSet<>(); Class clazz = entityClass; while (clazz != Object.class) { diff --git a/src/main/java/eu/koboo/en2do/utility/EntityUtils.java b/src/main/java/eu/koboo/en2do/utility/EntityUtils.java index 8ce57946..97fdc520 100644 --- a/src/main/java/eu/koboo/en2do/utility/EntityUtils.java +++ b/src/main/java/eu/koboo/en2do/utility/EntityUtils.java @@ -1,6 +1,7 @@ package eu.koboo.en2do.utility; import lombok.experimental.UtilityClass; +import org.jetbrains.annotations.NotNull; import java.beans.BeanInfo; import java.beans.IntrospectionException; @@ -23,7 +24,7 @@ public class EntityUtils { * @param from The entity to copy from * @param to The entity to copy to */ - public void copyProperties(Object from, Object to) { + public void copyProperties(@NotNull Object from, @NotNull Object to) { Class fromClass = from.getClass(); Class toClass = to.getClass(); diff --git a/src/main/java/eu/koboo/en2do/utility/FieldUtils.java b/src/main/java/eu/koboo/en2do/utility/FieldUtils.java index a91f7d39..e725dfb9 100644 --- a/src/main/java/eu/koboo/en2do/utility/FieldUtils.java +++ b/src/main/java/eu/koboo/en2do/utility/FieldUtils.java @@ -1,6 +1,8 @@ package eu.koboo.en2do.utility; import lombok.experimental.UtilityClass; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.lang.reflect.Field; import java.util.Arrays; @@ -20,7 +22,7 @@ public class FieldUtils { * @param The generic type of the class * @return The Set with all found fields of the given class. */ - public Set collectFields(Class typeClass) { + public @NotNull Set collectFields(@NotNull Class typeClass) { Set fields = new HashSet<>(); Class clazz = typeClass; while (clazz != Object.class) { @@ -41,7 +43,7 @@ public Set collectFields(Class typeClass) { * @param fieldSet The Set, which should be iterated through * @return The field, if found. If not found, it returns "null" */ - public Field findFieldByName(String fieldName, Set fieldSet) { + public @Nullable Field findFieldByName(@NotNull String fieldName, @NotNull Set fieldSet) { for (Field field : fieldSet) { if (!field.getName().equalsIgnoreCase(fieldName)) { continue; diff --git a/src/main/java/eu/koboo/en2do/utility/GenericUtils.java b/src/main/java/eu/koboo/en2do/utility/GenericUtils.java index 88f0b162..514dd3af 100644 --- a/src/main/java/eu/koboo/en2do/utility/GenericUtils.java +++ b/src/main/java/eu/koboo/en2do/utility/GenericUtils.java @@ -1,6 +1,7 @@ package eu.koboo.en2do.utility; import lombok.experimental.UtilityClass; +import org.jetbrains.annotations.NotNull; import java.lang.reflect.Method; import java.lang.reflect.Parameter; @@ -15,10 +16,11 @@ public class GenericUtils { /** * Gets the generic type of the return type from the given method + * * @param method The method with the return type * @return The class of the generic type */ - public Class getGenericTypeOfReturnType(Method method) { + public @NotNull Class getGenericTypeOfReturnType(@NotNull Method method) { Type returnType = method.getGenericReturnType(); ParameterizedType type = (ParameterizedType) returnType; return (Class) type.getActualTypeArguments()[0]; @@ -26,11 +28,12 @@ public Class getGenericTypeOfReturnType(Method method) { /** * Gets the generic type of the parameter at the given from the given method - * @param method The method with the parameter + * + * @param method The method with the parameter * @param paramIndex The index of the parameter * @return The class of the generic type of the parameter */ - public Class getGenericTypeOfParameter(Method method, int paramIndex) { + public @NotNull Class getGenericTypeOfParameter(@NotNull Method method, int paramIndex) { Parameter parameter = method.getParameters()[paramIndex]; Type parameterType = parameter.getParameterizedType(); ParameterizedType type = (ParameterizedType) parameterType; @@ -39,11 +42,12 @@ public Class getGenericTypeOfParameter(Method method, int paramIndex) { /** * Checks if class1 and class2 is not the same type + * * @param class1 The first given class * @param class2 The second given class - * @return true if class1 and class2 are not same type type + * @return true if class1 and class2 are not same type */ - public boolean isNotTypeOf(Class class1, Class class2) { + public boolean isNotTypeOf(@NotNull Class class1, @NotNull Class class2) { if (isBoolean(class1) && isBoolean(class2)) { return false; } @@ -70,64 +74,71 @@ public boolean isNotTypeOf(Class class1, Class class2) { /** * Checks if the given class is a type of "boolean" + * * @param clazz The class to check * @return true, if the class is type boolean */ - private boolean isBoolean(Class clazz) { + private boolean isBoolean(@NotNull Class clazz) { return clazz.isAssignableFrom(Boolean.class) || clazz.isAssignableFrom(boolean.class); } /** * Checks if the given class is a type of "short" + * * @param clazz The class to check * @return true, if the class is type short */ - private boolean isShort(Class clazz) { + private boolean isShort(@NotNull Class clazz) { return clazz.isAssignableFrom(Short.class) || clazz.isAssignableFrom(short.class); } /** * Checks if the given class is a type of "float" + * * @param clazz The class to check * @return true, if the class is type float */ - private boolean isFloat(Class clazz) { + private boolean isFloat(@NotNull Class clazz) { return clazz.isAssignableFrom(Float.class) || clazz.isAssignableFrom(float.class); } /** * Checks if the given class is a type of "int" + * * @param clazz The class to check * @return true, if the class is type int */ - private boolean isInteger(Class clazz) { + private boolean isInteger(@NotNull Class clazz) { return clazz.isAssignableFrom(Integer.class) || clazz.isAssignableFrom(int.class); } /** * Checks if the given class is a type of "long" + * * @param clazz The class to check * @return true, if the class is type long */ - private boolean isLong(Class clazz) { + private boolean isLong(@NotNull Class clazz) { return clazz.isAssignableFrom(Long.class) || clazz.isAssignableFrom(long.class); } /** * Checks if the given class is a type of "double" + * * @param clazz The class to check * @return true, if the class is type double */ - private boolean isDouble(Class clazz) { + private boolean isDouble(@NotNull Class clazz) { return clazz.isAssignableFrom(Double.class) || clazz.isAssignableFrom(double.class); } /** * Checks if the given class is a type of "char" + * * @param clazz The class to check * @return true, if the class is type char */ - private boolean isChar(Class clazz) { + private boolean isChar(@NotNull Class clazz) { return clazz.isAssignableFrom(Character.class) || clazz.isAssignableFrom(char.class); } } diff --git a/src/test/java/eu/koboo/en2do/test/Const.java b/src/test/java/eu/koboo/en2do/test/Const.java index 7ab6ace6..d4b7931a 100644 --- a/src/test/java/eu/koboo/en2do/test/Const.java +++ b/src/test/java/eu/koboo/en2do/test/Const.java @@ -5,6 +5,7 @@ import eu.koboo.en2do.test.customer.Customer; import eu.koboo.en2do.test.customer.CustomerType; import eu.koboo.en2do.test.customer.Order; +import org.jetbrains.annotations.NotNull; import java.util.Arrays; import java.util.List; @@ -31,7 +32,7 @@ public class Const { ); public static final CustomerType TYPE = CustomerType.DEFAULT; - public static Customer createNewCustomer() { + public static @NotNull Customer createNewCustomer() { Customer customer = new Customer(); customer.setUniqueId(UNIQUE_ID); customer.setCustomerId(CUSTOMER_ID); @@ -49,7 +50,7 @@ public static Customer createNewCustomer() { return customer; } - public static Alien createNewAlien() { + public static @NotNull Alien createNewAlien() { Alien alien = new Alien(); alien.setUniqueId(UNIQUE_ID); alien.setUfoIdList(Map.of(10L, "Millenium Falcon", 20L, "X-WING", 30L, "Death-Star")); diff --git a/src/test/java/eu/koboo/en2do/test/RepositoryTest.java b/src/test/java/eu/koboo/en2do/test/RepositoryTest.java index 06010a97..75131443 100644 --- a/src/test/java/eu/koboo/en2do/test/RepositoryTest.java +++ b/src/test/java/eu/koboo/en2do/test/RepositoryTest.java @@ -5,6 +5,7 @@ import lombok.AccessLevel; import lombok.experimental.FieldDefaults; import lombok.extern.slf4j.Slf4j; +import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.*; import static org.junit.jupiter.api.Assertions.*; @@ -34,9 +35,9 @@ public void finish() { log.info("Stopping Unit-Test [" + getClass().getName() + "]"); assertNotNull(manager); assertNotNull(repository); - //assertTrue(repository.drop()); + assertTrue(repository.drop()); assertTrue(manager.close()); } - public abstract Class repositoryClass(); + public abstract @NotNull Class repositoryClass(); } diff --git a/src/test/java/eu/koboo/en2do/test/alien/AlienRepositoryTest.java b/src/test/java/eu/koboo/en2do/test/alien/AlienRepositoryTest.java index a24174a6..b8129e55 100644 --- a/src/test/java/eu/koboo/en2do/test/alien/AlienRepositoryTest.java +++ b/src/test/java/eu/koboo/en2do/test/alien/AlienRepositoryTest.java @@ -1,13 +1,14 @@ package eu.koboo.en2do.test.alien; import eu.koboo.en2do.test.RepositoryTest; +import org.jetbrains.annotations.NotNull; import java.util.UUID; public class AlienRepositoryTest extends RepositoryTest { @Override - public Class repositoryClass() { + public @NotNull Class repositoryClass() { return AlienRepository.class; } } diff --git a/src/test/java/eu/koboo/en2do/test/customer/Customer.java b/src/test/java/eu/koboo/en2do/test/customer/Customer.java index 5e4dcbaa..ba0b6cbb 100644 --- a/src/test/java/eu/koboo/en2do/test/customer/Customer.java +++ b/src/test/java/eu/koboo/en2do/test/customer/Customer.java @@ -31,6 +31,7 @@ public class Customer { String city; Long phoneNumber; double balance; + double balanceRenamed; List orders; // Embedded object list CustomerType customerType; // enum type Date createTime; // 1. ttl object diff --git a/src/test/java/eu/koboo/en2do/test/customer/CustomerRepository.java b/src/test/java/eu/koboo/en2do/test/customer/CustomerRepository.java index 8fca28f3..1ee3987d 100644 --- a/src/test/java/eu/koboo/en2do/test/customer/CustomerRepository.java +++ b/src/test/java/eu/koboo/en2do/test/customer/CustomerRepository.java @@ -1,6 +1,8 @@ package eu.koboo.en2do.test.customer; import eu.koboo.en2do.repository.*; +import eu.koboo.en2do.repository.methods.async.Async; +import eu.koboo.en2do.repository.methods.fields.UpdateBatch; import eu.koboo.en2do.repository.methods.pagination.Pagination; import eu.koboo.en2do.repository.methods.sort.Limit; import eu.koboo.en2do.repository.methods.sort.Skip; @@ -10,6 +12,7 @@ import java.util.List; import java.util.UUID; +import java.util.concurrent.CompletableFuture; @SuppressWarnings("unused") @Collection("customer_repository") @@ -17,12 +20,16 @@ @DropEntitiesOnStart //@SeparateEntityId @AppendMethodAsComment -public interface CustomerRepository extends Repository { +public interface CustomerRepository extends Repository, AsyncRepository { Customer findFirstByFirstName(String firstName); long countByFirstName(String firstName); + @Transform("countByCustomerIdExistsAndCustomerId") + @Async + CompletableFuture asyncCountCustomerId(int customerId); + long countByCustomerId(int customerId); boolean deleteByFirstName(String firstName); @@ -72,4 +79,6 @@ public interface CustomerRepository extends Repository { List myTransformedMethod2(String street); List pageByCustomerIdNot(int customerId, Pagination sorter); + + boolean updateFieldsByFirstName(String firstName, UpdateBatch updateBatch); } diff --git a/src/test/java/eu/koboo/en2do/test/customer/CustomerRepositoryTest.java b/src/test/java/eu/koboo/en2do/test/customer/CustomerRepositoryTest.java index 006bf01a..d9e17bbb 100644 --- a/src/test/java/eu/koboo/en2do/test/customer/CustomerRepositoryTest.java +++ b/src/test/java/eu/koboo/en2do/test/customer/CustomerRepositoryTest.java @@ -1,13 +1,14 @@ package eu.koboo.en2do.test.customer; import eu.koboo.en2do.test.RepositoryTest; +import org.jetbrains.annotations.NotNull; import java.util.UUID; public class CustomerRepositoryTest extends RepositoryTest { @Override - public Class repositoryClass() { + public @NotNull Class repositoryClass() { return CustomerRepository.class; } } diff --git a/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncCountAllTest.java b/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncCountAllTest.java new file mode 100644 index 00000000..4be6d3af --- /dev/null +++ b/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncCountAllTest.java @@ -0,0 +1,43 @@ +package eu.koboo.en2do.test.customer.async; + +import eu.koboo.en2do.test.Const; +import eu.koboo.en2do.test.customer.Customer; +import eu.koboo.en2do.test.customer.CustomerRepositoryTest; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +public class CustomerAsyncCountAllTest extends CustomerRepositoryTest { + + @Test + @Order(1) + public void cleanUpRepository() { + repository.asyncFindAll().thenAccept(customerList -> { + assertNotNull(customerList); + assertTrue(customerList.isEmpty()); + }); + } + + @Test + @Order(2) + public void saveCustomer() { + for (int i = 0; i < 15; i++) { + Customer customer = Const.createNewCustomer(); + assertNotNull(customer); + customer.setUniqueId(UUID.randomUUID()); + customer.setCustomerId(i); + repository.asyncSave(customer).thenAccept(Assertions::assertTrue); + repository.asyncExists(customer).thenAccept(Assertions::assertTrue); + } + } + + @Test + @Order(3) + public void countCustomer() { + repository.asyncCountAll().thenAccept(count -> assertEquals(15, count)); + } +} diff --git a/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncDeleteAllTest.java b/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncDeleteAllTest.java new file mode 100644 index 00000000..f54a1444 --- /dev/null +++ b/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncDeleteAllTest.java @@ -0,0 +1,51 @@ +package eu.koboo.en2do.test.customer.async; + +import eu.koboo.en2do.test.Const; +import eu.koboo.en2do.test.customer.Customer; +import eu.koboo.en2do.test.customer.CustomerRepositoryTest; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +public class CustomerAsyncDeleteAllTest extends CustomerRepositoryTest { + + static List customerList; + + @Test + @Order(1) + public void cleanUpRepository() { + repository.asyncFindAll().thenAccept(customerList -> { + assertNotNull(customerList); + assertTrue(customerList.isEmpty()); + }); + } + + @Test + @Order(2) + public void saveCustomer() { + customerList = new ArrayList<>(); + for (int i = 0; i < 15; i++) { + Customer customer = Const.createNewCustomer(); + assertNotNull(customer); + customer.setUniqueId(UUID.randomUUID()); + customer.setCustomerId(i); + repository.asyncSave(customer).thenAccept(Assertions::assertTrue); + repository.asyncExists(customer).thenAccept(Assertions::assertTrue); + customerList.add(customer); + } + } + + @Test + @Order(3) + public void deleteAndCountCustomer() { + repository.asyncCountAll().thenAccept(count -> assertEquals(15, count)); + repository.asyncDeleteAll(customerList).thenAccept(Assertions::assertTrue); + repository.asyncCountAll().thenAccept(count -> assertEquals(0, count)); + } +} diff --git a/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncDeleteByIdTest.java b/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncDeleteByIdTest.java new file mode 100644 index 00000000..174d9788 --- /dev/null +++ b/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncDeleteByIdTest.java @@ -0,0 +1,39 @@ +package eu.koboo.en2do.test.customer.async; + +import eu.koboo.en2do.test.Const; +import eu.koboo.en2do.test.customer.Customer; +import eu.koboo.en2do.test.customer.CustomerRepositoryTest; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class CustomerAsyncDeleteByIdTest extends CustomerRepositoryTest { + + @Test + @Order(1) + public void cleanUpRepository() { + repository.asyncFindAll().thenAccept(customerList -> { + assertNotNull(customerList); + assertTrue(customerList.isEmpty()); + }); + } + + @Test + @Order(2) + public void saveCustomer() { + Customer customer = Const.createNewCustomer(); + assertNotNull(customer); + repository.asyncSave(customer).thenAccept(Assertions::assertTrue); + repository.asyncExists(customer).thenAccept(Assertions::assertTrue); + } + + @Test + @Order(3) + public void deleteCustomer() { + repository.asyncDeleteById(Const.UNIQUE_ID).thenAccept(Assertions::assertTrue); + repository.asyncExistsById(Const.UNIQUE_ID).thenAccept(Assertions::assertFalse); + repository.asyncCountCustomerId(Const.CUSTOMER_ID).thenAccept(count -> assertEquals(0, count)); + } +} diff --git a/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncDeleteTest.java b/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncDeleteTest.java new file mode 100644 index 00000000..3fbabfc4 --- /dev/null +++ b/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncDeleteTest.java @@ -0,0 +1,41 @@ +package eu.koboo.en2do.test.customer.async; + +import eu.koboo.en2do.test.Const; +import eu.koboo.en2do.test.customer.Customer; +import eu.koboo.en2do.test.customer.CustomerRepositoryTest; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class CustomerAsyncDeleteTest extends CustomerRepositoryTest { + + static Customer customer; + + @Test + @Order(1) + public void cleanUpRepository() { + repository.asyncFindAll().thenAccept(customerList -> { + assertNotNull(customerList); + assertTrue(customerList.isEmpty()); + }); + } + + @Test + @Order(2) + public void saveCustomer() { + customer = Const.createNewCustomer(); + assertNotNull(customer); + repository.asyncSave(customer).thenAccept(Assertions::assertTrue); + repository.asyncExists(customer).thenAccept(Assertions::assertTrue); + } + + @Test + @Order(3) + public void deleteCustomer() { + repository.asyncDelete(customer).thenAccept(Assertions::assertTrue); + repository.asyncExistsById(Const.UNIQUE_ID).thenAccept(Assertions::assertFalse); + repository.asyncCountCustomerId(Const.CUSTOMER_ID).thenAccept(count -> assertEquals(0, count)); + } +} diff --git a/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncDropTest.java b/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncDropTest.java new file mode 100644 index 00000000..d941e4cb --- /dev/null +++ b/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncDropTest.java @@ -0,0 +1,51 @@ +package eu.koboo.en2do.test.customer.async; + +import eu.koboo.en2do.test.Const; +import eu.koboo.en2do.test.customer.Customer; +import eu.koboo.en2do.test.customer.CustomerRepositoryTest; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +public class CustomerAsyncDropTest extends CustomerRepositoryTest { + + static List customerList; + + @Test + @Order(1) + public void cleanUpRepository() { + repository.asyncFindAll().thenAccept(customerList -> { + assertNotNull(customerList); + assertTrue(customerList.isEmpty()); + }); + } + + @Test + @Order(2) + public void saveCustomer() { + customerList = new ArrayList<>(); + for (int i = 0; i < 15; i++) { + Customer customer = Const.createNewCustomer(); + assertNotNull(customer); + customer.setUniqueId(UUID.randomUUID()); + customer.setCustomerId(i); + repository.asyncSave(customer).thenAccept(Assertions::assertTrue); + repository.asyncExists(customer).thenAccept(Assertions::assertTrue); + customerList.add(customer); + } + } + + @Test + @Order(3) + public void deleteAndCountCustomer() { + repository.asyncCountAll().thenAccept(count -> assertEquals(15, count)); + repository.asyncDeleteAll(customerList).thenAccept(Assertions::assertTrue); + repository.asyncCountAll().thenAccept(count -> assertEquals(0, count)); + } +} diff --git a/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncExistsByIdTest.java b/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncExistsByIdTest.java new file mode 100644 index 00000000..0592f73f --- /dev/null +++ b/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncExistsByIdTest.java @@ -0,0 +1,33 @@ +package eu.koboo.en2do.test.customer.async; + +import eu.koboo.en2do.test.Const; +import eu.koboo.en2do.test.customer.Customer; +import eu.koboo.en2do.test.customer.CustomerRepositoryTest; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class CustomerAsyncExistsByIdTest extends CustomerRepositoryTest { + + @Test + @Order(1) + public void cleanUpRepository() { + repository.asyncFindAll().thenAccept(customerList -> { + assertNotNull(customerList); + assertTrue(customerList.isEmpty()); + }); + } + + @Test + @Order(2) + public void saveCustomer() { + Customer customer = Const.createNewCustomer(); + assertNotNull(customer); + repository.asyncExistsById(customer.getUniqueId()).thenAccept(Assertions::assertFalse); + repository.asyncSave(customer).thenAccept(Assertions::assertTrue); + repository.asyncExistsById(customer.getUniqueId()).thenAccept(Assertions::assertTrue); + } +} diff --git a/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncExistsTest.java b/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncExistsTest.java new file mode 100644 index 00000000..04522e14 --- /dev/null +++ b/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncExistsTest.java @@ -0,0 +1,35 @@ +package eu.koboo.en2do.test.customer.async; + +import eu.koboo.en2do.test.Const; +import eu.koboo.en2do.test.customer.Customer; +import eu.koboo.en2do.test.customer.CustomerRepositoryTest; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class CustomerAsyncExistsTest extends CustomerRepositoryTest { + + Customer customer; + + @Test + @Order(1) + public void cleanUpRepository() { + repository.asyncFindAll().thenAccept(customerList -> { + assertNotNull(customerList); + assertTrue(customerList.isEmpty()); + }); + } + + @Test + @Order(2) + public void saveCustomer() { + customer = Const.createNewCustomer(); + assertNotNull(customer); + repository.asyncExists(customer).thenAccept(Assertions::assertFalse); + repository.asyncSave(customer).thenAccept(Assertions::assertTrue); + repository.asyncExists(customer).thenAccept(Assertions::assertTrue); + } +} diff --git a/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncFindAllTest.java b/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncFindAllTest.java new file mode 100644 index 00000000..2d780810 --- /dev/null +++ b/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncFindAllTest.java @@ -0,0 +1,49 @@ +package eu.koboo.en2do.test.customer.async; + +import eu.koboo.en2do.test.Const; +import eu.koboo.en2do.test.customer.Customer; +import eu.koboo.en2do.test.customer.CustomerRepositoryTest; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +public class CustomerAsyncFindAllTest extends CustomerRepositoryTest { + + @Test + @Order(1) + public void cleanUpRepository() { + repository.asyncFindAll().thenAccept(customerList -> { + assertNotNull(customerList); + assertTrue(customerList.isEmpty()); + }); + } + + @Test + @Order(2) + public void saveCustomer() { + for (int i = 0; i < 15; i++) { + Customer customer = Const.createNewCustomer(); + assertNotNull(customer); + customer.setUniqueId(UUID.randomUUID()); + customer.setCustomerId(i); + repository.asyncSave(customer).thenAccept(Assertions::assertTrue); + repository.asyncExists(customer).thenAccept(Assertions::assertTrue); + } + } + + @Test + @Order(3) + public void countCustomer() { + repository.asyncFindAll().thenAccept(customerList -> { + assertNotNull(customerList); + assertFalse(customerList.isEmpty()); + assertEquals(15, customerList.size()); + repository.asyncDeleteAll(customerList).thenAccept(Assertions::assertTrue); + }); + repository.asyncCountAll().thenAccept(count -> assertEquals(0, count)); + } +} diff --git a/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncFindFirstByIdTest.java b/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncFindFirstByIdTest.java new file mode 100644 index 00000000..21b4a20e --- /dev/null +++ b/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncFindFirstByIdTest.java @@ -0,0 +1,46 @@ +package eu.koboo.en2do.test.customer.async; + +import eu.koboo.en2do.test.Const; +import eu.koboo.en2do.test.customer.Customer; +import eu.koboo.en2do.test.customer.CustomerRepositoryTest; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class CustomerAsyncFindFirstByIdTest extends CustomerRepositoryTest { + + @Test + @Order(1) + public void cleanUpRepository() { + repository.asyncFindAll().thenAccept(customerList -> { + assertNotNull(customerList); + assertTrue(customerList.isEmpty()); + }); + } + + @Test + @Order(2) + public void saveCustomer() { + Customer customer = Const.createNewCustomer(); + assertNotNull(customer); + repository.asyncExists(customer).thenAccept(Assertions::assertFalse); + repository.asyncSave(customer).thenAccept(Assertions::assertTrue); + repository.asyncExists(customer).thenAccept(Assertions::assertTrue); + } + + @Test + @Order(3) + public void findCustomer() { + repository.asyncExistsById(Const.UNIQUE_ID).thenAccept(Assertions::assertTrue); + repository.asyncFindFirstById(Const.UNIQUE_ID).thenAccept(customer -> { + assertNotNull(customer); + assertEquals(Const.FIRST_NAME, customer.getFirstName()); + assertEquals(Const.LAST_NAME, customer.getLastName()); + assertEquals(Const.BIRTHDAY, customer.getBirthday()); + assertEquals(Const.PHONE_NUMBER, customer.getPhoneNumber()); + assertEquals(Const.ORDERS.size(), customer.getOrders().size()); + }); + } +} diff --git a/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncPageAllTest.java b/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncPageAllTest.java new file mode 100644 index 00000000..3239ee81 --- /dev/null +++ b/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncPageAllTest.java @@ -0,0 +1,57 @@ +package eu.koboo.en2do.test.customer.async; + +import eu.koboo.en2do.repository.methods.pagination.Pagination; +import eu.koboo.en2do.test.Const; +import eu.koboo.en2do.test.customer.Customer; +import eu.koboo.en2do.test.customer.CustomerRepositoryTest; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +public class CustomerAsyncPageAllTest extends CustomerRepositoryTest { + + @Test + @Order(1) + public void cleanUpRepository() { + repository.asyncFindAll().thenAccept(customerList -> { + assertNotNull(customerList); + assertTrue(customerList.isEmpty()); + }); + } + + @Test + @Order(2) + public void saveCustomer() { + for (int i = 0; i < 15; i++) { + Customer customer = Const.createNewCustomer(); + assertNotNull(customer); + customer.setUniqueId(UUID.randomUUID()); + customer.setCustomerId(i); + repository.asyncSave(customer).thenAccept(Assertions::assertTrue); + repository.asyncExists(customer).thenAccept(Assertions::assertTrue); + } + } + + @Test + @Order(3) + public void findCustomer() { + repository.asyncPageAll(Pagination.of(5).page(2)).thenAccept(customerList -> { + assertNotNull(customerList); + assertFalse(customerList.isEmpty()); + assertEquals(5, customerList.size()); + for (Customer customer : customerList) { + assertNotNull(customer); + System.out.println(customer.getCustomerId()); + assertEquals(Const.FIRST_NAME, customer.getFirstName()); + assertEquals(Const.LAST_NAME, customer.getLastName()); + assertEquals(Const.BIRTHDAY, customer.getBirthday()); + assertEquals(Const.PHONE_NUMBER, customer.getPhoneNumber()); + assertEquals(Const.ORDERS.size(), customer.getOrders().size()); + } + }); + } +} diff --git a/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncSortAllTest.java b/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncSortAllTest.java new file mode 100644 index 00000000..bc766924 --- /dev/null +++ b/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncSortAllTest.java @@ -0,0 +1,57 @@ +package eu.koboo.en2do.test.customer.async; + +import eu.koboo.en2do.repository.methods.sort.Sort; +import eu.koboo.en2do.test.Const; +import eu.koboo.en2do.test.customer.Customer; +import eu.koboo.en2do.test.customer.CustomerRepositoryTest; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +public class CustomerAsyncSortAllTest extends CustomerRepositoryTest { + + @Test + @Order(1) + public void cleanUpRepository() { + repository.asyncFindAll().thenAccept(customerList -> { + assertNotNull(customerList); + assertTrue(customerList.isEmpty()); + }); + } + + @Test + @Order(2) + public void saveCustomer() { + for (int i = 0; i < 15; i++) { + Customer customer = Const.createNewCustomer(); + assertNotNull(customer); + customer.setUniqueId(UUID.randomUUID()); + customer.setCustomerId(i); + repository.asyncSave(customer).thenAccept(Assertions::assertTrue); + repository.asyncExists(customer).thenAccept(Assertions::assertTrue); + } + } + + @Test + @Order(3) + public void findCustomer() { + repository.asyncSortAll(Sort.of().order("customerId", true).limit(10).skip(5)).thenAccept(customerList -> { + assertNotNull(customerList); + assertFalse(customerList.isEmpty()); + assertEquals(5, customerList.size()); + for (Customer customer : customerList) { + assertNotNull(customer); + System.out.println(customer.getCustomerId()); + assertEquals(Const.FIRST_NAME, customer.getFirstName()); + assertEquals(Const.LAST_NAME, customer.getLastName()); + assertEquals(Const.BIRTHDAY, customer.getBirthday()); + assertEquals(Const.PHONE_NUMBER, customer.getPhoneNumber()); + assertEquals(Const.ORDERS.size(), customer.getOrders().size()); + } + }); + } +} diff --git a/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncUpdateAllFieldsRemoveTest.java b/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncUpdateAllFieldsRemoveTest.java new file mode 100644 index 00000000..ac780035 --- /dev/null +++ b/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncUpdateAllFieldsRemoveTest.java @@ -0,0 +1,65 @@ +package eu.koboo.en2do.test.customer.async; + +import eu.koboo.en2do.repository.methods.fields.FieldUpdate; +import eu.koboo.en2do.repository.methods.fields.UpdateBatch; +import eu.koboo.en2do.test.Const; +import eu.koboo.en2do.test.customer.Customer; +import eu.koboo.en2do.test.customer.CustomerRepositoryTest; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +public class CustomerAsyncUpdateAllFieldsRemoveTest extends CustomerRepositoryTest { + + @Test + @Order(1) + public void cleanUpRepository() { + repository.asyncFindAll().thenAccept(customerList -> { + assertNotNull(customerList); + assertTrue(customerList.isEmpty()); + }); + } + + @Test + @Order(2) + public void saveCustomer() { + for (int i = 0; i < 15; i++) { + Customer customer = Const.createNewCustomer(); + assertNotNull(customer); + customer.setUniqueId(UUID.randomUUID()); + customer.setCustomerId(i); + repository.asyncSave(customer).thenAccept(Assertions::assertTrue); + repository.asyncExists(customer).thenAccept(Assertions::assertTrue); + } + } + + @Test + @Order(3) + public void countCustomer() { + repository.asyncCountAll().thenAccept(count -> assertEquals(15, count)); + } + + @Test + @Order(4) + public void setFieldValue() { + repository.asyncUpdateAllFields(UpdateBatch.of(FieldUpdate.remove("postalCode"))) + .thenAccept(Assertions::assertTrue); + } + + @Test + @Order(5) + public void checkFieldValue() { + repository.asyncFindAll().thenAccept(customerList -> { + assertNotNull(customerList); + assertFalse(customerList.isEmpty()); + assertEquals(15, customerList.size()); + for (Customer customer : customerList) { + assertNull(customer.getPostalCode()); + } + }); + } +} diff --git a/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncUpdateAllFieldsRenameTest.java b/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncUpdateAllFieldsRenameTest.java new file mode 100644 index 00000000..53fc3b13 --- /dev/null +++ b/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncUpdateAllFieldsRenameTest.java @@ -0,0 +1,65 @@ +package eu.koboo.en2do.test.customer.async; + +import eu.koboo.en2do.repository.methods.fields.FieldUpdate; +import eu.koboo.en2do.repository.methods.fields.UpdateBatch; +import eu.koboo.en2do.test.Const; +import eu.koboo.en2do.test.customer.Customer; +import eu.koboo.en2do.test.customer.CustomerRepositoryTest; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +public class CustomerAsyncUpdateAllFieldsRenameTest extends CustomerRepositoryTest { + + @Test + @Order(1) + public void cleanUpRepository() { + repository.asyncFindAll().thenAccept(customerList -> { + assertNotNull(customerList); + assertTrue(customerList.isEmpty()); + }); + } + + @Test + @Order(2) + public void saveCustomer() { + for (int i = 0; i < 15; i++) { + Customer customer = Const.createNewCustomer(); + assertNotNull(customer); + customer.setUniqueId(UUID.randomUUID()); + customer.setCustomerId(i); + repository.asyncSave(customer).thenAccept(Assertions::assertTrue); + repository.asyncExists(customer).thenAccept(Assertions::assertTrue); + } + } + + @Test + @Order(3) + public void countCustomer() { + repository.asyncCountAll().thenAccept(count -> assertEquals(15, count)); + } + + @Test + @Order(4) + public void setFieldValue() { + repository.asyncUpdateAllFields(UpdateBatch.of(FieldUpdate.rename("balance", "balanceRenamed"))) + .thenAccept(Assertions::assertTrue); + } + + @Test + @Order(5) + public void checkFieldValue() { + repository.asyncFindAll().thenAccept(customerList -> { + assertNotNull(customerList); + assertFalse(customerList.isEmpty()); + assertEquals(15, customerList.size()); + for (Customer customer : customerList) { + assertEquals(987654321, customer.getPostalCode()); + } + }); + } +} diff --git a/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncUpdateAllFieldsSetTest.java b/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncUpdateAllFieldsSetTest.java new file mode 100644 index 00000000..99397a52 --- /dev/null +++ b/src/test/java/eu/koboo/en2do/test/customer/async/CustomerAsyncUpdateAllFieldsSetTest.java @@ -0,0 +1,66 @@ +package eu.koboo.en2do.test.customer.async; + +import eu.koboo.en2do.repository.methods.fields.FieldUpdate; +import eu.koboo.en2do.repository.methods.fields.UpdateBatch; +import eu.koboo.en2do.test.Const; +import eu.koboo.en2do.test.customer.Customer; +import eu.koboo.en2do.test.customer.CustomerRepositoryTest; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +public class CustomerAsyncUpdateAllFieldsSetTest extends CustomerRepositoryTest { + + @Test + @Order(1) + public void cleanUpRepository() { + repository.asyncFindAll().thenAccept(customerList -> { + assertNotNull(customerList); + assertTrue(customerList.isEmpty()); + }); + } + + @Test + @Order(2) + public void saveCustomer() { + for (int i = 0; i < 15; i++) { + Customer customer = Const.createNewCustomer(); + assertNotNull(customer); + customer.setUniqueId(UUID.randomUUID()); + customer.setCustomerId(i); + repository.asyncSave(customer).thenAccept(Assertions::assertTrue); + repository.asyncExists(customer).thenAccept(Assertions::assertTrue); + } + } + + @Test + @Order(3) + public void countCustomer() { + repository.asyncCountAll().thenAccept(count -> assertEquals(15, count)); + } + + @Test + @Order(4) + public void setFieldValue() { + repository.asyncUpdateAllFields(UpdateBatch.of(FieldUpdate.set("postalCode", 987654321))) + .thenAccept(Assertions::assertTrue); + } + + @Test + @Order(5) + public void checkFieldValue() { + repository.asyncFindAll().thenAccept(customerList -> { + assertNotNull(customerList); + assertFalse(customerList.isEmpty()); + assertEquals(15, customerList.size()); + for (Customer customer : customerList) { + assertEquals(0, customer.getBalance()); + assertEquals(Const.BALANCE, customer.getBalanceRenamed()); + } + }); + } +} diff --git a/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerCountByCustomerIdTest.java b/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerCountByCustomerIdTest.java index 66f09c31..f92abf5a 100644 --- a/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerCountByCustomerIdTest.java +++ b/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerCountByCustomerIdTest.java @@ -25,6 +25,7 @@ public void cleanUpRepository() { @Order(2) public void saveCustomer() { Customer customer = Const.createNewCustomer(); + assertNotNull(customer); customer.setUniqueId(UUID.randomUUID()); assertTrue(repository.save(customer)); assertTrue(repository.exists(customer)); diff --git a/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerCountByLastNameTest.java b/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerCountByLastNameTest.java index 45873fd3..45b05103 100644 --- a/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerCountByLastNameTest.java +++ b/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerCountByLastNameTest.java @@ -25,6 +25,7 @@ public void cleanUpRepository() { @Order(2) public void saveCustomer() { Customer customer = Const.createNewCustomer(); + assertNotNull(customer); customer.setUniqueId(UUID.randomUUID()); assertTrue(repository.save(customer)); assertTrue(repository.exists(customer)); diff --git a/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerDeleteByFirstNameTest.java b/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerDeleteByFirstNameTest.java index 98e3c009..4b158ad2 100644 --- a/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerDeleteByFirstNameTest.java +++ b/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerDeleteByFirstNameTest.java @@ -24,6 +24,7 @@ public void cleanUpRepository() { @Order(2) public void saveCustomer() { Customer customer = Const.createNewCustomer(); + assertNotNull(customer); assertTrue(repository.save(customer)); assertTrue(repository.exists(customer)); } diff --git a/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerExistsByLastNameContainsTest.java b/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerExistsByLastNameContainsTest.java index 4a68e6bb..e3021889 100644 --- a/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerExistsByLastNameContainsTest.java +++ b/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerExistsByLastNameContainsTest.java @@ -26,6 +26,7 @@ public void cleanUpRepository() { @Order(2) public void saveCustomer() { Customer customer = Const.createNewCustomer(); + assertNotNull(customer); customer.setUniqueId(UUID.randomUUID()); assertTrue(repository.save(customer)); assertTrue(repository.exists(customer)); diff --git a/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerExistsByLastNameTest.java b/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerExistsByLastNameTest.java index 24971c38..9e4d5c5c 100644 --- a/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerExistsByLastNameTest.java +++ b/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerExistsByLastNameTest.java @@ -26,6 +26,7 @@ public void cleanUpRepository() { @Order(2) public void saveCustomer() { Customer customer = Const.createNewCustomer(); + assertNotNull(customer); customer.setUniqueId(UUID.randomUUID()); assertTrue(repository.save(customer)); assertTrue(repository.exists(customer)); diff --git a/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerFindFirstByFirstNameContainsTest.java b/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerFindFirstByFirstNameContainsTest.java index 9db5d043..9d45f968 100644 --- a/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerFindFirstByFirstNameContainsTest.java +++ b/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerFindFirstByFirstNameContainsTest.java @@ -25,6 +25,7 @@ public void cleanUpRepository() { public void saveCustomer() { Customer customer = Const.createNewCustomer(); assertNotNull(customer); + assertNotNull(customer); assertTrue(repository.save(customer)); assertTrue(repository.exists(customer)); } diff --git a/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerFindManyCustomerIdNotInTest.java b/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerFindManyCustomerIdNotInTest.java index 07740e39..7d755452 100644 --- a/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerFindManyCustomerIdNotInTest.java +++ b/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerFindManyCustomerIdNotInTest.java @@ -27,6 +27,7 @@ public void cleanUpRepository() { public void saveCustomer() { for (int i = 0; i < 10; i++) { Customer customer = Const.createNewCustomer(); + assertNotNull(customer); customer.setUniqueId(UUID.randomUUID()); customer.setCustomerId(i); assertTrue(repository.save(customer)); diff --git a/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerFindManySortAnnotationTest.java b/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerFindManySortAnnotationTest.java index dc10b672..8f58dcec 100644 --- a/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerFindManySortAnnotationTest.java +++ b/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerFindManySortAnnotationTest.java @@ -26,6 +26,7 @@ public void cleanUpRepository() { public void saveCustomer() { for (int i = 0; i < 15; i++) { Customer customer = Const.createNewCustomer(); + assertNotNull(customer); customer.setUniqueId(UUID.randomUUID()); customer.setCustomerId(i); assertTrue(repository.save(customer)); diff --git a/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerFindManySortParameterTest.java b/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerFindManySortParameterTest.java index e221098c..e364e765 100644 --- a/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerFindManySortParameterTest.java +++ b/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerFindManySortParameterTest.java @@ -27,6 +27,7 @@ public void cleanUpRepository() { public void saveCustomer() { for (int i = 0; i < 15; i++) { Customer customer = Const.createNewCustomer(); + assertNotNull(customer); customer.setUniqueId(UUID.randomUUID()); customer.setCustomerId(i); assertTrue(repository.save(customer)); diff --git a/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerTransformTest.java b/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerTransformTest.java index 0fb5b8de..a22383fe 100644 --- a/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerTransformTest.java +++ b/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerTransformTest.java @@ -26,6 +26,7 @@ public void cleanUpRepository() { public void saveCustomer() { for (int i = 0; i < 15; i++) { Customer customer = Const.createNewCustomer(); + assertNotNull(customer); customer.setUniqueId(UUID.randomUUID()); customer.setCustomerId(i); assertTrue(repository.save(customer)); diff --git a/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerUpdateFieldByFirstNameRemoveTest.java b/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerUpdateFieldByFirstNameRemoveTest.java new file mode 100644 index 00000000..c39acfbf --- /dev/null +++ b/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerUpdateFieldByFirstNameRemoveTest.java @@ -0,0 +1,67 @@ +package eu.koboo.en2do.test.customer.dynamic; + +import eu.koboo.en2do.repository.methods.fields.FieldUpdate; +import eu.koboo.en2do.repository.methods.fields.UpdateBatch; +import eu.koboo.en2do.test.Const; +import eu.koboo.en2do.test.customer.Customer; +import eu.koboo.en2do.test.customer.CustomerRepositoryTest; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +public class CustomerUpdateFieldByFirstNameRemoveTest extends CustomerRepositoryTest { + + @Test + @Order(1) + public void cleanUpRepository() { + List customerList = repository.findAll(); + assertNotNull(customerList); + assertTrue(customerList.isEmpty()); + } + + @Test + @Order(2) + public void saveCustomer() { + for (int i = 0; i < 15; i++) { + Customer customer = Const.createNewCustomer(); + assertNotNull(customer); + customer.setUniqueId(UUID.randomUUID()); + customer.setCustomerId(i); + assertTrue(repository.save(customer)); + assertTrue(repository.exists(customer)); + } + } + + @Test + @Order(3) + public void countCustomer() { + List customerList = repository.findAll(); + assertNotNull(customerList); + assertFalse(customerList.isEmpty()); + assertEquals(15, customerList.size()); + } + + @Test + @Order(4) + public void setFieldValue() { + assertTrue(repository.updateFieldsByFirstName("Rainer", + UpdateBatch.of(FieldUpdate.remove("postalCode")) + )); + } + + @Test + @Order(5) + public void checkFieldValue() { + List customerList = repository.findAll(); + assertNotNull(customerList); + assertFalse(customerList.isEmpty()); + assertEquals(15, customerList.size()); + for (Customer customer : customerList) { + assertNull(customer.getPostalCode()); + } + } +} diff --git a/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerUpdateFieldByFirstNameRenameTest.java b/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerUpdateFieldByFirstNameRenameTest.java new file mode 100644 index 00000000..cc3ea2fd --- /dev/null +++ b/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerUpdateFieldByFirstNameRenameTest.java @@ -0,0 +1,68 @@ +package eu.koboo.en2do.test.customer.dynamic; + +import eu.koboo.en2do.repository.methods.fields.FieldUpdate; +import eu.koboo.en2do.repository.methods.fields.UpdateBatch; +import eu.koboo.en2do.test.Const; +import eu.koboo.en2do.test.customer.Customer; +import eu.koboo.en2do.test.customer.CustomerRepositoryTest; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +public class CustomerUpdateFieldByFirstNameRenameTest extends CustomerRepositoryTest { + + @Test + @Order(1) + public void cleanUpRepository() { + List customerList = repository.findAll(); + assertNotNull(customerList); + assertTrue(customerList.isEmpty()); + } + + @Test + @Order(2) + public void saveCustomer() { + for (int i = 0; i < 15; i++) { + Customer customer = Const.createNewCustomer(); + assertNotNull(customer); + customer.setUniqueId(UUID.randomUUID()); + customer.setCustomerId(i); + assertTrue(repository.save(customer)); + assertTrue(repository.exists(customer)); + } + } + + @Test + @Order(3) + public void countCustomer() { + List customerList = repository.findAll(); + assertNotNull(customerList); + assertFalse(customerList.isEmpty()); + assertEquals(15, customerList.size()); + } + + @Test + @Order(4) + public void setFieldValue() { + assertTrue(repository.updateFieldsByFirstName("Rainer", + UpdateBatch.of(FieldUpdate.rename("balance", "balanceRenamed")) + )); + } + + @Test + @Order(5) + public void checkFieldValue() { + List customerList = repository.findAll(); + assertNotNull(customerList); + assertFalse(customerList.isEmpty()); + assertEquals(15, customerList.size()); + for (Customer customer : customerList) { + assertEquals(0, customer.getBalance()); + assertEquals(Const.BALANCE, customer.getBalanceRenamed()); + } + } +} diff --git a/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerUpdateFieldByFirstNameSetTest.java b/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerUpdateFieldByFirstNameSetTest.java new file mode 100644 index 00000000..1897b3ac --- /dev/null +++ b/src/test/java/eu/koboo/en2do/test/customer/dynamic/CustomerUpdateFieldByFirstNameSetTest.java @@ -0,0 +1,67 @@ +package eu.koboo.en2do.test.customer.dynamic; + +import eu.koboo.en2do.repository.methods.fields.FieldUpdate; +import eu.koboo.en2do.repository.methods.fields.UpdateBatch; +import eu.koboo.en2do.test.Const; +import eu.koboo.en2do.test.customer.Customer; +import eu.koboo.en2do.test.customer.CustomerRepositoryTest; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +public class CustomerUpdateFieldByFirstNameSetTest extends CustomerRepositoryTest { + + @Test + @Order(1) + public void cleanUpRepository() { + List customerList = repository.findAll(); + assertNotNull(customerList); + assertTrue(customerList.isEmpty()); + } + + @Test + @Order(2) + public void saveCustomer() { + for (int i = 0; i < 15; i++) { + Customer customer = Const.createNewCustomer(); + assertNotNull(customer); + customer.setUniqueId(UUID.randomUUID()); + customer.setCustomerId(i); + assertTrue(repository.save(customer)); + assertTrue(repository.exists(customer)); + } + } + + @Test + @Order(3) + public void countCustomer() { + List customerList = repository.findAll(); + assertNotNull(customerList); + assertFalse(customerList.isEmpty()); + assertEquals(15, customerList.size()); + } + + @Test + @Order(4) + public void setFieldValue() { + assertTrue(repository.updateFieldsByFirstName("Rainer", + UpdateBatch.of(FieldUpdate.set("postalCode", 987654321)) + )); + } + + @Test + @Order(5) + public void checkFieldValue() { + List customerList = repository.findAll(); + assertNotNull(customerList); + assertFalse(customerList.isEmpty()); + assertEquals(15, customerList.size()); + for (Customer customer : customerList) { + assertEquals(987654321, customer.getPostalCode()); + } + } +} diff --git a/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerCountAllTest.java b/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerCountAllTest.java index 670c0283..fa71aeaf 100644 --- a/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerCountAllTest.java +++ b/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerCountAllTest.java @@ -26,6 +26,7 @@ public void cleanUpRepository() { public void saveCustomer() { for (int i = 0; i < 15; i++) { Customer customer = Const.createNewCustomer(); + assertNotNull(customer); customer.setUniqueId(UUID.randomUUID()); customer.setCustomerId(i); assertTrue(repository.save(customer)); diff --git a/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerDeleteAllTest.java b/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerDeleteAllTest.java index 49fb8bd9..6a9b7a1d 100644 --- a/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerDeleteAllTest.java +++ b/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerDeleteAllTest.java @@ -30,6 +30,7 @@ public void saveCustomer() { customerList = new ArrayList<>(); for (int i = 0; i < 15; i++) { Customer customer = Const.createNewCustomer(); + assertNotNull(customer); customer.setUniqueId(UUID.randomUUID()); customer.setCustomerId(i); assertTrue(repository.save(customer)); diff --git a/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerDeleteByIdTest.java b/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerDeleteByIdTest.java index 30fe5511..e3052299 100644 --- a/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerDeleteByIdTest.java +++ b/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerDeleteByIdTest.java @@ -24,6 +24,7 @@ public void cleanUpRepository() { @Order(2) public void saveCustomer() { Customer customer = Const.createNewCustomer(); + assertNotNull(customer); assertTrue(repository.save(customer)); assertTrue(repository.exists(customer)); } diff --git a/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerDeleteTest.java b/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerDeleteTest.java index bc1c7af2..efe773ea 100644 --- a/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerDeleteTest.java +++ b/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerDeleteTest.java @@ -26,6 +26,7 @@ public void cleanUpRepository() { @Order(2) public void saveCustomer() { customer = Const.createNewCustomer(); + assertNotNull(customer); assertTrue(repository.save(customer)); assertTrue(repository.exists(customer)); } diff --git a/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerDropTest.java b/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerDropTest.java index ff4b6a49..bf1a794d 100644 --- a/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerDropTest.java +++ b/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerDropTest.java @@ -30,6 +30,7 @@ public void saveCustomer() { customerList = new ArrayList<>(); for (int i = 0; i < 15; i++) { Customer customer = Const.createNewCustomer(); + assertNotNull(customer); customer.setUniqueId(UUID.randomUUID()); customer.setCustomerId(i); assertTrue(repository.save(customer)); diff --git a/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerFindAllTest.java b/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerFindAllTest.java index cb64cb18..e27dc75c 100644 --- a/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerFindAllTest.java +++ b/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerFindAllTest.java @@ -26,6 +26,7 @@ public void cleanUpRepository() { public void saveCustomer() { for (int i = 0; i < 15; i++) { Customer customer = Const.createNewCustomer(); + assertNotNull(customer); customer.setUniqueId(UUID.randomUUID()); customer.setCustomerId(i); assertTrue(repository.save(customer)); diff --git a/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerPageAllTest.java b/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerPageAllTest.java index 4a3f3e22..9a259318 100644 --- a/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerPageAllTest.java +++ b/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerPageAllTest.java @@ -27,6 +27,7 @@ public void cleanUpRepository() { public void saveCustomer() { for (int i = 0; i < 15; i++) { Customer customer = Const.createNewCustomer(); + assertNotNull(customer); customer.setUniqueId(UUID.randomUUID()); customer.setCustomerId(i); assertTrue(repository.save(customer)); diff --git a/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerSortAllTest.java b/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerSortAllTest.java index 4745d0bc..dd0b4a4b 100644 --- a/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerSortAllTest.java +++ b/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerSortAllTest.java @@ -27,6 +27,7 @@ public void cleanUpRepository() { public void saveCustomer() { for (int i = 0; i < 15; i++) { Customer customer = Const.createNewCustomer(); + assertNotNull(customer); customer.setUniqueId(UUID.randomUUID()); customer.setCustomerId(i); assertTrue(repository.save(customer)); diff --git a/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerUpdateAllFieldsRemoveTest.java b/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerUpdateAllFieldsRemoveTest.java new file mode 100644 index 00000000..4eea2120 --- /dev/null +++ b/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerUpdateAllFieldsRemoveTest.java @@ -0,0 +1,67 @@ +package eu.koboo.en2do.test.customer.predefined; + +import eu.koboo.en2do.repository.methods.fields.FieldUpdate; +import eu.koboo.en2do.repository.methods.fields.UpdateBatch; +import eu.koboo.en2do.test.Const; +import eu.koboo.en2do.test.customer.Customer; +import eu.koboo.en2do.test.customer.CustomerRepositoryTest; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +public class CustomerUpdateAllFieldsRemoveTest extends CustomerRepositoryTest { + + @Test + @Order(1) + public void cleanUpRepository() { + List customerList = repository.findAll(); + assertNotNull(customerList); + assertTrue(customerList.isEmpty()); + } + + @Test + @Order(2) + public void saveCustomer() { + for (int i = 0; i < 15; i++) { + Customer customer = Const.createNewCustomer(); + assertNotNull(customer); + customer.setUniqueId(UUID.randomUUID()); + customer.setCustomerId(i); + assertTrue(repository.save(customer)); + assertTrue(repository.exists(customer)); + } + } + + @Test + @Order(3) + public void countCustomer() { + List customerList = repository.findAll(); + assertNotNull(customerList); + assertFalse(customerList.isEmpty()); + assertEquals(15, customerList.size()); + } + + @Test + @Order(4) + public void setFieldValue() { + assertTrue(repository.updateAllFields( + UpdateBatch.of(FieldUpdate.remove("postalCode")) + )); + } + + @Test + @Order(5) + public void checkFieldValue() { + List customerList = repository.findAll(); + assertNotNull(customerList); + assertFalse(customerList.isEmpty()); + assertEquals(15, customerList.size()); + for (Customer customer : customerList) { + assertNull(customer.getPostalCode()); + } + } +} diff --git a/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerUpdateAllFieldsRenameTest.java b/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerUpdateAllFieldsRenameTest.java new file mode 100644 index 00000000..ba7f53e2 --- /dev/null +++ b/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerUpdateAllFieldsRenameTest.java @@ -0,0 +1,68 @@ +package eu.koboo.en2do.test.customer.predefined; + +import eu.koboo.en2do.repository.methods.fields.FieldUpdate; +import eu.koboo.en2do.repository.methods.fields.UpdateBatch; +import eu.koboo.en2do.test.Const; +import eu.koboo.en2do.test.customer.Customer; +import eu.koboo.en2do.test.customer.CustomerRepositoryTest; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +public class CustomerUpdateAllFieldsRenameTest extends CustomerRepositoryTest { + + @Test + @Order(1) + public void cleanUpRepository() { + List customerList = repository.findAll(); + assertNotNull(customerList); + assertTrue(customerList.isEmpty()); + } + + @Test + @Order(2) + public void saveCustomer() { + for (int i = 0; i < 15; i++) { + Customer customer = Const.createNewCustomer(); + assertNotNull(customer); + customer.setUniqueId(UUID.randomUUID()); + customer.setCustomerId(i); + assertTrue(repository.save(customer)); + assertTrue(repository.exists(customer)); + } + } + + @Test + @Order(3) + public void countCustomer() { + List customerList = repository.findAll(); + assertNotNull(customerList); + assertFalse(customerList.isEmpty()); + assertEquals(15, customerList.size()); + } + + @Test + @Order(4) + public void setFieldValue() { + assertTrue(repository.updateAllFields( + UpdateBatch.of(FieldUpdate.rename("balance", "balanceRenamed")) + )); + } + + @Test + @Order(5) + public void checkFieldValue() { + List customerList = repository.findAll(); + assertNotNull(customerList); + assertFalse(customerList.isEmpty()); + assertEquals(15, customerList.size()); + for (Customer customer : customerList) { + assertEquals(0, customer.getBalance()); + assertEquals(Const.BALANCE, customer.getBalanceRenamed()); + } + } +} diff --git a/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerUpdateAllFieldsSetTest.java b/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerUpdateAllFieldsSetTest.java new file mode 100644 index 00000000..b3a5a815 --- /dev/null +++ b/src/test/java/eu/koboo/en2do/test/customer/predefined/CustomerUpdateAllFieldsSetTest.java @@ -0,0 +1,67 @@ +package eu.koboo.en2do.test.customer.predefined; + +import eu.koboo.en2do.repository.methods.fields.FieldUpdate; +import eu.koboo.en2do.repository.methods.fields.UpdateBatch; +import eu.koboo.en2do.test.Const; +import eu.koboo.en2do.test.customer.Customer; +import eu.koboo.en2do.test.customer.CustomerRepositoryTest; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +public class CustomerUpdateAllFieldsSetTest extends CustomerRepositoryTest { + + @Test + @Order(1) + public void cleanUpRepository() { + List customerList = repository.findAll(); + assertNotNull(customerList); + assertTrue(customerList.isEmpty()); + } + + @Test + @Order(2) + public void saveCustomer() { + for (int i = 0; i < 15; i++) { + Customer customer = Const.createNewCustomer(); + assertNotNull(customer); + customer.setUniqueId(UUID.randomUUID()); + customer.setCustomerId(i); + assertTrue(repository.save(customer)); + assertTrue(repository.exists(customer)); + } + } + + @Test + @Order(3) + public void countCustomer() { + List customerList = repository.findAll(); + assertNotNull(customerList); + assertFalse(customerList.isEmpty()); + assertEquals(15, customerList.size()); + } + + @Test + @Order(4) + public void setFieldValue() { + assertTrue(repository.updateAllFields( + UpdateBatch.of(FieldUpdate.set("postalCode", 987654321)) + )); + } + + @Test + @Order(5) + public void checkFieldValue() { + List customerList = repository.findAll(); + assertNotNull(customerList); + assertFalse(customerList.isEmpty()); + assertEquals(15, customerList.size()); + for (Customer customer : customerList) { + assertEquals(987654321, customer.getPostalCode()); + } + } +} diff --git a/src/test/java/eu/koboo/en2do/test/customerextended/CustomerExtendedRepositoryTest.java b/src/test/java/eu/koboo/en2do/test/customerextended/CustomerExtendedRepositoryTest.java index 3820b1ff..a1e26a74 100644 --- a/src/test/java/eu/koboo/en2do/test/customerextended/CustomerExtendedRepositoryTest.java +++ b/src/test/java/eu/koboo/en2do/test/customerextended/CustomerExtendedRepositoryTest.java @@ -1,13 +1,14 @@ package eu.koboo.en2do.test.customerextended; import eu.koboo.en2do.test.RepositoryTest; +import org.jetbrains.annotations.NotNull; import java.util.UUID; public class CustomerExtendedRepositoryTest extends RepositoryTest { @Override - public Class repositoryClass() { + public @NotNull Class repositoryClass() { return CustomerExtendedRepository.class; } }