From e227a2a5d14c41e22c2ec9f2f18a7a37f35faad7 Mon Sep 17 00:00:00 2001 From: Otavio Santana Date: Sat, 2 Mar 2024 20:43:01 +0000 Subject: [PATCH] feat: remove both communication column and document Signed-off-by: Otavio Santana --- .../jnosql-communication-column/.gitignore | 16 - .../jnosql-communication-column/pom.xml | 39 -- .../src/main/java/module-info.java | 22 - .../column/ColumnConfiguration.java | 65 -- .../column/ColumnDeleteQuery.java | 381 ----------- .../column/ColumnDeleteQueryParams.java | 49 -- .../communication/column/ColumnEntity.java | 330 ---------- .../communication/column/ColumnManager.java | 216 ------ .../column/ColumnManagerFactory.java | 34 - .../column/ColumnObserverParser.java | 51 -- .../column/ColumnPreparedStatement.java | 190 ------ .../communication/column/ColumnQuery.java | 623 ------------------ .../column/ColumnQueryParams.java | 47 -- .../column/ColumnQueryParser.java | 94 --- .../jnosql/communication/column/Columns.java | 84 --- .../column/ConditionQueryParser.java | 52 -- .../column/ConditionQuerySupplier.java | 40 -- .../communication/column/Conditions.java | 75 --- .../communication/column/DefaultColumn.java | 47 -- .../column/DefaultColumnDeleteQuery.java | 64 -- .../column/DefaultColumnQuery.java | 85 --- .../column/DefaultColumnQueryBuilder.java | 162 ----- .../column/DefaultDeleteQueryBuilder.java | 113 ---- .../DefaultFluentColumnQueryBuilder.java | 207 ------ .../DefaultFluentDeleteQueryBuilder.java | 143 ---- .../column/DeleteQueryParser.java | 101 --- .../column/InsertQueryParser.java | 90 --- .../communication/column/JsonObjects.java | 38 -- .../column/SelectQueryParser.java | 116 ---- .../column/UpdateQueryParser.java | 84 --- .../jnosql/communication/column/Values.java | 73 -- .../communication/column/package-info.java | 28 - .../column/ColumnDeleteQueryParamsTest.java | 155 ----- .../column/ColumnDeleteQueryTest.java | 65 -- .../column/ColumnEntityTest.java | 438 ------------ .../column/ColumnParamsTest.java | 41 -- .../communication/column/ColumnTest.java | 86 --- .../communication/column/ColumnsTest.java | 65 -- .../column/DefaultColumnConditionTest.java | 247 ------- .../column/DefaultColumnDeleteQueryTest.java | 63 -- .../column/DefaultColumnQueryBuilderTest.java | 362 ---------- .../column/DefaultColumnQueryParserTest.java | 230 ------- .../column/DefaultColumnQueryTest.java | 116 ---- .../column/DefaultDeleteQueryBuilderTest.java | 265 -------- .../column/DefaultSelectQueryBuilderTest.java | 343 ---------- .../column/DeleteQueryParserTest.java | 397 ----------- .../column/InsertQueryParserTest.java | 259 -------- .../column/SelectQueryParserTest.java | 508 -------------- .../column/UpdateQueryParserTest.java | 135 ---- .../jnosql-communication-document/.gitignore | 16 - .../jnosql-communication-document/pom.xml | 44 -- .../src/main/java/module-info.java | 21 - .../document/BaseQueryBuilder.java | 109 --- .../document/ConditionQueryParser.java | 51 -- .../document/ConditionQuerySupplier.java | 40 -- .../communication/document/Conditions.java | 85 --- .../document/DefaultDocument.java | 43 -- .../document/DefaultDocumentDeleteQuery.java | 63 -- .../DefaultDocumentDeleteQueryBuilder.java | 113 ---- .../document/DefaultDocumentQuery.java | 80 --- .../document/DefaultDocumentQueryBuilder.java | 162 ----- .../DefaultFluentDeleteQueryBuilder.java | 144 ---- .../DefaultFluentDocumentQueryBuilder.java | 207 ------ .../document/DeleteQueryParser.java | 99 --- .../communication/document/Document.java | 86 --- .../document/DocumentCondition.java | 494 -------------- .../document/DocumentConditionProvider.java | 46 -- .../document/DocumentConfiguration.java | 64 -- .../document/DocumentDeleteQuery.java | 374 ----------- .../document/DocumentDeleteQueryParams.java | 72 -- .../document/DocumentEntity.java | 346 ---------- .../document/DocumentManager.java | 214 ------ .../document/DocumentManagerFactory.java | 34 - .../document/DocumentObserverParser.java | 51 -- .../document/DocumentPreparedStatement.java | 193 ------ .../communication/document/DocumentQuery.java | 614 ----------------- .../document/DocumentQueryParams.java | 71 -- .../document/DocumentQueryParser.java | 99 --- .../communication/document/Documents.java | 85 --- .../document/InsertQueryParser.java | 86 --- .../communication/document/JsonObjects.java | 39 -- .../document/SelectQueryParser.java | 108 --- .../document/UpdateQueryParser.java | 78 --- .../jnosql/communication/document/Values.java | 74 --- .../communication/document/package-info.java | 45 -- .../DefaultDocumentDeleteQueryTest.java | 67 -- .../DefaultDocumentQueryBuilderTest.java | 307 --------- .../document/DefaultDocumentQueryTest.java | 123 ---- .../DefaultFluentDeleteQueryBuilderTest.java | 266 -------- ...DefaultFluentDocumentQueryBuilderTest.java | 344 ---------- .../document/DeleteQueryParserTest.java | 398 ----------- .../document/DocumentConditionTest.java | 246 ------- .../document/DocumentEntityTest.java | 451 ------------- .../document/DocumentParamsTest.java | 41 -- .../document/DocumentQueryBuilderTest.java | 362 ---------- .../document/DocumentQueryParserTest.java | 232 ------- .../communication/document/DocumentTest.java | 88 --- .../communication/document/DocumentsTest.java | 62 -- .../document/InsertQueryParserTest.java | 259 -------- .../document/SelectQueryParserTest.java | 513 -------------- .../document/UpdateQueryParserTest.java | 138 ---- 101 files changed, 15981 deletions(-) delete mode 100644 jnosql-communication/jnosql-communication-column/.gitignore delete mode 100644 jnosql-communication/jnosql-communication-column/pom.xml delete mode 100644 jnosql-communication/jnosql-communication-column/src/main/java/module-info.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnConfiguration.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnDeleteQuery.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnDeleteQueryParams.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnEntity.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnManager.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnManagerFactory.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnObserverParser.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnPreparedStatement.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnQuery.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnQueryParams.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnQueryParser.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/Columns.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ConditionQueryParser.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ConditionQuerySupplier.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/Conditions.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/DefaultColumn.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/DefaultColumnDeleteQuery.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/DefaultColumnQuery.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/DefaultColumnQueryBuilder.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/DefaultDeleteQueryBuilder.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/DefaultFluentColumnQueryBuilder.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/DefaultFluentDeleteQueryBuilder.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/DeleteQueryParser.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/InsertQueryParser.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/JsonObjects.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/SelectQueryParser.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/UpdateQueryParser.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/Values.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/package-info.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/ColumnDeleteQueryParamsTest.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/ColumnDeleteQueryTest.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/ColumnEntityTest.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/ColumnParamsTest.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/ColumnTest.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/ColumnsTest.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/DefaultColumnConditionTest.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/DefaultColumnDeleteQueryTest.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/DefaultColumnQueryBuilderTest.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/DefaultColumnQueryParserTest.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/DefaultColumnQueryTest.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/DefaultDeleteQueryBuilderTest.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/DefaultSelectQueryBuilderTest.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/DeleteQueryParserTest.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/InsertQueryParserTest.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/SelectQueryParserTest.java delete mode 100644 jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/UpdateQueryParserTest.java delete mode 100644 jnosql-communication/jnosql-communication-document/.gitignore delete mode 100644 jnosql-communication/jnosql-communication-document/pom.xml delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/module-info.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/BaseQueryBuilder.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/ConditionQueryParser.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/ConditionQuerySupplier.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/Conditions.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DefaultDocument.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DefaultDocumentDeleteQuery.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DefaultDocumentDeleteQueryBuilder.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DefaultDocumentQuery.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DefaultDocumentQueryBuilder.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DefaultFluentDeleteQueryBuilder.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DefaultFluentDocumentQueryBuilder.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DeleteQueryParser.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/Document.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentCondition.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentConditionProvider.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentConfiguration.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentDeleteQuery.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentDeleteQueryParams.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentEntity.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentManager.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentManagerFactory.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentObserverParser.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentPreparedStatement.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentQuery.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentQueryParams.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentQueryParser.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/Documents.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/InsertQueryParser.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/JsonObjects.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/SelectQueryParser.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/UpdateQueryParser.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/Values.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/package-info.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DefaultDocumentDeleteQueryTest.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DefaultDocumentQueryBuilderTest.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DefaultDocumentQueryTest.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DefaultFluentDeleteQueryBuilderTest.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DefaultFluentDocumentQueryBuilderTest.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DeleteQueryParserTest.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DocumentConditionTest.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DocumentEntityTest.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DocumentParamsTest.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DocumentQueryBuilderTest.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DocumentQueryParserTest.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DocumentTest.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DocumentsTest.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/InsertQueryParserTest.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/SelectQueryParserTest.java delete mode 100644 jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/UpdateQueryParserTest.java diff --git a/jnosql-communication/jnosql-communication-column/.gitignore b/jnosql-communication/jnosql-communication-column/.gitignore deleted file mode 100644 index b43ed9333..000000000 --- a/jnosql-communication/jnosql-communication-column/.gitignore +++ /dev/null @@ -1,16 +0,0 @@ -target/ -pom.xml.tag -pom.xml.releaseBackup -pom.xml.versionsBackup -pom.xml.next -test-output/ -/doc -*.iml -*.idea -*.log -.classpath --project -/.resourceCache -/.project -/.idea -.settings/ diff --git a/jnosql-communication/jnosql-communication-column/pom.xml b/jnosql-communication/jnosql-communication-column/pom.xml deleted file mode 100644 index bbef22485..000000000 --- a/jnosql-communication/jnosql-communication-column/pom.xml +++ /dev/null @@ -1,39 +0,0 @@ - - - - - 4.0.0 - - - org.eclipse.jnosql.communication - jnosql-communication - 1.1.1-SNAPSHOT - - - jnosql-communication-column - jar - - - - ${project.groupId} - jnosql-communication-query - ${project.version} - - - diff --git a/jnosql-communication/jnosql-communication-column/src/main/java/module-info.java b/jnosql-communication/jnosql-communication-column/src/main/java/module-info.java deleted file mode 100644 index d56c45e1f..000000000 --- a/jnosql-communication/jnosql-communication-column/src/main/java/module-info.java +++ /dev/null @@ -1,22 +0,0 @@ - -/* - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * You may elect to redistribute this code under either of these licenses. - * Contributors: - * Otavio Santana - */ -module org.eclipse.jnosql.communication.column { - requires org.eclipse.jnosql.communication.core; - requires org.eclipse.jnosql.communication.query; - requires jakarta.json.bind; - requires jakarta.json; - requires jakarta.data; - - opens org.eclipse.jnosql.communication.column; - -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnConfiguration.java b/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnConfiguration.java deleted file mode 100644 index abd2a69cb..000000000 --- a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnConfiguration.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * - * Copyright (c) 2023 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - - -import org.eclipse.jnosql.communication.CommunicationException; -import org.eclipse.jnosql.communication.Settings; - -import java.util.ServiceLoader; -import java.util.function.Function; - - -/** - * It is a function that reads from {@link Settings} and then creates a manager factory instance. - * It should return a {@link NullPointerException} when the {@link Settings} parameter is null. - * - * @see ColumnManagerFactory - * @see ColumnManager - */ -public interface ColumnConfiguration extends Function { - - - /** - * creates and returns a {@link ColumnConfiguration} instance from {@link ServiceLoader} - * - * @param the configuration type - * @return {@link ColumnConfiguration} instance - */ - static T getConfiguration() { - return (T) ServiceLoader.load(ColumnConfiguration.class) - .stream() - .map(ServiceLoader.Provider::get) - .findFirst().orElseThrow(() -> new CommunicationException("No ColumnConfiguration implementation found!")); - } - - /** - * creates and returns a {@link ColumnConfiguration} instance from {@link ServiceLoader} - * for a particular provider implementation. - * - * @param the configuration type - * @param type the particular provider - * @return {@link ColumnConfiguration} instance - */ - static T getConfiguration(Class type) { - return (T) ServiceLoader.load(ColumnConfiguration.class) - .stream() - .map(ServiceLoader.Provider::get) - .filter(type::isInstance) - .findFirst().orElseThrow(() -> new CommunicationException("No ColumnConfiguration implementation found!")); - } -} diff --git a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnDeleteQuery.java b/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnDeleteQuery.java deleted file mode 100644 index 5c8f197e2..000000000 --- a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnDeleteQuery.java +++ /dev/null @@ -1,381 +0,0 @@ -/* - * - * Copyright (c) 2023 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - - -import java.util.List; -import java.util.Optional; -import java.util.function.Function; -import java.util.function.Supplier; -import java.util.stream.Stream; - -import static java.util.Arrays.asList; -import static java.util.Collections.emptyList; -import static java.util.Objects.requireNonNull; - -/** - * A unit that has the columnFamily and condition to delete from conditions. - * This instance will be used on: - *

{@link ColumnManager#delete(ColumnDeleteQuery)}

- */ -public interface ColumnDeleteQuery { - - - /** - * getter the columnFamily name - * - * @return the columnFamily name - */ - String name(); - - /** - * getter the condition - * If empty, {@link Optional#empty()} is true, the implementation might either return - * an unsupported exception or delete same elements in the database. - * - * @return the condition - */ - Optional condition(); - - /** - * Defines which columns will be removed, the database provider might use this information - * to remove just these fields instead of all entity from {@link ColumnDeleteQuery} - * - * @return the columns - */ - List columns(); - - /** - * It starts the first step of {@link ColumnDelete} API using a fluent-API way. - * This first step will inform the fields to delete in the query instead of the whole record. - * This behavior might be different for each NoSQL database provider; therefore, - * it might be ignored for some implementations. - * - * @param columns the column fields to delete query - * @return a new {@link ColumnDelete} instance - * @throws NullPointerException when there is a null element - */ - static ColumnDelete delete(String... columns) { - Stream.of(columns).forEach(d -> requireNonNull(d, "there is null column in the query")); - return new DefaultFluentDeleteQueryBuilder(asList(columns)); - } - - /** - * It starts the first step of {@link ColumnDelete} API using a fluent-API way. - * Once there is no field, it will remove the whole record instead of some fields on the database. - * - * @return a new {@link ColumnDelete} instance - */ - static ColumnDelete delete() { - return new DefaultFluentDeleteQueryBuilder(emptyList()); - } - - /** - * It starts the first step of {@link ColumnDeleteQuery} creation using a builder pattern. - * This first step will inform the fields to delete in the query instead of the whole record. - * This behavior might be different for each NoSQL database provider; therefore, - * it might be ignored for some implementations. - * - * @param documents the column fields to delete query - * @return a {@link ColumnDeleteQueryBuilder} instance - * @throws NullPointerException when there is a null element - */ - static ColumnDeleteQueryBuilder builder(String... documents) { - Stream.of(documents).forEach(d -> requireNonNull(d, "there is null document in the query")); - ColumnDeleteQueryBuilder builder = new DefaultDeleteQueryBuilder(); - Stream.of(documents).forEach(builder::delete); - return builder; - } - - /** - * It starts the first step of {@link ColumnDeleteQueryBuilder} creation using a builder pattern. - * Once there is no field, it will remove the whole record instead of some fields on the database. - * - * @return a {@link ColumnDeleteQueryBuilder} instance - */ - static ColumnDeleteQueryBuilder builder() { - return new DefaultDeleteQueryBuilder(); - } - - - /** - * The initial element in the Column delete query - */ - interface ColumnDelete { - - /** - * Defines the column family in the delete query - * - * @param columnFamily the column family to query - * @return a {@link ColumnDeleteFrom query} - * @throws NullPointerException when columnFamily is null - */ - ColumnDeleteFrom from(String columnFamily); - } - - /** - * A supplier class of {@link ColumnDelete} - */ - interface ColumnDeleteProvider extends Function, Supplier { - } - - interface ColumnDeleteQueryBuilderProvider extends Function, - Supplier { - - } - - /** - * The Column Delete Query - */ - interface ColumnDeleteFrom extends ColumnDeleteQueryBuild { - - - /** - * Starts a new condition defining the column name - * - * @param name the column name - * @return a new {@link ColumnDeleteNameCondition} - * @throws NullPointerException when name is null - */ - ColumnDeleteNameCondition where(String name); - - } - - /** - * The base to delete name condition - */ - interface ColumnDeleteNameCondition { - - - /** - * Creates the equals condition - * - * @param value the value to the condition - * @param the type - * @return the {@link ColumnDeleteWhere} - * @throws NullPointerException when value is null - */ - ColumnDeleteWhere eq(T value); - - /** - * Creates the like condition - * - * @param value the value to the condition - * @return the {@link ColumnDeleteWhere} - * @throws NullPointerException when value is null - */ - ColumnDeleteWhere like(String value); - - /** - * Creates the greater than condition - * - * @param value the value to the condition - * @param the type - * @return the {@link ColumnDeleteWhere} - * @throws NullPointerException when value is null - */ - ColumnDeleteWhere gt(T value); - - /** - * Creates the greater equals than condition - * - * @param the type - * @param value the value to the condition - * @return the {@link ColumnDeleteWhere} - * @throws NullPointerException when value is null - */ - ColumnDeleteWhere gte(T value); - - /** - * Creates the lesser than condition - * - * @param the type - * @param value the value to the condition - * @return the {@link ColumnDeleteWhere} - * @throws NullPointerException when value is null - */ - ColumnDeleteWhere lt(T value); - - /** - * Creates the lesser equals than condition - * - * @param the type - * @param value the value to the condition - * @return the {@link ColumnDeleteWhere} - * @throws NullPointerException when value is null - */ - ColumnDeleteWhere lte(T value); - - /** - * Creates the between condition - * - * @param the type - * @param valueA the values within a given range - * @param valueB the values within a given range - * @return the {@link ColumnDeleteWhere} - * @throws NullPointerException when either valueA or valueB are null - */ - ColumnDeleteWhere between(T valueA, T valueB); - - /** - * Creates in condition - * - * @param values the values - * @param the type - * @return the {@link ColumnDeleteWhere} - * @throws NullPointerException when value is null - */ - ColumnDeleteWhere in(Iterable values); - - /** - * Creates the equals condition - * - * @return {@link ColumnDeleteNotCondition} - */ - ColumnDeleteNotCondition not(); - - - } - - /** - * The column not condition - */ - interface ColumnDeleteNotCondition extends ColumnDeleteNameCondition { - } - - /** - * The last step to the build of {@link ColumnDeleteQuery}. - * It either can return a new {@link ColumnDeleteQuery} instance or execute a query with - * {@link ColumnManager} - */ - interface ColumnDeleteQueryBuild { - - /** - * Creates a new instance of {@link ColumnDeleteQuery} - * - * @return a new {@link ColumnDeleteQuery} instance - */ - ColumnDeleteQuery build(); - - /** - * executes the {@link ColumnManager#delete(ColumnDeleteQuery)} - * - * @param manager the entity manager - * @throws NullPointerException when manager is null - */ - void delete(ColumnManager manager); - - } - - /** - * The Column Where whose define the condition in the delete query. - */ - interface ColumnDeleteWhere extends ColumnDeleteQueryBuild { - - - /** - * Starts a new condition in the select using {@link ColumnCondition#and(ColumnCondition)} - * - * @param name a condition to be added - * @return the same {@link ColumnDeleteNameCondition} with the condition appended - * @throws NullPointerException when condition is null - */ - ColumnDeleteNameCondition and(String name); - - /** - * Starts a new condition in the select using {@link ColumnCondition#or(ColumnCondition)} - * - * @param name a condition to be added - * @return the same {@link ColumnDeleteNameCondition} with the condition appended - * @throws NullPointerException when condition is null - */ - ColumnDeleteNameCondition or(String name); - - } - - /** - * Besides the fluent-API with the select {@link ColumnDeleteQuery#delete()}, the API also has support for creating - * a {@link ColumnDeleteQuery} instance using a builder pattern. - * The goal is the same; however, it provides more possibilities, such as more complex queries. - *

- * The goal is the same; however, it provides more possibilities, such as more complex queries. - * The ColumnQueryBuilder is not brighter than a fluent-API; it has the same validation in the creation method. - * It is a mutable and non-thread-safe class. - */ - interface ColumnDeleteQueryBuilder { - /** - * Append a new column in to delete query. - * It informs the fields to delete in the query instead of the whole record. - * This behavior might be different for each NoSQL database provider; therefore, it might be ignored for some implementations. - * - * @param column a column field to delete query - * @return the {@link ColumnDeleteQueryBuilder} - * @throws NullPointerException when the document is null - */ - ColumnDeleteQueryBuilder delete(String column); - - /** - * Append a new column in to delete query. - * This first step will inform the fields to delete in the query instead of the whole record. - * This behavior might be different for each NoSQL database provider; therefore, it might be ignored for some implementations. - * - * @param columns The columns fields to delete query - * @return the {@link ColumnDeleteQueryBuilder} - * @throws NullPointerException when there is a null element - */ - ColumnDeleteQueryBuilder delete(String... columns); - - /** - * Define the column family in the query, this element is mandatory to build - * the {@link ColumnDeleteQueryBuilder} - * - * @param columnFamily the column family to query - * @return the {@link ColumnDeleteQueryBuilder} - * @throws NullPointerException when columnFamily is null - */ - ColumnDeleteQueryBuilder from(String columnFamily); - - /** - * Either add or replace the condition in the query. It has a different behavior than the previous method - * because it won't append it. Therefore, it will create when it is the first time or replace when it was executed once. - * - * @param condition the {@link ColumnCondition} in the query - * @return the {@link ColumnDeleteQueryBuilder} - * @throws NullPointerException when condition is null - */ - ColumnDeleteQueryBuilder where(ColumnCondition condition); - - /** - * It will validate and then create a {@link ColumnDeleteQuery} instance. - * - * @return {@link ColumnDeleteQuery} - * @throws IllegalStateException It returns a state exception when an element is not valid or not fill-up, - * such as the {@link ColumnDeleteQueryBuilder#from(String)} method was not called. - */ - ColumnDeleteQuery build(); - - /** - * executes the {@link ColumnManager#delete(ColumnDeleteQuery)} - * - * @param manager the entity manager - * @throws NullPointerException when manager is null - * @throws IllegalStateException It returns a state exception when an element is not valid or not fill-up, - * such as the {@link ColumnDeleteQueryBuilder#from(String)} method was not called. - */ - void delete(ColumnManager manager); - } -} diff --git a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnDeleteQueryParams.java b/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnDeleteQueryParams.java deleted file mode 100644 index 4c6b31ffc..000000000 --- a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnDeleteQueryParams.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - - -import org.eclipse.jnosql.communication.Params; - - -/** - * The result of {@link ColumnDeleteQueryParams} that has {@link ColumnDeleteQuery} and {@link Params}. - */ -public record ColumnDeleteQueryParams(ColumnDeleteQuery query, Params params) { - - /** - * The {@link ColumnDeleteQuery} - * - * @return a {@link ColumnDeleteQuery} instance - */ - @Override - public ColumnDeleteQuery query() { - return query; - } - - /** - * The {@link Params} - * - * @return a {@link Params} instance - */ - @Override - public Params params() { - return params; - } - - -} diff --git a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnEntity.java b/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnEntity.java deleted file mode 100644 index d3602738f..000000000 --- a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnEntity.java +++ /dev/null @@ -1,330 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ - -package org.eclipse.jnosql.communication.column; - - -import org.eclipse.jnosql.communication.TypeSupplier; -import org.eclipse.jnosql.communication.Value; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; -import java.util.Set; - -import static java.util.Collections.unmodifiableSet; -import static java.util.Objects.requireNonNull; -import static java.util.Optional.ofNullable; -import static java.util.stream.Collectors.collectingAndThen; -import static java.util.stream.Collectors.toList; - -/** - * The communication level entity. It is the API entity between the database and the Jakarta NoSQL communication level. - * It represents a column family. - * Each ColumnEntity has a name and one or more {@link Column}. - * - * @see Column - * @see ColumnEntity#columns() - * @see ColumnEntity#name() - */ -public class ColumnEntity { - - private final Map columns = new HashMap<>(); - - private final String name; - - ColumnEntity(String name) { - this.name = name; - } - - - public void addAll(List columns) { - Objects.requireNonNull(columns, "The object column is required"); - columns.forEach(this::add); - } - - /** - * Appends the specified column to the end of this list - * - * @param column - column to be added - * @throws NullPointerException when column is null - */ - public void add(Column column) { - Objects.requireNonNull(column, "Column is required"); - this.columns.put(column.name(), column); - } - - /** - * add a column within {@link ColumnEntity} - * - * @param name a name of the column - * @param value the information of the column - * @throws UnsupportedOperationException when this method is not supported - * @throws NullPointerException when either name is null - */ - public void add(String name, Object value) { - requireNonNull(name, "name is required"); - this.columns.put(name, Column.of(name, Value.of(value))); - } - - /** - * add a column within {@link ColumnEntity} - * - * @param name a name of the column - * @param value the information of the column - * @throws UnsupportedOperationException when this method is not supported - * @throws NullPointerException when either name is null - */ - public void add(String name, Value value) { - requireNonNull(name, "name is required"); - this.columns.put(name, Column.of(name, value)); - } - - /** - * Adds a column with a null value to the collection of columns. - * - * @param name the name of the column to add; must not be {@code null} - * @throws NullPointerException if the provided {@code name} is {@code null} - */ - public void addNull(String name){ - requireNonNull(name, "name is required"); - this.columns.put(name, Column.of(name, Value.ofNull())); - } - - /** - * Converts the columns to a Map where: - * the key is the name the column - * The value is the {@link Value#get()} of the map - * - * @return a map instance - */ - public Map toMap() { - Map map = new HashMap<>(); - for (Map.Entry entry : columns.entrySet()) { - Column column = entry.getValue(); - map.put(entry.getKey(), convert(column.get())); - } - return Collections.unmodifiableMap(map); - } - - private Object convert(Object value) { - if (value instanceof Column) { - Column column = Column.class.cast(value); - return Collections.singletonMap(column.name(), convert(column.get())); - } else if (value instanceof Iterable) { - List list = new ArrayList<>(); - Iterable.class.cast(value).forEach(e -> list.add(convert(e))); - return list; - } - return value; - } - - /** - * Returns all columns from this Column Family - * - * @return an immutable list of columns - */ - public List columns() { - return columns.values() - .stream() - .collect(collectingAndThen(toList(), Collections::unmodifiableList)); - } - - /** - * Column Family's name - * - * @return Column Family's name - */ - public String name() { - return name; - } - - /** - * Remove a column whose name is informed in parameter. - * - * @param name a column name - * @return if a column was removed or not - * @throws NullPointerException when column is null - */ - public boolean remove(String name) { - requireNonNull(name, "name is required"); - return columns.remove(name) != null; - } - - /** - * Find column a column from columnName - * - * @param columnName a column name - * @return an {@link Optional} instance with the result - * @throws NullPointerException when columnName is null - */ - public Optional find(String columnName) { - requireNonNull(columnName, "columnName is required"); - Column column = columns.get(columnName); - return ofNullable(column); - } - - /** - * Find a column and converts to specific value from {@link Class} - * It is an alias to {@link Value#get(Class)} - * - * @param the type class - * @param columnName a name of a column - * @param type the type to convert the value - * @return an {@link Optional} instance with the result - * @throws NullPointerException when there are null parameters - */ - public Optional find(String columnName, Class type) { - Objects.requireNonNull(columnName, "columnName is required"); - Objects.requireNonNull(type, "type is required"); - return ofNullable(columns.get(columnName)) - .map(c -> c.get(type)); - } - - /** - * Find a column and converts to specific value from {@link TypeSupplier} - * It is an alias to {@link Value#get(TypeSupplier)} - * - * @param the type class - * @param columnName a name of a column - * @param type the type to convert the value - * @return an {@link Optional} instance with the result - * @throws NullPointerException when there are null parameters - */ - public Optional find(String columnName, TypeSupplier type) { - Objects.requireNonNull(columnName, "columnName is required"); - Objects.requireNonNull(type, "type is required"); - return ofNullable(columns.get(columnName)) - .map(v -> v.get(type)); - } - - /** - * Returns the number of elements in this list. - * - * @return the number of elements in this list - */ - public int size() { - return columns.size(); - } - - /** - * Returns true if the number of columns is zero otherwise false. - * - * @return true if there isn't elements to {@link ColumnEntity#columns()} - */ - public boolean isEmpty() { - return columns.isEmpty(); - } - - /** - * make copy of itself - * - * @return an instance copy - */ - public ColumnEntity copy() { - ColumnEntity entity = new ColumnEntity(this.name); - entity.columns.putAll(new HashMap<>(this.columns)); - return entity; - } - - /** - * Returns a Set view of the names of column contained in Column Entity - * - * @return the keys - */ - public Set columnNames() { - return unmodifiableSet(columns.keySet()); - } - - /** - * Returns a Collection view of the values contained in this ColumnEntity. - * - * @return the collection of values - */ - public Collection values() { - return columns - .values() - .stream() - .map(Column::value) - .collect(collectingAndThen(toList(), Collections::unmodifiableList)); - } - - /** - * Returns true if this ColumnEntity contains a column whose the name is informed - * - * @param name the column name - * @return true if find a column and otherwise false - */ - public boolean contains(String name) { - requireNonNull(name, "name is required"); - return columns.containsKey(name); - } - - /** - * Removes all Columns - */ - public void clear() { - columns.clear(); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - ColumnEntity that = (ColumnEntity) o; - return Objects.equals(columns, that.columns) && - Objects.equals(name, that.name); - } - - @Override - public int hashCode() { - return Objects.hash(columns, name); - } - - @Override - public String toString() { - return "DefaultColumnEntity{" + "columns=" + toMap() + - ", name='" + name + '\'' + - '}'; - } - - - /** - * Creates a column family instance - * - * @param name a name to column family - * @return a ColumnEntity instance - */ - public static ColumnEntity of(String name) { - return new ColumnEntity(requireNonNull(name, "name is required")); - } - - public static ColumnEntity of(String name, List columns) { - ColumnEntity columnEntity = new ColumnEntity(name); - columnEntity.addAll(columns); - return columnEntity; - } -} diff --git a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnManager.java b/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnManager.java deleted file mode 100644 index 8a063b66e..000000000 --- a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnManager.java +++ /dev/null @@ -1,216 +0,0 @@ -/* - * - * Copyright (c) 2023 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - - -import jakarta.data.exceptions.NonUniqueResultException; - -import java.time.Duration; -import java.util.Iterator; -import java.util.Objects; -import java.util.Optional; -import java.util.stream.Stream; - -/** - * The manager instance bridges the Jakarta NoSQL and the NoSQL vendor. - * - * @see ColumnEntity - */ -public interface ColumnManager extends AutoCloseable { - - /** - * Returns the database's name of this {@link ColumnManager} - * - * @return the database's name - */ - String name(); - - /** - * Saves a Column family entity - * - * @param entity column family to be saved - * @return the entity saved - * @throws NullPointerException when entity is null - */ - ColumnEntity insert(ColumnEntity entity); - - /** - * Updates a Column family entity - * - * @param entity column family to be saved - * @return the entity saved - * @throws NullPointerException when entity is null - */ - ColumnEntity update(ColumnEntity entity); - - /** - * Updates a Column family entities, by default it's just run for each saving using - * {@link ColumnManager#update(ColumnEntity)}, each NoSQL vendor might - * replace to a more appropriate one. - * - * @param entities column family to be saved - * @return the entity saved - * @throws NullPointerException when entities is null - */ - Iterable update(Iterable entities); - - /** - * Saves a Column family entity with time to live - * - * @param entity column family to be saved - * @param ttl time to live - * @return the entity saved - * @throws NullPointerException when either entity or ttl are null - * @throws UnsupportedOperationException when the database does not support this feature - */ - ColumnEntity insert(ColumnEntity entity, Duration ttl); - - /** - * Saves a Column family entities, by default it's just run for each saving using - * {@link ColumnManager#insert(ColumnEntity)}, each NoSQL vendor might - * replace to a more appropriate one. - * - * @param entities column family to be saved - * @return the entity saved - * @throws NullPointerException when entities is null - */ - Iterable insert(Iterable entities); - - /** - * Saves a Column family entity with time to live, by default it's just run for each saving using - * {@link ColumnManager#insert(ColumnEntity, Duration)}, - * each NoSQL vendor might replace to a more appropriate one. - * - * @param entities column family to be saved - * @param ttl time to live - * @return the entity saved - * @throws NullPointerException when either entity or ttl are null - * @throws UnsupportedOperationException when the database does not support this feature - */ - Iterable insert(Iterable entities, Duration ttl); - - /** - * Deletes an entity - * - * @param query the select to delete an entity - * @throws NullPointerException when either select or collection are null - * @throws UnsupportedOperationException if the implementation does not support any operation that a query has. - */ - void delete(ColumnDeleteQuery query); - - /** - * Finds {@link ColumnEntity} from select - * - * @param query - select to figure out entities - * @return entities found by select - * @throws NullPointerException when select is null - * @throws UnsupportedOperationException if the implementation does not support any operation that a query has. - */ - Stream select(ColumnQuery query); - - /** - * Returns the number of items in the column family that match a specified query. - * @param query the query - * @return the number of documents from query - * @throws NullPointerException when query is null - */ - default long count(ColumnQuery query) { - Objects.requireNonNull(query, "query is required"); - return this.select(DefaultColumnQuery.countBy(query)).count(); - } - - /** - * Returns whether an entity that match a specified query. - * @param query the query - * @return true if an entity with the given query exists, false otherwise. - * @throws NullPointerException when query it null - */ - default boolean exists(ColumnQuery query) { - Objects.requireNonNull(query, "query is required"); - return this.select(DefaultColumnQuery.existsBy(query)).findAny().isPresent(); - } - - /** - * Executes a query and returns the result, when the operations are insert, update and select - * command it will return the result of the operation when the command is delete it will return an empty collection. - * - * @param query the query as {@link String} - * @return the result of the operation if delete it will always return an empty list - * @throws NullPointerException when there is parameter null - * @throws IllegalArgumentException when the query has value parameters - * @throws IllegalStateException when there is not {@link ColumnQueryParser} - */ - default Stream query(String query) { - Objects.requireNonNull(query, "query is required"); - ColumnQueryParser parser = new ColumnQueryParser(); - return parser.query(query, this, ColumnObserverParser.EMPTY); - } - - /** - * Executes a query and returns the result, when the operations are insert, update and select - * command it will return the result of the operation when the command is delete it will return an empty collection. - * - * @param query the query as {@link String} - * @return a {@link ColumnPreparedStatement} instance - * @throws NullPointerException when there is parameter null - * @throws IllegalStateException when there is not {@link ColumnQueryParser} - */ - default ColumnPreparedStatement prepare(String query) { - Objects.requireNonNull(query, "query is required"); - ColumnQueryParser parser = new ColumnQueryParser(); - return parser.prepare(query, this, ColumnObserverParser.EMPTY); - } - - /** - * Returns a single entity from select - * - * @param query - select to figure out entities - * @return an entity on {@link Optional} or {@link Optional#empty()} when the result is not found. - * @throws NonUniqueResultException when the result has more than one entity - * @throws NullPointerException when select is null - * @throws UnsupportedOperationException if the implementation does not support any operation that a query has. - */ - default Optional singleResult(ColumnQuery query) { - Objects.requireNonNull(query, "query is required"); - Stream entities = select(query); - final Iterator iterator = entities.iterator(); - if (!iterator.hasNext()) { - return Optional.empty(); - } - final ColumnEntity entity = iterator.next(); - if (!iterator.hasNext()) { - return Optional.of(entity); - } - throw new NonUniqueResultException("The select returns more than one entity, select: " + query); - } - - /** - * Returns the number of elements from column family - * - * @param columnFamily the column family - * @return the number of elements - * @throws NullPointerException when column family is null - * @throws UnsupportedOperationException when the database dot not have support - */ - long count(String columnFamily); - - /** - * closes a resource - */ - void close(); - -} diff --git a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnManagerFactory.java b/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnManagerFactory.java deleted file mode 100644 index ad101e909..000000000 --- a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnManagerFactory.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * - * Copyright (c) 2023 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - -import java.util.function.Function; - -/** - * The manager factory instance creates a manager instance from the database name. - * It should return a {@link NullPointerException} when the {@link String} parameter is null. - * - * @see ColumnManager - */ -public interface ColumnManagerFactory extends Function, AutoCloseable { - - - /** - * closes a resource - */ - void close(); -} diff --git a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnObserverParser.java b/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnObserverParser.java deleted file mode 100644 index 274f5f657..000000000 --- a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnObserverParser.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * - * Copyright (c) 2023 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - -/** - * An observer to a parser; this observer allows checking both the name of an entity and the fields. - * This observer might be used to the mapper process. - */ -public interface ColumnObserverParser { - - ColumnObserverParser EMPTY = new ColumnObserverParser() { - }; - - /** - * Fire an event to entity name - * - * @param entity the entity - * @return the field result - * @throws NullPointerException when the entity is null - */ - default String fireEntity(String entity) { - return entity; - } - - /** - * Fire an event to each field in case of mapper process - * - * @param field the field - * @param entity the entity - * @return the field result - * @throws NullPointerException when there is null parameter - */ - default String fireField(String entity, String field) { - return field; - } - -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnPreparedStatement.java b/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnPreparedStatement.java deleted file mode 100644 index 01aab63ca..000000000 --- a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnPreparedStatement.java +++ /dev/null @@ -1,190 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - - -import jakarta.data.exceptions.NonUniqueResultException; -import org.eclipse.jnosql.communication.Params; -import org.eclipse.jnosql.communication.QueryException; - -import java.time.Duration; -import java.util.Iterator; -import java.util.List; -import java.util.Objects; -import java.util.Optional; -import java.util.stream.Stream; - - -/** - * An object that represents a precompiled Query statement. - */ -public final class ColumnPreparedStatement { - - private final ColumnEntity entity; - - private final ColumnQuery columnQuery; - - private final ColumnDeleteQuery columnDeleteQuery; - - private final PreparedStatementType type; - - private final Params params; - - private final String query; - - private final List paramsLeft; - - private final Duration duration; - - private final ColumnManager manager; - - private ColumnPreparedStatement(ColumnEntity entity, - ColumnQuery columnQuery, - ColumnDeleteQuery columnDeleteQuery, - PreparedStatementType type, - Params params, - String query, - List paramsLeft, - Duration duration, - ColumnManager manager) { - this.entity = entity; - this.columnQuery = columnQuery; - this.columnDeleteQuery = columnDeleteQuery; - this.type = type; - this.params = params; - this.query = query; - this.paramsLeft = paramsLeft; - this.manager = manager; - this.duration = duration; - } - - /** - * Binds an argument to a positional parameter. - * - * @param name the parameter name - * @param value the parameter value - * @return the same query instance - * @throws NullPointerException when there is null parameter - */ - public ColumnPreparedStatement bind(String name, Object value) { - Objects.requireNonNull(name, "name is required"); - Objects.requireNonNull(value, "value is required"); - - paramsLeft.remove(name); - params.bind(name, value); - return this; - } - - /** - * Returns the result as a single element otherwise it will return an {@link Optional#empty()} - * - * @return the single result - */ - public Stream result() { - if (!paramsLeft.isEmpty()) { - throw new QueryException("Check all the parameters before execute the query, params left: " + paramsLeft); - } - switch (type) { - case SELECT -> { - return manager.select(columnQuery); - } - case DELETE -> { - manager.delete(columnDeleteQuery); - return Stream.empty(); - } - case UPDATE -> { - return Stream.of(manager.update(entity)); - } - case INSERT -> { - if (Objects.isNull(duration)) { - return Stream.of(manager.insert(entity)); - } else { - return Stream.of(manager.insert(entity, duration)); - } - } - default -> throw new UnsupportedOperationException("there is not support to operation type: " + type); - } - } - - public Optional singleResult() { - Stream entities = result(); - final Iterator iterator = entities.iterator(); - - if (!iterator.hasNext()) { - return Optional.empty(); - } - final ColumnEntity next = iterator.next(); - if (!iterator.hasNext()) { - return Optional.of(next); - } - - throw new NonUniqueResultException("The select returns more than one entity, select: " + query); - } - - enum PreparedStatementType { - SELECT, DELETE, UPDATE, INSERT - } - - - @Override - public String toString() { - return query; - } - - static ColumnPreparedStatement select( - ColumnQuery columnQuery, - Params params, - String query, - ColumnManager manager) { - return new ColumnPreparedStatement(null, columnQuery, - null, PreparedStatementType.SELECT, params, query, - params.getParametersNames(), null, manager); - - } - - static ColumnPreparedStatement delete(ColumnDeleteQuery columnDeleteQuery, - Params params, - String query, - ColumnManager manager) { - - return new ColumnPreparedStatement(null, null, - columnDeleteQuery, PreparedStatementType.DELETE, params, query, - params.getParametersNames(), null, manager); - - } - - static ColumnPreparedStatement insert(ColumnEntity entity, - Params params, - String query, - Duration duration, - ColumnManager manager) { - return new ColumnPreparedStatement(entity, null, - null, PreparedStatementType.INSERT, params, query, - params.getParametersNames(), duration, manager); - - } - - static ColumnPreparedStatement update(ColumnEntity entity, - Params params, - String query, - ColumnManager manager) { - return new ColumnPreparedStatement(entity, null, - null, PreparedStatementType.UPDATE, params, query, - params.getParametersNames(), null, manager); - - } -} diff --git a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnQuery.java b/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnQuery.java deleted file mode 100644 index 0aca1f9d3..000000000 --- a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnQuery.java +++ /dev/null @@ -1,623 +0,0 @@ -/* - * - * Copyright (c) 2023 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - - - -import jakarta.data.Sort; - -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Optional; -import java.util.function.Function; -import java.util.function.Supplier; -import java.util.stream.Stream; - -import static java.util.Objects.requireNonNull; - -/** - * Class that contains information to do a select to {@link ColumnEntity} - * - * @see ColumnManager#select(ColumnQuery) - * @see ColumnCondition - * @see Sort - */ -public interface ColumnQuery { - - - /** - * @return The maximum number of results the select object was set to retrieve. - * The implementation might ignore this option. - */ - long limit(); - - /** - * @return The position of the first result the select object was set to retrieve. - * The implementation might ignore this option. - */ - long skip(); - - /** - * The column family name - * - * @return the column family name - */ - String name(); - - /** - * The conditions that contains in this {@link ColumnQuery} - * If empty, {@link Optional#empty()} is true, the implementation might either return an unsupported exception or returns same elements in the database. - * - * @return the conditions - */ - Optional condition(); - - /** - * Returns the columns to returns in that query if empty will return all elements in the query. - * The implementation might ignore this option. - * - * @return the columns - */ - List columns(); - - /** - * The sorts that contains in this {@link ColumnQuery} - * The implementation might ignore this option. - * - * @return the sorts - */ - List> sorts(); - - /** - * It starts the first step of {@link ColumnSelect} creation using a fluent-API way. - * This first step will inform the fields to return to the query, such as a "select field, fieldB from database" - * in a database query. - * - * @param columns - The document fields to query, optional. - * @return a new {@link ColumnSelect} instance - * @throws NullPointerException when there is a null element - */ - static ColumnSelect select(String... columns) { - Stream.of(columns).forEach(d -> requireNonNull(d, "there is null document in the query")); - return new DefaultFluentColumnQueryBuilder(Arrays.asList(columns)); - } - - /** - * It starts the first step of {@link ColumnQuery} creation using a fluent-API way. - * This first step will inform the fields to return to the query, such as a "select field, fieldB from database" in a database query. - * Once empty, it will return all elements in the query, similar to "select * from database" in a database query. - * - * @return a new {@link ColumnSelect} instance - */ - static ColumnSelect select() { - return new DefaultFluentColumnQueryBuilder(Collections.emptyList()); - } - - /** - * It starts the first step of {@link ColumnQuery} creation using a builder pattern. - * This first step will inform the fields to return to the query, such as a "select field, fieldB from database" in a database query. - * - * @return {@link ColumnQueryBuilder} instance - */ - static ColumnQueryBuilder builder() { - return new DefaultColumnQueryBuilder(); - } - - /** - * It starts the first step of {@link ColumnQuery} creation using a builder pattern. - * This first step will inform the fields to return to the query, such as a "select field, fieldB from database" in a database query. - * Once empty, it will return all elements in the query, similar to "select * from database" in a database query. - * - * @param columns The document fields to query, optional. - * @return {@link ColumnQueryBuilder} instance - */ - static ColumnQueryBuilder builder(String... columns) { - Stream.of(columns).forEach(d -> requireNonNull(d, "there is null column in the query")); - ColumnQueryBuilder builder = new DefaultColumnQueryBuilder(); - Stream.of(columns).forEach(builder::select); - return builder; - } - - - /** - * The ColumnFrom Query - */ - interface ColumnFrom extends ColumnQueryBuild { - - - /** - * Starts a new condition defining the column name - * - * @param name the column name - * @return a new {@link ColumnNameCondition} - * @throws NullPointerException when name is null - */ - ColumnNameCondition where(String name); - - /** - * Defines the position of the first result to retrieve. - * It will depend on the NoSQL vendor implementation, but it will discard or skip the search result. - * The default value is zero, and it will replace the current property. - * - * @param skip the first result to retrieve - * @return a query with first result defined - * @throws IllegalArgumentException if limit is negative - */ - ColumnSkip skip(long skip); - - - /** - * Defines the maximum number of results to retrieve. - * It will truncate to be no longer than limit. - * The default value is zero, and it will replace the current property. - * - * @param limit the limit - * @return the {@link ColumnQueryBuilder} - * @throws IllegalArgumentException if limit is negative - */ - ColumnLimit limit(long limit); - - /** - * Add the order how the result will return - * - * @param name the name to be ordered - * @return a query with the sort defined - * @throws NullPointerException when name is null - */ - ColumnOrder orderBy(String name); - - - } - - /** - * The Column Order whose define the maximum number of results to retrieve. - */ - interface ColumnLimit extends ColumnQueryBuild { - - /** - * Defines the position of the first result to retrieve. - * It will depend on the NoSQL vendor implementation, but it will discard or skip the search result. - * The default value is zero, and it will replace the current property. - * - * @param skip the first result to retrieve - * @return a query with first result defined - * @throws IllegalArgumentException if limit is negative - */ - ColumnSkip skip(long skip); - - } - - /** - * The Column name order a query - */ - interface ColumnNameOrder extends ColumnQueryBuild { - - - /** - * Add the order how the result will return - * - * @param name the name to be ordered - * @return a query with the sort defined - * @throws NullPointerException when name is null - */ - ColumnOrder orderBy(String name); - - /** - * Defines the position of the first result to retrieve. - * It will depend on the NoSQL vendor implementation, but it will discard or skip the search result. - * The default value is zero, and it will replace the current property. - * - * @param skip the first result to retrieve - * @return a query with first result defined - * @throws IllegalArgumentException if limit is negative - */ - ColumnSkip skip(long skip); - - - /** - * Defines the maximum number of results to retrieve. - * It will truncate to be no longer than limit. - * The default value is zero, and it will replace the current property. - * - * @param limit the limit - * @return the {@link ColumnQueryBuilder} - * @throws IllegalArgumentException if limit is negative - */ - ColumnLimit limit(long limit); - - - } - - /** - * The definition to either {@link jakarta.data.Direction} - */ - interface ColumnOrder { - - - /** - * Defines the order as {@link jakarta.data.Direction#ASC} - * @return the {@link ColumnNameOrder} instance - */ - ColumnNameOrder asc(); - - /** - * Defines the order as {@link jakarta.data.Direction#DESC} - * @return the {@link ColumnNameOrder} instance - */ - ColumnNameOrder desc(); - } - - /** - * The last step to the build of {@link ColumnQuery}. - * It either can return a new {@link ColumnQuery} instance or execute a query with - * {@link ColumnManager} - */ - interface ColumnQueryBuild { - - /** - * Creates a new instance of {@link ColumnQuery} - * - * @return a new {@link ColumnQuery} instance - */ - ColumnQuery build(); - - /** - * Executes {@link ColumnManager#select(ColumnQuery)} - * - * @param manager the entity manager - * @return the result of {@link ColumnManager#select(ColumnQuery)} - * @throws NullPointerException when manager is null - */ - Stream getResult(ColumnManager manager); - - /** - * Executes {@link ColumnManager#singleResult(ColumnQuery)} - * - * @param manager the entity manager - * @return the result of {@link ColumnManager#singleResult(ColumnQuery)} - * @throws NullPointerException when manager is null - */ - Optional getSingleResult(ColumnManager manager); - - } - - /** - * The initial element in the Column query - */ - interface ColumnSelect { - - /** - * Defines the column family in the query - * - * @param columnFamily the column family to query - * @return a {@link ColumnFrom query} - * @throws NullPointerException when columnFamily is null - */ - ColumnFrom from(String columnFamily); - } - - /** - * A provider class of {@link ColumnSelect} - */ - interface ColumnSelectProvider extends Function, Supplier { - } - - /** - * A provider class of {@link ColumnQueryBuilder} - */ - interface ColumnQueryBuilderProvider extends Function, Supplier { - } - - - /** - * The Column Order whose define the position of the first result to retrieve. - */ - interface ColumnSkip extends ColumnQueryBuild { - - - /** - * Defines the maximum number of results to retrieve. - * It will truncate to be no longer than limit. - * The default value is zero, and it will replace the current property. - * - * @param limit the limit - * @return the {@link ColumnQueryBuilder} - * @throws IllegalArgumentException if limit is negative - */ - ColumnLimit limit(long limit); - - } - - /** - * The Column Where whose define the condition in the query. - */ - interface ColumnWhere extends ColumnQueryBuild { - - - /** - * Starts a new condition in the select using - * {@link ColumnCondition#and(ColumnCondition)} - * - * @param name a condition to be added - * @return the same {@link ColumnNameCondition} with the condition appended - * @throws NullPointerException when condition is null - */ - ColumnNameCondition and(String name); - - /** - * Appends a new condition in the select using - * {@link ColumnCondition#or(ColumnCondition)} - * - * @param name a condition to be added - * @return the same {@link ColumnNameCondition} with the condition appended - * @throws NullPointerException when condition is null - */ - ColumnNameCondition or(String name); - - /** - * Defines the position of the first result to retrieve. - * It will depend on the NoSQL vendor implementation, but it will discard or skip the search result. - * The default value is zero, and it will replace the current property. - * - * @param skip the first result to retrieve - * @return a query with first result defined - * @throws IllegalArgumentException if limit is negative - */ - ColumnSkip skip(long skip); - - /** - * Defines the maximum number of results to retrieve. - * It will truncate to be no longer than limit. - * The default value is zero, and it will replace the current property. - * - * @param limit the limit - * @return the {@link ColumnQueryBuilder} - * @throws IllegalArgumentException if limit is negative - */ - ColumnLimit limit(long limit); - - /** - * Add the order how the result will return - * - * @param name the name to order - * @return a query with the sort defined - * @throws NullPointerException when name is null - */ - ColumnOrder orderBy(String name); - - } - - /** - * The base to name condition - */ - interface ColumnNameCondition { - - - /** - * Creates the equals condition - * - * @param value the value to the condition - * @param the type - * @return the {@link ColumnWhere} - * @throws NullPointerException when value is null - */ - ColumnWhere eq(T value); - - /** - * Creates the like condition - * - * @param value the value to the condition - * @return the {@link ColumnWhere} - * @throws NullPointerException when value is null - */ - ColumnWhere like(String value); - - /** - * Creates the greater than condition - * - * @param the type - * @param value the value to the condition - * @return the {@link ColumnWhere} - * @throws NullPointerException when value is null - */ - ColumnWhere gt(T value); - - /** - * Creates the greater equals than condition - * - * @param the type - * @param value the value to the condition - * @return the {@link ColumnWhere} - * @throws NullPointerException when value is null - */ - ColumnWhere gte(T value); - - /** - * Creates the lesser than condition - * - * @param the type - * @param value the value to the condition - * @return the {@link ColumnWhere} - * @throws NullPointerException when value is null - */ - ColumnWhere lt(T value); - - /** - * Creates the lesser equals than condition - * - * @param the type - * @param value the value to the condition - * @return the {@link ColumnWhere} - * @throws NullPointerException when value is null - */ - ColumnWhere lte(T value); - - /** - * Creates the between condition - * - * @param the type - * @param valueA the values within a given range - * @param valueB the values within a given range - * @return the {@link ColumnWhere} - * @throws NullPointerException when either valueA or valueB are null - */ - ColumnWhere between(T valueA, T valueB); - - /** - * Creates in condition - * - * @param values the values - * @param the type - * @return the {@link ColumnWhere} - * @throws NullPointerException when value is null - */ - ColumnWhere in(Iterable values); - - /** - * Creates the equals condition - * - * @return {@link ColumnNotCondition} - */ - ColumnNotCondition not(); - - } - - /** - * The column not condition - */ - interface ColumnNotCondition extends ColumnNameCondition { - } - - /** - * Besides, the fluent-API with the select method, the API also has support for creating a {@link ColumnQuery} instance using a builder pattern. - * The goal is the same; however, it provides more possibilities, such as more complex queries. - * The ColumnQueryBuilder is not brighter than a fluent-API; it has the same validation in the creation method. - * It is a mutable and non-thread-safe class. - */ - interface ColumnQueryBuilder { - /** - * Append a new column in the search result. The query will return the result by elements declared such as "select column from database" - * If it remains empty, it will return all the possible fields, similar to "select * from database" - * - * @param column a field to return to the search - * @return the {@link ColumnQueryBuilder} - * @throws NullPointerException when the document is null - */ - ColumnQueryBuilder select(String column); - - /** - * Append new columns in the search result. The query will return the result by elements declared such as "select column from database" - * If it remains empty, it will return all the possible fields, similar to "select * from database" - * - * @param columns a field to return to the search - * @return the {@link ColumnQueryBuilder} - * @throws NullPointerException when there is a null element - */ - ColumnQueryBuilder select(String... columns); - - /** - * Append a new sort in the query. The first one has more precedence than the next one. - * - * @param sort the {@link Sort} - * @return the {@link ColumnQueryBuilder} - * @throws NullPointerException when the sort is null - */ - ColumnQueryBuilder sort(Sort sort); - - /** - * Append sorts in the query. The first one has more precedence than the next one. - * - * @param sorts the array of {@link Sort} - * @return the {@link ColumnQueryBuilder} - * @throws NullPointerException when there is a null sort - */ - ColumnQueryBuilder sort(Sort... sorts); - - /** - * Define the column family in the query, this element is mandatory to build the {@link ColumnQuery} - * - * @param columnFamily the column family to query - * @return the {@link ColumnQueryBuilder} - * @throws NullPointerException when documentCollection is null - */ - ColumnQueryBuilder from(String columnFamily); - - /** - * Either add or replace the condition in the query. It has a different behavior than the previous method - * because it won't append it. Therefore, it will create when it is the first time or replace when it was executed once. - * - * @param condition the {@link ColumnCondition} in the query - * @return the {@link ColumnQueryBuilder} - * @throws NullPointerException when condition is null - */ - ColumnQueryBuilder where(ColumnCondition condition); - - /** - * Defines the position of the first result to retrieve. - * It will depend on the NoSQL vendor implementation, but it will discard or skip the search result. - * The default value is zero, and it will replace the current property. - * - * @param skip the first result to retrieve - * @return a query with first result defined - * @throws IllegalArgumentException if limit is negative - */ - ColumnQueryBuilder skip(long skip); - - /** - * Defines the maximum number of results to retrieve. - * It will truncate to be no longer than limit. - * The default value is zero, and it will replace the current property. - * - * @param limit the limit - * @return the {@link ColumnQueryBuilder} - * @throws IllegalArgumentException if limit is negative - */ - ColumnQueryBuilder limit(long limit); - - /** - * It will validate and then create a {@link ColumnQuery} instance. - * - * @return {@link ColumnQuery} - * @throws IllegalStateException It returns a state exception when an element is not valid or not fill-up, - * such as the {@link ColumnQueryBuilder#from(String)} method was not called. - */ - ColumnQuery build(); - - /** - * Executes {@link ColumnManager#select(ColumnQuery)} - * - * @param manager the entity manager - * @return the result of {@link ColumnManager#select(ColumnQuery)} - * @throws NullPointerException when manager is null - * @throws IllegalStateException It returns a state exception when an element is not valid or not fill-up, - * such as the {@link ColumnQueryBuilder#from(String)} method was not called. - */ - Stream getResult(ColumnManager manager); - - /** - * Executes {@link ColumnManager#singleResult(ColumnQuery)} - * - * @param manager the entity manager - * @return the result of {@link ColumnManager#singleResult(ColumnQuery)} - * @throws NullPointerException when manager is null - * @throws IllegalStateException It returns a state exception when an element is not valid or not fill-up, - * such as the {@link ColumnQueryBuilder#from(String)} method was not called. - */ - Optional getSingleResult(ColumnManager manager); - } -} diff --git a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnQueryParams.java b/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnQueryParams.java deleted file mode 100644 index 034241668..000000000 --- a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnQueryParams.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - - -import org.eclipse.jnosql.communication.Params; - - -/** - * The result of {@link org.eclipse.jnosql.communication.query.SelectQueryConverter} that has {@link ColumnQuery} and {@link Params}. - */ -public record ColumnQueryParams(ColumnQuery query, Params params) { - - /** - * The {@link ColumnQuery} - * - * @return a {@link ColumnQuery} instance - */ - @Override - public ColumnQuery query() { - return query; - } - - /** - * The {@link Params} - * - * @return a {@link Params} instance - */ - @Override - public Params params() { - return params; - } -} diff --git a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnQueryParser.java b/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnQueryParser.java deleted file mode 100644 index cac4e4018..000000000 --- a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ColumnQueryParser.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - -import org.eclipse.jnosql.communication.QueryException; - -import java.util.Objects; -import java.util.stream.Stream; - -/** - * A query parser to column database type, this class will convert a String to an operation in {@link ColumnManager}. - */ -public final class ColumnQueryParser { - - private final SelectQueryParser select = new SelectQueryParser(); - private final DeleteQueryParser delete = new DeleteQueryParser(); - private final InsertQueryParser insert = new InsertQueryParser(); - private final UpdateQueryParser update = new UpdateQueryParser(); - - /** - * Executes a query and returns the result, when the operations are insert, update and select - * command it will return the result of the operation when the command is delete it will return an empty collection. - * - * @param query the query as {@link String} - * @param manager the manager - * @param observer the observer - * @return the result of the operation if delete it will always return an empty list - * @throws NullPointerException when there is parameter null - * @throws IllegalArgumentException when the query has value parameters - * @throws QueryException when there is error in the syntax - */ - public Stream query(String query, ColumnManager manager, ColumnObserverParser observer) { - validation(query, manager, observer); - String command = query.substring(0, 6); - return switch (command) { - case "select" -> select.query(query, manager, observer); - case "delete" -> delete.query(query, manager, observer); - case "insert" -> insert.query(query, manager, observer); - case "update" -> update.query(query, manager, observer); - default -> - throw new QueryException(String.format("The command was not recognized at the query %s ", query)); - }; - } - - /** - * Executes a query and returns a {@link ColumnPreparedStatement}, when the operations are insert, update and select - * command it will return the result of the operation when the command is delete it will return an empty collection. - * - * @param query the query as {@link String} - * @param manager the manager - * @param observer the observer - * @return a {@link ColumnPreparedStatement} instance - * @throws NullPointerException when there is parameter null - * @throws IllegalArgumentException when the query has value parameters - * @throws QueryException when there is error in the syntax - */ - public ColumnPreparedStatement prepare(String query, ColumnManager manager, ColumnObserverParser observer) { - validation(query, manager, observer); - String command = query.substring(0, 6); - - return switch (command) { - case "select" -> select.prepare(query, manager, observer); - case "delete" -> delete.prepare(query, manager, observer); - case "insert" -> insert.prepare(query, manager, observer); - case "update" -> update.prepare(query, manager, observer); - default -> - throw new QueryException(String.format("The command was not recognized at the query %s ", query)); - }; - } - - - private void validation(String query, ColumnManager manager, ColumnObserverParser observer) { - Objects.requireNonNull(query, "query is required"); - Objects.requireNonNull(manager, "manager is required"); - Objects.requireNonNull(observer, "manager is observer"); - if (query.length() < 6) { - throw new QueryException(String.format("The query %s is invalid", query)); - } - } -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/Columns.java b/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/Columns.java deleted file mode 100644 index a36ef9c58..000000000 --- a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/Columns.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - - - -import org.eclipse.jnosql.communication.Value; - -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.function.Predicate; - -import static java.util.stream.Collectors.toList; -import static java.util.stream.StreamSupport.stream; - -/** - * Utilitarian class to {@link Column} - */ -public final class Columns { - - private static final Predicate> IS_VALUE_NULL = e -> Objects.nonNull(e.getValue()); - - private Columns() { - } - - /** - * Creates a column instance - * - * @param name column's name - * @param value column's value - * @return a column's instance - * @throws NullPointerException when name is null - */ - public static Column of(String name, Object value) { - return Column.of(name, Value.of(value)); - } - - /** - * Converts a Map to columns where: the key gonna be a column's name the value a column's value and null values - * elements will be ignored. - * - * @param values map to be converted - * @return a list of columns - * @throws NullPointerException when values is null - */ - public static List of(Map values) { - Objects.requireNonNull(values, "values is required"); - return values.entrySet().stream() - .filter(IS_VALUE_NULL) - .map(e -> Column.of(e.getKey(), getValue(e.getValue()))) - .collect(toList()); - } - - private static Object getValue(Object value) { - - if (value instanceof Map) { - List list = Columns.of((Map.class.cast(value))); - if(list.size() == 1) { - return list.get(0); - } - return list; - } - if (value instanceof Iterable) { - return stream(Iterable.class.cast(value).spliterator(), false) - .map(Columns::getValue).collect(toList()); - } - return value; - } -} diff --git a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ConditionQueryParser.java b/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ConditionQueryParser.java deleted file mode 100644 index 502bd4ad0..000000000 --- a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ConditionQueryParser.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - - -import org.eclipse.jnosql.communication.Params; -import org.eclipse.jnosql.communication.QueryException; -import org.eclipse.jnosql.communication.query.JSONQueryValue; - -import java.util.List; - -/** - * A template class to Update and Insert query parser to extract the condition - */ -abstract class ConditionQueryParser { - - - protected ColumnEntity getEntity(ConditionQuerySupplier query, String columnFamily, Params params, - ColumnObserverParser observer) { - ColumnEntity entity = ColumnEntity.of(columnFamily); - - if (query.useJSONCondition()) { - JSONQueryValue jsonValue = query.value() - .orElseThrow(() -> new QueryException("It is an invalid state of" + - " either Update or Insert.")); - List columns = JsonObjects.getColumns(jsonValue.get()); - entity.addAll(columns); - return entity; - } - - query.conditions() - .stream() - .map(c -> Conditions.getCondition(c, params, observer, columnFamily)) - .map(ColumnCondition::column) - .forEach(entity::add); - return entity; - } -} diff --git a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ConditionQuerySupplier.java b/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ConditionQuerySupplier.java deleted file mode 100644 index 1d4c90f6f..000000000 --- a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/ConditionQuerySupplier.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - - -import org.eclipse.jnosql.communication.query.JSONQueryValue; -import org.eclipse.jnosql.communication.query.QueryCondition; - -import java.util.List; -import java.util.Optional; - -/** - * A base supplier to {@link org.eclipse.jnosql.communication.query.InsertQuery} and - * {@link org.eclipse.jnosql.communication.query.UpdateQuery} - */ -interface ConditionQuerySupplier { - - List conditions(); - - Optional value(); - - default boolean useJSONCondition() { - return conditions().isEmpty() && value().isPresent(); - } - -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/Conditions.java b/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/Conditions.java deleted file mode 100644 index dcd3375b4..000000000 --- a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/Conditions.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - - -import org.eclipse.jnosql.communication.Params; -import org.eclipse.jnosql.communication.QueryException; -import org.eclipse.jnosql.communication.query.ConditionQueryValue; -import org.eclipse.jnosql.communication.query.QueryCondition; -import org.eclipse.jnosql.communication.query.Where; - -final class Conditions { - - private Conditions() { - } - - static ColumnCondition getCondition(Where where, Params params, ColumnObserverParser observer, String entity) { - QueryCondition condition = where.condition(); - return getCondition(condition, params, observer, entity); - } - - static ColumnCondition getCondition(QueryCondition condition, Params parameters, ColumnObserverParser observer, String entity) { - return switch (condition.condition()) { - case EQUALS -> ColumnCondition.eq(Column.of(getName(condition, observer, entity), - Values.get(condition.value(), parameters))); - case GREATER_THAN -> ColumnCondition.gt(Column.of(getName(condition, observer, entity), - Values.get(condition.value(), parameters))); - case GREATER_EQUALS_THAN -> ColumnCondition.gte(Column.of(getName(condition, observer, entity), - Values.get(condition.value(), parameters))); - case LESSER_THAN -> ColumnCondition.lt(Column.of(getName(condition, observer, entity), - Values.get(condition.value(), parameters))); - case LESSER_EQUALS_THAN -> ColumnCondition.lte(Column.of(getName(condition, observer, entity), - Values.get(condition.value(), parameters))); - case IN -> ColumnCondition.in(Column.of(getName(condition, observer, entity), - Values.get(condition.value(), parameters))); - case LIKE -> ColumnCondition.like(Column.of(getName(condition, observer, entity), - Values.get(condition.value(), - parameters))); - case BETWEEN -> ColumnCondition.between(Column.of(getName(condition, observer, entity), - Values.get(condition.value(), - parameters))); - case NOT -> getCondition(ConditionQueryValue.class.cast(condition.value()).get().get(0), - parameters, observer, - entity).negate(); - case OR -> ColumnCondition.or(ConditionQueryValue.class.cast(condition.value()) - .get() - .stream().map(v -> getCondition(v, parameters, observer, entity)) - .toArray(ColumnCondition[]::new)); - case AND -> ColumnCondition.and(ConditionQueryValue.class.cast(condition.value()) - .get() - .stream().map(v -> getCondition(v, parameters, observer, entity)) - .toArray(ColumnCondition[]::new)); - default -> throw new QueryException("There is not support the type: " + condition.condition()); - }; - } - - private static String getName(QueryCondition condition, ColumnObserverParser observer, String entity) { - return observer.fireField(entity, condition.name()); - } - -} diff --git a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/DefaultColumn.java b/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/DefaultColumn.java deleted file mode 100644 index abe886ee3..000000000 --- a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/DefaultColumn.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * - * Copyright (c) 2023 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - -import org.eclipse.jnosql.communication.TypeSupplier; -import org.eclipse.jnosql.communication.Value; - -import java.util.Objects; - -/** - * Default implementation of {@link Column} - */ -record DefaultColumn(String name, Value value) implements Column { - - - @Override - public T get(Class type) { - Objects.requireNonNull(type, "type is required"); - return value.get(type); - } - - @Override - public T get(TypeSupplier supplier) { - Objects.requireNonNull(supplier, "supplier is required"); - return value.get(supplier); - } - - @Override - public Object get() { - return value.get(); - } - -} diff --git a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/DefaultColumnDeleteQuery.java b/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/DefaultColumnDeleteQuery.java deleted file mode 100644 index b27d17549..000000000 --- a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/DefaultColumnDeleteQuery.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - - - -import java.util.List; -import java.util.Objects; -import java.util.Optional; - -import static java.util.Collections.unmodifiableList; -import static java.util.Optional.ofNullable; - -/** - * The default implementation of {@link ColumnDeleteQuery} - */ -record DefaultColumnDeleteQuery(String name, ColumnCondition columnCondition, List columns) - implements ColumnDeleteQuery { - - - @Override - public Optional condition() { - return ofNullable(columnCondition); - } - - @Override - public List columns() { - return unmodifiableList(columns); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (!(o instanceof ColumnDeleteQuery that)) { - return false; - } - return Objects.equals(name, that.name()) && - Objects.equals(columnCondition, that.condition().orElse(null)) && - Objects.equals(columns, that.columns()); - } - - @Override - public int hashCode() { - return Objects.hash(name, columnCondition, columns); - } - - -} diff --git a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/DefaultColumnQuery.java b/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/DefaultColumnQuery.java deleted file mode 100644 index 2200f6d2e..000000000 --- a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/DefaultColumnQuery.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - - - -import jakarta.data.Sort; - -import java.util.Collections; -import java.util.List; -import java.util.Objects; -import java.util.Optional; - -import static java.util.Collections.unmodifiableList; -import static java.util.Optional.ofNullable; - -/** - * The default implementation of column query. - */ -record DefaultColumnQuery(long limit, long skip, String name, - List columns, List> sorts, ColumnCondition columnCondition) - implements ColumnQuery { - - - @Override - public Optional condition() { - return ofNullable(columnCondition).map(ColumnCondition::readOnly); - } - - @Override - public List columns() { - return unmodifiableList(columns); - } - - @Override - public List> sorts() { - return unmodifiableList(sorts); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (!(o instanceof ColumnQuery that)) { - return false; - } - return limit == that.limit() && - skip == that.skip() && - Objects.equals(name, that.name()) && - Objects.equals(columns, that.columns()) && - Objects.equals(sorts, that.sorts()) && - Objects.equals(columnCondition, that.condition().orElse(null)); - } - - @Override - public int hashCode() { - return Objects.hash(limit, skip, name, columns, sorts, columnCondition); - } - - - static ColumnQuery countBy(ColumnQuery query) { - return new DefaultColumnQuery(0, 0, query.name(), query.columns(), - Collections.emptyList(), query.condition().orElse(null)); - } - - static ColumnQuery existsBy(ColumnQuery query) { - return new DefaultColumnQuery(1, 0, query.name(), query.columns(), - Collections.emptyList(), query.condition().orElse(null)); - } -} diff --git a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/DefaultColumnQueryBuilder.java b/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/DefaultColumnQueryBuilder.java deleted file mode 100644 index f71107a15..000000000 --- a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/DefaultColumnQueryBuilder.java +++ /dev/null @@ -1,162 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - - -import jakarta.data.Sort; - -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; -import java.util.Optional; -import java.util.function.Consumer; -import java.util.stream.Stream; - -import static java.util.Objects.requireNonNull; - -class DefaultColumnQueryBuilder implements ColumnQuery.ColumnQueryBuilder { - - private final List columns = new ArrayList<>(); - - private final List> sorts = new ArrayList<>(); - - private String documentCollection; - - private ColumnCondition condition; - - private long skip; - - private long limit; - - - @Override - public ColumnQuery.ColumnQueryBuilder select(String column) { - Objects.requireNonNull(column, "column is required"); - this.columns.add(column); - return this; - } - - @Override - public ColumnQuery.ColumnQueryBuilder select(String... columns) { - Consumer validNull = d -> requireNonNull(d, "there is null column in the query"); - Consumer consume = this.columns::add; - Stream.of(columns).forEach(validNull.andThen(consume)); - return this; - } - - @Override - public ColumnQuery.ColumnQueryBuilder sort(Sort sort) { - Objects.requireNonNull(sort, "sort is required"); - this.sorts.add(sort); - return this; - } - - @Override - public ColumnQuery.ColumnQueryBuilder sort(Sort... sorts) { - Consumer validNull = d -> requireNonNull(d, "there is null document in the query"); - Consumer consume = this.sorts::add; - Stream.of(sorts).forEach(validNull.andThen(consume)); - return this; - } - - @Override - public ColumnQuery.ColumnQueryBuilder from(String documentCollection) { - Objects.requireNonNull(documentCollection, "documentCollection is required"); - this.documentCollection = documentCollection; - return this; - } - - @Override - public ColumnQuery.ColumnQueryBuilder where(ColumnCondition condition) { - Objects.requireNonNull(condition, "condition is required"); - this.condition = condition; - return this; - } - - @Override - public ColumnQuery.ColumnQueryBuilder skip(long skip) { - if (skip < 0) { - throw new IllegalArgumentException("The skip should not be negative, skip: " + skip); - } - this.skip = skip; - return this; - } - - @Override - public ColumnQuery.ColumnQueryBuilder limit(long limit) { - if (limit < 0) { - throw new IllegalArgumentException("The limit should not be negative, limit: " + limit); - } - this.limit = limit; - return this; - } - - @Override - public ColumnQuery build() { - if (Objects.isNull(documentCollection)) { - throw new IllegalArgumentException("The document collection is mandatory to build"); - } - return new DefaultColumnQuery(limit, skip, documentCollection, - columns, sorts, condition); - } - - @Override - public Stream getResult(ColumnManager manager) { - Objects.requireNonNull(manager, "manager is required"); - return manager.select(build()); - } - - @Override - public Optional getSingleResult(ColumnManager manager) { - Objects.requireNonNull(manager, "manager is required"); - return manager.singleResult(build()); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - DefaultColumnQueryBuilder that = (DefaultColumnQueryBuilder) o; - return skip == that.skip - && limit == that.limit - && Objects.equals(columns, that.columns) - && Objects.equals(sorts, that.sorts) - && Objects.equals(documentCollection, that.documentCollection) - && Objects.equals(condition, that.condition); - } - - @Override - public int hashCode() { - return Objects.hash(columns, sorts, documentCollection, condition, skip, limit); - } - - @Override - public String toString() { - return "DefaultColumnQueryBuilder{" + - "columns=" + columns + - ", sorts=" + sorts + - ", documentCollection='" + documentCollection + '\'' + - ", condition=" + condition + - ", skip=" + skip + - ", limit=" + limit + - '}'; - } -} diff --git a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/DefaultDeleteQueryBuilder.java b/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/DefaultDeleteQueryBuilder.java deleted file mode 100644 index bc25ad4a2..000000000 --- a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/DefaultDeleteQueryBuilder.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - -import jakarta.data.Sort; -import org.eclipse.jnosql.communication.column.ColumnDeleteQuery.ColumnDeleteQueryBuilder; - -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; -import java.util.function.Consumer; -import java.util.stream.Stream; - -import static java.util.Objects.requireNonNull; - -class DefaultDeleteQueryBuilder implements ColumnDeleteQueryBuilder { - - private final List documents = new ArrayList<>(); - - private final List sorts = new ArrayList<>(); - - private String documentCollection; - - private ColumnCondition condition; - - - @Override - public ColumnDeleteQueryBuilder delete(String column) { - Objects.requireNonNull(column, "column is required"); - this.documents.add(column); - return this; - } - - @Override - public ColumnDeleteQueryBuilder delete(String... columns) { - Consumer validNull = c -> requireNonNull(c, "there is null column in the query"); - Consumer consume = this.documents::add; - Stream.of(columns).forEach(validNull.andThen(consume)); - return this; - } - - @Override - public ColumnDeleteQueryBuilder from(String documentCollection) { - Objects.requireNonNull(documentCollection, "documentCollection is required"); - this.documentCollection = documentCollection; - return this; - } - - @Override - public ColumnDeleteQueryBuilder where(ColumnCondition condition) { - Objects.requireNonNull(condition, "condition is required"); - this.condition = condition; - return this; - } - - @Override - public ColumnDeleteQuery build() { - if (Objects.isNull(documentCollection)) { - throw new IllegalArgumentException("The document collection is mandatory to build"); - } - return new DefaultColumnDeleteQuery(documentCollection, condition, documents); - } - - @Override - public void delete(ColumnManager manager) { - Objects.requireNonNull(manager, "manager is required"); - manager.delete(build()); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - DefaultDeleteQueryBuilder that = (DefaultDeleteQueryBuilder) o; - return Objects.equals(documents, that.documents) - && Objects.equals(sorts, that.sorts) - && Objects.equals(documentCollection, that.documentCollection) - && Objects.equals(condition, that.condition); - } - - @Override - public int hashCode() { - return Objects.hash(documents, sorts, documentCollection, condition); - } - - @Override - public String toString() { - return "DefaultDeleteQueryBuilder{" + - "documents=" + documents + - ", sorts=" + sorts + - ", documentCollection='" + documentCollection + '\'' + - ", condition=" + condition + - '}'; - } -} diff --git a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/DefaultFluentColumnQueryBuilder.java b/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/DefaultFluentColumnQueryBuilder.java deleted file mode 100644 index 9fcb5d5d7..000000000 --- a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/DefaultFluentColumnQueryBuilder.java +++ /dev/null @@ -1,207 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - - -import jakarta.data.Sort; -import org.eclipse.jnosql.communication.column.ColumnQuery.ColumnFrom; -import org.eclipse.jnosql.communication.column.ColumnQuery.ColumnLimit; -import org.eclipse.jnosql.communication.column.ColumnQuery.ColumnNameCondition; -import org.eclipse.jnosql.communication.column.ColumnQuery.ColumnNameOrder; -import org.eclipse.jnosql.communication.column.ColumnQuery.ColumnNotCondition; -import org.eclipse.jnosql.communication.column.ColumnQuery.ColumnOrder; -import org.eclipse.jnosql.communication.column.ColumnQuery.ColumnQueryBuild; -import org.eclipse.jnosql.communication.column.ColumnQuery.ColumnSelect; -import org.eclipse.jnosql.communication.column.ColumnQuery.ColumnSkip; -import org.eclipse.jnosql.communication.column.ColumnQuery.ColumnWhere; - -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; -import java.util.stream.Stream; - -import static java.util.Objects.requireNonNull; - -/** - * The default implementation of the Select in the column - */ -class DefaultFluentColumnQueryBuilder extends BaseQueryBuilder implements - ColumnSelect, ColumnFrom, ColumnLimit, ColumnSkip, - ColumnOrder, ColumnNameCondition, ColumnNotCondition, ColumnNameOrder, ColumnWhere, ColumnQueryBuild { - - - private String columnFamily; - - private long skip; - - private long limit; - - private final List> sorts = new ArrayList<>(); - - private final List columns; - - - DefaultFluentColumnQueryBuilder(List columns) { - this.columns = columns; - } - - - @Override - public ColumnFrom from(String columnFamily) { - requireNonNull(columnFamily, "columnFamily is required"); - this.columnFamily = columnFamily; - return this; - } - - - @Override - public ColumnNameCondition where(String name) { - requireNonNull(name, "name is required"); - this.name = name; - return this; - } - - @Override - public ColumnNameCondition and(String name) { - requireNonNull(name, "name is required"); - this.name = name; - this.and = true; - return this; - } - - @Override - public ColumnNameCondition or(String name) { - requireNonNull(name, "name is required"); - this.name = name; - this.and = false; - return this; - } - - @Override - public ColumnSkip skip(long skip) { - if (skip < 0) { - throw new IllegalArgumentException("The skip should not be negative, skip: " + skip); - } - this.skip = skip; - return this; - } - - @Override - public ColumnLimit limit(long limit) { - if (limit < 0) { - throw new IllegalArgumentException("The limit should not be negative, limit: " + limit); - } - this.limit = limit; - return this; - } - - @Override - public ColumnOrder orderBy(String name) { - requireNonNull(name, "name is required"); - this.name = name; - return this; - } - - - @Override - public ColumnNotCondition not() { - this.negate = true; - return this; - } - - @Override - public ColumnWhere eq(T value) { - eqImpl(value); - return this; - } - - @Override - public ColumnWhere like(String value) { - likeImpl(value); - return this; - } - - @Override - public ColumnWhere gt(T value) { - gtImpl(value); - return this; - } - - @Override - public ColumnWhere gte(T value) { - gteImpl(value); - return this; - } - - @Override - public ColumnWhere lt(T value) { - ltImpl(value); - return this; - } - - - @Override - public ColumnWhere lte(T value) { - lteImpl(value); - return this; - } - - @Override - public ColumnWhere between(T valueA, T valueB) { - betweenImpl(valueA, valueB); - return this; - } - - - @Override - public ColumnWhere in(Iterable values) { - inImpl(values); - return this; - } - - - @Override - public ColumnNameOrder asc() { - this.sorts.add(Sort.asc(name)); - return this; - } - - @Override - public ColumnNameOrder desc() { - this.sorts.add(Sort.desc(name)); - return this; - } - - - @Override - public ColumnQuery build() { - return new DefaultColumnQuery(limit, skip, columnFamily, columns, sorts, condition); - } - - @Override - public Stream getResult(ColumnManager manager) { - requireNonNull(manager, "manager is required"); - return manager.select(this.build()); - } - - @Override - public Optional getSingleResult(ColumnManager manager) { - requireNonNull(manager, "manager is required"); - return manager.singleResult(this.build()); - } - -} diff --git a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/DefaultFluentDeleteQueryBuilder.java b/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/DefaultFluentDeleteQueryBuilder.java deleted file mode 100644 index 473c3cbbe..000000000 --- a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/DefaultFluentDeleteQueryBuilder.java +++ /dev/null @@ -1,143 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - - -import org.eclipse.jnosql.communication.column.ColumnDeleteQuery.ColumnDelete; -import org.eclipse.jnosql.communication.column.ColumnDeleteQuery.ColumnDeleteFrom; -import org.eclipse.jnosql.communication.column.ColumnDeleteQuery.ColumnDeleteNotCondition; -import org.eclipse.jnosql.communication.column.ColumnDeleteQuery.ColumnDeleteWhere; - -import java.util.List; - -import static java.util.Objects.requireNonNull; - -/** - * The default implementation to Delete query - */ -class DefaultFluentDeleteQueryBuilder extends BaseQueryBuilder implements ColumnDelete, ColumnDeleteFrom, - ColumnDeleteWhere, ColumnDeleteNotCondition { - - private String columnFamily; - - - private final List columns; - - - DefaultFluentDeleteQueryBuilder(List columns) { - this.columns = columns; - } - - @Override - public ColumnDeleteFrom from(String columnFamily) { - requireNonNull(columnFamily, "columnFamily is required"); - this.columnFamily = columnFamily; - return this; - } - - - @Override - public ColumnDeleteQuery.ColumnDeleteNameCondition where(String name) { - requireNonNull(name, "name is required"); - this.name = name; - return this; - } - - - @Override - public ColumnDeleteQuery.ColumnDeleteNameCondition and(String name) { - requireNonNull(name, "name is required"); - this.name = name; - this.and = true; - return this; - } - - @Override - public ColumnDeleteQuery.ColumnDeleteNameCondition or(String name) { - requireNonNull(name, "name is required"); - this.name = name; - this.and = false; - return this; - } - - - @Override - public ColumnDeleteNotCondition not() { - this.negate = true; - return this; - } - - @Override - public ColumnDeleteWhere eq(T value) { - eqImpl(value); - return this; - } - - @Override - public ColumnDeleteWhere like(String value) { - likeImpl(value); - return this; - } - - @Override - public ColumnDeleteWhere gt(T value) { - gtImpl(value); - return this; - } - - @Override - public ColumnDeleteWhere gte(T value) { - gteImpl(value); - return this; - } - - @Override - public ColumnDeleteWhere lt(T value) { - ltImpl(value); - return this; - } - - @Override - public ColumnDeleteWhere lte(T value) { - lteImpl(value); - return this; - } - - @Override - public ColumnDeleteWhere between(T valueA, T valueB) { - betweenImpl(valueA, valueB); - return this; - } - - @Override - public ColumnDeleteWhere in(Iterable values) { - inImpl(values); - return this; - } - - @Override - public ColumnDeleteQuery build() { - return new DefaultColumnDeleteQuery(columnFamily, condition, columns); - } - - @Override - public void delete(ColumnManager manager) { - requireNonNull(manager, "manager is required"); - manager.delete(this.build()); - } - -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/DeleteQueryParser.java b/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/DeleteQueryParser.java deleted file mode 100644 index 1de49ed45..000000000 --- a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/DeleteQueryParser.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - - -import org.eclipse.jnosql.communication.Params; -import org.eclipse.jnosql.communication.QueryException; -import org.eclipse.jnosql.communication.query.DeleteQuery; -import org.eclipse.jnosql.communication.query.DeleteQueryConverter; - -import java.util.List; -import java.util.function.BiFunction; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import static java.util.Objects.requireNonNull; - -public final class DeleteQueryParser implements BiFunction { - - - - Stream query(String query, ColumnManager manager, ColumnObserverParser observer) { - - ColumnDeleteQuery deleteQuery = getQuery(query, observer); - manager.delete(deleteQuery); - return Stream.empty(); - } - - - ColumnPreparedStatement prepare(String query, ColumnManager manager, - ColumnObserverParser observer) { - Params params = Params.newParams(); - ColumnDeleteQuery columnDeleteQuery = getQuery(query, params, observer); - return ColumnPreparedStatement.delete(columnDeleteQuery, params, query, manager); - } - - - - @Override - public ColumnDeleteQueryParams apply(DeleteQuery deleteQuery, - ColumnObserverParser columnObserverParser) { - - requireNonNull(deleteQuery, "deleteQuery is required"); - requireNonNull(columnObserverParser, "columnObserverParser is required"); - Params params = Params.newParams(); - ColumnDeleteQuery query = getQuery(params, columnObserverParser, deleteQuery); - return new ColumnDeleteQueryParams(query, params); - } - - private ColumnDeleteQuery getQuery(String query, Params params, ColumnObserverParser observer) { - DeleteQueryConverter converter = new DeleteQueryConverter(); - DeleteQuery deleteQuery = converter.apply(query); - - return getQuery(params, observer, deleteQuery); - } - - private ColumnDeleteQuery getQuery(Params params, ColumnObserverParser observer, DeleteQuery deleteQuery) { - String columnFamily = observer.fireEntity(deleteQuery.entity()); - List columns = deleteQuery.fields().stream() - .map(f -> observer.fireField(columnFamily, f)) - .collect(Collectors.toList()); - ColumnCondition condition = deleteQuery.where().map(c -> Conditions.getCondition(c, params, observer, columnFamily)) - .orElse(null); - - return new DefaultColumnDeleteQuery(columnFamily, condition, columns); - } - - private ColumnDeleteQuery getQuery(String query, ColumnObserverParser observer) { - - DeleteQueryConverter converter = new DeleteQueryConverter(); - DeleteQuery deleteQuery = converter.apply(query); - - String columnFamily = observer.fireEntity(deleteQuery.entity()); - List columns = deleteQuery.fields().stream() - .map(f -> observer.fireField(columnFamily, f)) - .collect(Collectors.toList()); - Params params = Params.newParams(); - - ColumnCondition condition = deleteQuery.where() - .map(c -> Conditions.getCondition(c, params, observer, columnFamily)).orElse(null); - - if (params.isNotEmpty()) { - throw new QueryException("To run a query with a parameter use a PrepareStatement instead."); - } - return new DefaultColumnDeleteQuery(columnFamily, condition, columns); - } -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/InsertQueryParser.java b/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/InsertQueryParser.java deleted file mode 100644 index eace00d2b..000000000 --- a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/InsertQueryParser.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - - -import org.eclipse.jnosql.communication.Params; -import org.eclipse.jnosql.communication.QueryException; -import org.eclipse.jnosql.communication.query.InsertQuery; -import org.eclipse.jnosql.communication.query.InsertQueryConverter; -import org.eclipse.jnosql.communication.query.JSONQueryValue; -import org.eclipse.jnosql.communication.query.QueryCondition; - -import java.time.Duration; -import java.util.List; -import java.util.Optional; -import java.util.stream.Stream; - -final class InsertQueryParser extends ConditionQueryParser { - - - - Stream query(String query, ColumnManager manager, ColumnObserverParser observer) { - - InsertQueryConverter converter = new InsertQueryConverter(); - InsertQuery insertQuery = converter.apply(query); - - String columnFamily = insertQuery.entity(); - Params params = Params.newParams(); - - ColumnEntity entity = getEntity(insertQuery, columnFamily, params, observer); - - Optional ttl = insertQuery.ttl(); - if (params.isNotEmpty()) { - throw new QueryException("To run a query with a parameter use a PrepareStatement instead."); - } - return ttl.map(duration -> Stream.of(manager.insert(entity, duration))) - .orElseGet(() -> Stream.of(manager.insert(entity))); - } - - - ColumnPreparedStatement prepare(String query, ColumnManager manager, - ColumnObserverParser observer) { - - InsertQueryConverter converter = new InsertQueryConverter(); - InsertQuery insertQuery = converter.apply(query); - - String columnFamily = observer.fireEntity(insertQuery.entity()); - Params params = Params.newParams(); - - Optional ttl = insertQuery.ttl(); - ColumnEntity entity = getEntity(insertQuery, columnFamily, params, observer); - - return ColumnPreparedStatement.insert(entity, params, query, ttl.orElse(null), manager); - - } - - private ColumnEntity getEntity(InsertQuery insertQuery, String columnFamily, Params params, - ColumnObserverParser observer) { - return getEntity(new InsertQueryConditionSupplier(insertQuery), columnFamily, params, observer); - } - - - private record InsertQueryConditionSupplier(InsertQuery query) implements ConditionQuerySupplier { - - @Override - public List conditions() { - return query.conditions(); - } - - @Override - public Optional value() { - return query.value(); - } - } - -} diff --git a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/JsonObjects.java b/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/JsonObjects.java deleted file mode 100644 index 430da0a9f..000000000 --- a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/JsonObjects.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - - -import jakarta.json.JsonObject; -import jakarta.json.bind.Jsonb; -import jakarta.json.bind.JsonbBuilder; -import java.util.List; -import java.util.Map; - -final class JsonObjects { - - private static final Jsonb JSON = JsonbBuilder.create(); - - private JsonObjects() { - } - - static List getColumns(JsonObject jsonObject) { - Map map = JSON.fromJson(jsonObject.toString(), Map.class); - return Columns.of(map); - } - -} diff --git a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/SelectQueryParser.java b/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/SelectQueryParser.java deleted file mode 100644 index 1ab74bf3d..000000000 --- a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/SelectQueryParser.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - - -import jakarta.data.Direction; -import jakarta.data.Sort; -import org.eclipse.jnosql.communication.Params; -import org.eclipse.jnosql.communication.QueryException; -import org.eclipse.jnosql.communication.query.SelectQuery; -import org.eclipse.jnosql.communication.query.SelectQueryConverter; - -import java.util.List; -import java.util.Objects; -import java.util.function.BiFunction; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import static java.util.stream.Collectors.toList; - -public final class SelectQueryParser implements BiFunction { - - - public SelectQueryParser() { - } - - Stream query(String query, ColumnManager manager, ColumnObserverParser observer) { - - ColumnQuery columnQuery = getColumnQuery(query, observer); - return manager.select(columnQuery); - } - - - ColumnPreparedStatement prepare(String query, ColumnManager manager, ColumnObserverParser observer) { - - Params params = Params.newParams(); - SelectQueryConverter converter = new SelectQueryConverter(); - SelectQuery selectQuery = converter.apply(query); - - ColumnQuery columnQuery = getColumnQuery(params, selectQuery, observer); - return ColumnPreparedStatement.select(columnQuery, params, query, manager); - } - - - @Override - public ColumnQueryParams apply(SelectQuery selectQuery, ColumnObserverParser observer) { - Objects.requireNonNull(selectQuery, "selectQuery is required"); - Objects.requireNonNull(observer, "observer is required"); - - Params params = Params.newParams(); - ColumnQuery columnQuery = getColumnQuery(params, selectQuery, observer); - return new ColumnQueryParams(columnQuery, params); - } - - - private ColumnQuery getColumnQuery(String query, ColumnObserverParser observer) { - - SelectQueryConverter converter = new SelectQueryConverter(); - SelectQuery selectQuery = converter.apply(query); - String columnFamily = observer.fireEntity(selectQuery.entity()); - long limit = selectQuery.limit(); - long skip = selectQuery.skip(); - List columns = selectQuery.fields().stream() - .map(f -> observer.fireField(columnFamily, f)) - .collect(Collectors.toList()); - List> sorts = selectQuery.orderBy().stream().map(s -> toSort(s, observer, columnFamily)) - .collect(toList()); - - Params params = Params.newParams(); - ColumnCondition condition = selectQuery.where() - .map(c -> Conditions.getCondition(c, params, observer, columnFamily)).orElse(null); - - if (params.isNotEmpty()) { - throw new QueryException("To run a query with a parameter use a PrepareStatement instead."); - } - return new DefaultColumnQuery(limit, skip, columnFamily, columns, sorts, condition); - } - - private ColumnQuery getColumnQuery(Params params, SelectQuery selectQuery, ColumnObserverParser observer) { - - String columnFamily = observer.fireEntity(selectQuery.entity()); - long limit = selectQuery.limit(); - long skip = selectQuery.skip(); - List columns = selectQuery.fields().stream() - .map(f -> observer.fireField(columnFamily, f)) - .collect(Collectors.toList()); - - List> sorts = selectQuery.orderBy().stream().map(s -> toSort(s, observer, columnFamily)).collect(toList()); - ColumnCondition condition = selectQuery.where() - .map(c -> Conditions.getCondition(c, params, observer, columnFamily)) - .orElse(null); - - return new DefaultColumnQuery(limit, skip, columnFamily, columns, sorts, condition); - } - - private Sort toSort(Sort sort, ColumnObserverParser observer, String entity) { - return Sort.of(observer.fireField(entity, sort.property()), - sort.isAscending()? Direction.ASC: Direction.DESC, false); - } - - -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/UpdateQueryParser.java b/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/UpdateQueryParser.java deleted file mode 100644 index 208edb872..000000000 --- a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/UpdateQueryParser.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - - -import org.eclipse.jnosql.communication.Params; -import org.eclipse.jnosql.communication.QueryException; -import org.eclipse.jnosql.communication.query.JSONQueryValue; -import org.eclipse.jnosql.communication.query.QueryCondition; -import org.eclipse.jnosql.communication.query.UpdateQuery; -import org.eclipse.jnosql.communication.query.UpdateQueryConverter; - -import java.util.List; -import java.util.Optional; -import java.util.stream.Stream; - -final class UpdateQueryParser extends ConditionQueryParser { - - - - Stream query(String query, ColumnManager manager, ColumnObserverParser observer) { - - UpdateQueryConverter converter = new UpdateQueryConverter(); - UpdateQuery updateQuery = converter.apply(query); - - Params params = Params.newParams(); - - ColumnEntity entity = getEntity(params, updateQuery, observer); - - if (params.isNotEmpty()) { - throw new QueryException("To run a query with a parameter use a PrepareStatement instead."); - } - return Stream.of(manager.update(entity)); - } - - - ColumnPreparedStatement prepare(String query, ColumnManager manager, ColumnObserverParser observer) { - - Params params = Params.newParams(); - - UpdateQueryConverter converter = new UpdateQueryConverter(); - UpdateQuery updateQuery = converter.apply(query); - - ColumnEntity entity = getEntity(params, updateQuery, observer); - - return ColumnPreparedStatement.update(entity, params, query, manager); - } - - - private ColumnEntity getEntity(Params params, UpdateQuery updateQuery, ColumnObserverParser observer) { - String columnFamily = observer.fireEntity(updateQuery.entity()); - - return getEntity(new UpdateQueryConditionSupplier(updateQuery), columnFamily, params, observer); - } - - private record UpdateQueryConditionSupplier(UpdateQuery query) implements ConditionQuerySupplier { - - - @Override - public List conditions() { - return query.conditions(); - } - - @Override - public Optional value() { - return query.value(); - } - } - -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/Values.java b/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/Values.java deleted file mode 100644 index bc6bc4625..000000000 --- a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/Values.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - -import org.eclipse.jnosql.communication.Params; -import org.eclipse.jnosql.communication.QueryException; -import org.eclipse.jnosql.communication.Value; -import org.eclipse.jnosql.communication.query.ArrayQueryValue; -import org.eclipse.jnosql.communication.query.Function; -import org.eclipse.jnosql.communication.query.FunctionQueryValue; -import org.eclipse.jnosql.communication.query.JSONQueryValue; -import org.eclipse.jnosql.communication.query.ParamQueryValue; -import org.eclipse.jnosql.communication.query.QueryValue; -import org.eclipse.jnosql.communication.query.ValueType; - -import java.util.Arrays; -import java.util.stream.Stream; - -import static java.util.stream.Collectors.toList; - -final class Values { - - private Values() { - } - - static Object get(QueryValue value, Params parameters) { - - ValueType type = value.type(); - switch (type) { - case NUMBER, STRING, BOOLEAN -> { - return value.get(); - } - case PARAMETER -> { - return parameters.add(ParamQueryValue.class.cast(value).get()); - } - case ARRAY -> { - return Stream.of(ArrayQueryValue.class.cast(value).get()) - .map(v -> get(v, parameters)) - .collect(toList()); - } - case FUNCTION -> { - Function function = FunctionQueryValue.class.cast(value).get(); - String name = function.name(); - Object[] params = function.params(); - if ("convert".equals(name)) { - return Value.of(get(QueryValue.class.cast(params[0]), parameters)) - .get((Class) params[1]); - } - String message = String.format("There is not support to the function: %s with parameters %s", name, - Arrays.toString(params)); - throw new QueryException(message); - } - case JSON -> { - return JsonObjects.getColumns(JSONQueryValue.class.cast(value).get()); - } - default -> throw new QueryException("There is not support to the value: " + type); - } - } -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/package-info.java b/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/package-info.java deleted file mode 100644 index a3990f215..000000000 --- a/jnosql-communication/jnosql-communication-column/src/main/java/org/eclipse/jnosql/communication/column/package-info.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ - -/** - * This package contains all objects to use a Column Family API. This API gonna focus in domain, in other words, - * ubiquitous language. A column family is a NoSQL object that contains columns of related data. It is a tuple (pair) - * that consists of a key-value pair, where the key is mapped to a value that is a set of columns. - * In analogy with relational databases, a column family is as a "table", each key-value pair being a "row". - * Each column is a tuple (triplet) consisting of a column name, a value, and a timestamp. In a relational - * database table, this data would be grouped together within a table with other non-related data. - * Ref: https://en.wikipedia.org/wiki/Column_family - * - */ -package org.eclipse.jnosql.communication.column; \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/ColumnDeleteQueryParamsTest.java b/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/ColumnDeleteQueryParamsTest.java deleted file mode 100644 index 959cbf552..000000000 --- a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/ColumnDeleteQueryParamsTest.java +++ /dev/null @@ -1,155 +0,0 @@ -/* - * Copyright (c) 2023 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Maximillian Arruda - * - */ -package org.eclipse.jnosql.communication.column; - -import org.eclipse.jnosql.communication.Params; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; - -import java.util.UUID; -import java.util.stream.Stream; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.params.provider.Arguments.arguments; - -class ColumnDeleteQueryParamsTest { - - @ParameterizedTest - @MethodSource("scenarios") - void shouldInstantiateSuccessfully(ColumnDeleteQuery query, Params params) { - ColumnDeleteQueryParams target = newInstance(query, params); - assertThat(target).isNotNull(); - } - - @ParameterizedTest - @MethodSource("scenarios") - void shouldReturnTheSameQueryInstance(ColumnDeleteQuery expectedQuery, Params params) { - var target = newInstance(expectedQuery, params); - assertThat(expectedQuery).isSameAs(target.query()); - } - - @ParameterizedTest - @MethodSource("scenarios") - void shouldReturnTheSameParamsInstance(ColumnDeleteQuery query, Params expectedParams) { - var target = newInstance(query, expectedParams); - assertThat(expectedParams).isSameAs(target.params()); - } - - @ParameterizedTest - @MethodSource("scenarios") - void shouldBeNotEqualsToNull(ColumnDeleteQuery query, Params params) { - var instance = newInstance(query, params); - assertThat(instance).isNotEqualTo(null); - } - - - @ParameterizedTest - @MethodSource("scenarios") - void shouldBeNotEqualsToAnyOtherInstanceOfDifferentType(ColumnDeleteQuery query, Params params) { - var instance = newInstance(query, params); - assertThat(instance).isNotEqualTo(new Object()); - } - - @ParameterizedTest - @MethodSource("scenarios") - void shouldBeEqualsToItself(ColumnDeleteQuery query, Params params) { - var instance = newInstance(query, params); - assertThat(instance).isEqualTo(instance); - } - - @Test - void shouldBeEqualsWhenQueryAndParamsAreUsedByTwoDifferentInstances() { - var query = newDummyColumnDeleteQuery(); - var params = newDummyParams(); - - var leftInstance = newInstance(query, params); - var rightInstance = newInstance(query, params); - - assertThat(leftInstance).isEqualTo(rightInstance); - } - - @ParameterizedTest - @MethodSource("scenarios") - void shouldBeNotEqualsWhenDifferentQueryAndParamsAreUsedByTwoDifferentInstances(ColumnDeleteQuery query, Params params) { - - var leftInstance = newInstance(query, params); - var rightInstance = newInstance(newDummyColumnDeleteQuery(), newDummyParams()); - - assertThat(leftInstance).isNotEqualTo(rightInstance); - } - - @Test - void shouldHashCodeBeConditionedToQueryAndParamsAttributes() { - - ColumnDeleteQuery firstQuery = newDummyColumnDeleteQuery(); - Params firstParams = newDummyParams(); - - var fistInstance = newInstance(firstQuery, firstParams); - var secondInstance = newInstance(firstQuery, firstParams); - - assertThat(fistInstance).hasSameHashCodeAs(secondInstance); - - ColumnDeleteQuery secondQuery = newDummyColumnDeleteQuery(); - Params secondParams = newDummyParams(); - - var thirdInstance = newInstance(secondQuery, secondParams); - - assertThat(fistInstance.hashCode()).isNotEqualTo(thirdInstance.hashCode()); - - } - - static Stream scenarios() { - return Stream.of( - givenNullArguments(), - givenColumnDeleteQueryOnly(), - givenParamsOnly(), - givenValidArguments() - ); - } - - private ColumnDeleteQueryParams newInstance(ColumnDeleteQuery query, Params params) { - return new ColumnDeleteQueryParams(query,params); - } - - private static Params newDummyParams() { - Params params = Params.newParams(); - params.add(UUID.randomUUID().toString()); - return params; - } - - private static ColumnDeleteQuery newDummyColumnDeleteQuery() { - return ColumnDeleteQuery.builder().from(UUID.randomUUID().toString()).build(); - } - - private static Arguments givenValidArguments() { - return arguments(newDummyColumnDeleteQuery(), newDummyParams()); - } - - private static Arguments givenParamsOnly() { - return arguments(null, newDummyParams()); - } - - private static Arguments givenColumnDeleteQueryOnly() { - return arguments(newDummyColumnDeleteQuery(), null); - } - - private static Arguments givenNullArguments() { - return arguments(null, null); - } - -} diff --git a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/ColumnDeleteQueryTest.java b/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/ColumnDeleteQueryTest.java deleted file mode 100644 index c9f67aed3..000000000 --- a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/ColumnDeleteQueryTest.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (c) 2023 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Maximillian Arruda - * - */ - -package org.eclipse.jnosql.communication.column; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; - -import java.util.Objects; -import java.util.stream.Stream; - -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.params.provider.Arguments.arguments; - -class ColumnDeleteQueryTest { - - @Test - void shouldBuilderThrownException() { - assertThrows(NullPointerException.class, () -> ColumnDeleteQuery.builder(new String[]{null})); - } - - @ParameterizedTest(name = "{0} passed to build method then a valid builder should be returned") - @MethodSource("testScenarios") - void shouldBuildReturnsAValidBuilder(String scenario, String[] documents) { - var builder = Objects.isNull(documents) ? ColumnDeleteQuery.builder() : ColumnDeleteQuery.builder(documents); - assertNotNull(builder); - } - - @Test - void shouldDeleteThrownException() { - assertThrows(NullPointerException.class, () -> ColumnDeleteQuery.delete(new String[]{null})); - } - - @ParameterizedTest(name = "{0} passed to delete method then a valid builder should be returned") - @MethodSource("testScenarios") - void shouldDeleteReturnsAValidBuilder(String scenario, String[] documents) { - var builder = Objects.isNull(documents) ? ColumnDeleteQuery.delete() : ColumnDeleteQuery.delete(documents); - assertNotNull(builder); - } - - static Stream testScenarios() { - return Stream.of( - arguments("when an empty array", new String[0]), - arguments("when a non empty array", new String[]{"doc1", "doc2", "doc2"}), - arguments("when zero arguments", null) - ); - } - -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/ColumnEntityTest.java b/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/ColumnEntityTest.java deleted file mode 100644 index cf89a43b7..000000000 --- a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/ColumnEntityTest.java +++ /dev/null @@ -1,438 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ - -package org.eclipse.jnosql.communication.column; - -import org.assertj.core.api.SoftAssertions; -import org.eclipse.jnosql.communication.TypeReference; -import org.eclipse.jnosql.communication.Value; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Optional; - -import static java.util.Arrays.asList; -import static java.util.Collections.singletonList; -import static java.util.Collections.singletonMap; -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotSame; -import static org.junit.jupiter.api.Assertions.assertTrue; - - -class ColumnEntityTest { - - @Test - void shouldReturnErrorWhenNameIsNull() { - Assertions.assertThrows(NullPointerException.class, () -> ColumnEntity.of(null)); - } - - @Test - void shouldReturnErrorWhenColumnsIsNull() { - Assertions.assertThrows(NullPointerException.class, () -> ColumnEntity.of("entity", null)); - } - - @Test - void shouldReturnOneColumn() { - ColumnEntity entity = ColumnEntity.of("entity"); - assertEquals(Integer.valueOf(0), Integer.valueOf(entity.size())); - assertTrue(entity.isEmpty()); - - entity.add(Column.of("name", "name")); - entity.add(Column.of("name2", Value.of("name2"))); - assertFalse(entity.isEmpty()); - assertEquals(Integer.valueOf(2), Integer.valueOf(entity.size())); - assertFalse(ColumnEntity.of("entity", singletonList(Column.of("name", "name"))).isEmpty()); - } - - @Test - void shouldDoCopy() { - ColumnEntity entity = ColumnEntity.of("entity", singletonList(Column.of("name", "name"))); - ColumnEntity copy = entity.copy(); - assertNotSame(entity, copy); - assertEquals(entity, copy); - - } - - @Test - void shouldFindColumn() { - Column column = Column.of("name", "name"); - ColumnEntity entity = ColumnEntity.of("entity", singletonList(column)); - Optional name = entity.find("name"); - Optional notfound = entity.find("not_found"); - assertTrue(name.isPresent()); - assertFalse(notfound.isPresent()); - assertEquals(column, name.get()); - } - - @Test - void shouldReturnErrorWhenFindColumnIsNull() { - Column column = Column.of("name", "name"); - Assertions.assertThrows(NullPointerException.class, () -> { - ColumnEntity entity = ColumnEntity.of("entity", singletonList(column)); - entity.find(null); - }); - } - - @Test - void shouldFindValue() { - Column column = Column.of("name", "name"); - ColumnEntity entity = ColumnEntity.of("entity", singletonList(column)); - Optional name = entity.find("name", String.class); - Assertions.assertNotNull(name); - Assertions.assertTrue(name.isPresent()); - Assertions.assertEquals("name", name.orElse("")); - } - - @Test - void shouldNotFindValue() { - Column column = Column.of("name", "name"); - ColumnEntity entity = ColumnEntity.of("entity", singletonList(column)); - Optional name = entity.find("not_found", String.class); - Assertions.assertNotNull(name); - Assertions.assertFalse(name.isPresent()); - } - - @Test - void shouldFindTypeSupplier() { - Column column = Column.of("name", "name"); - ColumnEntity entity = ColumnEntity.of("entity", singletonList(column)); - List names = entity.find("name", new TypeReference>() {}) - .orElse(Collections.emptyList()); - Assertions.assertNotNull(names); - Assertions.assertFalse(names.isEmpty()); - assertThat(names).contains("name"); - } - - @Test - void shouldNotFindTypeSupplier() { - Column column = Column.of("name", "name"); - ColumnEntity entity = ColumnEntity.of("entity", singletonList(column)); - List names = entity.find("not_find", new TypeReference>() {}) - .orElse(Collections.emptyList()); - Assertions.assertNotNull(names); - Assertions.assertTrue(names.isEmpty()); - } - - @Test - void shouldRemoveColumn() { - Column column = Column.of("name", "name"); - ColumnEntity entity = ColumnEntity.of("entity", singletonList(column)); - assertTrue(entity.remove("name")); - assertTrue(entity.isEmpty()); - } - - @Test - void shouldConvertToMap() { - Column column = Column.of("name", "name"); - ColumnEntity entity = ColumnEntity.of("entity", singletonList(column)); - Map result = entity.toMap(); - assertFalse(result.isEmpty()); - assertEquals(Integer.valueOf(1), Integer.valueOf(result.size())); - assertEquals(column.name(), result.keySet().stream().findAny().get()); - } - - @Test - void shouldConvertSubColumn() { - Column column = Column.of("name", "name"); - ColumnEntity entity = ColumnEntity.of("entity", singletonList(Column.of("sub", column))); - Map result = entity.toMap(); - assertFalse(result.isEmpty()); - assertEquals(Integer.valueOf(1), Integer.valueOf(result.size())); - Map map = (Map) result.get("sub"); - assertEquals("name", map.get("name")); - } - - - @Test - void shouldConvertSubColumnListToMap() { - ColumnEntity entity = ColumnEntity.of("entity"); - entity.add(Column.of("_id", "id")); - List columns = asList(Column.of("name", "Ada"), Column.of("type", "type"), - Column.of("information", "ada@lovelace.com")); - - entity.add(Column.of("contacts", columns)); - Map result = entity.toMap(); - assertEquals("id", result.get("_id")); - List> contacts = (List>) result.get("contacts"); - assertEquals(3, contacts.size()); - assertThat(contacts).contains(singletonMap("name", "Ada"), singletonMap("type", "type"), - singletonMap("information", "ada@lovelace.com")); - - } - - @Test - void shouldConvertSubColumnListToMap2() { - ColumnEntity entity = ColumnEntity.of("entity"); - entity.add(Column.of("_id", "id")); - List> columns = new ArrayList<>(); - columns.add(asList(Column.of("name", "Ada"), Column.of("type", "type"), - Column.of("information", "ada@lovelace.com"))); - - entity.add(Column.of("contacts", columns)); - Map result = entity.toMap(); - assertEquals("id", result.get("_id")); - List>> contacts = (List>>) result.get("contacts"); - assertEquals(1, contacts.size()); - List> maps = contacts.get(0); - assertEquals(3, maps.size()); - assertThat(maps).contains(singletonMap("name", "Ada"), singletonMap("type", "type"), - singletonMap("information", "ada@lovelace.com")); - - } - - @Test - void shouldCreateANewInstance() { - String name = "name"; - ColumnEntity entity = new ColumnEntity(name); - assertEquals(name, entity.name()); - } - - @Test - void shouldCreateAnEmptyEntity() { - ColumnEntity entity = new ColumnEntity("name"); - assertTrue(entity.isEmpty()); - } - - @Test - void shouldReturnAnErrorWhenAddANullColumn() { - Assertions.assertThrows(NullPointerException.class, () -> { - ColumnEntity entity = new ColumnEntity("name"); - entity.add(null); - }); - } - - @Test - void shouldAddANewColumn() { - ColumnEntity entity = new ColumnEntity("name"); - entity.add(Column.of("column", 12)); - assertFalse(entity.isEmpty()); - assertEquals(1, entity.size()); - } - - @Test - void shouldReturnErrorWhenAddAnNullIterable() { - Assertions.assertThrows(NullPointerException.class, () -> { - ColumnEntity entity = new ColumnEntity("name"); - entity.addAll(null); - }); - } - - @Test - void shouldAddAllColumns() { - ColumnEntity entity = new ColumnEntity("name"); - entity.addAll(Arrays.asList(Column.of("name", 12), Column.of("value", "value"))); - assertFalse(entity.isEmpty()); - assertEquals(2, entity.size()); - } - - - @Test - void shouldNotFindColumn() { - ColumnEntity entity = new ColumnEntity("name"); - Optional column = entity.find("name"); - assertFalse(column.isPresent()); - } - - @Test - void shouldRemoveByName() { - ColumnEntity entity = new ColumnEntity("name"); - entity.add(Column.of("value", 32D)); - assertTrue(entity.remove("value")); - assertTrue(entity.isEmpty()); - } - - @Test - void shouldReturnErrorWhenRemovedNameIsNull() { - Assertions.assertThrows(NullPointerException.class, () -> { - ColumnEntity entity = new ColumnEntity("name"); - entity.remove(null); - }); - } - - @Test - void shouldNotRemoveByName() { - ColumnEntity entity = new ColumnEntity("name"); - entity.add(Column.of("value", 32D)); - - assertFalse(entity.remove("value1")); - assertFalse(entity.isEmpty()); - } - - - @Test - void shouldReturnErrorWhenRemoveByNameIsNull() { - Assertions.assertThrows(NullPointerException.class, () -> { - ColumnEntity entity = new ColumnEntity("name"); - entity.remove(null); - }); - } - - - @Test - void shouldAddColumnAsNameAndObject() { - ColumnEntity entity = new ColumnEntity("columnFamily"); - entity.add("name", 10); - assertEquals(1, entity.size()); - Optional name = entity.find("name"); - assertTrue(name.isPresent()); - assertEquals(10, name.get().get()); - } - - @Test - void shouldAddColumnAsNameAndValue() { - ColumnEntity entity = new ColumnEntity("columnFamily"); - entity.add("name", Value.of(10)); - assertEquals(1, entity.size()); - Optional name = entity.find("name"); - assertTrue(name.isPresent()); - assertEquals(10, name.get().get()); - } - - @Test - void shouldReturnWhenAddColumnsObjectWhenHasNullObject() { - ColumnEntity entity = new ColumnEntity("columnFamily"); - entity.add("name", null); - assertEquals(1, entity.size()); - Column name = entity.find("name").orElseThrow(); - SoftAssertions.assertSoftly(softly -> { - softly.assertThat(name.name()).isEqualTo("name"); - softly.assertThat(name.get()).isNull(); - }); - } - - @Test - void shouldReturnErrorWhenAddColumnsObjectWhenHasNullColumnName() { - Assertions.assertThrows(NullPointerException.class, () -> { - ColumnEntity entity = new ColumnEntity("columnFamily"); - entity.add(null, 10); - }); - } - - @Test - void shouldReturnErrorWhenAddColumnsValueWhenHasNullColumnName() { - Assertions.assertThrows(NullPointerException.class, () -> { - ColumnEntity entity = new ColumnEntity("columnFamily"); - entity.add(null, Value.of(12)); - }); - } - - - @Test - void shouldAvoidDuplicatedColumn() { - ColumnEntity entity = new ColumnEntity("columnFamily"); - entity.add("name", 10); - entity.add("name", 13); - assertEquals(1, entity.size()); - Optional column = entity.find("name"); - assertEquals(Column.of("name", 13), column.get()); - } - - @Test - void shouldAvoidDuplicatedColumnWhenAddList() { - List columns = asList(Column.of("name", 10), Column.of("name", 13)); - ColumnEntity entity = new ColumnEntity("columnFamily"); - entity.addAll(columns); - assertEquals(1, entity.size()); - assertEquals(1, ColumnEntity.of("columnFamily", columns).size()); - } - - @Test - void shouldReturnsTheColumnNames() { - List columns = asList(Column.of("name", 10), Column.of("name2", 11), - Column.of("name3", 12), Column.of("name4", 13), - Column.of("name5", 14), Column.of("name5", 16)); - - ColumnEntity columnFamily = ColumnEntity.of("columnFamily", columns); - assertThat(columnFamily.columnNames()) - .hasSize(5) - .contains("name", "name2", "name3", "name4", "name5"); - - } - - @Test - void shouldReturnsTheColumnValues() { - List columns = asList(Column.of("name", 10), Column.of("name2", 11), - Column.of("name3", 12), Column.of("name4", 13), - Column.of("name5", 14), Column.of("name5", 16)); - - ColumnEntity columnFamily = ColumnEntity.of("columnFamily", columns); - assertThat(columnFamily.values()).contains(Value.of(10), Value.of(11), Value.of(12), - Value.of(13), Value.of(16)); - } - - @Test - void shouldReturnTrueWhenContainsElement() { - List columns = asList(Column.of("name", 10), Column.of("name2", 11), - Column.of("name3", 12), Column.of("name4", 13), - Column.of("name5", 14), Column.of("name5", 16)); - - ColumnEntity columnFamily = ColumnEntity.of("columnFamily", columns); - - assertTrue(columnFamily.contains("name")); - assertTrue(columnFamily.contains("name2")); - assertTrue(columnFamily.contains("name3")); - assertTrue(columnFamily.contains("name4")); - assertTrue(columnFamily.contains("name5")); - } - - @Test - void shouldReturnFalseWhenDoesNotContainElement() { - List columns = asList(Column.of("name", 10), Column.of("name2", 11), - Column.of("name3", 12), Column.of("name4", 13), - Column.of("name5", 14), Column.of("name5", 16)); - - ColumnEntity columnFamily = ColumnEntity.of("columnFamily", columns); - - assertFalse(columnFamily.contains("name6")); - assertFalse(columnFamily.contains("name7")); - assertFalse(columnFamily.contains("name8")); - assertFalse(columnFamily.contains("name9")); - assertFalse(columnFamily.contains("name10")); - } - - @Test - void shouldRemoveAllElementsWhenUseClearMethod() { - List columns = asList(Column.of("name", 10), Column.of("name2", 11), - Column.of("name3", 12), Column.of("name4", 13), - Column.of("name5", 14), Column.of("name5", 16)); - - ColumnEntity columnFamily = ColumnEntity.of("columnFamily", columns); - - assertFalse(columnFamily.isEmpty()); - columnFamily.clear(); - assertTrue(columnFamily.isEmpty()); - } - - @Test - void shouldCreateNull(){ - ColumnEntity entity = ColumnEntity.of("entity"); - entity.addNull("name"); - Column name = entity.find("name").orElseThrow(); - SoftAssertions.assertSoftly(softly -> { - softly.assertThat(name.name()).isEqualTo("name"); - softly.assertThat(name.get()).isNull(); - }); - } - -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/ColumnParamsTest.java b/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/ColumnParamsTest.java deleted file mode 100644 index 681516a15..000000000 --- a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/ColumnParamsTest.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * Maximillian Arruda - * - */ -package org.eclipse.jnosql.communication.column; - -import org.eclipse.jnosql.communication.Params; -import org.eclipse.jnosql.communication.Value; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -class ColumnParamsTest { - - @Test - void shouldSetParameter() { - Params params = Params.newParams(); - Value name = params.add("name"); - Column column = Column.of("name", name); - params.bind("name", "Ada Lovelace"); - - assertEquals("Ada Lovelace", column.get()); - - params.bind("name", "Diana"); - assertEquals("Diana", column.get()); - } - -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/ColumnTest.java b/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/ColumnTest.java deleted file mode 100644 index e377ea61f..000000000 --- a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/ColumnTest.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - - -import org.assertj.core.api.SoftAssertions; -import org.eclipse.jnosql.communication.TypeReference; -import org.eclipse.jnosql.communication.Value; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.util.List; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; - -class ColumnTest { - - private static final Value DEFAULT_VALUE = Value.of(12); - - @Test - void shouldReturnNameWhenNameIsNull() { - Assertions.assertThrows(NullPointerException.class, () -> Column.of(null, DEFAULT_VALUE)); - } - - @Test - void shouldReturnNameWhenValueIsNull() { - Column column = Column.of("Name", null); - SoftAssertions.assertSoftly(softly -> { - softly.assertThat(column.name()).isEqualTo("Name"); - softly.assertThat(column.value().isNull()).isTrue(); - }); - } - - @Test - void shouldCreateAnColumnInstance() { - String name = "name"; - Column column = Column.of(name, DEFAULT_VALUE); - assertNotNull(column); - assertEquals(name, column.name()); - assertEquals(DEFAULT_VALUE, column.value()); - } - - @Test - void shouldBeEquals() { - assertEquals(Column.of("name", DEFAULT_VALUE), Column.of("name", DEFAULT_VALUE)); - } - - @Test - void shouldReturnGetObject() { - Value value = Value.of("text"); - Column column = Column.of("name", value); - assertEquals(value.get(), column.get()); - } - - @Test - void shouldReturnGetClass() { - Value value = Value.of("text"); - Column column = Column.of("name", value); - assertEquals(value.get(String.class), column.get(String.class)); - } - - - @Test - void shouldReturnGetType() { - Value value = Value.of("text"); - Column column = Column.of("name", value); - TypeReference> typeReference = new TypeReference<>() { - }; - assertEquals(value.get(typeReference), column.get(typeReference)); - } -} diff --git a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/ColumnsTest.java b/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/ColumnsTest.java deleted file mode 100644 index 981322c8d..000000000 --- a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/ColumnsTest.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ - -package org.eclipse.jnosql.communication.column; - -import org.junit.jupiter.api.Test; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import static java.util.Collections.singletonList; -import static java.util.Collections.singletonMap; -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; - - -class ColumnsTest { - - @Test - void shouldCreateColumn() { - Column column = Columns.of("name", "Ada"); - assertEquals("name", column.name()); - assertEquals("Ada", column.get()); - } - - @Test - void shouldCreateColumnsFromMap() { - Map map = singletonMap("name", "Ada"); - List columns = Columns.of(map); - assertFalse(columns.isEmpty()); - assertThat(columns).contains(Column.of("name", "Ada")); - } - - - @Test - void shouldCreateRecursiveMap() { - List>> list = new ArrayList<>(); - Map map = singletonMap("mobile", "55 1234-4567"); - list.add(singletonList(map)); - - List columns = Columns.of(singletonMap("contact", list)); - assertEquals(1, columns.size()); - Column column = columns.get(0); - assertEquals("contact", column.name()); - List> result = (List>) column.get(); - assertEquals(Column.of("mobile", "55 1234-4567"), result.get(0).get(0)); - - } -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/DefaultColumnConditionTest.java b/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/DefaultColumnConditionTest.java deleted file mode 100644 index 5af59c8c6..000000000 --- a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/DefaultColumnConditionTest.java +++ /dev/null @@ -1,247 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - -import org.eclipse.jnosql.communication.Condition; -import org.eclipse.jnosql.communication.TypeReference; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrows; - - -class DefaultColumnConditionTest { - - private final ColumnCondition lte = ColumnCondition.lte(Column.of("salary", 10.32)); - - @Test - void shouldReturnErrorWhenColumnIsNull() { - assertThrows(NullPointerException.class, () -> ColumnCondition.of(null, Condition.AND)); - } - - @Test - void shouldCreateAnInstance() { - Column name = Column.of("name", "Otavio"); - ColumnCondition condition = ColumnCondition.of(name, Condition.EQUALS); - assertNotNull(condition); - assertEquals(name, condition.column()); - assertEquals(Condition.EQUALS, condition.condition()); - } - - @Test - void shouldCreateNegationCondition() { - Column age = Column.of("age", 26); - ColumnCondition condition = ColumnCondition.of(age, Condition.GREATER_THAN); - ColumnCondition negate = condition.negate(); - Column negateColumn = negate.column(); - assertEquals(Condition.NOT, negate.condition()); - assertEquals(Condition.NOT.getNameField(), negateColumn.name()); - assertEquals(ColumnCondition.of(age, Condition.GREATER_THAN), negateColumn.value().get()); - } - - @Test - void shouldReturnValidDoubleNegation() { - Column age = Column.of("age", 26); - ColumnCondition condition = ColumnCondition.of(age, Condition.GREATER_THAN); - ColumnCondition affirmative = condition.negate().negate(); - Assertions.assertEquals(condition, affirmative); - } - - @Test - void shouldCreateAndCondition() { - Column age = Column.of("age", 26); - Column name = Column.of("name", "Otavio"); - ColumnCondition condition1 = ColumnCondition.of(name, Condition.EQUALS); - ColumnCondition condition2 = ColumnCondition.of(age, Condition.GREATER_THAN); - - ColumnCondition and = condition1.and(condition2); - Column andColumn = and.column(); - assertEquals(Condition.AND, and.condition()); - assertEquals(Condition.AND.getNameField(), andColumn.name()); - assertThat(andColumn.value().get(new TypeReference>() { - })).contains(condition1, condition2); - - } - - @Test - void shouldCreateOrCondition() { - Column age = Column.of("age", 26); - Column name = Column.of("name", "Otavio"); - ColumnCondition condition1 = ColumnCondition.of(name, Condition.EQUALS); - ColumnCondition condition2 = ColumnCondition.of(age, Condition.GREATER_THAN); - - ColumnCondition and = condition1.or(condition2); - Column andColumn = and.column(); - assertEquals(Condition.OR, and.condition()); - assertEquals(Condition.OR.getNameField(), andColumn.name()); - assertThat(andColumn.value().get(new TypeReference>() { - })).contains(condition1, condition2); - - } - - @Test - void shouldReturnErrorWhenCreateAndWithNullValues() { - assertThrows(NullPointerException.class, () -> ColumnCondition.and((ColumnCondition[]) null)); - } - - - @Test - void shouldReturnErrorWhenCreateOrWithNullValues() { - assertThrows(NullPointerException.class, () -> ColumnCondition.or((ColumnCondition[]) null)); - } - - - @Test - void shouldAppendAnd() { - ColumnCondition eq = ColumnCondition.eq(Column.of("name", "otavio")); - ColumnCondition gt = ColumnCondition.gt(Column.of("age", 10)); - ColumnCondition and = ColumnCondition.and(eq, gt); - assertEquals(Condition.AND, and.condition()); - List conditions = and.column().get(new TypeReference<>() { - }); - assertThat(conditions).contains(eq, gt); - } - - @Test - void shouldAppendOr() { - ColumnCondition eq = ColumnCondition.eq(Column.of("name", "otavio")); - ColumnCondition gt = ColumnCondition.gt(Column.of("age", 10)); - ColumnCondition and = ColumnCondition.or(eq, gt); - assertEquals(Condition.OR, and.condition()); - List conditions = and.column().get(new TypeReference<>() { - }); - assertThat(conditions).contains(eq, gt); - } - - @Test - void shouldAnd() { - ColumnCondition eq = ColumnCondition.eq(Column.of("name", "otavio")); - ColumnCondition gt = ColumnCondition.gt(Column.of("age", 10)); - ColumnCondition lte = ColumnCondition.lte(Column.of("salary", 10_000.00)); - - ColumnCondition and = eq.and(gt); - List conditions = and.column().get(new TypeReference<>() { - }); - assertEquals(Condition.AND, and.condition()); - assertThat(conditions).contains(eq, gt); - ColumnCondition result = and.and(lte); - - assertEquals(Condition.AND, result.condition()); - assertThat(result.column().get(new TypeReference>() { - })).contains(eq, gt, lte); - - } - - @Test - void shouldOr() { - ColumnCondition eq = ColumnCondition.eq(Column.of("name", "otavio")); - ColumnCondition gt = ColumnCondition.gt(Column.of("age", 10)); - ColumnCondition lte = ColumnCondition.lte(Column.of("salary", 10_000.00)); - - ColumnCondition or = eq.or(gt); - List conditions = or.column().get(new TypeReference<>() { - }); - assertEquals(Condition.OR, or.condition()); - assertThat(conditions).contains(eq, gt); - ColumnCondition result = or.or(lte); - - assertEquals(Condition.OR, result.condition()); - assertThat(result.column().get(new TypeReference>() { - })).contains(eq, gt, lte); - - } - - @Test - void shouldNegate() { - ColumnCondition eq = ColumnCondition.eq(Column.of("name", "otavio")); - ColumnCondition negate = eq.negate(); - assertEquals(Condition.NOT, negate.condition()); - ColumnCondition condition = negate.column().get(ColumnCondition.class); - assertEquals(eq, condition); - } - - @Test - void shouldAffirmDoubleNegate() { - ColumnCondition eq = ColumnCondition.eq(Column.of("name", "otavio")); - ColumnCondition affirm = eq.negate().negate(); - assertEquals(eq.condition(), affirm.condition()); - - } - - @Test - void shouldReturnErrorWhenBetweenIsNull() { - assertThrows(NullPointerException.class, () -> ColumnCondition.between(null)); - } - - @Test - void shouldReturnErrorWhenBetweenIsNotIterable() { - assertThrows(IllegalArgumentException.class, () -> { - Column column = Column.of("age", 12); - ColumnCondition.between(column); - }); - } - - @Test - void shouldReturnErrorWhenIterableHasOneElement() { - assertThrows(IllegalArgumentException.class, () -> { - Column column = Column.of("age", Collections.singleton(12)); - ColumnCondition.between(column); - }); - } - - @Test - void shouldReturnErrorWhenIterableHasMoreThanTwoElement2() { - assertThrows(IllegalArgumentException.class, () -> { - Column column = Column.of("age", Arrays.asList(12, 12, 12)); - ColumnCondition.between(column); - }); - } - - @Test - void shouldReturnBetween() { - Column column = Column.of("age", Arrays.asList(12, 13)); - ColumnCondition between = ColumnCondition.between(column); - assertEquals(Condition.BETWEEN, between.condition()); - Iterable integers = between.column().get(new TypeReference<>() { - }); - assertThat(integers).contains(12, 13); - } - - @Test - void shouldReturnErrorWhenInConditionIsInvalid() { - assertThrows(NullPointerException.class, () -> ColumnCondition.in(null)); - assertThrows(IllegalArgumentException.class, () -> ColumnCondition.in(Column.of("value", 10))); - } - - @Test - void shouldReturnInClause() { - Column column = Column.of("age", Arrays.asList(12, 13)); - ColumnCondition in = ColumnCondition.in(column); - assertEquals(Condition.IN, in.condition()); - Iterable integers = in.column().get(new TypeReference<>() { - }); - assertThat(integers).contains(12, 13); - } - -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/DefaultColumnDeleteQueryTest.java b/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/DefaultColumnDeleteQueryTest.java deleted file mode 100644 index c9e132a73..000000000 --- a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/DefaultColumnDeleteQueryTest.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertTrue; - - -class DefaultColumnDeleteQueryTest { - - private ColumnDeleteQuery query; - - - @BeforeEach - void setUp() { - query = ColumnDeleteQuery.delete().from("columnFamily").build(); - } - - @Test - void shouldNotEditColumns() { - Assertions.assertThrows(UnsupportedOperationException.class, () -> { - List columns = query.columns(); - assertTrue(columns.isEmpty()); - columns.clear(); - }); - } - - @Test - void shouldHasCode(){ - var query = ColumnDeleteQuery.delete().from("columnFamily").build(); - var query1 =ColumnDeleteQuery.delete().from("columnFamily").build(); - assertThat(query.hashCode()).isEqualTo(query1.hashCode()); - } - - @Test - void shouldEquals(){ - var query =ColumnDeleteQuery.delete().from("columnFamily").build(); - var query1 =ColumnDeleteQuery.delete().from("columnFamily").build(); - assertThat(query).isEqualTo(query1); - assertThat(query).isEqualTo(query); - assertThat(query).isNotEqualTo("query"); - } -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/DefaultColumnQueryBuilderTest.java b/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/DefaultColumnQueryBuilderTest.java deleted file mode 100644 index 11dc5c0ac..000000000 --- a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/DefaultColumnQueryBuilderTest.java +++ /dev/null @@ -1,362 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - -import org.eclipse.jnosql.communication.Condition; -import jakarta.data.Sort; -import org.eclipse.jnosql.communication.TypeReference; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.mockito.ArgumentCaptor; -import org.mockito.Mockito; - -import java.util.Arrays; -import java.util.List; -import java.util.Optional; -import java.util.stream.Stream; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.eclipse.jnosql.communication.column.ColumnCondition.eq; -import static org.eclipse.jnosql.communication.column.ColumnQuery.builder; -import static org.junit.jupiter.api.Assertions.*; - -class DefaultColumnQueryBuilderTest { - - @Test - void shouldReturnErrorWhenHasNullElementInSelect() { - assertThrows(NullPointerException.class, () -> builder("document", "document'", null)); - } - - @Test - void shouldBuilder() { - String documentCollection = "documentCollection"; - ColumnQuery query = builder().from(documentCollection).build(); - assertTrue(query.columns().isEmpty()); - assertFalse(query.condition().isPresent()); - assertEquals(documentCollection, query.name()); - } - - @Test - void shouldSelectDocument() { - String documentCollection = "documentCollection"; - ColumnQuery query = builder("document", "document2").from(documentCollection).build(); - assertThat(query.columns()).contains("document", "document2"); - assertFalse(query.condition().isPresent()); - assertEquals(documentCollection, query.name()); - } - - @Test - void shouldReturnErrorWhenFromIsNull() { - assertThrows(NullPointerException.class, () -> builder().from(null)); - } - - - @Test - void shouldSelectOrderAsc() { - String documentCollection = "documentCollection"; - ColumnQuery query = builder().from(documentCollection).sort(Sort.asc("name")).build(); - assertTrue(query.columns().isEmpty()); - assertFalse(query.condition().isPresent()); - assertEquals(documentCollection, query.name()); - assertThat(query.sorts()).contains(Sort.asc("name")); - } - - @Test - void shouldSelectOrderDesc() { - String documentCollection = "documentCollection"; - ColumnQuery query = builder().from(documentCollection).sort(Sort.desc("name")).build(); - assertTrue(query.columns().isEmpty()); - assertFalse(query.condition().isPresent()); - assertEquals(documentCollection, query.name()); - assertThat(query.sorts()).contains(Sort.desc("name")); - } - - - @Test - void shouldReturnErrorSelectWhenOrderIsNull() { - assertThrows(NullPointerException.class,() -> { - String documentCollection = "documentCollection"; - builder().from(documentCollection).sort((Sort) null); - }); - } - - @Test - void shouldSelectLimit() { - String documentCollection = "documentCollection"; - ColumnQuery query = builder().from(documentCollection).limit(10).build(); - assertTrue(query.columns().isEmpty()); - assertFalse(query.condition().isPresent()); - assertEquals(documentCollection, query.name()); - assertEquals(10L, query.limit()); - } - - @Test - void shouldReturnErrorWhenLimitIsNegative() { - String documentCollection = "documentCollection"; - Assertions.assertThrows(IllegalArgumentException.class, () -> builder().from(documentCollection).limit(-1)); - } - - @Test - void shouldSelectSkip() { - String documentCollection = "documentCollection"; - ColumnQuery query = builder().from(documentCollection).skip(10).build(); - assertTrue(query.columns().isEmpty()); - assertFalse(query.condition().isPresent()); - assertEquals(documentCollection, query.name()); - assertEquals(10L, query.skip()); - } - - @Test - void shouldReturnErrorWhenSkipIsNegative() { - String documentCollection = "documentCollection"; - Assertions.assertThrows(IllegalArgumentException.class, () -> builder().from(documentCollection).skip(-1)); - } - - @Test - void shouldSelectWhereNameEq() { - String documentCollection = "documentCollection"; - String name = "Ada Lovelace"; - - ColumnQuery query = builder().from(documentCollection) - .where(eq("name", name)) - .build(); - ColumnCondition condition = query.condition().get(); - - Column column = condition.column(); - - assertTrue(query.columns().isEmpty()); - assertEquals(documentCollection, query.name()); - assertEquals(Condition.EQUALS, condition.condition()); - assertEquals("name", column.name()); - assertEquals(name, column.get()); - - } - - @Test - void shouldSelectWhereNameLike() { - String documentCollection = "documentCollection"; - String name = "Ada Lovelace"; - ColumnQuery query = builder().from(documentCollection) - .where(ColumnCondition.like("name", name)) - .build(); - ColumnCondition condition = query.condition().get(); - - Column column = condition.column(); - - assertTrue(query.columns().isEmpty()); - assertEquals(documentCollection, query.name()); - assertEquals(Condition.LIKE, condition.condition()); - assertEquals("name", column.name()); - assertEquals(name, column.get()); - } - - @Test - void shouldSelectWhereNameGt() { - String documentCollection = "documentCollection"; - Number value = 10; - - ColumnQuery query = builder().from(documentCollection).where(ColumnCondition.gt("name", 10)) - .build(); - - ColumnCondition condition = query.condition().get(); - - Column column = condition.column(); - - assertTrue(query.columns().isEmpty()); - assertEquals(documentCollection, query.name()); - assertEquals(Condition.GREATER_THAN, condition.condition()); - assertEquals("name", column.name()); - assertEquals(value, column.get()); - } - - @Test - void shouldSelectWhereNameGte() { - String documentCollection = "documentCollection"; - Number value = 10; - ColumnCondition gteName = ColumnCondition.gte("name", value); - ColumnQuery query = builder().from(documentCollection).where(gteName).build(); - ColumnCondition condition = query.condition().get(); - - Column column = condition.column(); - - assertTrue(query.columns().isEmpty()); - assertEquals(documentCollection, query.name()); - assertEquals(Condition.GREATER_EQUALS_THAN, condition.condition()); - assertEquals("name", column.name()); - assertEquals(value, column.get()); - } - - @Test - void shouldSelectWhereNameLt() { - String documentCollection = "documentCollection"; - Number value = 10; - - ColumnQuery query = builder().from(documentCollection).where(ColumnCondition.lt("name", value)) - .build(); - ColumnCondition condition = query.condition().get(); - - Column column = condition.column(); - - assertTrue(query.columns().isEmpty()); - assertEquals(documentCollection, query.name()); - assertEquals(Condition.LESSER_THAN, condition.condition()); - assertEquals("name", column.name()); - assertEquals(value, column.get()); - } - - @Test - void shouldSelectWhereNameLte() { - String documentCollection = "documentCollection"; - Number value = 10; - ColumnQuery query = builder().from(documentCollection).where(ColumnCondition.lte("name", value)) - .build(); - ColumnCondition condition = query.condition().get(); - - Column column = condition.column(); - - assertTrue(query.columns().isEmpty()); - assertEquals(documentCollection, query.name()); - assertEquals(Condition.LESSER_EQUALS_THAN, condition.condition()); - assertEquals("name", column.name()); - assertEquals(value, column.get()); - } - - @Test - void shouldSelectWhereNameBetween() { - String documentCollection = "documentCollection"; - Number valueA = 10; - Number valueB = 20; - - ColumnQuery query = builder().from(documentCollection) - .where(ColumnCondition.between("name", Arrays.asList(valueA, valueB))) - .build(); - ColumnCondition condition = query.condition().get(); - - Column column = condition.column(); - - assertTrue(query.columns().isEmpty()); - assertEquals(documentCollection, query.name()); - assertEquals(Condition.BETWEEN, condition.condition()); - assertEquals("name", column.name()); - assertThat(column.get(new TypeReference>() { - })).contains(10, 20); - } - - @Test - void shouldSelectWhereNameNot() { - String documentCollection = "documentCollection"; - String name = "Ada Lovelace"; - - ColumnQuery query = builder().from(documentCollection).where(eq("name", name).negate()) - .build(); - ColumnCondition condition = query.condition().get(); - - Column column = condition.column(); - ColumnCondition negate = column.get(ColumnCondition.class); - assertTrue(query.columns().isEmpty()); - assertEquals(documentCollection, query.name()); - assertEquals(Condition.NOT, condition.condition()); - assertEquals(Condition.EQUALS, negate.condition()); - assertEquals("name", negate.column().name()); - assertEquals(name, negate.column().get()); - } - - - @Test - void shouldSelectWhereNameAnd() { - String documentCollection = "documentCollection"; - String name = "Ada Lovelace"; - ColumnCondition nameEqualsAda = eq("name", name); - ColumnCondition ageOlderTen = ColumnCondition.gt("age", 10); - ColumnQuery query = builder().from(documentCollection) - .where(ColumnCondition.and(nameEqualsAda, ageOlderTen)) - .build(); - ColumnCondition condition = query.condition().get(); - - Column column = condition.column(); - List conditions = column.get(new TypeReference<>() { - }); - assertEquals(Condition.AND, condition.condition()); - org.assertj.core.api.Assertions.assertThat(conditions).contains(eq(Column.of("name", name)), - ColumnCondition.gt(Column.of("age", 10))); - } - - @Test - void shouldSelectWhereNameOr() { - String documentCollection = "documentCollection"; - String name = "Ada Lovelace"; - ColumnCondition nameEqualsAda = eq("name", name); - ColumnCondition ageOlderTen = ColumnCondition.gt("age", 10); - ColumnQuery query = builder().from(documentCollection).where(ColumnCondition.or(nameEqualsAda, ageOlderTen)) - .build(); - ColumnCondition condition = query.condition().get(); - - Column column = condition.column(); - List conditions = column.get(new TypeReference<>() { - }); - assertEquals(Condition.OR, condition.condition()); - org.assertj.core.api.Assertions.assertThat(conditions).contains(eq(Column.of("name", name)), - ColumnCondition.gt(Column.of("age", 10))); - } - - - @Test - void shouldSelectNegate() { - String columnFamily = "columnFamily"; - ColumnCondition nameNotEqualsLucas = eq("name", "Lucas").negate(); - ColumnQuery query = builder().from(columnFamily) - .where(nameNotEqualsLucas).build(); - - ColumnCondition condition = query.condition().orElseThrow(RuntimeException::new); - assertEquals(columnFamily, query.name()); - Column column = condition.column(); - List conditions = column.get(new TypeReference<>() { - }); - - assertEquals(Condition.NOT, condition.condition()); - org.assertj.core.api.Assertions.assertThat(conditions).contains(eq(Column.of("name", "Lucas"))); - - } - - - @Test - void shouldExecuteManager() { - ColumnManager manager = Mockito.mock(ColumnManager.class); - ArgumentCaptor queryCaptor = ArgumentCaptor.forClass(ColumnQuery.class); - String collection = "collection"; - Stream entities = builder().from(collection).getResult(manager); - Mockito.verify(manager).select(queryCaptor.capture()); - checkQuery(queryCaptor, collection); - } - - @Test - void shouldExecuteSingleResultManager() { - ColumnManager manager = Mockito.mock(ColumnManager.class); - ArgumentCaptor queryCaptor = ArgumentCaptor.forClass(ColumnQuery.class); - String collection = "collection"; - Optional entities = builder().from(collection).getSingleResult(manager); - Mockito.verify(manager).singleResult(queryCaptor.capture()); - checkQuery(queryCaptor, collection); - } - - private void checkQuery(ArgumentCaptor queryCaptor, String collection) { - ColumnQuery query = queryCaptor.getValue(); - assertTrue(query.columns().isEmpty()); - assertFalse(query.condition().isPresent()); - assertEquals(collection, query.name()); - } -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/DefaultColumnQueryParserTest.java b/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/DefaultColumnQueryParserTest.java deleted file mode 100644 index ecac4342b..000000000 --- a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/DefaultColumnQueryParserTest.java +++ /dev/null @@ -1,230 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - -import org.eclipse.jnosql.communication.Condition; -import jakarta.data.exceptions.NonUniqueResultException; -import org.eclipse.jnosql.communication.QueryException; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; -import org.mockito.ArgumentCaptor; -import org.mockito.Mockito; - -import java.util.Optional; -import java.util.stream.Stream; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; - -class DefaultColumnQueryParserTest { - - private final ColumnQueryParser parser = new ColumnQueryParser(); - - - private final ColumnManager manager = Mockito.mock(ColumnManager.class); - - @Test - void shouldReturnNPEWhenThereIsNullParameter() { - assertThrows(NullPointerException.class, () -> parser.query(null, manager, ColumnObserverParser.EMPTY)); - assertThrows(NullPointerException.class, () -> parser.query("select * from God", null, ColumnObserverParser.EMPTY)); - } - - @Test - void shouldReturnErrorWhenHasInvalidQuery() { - assertThrows(QueryException.class, () -> parser.query("inva", manager, ColumnObserverParser.EMPTY)); - assertThrows(QueryException.class, () -> parser.query("invalid", manager, ColumnObserverParser.EMPTY)); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God"}) - void shouldReturnParserQuery(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnQuery.class); - parser.query(query, manager, ColumnObserverParser.EMPTY); - Mockito.verify(manager).select(captor.capture()); - ColumnQuery columnQuery = captor.getValue(); - - assertTrue(columnQuery.columns().isEmpty()); - assertTrue(columnQuery.sorts().isEmpty()); - assertEquals(0L, columnQuery.limit()); - assertEquals(0L, columnQuery.skip()); - assertEquals("God", columnQuery.name()); - assertFalse(columnQuery.condition().isPresent()); - - } - - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God"}) - void shouldReturnParserQuery1(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnDeleteQuery.class); - parser.query(query, manager, ColumnObserverParser.EMPTY); - Mockito.verify(manager).delete(captor.capture()); - ColumnDeleteQuery columnDeleteQuery = captor.getValue(); - - assertTrue(columnDeleteQuery.columns().isEmpty()); - assertEquals("God", columnDeleteQuery.name()); - assertFalse(columnDeleteQuery.condition().isPresent()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"insert God (name = \"Diana\")"}) - void shouldReturnParserQuery2(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnEntity.class); - parser.query(query, manager, ColumnObserverParser.EMPTY); - Mockito.verify(manager).insert(captor.capture()); - ColumnEntity entity = captor.getValue(); - - - assertEquals("God", entity.name()); - assertEquals(Column.of("name", "Diana"), entity.find("name").get()); - } - - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"update God (name = \"Diana\")"}) - void shouldReturnParserQuery3(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnEntity.class); - parser.query(query, manager, ColumnObserverParser.EMPTY); - Mockito.verify(manager).update(captor.capture()); - ColumnEntity entity = captor.getValue(); - - - assertEquals("God", entity.name()); - assertEquals(Column.of("name", "Diana"), entity.find("name").get()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where age = @age"}) - void shouldExecutePrepareStatement(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnDeleteQuery.class); - - ColumnPreparedStatement prepare = parser.prepare(query, manager, ColumnObserverParser.EMPTY); - prepare.bind("age", 12); - prepare.result(); - Mockito.verify(manager).delete(captor.capture()); - ColumnDeleteQuery columnDeleteQuery = captor.getValue(); - ColumnCondition columnCondition = columnDeleteQuery.condition().get(); - Column column = columnCondition.column(); - assertEquals(Condition.EQUALS, columnCondition.condition()); - assertEquals("age", column.name()); - assertEquals(12, column.get()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"insert God (name = @name)"}) - void shouldExecutePrepareStatement1(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnEntity.class); - ColumnPreparedStatement prepare = parser.prepare(query, manager, ColumnObserverParser.EMPTY); - prepare.bind("name", "Diana"); - prepare.result(); - Mockito.verify(manager).insert(captor.capture()); - ColumnEntity entity = captor.getValue(); - assertEquals("God", entity.name()); - assertEquals(Column.of("name", "Diana"), entity.find("name").get()); - - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where age = @age"}) - void shouldExecutePrepareStatement2(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnQuery.class); - - ColumnPreparedStatement prepare = parser.prepare(query, manager, ColumnObserverParser.EMPTY); - prepare.bind("age", 12); - prepare.result(); - Mockito.verify(manager).select(captor.capture()); - ColumnQuery columnQuery = captor.getValue(); - ColumnCondition columnCondition = columnQuery.condition().get(); - Column column = columnCondition.column(); - assertEquals(Condition.EQUALS, columnCondition.condition()); - assertEquals("age", column.name()); - assertEquals(12, column.get()); - } - - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"update God (name = @name)"}) - void shouldExecutePrepareStatement3(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnEntity.class); - ColumnPreparedStatement prepare = parser.prepare(query, manager, ColumnObserverParser.EMPTY); - prepare.bind("name", "Diana"); - prepare.result(); - Mockito.verify(manager).update(captor.capture()); - ColumnEntity entity = captor.getValue(); - assertEquals("God", entity.name()); - assertEquals(Column.of("name", "Diana"), entity.find("name").get()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where age = @age"}) - void shouldSingleResult(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnQuery.class); - - Mockito.when(manager.select(Mockito.any(ColumnQuery.class))) - .thenReturn(Stream.of(Mockito.mock(ColumnEntity.class))); - - ColumnPreparedStatement prepare = parser.prepare(query, manager, ColumnObserverParser.EMPTY); - prepare.bind("age", 12); - final Optional result = prepare.singleResult(); - Mockito.verify(manager).select(captor.capture()); - ColumnQuery columnQuery = captor.getValue(); - ColumnCondition columnCondition = columnQuery.condition().get(); - Column column = columnCondition.column(); - assertEquals(Condition.EQUALS, columnCondition.condition()); - assertEquals("age", column.name()); - assertEquals(12, column.get()); - assertTrue(result.isPresent()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where age = @age"}) - void shouldReturnEmptySingleResult(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnQuery.class); - - Mockito.when(manager.select(Mockito.any(ColumnQuery.class))) - .thenReturn(Stream.empty()); - - ColumnPreparedStatement prepare = parser.prepare(query, manager, ColumnObserverParser.EMPTY); - prepare.bind("age", 12); - final Optional result = prepare.singleResult(); - Mockito.verify(manager).select(captor.capture()); - ColumnQuery columnQuery = captor.getValue(); - ColumnCondition columnCondition = columnQuery.condition().get(); - Column column = columnCondition.column(); - assertEquals(Condition.EQUALS, columnCondition.condition()); - assertEquals("age", column.name()); - assertEquals(12, column.get()); - assertFalse(result.isPresent()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where age = @age"}) - void shouldReturnErrorSingleResult(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnQuery.class); - - Mockito.when(manager.select(Mockito.any(ColumnQuery.class))) - .thenReturn(Stream.of(Mockito.mock(ColumnEntity.class), Mockito.mock(ColumnEntity.class))); - - ColumnPreparedStatement prepare = parser.prepare(query, manager, ColumnObserverParser.EMPTY); - prepare.bind("age", 12); - assertThrows(NonUniqueResultException.class, prepare::singleResult); - } - -} diff --git a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/DefaultColumnQueryTest.java b/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/DefaultColumnQueryTest.java deleted file mode 100644 index 348a88652..000000000 --- a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/DefaultColumnQueryTest.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - -import jakarta.data.Sort; -import org.eclipse.jnosql.communication.Condition; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import java.util.List; - -import static org.eclipse.jnosql.communication.column.ColumnQuery.select; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - - -class DefaultColumnQueryTest { - - private ColumnQuery query; - - @BeforeEach - public void setUp() { - query = select().from("columnFamily").build(); - } - - @Test - void shouldNotRemoveColumns() { - Assertions.assertThrows(UnsupportedOperationException.class, () -> { - List columns = query.columns(); - assertTrue(columns.isEmpty()); - columns.clear(); - }); - } - - @Test - void shouldNotRemoveSort() { - Assertions.assertThrows(UnsupportedOperationException.class, () -> { - List> sorts = query.sorts(); - assertTrue(sorts.isEmpty()); - sorts.clear(); - }); - } - - @Test - void shouldConvertCountyBy() { - ColumnQuery query = ColumnQuery.select().from("entity") - .where("name").eq("predicate") - .orderBy("name").asc().build(); - - ColumnQuery countQuery = DefaultColumnQuery.countBy(query); - Assertions.assertNotNull(countQuery); - assertEquals("entity", countQuery.name()); - assertEquals(0, countQuery.limit()); - assertEquals(0, countQuery.skip()); - assertTrue(countQuery.sorts().isEmpty()); - ColumnCondition condition = countQuery.condition().orElseThrow(); - Assertions.assertEquals(Condition.EQUALS, condition.condition()); - } - - @Test - void shouldConvertExistsBy() { - ColumnQuery query = ColumnQuery.select().from("entity") - .where("name").eq("predicate") - .orderBy("name").asc().build(); - - ColumnQuery countQuery = DefaultColumnQuery.existsBy(query); - Assertions.assertNotNull(countQuery); - assertEquals("entity", countQuery.name()); - assertEquals(1, countQuery.limit()); - assertEquals(0, countQuery.skip()); - assertTrue(countQuery.sorts().isEmpty()); - ColumnCondition condition = countQuery.condition().orElseThrow(); - Assertions.assertEquals(Condition.EQUALS, condition.condition()); - } - - @Test - void shouldHasCode(){ - ColumnQuery query = ColumnQuery.select().from("entity") - .where("name").eq("predicate") - .orderBy("name").asc().build(); - ColumnQuery query2 = ColumnQuery.select().from("entity") - .where("name").eq("predicate") - .orderBy("name").asc().build(); - - Assertions.assertEquals(query.hashCode(), query2.hashCode()); - } - - @Test - void shouldEquals(){ - ColumnQuery query = ColumnQuery.select().from("entity") - .where("name").eq("predicate") - .orderBy("name").asc().build(); - ColumnQuery query2 = ColumnQuery.select().from("entity") - .where("name").eq("predicate") - .orderBy("name").asc().build(); - - Assertions.assertEquals(query, query2); - Assertions.assertEquals(query, query); - Assertions.assertNotEquals(query, "query"); - } -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/DefaultDeleteQueryBuilderTest.java b/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/DefaultDeleteQueryBuilderTest.java deleted file mode 100644 index 6004bbc71..000000000 --- a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/DefaultDeleteQueryBuilderTest.java +++ /dev/null @@ -1,265 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - - -import org.eclipse.jnosql.communication.Condition; -import org.eclipse.jnosql.communication.TypeReference; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.mockito.ArgumentCaptor; -import org.mockito.Mockito; - -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.eclipse.jnosql.communication.column.ColumnCondition.eq; -import static org.eclipse.jnosql.communication.column.ColumnDeleteQuery.delete; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.verify; - -class DefaultDeleteQueryBuilderTest { - - @Test - void shouldReturnErrorWhenHasNullElementInSelect() { - Assertions.assertThrows(NullPointerException.class, () -> delete("column", "column", null)); - } - - @Test - void shouldDelete() { - String columnFamily = "columnFamily"; - ColumnDeleteQuery query = delete().from(columnFamily).build(); - assertTrue(query.columns().isEmpty()); - assertFalse(query.condition().isPresent()); - assertEquals(columnFamily, query.name()); - } - - @Test - void shouldDeleteColumns() { - String columnFamily = "columnFamily"; - ColumnDeleteQuery query = delete("column", "column2").from(columnFamily).build(); - assertThat(query.columns()).contains("column", "column2"); - assertFalse(query.condition().isPresent()); - assertEquals(columnFamily, query.name()); - } - - - @Test - void shouldReturnErrorWhenFromIsNull() { - Assertions.assertThrows(NullPointerException.class, () -> delete().from(null)); - } - - - @Test - void shouldSelectWhereNameEq() { - String columnFamily = "columnFamily"; - String name = "Ada Lovelace"; - ColumnDeleteQuery query = delete().from(columnFamily).where("name").eq(name).build(); - ColumnCondition condition = query.condition().get(); - - Column column = condition.column(); - - assertTrue(query.columns().isEmpty()); - assertEquals(columnFamily, query.name()); - assertEquals(Condition.EQUALS, condition.condition()); - assertEquals("name", column.name()); - assertEquals(name, column.get()); - - } - - @Test - void shouldSelectWhereNameLike() { - String columnFamily = "columnFamily"; - String name = "Ada Lovelace"; - ColumnDeleteQuery query = delete().from(columnFamily).where("name").like(name).build(); - ColumnCondition condition = query.condition().get(); - - Column column = condition.column(); - - assertTrue(query.columns().isEmpty()); - assertEquals(columnFamily, query.name()); - assertEquals(Condition.LIKE, condition.condition()); - assertEquals("name", column.name()); - assertEquals(name, column.get()); - } - - @Test - void shouldSelectWhereNameGt() { - String columnFamily = "columnFamily"; - Number value = 10; - ColumnDeleteQuery query = delete().from(columnFamily).where("name").gt(value).build(); - ColumnCondition condition = query.condition().get(); - - Column column = condition.column(); - - assertTrue(query.columns().isEmpty()); - assertEquals(columnFamily, query.name()); - assertEquals(Condition.GREATER_THAN, condition.condition()); - assertEquals("name", column.name()); - assertEquals(value, column.get()); - } - - @Test - void shouldSelectWhereNameGte() { - String columnFamily = "columnFamily"; - Number value = 10; - ColumnDeleteQuery query = delete().from(columnFamily).where("name").gte(value).build(); - ColumnCondition condition = query.condition().get(); - - Column column = condition.column(); - - assertTrue(query.columns().isEmpty()); - assertEquals(columnFamily, query.name()); - assertEquals(Condition.GREATER_EQUALS_THAN, condition.condition()); - assertEquals("name", column.name()); - assertEquals(value, column.get()); - } - - @Test - void shouldSelectWhereNameLt() { - String columnFamily = "columnFamily"; - Number value = 10; - ColumnDeleteQuery query = delete().from(columnFamily).where("name").lt(value).build(); - ColumnCondition columnCondition = query.condition().get(); - - Column column = columnCondition.column(); - - assertTrue(query.columns().isEmpty()); - assertEquals(columnFamily, query.name()); - assertEquals(Condition.LESSER_THAN, columnCondition.condition()); - assertEquals("name", column.name()); - assertEquals(value, column.get()); - } - - @Test - void shouldSelectWhereNameLte() { - String columnFamily = "columnFamily"; - Number value = 10; - ColumnDeleteQuery query = delete().from(columnFamily).where("name").lte(value).build(); - ColumnCondition columnCondition = query.condition().get(); - - Column column = columnCondition.column(); - - assertTrue(query.columns().isEmpty()); - assertEquals(columnFamily, query.name()); - assertEquals(Condition.LESSER_EQUALS_THAN, columnCondition.condition()); - assertEquals("name", column.name()); - assertEquals(value, column.get()); - } - - @Test - void shouldSelectWhereNameBetween() { - String columnFamily = "columnFamily"; - Number valueA = 10; - Number valueB = 20; - ColumnDeleteQuery query = delete().from(columnFamily).where("name").between(valueA, valueB).build(); - ColumnCondition condition = query.condition().get(); - - Column column = condition.column(); - - assertTrue(query.columns().isEmpty()); - assertEquals(columnFamily, query.name()); - assertEquals(Condition.BETWEEN, condition.condition()); - assertEquals("name", column.name()); - assertThat(column.get(new TypeReference>() { - })).contains(10, 20); - } - - @Test - void shouldSelectWhereNameNot() { - String columnFamily = "columnFamily"; - String name = "Ada Lovelace"; - ColumnDeleteQuery query = delete().from(columnFamily).where("name").not().eq(name).build(); - ColumnCondition condition = query.condition().get(); - - Column column = condition.column(); - ColumnCondition negate = column.get(ColumnCondition.class); - assertTrue(query.columns().isEmpty()); - assertEquals(columnFamily, query.name()); - assertEquals(Condition.NOT, condition.condition()); - assertEquals(Condition.EQUALS, negate.condition()); - assertEquals("name", negate.column().name()); - assertEquals(name, negate.column().get()); - } - - - @Test - void shouldSelectWhereNameAnd() { - String columnFamily = "columnFamily"; - String name = "Ada Lovelace"; - ColumnDeleteQuery query = delete().from(columnFamily).where("name").eq(name).and("age").gt(10).build(); - ColumnCondition condition = query.condition().get(); - - Column column = condition.column(); - List conditions = column.get(new TypeReference<>() { - }); - assertEquals(Condition.AND, condition.condition()); - org.assertj.core.api.Assertions.assertThat(conditions).contains(eq(Column.of("name", name)), - ColumnCondition.gt(Column.of("age", 10))); - } - - @Test - void shouldSelectWhereNameOr() { - String columnFamily = "columnFamily"; - String name = "Ada Lovelace"; - ColumnDeleteQuery query = delete().from(columnFamily).where("name").eq(name).or("age").gt(10).build(); - ColumnCondition condition = query.condition().get(); - - Column column = condition.column(); - List conditions = column.get(new TypeReference<>() { - }); - assertEquals(Condition.OR, condition.condition()); - org.assertj.core.api.Assertions.assertThat(conditions).contains(eq(Column.of("name", name)), - ColumnCondition.gt(Column.of("age", 10))); - } - - @Test - void shouldDeleteNegate() { - String columnFamily = "columnFamily"; - ColumnDeleteQuery query = delete().from(columnFamily).where("city").not().eq("Assis") - .and("name").not().eq("Lucas").build(); - - ColumnCondition condition = query.condition().orElseThrow(RuntimeException::new); - assertEquals(columnFamily, query.name()); - Column column = condition.column(); - List conditions = column.get(new TypeReference<>() { - }); - - assertEquals(Condition.AND, condition.condition()); - org.assertj.core.api.Assertions.assertThat(conditions).contains(eq(Column.of("city", "Assis")).negate(), - eq(Column.of("name", "Lucas")).negate()); - - - } - - @Test - void shouldExecuteDelete() { - String columnFamily = "columnFamily"; - ColumnManager manager = Mockito.mock(ColumnManager.class); - ArgumentCaptor queryCaptor = ArgumentCaptor.forClass(ColumnDeleteQuery.class); - delete().from(columnFamily).delete(manager); - verify(manager).delete(queryCaptor.capture()); - - ColumnDeleteQuery query = queryCaptor.getValue(); - assertTrue(query.columns().isEmpty()); - assertFalse(query.condition().isPresent()); - assertEquals(columnFamily, query.name()); - } - -} diff --git a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/DefaultSelectQueryBuilderTest.java b/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/DefaultSelectQueryBuilderTest.java deleted file mode 100644 index 032cd846f..000000000 --- a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/DefaultSelectQueryBuilderTest.java +++ /dev/null @@ -1,343 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - -import org.eclipse.jnosql.communication.Condition; -import jakarta.data.Sort; -import jakarta.data.Direction; -import org.eclipse.jnosql.communication.TypeReference; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.mockito.ArgumentCaptor; -import org.mockito.Mockito; - -import java.util.List; -import java.util.Optional; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.eclipse.jnosql.communication.column.ColumnCondition.eq; -import static org.eclipse.jnosql.communication.column.ColumnQuery.builder; -import static org.eclipse.jnosql.communication.column.ColumnQuery.select; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; - -class DefaultSelectQueryBuilderTest { - - - @Test - void shouldReturnErrorWhenHasNullElementInSelect() { - Assertions.assertThrows(NullPointerException.class, () -> select("column", "column", null)); - } - - @Test - void shouldSelect() { - String columnFamily = "columnFamily"; - ColumnQuery query = select().from(columnFamily).build(); - assertTrue(query.columns().isEmpty()); - assertFalse(query.condition().isPresent()); - assertEquals(columnFamily, query.name()); - } - - @Test - void shouldSelectColumns() { - String columnFamily = "columnFamily"; - ColumnQuery query = select("column", "column2").from(columnFamily).build(); - assertThat(query.columns()).contains("column", "column2"); - assertFalse(query.condition().isPresent()); - assertEquals(columnFamily, query.name()); - } - - @Test - void shouldReturnErrorWhenFromIsNull() { - Assertions.assertThrows(NullPointerException.class, () -> select().from(null)); - } - - - @Test - void shouldSelectOrderAsc() { - String columnFamily = "columnFamily"; - ColumnQuery query = select().from(columnFamily).orderBy("name").asc().build(); - assertTrue(query.columns().isEmpty()); - assertFalse(query.condition().isPresent()); - assertEquals(columnFamily, query.name()); - assertThat(query.sorts()).contains(Sort.of("name", Direction.ASC, false)); - } - - @Test - void shouldSelectOrderDesc() { - String columnFamily = "columnFamily"; - ColumnQuery query = select().from(columnFamily).orderBy("name").desc().build(); - assertTrue(query.columns().isEmpty()); - assertFalse(query.condition().isPresent()); - assertEquals(columnFamily, query.name()); - assertThat(query.sorts()).contains(Sort.of("name", Direction.DESC, false)); - } - - @Test - void shouldReturnErrorSelectWhenOrderIsNull() { - Assertions.assertThrows(NullPointerException.class, () -> { - String columnFamily = "columnFamily"; - select().from(columnFamily).orderBy(null); - }); - } - - @Test - void shouldSelectLimit() { - String columnFamily = "columnFamily"; - ColumnQuery query = select().from(columnFamily).limit(10).build(); - assertTrue(query.columns().isEmpty()); - assertFalse(query.condition().isPresent()); - assertEquals(columnFamily, query.name()); - assertEquals(10L, query.limit()); - } - - @Test - void shouldReturnErrorWhenLimitIsNegative() { - String columnFamily = "columnFamily"; - Assertions.assertThrows(IllegalArgumentException.class, () -> builder().from(columnFamily).limit(-1)); - } - - @Test - void shouldSelectSkip() { - String columnFamily = "columnFamily"; - ColumnQuery query = select().from(columnFamily).skip(10).build(); - assertTrue(query.columns().isEmpty()); - assertFalse(query.condition().isPresent()); - assertEquals(columnFamily, query.name()); - assertEquals(10L, query.skip()); - } - - @Test - void shouldReturnErrorWhenSkipIsNegative() { - String columnFamily = "columnFamily"; - Assertions.assertThrows(IllegalArgumentException.class, () -> builder().from(columnFamily).skip(-1)); - } - - @Test - void shouldSelectWhereNameEq() { - String columnFamily = "columnFamily"; - String name = "Ada Lovelace"; - ColumnQuery query = select().from(columnFamily).where("name").eq(name).build(); - ColumnCondition condition = query.condition().get(); - - Column column = condition.column(); - - assertTrue(query.columns().isEmpty()); - assertEquals(columnFamily, query.name()); - assertEquals(Condition.EQUALS, condition.condition()); - assertEquals("name", column.name()); - assertEquals(name, column.get()); - - } - - @Test - void shouldSelectWhereNameLike() { - String columnFamily = "columnFamily"; - String name = "Ada Lovelace"; - ColumnQuery query = select().from(columnFamily).where("name").like(name).build(); - ColumnCondition condition = query.condition().get(); - - Column column = condition.column(); - - assertTrue(query.columns().isEmpty()); - assertEquals(columnFamily, query.name()); - assertEquals(Condition.LIKE, condition.condition()); - assertEquals("name", column.name()); - assertEquals(name, column.get()); - } - - @Test - void shouldSelectWhereNameGt() { - String columnFamily = "columnFamily"; - Number value = 10; - ColumnQuery query = select().from(columnFamily).where("name").gt(value).build(); - ColumnCondition condition = query.condition().get(); - - Column column = condition.column(); - - assertTrue(query.columns().isEmpty()); - assertEquals(columnFamily, query.name()); - assertEquals(Condition.GREATER_THAN, condition.condition()); - assertEquals("name", column.name()); - assertEquals(value, column.get()); - } - - @Test - void shouldSelectWhereNameGte() { - String columnFamily = "columnFamily"; - Number value = 10; - ColumnQuery query = select().from(columnFamily).where("name").gte(value).build(); - ColumnCondition condition = query.condition().get(); - - Column column = condition.column(); - - assertTrue(query.columns().isEmpty()); - assertEquals(columnFamily, query.name()); - assertEquals(Condition.GREATER_EQUALS_THAN, condition.condition()); - assertEquals("name", column.name()); - assertEquals(value, column.get()); - } - - @Test - void shouldSelectWhereNameLt() { - String columnFamily = "columnFamily"; - Number value = 10; - ColumnQuery query = select().from(columnFamily).where("name").lt(value).build(); - ColumnCondition condition = query.condition().get(); - - Column column = condition.column(); - - assertTrue(query.columns().isEmpty()); - assertEquals(columnFamily, query.name()); - assertEquals(Condition.LESSER_THAN, condition.condition()); - assertEquals("name", column.name()); - assertEquals(value, column.get()); - } - - @Test - void shouldSelectWhereNameLte() { - String columnFamily = "columnFamily"; - Number value = 10; - ColumnQuery query = select().from(columnFamily).where("name").lte(value).build(); - ColumnCondition condition = query.condition().get(); - - Column column = condition.column(); - - assertTrue(query.columns().isEmpty()); - assertEquals(columnFamily, query.name()); - assertEquals(Condition.LESSER_EQUALS_THAN, condition.condition()); - assertEquals("name", column.name()); - assertEquals(value, column.get()); - } - - @Test - void shouldSelectWhereNameBetween() { - String columnFamily = "columnFamily"; - Number valueA = 10; - Number valueB = 20; - ColumnQuery query = select().from(columnFamily).where("name").between(valueA, valueB).build(); - ColumnCondition condition = query.condition().get(); - - Column column = condition.column(); - - assertTrue(query.columns().isEmpty()); - assertEquals(columnFamily, query.name()); - assertEquals(Condition.BETWEEN, condition.condition()); - assertEquals("name", column.name()); - assertThat(column.get(new TypeReference>() { - })).contains(10, 20); - } - - @Test - void shouldSelectWhereNameNot() { - String columnFamily = "columnFamily"; - String name = "Ada Lovelace"; - ColumnQuery query = select().from(columnFamily).where("name").not().eq(name).build(); - ColumnCondition condition = query.condition().get(); - - Column column = condition.column(); - ColumnCondition negate = column.get(ColumnCondition.class); - assertTrue(query.columns().isEmpty()); - assertEquals(columnFamily, query.name()); - assertEquals(Condition.NOT, condition.condition()); - assertEquals(Condition.EQUALS, negate.condition()); - assertEquals("name", negate.column().name()); - assertEquals(name, negate.column().get()); - } - - - @Test - void shouldSelectWhereNameAnd() { - String columnFamily = "columnFamily"; - String name = "Ada Lovelace"; - ColumnQuery query = select().from(columnFamily).where("name").eq(name).and("age").gt(10).build(); - ColumnCondition condition = query.condition().get(); - - Column column = condition.column(); - List conditions = column.get(new TypeReference<>() { - }); - assertEquals(Condition.AND, condition.condition()); - org.assertj.core.api.Assertions.assertThat(conditions).contains(eq(Column.of("name", name)), - ColumnCondition.gt(Column.of("age", 10))); - } - - @Test - void shouldSelectWhereNameOr() { - String columnFamily = "columnFamily"; - String name = "Ada Lovelace"; - ColumnQuery query = select().from(columnFamily).where("name").eq(name).or("age").gt(10).build(); - ColumnCondition condition = query.condition().get(); - - Column column = condition.column(); - List conditions = column.get(new TypeReference<>() { - }); - assertEquals(Condition.OR, condition.condition()); - org.assertj.core.api.Assertions.assertThat(conditions).contains(eq(Column.of("name", name)), - ColumnCondition.gt(Column.of("age", 10))); - } - - @Test - void shouldSelectNegate() { - String columnFamily = "columnFamily"; - ColumnQuery query = select().from(columnFamily).where("city").not().eq("Assis") - .and("name").not().eq("Lucas").build(); - - ColumnCondition condition = query.condition().orElseThrow(RuntimeException::new); - assertEquals(columnFamily, query.name()); - Column column = condition.column(); - List conditions = column.get(new TypeReference<>() { - }); - - assertEquals(Condition.AND, condition.condition()); - org.assertj.core.api.Assertions.assertThat(conditions).contains(eq(Column.of("city", "Assis")).negate(), - eq(Column.of("name", "Lucas")).negate()); - - - } - - @Test - void shouldExecuteManager() { - ColumnManager manager = Mockito.mock(ColumnManager.class); - ArgumentCaptor queryCaptor = ArgumentCaptor.forClass(ColumnQuery.class); - String columnFamily = "columnFamily"; - Stream entities = select().from(columnFamily).getResult(manager); - entities.collect(Collectors.toList()); - Mockito.verify(manager).select(queryCaptor.capture()); - checkQuery(queryCaptor, columnFamily); - } - - @Test - void shouldExecuteSingleResultManager() { - ColumnManager manager = Mockito.mock(ColumnManager.class); - ArgumentCaptor queryCaptor = ArgumentCaptor.forClass(ColumnQuery.class); - String columnFamily = "columnFamily"; - Optional entities = select().from(columnFamily).getSingleResult(manager); - Mockito.verify(manager).singleResult(queryCaptor.capture()); - checkQuery(queryCaptor, columnFamily); - } - - private void checkQuery(ArgumentCaptor queryCaptor, String columnFamily) { - ColumnQuery query = queryCaptor.getValue(); - assertTrue(query.columns().isEmpty()); - assertFalse(query.condition().isPresent()); - assertEquals(columnFamily, query.name()); - } - -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/DeleteQueryParserTest.java b/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/DeleteQueryParserTest.java deleted file mode 100644 index 58afb284a..000000000 --- a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/DeleteQueryParserTest.java +++ /dev/null @@ -1,397 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - -import org.assertj.core.api.Assertions; -import org.eclipse.jnosql.communication.Condition; -import org.eclipse.jnosql.communication.QueryException; -import org.eclipse.jnosql.communication.TypeReference; -import org.eclipse.jnosql.communication.Value; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; -import org.mockito.ArgumentCaptor; -import org.mockito.Mockito; - -import java.util.Arrays; -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.eclipse.jnosql.communication.column.ColumnCondition.eq; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; - -class DeleteQueryParserTest { - - private final DeleteQueryParser parser = new DeleteQueryParser(); - - private final ColumnManager manager = Mockito.mock(ColumnManager.class); - - private final ColumnObserverParser observer = new ColumnObserverParser() { - }; - - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God"}) - void shouldReturnParserQuery(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnDeleteQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).delete(captor.capture()); - ColumnDeleteQuery columnQuery = captor.getValue(); - - assertTrue(columnQuery.columns().isEmpty()); - assertEquals("God", columnQuery.name()); - assertFalse(columnQuery.condition().isPresent()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete name, address from God"}) - void shouldReturnParserQuery1(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnDeleteQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).delete(captor.capture()); - ColumnDeleteQuery columnQuery = captor.getValue(); - - assertThat(columnQuery.columns()).contains("name", "address"); - assertEquals("God", columnQuery.name()); - assertFalse(columnQuery.condition().isPresent()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where stamina > 10.23"}) - void shouldReturnParserQuery11(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnDeleteQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).delete(captor.capture()); - ColumnDeleteQuery columnQuery = captor.getValue(); - - checkBaseQuery(columnQuery); - assertTrue(columnQuery.condition().isPresent()); - ColumnCondition condition = columnQuery.condition().get(); - - assertEquals(Condition.GREATER_THAN, condition.condition()); - assertEquals(Column.of("stamina", 10.23), condition.column()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where stamina >= -10.23"}) - void shouldReturnParserQuery12(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnDeleteQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).delete(captor.capture()); - ColumnDeleteQuery columnQuery = captor.getValue(); - - checkBaseQuery(columnQuery); - assertTrue(columnQuery.condition().isPresent()); - ColumnCondition condition = columnQuery.condition().get(); - - assertEquals(Condition.GREATER_EQUALS_THAN, condition.condition()); - assertEquals(Column.of("stamina", -10.23), condition.column()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where stamina <= -10.23"}) - void shouldReturnParserQuery13(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnDeleteQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).delete(captor.capture()); - ColumnDeleteQuery columnQuery = captor.getValue(); - - checkBaseQuery(columnQuery); - assertTrue(columnQuery.condition().isPresent()); - ColumnCondition condition = columnQuery.condition().get(); - - assertEquals(Condition.LESSER_EQUALS_THAN, condition.condition()); - assertEquals(Column.of("stamina", -10.23), condition.column()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where stamina < -10.23"}) - void shouldReturnParserQuery14(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnDeleteQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).delete(captor.capture()); - ColumnDeleteQuery columnQuery = captor.getValue(); - - checkBaseQuery(columnQuery); - assertTrue(columnQuery.condition().isPresent()); - ColumnCondition condition = columnQuery.condition().get(); - - assertEquals(Condition.LESSER_THAN, condition.condition()); - assertEquals(Column.of("stamina", -10.23), condition.column()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where age between 10 and 30"}) - void shouldReturnParserQuery15(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnDeleteQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).delete(captor.capture()); - ColumnDeleteQuery columnQuery = captor.getValue(); - - checkBaseQuery(columnQuery); - assertTrue(columnQuery.condition().isPresent()); - ColumnCondition condition = columnQuery.condition().get(); - - assertEquals(Condition.BETWEEN, condition.condition()); - assertEquals(Column.of("age", Arrays.asList(10L, 30L)), condition.column()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where name = \"diana\""}) - void shouldReturnParserQuery16(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnDeleteQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).delete(captor.capture()); - ColumnDeleteQuery columnQuery = captor.getValue(); - - checkBaseQuery(columnQuery); - assertTrue(columnQuery.condition().isPresent()); - ColumnCondition condition = columnQuery.condition().get(); - - assertEquals(Condition.EQUALS, condition.condition()); - assertEquals(Column.of("name", "diana"), condition.column()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where siblings = {\"apollo\": \"Brother\", \"Zeus\": \"Father\"}"}) - void shouldReturnParserQuery18(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnDeleteQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).delete(captor.capture()); - ColumnDeleteQuery columnQuery = captor.getValue(); - - checkBaseQuery(columnQuery); - assertTrue(columnQuery.condition().isPresent()); - ColumnCondition condition = columnQuery.condition().get(); - - assertEquals(Condition.EQUALS, condition.condition()); - Column column = condition.column(); - List columns = column.get(new TypeReference<>() { - }); - Assertions.assertThat(columns).contains(Column.of("apollo", "Brother"), - Column.of("Zeus", "Father")); - assertEquals("siblings", column.name()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where age = convert(12, java.lang.Integer)"}) - void shouldReturnParserQuery19(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnDeleteQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).delete(captor.capture()); - ColumnDeleteQuery columnQuery = captor.getValue(); - - checkBaseQuery(columnQuery); - assertTrue(columnQuery.condition().isPresent()); - ColumnCondition condition = columnQuery.condition().get(); - Column column = condition.column(); - assertEquals(Condition.EQUALS, condition.condition()); - assertEquals("age", column.name()); - assertEquals(Value.of(12), column.value()); - - - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where name in (\"Ada\", \"Apollo\")"}) - void shouldReturnParserQuery20(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnDeleteQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).delete(captor.capture()); - ColumnDeleteQuery columnQuery = captor.getValue(); - - checkBaseQuery(columnQuery); - assertTrue(columnQuery.condition().isPresent()); - ColumnCondition condition = columnQuery.condition().get(); - Column column = condition.column(); - assertEquals(Condition.IN, condition.condition()); - assertEquals("name", column.name()); - List values = column.get(new TypeReference<>() { - }); - assertThat(values).contains("Ada", "Apollo"); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where name like \"Ada\""}) - void shouldReturnParserQuery21(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnDeleteQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).delete(captor.capture()); - ColumnDeleteQuery columnQuery = captor.getValue(); - - checkBaseQuery(columnQuery); - assertTrue(columnQuery.condition().isPresent()); - ColumnCondition condition = columnQuery.condition().get(); - Column column = condition.column(); - assertEquals(Condition.LIKE, condition.condition()); - assertEquals("name", column.name()); - assertEquals("Ada", column.get()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where name not like \"Ada\""}) - void shouldReturnParserQuery22(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnDeleteQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).delete(captor.capture()); - ColumnDeleteQuery columnQuery = captor.getValue(); - - checkBaseQuery(columnQuery); - assertTrue(columnQuery.condition().isPresent()); - ColumnCondition condition = columnQuery.condition().get(); - Column column = condition.column(); - assertEquals(Condition.NOT, condition.condition()); - List conditions = column.get(new TypeReference<>() { - }); - ColumnCondition columnCondition = conditions.get(0); - assertEquals(Condition.LIKE, columnCondition.condition()); - assertEquals(Column.of("name", "Ada"), columnCondition.column()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where name = \"Ada\" and age = 20"}) - void shouldReturnParserQuery23(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnDeleteQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).delete(captor.capture()); - ColumnDeleteQuery columnQuery = captor.getValue(); - - checkBaseQuery(columnQuery); - assertTrue(columnQuery.condition().isPresent()); - ColumnCondition condition = columnQuery.condition().get(); - Column column = condition.column(); - assertEquals(Condition.AND, condition.condition()); - List conditions = column.get(new TypeReference<>() { - }); - Assertions.assertThat(conditions).contains(eq(Column.of("name", "Ada")), - eq(Column.of("age", 20L))); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where name = \"Ada\" or age = 20"}) - void shouldReturnParserQuery24(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnDeleteQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).delete(captor.capture()); - ColumnDeleteQuery columnQuery = captor.getValue(); - - checkBaseQuery(columnQuery); - assertTrue(columnQuery.condition().isPresent()); - ColumnCondition condition = columnQuery.condition().get(); - Column column = condition.column(); - assertEquals(Condition.OR, condition.condition()); - List conditions = column.get(new TypeReference<>() { - }); - Assertions.assertThat(conditions).contains(eq(Column.of("name", "Ada")), - eq(Column.of("age", 20L))); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where name = \"Ada\" and age = 20 or" + - " siblings = {\"apollo\": \"Brother\", \"Zeus\": \"Father\"}"}) - void shouldReturnParserQuery25(String query) { - - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnDeleteQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).delete(captor.capture()); - ColumnDeleteQuery columnQuery = captor.getValue(); - - checkBaseQuery(columnQuery); - assertTrue(columnQuery.condition().isPresent()); - ColumnCondition condition = columnQuery.condition().get(); - Column column = condition.column(); - assertEquals(Condition.AND, condition.condition()); - List conditions = column.get(new TypeReference<>() { - }); - assertEquals(Condition.EQUALS, conditions.get(0).condition()); - assertEquals(Condition.EQUALS, conditions.get(1).condition()); - assertEquals(Condition.OR, conditions.get(2).condition()); - - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where name = \"Ada\" and age = 20 or" + - " siblings = {\"apollo\": \"Brother\", \"Zeus\": \"Father\"} and birthday =" + - " convert(\"2007-12-03\", java.time.LocalDate)"}) - void shouldReturnParserQuery26(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnDeleteQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).delete(captor.capture()); - ColumnDeleteQuery columnQuery = captor.getValue(); - - checkBaseQuery(columnQuery); - assertTrue(columnQuery.condition().isPresent()); - ColumnCondition condition = columnQuery.condition().get(); - Column column = condition.column(); - assertEquals(Condition.AND, condition.condition()); - List conditions = column.get(new TypeReference<>() { - }); - assertEquals(Condition.EQUALS, conditions.get(0).condition()); - assertEquals(Condition.EQUALS, conditions.get(1).condition()); - assertEquals(Condition.OR, conditions.get(2).condition()); - assertEquals(Condition.EQUALS, conditions.get(3).condition()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where age = @age"}) - void shouldReturnErrorWhenNeedPrepareStatement(String query) { - - assertThrows(QueryException.class, () -> parser.query(query, manager, observer)); - - - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where age = @age"}) - void shouldReturnErrorWhenIsQueryWithParam(String query) { - - assertThrows(QueryException.class, () -> parser.query(query, manager, observer)); - - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where age = @age"}) - void shouldReturnErrorWhenDontBindParameters(String query) { - - ColumnPreparedStatement prepare = parser.prepare(query, manager, observer); - assertThrows(QueryException.class, prepare::result); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where age = @age"}) - void shouldExecutePrepareStatement(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnDeleteQuery.class); - - ColumnPreparedStatement prepare = parser.prepare(query, manager, observer); - prepare.bind("age", 12); - prepare.result(); - Mockito.verify(manager).delete(captor.capture()); - ColumnDeleteQuery columnQuery = captor.getValue(); - ColumnCondition columnCondition = columnQuery.condition().get(); - Column column = columnCondition.column(); - assertEquals(Condition.EQUALS, columnCondition.condition()); - assertEquals("age", column.name()); - assertEquals(12, column.get()); - } - - private void checkBaseQuery(ColumnDeleteQuery columnQuery) { - assertTrue(columnQuery.columns().isEmpty()); - assertEquals("God", columnQuery.name()); - } -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/InsertQueryParserTest.java b/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/InsertQueryParserTest.java deleted file mode 100644 index 522dcfe21..000000000 --- a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/InsertQueryParserTest.java +++ /dev/null @@ -1,259 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - -import org.assertj.core.api.Assertions; -import org.eclipse.jnosql.communication.QueryException; -import org.eclipse.jnosql.communication.TypeReference; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; -import org.mockito.ArgumentCaptor; -import org.mockito.Mockito; - -import java.math.BigDecimal; -import java.time.Duration; -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - -class InsertQueryParserTest { - - private final InsertQueryParser parser = new InsertQueryParser(); - - private final ColumnManager manager = Mockito.mock(ColumnManager.class); - - private final ColumnObserverParser observer = new ColumnObserverParser() { - }; - - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"insert God (name = \"Diana\")"}) - void shouldReturnParserQuery(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnEntity.class); - parser.query(query, manager, observer); - Mockito.verify(manager).insert(captor.capture()); - ColumnEntity entity = captor.getValue(); - - - assertEquals("God", entity.name()); - assertEquals(Column.of("name", "Diana"), entity.find("name").get()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"insert God (age = 30, name = \"Artemis\")"}) - void shouldReturnParserQuery1(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnEntity.class); - parser.query(query, manager, observer); - Mockito.verify(manager).insert(captor.capture()); - ColumnEntity entity = captor.getValue(); - - assertEquals("God", entity.name()); - assertEquals(Column.of("name", "Artemis"), entity.find("name").get()); - assertEquals(Column.of("age", 30L), entity.find("age").get()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"insert God (name = \"Diana\") 10 day"}) - void shouldReturnParserQuery2(String query) { - - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnEntity.class); - ArgumentCaptor durationCaptor = ArgumentCaptor.forClass(Duration.class); - - parser.query(query, manager, observer); - Mockito.verify(manager).insert(captor.capture(), durationCaptor.capture()); - ColumnEntity entity = captor.getValue(); - Duration duration = durationCaptor.getValue(); - - assertEquals("God", entity.name()); - assertEquals(Column.of("name", "Diana"), entity.find("name").get()); - assertEquals(Duration.ofDays(10L), duration); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"insert God (name = \"Diana\") 10 hour"}) - void shouldReturnParserQuery3(String query) { - - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnEntity.class); - ArgumentCaptor durationCaptor = ArgumentCaptor.forClass(Duration.class); - - parser.query(query, manager, observer); - Mockito.verify(manager).insert(captor.capture(), durationCaptor.capture()); - ColumnEntity entity = captor.getValue(); - Duration duration = durationCaptor.getValue(); - - assertEquals("God", entity.name()); - assertEquals(Column.of("name", "Diana"), entity.find("name").get()); - assertEquals(Duration.ofHours(10L), duration); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"insert God (name = \"Diana\") 10 minute"}) - void shouldReturnParserQuery4(String query) { - - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnEntity.class); - ArgumentCaptor durationCaptor = ArgumentCaptor.forClass(Duration.class); - - parser.query(query, manager, observer); - Mockito.verify(manager).insert(captor.capture(), durationCaptor.capture()); - ColumnEntity entity = captor.getValue(); - Duration duration = durationCaptor.getValue(); - - assertEquals("God", entity.name()); - assertEquals(Column.of("name", "Diana"), entity.find("name").get()); - assertEquals(Duration.ofMinutes(10L), duration); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"insert God (name = \"Diana\") 10 second"}) - void shouldReturnParserQuery5(String query) { - - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnEntity.class); - ArgumentCaptor durationCaptor = ArgumentCaptor.forClass(Duration.class); - - parser.query(query, manager, observer); - Mockito.verify(manager).insert(captor.capture(), durationCaptor.capture()); - ColumnEntity entity = captor.getValue(); - Duration duration = durationCaptor.getValue(); - - assertEquals("God", entity.name()); - assertEquals(Column.of("name", "Diana"), entity.find("name").get()); - assertEquals(Duration.ofSeconds(10L), duration); - } - - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"insert God (name = \"Diana\") 10 millisecond"}) - void shouldReturnParserQuery6(String query) { - - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnEntity.class); - ArgumentCaptor durationCaptor = ArgumentCaptor.forClass(Duration.class); - - parser.query(query, manager, observer); - Mockito.verify(manager).insert(captor.capture(), durationCaptor.capture()); - ColumnEntity entity = captor.getValue(); - Duration duration = durationCaptor.getValue(); - - assertEquals("God", entity.name()); - assertEquals(Column.of("name", "Diana"), entity.find("name").get()); - assertEquals(Duration.ofMillis(10L), duration); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"insert God (name = \"Diana\") 10 nanosecond"}) - void shouldReturnParserQuery7(String query) { - - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnEntity.class); - ArgumentCaptor durationCaptor = ArgumentCaptor.forClass(Duration.class); - - parser.query(query, manager, observer); - Mockito.verify(manager).insert(captor.capture(), durationCaptor.capture()); - ColumnEntity entity = captor.getValue(); - Duration duration = durationCaptor.getValue(); - - assertEquals("God", entity.name()); - assertEquals(Column.of("name", "Diana"), entity.find("name").get()); - assertEquals(Duration.ofNanos(10L), duration); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"insert Person {\"name\":\"Ada Lovelace\"}"}) - void shouldReturnParserQuery8(String query) { - - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnEntity.class); - - parser.query(query, manager, observer); - Mockito.verify(manager).insert(captor.capture()); - ColumnEntity entity = captor.getValue(); - - assertEquals("Person", entity.name()); - assertEquals(Column.of("name", "Ada Lovelace"), entity.find("name").get()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"insert Person {\"name\":\"Ada Lovelace\"} 10 nanosecond"}) - void shouldReturnParserQuery9(String query) { - ArgumentCaptor durationCaptor = ArgumentCaptor.forClass(Duration.class); - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnEntity.class); - - parser.query(query, manager, observer); - Mockito.verify(manager).insert(captor.capture(), durationCaptor.capture()); - ColumnEntity entity = captor.getValue(); - Duration duration = durationCaptor.getValue(); - - assertEquals("Person", entity.name()); - assertEquals(Column.of("name", "Ada Lovelace"), entity.find("name").get()); - assertEquals(Duration.ofNanos(10L), duration); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"insert Person {\"name\": \"Ada Lovelace\", \"age\": 12, \"sibling\":" + - " [\"Ana\" ,\"Maria\"]," + - " \"address\":{\"country\": \"United Kingdom\", \"city\": \"London\"}}"}) - void shouldReturnParserQuery10(String query) { - - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnEntity.class); - - parser.query(query, manager, observer); - Mockito.verify(manager).insert(captor.capture()); - ColumnEntity entity = captor.getValue(); - List siblings = entity.find("sibling").get().get(new TypeReference<>() { - }); - List address = entity.find("address").get().get(new TypeReference<>() { - }); - assertEquals("Person", entity.name()); - assertEquals(Column.of("name", "Ada Lovelace"), entity.find("name").get()); - assertEquals(Column.of("age", BigDecimal.valueOf(12)), entity.find("age").get()); - assertThat(siblings).contains("Ana", "Maria"); - Assertions.assertThat(address).contains( - Column.of("country", "United Kingdom"), - Column.of("city", "London")); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"insert God (name = @name)"}) - void shouldReturnErrorWhenShouldUsePrepareStatement(String query) { - - assertThrows(QueryException.class, () -> parser.query(query, manager, observer)); - } - - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"insert God (name = @name)"}) - void shouldReturnErrorWhenDoesNotBindBeforeExecuteQuery(String query) { - - ColumnPreparedStatement prepare = parser.prepare(query, manager, observer); - assertThrows(QueryException.class, prepare::result); - } - - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"insert God (name = @name)"}) - void shouldExecutePrepareStatement(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnEntity.class); - ColumnPreparedStatement prepare = parser.prepare(query, manager, observer); - prepare.bind("name", "Diana"); - prepare.result(); - Mockito.verify(manager).insert(captor.capture()); - ColumnEntity entity = captor.getValue(); - assertEquals("God", entity.name()); - assertEquals(Column.of("name", "Diana"), entity.find("name").get()); - - } - -} diff --git a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/SelectQueryParserTest.java b/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/SelectQueryParserTest.java deleted file mode 100644 index 6ecb3c552..000000000 --- a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/SelectQueryParserTest.java +++ /dev/null @@ -1,508 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - -import org.assertj.core.api.Assertions; -import org.eclipse.jnosql.communication.Condition; -import org.eclipse.jnosql.communication.QueryException; -import jakarta.data.Sort; -import jakarta.data.Direction; -import org.eclipse.jnosql.communication.TypeReference; -import org.eclipse.jnosql.communication.Value; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; -import org.mockito.ArgumentCaptor; -import org.mockito.Mockito; - -import java.util.Arrays; -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.eclipse.jnosql.communication.column.ColumnCondition.eq; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; - -class SelectQueryParserTest { - - private final SelectQueryParser parser = new SelectQueryParser(); - - private final ColumnManager manager = Mockito.mock(ColumnManager.class); - - private final ColumnObserverParser observer = new ColumnObserverParser() { - }; - - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select name, address from God"}) - void shouldReturnParserQuery1(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).select(captor.capture()); - ColumnQuery columnQuery = captor.getValue(); - - assertThat(columnQuery.columns()).contains("name", "address"); - assertTrue(columnQuery.sorts().isEmpty()); - assertEquals(0L, columnQuery.limit()); - assertEquals(0L, columnQuery.skip()); - assertEquals("God", columnQuery.name()); - assertFalse(columnQuery.condition().isPresent()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God order by name"}) - void shouldReturnParserQuery3(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).select(captor.capture()); - ColumnQuery columnQuery = captor.getValue(); - - assertTrue(columnQuery.columns().isEmpty()); - assertThat(columnQuery.sorts()).contains(Sort.of("name", Direction.ASC, false)); - assertEquals(0L, columnQuery.limit()); - assertEquals(0L, columnQuery.skip()); - assertEquals("God", columnQuery.name()); - assertFalse(columnQuery.condition().isPresent()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God order by name asc"}) - void shouldReturnParserQuery4(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).select(captor.capture()); - ColumnQuery columnQuery = captor.getValue(); - - assertTrue(columnQuery.columns().isEmpty()); - assertThat(columnQuery.sorts()).contains(Sort.of("name", Direction.ASC, false)); - assertEquals(0L, columnQuery.limit()); - assertEquals(0L, columnQuery.skip()); - assertEquals("God", columnQuery.name()); - assertFalse(columnQuery.condition().isPresent()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God order by name desc"}) - void shouldReturnParserQuery5(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).select(captor.capture()); - ColumnQuery columnQuery = captor.getValue(); - - assertTrue(columnQuery.columns().isEmpty()); - assertThat(columnQuery.sorts()).contains(Sort.of("name", Direction.DESC, false)); - assertEquals(0L, columnQuery.limit()); - assertEquals(0L, columnQuery.skip()); - assertEquals("God", columnQuery.name()); - assertFalse(columnQuery.condition().isPresent()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God order by name desc age asc"}) - void shouldReturnParserQuery6(String query) { - - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).select(captor.capture()); - ColumnQuery columnQuery = captor.getValue(); - - assertTrue(columnQuery.columns().isEmpty()); - assertThat(columnQuery.sorts()).contains(Sort.of("name", Direction.DESC, false), Sort.of("age", - Direction.ASC, false)); - assertEquals(0L, columnQuery.limit()); - assertEquals(0L, columnQuery.skip()); - assertEquals("God", columnQuery.name()); - assertFalse(columnQuery.condition().isPresent()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God skip 12"}) - void shouldReturnParserQuery7(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).select(captor.capture()); - ColumnQuery columnQuery = captor.getValue(); - - checkBaseQuery(columnQuery, 0L, 12L); - assertFalse(columnQuery.condition().isPresent()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God limit 12"}) - void shouldReturnParserQuery8(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).select(captor.capture()); - ColumnQuery columnQuery = captor.getValue(); - - checkBaseQuery(columnQuery, 12L, 0L); - assertFalse(columnQuery.condition().isPresent()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God skip 10 limit 12"}) - void shouldReturnParserQuery9(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).select(captor.capture()); - ColumnQuery columnQuery = captor.getValue(); - - assertTrue(columnQuery.columns().isEmpty()); - assertTrue(columnQuery.sorts().isEmpty()); - assertEquals(12L, columnQuery.limit()); - assertEquals(10L, columnQuery.skip()); - assertEquals("God", columnQuery.name()); - assertFalse(columnQuery.condition().isPresent()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where age = 10"}) - void shouldReturnParserQuery10(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).select(captor.capture()); - ColumnQuery columnQuery = captor.getValue(); - - checkBaseQuery(columnQuery, 0L, 0L); - assertTrue(columnQuery.condition().isPresent()); - ColumnCondition condition = columnQuery.condition().get(); - - assertEquals(Condition.EQUALS, condition.condition()); - assertEquals(Column.of("age", 10L), condition.column()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where stamina > 10.23"}) - void shouldReturnParserQuery11(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).select(captor.capture()); - ColumnQuery columnQuery = captor.getValue(); - - checkBaseQuery(columnQuery, 0L, 0L); - assertTrue(columnQuery.condition().isPresent()); - ColumnCondition condition = columnQuery.condition().get(); - - assertEquals(Condition.GREATER_THAN, condition.condition()); - assertEquals(Column.of("stamina", 10.23), condition.column()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where stamina >= -10.23"}) - void shouldReturnParserQuery12(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnQuery.class); - - parser.query(query, manager, observer); - Mockito.verify(manager).select(captor.capture()); - ColumnQuery columnQuery = captor.getValue(); - - checkBaseQuery(columnQuery, 0L, 0L); - assertTrue(columnQuery.condition().isPresent()); - ColumnCondition condition = columnQuery.condition().get(); - - assertEquals(Condition.GREATER_EQUALS_THAN, condition.condition()); - assertEquals(Column.of("stamina", -10.23), condition.column()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where stamina <= -10.23"}) - void shouldReturnParserQuery13(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).select(captor.capture()); - ColumnQuery columnQuery = captor.getValue(); - - checkBaseQuery(columnQuery, 0L, 0L); - assertTrue(columnQuery.condition().isPresent()); - ColumnCondition condition = columnQuery.condition().get(); - - assertEquals(Condition.LESSER_EQUALS_THAN, condition.condition()); - assertEquals(Column.of("stamina", -10.23), condition.column()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where stamina < -10.23"}) - void shouldReturnParserQuery14(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).select(captor.capture()); - ColumnQuery columnQuery = captor.getValue(); - - checkBaseQuery(columnQuery, 0L, 0L); - assertTrue(columnQuery.condition().isPresent()); - ColumnCondition condition = columnQuery.condition().get(); - - assertEquals(Condition.LESSER_THAN, condition.condition()); - assertEquals(Column.of("stamina", -10.23), condition.column()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where age between 10 and 30"}) - void shouldReturnParserQuery15(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).select(captor.capture()); - ColumnQuery columnQuery = captor.getValue(); - - checkBaseQuery(columnQuery, 0L, 0L); - assertTrue(columnQuery.condition().isPresent()); - ColumnCondition condition = columnQuery.condition().get(); - - assertEquals(Condition.BETWEEN, condition.condition()); - assertEquals(Column.of("age", Arrays.asList(10L, 30L)), condition.column()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where name = \"diana\""}) - void shouldReturnParserQuery16(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).select(captor.capture()); - ColumnQuery columnQuery = captor.getValue(); - - checkBaseQuery(columnQuery, 0L, 0L); - assertTrue(columnQuery.condition().isPresent()); - ColumnCondition condition = columnQuery.condition().get(); - - assertEquals(Condition.EQUALS, condition.condition()); - assertEquals(Column.of("name", "diana"), condition.column()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where siblings = {\"apollo\": \"Brother\", \"Zeus\": \"Father\"}"}) - void shouldReturnParserQuery18(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).select(captor.capture()); - ColumnQuery columnQuery = captor.getValue(); - - checkBaseQuery(columnQuery, 0L, 0L); - assertTrue(columnQuery.condition().isPresent()); - ColumnCondition condition = columnQuery.condition().get(); - - assertEquals(Condition.EQUALS, condition.condition()); - Column column = condition.column(); - List columns = column.get(new TypeReference<>() { - }); - Assertions.assertThat(columns).contains(Column.of("apollo", "Brother"), - Column.of("Zeus", "Father")); - assertEquals("siblings", column.name()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where age = convert(12, java.lang.Integer)"}) - void shouldReturnParserQuery19(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).select(captor.capture()); - ColumnQuery columnQuery = captor.getValue(); - - checkBaseQuery(columnQuery, 0L, 0L); - assertTrue(columnQuery.condition().isPresent()); - ColumnCondition condition = columnQuery.condition().get(); - Column column = condition.column(); - assertEquals(Condition.EQUALS, condition.condition()); - assertEquals("age", column.name()); - assertEquals(Value.of(12), column.value()); - - - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where name in (\"Ada\", \"Apollo\")"}) - void shouldReturnParserQuery20(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).select(captor.capture()); - ColumnQuery columnQuery = captor.getValue(); - - checkBaseQuery(columnQuery, 0L, 0L); - assertTrue(columnQuery.condition().isPresent()); - ColumnCondition condition = columnQuery.condition().get(); - Column column = condition.column(); - assertEquals(Condition.IN, condition.condition()); - assertEquals("name", column.name()); - List values = column.get(new TypeReference<>() { - }); - assertThat(values).contains("Ada", "Apollo"); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where name like \"Ada\""}) - void shouldReturnParserQuery21(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).select(captor.capture()); - ColumnQuery columnQuery = captor.getValue(); - - checkBaseQuery(columnQuery, 0L, 0L); - assertTrue(columnQuery.condition().isPresent()); - ColumnCondition condition = columnQuery.condition().get(); - Column column = condition.column(); - assertEquals(Condition.LIKE, condition.condition()); - assertEquals("name", column.name()); - assertEquals("Ada", column.get()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where name not like \"Ada\""}) - void shouldReturnParserQuery22(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).select(captor.capture()); - ColumnQuery columnQuery = captor.getValue(); - - checkBaseQuery(columnQuery, 0L, 0L); - assertTrue(columnQuery.condition().isPresent()); - ColumnCondition condition = columnQuery.condition().get(); - Column column = condition.column(); - assertEquals(Condition.NOT, condition.condition()); - List conditions = column.get(new TypeReference<>() { - }); - ColumnCondition columnCondition = conditions.get(0); - assertEquals(Condition.LIKE, columnCondition.condition()); - assertEquals(Column.of("name", "Ada"), columnCondition.column()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where name = \"Ada\" and age = 20"}) - void shouldReturnParserQuery23(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).select(captor.capture()); - ColumnQuery columnQuery = captor.getValue(); - - checkBaseQuery(columnQuery, 0L, 0L); - assertTrue(columnQuery.condition().isPresent()); - ColumnCondition condition = columnQuery.condition().get(); - Column column = condition.column(); - assertEquals(Condition.AND, condition.condition()); - List conditions = column.get(new TypeReference<>() { - }); - Assertions.assertThat(conditions).contains(eq(Column.of("name", "Ada")), - eq(Column.of("age", 20L))); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where name = \"Ada\" or age = 20"}) - void shouldReturnParserQuery24(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).select(captor.capture()); - ColumnQuery columnQuery = captor.getValue(); - - checkBaseQuery(columnQuery, 0L, 0L); - assertTrue(columnQuery.condition().isPresent()); - ColumnCondition condition = columnQuery.condition().get(); - Column column = condition.column(); - assertEquals(Condition.OR, condition.condition()); - List conditions = column.get(new TypeReference<>() { - }); - Assertions.assertThat(conditions).contains(eq(Column.of("name", "Ada")), - eq(Column.of("age", 20L))); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where name = \"Ada\" and age = 20 or" + - " siblings = {\"apollo\": \"Brother\", \"Zeus\": \"Father\"}"}) - void shouldReturnParserQuery25(String query) { - - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).select(captor.capture()); - ColumnQuery columnQuery = captor.getValue(); - - checkBaseQuery(columnQuery, 0L, 0L); - assertTrue(columnQuery.condition().isPresent()); - ColumnCondition condition = columnQuery.condition().get(); - Column column = condition.column(); - assertEquals(Condition.AND, condition.condition()); - List conditions = column.get(new TypeReference<>() { - }); - assertEquals(Condition.EQUALS, conditions.get(0).condition()); - assertEquals(Condition.EQUALS, conditions.get(1).condition()); - assertEquals(Condition.OR, conditions.get(2).condition()); - - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where name = \"Ada\" and age = 20 or" + - " siblings = {\"apollo\": \"Brother\", \"Zeus\": \"Father\"} and birthday =" + - " convert(\"2007-12-03\", java.time.LocalDate)"}) - void shouldReturnParserQuery26(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnQuery.class); - parser.query(query, manager, observer); - Mockito.verify(manager).select(captor.capture()); - ColumnQuery columnQuery = captor.getValue(); - - checkBaseQuery(columnQuery, 0L, 0L); - assertTrue(columnQuery.condition().isPresent()); - ColumnCondition condition = columnQuery.condition().get(); - Column column = condition.column(); - assertEquals(Condition.AND, condition.condition()); - List conditions = column.get(new TypeReference<>() { - }); - assertEquals(Condition.EQUALS, conditions.get(0).condition()); - assertEquals(Condition.EQUALS, conditions.get(1).condition()); - assertEquals(Condition.OR, conditions.get(2).condition()); - assertEquals(Condition.EQUALS, conditions.get(3).condition()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where age = @age"}) - void shouldReturnErrorWhenIsQueryWithParam(String query) { - - assertThrows(QueryException.class, () -> parser.query(query, manager, observer)); - - - } - - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where age = @age"}) - void shouldReturnErrorWhenDontBindParameters(String query) { - - ColumnPreparedStatement prepare = parser.prepare(query, manager, observer); - assertThrows(QueryException.class, prepare::result); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where age = @age"}) - void shouldExecutePrepareStatement(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnQuery.class); - - ColumnPreparedStatement prepare = parser.prepare(query, manager, observer); - prepare.bind("age", 12); - prepare.result(); - Mockito.verify(manager).select(captor.capture()); - ColumnQuery columnQuery = captor.getValue(); - ColumnCondition columnCondition = columnQuery.condition().get(); - Column column = columnCondition.column(); - assertEquals(Condition.EQUALS, columnCondition.condition()); - assertEquals("age", column.name()); - assertEquals(12, column.get()); - } - - private void checkBaseQuery(ColumnQuery columnQuery, long limit, long skip) { - assertTrue(columnQuery.columns().isEmpty()); - assertTrue(columnQuery.sorts().isEmpty()); - assertEquals(limit, columnQuery.limit()); - assertEquals(skip, columnQuery.skip()); - assertEquals("God", columnQuery.name()); - } -} diff --git a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/UpdateQueryParserTest.java b/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/UpdateQueryParserTest.java deleted file mode 100644 index b5b51233e..000000000 --- a/jnosql-communication/jnosql-communication-column/src/test/java/org/eclipse/jnosql/communication/column/UpdateQueryParserTest.java +++ /dev/null @@ -1,135 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.column; - -import org.assertj.core.api.Assertions; -import org.eclipse.jnosql.communication.QueryException; -import org.eclipse.jnosql.communication.TypeReference; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; -import org.mockito.ArgumentCaptor; -import org.mockito.Mockito; - -import java.math.BigDecimal; -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - -class UpdateQueryParserTest { - - private final UpdateQueryParser parser = new UpdateQueryParser(); - - private final ColumnManager manager = Mockito.mock(ColumnManager.class); - - private final ColumnObserverParser observer = new ColumnObserverParser() { - }; - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"update God (name = \"Diana\")"}) - void shouldReturnParserQuery(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnEntity.class); - parser.query(query, manager, observer); - Mockito.verify(manager).update(captor.capture()); - ColumnEntity entity = captor.getValue(); - - - assertEquals("God", entity.name()); - assertEquals(Column.of("name", "Diana"), entity.find("name").get()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"update God (age = 30, name = \"Artemis\")"}) - void shouldReturnParserQuery1(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnEntity.class); - parser.query(query, manager, observer); - Mockito.verify(manager).update(captor.capture()); - ColumnEntity entity = captor.getValue(); - - assertEquals("God", entity.name()); - assertEquals(Column.of("name", "Artemis"), entity.find("name").get()); - assertEquals(Column.of("age", 30L), entity.find("age").get()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"update God (name = @name)"}) - void shouldReturnParserQuery2(String query) { - - assertThrows(QueryException.class, () -> parser.query(query, manager, observer)); - } - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"update Person {\"name\":\"Ada Lovelace\"}"}) - void shouldReturnParserQuery3(String query) { - - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnEntity.class); - - parser.query(query, manager, observer); - Mockito.verify(manager).update(captor.capture()); - ColumnEntity entity = captor.getValue(); - - assertEquals("Person", entity.name()); - assertEquals(Column.of("name", "Ada Lovelace"), entity.find("name").get()); - } - - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"update Person {\"name\": \"Ada Lovelace\", \"age\": 12, \"sibling\":" + - " [\"Ana\" ,\"Maria\"]," + - " \"address\":{\"country\": \"United Kingdom\", \"city\": \"London\"}}"}) - void shouldReturnParserQuery4(String query) { - - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnEntity.class); - - parser.query(query, manager, observer); - Mockito.verify(manager).update(captor.capture()); - ColumnEntity entity = captor.getValue(); - List siblings = entity.find("sibling").get().get(new TypeReference<>() { - }); - List address = entity.find("address").get().get(new TypeReference<>() { - }); - assertEquals("Person", entity.name()); - assertEquals(Column.of("name", "Ada Lovelace"), entity.find("name").get()); - assertEquals(Column.of("age", BigDecimal.valueOf(12)), entity.find("age").get()); - assertThat(siblings).contains("Ana", "Maria"); - Assertions.assertThat(address).contains( - Column.of("country", "United Kingdom"), - Column.of("city", "London")); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"update God (name = @name)"}) - void shouldReturnErrorWhenDoesNotBindBeforeExecuteQuery(String query) { - - ColumnPreparedStatement prepare = parser.prepare(query, manager, observer); - assertThrows(QueryException.class, prepare::result); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"update God (name = @name)"}) - void shouldExecutePrepareStatement(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(ColumnEntity.class); - ColumnPreparedStatement prepare = parser.prepare(query, manager, observer); - prepare.bind("name", "Diana"); - prepare.result(); - Mockito.verify(manager).update(captor.capture()); - ColumnEntity entity = captor.getValue(); - assertEquals("God", entity.name()); - assertEquals(Column.of("name", "Diana"), entity.find("name").get()); - - } -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-document/.gitignore b/jnosql-communication/jnosql-communication-document/.gitignore deleted file mode 100644 index b43ed9333..000000000 --- a/jnosql-communication/jnosql-communication-document/.gitignore +++ /dev/null @@ -1,16 +0,0 @@ -target/ -pom.xml.tag -pom.xml.releaseBackup -pom.xml.versionsBackup -pom.xml.next -test-output/ -/doc -*.iml -*.idea -*.log -.classpath --project -/.resourceCache -/.project -/.idea -.settings/ diff --git a/jnosql-communication/jnosql-communication-document/pom.xml b/jnosql-communication/jnosql-communication-document/pom.xml deleted file mode 100644 index a624b1d12..000000000 --- a/jnosql-communication/jnosql-communication-document/pom.xml +++ /dev/null @@ -1,44 +0,0 @@ - - - - - 4.0.0 - - - org.eclipse.jnosql.communication - jnosql-communication - 1.1.1-SNAPSHOT - - - jnosql-communication-document - jar - - - - ${project.groupId} - jnosql-communication-core - ${project.version} - - - ${project.groupId} - jnosql-communication-query - ${project.version} - - - diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/module-info.java b/jnosql-communication/jnosql-communication-document/src/main/java/module-info.java deleted file mode 100644 index 2c83bf0e1..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/module-info.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * You may elect to redistribute this code under either of these licenses. - * Contributors: - * Otavio Santana - */ -module org.eclipse.jnosql.communication.document { - requires org.eclipse.jnosql.communication.core; - requires org.eclipse.jnosql.communication.query; - requires jakarta.json.bind; - requires jakarta.json; - requires jakarta.data; - exports org.eclipse.jnosql.communication.document; - opens org.eclipse.jnosql.communication.document; - uses org.eclipse.jnosql.communication.document.DocumentConfiguration; -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/BaseQueryBuilder.java b/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/BaseQueryBuilder.java deleted file mode 100644 index 627450a9e..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/BaseQueryBuilder.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - - -import static java.util.Arrays.asList; -import static java.util.Objects.nonNull; -import static java.util.Objects.requireNonNull; - -abstract class BaseQueryBuilder { - - - protected String name; - - protected boolean negate; - - protected boolean and; - - protected DocumentCondition condition; - - protected void eqImpl(T value) { - requireNonNull(value, "value is required"); - DocumentCondition newCondition = DocumentCondition.eq(Document.of(name, value)); - appendCondition(newCondition); - } - - protected void gtImpl(T value) { - requireNonNull(value, "value is required"); - DocumentCondition newCondition = DocumentCondition.gt(Document.of(name, value)); - appendCondition(newCondition); - } - - protected void likeImpl(String value) { - requireNonNull(value, "value is required"); - DocumentCondition newCondition = DocumentCondition.like(Document.of(name, value)); - appendCondition(newCondition); - } - - protected void ltImpl(T value) { - requireNonNull(value, "value is required"); - DocumentCondition newCondition = DocumentCondition.lt(Document.of(name, value)); - appendCondition(newCondition); - } - - protected void lteImpl(T value) { - requireNonNull(value, "value is required"); - DocumentCondition newCondition = DocumentCondition.lte(Document.of(name, value)); - appendCondition(newCondition); - } - - protected void gteImpl(T value) { - requireNonNull(value, "value is required"); - DocumentCondition newCondition = DocumentCondition.gte(Document.of(name, value)); - appendCondition(newCondition); - } - - protected void betweenImpl(T valueA, T valueB) { - requireNonNull(valueA, "valueA is required"); - requireNonNull(valueB, "valueB is required"); - DocumentCondition newCondition = DocumentCondition.between(Document.of(name, asList(valueA, valueB))); - appendCondition(newCondition); - } - - protected void inImpl(Iterable values) { - requireNonNull(values, "values is required"); - DocumentCondition newCondition = DocumentCondition.in(Document.of(name, values)); - appendCondition(newCondition); - } - - - protected void appendCondition(DocumentCondition newCondition) { - DocumentCondition documentCondition = getDocumentCondition(newCondition); - - if (nonNull(condition)) { - if (and) { - this.condition = condition.and(documentCondition); - } else { - this.condition = condition.or(documentCondition); - } - } else { - this.condition = documentCondition; - } - this.negate = false; - this.name = null; - } - - private DocumentCondition getDocumentCondition(DocumentCondition newCondition) { - if (negate) { - return newCondition.negate(); - } else { - return newCondition; - } - } - -} diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/ConditionQueryParser.java b/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/ConditionQueryParser.java deleted file mode 100644 index 438cc1b47..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/ConditionQueryParser.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - - -import org.eclipse.jnosql.communication.Params; -import org.eclipse.jnosql.communication.QueryException; -import org.eclipse.jnosql.communication.query.JSONQueryValue; - -import java.util.List; - - -/** - * A template class to Update and Insert query parser to extract the condition - */ -abstract class ConditionQueryParser { - - protected DocumentEntity getEntity(ConditionQuerySupplier query, String collection, Params params, - DocumentObserverParser observer) { - DocumentEntity entity = DocumentEntity.of(collection); - - if (query.useJSONCondition()) { - JSONQueryValue jsonValue = query.value().orElseThrow(() -> new QueryException("It is an invalid state of" + - " either Update or Insert.")); - List documents = JsonObjects.getDocuments(jsonValue.get()); - entity.addAll(documents); - return entity; - } - - query.conditions() - .stream() - .map(c -> Conditions.getCondition(c, params, observer, collection)) - .map(DocumentCondition::document) - .forEach(entity::add); - return entity; - } -} diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/ConditionQuerySupplier.java b/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/ConditionQuerySupplier.java deleted file mode 100644 index 868dd3896..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/ConditionQuerySupplier.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - - -import org.eclipse.jnosql.communication.query.JSONQueryValue; -import org.eclipse.jnosql.communication.query.QueryCondition; - -import java.util.List; -import java.util.Optional; - -/** - * A base supplier to {@link org.eclipse.jnosql.communication.query.InsertQuery} and - * {@link org.eclipse.jnosql.communication.query.UpdateQuery} - */ -interface ConditionQuerySupplier { - - List conditions(); - - Optional value(); - - default boolean useJSONCondition() { - return conditions().isEmpty() && value().isPresent(); - } - -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/Conditions.java b/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/Conditions.java deleted file mode 100644 index e263ea0de..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/Conditions.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - - -import org.eclipse.jnosql.communication.Params; -import org.eclipse.jnosql.communication.QueryException; -import org.eclipse.jnosql.communication.query.ConditionQueryValue; -import org.eclipse.jnosql.communication.query.QueryCondition; -import org.eclipse.jnosql.communication.query.Where; - -import static org.eclipse.jnosql.communication.document.DocumentCondition.between; -import static org.eclipse.jnosql.communication.document.DocumentCondition.eq; -import static org.eclipse.jnosql.communication.document.DocumentCondition.gt; -import static org.eclipse.jnosql.communication.document.DocumentCondition.gte; -import static org.eclipse.jnosql.communication.document.DocumentCondition.in; -import static org.eclipse.jnosql.communication.document.DocumentCondition.like; -import static org.eclipse.jnosql.communication.document.DocumentCondition.lt; -import static org.eclipse.jnosql.communication.document.DocumentCondition.lte; -import static org.eclipse.jnosql.communication.document.DocumentCondition.and; -import static org.eclipse.jnosql.communication.document.DocumentCondition.or; - - -final class Conditions { - - private Conditions() { - } - - static DocumentCondition getCondition(Where where, Params params, DocumentObserverParser observer, String entity) { - QueryCondition condition = where.condition(); - return getCondition(condition, params, observer, entity); - } - - static DocumentCondition getCondition(QueryCondition condition, Params parameters, DocumentObserverParser observer, String entity) { - return switch (condition.condition()) { - case EQUALS -> eq(Document.of(getName(condition, observer, entity), - Values.get(condition.value(), parameters))); - case GREATER_THAN -> gt(Document.of(getName(condition, observer, entity), - Values.get(condition.value(), parameters))); - case GREATER_EQUALS_THAN -> gte(Document.of(getName(condition, observer, entity), - Values.get(condition.value(), parameters))); - case LESSER_THAN -> lt(Document.of(getName(condition, observer, entity), - Values.get(condition.value(), parameters))); - case LESSER_EQUALS_THAN -> lte(Document.of(getName(condition, observer, entity), - Values.get(condition.value(), parameters))); - case IN -> in(Document.of(getName(condition, observer, entity), - Values.get(condition.value(), parameters))); - case LIKE -> like(Document.of(getName(condition, observer, entity), - Values.get(condition.value(), parameters))); - case BETWEEN -> between(Document.of(getName(condition, observer, entity), - Values.get(condition.value(), parameters))); - case NOT -> getCondition(ConditionQueryValue.class.cast(condition.value()).get().get(0), - parameters, observer, - entity).negate(); - case OR -> or(ConditionQueryValue.class.cast(condition.value()) - .get() - .stream().map(v -> getCondition(v, parameters, observer, entity)) - .toArray(DocumentCondition[]::new)); - case AND -> and(ConditionQueryValue.class.cast(condition.value()) - .get() - .stream().map(v -> getCondition(v, parameters, observer, entity)) - .toArray(DocumentCondition[]::new)); - default -> throw new QueryException("There is not support the type: " + condition.condition()); - }; - } - - private static String getName(QueryCondition condition, - DocumentObserverParser observer, String entity) { - return observer.fireField(entity, condition.name()); - } -} diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DefaultDocument.java b/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DefaultDocument.java deleted file mode 100644 index 01a806d30..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DefaultDocument.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * - * Copyright (c) 2023 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - -import org.eclipse.jnosql.communication.TypeSupplier; -import org.eclipse.jnosql.communication.Value; - -import java.util.Objects; - -record DefaultDocument(String name, Value value) implements Document { - - @Override - public T get(Class type) { - Objects.requireNonNull(type, "type is required"); - return value.get(type); - } - - @Override - public T get(TypeSupplier supplier) { - Objects.requireNonNull(supplier, "supplier is required"); - return value.get(supplier); - } - - @Override - public Object get() { - return value.get(); - } - -} diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DefaultDocumentDeleteQuery.java b/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DefaultDocumentDeleteQuery.java deleted file mode 100644 index d5ec7bf80..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DefaultDocumentDeleteQuery.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - - - -import java.util.List; -import java.util.Objects; -import java.util.Optional; - -import static java.util.Collections.unmodifiableList; -import static java.util.Optional.ofNullable; - -/** - * The default implementation of {@link DocumentDeleteQuery} - */ -record DefaultDocumentDeleteQuery (String name, - - DocumentCondition documentCondition, List documents) implements DocumentDeleteQuery { - - @Override - public Optional condition() { - return ofNullable(documentCondition).map(DocumentCondition::readOnly); - } - - @Override - public List documents() { - return unmodifiableList(documents); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (!(o instanceof DocumentDeleteQuery that)) { - return false; - } - return Objects.equals(name, that.name()) && - Objects.equals(documentCondition, that.condition().orElse(null)) && - Objects.equals(documents, that.documents()); - } - - @Override - public int hashCode() { - return Objects.hash(name, documentCondition, documents); - } - -} diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DefaultDocumentDeleteQueryBuilder.java b/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DefaultDocumentDeleteQueryBuilder.java deleted file mode 100644 index 69e7911b1..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DefaultDocumentDeleteQueryBuilder.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - - -import jakarta.data.Sort; - -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; -import java.util.function.Consumer; -import java.util.stream.Stream; - -import static java.util.Objects.requireNonNull; - -final class DefaultDocumentDeleteQueryBuilder implements DocumentDeleteQuery.DocumentDeleteQueryBuilder { - - private final List documents = new ArrayList<>(); - - private final List sorts = new ArrayList<>(); - - private String documentCollection; - - private DocumentCondition condition; - - - @Override - public DefaultDocumentDeleteQueryBuilder delete(String document) { - Objects.requireNonNull(document, "document is required"); - this.documents.add(document); - return this; - } - - @Override - public DefaultDocumentDeleteQueryBuilder delete(String... documents) { - Consumer validNull = d -> requireNonNull(d, "there is null document in the query"); - Consumer consume = this.documents::add; - Stream.of(documents).forEach(validNull.andThen(consume)); - return this; - } - - @Override - public DefaultDocumentDeleteQueryBuilder from(String documentCollection) { - Objects.requireNonNull(documentCollection, "documentCollection is required"); - this.documentCollection = documentCollection; - return this; - } - - @Override - public DefaultDocumentDeleteQueryBuilder where(DocumentCondition condition) { - Objects.requireNonNull(condition, "condition is required"); - this.condition = condition; - return this; - } - - @Override - public DocumentDeleteQuery build() { - if (Objects.isNull(documentCollection)) { - throw new IllegalArgumentException("The document collection is mandatory to build"); - } - return new DefaultDocumentDeleteQuery(documentCollection, condition, documents); - } - - @Override - public void delete(DocumentManager manager) { - Objects.requireNonNull(manager, "manager is required"); - manager.delete(build()); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - DefaultDocumentDeleteQueryBuilder that = (DefaultDocumentDeleteQueryBuilder) o; - return Objects.equals(documents, that.documents) - && Objects.equals(sorts, that.sorts) - && Objects.equals(documentCollection, that.documentCollection) - && Objects.equals(condition, that.condition); - } - - @Override - public int hashCode() { - return Objects.hash(documents, sorts, documentCollection, condition); - } - - @Override - public String toString() { - return "DefaultDeleteQueryBuilder{" + - "documents=" + documents + - ", sorts=" + sorts + - ", documentCollection='" + documentCollection + '\'' + - ", condition=" + condition + - '}'; - } -} diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DefaultDocumentQuery.java b/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DefaultDocumentQuery.java deleted file mode 100644 index f575c7f43..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DefaultDocumentQuery.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - - -import jakarta.data.Sort; - -import java.util.Collections; -import java.util.List; -import java.util.Objects; -import java.util.Optional; - -import static java.util.Collections.unmodifiableList; -import static java.util.Optional.ofNullable; - -record DefaultDocumentQuery(long limit, long skip, String name, - List documents, List> sorts, DocumentCondition documentCondition) implements DocumentQuery { - - - - @Override - public Optional condition() { - return ofNullable(documentCondition).map(DocumentCondition::readOnly); - } - - @Override - public List> sorts() { - return unmodifiableList(sorts); - } - - @Override - public List documents() { - return unmodifiableList(documents); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (!(o instanceof DocumentQuery that)) { - return false; - } - return limit == that.limit() && - skip == that.skip() && - Objects.equals(name, that.name()) && - Objects.equals(documentCondition, that.condition().orElse(null)) && - Objects.equals(sorts, that.sorts()) && - Objects.equals(documents, that.documents()); - } - - @Override - public int hashCode() { - return Objects.hash(limit, skip, name, documentCondition, sorts, documents); - } - - - static DocumentQuery countBy(DocumentQuery query) { - return new DefaultDocumentQuery(0, 0, query.name(), query.documents(), - Collections.emptyList(), query.condition().orElse(null)); - } - static DocumentQuery existsBy(DocumentQuery query) { - return new DefaultDocumentQuery(1, 0, query.name(), query.documents(), - Collections.emptyList(), query.condition().orElse(null)); - } -} diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DefaultDocumentQueryBuilder.java b/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DefaultDocumentQueryBuilder.java deleted file mode 100644 index 5af7020c2..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DefaultDocumentQueryBuilder.java +++ /dev/null @@ -1,162 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - - -import jakarta.data.Sort; - -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; -import java.util.Optional; -import java.util.function.Consumer; -import java.util.stream.Stream; - -import static java.util.Objects.requireNonNull; - -class DefaultDocumentQueryBuilder implements DocumentQuery.DocumentQueryBuilder { - - private final List documents = new ArrayList<>(); - - private final List> sorts = new ArrayList<>(); - - private String documentCollection; - - private DocumentCondition condition; - - private long skip; - - private long limit; - - - @Override - public DocumentQuery.DocumentQueryBuilder select(String document) { - Objects.requireNonNull(document, "document is required"); - this.documents.add(document); - return this; - } - - @Override - public DocumentQuery.DocumentQueryBuilder select(String... documents) { - Consumer validNull = d -> requireNonNull(d, "there is null document in the query"); - Consumer consume = this.documents::add; - Stream.of(documents).forEach(validNull.andThen(consume)); - return this; - } - - @Override - public DocumentQuery.DocumentQueryBuilder sort(Sort sort) { - Objects.requireNonNull(sort, "sort is required"); - this.sorts.add(sort); - return this; - } - - @Override - public DocumentQuery.DocumentQueryBuilder sort(Sort... sorts) { - Consumer validNull = d -> requireNonNull(d, "there is null document in the query"); - Consumer consume = this.sorts::add; - Stream.of(sorts).forEach(validNull.andThen(consume)); - return this; - } - - @Override - public DocumentQuery.DocumentQueryBuilder from(String documentCollection) { - Objects.requireNonNull(documentCollection, "documentCollection is required"); - this.documentCollection = documentCollection; - return this; - } - - @Override - public DocumentQuery.DocumentQueryBuilder where(DocumentCondition condition) { - Objects.requireNonNull(condition, "condition is required"); - this.condition = condition; - return this; - } - - @Override - public DocumentQuery.DocumentQueryBuilder skip(long skip) { - if (skip < 0) { - throw new IllegalArgumentException("The skip should not be negative, skip: " + skip); - } - this.skip = skip; - return this; - } - - @Override - public DocumentQuery.DocumentQueryBuilder limit(long limit) { - if (limit < 0) { - throw new IllegalArgumentException("The limit should not be negative, limit: " + limit); - } - this.limit = limit; - return this; - } - - @Override - public DocumentQuery build() { - if (Objects.isNull(documentCollection)) { - throw new IllegalArgumentException("The document collection is mandatory to build"); - } - return new DefaultDocumentQuery(limit, skip, documentCollection, - documents, sorts, condition); - } - - @Override - public Stream getResult(DocumentManager manager) { - Objects.requireNonNull(manager, "manager is required"); - return manager.select(build()); - } - - @Override - public Optional getSingleResult(DocumentManager manager) { - Objects.requireNonNull(manager, "manager is required"); - return manager.singleResult(build()); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - DefaultDocumentQueryBuilder that = (DefaultDocumentQueryBuilder) o; - return skip == that.skip - && limit == that.limit - && Objects.equals(documents, that.documents) - && Objects.equals(sorts, that.sorts) - && Objects.equals(documentCollection, that.documentCollection) - && Objects.equals(condition, that.condition); - } - - @Override - public int hashCode() { - return Objects.hash(documents, sorts, documentCollection, condition, skip, limit); - } - - @Override - public String toString() { - return "DefaultDocumentQueryBuilder{" + - "documents=" + documents + - ", sorts=" + sorts + - ", documentCollection='" + documentCollection + '\'' + - ", condition=" + condition + - ", skip=" + skip + - ", limit=" + limit + - '}'; - } -} diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DefaultFluentDeleteQueryBuilder.java b/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DefaultFluentDeleteQueryBuilder.java deleted file mode 100644 index 05b05ef61..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DefaultFluentDeleteQueryBuilder.java +++ /dev/null @@ -1,144 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - -import org.eclipse.jnosql.communication.document.DocumentDeleteQuery.DocumentDelete; -import org.eclipse.jnosql.communication.document.DocumentDeleteQuery.DocumentDeleteFrom; -import org.eclipse.jnosql.communication.document.DocumentDeleteQuery.DocumentDeleteNameCondition; -import org.eclipse.jnosql.communication.document.DocumentDeleteQuery.DocumentDeleteNotCondition; -import org.eclipse.jnosql.communication.document.DocumentDeleteQuery.DocumentDeleteWhere; - -import java.util.List; - -import static java.util.Objects.requireNonNull; - -/** - * The default implementation to Delete query - */ -class DefaultFluentDeleteQueryBuilder extends BaseQueryBuilder implements DocumentDelete, DocumentDeleteFrom, - DocumentDeleteWhere, DocumentDeleteNotCondition { - - private String documentCollection; - - private final List documents; - - - DefaultFluentDeleteQueryBuilder(List documents) { - this.documents = documents; - } - - @Override - public DocumentDeleteFrom from(String documentCollection) { - requireNonNull(documentCollection, "documentCollection is required"); - this.documentCollection = documentCollection; - return this; - } - - - @Override - public DocumentDeleteNameCondition where(String name) { - requireNonNull(name, "name is required"); - this.name = name; - return this; - } - - - @Override - public DocumentDeleteNameCondition and(String name) { - requireNonNull(name, "name is required"); - this.name = name; - this.and = true; - return this; - } - - @Override - public DocumentDeleteNameCondition or(String name) { - requireNonNull(name, "name is required"); - this.name = name; - this.and = false; - return this; - } - - - @Override - public DocumentDeleteNotCondition not() { - this.negate = true; - return this; - } - - @Override - public DocumentDeleteWhere eq(T value) { - eqImpl(value); - return this; - } - - @Override - public DocumentDeleteWhere like(String value) { - likeImpl(value); - return this; - } - - @Override - public DocumentDeleteWhere gt(T value) { - gtImpl(value); - return this; - } - - @Override - public DocumentDeleteWhere gte(T value) { - gteImpl(value); - return this; - } - - @Override - public DocumentDeleteWhere lt(T value) { - ltImpl(value); - return this; - } - - @Override - public DocumentDeleteWhere lte(T value) { - lteImpl(value); - return this; - } - - @Override - public DocumentDeleteWhere between(T valueA, T valueB) { - betweenImpl(valueA, valueB); - return this; - } - - @Override - public DocumentDeleteWhere in(Iterable values) { - inImpl(values); - return this; - } - - - @Override - public DocumentDeleteQuery build() { - return new DefaultDocumentDeleteQuery(documentCollection, condition, documents); - } - - @Override - public void delete(DocumentManager manager) { - requireNonNull(manager, "manager is required"); - manager.delete(this.build()); - } - - -} diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DefaultFluentDocumentQueryBuilder.java b/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DefaultFluentDocumentQueryBuilder.java deleted file mode 100644 index 6e69e3e1f..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DefaultFluentDocumentQueryBuilder.java +++ /dev/null @@ -1,207 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - - -import jakarta.data.Sort; -import jakarta.data.Direction; -import org.eclipse.jnosql.communication.document.DocumentQuery.DocumentFrom; -import org.eclipse.jnosql.communication.document.DocumentQuery.DocumentLimit; -import org.eclipse.jnosql.communication.document.DocumentQuery.DocumentNameCondition; -import org.eclipse.jnosql.communication.document.DocumentQuery.DocumentNameOrder; -import org.eclipse.jnosql.communication.document.DocumentQuery.DocumentNotCondition; -import org.eclipse.jnosql.communication.document.DocumentQuery.DocumentOrder; -import org.eclipse.jnosql.communication.document.DocumentQuery.DocumentQueryBuild; -import org.eclipse.jnosql.communication.document.DocumentQuery.DocumentSelect; -import org.eclipse.jnosql.communication.document.DocumentQuery.DocumentSkip; -import org.eclipse.jnosql.communication.document.DocumentQuery.DocumentWhere; - -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; -import java.util.stream.Stream; - -import static java.util.Objects.requireNonNull; - -/** - * The default implementation of the Select in the document - */ -class DefaultFluentDocumentQueryBuilder extends BaseQueryBuilder implements DocumentSelect, DocumentFrom, DocumentLimit, - DocumentSkip, DocumentOrder, DocumentNotCondition, DocumentNameOrder, DocumentWhere, DocumentQueryBuild { - - - private String documentCollection; - - private long skip; - - private long limit; - - private final List> sorts = new ArrayList<>(); - - private final List documents; - - - DefaultFluentDocumentQueryBuilder(List documents) { - this.documents = documents; - } - - - @Override - public DocumentFrom from(String documentCollection) { - requireNonNull(documentCollection, "documentCollection is required"); - this.documentCollection = documentCollection; - return this; - } - - - @Override - public DocumentNameCondition where(String name) { - requireNonNull(name, "name is required"); - this.name = name; - return this; - } - - @Override - public DocumentNameCondition and(String name) { - requireNonNull(name, "name is required"); - this.name = name; - this.and = true; - return this; - } - - @Override - public DocumentNameCondition or(String name) { - requireNonNull(name, "name is required"); - this.name = name; - this.and = false; - return this; - } - - @Override - public DocumentSkip skip(long skip) { - if (skip < 0) { - throw new IllegalArgumentException("The skip should not be negative, skip: " + skip); - } - this.skip = skip; - return this; - } - - @Override - public DocumentLimit limit(long limit) { - if (limit < 0) { - throw new IllegalArgumentException("The limit should not be negative, limit: " + limit); - } - this.limit = limit; - return this; - } - - @Override - public DocumentOrder orderBy(String name) { - requireNonNull(name, "name is required"); - this.name = name; - return this; - } - - - @Override - public DocumentNotCondition not() { - this.negate = true; - return this; - } - - @Override - public DocumentWhere eq(T value) { - eqImpl(value); - return this; - } - - @Override - public DocumentWhere like(String value) { - likeImpl(value); - return this; - } - - @Override - public DocumentWhere gt(T value) { - gtImpl(value); - return this; - } - - @Override - public DocumentWhere gte(T value) { - gteImpl(value); - return this; - } - - @Override - public DocumentWhere lt(T value) { - ltImpl(value); - return this; - } - - - @Override - public DocumentWhere lte(T value) { - lteImpl(value); - return this; - } - - @Override - public DocumentWhere between(T valueA, T valueB) { - betweenImpl(valueA, valueB); - return this; - } - - - @Override - public DocumentWhere in(Iterable values) { - inImpl(values); - return this; - } - - - @Override - public DocumentNameOrder asc() { - this.sorts.add(Sort.of(name, Direction.ASC, false)); - return this; - } - - @Override - public DocumentNameOrder desc() { - this.sorts.add(Sort.of(name, Direction.DESC, false)); - return this; - } - - - @Override - public DocumentQuery build() { - return new DefaultDocumentQuery(limit, skip, documentCollection, documents, sorts, condition); - } - - @Override - public Stream getResult(DocumentManager manager) { - requireNonNull(manager, "manager is required"); - return manager.select(this.build()); - } - - @Override - public Optional getSingleResult(DocumentManager manager) { - requireNonNull(manager, "manager is required"); - return manager.singleResult(this.build()); - } - -} diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DeleteQueryParser.java b/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DeleteQueryParser.java deleted file mode 100644 index e078cad6c..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DeleteQueryParser.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - - -import org.eclipse.jnosql.communication.Params; -import org.eclipse.jnosql.communication.QueryException; -import org.eclipse.jnosql.communication.query.DeleteQuery; -import org.eclipse.jnosql.communication.query.DeleteQueryConverter; - -import java.util.List; -import java.util.Objects; -import java.util.function.BiFunction; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -public final class DeleteQueryParser implements BiFunction { - - Stream query(String query, DocumentManager manager, DocumentObserverParser observer) { - - DocumentDeleteQuery documentQuery = getQuery(query, observer); - manager.delete(documentQuery); - return Stream.empty(); - } - - - DocumentPreparedStatement prepare(String query, DocumentManager collectionManager, - DocumentObserverParser observer) { - Params params = Params.newParams(); - DocumentDeleteQuery documentQuery = getQuery(query, params, observer); - return DocumentPreparedStatement.delete(documentQuery, params, query, collectionManager); - } - - - - @Override - public DocumentDeleteQueryParams apply(DeleteQuery deleteQuery, DocumentObserverParser observer) { - Objects.requireNonNull(deleteQuery, "deleteQuery is required"); - Objects.requireNonNull(observer, "observer is required"); - Params params = Params.newParams(); - DocumentDeleteQuery query = getQuery(params, observer, deleteQuery); - return new DocumentDeleteQueryParams(query, params); - } - - private DocumentDeleteQuery getQuery(String query, Params params, DocumentObserverParser observer) { - DeleteQueryConverter converter = new DeleteQueryConverter(); - DeleteQuery deleteQuery = converter.apply(query); - - return getQuery(params, observer, deleteQuery); - } - - private DocumentDeleteQuery getQuery(Params params, DocumentObserverParser observer, - DeleteQuery deleteQuery) { - String collection = observer.fireEntity(deleteQuery.entity()); - List documents = deleteQuery.fields().stream() - .map(f -> observer.fireField(collection, f)) - .collect(Collectors.toList()); - DocumentCondition condition = deleteQuery.where() - .map(c -> Conditions.getCondition(c, params, observer, collection)).orElse(null); - - return new DefaultDocumentDeleteQuery(collection, condition, documents); - } - - private DocumentDeleteQuery getQuery(String query, DocumentObserverParser observer) { - DeleteQueryConverter converter = new DeleteQueryConverter(); - DeleteQuery deleteQuery = converter.apply(query); - - String collection = observer.fireEntity(deleteQuery.entity()); - List documents = deleteQuery.fields().stream() - .map(f -> observer.fireField(collection, f)) - .collect(Collectors.toList()); - Params params = Params.newParams(); - - DocumentCondition condition = deleteQuery.where() - .map(c -> Conditions.getCondition(c, params, observer, collection)) - .orElse(null); - - if (params.isNotEmpty()) { - throw new QueryException("To run a query with a parameter use a PrepareStatement instead."); - } - return new DefaultDocumentDeleteQuery(collection, condition, documents); - } - - -} diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/Document.java b/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/Document.java deleted file mode 100644 index f841d422a..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/Document.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ - -package org.eclipse.jnosql.communication.document; - - -import org.eclipse.jnosql.communication.Entry; -import org.eclipse.jnosql.communication.TypeSupplier; -import org.eclipse.jnosql.communication.Value; - -import java.util.Objects; - -/** - * A Document is a tuple (pair) that consists of the name and its respective value. - * A {@link DocumentEntity} has one or more Documents. - */ -public interface Document extends Entry { - - - /** - * Alias to {@link Value#get(Class)} - * - * @param type {@link Value#get(Class)} - * @param {@link Value#get(Class)} - * @return {@link Value#get(Class)} - * @throws NullPointerException see {@link Value#get(Class)} - * @throws UnsupportedOperationException see {@link Value#get(Class)} - */ - T get(Class type) ; - - /** - * Alias to {@link Value#get(TypeSupplier)} - * - * @param supplier {@link Value#get(TypeSupplier)} - * @param {@link Value#get(TypeSupplier)} - * @return {@link Value#get(TypeSupplier)} - * @throws NullPointerException see {@link Value#get(TypeSupplier)} - * @throws UnsupportedOperationException see {@link Value#get(TypeSupplier)} - */ - T get(TypeSupplier supplier); - - - /** - * Alias to {@link Value#get()} - * - * @return {@link Value#get()} - */ - Object get(); - - /** - * Creates a document instance - * - * @param name - document's name - * @param value - document's value - * @param the value type - * @return a document instance - * @throws NullPointerException when name is null - * @see Documents - */ - static Document of(String name, V value) { - Objects.requireNonNull(name, "name is required"); - return new DefaultDocument(name, getValue(value)); - } - - private static Value getValue(Object value) { - if (value instanceof Value) { - return Value.class.cast(value); - } else { - return Value.of(value); - } - } -} diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentCondition.java b/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentCondition.java deleted file mode 100644 index e6480fbe9..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentCondition.java +++ /dev/null @@ -1,494 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ - -package org.eclipse.jnosql.communication.document; - - -import org.eclipse.jnosql.communication.Condition; -import org.eclipse.jnosql.communication.TypeReference; -import org.eclipse.jnosql.communication.Value; - -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; -import java.util.stream.StreamSupport; - -import static java.util.Arrays.asList; -import static java.util.Objects.requireNonNull; - -/** - * It is the state of column queries, with a condition and a value, as a {@link Document}, - * where both combined define a predicate. - * - * @see DocumentManager#select(DocumentQuery) - * @see Condition - */ -public final class DocumentCondition { - - private final Document document; - - private final Condition condition; - - private final boolean readOnly; - - private DocumentCondition(Document document, Condition condition) { - this.document = document; - this.condition = condition; - this.readOnly = false; - } - - private DocumentCondition(Document document, Condition condition, boolean readOnly) { - this.document = document; - this.condition = condition; - this.readOnly = readOnly; - } - - /** - * Gets the document to be used in the select - * - * @return a document instance - */ - public Document document() { - return document; - } - - /** - * Gets the conditions to be used in the select - * - * @return a Condition instance - * @see Condition - */ - public Condition condition() { - return condition; - } - - /** - * Creates a new {@link DocumentCondition} using the {@link Condition#AND} - * - * @param condition the condition to be aggregated - * @return the conditions joined as AND - * @throws NullPointerException when the condition is null - */ - public DocumentCondition and(DocumentCondition condition) { - validateReadOnly(); - requireNonNull(condition, "Conditions is required"); - if (Condition.AND.equals(this.condition)) { - Document column = getConditions(condition, Condition.AND); - return new DocumentCondition(column, Condition.AND); - } - return DocumentCondition.and(this, condition); - } - - /** - * Creates a new {@link DocumentCondition} negating the current one - * - * @return the negated condition - * @see Condition#NOT - */ - public DocumentCondition negate() { - validateReadOnly(); - if (Condition.NOT.equals(this.condition)) { - return this.document.get(DocumentCondition.class); - } else { - Document newDocument = Document.of(Condition.NOT.getNameField(), this); - return new DocumentCondition(newDocument, Condition.NOT); - } - } - - /** - * Creates a new {@link DocumentCondition} using the {@link Condition#OR} - * - * @param condition the condition to be aggregated - * @return the conditions joined as AND - * @throws NullPointerException when the condition is null - */ - public DocumentCondition or(DocumentCondition condition) { - validateReadOnly(); - requireNonNull(condition, "Condition is required"); - if (Condition.OR.equals(this.condition)) { - Document newDocument = getConditions(condition, Condition.OR); - return new DocumentCondition(newDocument, Condition.OR); - } - return DocumentCondition.or(this, condition); - } - - private void validateReadOnly() { - if (readOnly) { - throw new IllegalStateException("You cannot change the status after building the query"); - } - } - - - private Document getConditions(DocumentCondition columnCondition, Condition condition) { - List conditions = new ArrayList<>(document.get(new TypeReference>() { - })); - conditions.add(columnCondition); - return Document.of(condition.getNameField(), conditions); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - DocumentCondition that = (DocumentCondition) o; - return Objects.equals(document, that.document) && condition == that.condition; - } - - @Override - public int hashCode() { - return Objects.hash(document, condition); - } - - @Override - public String toString() { - return "DocumentCondition{" + "document=" + document + - ", condition=" + condition + - '}'; - } - - public static DocumentCondition readOnly(DocumentCondition condition) { - requireNonNull(condition, "condition is required"); - return new DocumentCondition(condition.document(), condition.condition(), true); - } - - public static DocumentCondition of(Document document, Condition condition) { - return new DocumentCondition(Objects.requireNonNull(document, "Document is required"), condition); - } - - /** - * Returns a new {@link DocumentCondition} aggregating ,as "AND", all the conditions as just one condition. - * The {@link Document} will storage the {@link Condition#getNameField()} as key and the value gonna be - * the {@link java.util.List} of all conditions, in other words. - *

Given:

- *
-     * {@code
-     * Document age = Document.of("age", 26);
-     * Document name = Document.of("name", "otavio");
-     * DocumentCondition condition = DocumentCondition.eq(name).and(DocumentCondition.gte(age));
-     * }
-     * 
- * The {@link DocumentCondition#document()} will have "_AND" as key and the list of condition as value. - * - * @param conditions the conditions to be aggregated - * @return the new {@link DocumentCondition} instance - * @throws NullPointerException when the conditions are null - */ - public static DocumentCondition and(DocumentCondition... conditions) { - requireNonNull(conditions, "condition is required"); - Document document = Document.of(Condition.AND.getNameField(), asList(conditions)); - return DocumentCondition.of(document, Condition.AND); - } - - /** - * Returns a new {@link DocumentCondition} aggregating ,as "OR", all the conditions as just one condition. - * The {@link Document} will storage the {@link Condition#getNameField()} as key and the value gonna be - * the {@link java.util.List} of all conditions, in other words. - *

Given:

- *
-     * {@code
-     * Document age = Document.of("age", 26);
-     * Document name = Document.of("name", "otavio");
-     * ColumnCondition condition = DocumentCondition.eq(name).or(DocumentCondition.gte(age));
-     * }
-     * 
- * The {@link DocumentCondition#document()} will have "_OR" as key and the list of condition as value. - * - * @param conditions the conditions to be aggregated - * @return the new {@link DocumentCondition} instance - * @throws NullPointerException when the condition is null - */ - public static DocumentCondition or(DocumentCondition... conditions) { - requireNonNull(conditions, "condition is required"); - Document document = Document.of(Condition.OR.getNameField(), asList(conditions)); - return DocumentCondition.of(document, Condition.OR); - } - - /** - * Creates a {@link DocumentCondition} that has a {@link Condition#EQUALS}, it means a select will scanning to a - * document collection that has the same name and equals value informed in this document. - * - * @param document a column instance - * @return a {@link DocumentCondition} with {@link Condition#EQUALS} - * @throws NullPointerException when column is null - */ - public static DocumentCondition eq(Document document) { - Objects.requireNonNull(document, "document is required"); - return new DocumentCondition(document, Condition.EQUALS); - } - - /** - * an alias method to {@link DocumentCondition#eq(Document)} where it will create a {@link Document} - * instance first and then apply te condition. - * - * @param name the name of the document - * @param value the document information - * @return a {@link DocumentCondition} with {@link Condition#EQUALS} - * @throws NullPointerException when either name or value is null - */ - public static DocumentCondition eq(String name, Object value) { - Objects.requireNonNull(name, "name is required"); - Objects.requireNonNull(value, "value is required"); - return new DocumentCondition(Document.of(name, value), Condition.EQUALS); - } - - /** - * Creates a {@link DocumentCondition} that has a {@link Condition#GREATER_THAN}, - * it means a select will scanning to a document collection that has the same name and the value - * greater than informed in this document. - * - * @param document a column instance - * @return a {@link DocumentCondition} with {@link Condition#GREATER_THAN} - * @throws NullPointerException when column is null - */ - public static DocumentCondition gt(Document document) { - Objects.requireNonNull(document, "document is required"); - return new DocumentCondition(document, Condition.GREATER_THAN); - } - - /** - * an alias method to {@link DocumentCondition#gt(Document)} where it will create a {@link Document} - * instance first and then apply te condition. - * - * @param name the name of the document - * @param value the document information - * @return a {@link DocumentCondition} with {@link Condition#GREATER_THAN} - * @throws NullPointerException when either name or value is null - */ - public static DocumentCondition gt(String name, Object value) { - Objects.requireNonNull(name, "name is required"); - Objects.requireNonNull(value, "value is required"); - return gt(Document.of(name, value)); - } - - /** - * Creates a {@link DocumentCondition} that has a {@link Condition#GREATER_EQUALS_THAN}, - * it means a select will scanning to a document collection that has the same name and the value - * greater or equals than informed in this document. - * - * @param document a column instance - * @return a {@link DocumentCondition} with {@link Condition#GREATER_EQUALS_THAN} - * @throws NullPointerException when column is null - */ - public static DocumentCondition gte(Document document) { - Objects.requireNonNull(document, "document is required"); - return new DocumentCondition(document, Condition.GREATER_EQUALS_THAN); - } - - /** - * an alias method to {@link DocumentCondition#gte(Document)} where it will create a {@link Document} - * instance first and then apply te condition. - * - * @param name the name of the document - * @param value the document information - * @return a {@link DocumentCondition} with {@link Condition#GREATER_EQUALS_THAN} - * @throws NullPointerException when either name or value is null - */ - public static DocumentCondition gte(String name, Object value) { - Objects.requireNonNull(name, "name is required"); - Objects.requireNonNull(value, "value is required"); - return gte(Document.of(name, value)); - } - - /** - * Creates a {@link DocumentCondition} that has a {@link Condition#LESSER_THAN}, it means a select will scanning to a - * document collection that has the same name and the value lesser than informed in this document. - * - * @param document a column instance - * @return a {@link DocumentCondition} with {@link Condition#LESSER_THAN} - * @throws NullPointerException when column is null - */ - public static DocumentCondition lt(Document document) { - Objects.requireNonNull(document, "document is required"); - return new DocumentCondition(document, Condition.LESSER_THAN); - } - - /** - * an alias method to {@link DocumentCondition#lt(Document)} where it will create a {@link Document} - * instance first and then apply te condition. - * - * @param name the name of the document - * @param value the document information - * @return a {@link DocumentCondition} with {@link Condition#LESSER_THAN} - * @throws NullPointerException when either name or value is null - */ - public static DocumentCondition lt(String name, Object value) { - Objects.requireNonNull(name, "name is required"); - Objects.requireNonNull(value, "value is required"); - return lt(Document.of(name, value)); - } - - /** - * Creates a {@link DocumentCondition} that has a {@link Condition#LESSER_EQUALS_THAN}, - * it means a select will scanning to a document collection that has the same name and the value - * lesser or equals than informed in this document. - * - * @param document a document instance - * @return a {@link DocumentCondition} with {@link Condition#LESSER_EQUALS_THAN} - * @throws NullPointerException when column is null - */ - public static DocumentCondition lte(Document document) { - Objects.requireNonNull(document, "document is required"); - return new DocumentCondition(document, Condition.LESSER_EQUALS_THAN); - } - - /** - * an alias method to {@link DocumentCondition#lte(Document)} where it will create a {@link Document} - * instance first and then apply te condition. - * - * @param name the name of the document - * @param value the document information - * @return a {@link DocumentCondition} with {@link Condition#LESSER_EQUALS_THAN} - * @throws NullPointerException when either name or value is null - */ - public static DocumentCondition lte(String name, Object value) { - Objects.requireNonNull(name, "name is required"); - Objects.requireNonNull(value, "value is required"); - return lte(Document.of(name, value)); - } - - /** - * Creates a {@link DocumentCondition} that has a {@link Condition#IN}, it means a select will scanning to a - * document collection that has the same name and the value is within informed in this document. - * - * @param document a column instance - * @return a {@link DocumentCondition} with {@link Condition#IN} - * @throws NullPointerException when column is null - */ - public static DocumentCondition in(Document document) { - Objects.requireNonNull(document, "document is required"); - Value value = document.value(); - checkInClause(value); - return new DocumentCondition(document, Condition.IN); - } - - /** - * an alias method to {@link DocumentCondition#in(Document)} where it will create a {@link Document} - * instance first and then apply te condition. - * - * @param name the name of the document - * @param value the document information - * @return a {@link DocumentCondition} with {@link Condition#IN} - * @throws NullPointerException when either name or value is null - */ - public static DocumentCondition in(String name, Object value) { - Objects.requireNonNull(name, "name is required"); - Objects.requireNonNull(value, "value is required"); - return in(Document.of(name, value)); - } - - /** - * Creates a {@link DocumentCondition} that has a {@link Condition#LIKE}, it means a select will scanning to a - * document collection that has the same name and the value is like than informed in this document. - * - * @param document a column instance - * @return a {@link DocumentCondition} with {@link Condition#LIKE} - * @throws NullPointerException when column is null - */ - public static DocumentCondition like(Document document) { - Objects.requireNonNull(document, "document is required"); - return new DocumentCondition(document, Condition.LIKE); - } - - - /** - * an alias method to {@link DocumentCondition#like(Document)} where it will create a {@link Document} - * instance first and then apply te condition. - * - * @param name the name of the document - * @param value the document information - * @return a {@link DocumentCondition} with {@link Condition#LIKE} - * @throws NullPointerException when either name or value is null - */ - public static DocumentCondition like(String name, Object value) { - Objects.requireNonNull(name, "name is required"); - Objects.requireNonNull(value, "value is required"); - return like(Document.of(name, value)); - } - - /** - * Creates a {@link DocumentCondition} that has a {@link Condition#BETWEEN}, - * it means a select will scanning to a document collection that is between two values informed - * on a document name. - * The document must have a {@link Document#get()} an {@link Iterable} implementation - * with just two elements. - * - * @param document a column instance - * @return The between condition - * @throws NullPointerException when document is null - * @throws IllegalArgumentException When the document neither has an Iterable instance or two elements on - * an Iterable. - */ - public static DocumentCondition between(Document document) { - Objects.requireNonNull(document, "document is required"); - checkBetweenClause(document.get()); - return new DocumentCondition(document, Condition.BETWEEN); - } - - /** - * Returns a predicate that is the negation of the supplied predicate. - * This is accomplished by returning result of the calling target.negate(). - * - * @param condition the condition - * @return a condition that negates the results of the supplied predicate - * @throws NullPointerException when condition is null - */ - public static DocumentCondition not(DocumentCondition condition) { - Objects.requireNonNull(condition, "condition is required"); - return condition.negate(); - } - - /** - * an alias method to {@link DocumentCondition#between(Document)} where it will create a {@link Document} - * instance first and then apply te condition. - * - * @param name the name of the document - * @param value the document information - * @return a {@link DocumentCondition} with {@link Condition#BETWEEN} - * @throws NullPointerException when either name or value is null - */ - public static DocumentCondition between(String name, Object value) { - Objects.requireNonNull(name, "name is required"); - Objects.requireNonNull(value, "value is required"); - return between(Document.of(name, value)); - } - - private static void checkInClause(Value value) { - if (!value.isInstanceOf(Iterable.class)) { - throw new IllegalArgumentException("On DocumentCondition#in you must use an iterable" + - " instead of class: " + value.getClass().getName()); - } - } - - private static void checkBetweenClause(Object value) { - if (Iterable.class.isInstance(value)) { - - long count = (int) StreamSupport.stream(Iterable.class.cast(value).spliterator(), false).count(); - - if (count != 2) { - throw new IllegalArgumentException("On DocumentCondition#between you must use an iterable" + - " with two elements"); - } - } else { - throw new IllegalArgumentException("On DocumentCondition#between you must use an iterable" + - " with two elements instead of class: " + value.getClass().getName()); - } - } -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentConditionProvider.java b/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentConditionProvider.java deleted file mode 100644 index 9b4286625..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentConditionProvider.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * - * Copyright (c) 2023 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - - -import org.eclipse.jnosql.communication.Condition; - -/** - * The default implementation of {@link DocumentConditionProvider} - */ -public final class DocumentConditionProvider { - - public DocumentCondition between(Document document) { - return DocumentCondition.between(document); - } - - public DocumentCondition and(DocumentCondition... conditions) { - return DocumentCondition.and(conditions); - } - - public DocumentCondition or(DocumentCondition... conditions) { - return DocumentCondition.or(conditions); - } - - public DocumentCondition in(Document document) { - return DocumentCondition.in(document); - } - - public DocumentCondition apply(Document document, Condition condition) { - return DocumentCondition.of(document, condition); - } -} diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentConfiguration.java b/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentConfiguration.java deleted file mode 100644 index d577bfbd9..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentConfiguration.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * - * Copyright (c) 2023 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - - -import org.eclipse.jnosql.communication.CommunicationException; -import org.eclipse.jnosql.communication.Settings; - -import java.util.ServiceLoader; -import java.util.function.Function; - -/** - * It is a function that reads from {@link Settings} and then creates a manager factory instance. - * It should return a {@link NullPointerException} when the {@link Settings} parameter is null. - * - * @see DocumentManagerFactory - * @see DocumentManager - */ -public interface DocumentConfiguration extends Function { - - /** - * creates and returns a {@link DocumentConfiguration} instance from {@link ServiceLoader} - * - * @param the configuration type - * @return {@link DocumentConfiguration} instance - */ - static T getConfiguration() { - return (T) ServiceLoader.load(DocumentConfiguration.class) - .stream() - .map(ServiceLoader.Provider::get) - .findFirst().orElseThrow(() -> new CommunicationException("No DocumentConfiguration implementation found!")); - - } - - /** - * creates and returns a {@link DocumentConfiguration} instance from {@link ServiceLoader} - * for a particular provider implementation. - * - * @param the configuration type - * @param type the particular provider - * @return {@link DocumentConfiguration} instance - */ - static T getConfiguration(Class type) { - return (T) ServiceLoader.load(DocumentConfiguration.class) - .stream() - .map(ServiceLoader.Provider::get) - .filter(type::isInstance) - .findFirst().orElseThrow(() -> new CommunicationException("No DocumentConfiguration implementation found!")); - } -} diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentDeleteQuery.java b/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentDeleteQuery.java deleted file mode 100644 index 1bb397d62..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentDeleteQuery.java +++ /dev/null @@ -1,374 +0,0 @@ -/* - * - * Copyright (c) 2023 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - - - -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Optional; -import java.util.function.Function; -import java.util.function.Supplier; -import java.util.stream.Stream; - -import static java.util.Objects.requireNonNull; - -/** - * A unit that has the columnFamily and condition to delete from conditions - *

- * This instance will be used on: - *

{@link DocumentManager#delete(DocumentDeleteQuery)}

- */ -public interface DocumentDeleteQuery { - - /** - * getter the collection name - * - * @return the collection name - */ - String name(); - - /** - * getter the condition - * - * @return the condition - */ - Optional condition(); - - /** - * Defines which columns will be removed, the database provider might use this information - * to remove just these fields instead of all entity from {@link DocumentDeleteQuery} - * - * @return the columns - */ - List documents(); - - /** - * It starts the first step of {@link DocumentDelete} API using a fluent-API way. - * This first step will inform the fields to delete in the query instead of the whole record. - * This behavior might be different for each NoSQL database provider; therefore, it might be ignored for some implementations. - * - * @param documents the column fields to delete query - * @return a new {@link DocumentDelete} instance - * @throws NullPointerException when there is a null element - */ - static DocumentDelete delete(String... documents) { - Stream.of(documents).forEach(d -> requireNonNull(d, "there is null document in the query")); - return new DefaultFluentDeleteQueryBuilder(Arrays.asList(documents)); - } - - /** - * It starts the first step of {@link DocumentDelete} API using a fluent-API way. - * Once there is no field, it will remove the whole record instead of some fields on the database. - * - * @return a new {@link DocumentDelete} instance - */ - static DocumentDelete delete() { - return new DefaultFluentDeleteQueryBuilder(Collections.emptyList()); - } - - /** - * It starts the first step of {@link DocumentDeleteQuery} creation using a builder pattern. - * This first step will inform the fields to delete in the query instead of the whole record. - * This behavior might be different for each NoSQL database provider; therefore, it might be ignored for some implementations. - * - * @param documents the column fields to delete query - * @return a {@link DocumentDeleteQueryBuilder} instance - * @throws NullPointerException when there is a null element - */ - static DocumentDeleteQueryBuilder builder(String... documents) { - Stream.of(documents).forEach(d -> requireNonNull(d, "there is null document in the query")); - DefaultDocumentDeleteQueryBuilder builder = new DefaultDocumentDeleteQueryBuilder(); - Stream.of(documents).forEach(builder::delete); - return builder; - } - - /** - * It starts the first step of {@link DocumentDeleteQuery} creation using a builder pattern. - * Once there is no field, it will remove the whole record instead of some fields on the database. - * - * @return a {@link DocumentDeleteQueryBuilder} instance - */ - static DocumentDeleteQueryBuilder builder() { - return new DefaultDocumentDeleteQueryBuilder(); - } - - - /** - * The initial element in the Document delete query - */ - interface DocumentDelete { - - /** - * Defines the document collection in the delete query - * - * @param documentCollection the document collection to query - * @return a {@link DocumentDeleteFrom query} - * @throws NullPointerException when documentCollection is null - */ - DocumentDeleteFrom from(String documentCollection); - - } - - /** - * A provider class of {@link DocumentDelete} - */ - interface DocumentDeleteProvider extends Function, Supplier { - } - - - - /** - * The Document Delete Query - */ - interface DocumentDeleteFrom extends DocumentDeleteQueryBuild { - - - /** - * Starts a new condition defining the column name - * - * @param name the column name - * @return a new {@link DocumentDeleteNameCondition} - * @throws NullPointerException when name is null - */ - DocumentDeleteNameCondition where(String name); - - } - - /** - * The base to delete name condition - */ - interface DocumentDeleteNameCondition { - - /** - * Creates the equals condition - * - * @param value the value to the condition - * @param the type - * @return the {@link DocumentDeleteWhere} - * @throws NullPointerException when value is null - */ - DocumentDeleteWhere eq(T value); - - /** - * Creates the like condition - * - * @param value the value to the condition - * @return the {@link DocumentDeleteWhere} - * @throws NullPointerException when value is null - */ - DocumentDeleteWhere like(String value); - - /** - * Creates the greater than condition - * - * @param the type - * @param value the value to the condition - * @return the {@link DocumentDeleteWhere} - * @throws NullPointerException when value is null - */ - DocumentDeleteWhere gt(T value); - - /** - * Creates the greater equals than condition - * - * @param the type - * @param value the value to the condition - * @return the {@link DocumentDeleteWhere} - * @throws NullPointerException when value is null - */ - DocumentDeleteWhere gte(T value); - - /** - * Creates the lesser than condition - * - * @param the type - * @param value the value to the condition - * @return the {@link DocumentDeleteWhere} - * @throws NullPointerException when value is null - */ - DocumentDeleteWhere lt(T value); - - /** - * Creates the lesser equals than condition - * - * @param the type - * @param value the value to the condition - * @return the {@link DocumentDeleteWhere} - * @throws NullPointerException when value is null - */ - DocumentDeleteWhere lte(T value); - - /** - * Creates the between condition - * - * @param the type - * @param valueA the values within a given range - * @param valueB the values within a given range - * @return the {@link DocumentDeleteWhere} - * @throws NullPointerException when either valueA or valueB are null - */ - DocumentDeleteWhere between(T valueA, T valueB); - - /** - * Creates in condition - * - * @param values the values - * @param the type - * @return the {@link DocumentDeleteWhere} - * @throws NullPointerException when value is null - */ - DocumentDeleteWhere in(Iterable values); - - /** - * Creates the equals condition - * - * @return {@link DocumentDeleteNotCondition} - */ - DocumentDeleteNotCondition not(); - - } - - /** - * The document not condition - */ - interface DocumentDeleteNotCondition extends DocumentDeleteNameCondition { - } - - /** - * The last step to the build of {@link DocumentDeleteQuery}. - * It either can return a new {@link DocumentDeleteQuery} instance or execute a query with - * {@link DocumentManager} - */ - interface DocumentDeleteQueryBuild { - - /** - * Creates a new instance of {@link DocumentDeleteQuery} - * - * @return a new {@link DocumentDeleteQuery} instance - */ - DocumentDeleteQuery build(); - - /** - * executes the {@link DocumentManager#delete(DocumentDeleteQuery)} - * - * @param manager the entity manager - * @throws NullPointerException when manager is null - */ - void delete(DocumentManager manager); - - } - - /** - * The Document Where whose define the condition in the delete query. - */ - interface DocumentDeleteWhere extends DocumentDeleteQueryBuild { - - - /** - * Starts a new condition in the select using {@link DocumentCondition#and(DocumentCondition)} - * - * @param name a condition to be added - * @return the same {@link DocumentDeleteNameCondition} with the condition appended - * @throws NullPointerException when condition is null - */ - DocumentDeleteNameCondition and(String name); - - /** - * Starts a new condition in the select using {@link DocumentCondition#or(DocumentCondition)} - * - * @param name a condition to be added - * @return the same {@link DocumentDeleteNameCondition} with the condition appended - * @throws NullPointerException when condition is null - */ - DocumentDeleteNameCondition or(String name); - - } - - /** - * Besides the fluent-API with the select {@link DocumentDeleteQuery#delete()}, the API also has support for creating - * a {@link DocumentDeleteQuery} instance using a builder pattern. - * The goal is the same; however, it provides more possibilities, such as more complex queries. - *

- * The goal is the same; however, it provides more possibilities, such as more complex queries. - * The DocumentQueryBuilder is not brighter than a fluent-API; it has the same validation in the creation method. - * It is a mutable and non-thread-safe class. - */ - interface DocumentDeleteQueryBuilder { - /** - * Append a new document in to delete query. - * It informs the fields to delete in the query instead of the whole record. - * This behavior might be different for each NoSQL database provider; therefore, it might be ignored for some implementations. - * - * @param document a column field to delete query - * @return the {@link DocumentDeleteQueryBuilder} - * @throws NullPointerException when the document is null - */ - DocumentDeleteQueryBuilder delete(String document); - - /** - * Append a new document in to delete query. - * This first step will inform the fields to delete in the query instead of the whole record. - * This behavior might be different for each NoSQL database provider; therefore, it might be ignored for some implementations. - * - * @param documents The column fields to delete query - * @return the {@link DocumentDeleteQueryBuilder} - * @throws NullPointerException when there is a null element - */ - DocumentDeleteQueryBuilder delete(String... documents); - - /** - * Define the document collection in the query, this element - * is mandatory to build the {@link DocumentDeleteQuery} - * - * @param documentCollection the document collection to query - * @return the {@link DocumentDeleteQueryBuilder} - * @throws NullPointerException when documentCollection is null - */ - DocumentDeleteQueryBuilder from(String documentCollection); - - /** - * Either add or replace the condition in the query. It has a different behavior than the previous method - * because it won't append it. Therefore, it will create when it is the first time or replace when it was executed once. - * - * @param condition the {@link DocumentCondition} in the query - * @return the {@link DocumentDeleteQueryBuilder} - * @throws NullPointerException when condition is null - */ - DocumentDeleteQueryBuilder where(DocumentCondition condition); - - /** - * It will validate and then create a {@link DocumentDeleteQuery} instance. - * - * @return {@link DocumentDeleteQuery} - * @throws IllegalStateException It returns a state exception when an element is not valid or not fill-up, - * such as the {@link DocumentDeleteQueryBuilder#from(String)} method was not called. - */ - DocumentDeleteQuery build(); - - /** - * executes the {@link DocumentManager#delete(DocumentDeleteQuery)} - * - * @param manager the entity manager - * @throws NullPointerException when manager is null - * @throws IllegalStateException It returns a state exception when an element is not valid or not fill-up, - * such as the {@link DocumentDeleteQueryBuilder#from(String)} method was not called. - */ - void delete(DocumentManager manager); - } -} diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentDeleteQueryParams.java b/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentDeleteQueryParams.java deleted file mode 100644 index aeb9728d4..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentDeleteQueryParams.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - -import org.eclipse.jnosql.communication.Params; - -import java.util.Objects; - -/** - * The result of {@link DocumentDeleteQueryParams} that has {@link DocumentDeleteQuery} and {@link Params}. - */ -public final class DocumentDeleteQueryParams { - - private final DocumentDeleteQuery query; - - private final Params params; - - DocumentDeleteQueryParams(DocumentDeleteQuery query, Params params) { - this.query = query; - this.params = params; - } - - /** - * The {@link DocumentDeleteQuery} - * - * @return a {@link DocumentDeleteQuery} instance - */ - public DocumentDeleteQuery query() { - return query; - } - - /** - * The {@link Params} - * - * @return a {@link Params} instance - */ - public Params params() { - return params; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - DocumentDeleteQueryParams that = (DocumentDeleteQueryParams) o; - return Objects.equals(query, that.query) && - Objects.equals(params, that.params); - } - - @Override - public int hashCode() { - return Objects.hash(query, params); - } -} diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentEntity.java b/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentEntity.java deleted file mode 100644 index 4224675ca..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentEntity.java +++ /dev/null @@ -1,346 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - - - -import org.eclipse.jnosql.communication.TypeSupplier; -import org.eclipse.jnosql.communication.Value; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; -import java.util.Set; - -import static java.util.Collections.singletonMap; -import static java.util.Collections.unmodifiableSet; -import static java.util.Comparator.comparing; -import static java.util.Objects.requireNonNull; -import static java.util.Optional.ofNullable; -import static java.util.stream.Collectors.collectingAndThen; -import static java.util.stream.Collectors.toList; - -/** - * The communication level entity. It is the API entity between the database and the Jakarta NoSQL communication level. - * It represents a document collection. - * Each DocumentEntity has a name and one or more {@link Document}. - * - * @see Document - * @see DocumentEntity#documents() - * @see DocumentEntity#name() - */ -public class DocumentEntity { - - private final Map documents = new HashMap<>(); - - private final String name; - - DocumentEntity(String name) { - this.name = name; - } - - /** - * The collection name to {@link DocumentEntity} - * - * @return collection name - */ - public String name() { - return name; - } - - /** - * Remove a Document whose name is informed in parameter. - * - * @param documentName a document name - * @return if a column was removed or not - * @throws NullPointerException when documentName is null - */ - public boolean remove(String documentName) { - requireNonNull(documentName, "documentName is required"); - return documents.remove(documentName) != null; - } - - /** - * List of all documents - * - * @return all documents - */ - public List documents() { - return documents.values() - .stream() - .collect(collectingAndThen(toList(), - Collections::unmodifiableList)); - } - - /** - * add a document within {@link DocumentEntity} - * - * @param document a document to be included - * @throws UnsupportedOperationException when this method is not supported - * @throws NullPointerException when document is null - */ - public void add(Document document) { - requireNonNull(document, "Document is required"); - this.documents.put(document.name(), document); - } - - /** - * add a document within {@link DocumentEntity} - * - * @param documentName a name of the document - * @param value the information of the document - * @throws UnsupportedOperationException when this method is not supported - * @throws NullPointerException when name is null - */ - public void add(String documentName, Object value) { - requireNonNull(documentName, "documentName is required"); - this.documents.put(documentName, Document.of(documentName, Value.of(value))); - } - - /** - * add a document within {@link DocumentEntity} - * - * @param documentName a name of the document - * @param value the information of the document - * @throws UnsupportedOperationException when this method is not supported - * @throws NullPointerException when either name or value are null - */ - public void add(String documentName, Value value) { - requireNonNull(documentName, "documentName is required"); - this.documents.put(documentName, Document.of(documentName, value)); - } - - /** - * add all documents within {@link DocumentEntity} - * - * @param documents documents to be included - * @throws UnsupportedOperationException when this method is not supported - * @throws NullPointerException when document is null - */ - public void addAll(Iterable documents) { - requireNonNull(documents, "documents are required"); - documents.forEach(this::add); - } - - /** - * Find document from document name - * - * @param documentName a name of a document - * @return an {@link Optional} instance with the result - * @throws NullPointerException when documentName is null - */ - public Optional find(String documentName) { - requireNonNull(documentName, "documentName is required"); - Document document = documents.get(documentName); - return ofNullable(document); - } - - /** - * Find a document and converts to specific type from {@link Class}. - * It is an alias to {@link Value#get(Class)} - * - * @param documentName a name of a document - * @param type the type to convert the value - * @return an {@link Optional} instance with the result - * @throws NullPointerException when there are null parameters - */ - public Optional find(String documentName, Class type) { - Objects.requireNonNull(documentName, "documentName is required"); - Objects.requireNonNull(type, "type is required"); - return ofNullable(documents.get(documentName)) - .map(d -> d.get(type)); - } - - /** - * Find a document and converts to specific type from {@link TypeSupplier}. - * It is an alias to {@link Value#get(TypeSupplier)} - * - * @param documentName a name of a document - * @param type the type to convert the value - * @return a new instance converted to informed class - * @throws NullPointerException when there are null parameters - */ - public Optional find(String documentName, TypeSupplier type) { - Objects.requireNonNull(documentName, "documentName is required"); - Objects.requireNonNull(type, "type is required"); - return ofNullable(documents.get(documentName)).map(d -> d.get(type)); - } - - /** - * Returns the number of elements in this list. - * - * @return the number of elements in this list - */ - public int size() { - return documents.size(); - } - - /** - * Returns true if this list contains no elements. - * - * @return true if this list contains no elements - */ - public boolean isEmpty() { - return documents.isEmpty(); - } - - /** - * make copy of itself - * - * @return an instance copy - */ - public DocumentEntity copy() { - DocumentEntity entity = new DocumentEntity(this.name); - entity.documents.putAll(new HashMap<>(this.documents)); - return entity; - } - - /** - * Removes all Documents - */ - public void clear() { - this.documents.clear(); - } - - /** - * Returns a Set view of the names of document contained in Document Entity - * - * @return the keys - */ - public Set getDocumentNames() { - return unmodifiableSet(documents.keySet()); - } - - /** - * Returns a Collection view of the values contained in this DocumentEntity. - * - * @return the collection of values - */ - public Collection getValues() { - return documents - .values() - .stream() - .map(Document::value) - .collect(collectingAndThen(toList(), Collections::unmodifiableList)); - } - - /** - * Returns true if this DocumentEntity contains a document whose the name is informed - * - * @param documentName the document name - * @return true if find a document and otherwise false - */ - public boolean contains(String documentName) { - requireNonNull(documentName, "documentName is required"); - return documents.containsKey(documentName); - } - - /** - * Converts the columns to a Map where: - * the key is the name the column - * The value is the {@link Value#get()} of the map - * - * @return a map instance - */ - public Map toMap() { - Map map = new HashMap<>(); - for (Map.Entry entry : documents.entrySet()) { - Document document = entry.getValue(); - map.put(entry.getKey(), convert(document.get())); - } - return Collections.unmodifiableMap(map); - } - - private Object convert(Object value) { - if (value instanceof Document) { - Document column = Document.class.cast(value); - return singletonMap(column.name(), convert(column.get())); - } else if (value instanceof Iterable) { - List list = new ArrayList<>(); - Iterable.class.cast(value).forEach(e -> list.add(convert(e))); - return list; - } - return value; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (!(o instanceof DocumentEntity that)) { - return false; - } - return Objects.equals(this.documents().stream().sorted(comparing(Document::name)).collect(toList()), - that.documents().stream().sorted(comparing(Document::name)).collect(toList())) && - Objects.equals(name, that.name()); - } - - @Override - public int hashCode() { - return Objects.hash(documents, name); - } - - @Override - public String toString() { - return "DefaultDocumentEntity{" + "documents=" + documents + - ", name='" + name + '\'' + - '}'; - } - - /** - * Creates a {@link DocumentEntity} instance - * - * @param name the name of the collection - * @return a {@link DocumentEntity} instance - * @throws NullPointerException when name is null - */ - public static DocumentEntity of(String name) { - return new DocumentEntity(requireNonNull(name, "name is required")); - } - - /** - * Creates a {@link DocumentEntity} instance - * - * @param name the collection name - * @param documents the initial document inside {@link DocumentEntity} - * @return a {@link DocumentEntity} instance - * @throws NullPointerException when either name or documents are null - */ - public static DocumentEntity of(String name, List documents) { - requireNonNull(documents, "documents is required"); - DocumentEntity entity = new DocumentEntity(requireNonNull(name, "name is required")); - entity.addAll(documents); - return entity; - } - - /** - * Adds a document with a null value to the collection of documents. - * - * @param name the name of the document to add; must not be {@code null} - * @throws NullPointerException if the provided {@code name} is {@code null} - */ - public void addNull(String name) { - requireNonNull(name, "name is required"); - this.documents.put(name, Document.of(name, Value.ofNull())); - } -} diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentManager.java b/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentManager.java deleted file mode 100644 index bba3c9f7d..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentManager.java +++ /dev/null @@ -1,214 +0,0 @@ -/* - * - * Copyright (c) 2023 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - - -import jakarta.data.exceptions.NonUniqueResultException; - -import java.time.Duration; -import java.util.Iterator; -import java.util.Objects; -import java.util.Optional; -import java.util.stream.Stream; - -/** - * The manager instance bridges the Jakarta NoSQL and the NoSQL vendor. - * - * @see DocumentEntity - */ -public interface DocumentManager extends AutoCloseable { - - /** - * Returns the database's name of this {@link DocumentManager} - * - * @return the database's name - */ - String name(); - - /** - * Saves document collection entity - * - * @param entity entity to be saved - * @return the entity saved - * @throws NullPointerException when document is null - */ - DocumentEntity insert(DocumentEntity entity); - - /** - * Saves document collection entity with time to live - * - * @param entity entity to be saved - * @param ttl the time to live - * @return the entity saved - * @throws NullPointerException when either entity or ttl are null - * @throws UnsupportedOperationException when the database does not support this feature - */ - DocumentEntity insert(DocumentEntity entity, Duration ttl); - - /** - * Saves documents collection entity, by default it's just run for each saving using - * {@link DocumentManager#insert(DocumentEntity)}, - * each NoSQL vendor might replace to a more appropriate one. - * - * @param entities entities to be saved - * @return the entity saved - * @throws NullPointerException when entities is null - */ - Iterable insert(Iterable entities); - - /** - * Saves documents collection entity with time to live, by default it's just run for each saving using - * {@link DocumentManager#insert(DocumentEntity, Duration)}, - * each NoSQL vendor might replace to a more appropriate one. - * - * @param entities entities to be saved - * @param ttl time to live - * @return the entity saved - * @throws NullPointerException when entities is null - * @throws UnsupportedOperationException when the database does not support this feature - */ - Iterable insert(Iterable entities, Duration ttl); - - /** - * Updates an entity - * - * @param entity entity to be updated - * @return the entity updated - * @throws NullPointerException when entity is null - */ - DocumentEntity update(DocumentEntity entity); - - /** - * Updates documents collection entity, by default it's just run for each saving using - * {@link DocumentManager#update(DocumentEntity)}, - * each NoSQL vendor might replace to a more appropriate one. - * - * @param entities entities to be saved - * @return the entity saved - * @throws NullPointerException when entities is null - */ - Iterable update(Iterable entities); - - /** - * Deletes an entity - * - * @param query select to delete an entity - * @throws NullPointerException when select is null - * @throws UnsupportedOperationException if the implementation does not support any operation that a query has. - */ - void delete(DocumentDeleteQuery query); - - /** - * Finds {@link DocumentEntity} from select - * - * @param query - select to figure out entities - * @return entities found by select - * @throws NullPointerException when select is null - * @throws UnsupportedOperationException if the implementation does not support any operation that a query has. - */ - Stream select(DocumentQuery query); - - /** - * Returns the number of items in the collection that match a specified query. - * @param query the query - * @return the number of documents from query - * @throws NullPointerException when query is null - */ - default long count(DocumentQuery query) { - Objects.requireNonNull(query, "query is required"); - return this.select(DefaultDocumentQuery.countBy(query)).count(); - } - - /** - * Returns whether an entity that match a specified query. - * @param query the query - * @return true if an entity with the given query exists, false otherwise. - * @throws NullPointerException when query it null - */ - default boolean exists(DocumentQuery query) { - Objects.requireNonNull(query, "query is required"); - return this.select(DefaultDocumentQuery.existsBy(query)).findAny().isPresent(); - } - - /** - * Executes a query and returns the result, when the operations are insert, update and select - * command it will return the result of the operation when the command is delete it will return an empty collection. - * - * @param query the query as {@link String} - * @return the result of the operation if delete it will always return an empty list - * @throws NullPointerException when there is parameter null - * @throws IllegalArgumentException when the query has value parameters - */ - default Stream query(String query) { - Objects.requireNonNull(query, "query is required"); - DocumentQueryParser parser = new DocumentQueryParser(); - return parser.query(query, this, DocumentObserverParser.EMPTY); - } - - /** - * Executes a query and returns the result, when the operations are insert, update and select - * command it will return the result of the operation when the command is delete it will return an empty collection. - * - * @param query the query as {@link String} - * @return a {@link DocumentPreparedStatement} instance - * @throws NullPointerException when there is parameter null - * @throws IllegalStateException when there is not {@link DocumentQueryParser} - */ - default DocumentPreparedStatement prepare(String query) { - Objects.requireNonNull(query, "query is required"); - DocumentQueryParser parser = new DocumentQueryParser(); - return parser.prepare(query, this, DocumentObserverParser.EMPTY); - } - - /** - * Returns a single entity from select - * - * @param query - select to figure out entities - * @return an entity on {@link Optional} or {@link Optional#empty()} when the result is not found. - * @throws NullPointerException when select is null - * @throws UnsupportedOperationException if the implementation does not support any operation that a query has. - */ - default Optional singleResult(DocumentQuery query) { - Objects.requireNonNull(query, "query is required"); - Stream entities = select(query); - final Iterator iterator = entities.iterator(); - if (!iterator.hasNext()) { - return Optional.empty(); - } - final DocumentEntity entity = iterator.next(); - if (!iterator.hasNext()) { - return Optional.of(entity); - } - throw new NonUniqueResultException("The select returns more than one entity, select: " + query); - } - - /** - * Returns the number of elements from document collection - * - * @param documentCollection the document collection - * @return the number of elements - * @throws NullPointerException when document collection is null - * @throws UnsupportedOperationException when the database dot not have support - */ - long count(String documentCollection); - - /** - * closes a resource - */ - void close(); - -} diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentManagerFactory.java b/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentManagerFactory.java deleted file mode 100644 index 01a22b2b8..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentManagerFactory.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * - * Copyright (c) 2023 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - - -import java.util.function.Function; - -/** - * The manager factory instance creates a manager instance from the database name. - * It should return a {@link NullPointerException} when the {@link String} parameter is null. - * - * @see DocumentManager - */ -public interface DocumentManagerFactory extends Function, AutoCloseable { - - /** - * closes a resource - */ - void close(); -} diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentObserverParser.java b/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentObserverParser.java deleted file mode 100644 index 196edb17e..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentObserverParser.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * - * Copyright (c) 2023 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - -/** - * An observer to a parser; this observer allows checking both the name of an entity and the fields. - * This observer might be used to the mapper process. - */ -public interface DocumentObserverParser { - - DocumentObserverParser EMPTY = new DocumentObserverParser() { - - }; - - /** - * Fire an event to entity name - * - * @param entity the entity - * @return the field result - */ - default String fireEntity(String entity) { - return entity; - } - - /** - * Fire an event to each field in case of mapper process - * - * @param document the document - * @param entity the entity - * @return the field result - */ - default String fireField(String entity, String document) { - return document; - } - - -} diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentPreparedStatement.java b/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentPreparedStatement.java deleted file mode 100644 index 58a9e3653..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentPreparedStatement.java +++ /dev/null @@ -1,193 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - - -import jakarta.data.exceptions.NonUniqueResultException; -import org.eclipse.jnosql.communication.Params; -import org.eclipse.jnosql.communication.QueryException; - -import java.time.Duration; -import java.util.Iterator; -import java.util.List; -import java.util.Objects; -import java.util.Optional; -import java.util.stream.Stream; - - -/** - * An object that represents a precompiled Query statement. - */ -public final class DocumentPreparedStatement { - - private final DocumentEntity entity; - - private final DocumentQuery documentQuery; - - private final DocumentDeleteQuery documentDeleteQuery; - - private final PreparedStatementType type; - - private final Params params; - - private final String query; - - private final List paramsLeft; - - private final Duration duration; - - private final DocumentManager manager; - - private DocumentPreparedStatement(DocumentEntity entity, - DocumentQuery documentQuery, - DocumentDeleteQuery documentDeleteQuery, - PreparedStatementType type, - Params params, - String query, - List paramsLeft, - Duration duration, - DocumentManager manager) { - this.entity = entity; - this.documentQuery = documentQuery; - this.documentDeleteQuery = documentDeleteQuery; - this.type = type; - this.params = params; - this.query = query; - this.paramsLeft = paramsLeft; - this.manager = manager; - this.duration = duration; - } - - /** - * Binds an argument to a positional parameter. - * - * @param name the parameter name - * @param value the parameter value - * @return the same query instance - * @throws NullPointerException when there is null parameter - */ - public DocumentPreparedStatement bind(String name, Object value) { - Objects.requireNonNull(name, "name is required"); - Objects.requireNonNull(value, "value is required"); - - paramsLeft.remove(name); - params.bind(name, value); - return this; - } - - /** - * Executes a query and return the result as {@link Stream} - * - * @return The result stream, if delete it will return an empty list - */ - public Stream result() { - if (!paramsLeft.isEmpty()) { - throw new QueryException("Check all the parameters before execute the query, params left: " + paramsLeft); - } - switch (type) { - case SELECT -> { - return manager.select(documentQuery); - } - case DELETE -> { - manager.delete(documentDeleteQuery); - return Stream.empty(); - } - case UPDATE -> { - return Stream.of(manager.update(entity)); - } - case INSERT -> { - if (Objects.isNull(duration)) { - return Stream.of(manager.insert(entity)); - } else { - return Stream.of(manager.insert(entity, duration)); - } - } - default -> throw new UnsupportedOperationException("there is not support to operation type: " + type); - } - } - - /** - * Returns the result as a single element otherwise it will return an {@link Optional#empty()} - * - * @return the single result - */ - public Optional singleResult() { - Stream entities = result(); - final Iterator iterator = entities.iterator(); - if (!iterator.hasNext()) { - return Optional.empty(); - } - final DocumentEntity next = iterator.next(); - if (!iterator.hasNext()) { - return Optional.of(next); - } - throw new NonUniqueResultException("The select returns more than one entity, select: " + query); - } - - enum PreparedStatementType { - SELECT, DELETE, UPDATE, INSERT - } - - - @Override - public String toString() { - return query; - } - - static DocumentPreparedStatement select( - DocumentQuery documentQuery, - Params params, - String query, - DocumentManager manager) { - return new DocumentPreparedStatement(null, documentQuery, - null, PreparedStatementType.SELECT, params, query, - params.getParametersNames(), null, manager); - - } - - static DocumentPreparedStatement delete(DocumentDeleteQuery documentDeleteQuery, - Params params, - String query, - DocumentManager manager) { - - return new DocumentPreparedStatement(null, null, - documentDeleteQuery, PreparedStatementType.DELETE, params, query, - params.getParametersNames(), null, manager); - - } - - static DocumentPreparedStatement insert(DocumentEntity entity, - Params params, - String query, - Duration duration, - DocumentManager manager) { - return new DocumentPreparedStatement(entity, null, - null, PreparedStatementType.INSERT, params, query, - params.getParametersNames(), duration, manager); - - } - - static DocumentPreparedStatement update(DocumentEntity entity, - Params params, - String query, - DocumentManager manager) { - return new DocumentPreparedStatement(entity, null, - null, PreparedStatementType.UPDATE, params, query, - params.getParametersNames(), null, manager); - - } -} diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentQuery.java b/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentQuery.java deleted file mode 100644 index 45ddcbfc7..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentQuery.java +++ /dev/null @@ -1,614 +0,0 @@ -/* - * - * Copyright (c) 2023 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - - - -import jakarta.data.Sort; - -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Optional; -import java.util.function.Function; -import java.util.function.Supplier; -import java.util.stream.Stream; - -import static java.util.Objects.requireNonNull; - -/** - * Class that contains information to do a select to {@link DocumentManager} - * - * @see DocumentManager#select(DocumentQuery) - * @see DocumentCondition - * @see Sort - */ -public interface DocumentQuery { - - - /** - * @return The maximum number of results the select object was set to retrieve. - * The implementation might ignore this option. - */ - long limit(); - - /** - * @return The position of the first result the select object was set to retrieve. - * The implementation might ignore this option. - */ - long skip(); - - - /** - * The document collection name - * - * @return the document collection name - */ - String name(); - - /** - * The conditions that contains in this {@link DocumentQuery} - * If empty, {@link Optional#empty()} is true, the implementation might either return an unsupported exception or returns same elements in the database. - * - * @return the conditions - */ - Optional condition(); - - /** - * The sorts that contains in this {@link DocumentQuery} - * The implementation might ignore this option. - * - * @return the sorts - */ - List> sorts(); - - /** - * Returns the documents to returns in that query if empty will return all elements in the query. - * The implementation might ignore this option. - * - * @return the documents - */ - List documents(); - - /** - * It starts the first step of {@link DocumentQuery} creation using a fluent-API way. - * This first step will inform the fields to return to the query, such as a "select field, fieldB from database" - * in a database query. - * - * @param documents - The document fields to query, optional. - * @return a new {@link DocumentSelect} instance - * @throws NullPointerException when there is a null element - */ - static DocumentSelect select(String... documents) { - Stream.of(documents).forEach(d -> requireNonNull(d, "there is null document in the query")); - return new DefaultFluentDocumentQueryBuilder(Arrays.asList(documents)); - } - - /** - * It starts the first step of {@link DocumentQuery} creation using a fluent-API way. - * This first step will inform the fields to return to the query, such as a "select field, fieldB from database" in a database query. - * Once empty, it will return all elements in the query, similar to "select * from database" in a database query. - * - * @return a new {@link DocumentSelect} instance - */ - static DocumentSelect select() { - return new DefaultFluentDocumentQueryBuilder(Collections.emptyList()); - } - - /** - * It starts the first step of {@link DocumentQuery} creation using a builder pattern. - * This first step will inform the fields to return to the query, such as a "select field, fieldB from database" in a database query. - * - * @return {@link DocumentQueryBuilder} instance - */ - static DocumentQueryBuilder builder() { - return new DefaultDocumentQueryBuilder(); - } - - /** - * It starts the first step of {@link DocumentQuery} creation using a builder pattern. - * This first step will inform the fields to return to the query, such as a "select field, fieldB from database" in a database query. - * Once empty, it will return all elements in the query, similar to "select * from database" in a database query. - * - * @param documents The document fields to query, optional. - * @return {@link DocumentQueryBuilder} instance - */ - static DocumentQueryBuilder builder(String... documents) { - Stream.of(documents).forEach(d -> requireNonNull(d, "there is null document in the query")); - DefaultDocumentQueryBuilder builder = new DefaultDocumentQueryBuilder(); - Stream.of(documents).forEach(builder::select); - return builder; - } - - /** - * The DocumentFrom Query - */ - interface DocumentFrom extends DocumentQueryBuild { - - - /** - * Starts a new condition defining the column name - * - * @param name the column name - * @return a new {@link DocumentNameCondition} - * @throws NullPointerException when name is null - */ - DocumentNameCondition where(String name); - - /** - * Defines the position of the first result to retrieve. - * It will depend on the NoSQL vendor implementation, but it will discard or skip the search result. - * - * @param skip the first result to retrieve - * @return a query with first result defined - * @throws IllegalArgumentException if skip is negative - */ - DocumentSkip skip(long skip); - - - /** - * Defines the maximum number of results to retrieve. - * It will truncate to be no longer than limit. - * - * @param limit the limit - * @return a query with the limit defined - * @throws IllegalArgumentException if limit is negative - */ - DocumentLimit limit(long limit); - - /** - * Add the order how the result will return - * - * @param name the order - * @return a query with the sort defined - * @throws NullPointerException when name is null - */ - DocumentOrder orderBy(String name); - - - } - - /** - * A provider class of {@link DocumentSelect} - */ - interface DocumentSelectProvider extends Function, Supplier { - } - - /** - * A provider class of {@link DocumentQueryBuilder} - */ - interface DocumentQueryBuilderProvider extends Function, Supplier { - - } - - - /** - * The Document Order whose define the maximum number of results to retrieve. - */ - interface DocumentLimit extends DocumentQueryBuild { - - /** - * Defines the position of the first result to retrieve. - * It will depend on the NoSQL vendor implementation, but it will discard or skip the search result. - * - * @param skip the first result to retrieve - * @return a query with first result defined - * @throws IllegalArgumentException if skip is negative - */ - DocumentSkip skip(long skip); - - } - - /** - * The base to name condition - */ - interface DocumentNameCondition { - - - /** - * Creates the equals condition - * - * @param value the value to the condition - * @param the type - * @return the {@link DocumentWhere} - * @throws NullPointerException when value is null - */ - DocumentWhere eq(T value); - - /** - * Creates the like condition - * - * @param value the value to the condition - * @return the {@link DocumentWhere} - * @throws NullPointerException when value is null - */ - DocumentWhere like(String value); - - /** - * Creates the greater than condition - * - * @param the type - * @param value the value to the condition - * @return the {@link DocumentWhere} - * @throws NullPointerException when value is null - */ - DocumentWhere gt(T value); - - /** - * Creates the greater equals than condition - * - * @param the type - * @param value the value to the condition - * @return the {@link DocumentWhere} - * @throws NullPointerException when value is null - */ - DocumentWhere gte(T value); - - /** - * Creates the lesser than condition - * - * @param the type - * @param value the value to the condition - * @return the {@link DocumentWhere} - * @throws NullPointerException when value is null - */ - DocumentWhere lt(T value); - - /** - * Creates the lesser equals than condition - * - * @param the type - * @param value the value to the condition - * @return the {@link DocumentWhere} - * @throws NullPointerException when value is null - */ - DocumentWhere lte(T value); - - /** - * Creates the between condition - * - * @param the type - * @param valueA the values within a given range - * @param valueB the values within a given range - * @return the {@link DocumentWhere} - * @throws NullPointerException when either valueA or valueB are null - */ - DocumentWhere between(T valueA, T valueB); - - /** - * Creates in condition - * - * @param values the values - * @param the type - * @return the {@link DocumentWhere} - * @throws NullPointerException when value is null - */ - DocumentWhere in(Iterable values); - - /** - * Creates the equals condition - * - * @return {@link DocumentNotCondition} - */ - DocumentNotCondition not(); - } - - /** - * The Column name order to the builder - */ - interface DocumentNameOrder extends DocumentQueryBuild { - - /** - * Add the order how the result will return - * - * @param name the name to be ordered - * @return a query with the sort defined - * @throws NullPointerException when name is null - */ - DocumentOrder orderBy(String name); - - - /** - * Defines the position of the first result to retrieve. - * It will depend on the NoSQL vendor implementation, but it will discard or skip the search result. - * - * @param skip the first result to retrieve - * @return a query with first result defined - * @throws IllegalArgumentException if skip is negative - */ - DocumentSkip skip(long skip); - - - /** - * Defines the maximum number of results to retrieve. - * It will truncate to be no longer than limit. - * - * @param limit the limit - * @return a query with the limit defined - * @throws IllegalArgumentException if limit is negative - */ - DocumentLimit limit(long limit); - - - } - - /** - * The column not condition - */ - interface DocumentNotCondition extends DocumentNameCondition { - } - - /** - * The initial element in the Document query - */ - interface DocumentSelect { - - /** - * Defines the document collection in the query - * - * @param documentCollection the document collection to query - * @return a {@link DocumentFrom query} - * @throws NullPointerException when documentCollection is null - */ - DocumentFrom from(String documentCollection); - } - - /** - * The Document Order whose define the position of the first result to retrieve. - */ - interface DocumentSkip extends DocumentQueryBuild { - - /** - * Defines the maximum number of results to retrieve. - * It will truncate to be no longer than limit. - * - * @param limit the limit - * @return a query with the limit defined - * @throws IllegalArgumentException if limit is negative - */ - DocumentLimit limit(long limit); - - } - - /** - * The Document Where whose define the condition in the query. - */ - interface DocumentWhere extends DocumentQueryBuild { - - - /** - * Starts a new condition in the select using {@link DocumentCondition#and(DocumentCondition)} - * - * @param name a condition to be added - * @return the same {@link DocumentNameCondition} with the condition appended - * @throws NullPointerException when condition is null - */ - DocumentNameCondition and(String name); - - /** - * Appends a new condition in the select using {@link DocumentCondition#or(DocumentCondition)} - * - * @param name a condition to be added - * @return the same {@link DocumentNameCondition} with the condition appended - * @throws NullPointerException when condition is null - */ - DocumentNameCondition or(String name); - - /** - * Defines the position of the first result to retrieve. - * It will depend on the NoSQL vendor implementation, but it will discard or skip the search result. - * - * @param skip the first result to retrieve - * @return a query with first result defined - * @throws IllegalArgumentException if skip is negative - */ - DocumentSkip skip(long skip); - - - /** - * Defines the maximum number of results to retrieve. - * It will truncate to be no longer than limit. - * - * @param limit the limit - * @return a query with the limit defined - * @throws IllegalArgumentException if limit is negative - */ - DocumentLimit limit(long limit); - - /** - * Add the order how the result will return - * - * @param name the order - * @return a query with the sort defined - * @throws NullPointerException when name is null - */ - DocumentOrder orderBy(String name); - - } - - /** - * The Document Order whose define the sort in the query. - */ - interface DocumentOrder { - - /** - * Defines the order as {@link jakarta.data.Direction#ASC} - * - * @return the {@link DocumentNameOrder} instance - */ - DocumentNameOrder asc(); - - /** - * Defines the order as {@link jakarta.data.Direction#DESC} - * - * @return the {@link DocumentNameOrder} instance - */ - DocumentNameOrder desc(); - } - - /** - * The last step to the build of {@link DocumentQuery}. - * It either can return a new {@link DocumentQuery} instance or execute a query with - * {@link DocumentManager} - */ - interface DocumentQueryBuild { - - /** - * Creates a new instance of {@link DocumentQuery} - * - * @return a new {@link DocumentQuery} instance - */ - DocumentQuery build(); - - /** - * Executes {@link DocumentManager#select(DocumentQuery)} - * - * @param manager the entity manager - * @return the result of {@link DocumentManager#select(DocumentQuery)} - * @throws NullPointerException when manager is null - */ - Stream getResult(DocumentManager manager); - - /** - * Executes {@link DocumentManager#singleResult(DocumentQuery)} - * - * @param manager the entity manager - * @return the result of {@link DocumentManager#singleResult(DocumentQuery)} - * @throws NullPointerException when manager is null - */ - Optional getSingleResult(DocumentManager manager); - - } - - /** - * Besides, the fluent-API with the select method, the API also has support for creating a {@link DocumentQuery} instance using a builder pattern. - * The goal is the same; however, it provides more possibilities, such as more complex queries. - * The DocumentQueryBuilder is not brighter than a fluent-API; it has the same validation in the creation method. - * It is a mutable and non-thread-safe class. - */ - interface DocumentQueryBuilder { - /** - * Append a new document in the search result. The query will return the result by elements declared such as "select column from database" - * If it remains empty, it will return all the possible fields, similar to "select * from database" - * - * @param document a field to return to the search - * @return the {@link DocumentQueryBuilder} - * @throws NullPointerException when the document is null - */ - DocumentQueryBuilder select(String document); - - /** - * Append new documents in the search result. The query will return the result by elements declared such as "select column from database" - * If it remains empty, it will return all the possible fields, similar to "select * from database" - * - * @param documents a field to return to the search - * @return the {@link DocumentQueryBuilder} - * @throws NullPointerException when there is a null element - */ - DocumentQueryBuilder select(String... documents); - - /** - * Append a new sort in the query. The first one has more precedence than the next one. - * - * @param sort the {@link Sort} - * @return the {@link DocumentQueryBuilder} - * @throws NullPointerException when the sort is null - */ - DocumentQueryBuilder sort(Sort sort); - - /** - * Append sorts in the query. The first one has more precedence than the next one. - * - * @param sorts the array of {@link Sort} - * @return the {@link DocumentQueryBuilder} - * @throws NullPointerException when there is a null sort - */ - DocumentQueryBuilder sort(Sort... sorts); - - /** - * Define the document collection in the query, this element is mandatory to build the {@link DocumentQuery} - * - * @param documentCollection the document collection to query - * @return the {@link DocumentQueryBuilder} - * @throws NullPointerException when documentCollection is null - */ - DocumentQueryBuilder from(String documentCollection); - - /** - * Either add or replace the condition in the query. It has a different behavior than the previous method - * because it won't append it. Therefore, it will create when it is the first time or replace when it was executed once. - * - * @param condition the {@link DocumentCondition} in the query - * @return the {@link DocumentQueryBuilder} - * @throws NullPointerException when condition is null - */ - DocumentQueryBuilder where(DocumentCondition condition); - - /** - * Defines the position of the first result to retrieve. - * It will depend on the NoSQL vendor implementation, but it will discard or skip the search result. - * The default value is zero, and it will replace the current property. - * - * @param skip the first result to retrieve - * @return a query with first result defined - * @throws IllegalArgumentException if limit is negative - */ - DocumentQueryBuilder skip(long skip); - - /** - * Defines the maximum number of results to retrieve. - * It will truncate to be no longer than limit. - * The default value is zero, and it will replace the current property. - * - * @param limit the limit - * @return the {@link DocumentQueryBuilder} - * @throws IllegalArgumentException if limit is negative - */ - DocumentQueryBuilder limit(long limit); - - /** - * It will validate and then create a {@link DocumentQuery} instance. - * - * @return {@link DocumentQuery} - * @throws IllegalStateException It returns a state exception when an element is not valid or not fill-up, - * such as the {@link DocumentQueryBuilder#from(String)} method was not called. - */ - DocumentQuery build(); - - /** - * Executes {@link DocumentManager#select(DocumentQuery)} - * - * @param manager the entity manager - * @return the result of {@link DocumentManager#select(DocumentQuery)} - * @throws NullPointerException when manager is null - * @throws IllegalStateException It returns a state exception when an element is not valid or not fill-up, - * such as the {@link DocumentQueryBuilder#from(String)} method was not called. - */ - Stream getResult(DocumentManager manager); - - /** - * Executes {@link DocumentManager#singleResult(DocumentQuery)} - * - * @param manager the entity manager - * @return the result of {@link DocumentManager#singleResult(DocumentQuery)} - * @throws NullPointerException when manager is null - * @throws IllegalStateException It returns a state exception when an element is not valid or not fill-up, - * such as the {@link DocumentQueryBuilder#from(String)} method was not called. - */ - Optional getSingleResult(DocumentManager manager); - } -} diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentQueryParams.java b/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentQueryParams.java deleted file mode 100644 index adb9bd1cb..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentQueryParams.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - - -import org.eclipse.jnosql.communication.Params; - -import java.util.Objects; - -public final class DocumentQueryParams { - - private final DocumentQuery query; - - private final Params params; - - - DocumentQueryParams(DocumentQuery query, Params params) { - this.query = query; - this.params = params; - } - - /** - * The {@link DocumentQuery} - * - * @return a {@link DocumentQuery} instance - */ - public DocumentQuery query() { - return query; - } - - /** - * The {@link Params} - * - * @return a {@link Params} instance - */ - public Params params() { - return params; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - DocumentQueryParams that = (DocumentQueryParams) o; - return Objects.equals(query, that.query) && - Objects.equals(params, that.params); - } - - @Override - public int hashCode() { - return Objects.hash(query, params); - } -} diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentQueryParser.java b/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentQueryParser.java deleted file mode 100644 index 805abd2fc..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/DocumentQueryParser.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - - -import org.eclipse.jnosql.communication.QueryException; - -import java.util.Objects; -import java.util.stream.Stream; - - -/** - * A query parser to document database type, this class will convert a String to an operation in {@link DocumentManager}. - */ -public final class DocumentQueryParser { - - private final SelectQueryParser select = new SelectQueryParser(); - private final DeleteQueryParser delete = new DeleteQueryParser(); - private final InsertQueryParser insert = new InsertQueryParser(); - private final UpdateQueryParser update = new UpdateQueryParser(); - - /** - * Executes a query and returns the result, when the operations are insert, update and select - * command it will return the result of the operation when the command is delete it will return an empty collection. - * - * @param query the query as {@link String} - * @param collectionManager the collection manager - * @param observer the observer - * @return the result of the operation if delete it will always return an empty list - * @throws NullPointerException when there is parameter null - * @throws IllegalArgumentException when the query has value parameters - * @throws QueryException when there is error in the syntax - */ - public Stream query(String query, DocumentManager collectionManager, - DocumentObserverParser observer) { - validation(query, collectionManager, observer); - String command = query.substring(0, 6); - return switch (command) { - case "select" -> select.query(query, collectionManager, observer); - case "delete" -> delete.query(query, collectionManager, observer); - case "insert" -> insert.query(query, collectionManager, observer); - case "update" -> update.query(query, collectionManager, observer); - default -> - throw new QueryException(String.format("The command was not recognized at the query %s ", query)); - }; - } - - /** - * Executes a query and returns a {@link DocumentPreparedStatement}, when the operations are insert, update and select - * command it will return the result of the operation when the command is delete it will return an empty collection. - * - * @param query the query as {@link String} - * @param collectionManager the collection manager - * @param observer the observer - * @return a {@link DocumentPreparedStatement} instance - * @throws NullPointerException when there is parameter null - * @throws IllegalArgumentException when the query has value parameters - * @throws QueryException when there is error in the syntax - */ - public DocumentPreparedStatement prepare(String query, DocumentManager collectionManager, - DocumentObserverParser observer) { - - validation(query, collectionManager, observer); - String command = query.substring(0, 6); - - return switch (command) { - case "select" -> select.prepare(query, collectionManager, observer); - case "delete" -> delete.prepare(query, collectionManager, observer); - case "insert" -> insert.prepare(query, collectionManager, observer); - case "update" -> update.prepare(query, collectionManager, observer); - default -> - throw new QueryException(String.format("The command was not recognized at the query %s ", query)); - }; - } - - - private void validation(String query, DocumentManager collectionManager, DocumentObserverParser observer) { - Objects.requireNonNull(query, "query is required"); - Objects.requireNonNull(collectionManager, "collectionManager is required"); - Objects.requireNonNull(observer, "observer is required"); - if (query.length() < 6) { - throw new QueryException(String.format("The query %s is invalid", query)); - } - } -} diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/Documents.java b/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/Documents.java deleted file mode 100644 index 63477d7ce..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/Documents.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ - -package org.eclipse.jnosql.communication.document; - - -import org.eclipse.jnosql.communication.Value; - -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.function.Predicate; - -import static java.util.stream.Collectors.toList; -import static java.util.stream.StreamSupport.stream; - -/** - * This class has utilitarian class to {@link Document} - */ -public final class Documents { - - private static final Predicate> IS_VALUE_NULL = e -> Objects.nonNull(e.getValue()); - - private Documents() { - } - - /** - * An alias to {@link Document#of(String, Object)} - * - * @param name the name - * @param value the value - * @return the document instance - */ - public static Document of(String name, Object value) { - return Document.of(name, Value.of(value)); - } - - - /** - * Converts the map to {@link List} of {@link Document} - * - * @param values the map - * @return the list instance - * @throws NullPointerException when map is null - */ - public static List of(Map values) { - Objects.requireNonNull(values, "values is required"); - - return values.entrySet().stream() - .filter(IS_VALUE_NULL) - .map(e -> Document.of(e.getKey(), getValue(e.getValue()))) - .collect(toList()); - } - - private static Object getValue(Object value) { - - if (value instanceof Map) { - List list = Documents.of((Map.class.cast(value))); - if(list.size() == 1) { - return list.get(0); - } - return list; - } - if (value instanceof Iterable) { - return stream(Iterable.class.cast(value).spliterator(), false) - .map(Documents::getValue).collect(toList()); - } - return value; - } - -} diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/InsertQueryParser.java b/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/InsertQueryParser.java deleted file mode 100644 index 8f4e782c9..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/InsertQueryParser.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - - -import org.eclipse.jnosql.communication.Params; -import org.eclipse.jnosql.communication.QueryException; -import org.eclipse.jnosql.communication.query.InsertQuery; -import org.eclipse.jnosql.communication.query.InsertQueryConverter; -import org.eclipse.jnosql.communication.query.JSONQueryValue; -import org.eclipse.jnosql.communication.query.QueryCondition; - -import java.time.Duration; -import java.util.List; -import java.util.Optional; -import java.util.stream.Stream; - -final class InsertQueryParser extends ConditionQueryParser { - - - Stream query(String query, DocumentManager collectionManager, DocumentObserverParser observer) { - - InsertQueryConverter converter = new InsertQueryConverter(); - InsertQuery insertQuery = converter.apply(query); - - String collection = insertQuery.entity(); - Params params = Params.newParams(); - - DocumentEntity entity = getEntity(insertQuery, collection, params, observer); - - Optional ttl = insertQuery.ttl(); - if (params.isNotEmpty()) { - throw new QueryException("To run a query with a parameter use a PrepareStatement instead."); - } - return ttl.map(duration -> Stream.of(collectionManager.insert(entity, duration))) - .orElseGet(() -> Stream.of(collectionManager.insert(entity))); - } - - - DocumentPreparedStatement prepare(String query, DocumentManager collectionManager, DocumentObserverParser observer) { - - InsertQueryConverter converter = new InsertQueryConverter(); - InsertQuery insertQuery = converter.apply(query); - - String collection = observer.fireEntity(insertQuery.entity()); - Params params = Params.newParams(); - - Optional ttl = insertQuery.ttl(); - DocumentEntity entity = getEntity(insertQuery, collection, params, observer); - - return DocumentPreparedStatement.insert(entity, params, query, ttl.orElse(null), collectionManager); - - } - - private DocumentEntity getEntity(InsertQuery insertQuery, String collection, Params params, DocumentObserverParser observer) { - return getEntity(new InsertQueryConditionSupplier(insertQuery), collection, params, observer); - } - - - private record InsertQueryConditionSupplier(InsertQuery query) implements ConditionQuerySupplier { - - @Override - public List conditions() { - return query.conditions(); - } - - @Override - public Optional value() { - return query.value(); - } - } -} diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/JsonObjects.java b/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/JsonObjects.java deleted file mode 100644 index 3ac87c6c2..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/JsonObjects.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - - - -import jakarta.json.JsonObject; -import jakarta.json.bind.Jsonb; -import jakarta.json.bind.JsonbBuilder; -import java.util.List; -import java.util.Map; - -final class JsonObjects { - - private static final Jsonb JSON = JsonbBuilder.create(); - - private JsonObjects() { - } - - static List getDocuments(JsonObject jsonObject) { - Map map = JSON.fromJson(jsonObject.toString(), Map.class); - return Documents.of(map); - } - -} diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/SelectQueryParser.java b/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/SelectQueryParser.java deleted file mode 100644 index 8e6b7c746..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/SelectQueryParser.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - - -import jakarta.data.Direction; -import org.eclipse.jnosql.communication.Params; -import org.eclipse.jnosql.communication.QueryException; -import jakarta.data.Sort; -import org.eclipse.jnosql.communication.query.SelectQuery; -import org.eclipse.jnosql.communication.query.SelectQueryConverter; - -import java.util.List; -import java.util.Objects; -import java.util.function.BiFunction; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import static java.util.stream.Collectors.toList; - -public final class SelectQueryParser implements BiFunction { - - Stream query(String query, DocumentManager manager, DocumentObserverParser observer) { - DocumentQuery documentQuery = getDocumentQuery(query, observer); - return manager.select(documentQuery); - } - - - DocumentPreparedStatement prepare(String query, DocumentManager collectionManager, DocumentObserverParser observer) { - - Params params = Params.newParams(); - - SelectQueryConverter converter = new SelectQueryConverter(); - SelectQuery selectQuery = converter.apply(query); - - DocumentQuery documentQuery = getDocumentQuery(params, selectQuery, observer); - return DocumentPreparedStatement.select(documentQuery, params, query, collectionManager); - } - - - @Override - public DocumentQueryParams apply(SelectQuery selectQuery, DocumentObserverParser observer) { - Objects.requireNonNull(selectQuery, "selectQuery is required"); - Objects.requireNonNull(observer, "observer is required"); - Params params = Params.newParams(); - DocumentQuery columnQuery = getDocumentQuery(params, selectQuery, observer); - return new DocumentQueryParams(columnQuery, params); - } - - private DocumentQuery getDocumentQuery(String query, DocumentObserverParser observer) { - - SelectQueryConverter converter = new SelectQueryConverter(); - SelectQuery selectQuery = converter.apply(query); - String collection = observer.fireEntity(selectQuery.entity()); - long limit = selectQuery.limit(); - long skip = selectQuery.skip(); - List documents = selectQuery.fields().stream() - .map(f -> observer.fireField(collection, f)) - .collect(Collectors.toList()); - List> sorts = selectQuery.orderBy().stream().map(s -> toSort(s, observer, collection)) - .collect(toList()); - Params params = Params.newParams(); - DocumentCondition condition = selectQuery.where() - .map(c -> Conditions.getCondition(c, params, observer, collection)) - .orElse(null); - - if (params.isNotEmpty()) { - throw new QueryException("To run a query with a parameter use a PrepareStatement instead."); - } - return new DefaultDocumentQuery(limit, skip, collection, documents, sorts, condition); - } - - private DocumentQuery getDocumentQuery(Params params, SelectQuery selectQuery, DocumentObserverParser observer) { - - String collection = observer.fireEntity(selectQuery.entity()); - long limit = selectQuery.limit(); - long skip = selectQuery.skip(); - List documents = selectQuery.fields().stream() - .map(f -> observer.fireField(collection, f)) - .collect(Collectors.toList()); - - List> sorts = selectQuery.orderBy().stream().map(s -> toSort(s, observer, collection)).collect(toList()); - DocumentCondition condition = selectQuery.where() - .map(c -> Conditions.getCondition(c, params, observer, collection)).orElse(null); - - return new DefaultDocumentQuery(limit, skip, collection, documents, sorts, condition); - } - - private Sort toSort(Sort sort, DocumentObserverParser observer, String entity) { - return Sort.of(observer.fireField(entity, sort.property()), sort.isAscending() ? Direction.ASC : Direction.DESC, false); - } - - -} diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/UpdateQueryParser.java b/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/UpdateQueryParser.java deleted file mode 100644 index 7b69b8b86..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/UpdateQueryParser.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - -import org.eclipse.jnosql.communication.Params; -import org.eclipse.jnosql.communication.QueryException; -import org.eclipse.jnosql.communication.query.JSONQueryValue; -import org.eclipse.jnosql.communication.query.QueryCondition; -import org.eclipse.jnosql.communication.query.UpdateQuery; -import org.eclipse.jnosql.communication.query.UpdateQueryConverter; - -import java.util.List; -import java.util.Optional; -import java.util.stream.Stream; - -final class UpdateQueryParser extends ConditionQueryParser { - - - Stream query(String query, DocumentManager collectionManager, DocumentObserverParser observer) { - - UpdateQueryConverter converter = new UpdateQueryConverter(); - UpdateQuery updateQuery = converter.apply(query); - - Params params = Params.newParams(); - - DocumentEntity entity = getEntity(params, updateQuery, observer); - - if (params.isNotEmpty()) { - throw new QueryException("To run a query with a parameter use a PrepareStatement instead."); - } - return Stream.of(collectionManager.update(entity)); - } - - - DocumentPreparedStatement prepare(String query, DocumentManager collectionManager, DocumentObserverParser observer) { - - Params params = Params.newParams(); - - UpdateQueryConverter converter = new UpdateQueryConverter(); - UpdateQuery updateQuery = converter.apply(query); - - DocumentEntity entity = getEntity(params, updateQuery, observer); - return DocumentPreparedStatement.update(entity, params, query, collectionManager); - } - - private DocumentEntity getEntity(Params params, UpdateQuery updateQuery, DocumentObserverParser observer) { - String collection = observer.fireEntity(updateQuery.entity()); - return getEntity(new UpdateQueryConditionSupplier(updateQuery), collection, params, observer); - } - - private record UpdateQueryConditionSupplier(UpdateQuery query) implements ConditionQuerySupplier { - - @Override - public List conditions() { - return query.conditions(); - } - - @Override - public Optional value() { - return query.value(); - } - } - -} diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/Values.java b/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/Values.java deleted file mode 100644 index 40f8047f3..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/Values.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - - -import org.eclipse.jnosql.communication.Params; -import org.eclipse.jnosql.communication.QueryException; -import org.eclipse.jnosql.communication.Value; -import org.eclipse.jnosql.communication.query.ArrayQueryValue; -import org.eclipse.jnosql.communication.query.Function; -import org.eclipse.jnosql.communication.query.FunctionQueryValue; -import org.eclipse.jnosql.communication.query.JSONQueryValue; -import org.eclipse.jnosql.communication.query.ParamQueryValue; -import org.eclipse.jnosql.communication.query.QueryValue; -import org.eclipse.jnosql.communication.query.ValueType; - -import java.util.Arrays; -import java.util.stream.Stream; - -import static java.util.stream.Collectors.toList; - -final class Values { - - private Values() { - } - - static Object get(QueryValue value, Params parameters) { - - ValueType type = value.type(); - switch (type) { - case NUMBER, STRING, BOOLEAN -> { - return value.get(); - } - case PARAMETER -> { - return parameters.add(ParamQueryValue.class.cast(value).get()); - } - case ARRAY -> { - return Stream.of(ArrayQueryValue.class.cast(value).get()) - .map(v -> get(v, parameters)) - .collect(toList()); - } - case FUNCTION -> { - Function function = FunctionQueryValue.class.cast(value).get(); - String name = function.name(); - Object[] params = function.params(); - if ("convert".equals(name)) { - return Value.of(get(QueryValue.class.cast(params[0]), parameters)) - .get((Class) params[1]); - } - String message = String.format("There is not support to the function: %s with parameters %s", name, - Arrays.toString(params)); - throw new QueryException(message); - } - case JSON -> { - return JsonObjects.getDocuments(JSONQueryValue.class.cast(value).get()); - } - default -> throw new QueryException("There is not support to the value: " + type); - } - } -} diff --git a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/package-info.java b/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/package-info.java deleted file mode 100644 index 8ca1807a2..000000000 --- a/jnosql-communication/jnosql-communication-document/src/main/java/org/eclipse/jnosql/communication/document/package-info.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ - -/** - *This package contains all objects to use a Document Collection. This API going to focus in domain, in other words, - * ubiquitous language. - * A document-oriented database, or document store, - * is a computer program designed for storing, retrieving, and managing document-oriented information, - * also known as semi-structured data. Document-oriented databases are one of the main categories of NoSQL databases, - * and the popularity of the term "document-oriented database" has grown with the use of the term NoSQL itself. - * XML databases are a subclass of document-oriented databases that are optimized to work with XML documents. - * Graph databases are similar, but add another layer, the relationship, which allows them to link documents - * for rapid traversal. - * Document-oriented databases are inherently a subclass of the key-value store, another NoSQL database concept. - * The difference lies in the way the data is processed; in a key-value store the data is considered to be inherently - * opaque to the database, whereas a document-oriented system relies on internal structure in the document - * in order to extract - * metadata that the database engine uses for further optimization. Although the difference is often moot due to tools - * in the systems, - * conceptually the document-store is designed to offer a richer experience with modern programming techniques. - * Document databases contrast strongly with the traditional relational database (RDB). - * Relational databases generally store data in separate tables that are defined by the programmer, and a single object - * may be spread across several tables. - * Document databases store all information for a given object in a single instance in the database, and every stored - * object can be different from every other. - * This makes mapping objects into the database a simple task, normally eliminating anything similar to an - * object-relational mapping. This makes document stores attractive - * for programming web applications, which are subject to continual change in place, and where speed of deployment - * is an important issue. - */ -package org.eclipse.jnosql.communication.document; \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DefaultDocumentDeleteQueryTest.java b/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DefaultDocumentDeleteQueryTest.java deleted file mode 100644 index 313bfabc0..000000000 --- a/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DefaultDocumentDeleteQueryTest.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.eclipse.jnosql.communication.document.DocumentDeleteQuery.delete; -import static org.junit.jupiter.api.Assertions.assertTrue; - -class DefaultDocumentDeleteQueryTest { - - - private DocumentDeleteQuery query; - - - @BeforeEach - void setUp() { - query = delete().from("columnFamily").build(); - } - - @Test - void shouldNotEditColumns() { - Assertions.assertThrows(UnsupportedOperationException.class, () -> { - List documents = query.documents(); - assertTrue(documents.isEmpty()); - documents.clear(); - }); - } - - @Test - void shouldGenerateEquals() { - DocumentDeleteQuery query = delete().from("columnFamily").build(); - DocumentDeleteQuery query2 = delete().from("columnFamily").build(); - - assertThat(query).isEqualTo(query2); - assertThat(query).isEqualTo(query); - assertThat(query).isNotEqualTo("query"); - } - - @Test - void shouldGenerateHashCode() { - DocumentDeleteQuery query = delete().from("columnFamily").build(); - DocumentDeleteQuery query2 = delete().from("columnFamily").build(); - - assertThat(query.hashCode()).isEqualTo(query2.hashCode()); - } -} diff --git a/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DefaultDocumentQueryBuilderTest.java b/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DefaultDocumentQueryBuilderTest.java deleted file mode 100644 index c66be3727..000000000 --- a/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DefaultDocumentQueryBuilderTest.java +++ /dev/null @@ -1,307 +0,0 @@ -/* - * Copyright (c) 2023 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Maximillian Arruda - * - */ - -package org.eclipse.jnosql.communication.document; - -import jakarta.data.Sort; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import java.util.Optional; -import java.util.UUID; -import java.util.stream.Stream; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNotSame; -import static org.junit.jupiter.api.Assertions.assertSame; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.atLeastOnce; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -class DefaultDocumentQueryBuilderTest { - - private DefaultDocumentQueryBuilder builder; - - private String newRandomDocument() { - return UUID.randomUUID().toString(); - } - - private static Sort newRandomSortRef() { - return Sort.asc(UUID.randomUUID().toString()); - } - - private String newRandomDocumentCollection() { - return UUID.randomUUID().toString(); - } - - private DocumentCondition newRandomDocumentCondition() { - return DocumentCondition.eq(UUID.randomUUID().toString(), UUID.randomUUID().toString()); - } - - @BeforeEach - void before() { - builder = newBuilder(); - } - - private DefaultDocumentQueryBuilder newBuilder() { - return new DefaultDocumentQueryBuilder(); - } - - @Test - void shouldReturnErrorWhenSelectIsCalledWithNullDocument() { - assertThrows(NullPointerException.class, () -> builder.select((String) null)); - } - - @Test - void shouldAcceptToSelectOneValidDocument() { - assertSame(builder, builder.select(newRandomDocument())); - } - - @Test - void shouldAcceptToSelectAnEmptyArrayOfDocuments() { - assertSame(builder, builder.select()); - } - - @Test - void shouldReturnErrorWhenSelectIsCalledWithArrayOfDocumentsWithNullElement() { - assertThrows(NullPointerException.class, () -> builder.select(newRandomDocument(), null, newRandomDocument())); - } - - @Test - void shouldAcceptToSelectAnNonEmptyArrayOfDocumentsWithoutNullElement() { - assertSame(builder, builder.select(newRandomDocument(), newRandomDocument())); - } - - @Test - void shouldReturnErrorWhenSortIsCalledWithNullSortRef() { - assertThrows(NullPointerException.class, () -> builder.sort((Sort) null)); - } - - @Test - void shouldAcceptToSortOneValidSortReference() { - assertSame(builder, builder.sort(newRandomSortRef())); - } - - @Test - void shouldAcceptToSortAnEmptyArrayOfSortRef() { - assertSame(builder, builder.sort()); - } - - @Test - void shouldReturnErrorWhenSortIsCalledWithArrayOfSortRefWithNullElement() { - assertThrows(NullPointerException.class, () -> builder.sort(newRandomSortRef(), null, newRandomSortRef())); - } - - @Test - void shouldAcceptToSortAnNonEmptyArrayOfSortRefWithoutNullElement() { - assertSame(builder, builder.sort(newRandomSortRef(), newRandomSortRef())); - } - - @Test - void shouldReturnErrorWhenFromIsCalledWithNullDocumentCollection() { - assertThrows(NullPointerException.class, () -> builder.from(null)); - } - - @Test - void shouldAcceptToCallFromMethodWithValidDocumentCollection() { - assertSame(builder, builder.from(newRandomDocumentCollection())); - } - - @Test - void shouldReturnErrorWhenWhereIsCalledWithNullDocumentCondition() { - assertThrows(NullPointerException.class, () -> builder.where(null)); - } - - @Test - void shouldAcceptToCallWhereMethodWithValidDocumentCondition() { - assertSame(builder, builder.where(newRandomDocumentCondition())); - } - - - @Test - void shouldReturnErrorWhenSkipIsCalledWithArgumentLessThanZero() { - assertThrows(IllegalArgumentException.class, () -> builder.skip(-1)); - } - - @Test - void shouldAcceptToCallSkipWithArgumentGreaterThanZero() { - assertSame(builder, builder.skip(1)); - } - - @Test - void shouldReturnErrorWhenLimitIsCalledWithArgumentLessThanZero() { - assertThrows(IllegalArgumentException.class, () -> builder.limit(-1)); - } - - @Test - void shouldAcceptToCallLimitWithArgumentGreaterThanZero() { - assertSame(builder, builder.limit(1)); - } - - @Test - void shouldReturnErrorWhenBuildIsCalledWithoutDocumentCollectionIsNotProvidedPreviously() { - assertThrows(IllegalArgumentException.class, () -> builder.build()); - } - - @Test - void shouldAcceptToBuildWhenDocumentCollectionWasDefinedPreviously() { - DocumentQuery documentQuery = builder.from(newRandomDocumentCollection()).build(); - assertNotNull(documentQuery); - } - - @Test - void shouldReturnErrorWhenGetResultIsCalledWithNullDocumentManagerReference() { - assertThrows(NullPointerException.class, () -> builder.from(newRandomDocumentCollection()).getResult(null)); - } - - @Test - void shouldAcceptToCallGetResultWithAValidDocumentManager() { - - DocumentManager manager = mock(DocumentManager.class); - Stream expectedEntities = Stream.empty(); - when(manager.select(any(DocumentQuery.class))).thenReturn(expectedEntities); - String collection = newRandomDocumentCollection(); - Stream entities = builder.from(collection).getResult(manager); - assertSame(expectedEntities, entities); - verify(manager, atLeastOnce()).select(any(DocumentQuery.class)); - - } - - @Test - void shouldReturnErrorWhenGetSingleResultIsCalledWithNullDocumentManagerReference() { - assertThrows(NullPointerException.class, () -> builder.from(newRandomDocumentCollection()).getSingleResult(null)); - } - - @Test - void shouldAcceptToCallGetSingleResultWithAValidDocumentManager() { - - DocumentManager manager = mock(DocumentManager.class); - Optional expectedResult = Optional.empty(); - when(manager.singleResult(any(DocumentQuery.class))).thenReturn(expectedResult); - String collection = newRandomDocumentCollection(); - Optional result = builder.from(collection).getSingleResult(manager); - assertSame(expectedResult, result); - verify(manager, atLeastOnce()).singleResult(any(DocumentQuery.class)); - - } - - - @Test - void shouldBeEqualsToYourself() { - assertEquals(builder, builder); - } - - - @Test - void shouldBeNotEqualsToAnyOtherInstanceType() { - assertNotEquals(builder, new Object()); - } - - @Test - void shouldBeNotEqualsToAnotherBuilderWithDifferentAttributes() { - var anotherBuilder = newBuilder() - .select(newRandomDocument()) - .from(newRandomDocumentCollection()) - .sort(newRandomSortRef()) - .skip(1) - .limit(2); - - assertNotEquals(builder, anotherBuilder); - } - - @Test - void shouldBeEqualsToAnotherBuilderInstanceWithSameAttributes() { - String document = newRandomDocument(); - String documentCollection = newRandomDocumentCollection(); - Sort sort = newRandomSortRef(); - int skip = 1; - int limit = 2; - - var builder1 = newBuilder() - .select(document) - .from(documentCollection) - .sort(sort) - .skip(skip) - .limit(limit); - - var builder2 = newBuilder() - .select(document) - .from(documentCollection) - .sort(sort) - .skip(skip) - .limit(limit); - - assertNotSame(builder1,builder2); - - assertEquals(builder1, builder2); - } - - @Test - void shouldHashCodeBeDifferentWhenAttributesAreDifferent() { - - var builder1 = newBuilder() - .select(newRandomDocument()); - - var builder2 = newBuilder() - .select(newRandomDocument()); - - assertNotEquals(builder1.hashCode(),builder2.hashCode()); - - } - - @Test - void shouldHashCodeBeEqualsWhenAttributesAreTheSame() { - - String document = newRandomDocument(); - String documentCollection = newRandomDocumentCollection(); - Sort sort = newRandomSortRef(); - int skip = 1; - int limit = 2; - - var builder1 = newBuilder(); - var builder2 = newBuilder(); - - assertEquals(builder1.hashCode(), builder2.hashCode()); - - - builder1.select(document); - builder2.select(document); - assertEquals(builder1.hashCode(), builder2.hashCode()); - - builder1.from(documentCollection); - builder2.from(documentCollection); - assertEquals(builder1.hashCode(), builder2.hashCode()); - - builder1.sort(sort); - builder2.sort(sort); - assertEquals(builder1.hashCode(), builder2.hashCode()); - - builder1.skip(skip); - builder2.skip(skip); - assertEquals(builder1.hashCode(), builder2.hashCode()); - - builder1.limit(limit); - builder2.limit(limit); - assertEquals(builder1.hashCode(), builder2.hashCode()); - - } - -} diff --git a/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DefaultDocumentQueryTest.java b/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DefaultDocumentQueryTest.java deleted file mode 100644 index d807386d7..000000000 --- a/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DefaultDocumentQueryTest.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - -import jakarta.data.Sort; -import org.eclipse.jnosql.communication.Condition; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.eclipse.jnosql.communication.document.DocumentQuery.select; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - - -class DefaultDocumentQueryTest { - - - private DocumentQuery query; - - - @BeforeEach - void setUp() { - query = select().from("columnFamily").build(); - } - - - @Test - void shouldNotRemoveColumns() { - Assertions.assertThrows(UnsupportedOperationException.class, () -> { - List columns = query.documents(); - assertTrue(columns.isEmpty()); - columns.clear(); - }); - } - - - @Test - void shouldNotRemoveSort() { - Assertions.assertThrows(UnsupportedOperationException.class, () -> { - List> sorts = query.sorts(); - assertTrue(sorts.isEmpty()); - sorts.clear(); - }); - } - - @Test - void shouldConvertCountyBy() { - DocumentQuery query = DocumentQuery.select().from("entity") - .where("name").eq("predicate") - .orderBy("name").asc().build(); - - DocumentQuery countQuery = DefaultDocumentQuery.countBy(query); - Assertions.assertNotNull(countQuery); - assertEquals("entity", countQuery.name()); - assertEquals(0, countQuery.limit()); - assertEquals(0, countQuery.skip()); - assertTrue(countQuery.sorts().isEmpty()); - DocumentCondition condition = countQuery.condition().orElseThrow(); - Assertions.assertEquals(Condition.EQUALS, condition.condition()); - } - - @Test - void shouldConvertExistsBy() { - DocumentQuery query = DocumentQuery.select().from("entity") - .where("name").eq("predicate") - .orderBy("name").asc().build(); - - DocumentQuery countQuery = DefaultDocumentQuery.existsBy(query); - Assertions.assertNotNull(countQuery); - assertEquals("entity", countQuery.name()); - assertEquals(1, countQuery.limit()); - assertEquals(0, countQuery.skip()); - assertTrue(countQuery.sorts().isEmpty()); - DocumentCondition condition = countQuery.condition().orElseThrow(); - Assertions.assertEquals(Condition.EQUALS, condition.condition()); - } - - @Test - void shouldGenerateEquals(){ - DocumentQuery query = DocumentQuery.select().from("entity") - .where("name").eq("predicate") - .orderBy("name").asc().build(); - - DocumentQuery query2 = DocumentQuery.select().from("entity") - .where("name").eq("predicate") - .orderBy("name").asc().build(); - - assertThat(query).isEqualTo(query2); - assertThat(query).isEqualTo(query); - assertThat(query).isNotEqualTo("query"); - } - - @Test - void shouldGenerateHashCode(){ - DocumentQuery query = DocumentQuery.select().from("entity") - .where("name").eq("predicate") - .orderBy("name").asc().build(); - - DocumentQuery query2 = DocumentQuery.select().from("entity") - .where("name").eq("predicate") - .orderBy("name").asc().build(); - - assertThat(query.hashCode()).isEqualTo(query2.hashCode()); - } -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DefaultFluentDeleteQueryBuilderTest.java b/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DefaultFluentDeleteQueryBuilderTest.java deleted file mode 100644 index 6c21d843c..000000000 --- a/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DefaultFluentDeleteQueryBuilderTest.java +++ /dev/null @@ -1,266 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - - -import org.eclipse.jnosql.communication.Condition; -import org.eclipse.jnosql.communication.TypeReference; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.mockito.ArgumentCaptor; -import org.mockito.Mockito; - -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.eclipse.jnosql.communication.document.DocumentCondition.eq; -import static org.eclipse.jnosql.communication.document.DocumentDeleteQuery.delete; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.verify; - -class DefaultFluentDeleteQueryBuilderTest { - - @Test - void shouldReturnErrorWhenHasNullElementInSelect() { - Assertions.assertThrows(NullPointerException.class,() -> delete("document", "document", null)); - } - - @Test - void shouldDelete() { - String documentCollection = "documentCollection"; - DocumentDeleteQuery query = delete().from(documentCollection).build(); - assertTrue(query.documents().isEmpty()); - assertFalse(query.condition().isPresent()); - assertEquals(documentCollection, query.name()); - } - - @Test - void shouldDeleteDocuments() { - String documentCollection = "documentCollection"; - DocumentDeleteQuery query = delete("document", "document2").from(documentCollection).build(); - assertThat(query.documents()).contains("document", "document2"); - assertFalse(query.condition().isPresent()); - assertEquals(documentCollection, query.name()); - } - - - @Test - void shouldReturnErrorWhenFromIsNull() { - Assertions.assertThrows(NullPointerException.class,() -> delete().from(null)); - } - - @Test - void shouldSelectWhereNameEq() { - String documentCollection = "documentCollection"; - String name = "Ada Lovelace"; - DocumentDeleteQuery query = delete().from(documentCollection).where("name").eq(name).build(); - DocumentCondition condition = query.condition().get(); - - Document document = condition.document(); - - assertTrue(query.documents().isEmpty()); - assertEquals(documentCollection, query.name()); - assertEquals(Condition.EQUALS, condition.condition()); - assertEquals("name", document.name()); - assertEquals(name, document.get()); - - } - - @Test - void shouldSelectWhereNameLike() { - String documentCollection = "documentCollection"; - String name = "Ada Lovelace"; - DocumentDeleteQuery query = delete().from(documentCollection).where("name").like(name).build(); - DocumentCondition condition = query.condition().get(); - - Document document = condition.document(); - - assertTrue(query.documents().isEmpty()); - assertEquals(documentCollection, query.name()); - assertEquals(Condition.LIKE, condition.condition()); - assertEquals("name", document.name()); - assertEquals(name, document.get()); - } - - @Test - void shouldSelectWhereNameGt() { - String documentCollection = "documentCollection"; - Number value = 10; - DocumentDeleteQuery query = delete().from(documentCollection).where("name").gt(value).build(); - DocumentCondition condition = query.condition().get(); - - Document document = condition.document(); - - assertTrue(query.documents().isEmpty()); - assertEquals(documentCollection, query.name()); - assertEquals(Condition.GREATER_THAN, condition.condition()); - assertEquals("name", document.name()); - assertEquals(value, document.get()); - } - - @Test - void shouldSelectWhereNameGte() { - String documentCollection = "documentCollection"; - Number value = 10; - DocumentDeleteQuery query = delete().from(documentCollection).where("name").gte(value).build(); - DocumentCondition condition = query.condition().get(); - - Document document = condition.document(); - - assertTrue(query.documents().isEmpty()); - assertEquals(documentCollection, query.name()); - assertEquals(Condition.GREATER_EQUALS_THAN, condition.condition()); - assertEquals("name", document.name()); - assertEquals(value, document.get()); - } - - @Test - void shouldSelectWhereNameLt() { - String documentCollection = "documentCollection"; - Number value = 10; - DocumentDeleteQuery query = delete().from(documentCollection).where("name").lt(value).build(); - DocumentCondition condition = query.condition().get(); - - Document document = condition.document(); - - assertTrue(query.documents().isEmpty()); - assertEquals(documentCollection, query.name()); - assertEquals(Condition.LESSER_THAN, condition.condition()); - assertEquals("name", document.name()); - assertEquals(value, document.get()); - } - - @Test - void shouldSelectWhereNameLte() { - String documentCollection = "documentCollection"; - Number value = 10; - DocumentDeleteQuery query = delete().from(documentCollection).where("name").lte(value).build(); - DocumentCondition condition = query.condition().get(); - - Document document = condition.document(); - - assertTrue(query.documents().isEmpty()); - assertEquals(documentCollection, query.name()); - assertEquals(Condition.LESSER_EQUALS_THAN, condition.condition()); - assertEquals("name", document.name()); - assertEquals(value, document.get()); - } - - @Test - void shouldSelectWhereNameBetween() { - String documentCollection = "documentCollection"; - Number valueA = 10; - Number valueB = 20; - DocumentDeleteQuery query = delete().from(documentCollection).where("name").between(valueA, valueB).build(); - DocumentCondition condition = query.condition().get(); - - Document document = condition.document(); - - assertTrue(query.documents().isEmpty()); - assertEquals(documentCollection, query.name()); - assertEquals(Condition.BETWEEN, condition.condition()); - assertEquals("name", document.name()); - assertThat(document.get(new TypeReference>() {})).contains(10, 20); - } - - @Test - void shouldSelectWhereNameNot() { - String documentCollection = "documentCollection"; - String name = "Ada Lovelace"; - DocumentDeleteQuery query = delete().from(documentCollection).where("name").not().eq(name).build(); - DocumentCondition condition = query.condition().get(); - - Document column = condition.document(); - DocumentCondition negate = column.get(DocumentCondition.class); - assertTrue(query.documents().isEmpty()); - assertEquals(documentCollection, query.name()); - assertEquals(Condition.NOT, condition.condition()); - assertEquals(Condition.EQUALS, negate.condition()); - assertEquals("name", negate.document().name()); - assertEquals(name, negate.document().get()); - } - - - @Test - void shouldSelectWhereNameAnd() { - String documentCollection = "documentCollection"; - String name = "Ada Lovelace"; - DocumentDeleteQuery query = delete().from(documentCollection).where("name").eq(name).and("age") - .gt(10).build(); - DocumentCondition condition = query.condition().get(); - - Document document = condition.document(); - List conditions = document.get(new TypeReference<>() { - }); - assertEquals(Condition.AND, condition.condition()); - org.assertj.core.api.Assertions.assertThat(conditions).contains(eq(Document.of("name", name)), - DocumentCondition.gt(Document.of("age", 10))); - } - - @Test - void shouldSelectWhereNameOr() { - String documentCollection = "documentCollection"; - String name = "Ada Lovelace"; - DocumentDeleteQuery query = delete().from(documentCollection).where("name").eq(name) - .or("age").gt(10).build(); - DocumentCondition condition = query.condition().get(); - - Document document = condition.document(); - List conditions = document.get(new TypeReference<>() { - }); - assertEquals(Condition.OR, condition.condition()); - org.assertj.core.api.Assertions.assertThat(conditions).contains(eq(Document.of("name", name)), - DocumentCondition.gt(Document.of("age", 10))); - } - - - - @Test - void shouldDeleteNegate() { - String columnFamily = "columnFamily"; - DocumentDeleteQuery query = delete().from(columnFamily).where("city").not().eq("Assis") - .and("name").not().eq("Lucas").build(); - - DocumentCondition condition = query.condition().orElseThrow(RuntimeException::new); - assertEquals(columnFamily, query.name()); - Document column = condition.document(); - List conditions = column.get(new TypeReference<>() { - }); - - assertEquals(Condition.AND, condition.condition()); - org.assertj.core.api.Assertions.assertThat(conditions).contains(eq(Document.of("city", "Assis")).negate(), - eq(Document.of("name", "Lucas")).negate()); - - - } - - @Test - void shouldExecuteDelete() { - String collection = "collection"; - DocumentManager manager = Mockito.mock(DocumentManager.class); - ArgumentCaptor queryCaptor = ArgumentCaptor.forClass(DocumentDeleteQuery.class); - delete().from(collection).delete(manager); - verify(manager).delete(queryCaptor.capture()); - - DocumentDeleteQuery query = queryCaptor.getValue(); - assertTrue(query.documents().isEmpty()); - assertFalse(query.condition().isPresent()); - assertEquals(collection, query.name()); - } -} diff --git a/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DefaultFluentDocumentQueryBuilderTest.java b/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DefaultFluentDocumentQueryBuilderTest.java deleted file mode 100644 index 8d73bb2cd..000000000 --- a/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DefaultFluentDocumentQueryBuilderTest.java +++ /dev/null @@ -1,344 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - -import org.assertj.core.api.Assertions; -import org.eclipse.jnosql.communication.Condition; -import jakarta.data.Sort; -import org.eclipse.jnosql.communication.TypeReference; -import org.junit.jupiter.api.Test; -import org.mockito.ArgumentCaptor; -import org.mockito.Mockito; - -import java.util.List; -import java.util.Optional; -import java.util.stream.Stream; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.eclipse.jnosql.communication.document.DocumentCondition.eq; -import static org.eclipse.jnosql.communication.document.DocumentQuery.select; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; - - -class DefaultFluentDocumentQueryBuilderTest { - - - @Test - void shouldReturnErrorWhenHasNullElementInSelect() { - assertThrows(NullPointerException.class, () -> select("document", "document'", null)); - } - - @Test - void shouldSelect() { - String documentCollection = "documentCollection"; - DocumentQuery query = select().from(documentCollection).build(); - assertTrue(query.documents().isEmpty()); - assertFalse(query.condition().isPresent()); - assertEquals(documentCollection, query.name()); - } - - @Test - void shouldSelectDocument() { - String documentCollection = "documentCollection"; - DocumentQuery query = select("document", "document2").from(documentCollection).build(); - assertThat(query.documents()).contains("document", "document2"); - assertFalse(query.condition().isPresent()); - assertEquals(documentCollection, query.name()); - } - - @Test - void shouldReturnErrorWhenFromIsNull() { - assertThrows(NullPointerException.class, () -> select().from(null)); - } - - - @Test - void shouldSelectOrderAsc() { - String documentCollection = "documentCollection"; - DocumentQuery query = select().from(documentCollection).orderBy("name").asc().build(); - assertTrue(query.documents().isEmpty()); - assertFalse(query.condition().isPresent()); - assertEquals(documentCollection, query.name()); - assertThat(query.sorts()).contains(Sort.asc("name")); - } - - @Test - void shouldSelectOrderDesc() { - String documentCollection = "documentCollection"; - DocumentQuery query = select().from(documentCollection).orderBy("name").desc().build(); - assertTrue(query.documents().isEmpty()); - assertFalse(query.condition().isPresent()); - assertEquals(documentCollection, query.name()); - assertThat(query.sorts()).contains(Sort.desc("name")); - } - - - @Test - void shouldReturnErrorSelectWhenOrderIsNull() { - assertThrows(NullPointerException.class,() -> { - String documentCollection = "documentCollection"; - select().from(documentCollection).orderBy(null); - }); - } - - @Test - void shouldSelectLimit() { - String documentCollection = "documentCollection"; - DocumentQuery query = select().from(documentCollection).limit(10).build(); - assertTrue(query.documents().isEmpty()); - assertFalse(query.condition().isPresent()); - assertEquals(documentCollection, query.name()); - assertEquals(10L, query.limit()); - } - - @Test - void shouldReturnErrorWhenLimitIsNegative() { - String documentCollection = "documentCollection"; - assertThrows(IllegalArgumentException.class, () -> select().from(documentCollection).limit(-1)); - } - - @Test - void shouldSelectSkip() { - String documentCollection = "documentCollection"; - DocumentQuery query = select().from(documentCollection).skip(10).build(); - assertTrue(query.documents().isEmpty()); - assertFalse(query.condition().isPresent()); - assertEquals(documentCollection, query.name()); - assertEquals(10L, query.skip()); - } - - @Test - void shouldReturnErrorWhenSkipIsNegative() { - String documentCollection = "documentCollection"; - assertThrows(IllegalArgumentException.class, () -> select().from(documentCollection).skip(-1)); - } - - @Test - void shouldSelectWhereNameEq() { - String documentCollection = "documentCollection"; - String name = "Ada Lovelace"; - DocumentQuery query = select().from(documentCollection).where("name").eq(name).build(); - DocumentCondition condition = query.condition().get(); - - Document document = condition.document(); - - assertTrue(query.documents().isEmpty()); - assertEquals(documentCollection, query.name()); - assertEquals(Condition.EQUALS, condition.condition()); - assertEquals("name", document.name()); - assertEquals(name, document.get()); - - } - - @Test - void shouldSelectWhereNameLike() { - String documentCollection = "documentCollection"; - String name = "Ada Lovelace"; - DocumentQuery query = select().from(documentCollection).where("name").like(name).build(); - DocumentCondition condition = query.condition().get(); - - Document document = condition.document(); - - assertTrue(query.documents().isEmpty()); - assertEquals(documentCollection, query.name()); - assertEquals(Condition.LIKE, condition.condition()); - assertEquals("name", document.name()); - assertEquals(name, document.get()); - } - - @Test - void shouldSelectWhereNameGt() { - String documentCollection = "documentCollection"; - Number value = 10; - DocumentQuery query = select().from(documentCollection).where("name").gt(value).build(); - DocumentCondition condition = query.condition().get(); - - Document document = condition.document(); - - assertTrue(query.documents().isEmpty()); - assertEquals(documentCollection, query.name()); - assertEquals(Condition.GREATER_THAN, condition.condition()); - assertEquals("name", document.name()); - assertEquals(value, document.get()); - } - - @Test - void shouldSelectWhereNameGte() { - String documentCollection = "documentCollection"; - Number value = 10; - DocumentQuery query = select().from(documentCollection).where("name").gte(value).build(); - DocumentCondition condition = query.condition().get(); - - Document document = condition.document(); - - assertTrue(query.documents().isEmpty()); - assertEquals(documentCollection, query.name()); - assertEquals(Condition.GREATER_EQUALS_THAN, condition.condition()); - assertEquals("name", document.name()); - assertEquals(value, document.get()); - } - - @Test - void shouldSelectWhereNameLt() { - String documentCollection = "documentCollection"; - Number value = 10; - DocumentQuery query = select().from(documentCollection).where("name").lt(value).build(); - DocumentCondition condition = query.condition().get(); - - Document document = condition.document(); - - assertTrue(query.documents().isEmpty()); - assertEquals(documentCollection, query.name()); - assertEquals(Condition.LESSER_THAN, condition.condition()); - assertEquals("name", document.name()); - assertEquals(value, document.get()); - } - - @Test - void shouldSelectWhereNameLte() { - String documentCollection = "documentCollection"; - Number value = 10; - DocumentQuery query = select().from(documentCollection).where("name").lte(value).build(); - DocumentCondition condition = query.condition().get(); - - Document document = condition.document(); - - assertTrue(query.documents().isEmpty()); - assertEquals(documentCollection, query.name()); - assertEquals(Condition.LESSER_EQUALS_THAN, condition.condition()); - assertEquals("name", document.name()); - assertEquals(value, document.get()); - } - - @Test - void shouldSelectWhereNameBetween() { - String documentCollection = "documentCollection"; - Number valueA = 10; - Number valueB = 20; - DocumentQuery query = select().from(documentCollection).where("name").between(valueA, valueB).build(); - DocumentCondition condition = query.condition().get(); - - Document document = condition.document(); - - assertTrue(query.documents().isEmpty()); - assertEquals(documentCollection, query.name()); - assertEquals(Condition.BETWEEN, condition.condition()); - assertEquals("name", document.name()); - assertThat(document.get(new TypeReference>() { - })).contains(10, 20); - } - - @Test - void shouldSelectWhereNameNot() { - String documentCollection = "documentCollection"; - String name = "Ada Lovelace"; - DocumentQuery query = select().from(documentCollection).where("name").not().eq(name).build(); - DocumentCondition condition = query.condition().get(); - - Document column = condition.document(); - DocumentCondition negate = column.get(DocumentCondition.class); - assertTrue(query.documents().isEmpty()); - assertEquals(documentCollection, query.name()); - assertEquals(Condition.NOT, condition.condition()); - assertEquals(Condition.EQUALS, negate.condition()); - assertEquals("name", negate.document().name()); - assertEquals(name, negate.document().get()); - } - - - @Test - void shouldSelectWhereNameAnd() { - String documentCollection = "documentCollection"; - String name = "Ada Lovelace"; - DocumentQuery query = select().from(documentCollection).where("name").eq(name).and("age") - .gt(10).build(); - DocumentCondition condition = query.condition().get(); - - Document document = condition.document(); - List conditions = document.get(new TypeReference<>() { - }); - assertEquals(Condition.AND, condition.condition()); - Assertions.assertThat(conditions).contains(eq(Document.of("name", name)), - DocumentCondition.gt(Document.of("age", 10))); - } - - @Test - void shouldSelectWhereNameOr() { - String documentCollection = "documentCollection"; - String name = "Ada Lovelace"; - DocumentQuery query = select().from(documentCollection).where("name").eq(name).or("age").gt(10).build(); - DocumentCondition condition = query.condition().get(); - - Document document = condition.document(); - List conditions = document.get(new TypeReference<>() { - }); - assertEquals(Condition.OR, condition.condition()); - Assertions.assertThat(conditions).contains(eq(Document.of("name", name)), - DocumentCondition.gt(Document.of("age", 10))); - } - - - @Test - void shouldSelectNegate() { - String columnFamily = "columnFamily"; - DocumentQuery query = select().from(columnFamily).where("city").not().eq("Assis") - .and("name").not().eq("Lucas").build(); - - DocumentCondition condition = query.condition().orElseThrow(RuntimeException::new); - assertEquals(columnFamily, query.name()); - Document column = condition.document(); - List conditions = column.get(new TypeReference<>() { - }); - - assertEquals(Condition.AND, condition.condition()); - Assertions.assertThat(conditions).contains(eq(Document.of("city", "Assis")).negate(), - eq(Document.of("name", "Lucas")).negate()); - - } - - - @Test - void shouldExecuteManager() { - DocumentManager manager = Mockito.mock(DocumentManager.class); - ArgumentCaptor queryCaptor = ArgumentCaptor.forClass(DefaultDocumentQuery.class); - String collection = "collection"; - Stream entities = select().from(collection).getResult(manager); - Mockito.verify(manager).select(queryCaptor.capture()); - checkQuery(queryCaptor, collection); - } - - @Test - void shouldExecuteSingleResultManager() { - DocumentManager manager = Mockito.mock(DocumentManager.class); - ArgumentCaptor queryCaptor = ArgumentCaptor.forClass(DefaultDocumentQuery.class); - String collection = "collection"; - Optional entities = select().from(collection).getSingleResult(manager); - Mockito.verify(manager).singleResult(queryCaptor.capture()); - checkQuery(queryCaptor, collection); - } - - private void checkQuery(ArgumentCaptor queryCaptor, String collection) { - DocumentQuery query = queryCaptor.getValue(); - assertTrue(query.documents().isEmpty()); - assertFalse(query.condition().isPresent()); - assertEquals(collection, query.name()); - } - -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DeleteQueryParserTest.java b/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DeleteQueryParserTest.java deleted file mode 100644 index 96eca23d9..000000000 --- a/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DeleteQueryParserTest.java +++ /dev/null @@ -1,398 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - -import org.assertj.core.api.Assertions; -import org.eclipse.jnosql.communication.Condition; -import org.eclipse.jnosql.communication.QueryException; -import org.eclipse.jnosql.communication.TypeReference; -import org.eclipse.jnosql.communication.Value; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; -import org.mockito.ArgumentCaptor; -import org.mockito.Mockito; - -import java.util.Arrays; -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.eclipse.jnosql.communication.document.DocumentCondition.eq; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; - -class DeleteQueryParserTest { - - private final DeleteQueryParser parser = new DeleteQueryParser(); - - private final DocumentManager documentCollection = Mockito.mock(DocumentManager.class); - - private final DocumentObserverParser observer = new DocumentObserverParser() { - }; - - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God"}) - void shouldReturnParserQuery(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentDeleteQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).delete(captor.capture()); - DocumentDeleteQuery documentQuery = captor.getValue(); - - assertTrue(documentQuery.documents().isEmpty()); - assertEquals("God", documentQuery.name()); - assertFalse(documentQuery.condition().isPresent()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete name, address from God"}) - void shouldReturnParserQuery1(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentDeleteQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).delete(captor.capture()); - DocumentDeleteQuery documentQuery = captor.getValue(); - - assertThat(documentQuery.documents()).contains("name", "address"); - assertEquals("God", documentQuery.name()); - assertFalse(documentQuery.condition().isPresent()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where stamina > 10.23"}) - void shouldReturnParserQuery11(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentDeleteQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).delete(captor.capture()); - DocumentDeleteQuery documentQuery = captor.getValue(); - - checkBaseQuery(documentQuery); - assertTrue(documentQuery.condition().isPresent()); - DocumentCondition condition = documentQuery.condition().get(); - - assertEquals(Condition.GREATER_THAN, condition.condition()); - assertEquals(Document.of("stamina", 10.23), condition.document()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where stamina >= -10.23"}) - void shouldReturnParserQuery12(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentDeleteQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).delete(captor.capture()); - DocumentDeleteQuery documentQuery = captor.getValue(); - - checkBaseQuery(documentQuery); - assertTrue(documentQuery.condition().isPresent()); - DocumentCondition condition = documentQuery.condition().get(); - - assertEquals(Condition.GREATER_EQUALS_THAN, condition.condition()); - assertEquals(Document.of("stamina", -10.23), condition.document()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where stamina <= -10.23"}) - void shouldReturnParserQuery13(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentDeleteQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).delete(captor.capture()); - DocumentDeleteQuery documentQuery = captor.getValue(); - - checkBaseQuery(documentQuery); - assertTrue(documentQuery.condition().isPresent()); - DocumentCondition condition = documentQuery.condition().get(); - - assertEquals(Condition.LESSER_EQUALS_THAN, condition.condition()); - assertEquals(Document.of("stamina", -10.23), condition.document()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where stamina < -10.23"}) - void shouldReturnParserQuery14(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentDeleteQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).delete(captor.capture()); - DocumentDeleteQuery documentQuery = captor.getValue(); - - checkBaseQuery(documentQuery); - assertTrue(documentQuery.condition().isPresent()); - DocumentCondition condition = documentQuery.condition().get(); - - assertEquals(Condition.LESSER_THAN, condition.condition()); - assertEquals(Document.of("stamina", -10.23), condition.document()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where age between 10 and 30"}) - void shouldReturnParserQuery15(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentDeleteQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).delete(captor.capture()); - DocumentDeleteQuery documentQuery = captor.getValue(); - - checkBaseQuery(documentQuery); - assertTrue(documentQuery.condition().isPresent()); - DocumentCondition condition = documentQuery.condition().get(); - - assertEquals(Condition.BETWEEN, condition.condition()); - assertEquals(Document.of("age", Arrays.asList(10L, 30L)), condition.document()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where name = \"diana\""}) - void shouldReturnParserQuery16(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentDeleteQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).delete(captor.capture()); - DocumentDeleteQuery documentQuery = captor.getValue(); - - checkBaseQuery(documentQuery); - assertTrue(documentQuery.condition().isPresent()); - DocumentCondition condition = documentQuery.condition().get(); - - assertEquals(Condition.EQUALS, condition.condition()); - assertEquals(Document.of("name", "diana"), condition.document()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where siblings = {\"apollo\": \"Brother\", \"Zeus\": \"Father\"}"}) - void shouldReturnParserQuery18(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentDeleteQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).delete(captor.capture()); - DocumentDeleteQuery documentQuery = captor.getValue(); - - checkBaseQuery(documentQuery); - assertTrue(documentQuery.condition().isPresent()); - DocumentCondition condition = documentQuery.condition().get(); - - assertEquals(Condition.EQUALS, condition.condition()); - Document document = condition.document(); - List documents = document.get(new TypeReference<>() { - }); - Assertions.assertThat(documents).contains(Document.of("apollo", "Brother"), - Document.of("Zeus", "Father")); - assertEquals("siblings", document.name()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where age = convert(12, java.lang.Integer)"}) - void shouldReturnParserQuery19(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentDeleteQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).delete(captor.capture()); - DocumentDeleteQuery documentQuery = captor.getValue(); - - checkBaseQuery(documentQuery); - assertTrue(documentQuery.condition().isPresent()); - DocumentCondition condition = documentQuery.condition().get(); - Document document = condition.document(); - assertEquals(Condition.EQUALS, condition.condition()); - assertEquals("age", document.name()); - assertEquals(Value.of(12), document.value()); - - - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where name in (\"Ada\", \"Apollo\")"}) - void shouldReturnParserQuery20(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentDeleteQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).delete(captor.capture()); - DocumentDeleteQuery documentQuery = captor.getValue(); - - checkBaseQuery(documentQuery); - assertTrue(documentQuery.condition().isPresent()); - DocumentCondition condition = documentQuery.condition().get(); - Document document = condition.document(); - assertEquals(Condition.IN, condition.condition()); - assertEquals("name", document.name()); - List values = document.get(new TypeReference<>() { - }); - assertThat(values).contains("Ada", "Apollo"); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where name like \"Ada\""}) - void shouldReturnParserQuery21(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentDeleteQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).delete(captor.capture()); - DocumentDeleteQuery documentQuery = captor.getValue(); - - checkBaseQuery(documentQuery); - assertTrue(documentQuery.condition().isPresent()); - DocumentCondition condition = documentQuery.condition().get(); - Document document = condition.document(); - assertEquals(Condition.LIKE, condition.condition()); - assertEquals("name", document.name()); - assertEquals("Ada", document.get()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where name not like \"Ada\""}) - void shouldReturnParserQuery22(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentDeleteQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).delete(captor.capture()); - DocumentDeleteQuery documentQuery = captor.getValue(); - - checkBaseQuery(documentQuery); - assertTrue(documentQuery.condition().isPresent()); - DocumentCondition condition = documentQuery.condition().get(); - Document document = condition.document(); - assertEquals(Condition.NOT, condition.condition()); - List conditions = document.get(new TypeReference<>() { - }); - DocumentCondition documentCondition = conditions.get(0); - assertEquals(Condition.LIKE, documentCondition.condition()); - assertEquals(Document.of("name", "Ada"), documentCondition.document()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where name = \"Ada\" and age = 20"}) - void shouldReturnParserQuery23(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentDeleteQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).delete(captor.capture()); - DocumentDeleteQuery documentQuery = captor.getValue(); - - checkBaseQuery(documentQuery); - assertTrue(documentQuery.condition().isPresent()); - DocumentCondition condition = documentQuery.condition().get(); - Document document = condition.document(); - assertEquals(Condition.AND, condition.condition()); - List conditions = document.get(new TypeReference<>() { - }); - Assertions.assertThat(conditions).contains(eq(Document.of("name", "Ada")), - eq(Document.of("age", 20L))); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where name = \"Ada\" or age = 20"}) - void shouldReturnParserQuery24(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentDeleteQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).delete(captor.capture()); - DocumentDeleteQuery documentQuery = captor.getValue(); - - checkBaseQuery(documentQuery); - assertTrue(documentQuery.condition().isPresent()); - DocumentCondition condition = documentQuery.condition().get(); - Document document = condition.document(); - assertEquals(Condition.OR, condition.condition()); - List conditions = document.get(new TypeReference<>() { - }); - Assertions.assertThat(conditions).contains(eq(Document.of("name", "Ada")), - eq(Document.of("age", 20L))); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where name = \"Ada\" and age = 20 or" + - " siblings = {\"apollo\": \"Brother\", \"Zeus\": \"Father\"}"}) - void shouldReturnParserQuery25(String query) { - - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentDeleteQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).delete(captor.capture()); - DocumentDeleteQuery documentQuery = captor.getValue(); - - checkBaseQuery(documentQuery); - assertTrue(documentQuery.condition().isPresent()); - DocumentCondition condition = documentQuery.condition().get(); - Document document = condition.document(); - assertEquals(Condition.AND, condition.condition()); - List conditions = document.get(new TypeReference<>() { - }); - assertEquals(Condition.EQUALS, conditions.get(0).condition()); - assertEquals(Condition.EQUALS, conditions.get(1).condition()); - assertEquals(Condition.OR, conditions.get(2).condition()); - - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where name = \"Ada\" and age = 20 or" + - " siblings = {\"apollo\": \"Brother\", \"Zeus\": \"Father\"} and birthday =" + - " convert(\"2007-12-03\", java.time.LocalDate)"}) - void shouldReturnParserQuery26(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentDeleteQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).delete(captor.capture()); - DocumentDeleteQuery documentQuery = captor.getValue(); - - checkBaseQuery(documentQuery); - assertTrue(documentQuery.condition().isPresent()); - DocumentCondition condition = documentQuery.condition().get(); - Document document = condition.document(); - assertEquals(Condition.AND, condition.condition()); - List conditions = document.get(new TypeReference<>() { - }); - assertEquals(Condition.EQUALS, conditions.get(0).condition()); - assertEquals(Condition.EQUALS, conditions.get(1).condition()); - assertEquals(Condition.OR, conditions.get(2).condition()); - assertEquals(Condition.EQUALS, conditions.get(3).condition()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where age = @age"}) - void shouldReturnErrorWhenNeedPrepareStatement(String query) { - assertThrows(QueryException.class, () -> parser.query(query, documentCollection, observer)); - - - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where age = @age"}) - void shouldReturnErrorWhenIsQueryWithParam(String query) { - - assertThrows(QueryException.class, () -> parser.query(query, documentCollection, observer)); - - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where age = @age"}) - void shouldReturnErrorWhenDontBindParameters(String query) { - - DocumentPreparedStatement prepare = parser.prepare(query, documentCollection, observer); - assertThrows(QueryException.class, prepare::result); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where age = @age"}) - void shouldExecutePrepareStatement(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentDeleteQuery.class); - - DocumentPreparedStatement prepare = parser.prepare(query, documentCollection, observer); - prepare.bind("age", 12); - prepare.result(); - Mockito.verify(documentCollection).delete(captor.capture()); - DocumentDeleteQuery documentQuery = captor.getValue(); - DocumentCondition documentCondition = documentQuery.condition().get(); - Document document = documentCondition.document(); - assertEquals(Condition.EQUALS, documentCondition.condition()); - assertEquals("age", document.name()); - assertEquals(12, document.get()); - } - - - - private void checkBaseQuery(DocumentDeleteQuery documentQuery) { - assertTrue(documentQuery.documents().isEmpty()); - assertEquals("God", documentQuery.name()); - } -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DocumentConditionTest.java b/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DocumentConditionTest.java deleted file mode 100644 index ebe4ea649..000000000 --- a/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DocumentConditionTest.java +++ /dev/null @@ -1,246 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ - -package org.eclipse.jnosql.communication.document; - -import org.eclipse.jnosql.communication.Condition; -import org.eclipse.jnosql.communication.TypeReference; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrows; - - -class DocumentConditionTest { - - - @Test - void shouldReturnErrorWhenDocumentIsNull() { - assertThrows(NullPointerException.class, () -> DocumentCondition.of(null, Condition.AND)); - } - - @Test - void shouldCreateAnInstance() { - Document name = Document.of("name", "Otavio"); - DocumentCondition condition = DocumentCondition.of(name, Condition.EQUALS); - assertNotNull(condition); - assertEquals(name, condition.document()); - assertEquals(Condition.EQUALS, condition.condition()); - } - - @Test - void shouldCreateNegationCondition() { - Document age = Document.of("age", 26); - DocumentCondition condition = DocumentCondition.of(age, Condition.GREATER_THAN); - DocumentCondition negate = condition.negate(); - Document negateDocument = negate.document(); - assertEquals(Condition.NOT, negate.condition()); - assertEquals(Condition.NOT.getNameField(), negateDocument.name()); - assertEquals(DocumentCondition.of(age, Condition.GREATER_THAN), negateDocument.value().get()); - } - - @Test - void shouldReturnValidDoubleNegation() { - Document age = Document.of("age", 26); - DocumentCondition condition = DocumentCondition.of(age, Condition.GREATER_THAN); - DocumentCondition affirmative = condition.negate().negate(); - Assertions.assertEquals(condition, affirmative); - } - - @Test - void shouldCreateAndCondition() { - Document age = Document.of("age", 26); - Document name = Document.of("name", "Otavio"); - DocumentCondition condition1 = DocumentCondition.of(name, Condition.EQUALS); - DocumentCondition condition2 = DocumentCondition.of(age, Condition.GREATER_THAN); - - DocumentCondition and = condition1.and(condition2); - Document andDocument = and.document(); - assertEquals(Condition.AND, and.condition()); - assertEquals(Condition.AND.getNameField(), andDocument.name()); - assertThat(andDocument.value().get(new TypeReference>() { - })).contains(condition1, condition2); - - } - - @Test - void shouldCreateOrCondition() { - Document age = Document.of("age", 26); - Document name = Document.of("name", "Otavio"); - DocumentCondition condition1 = DocumentCondition.of(name, Condition.EQUALS); - DocumentCondition condition2 = DocumentCondition.of(age, Condition.GREATER_THAN); - - DocumentCondition and = condition1.or(condition2); - Document andDocument = and.document(); - assertEquals(Condition.OR, and.condition()); - assertEquals(Condition.OR.getNameField(), andDocument.name()); - assertThat(andDocument.value().get(new TypeReference>() { - })).contains(condition1, condition2); - - } - - @Test - void shouldReturnErrorWhenCreateAndWithNullValues() { - assertThrows(NullPointerException.class, () -> DocumentCondition.and((DocumentCondition[]) null)); - } - - - @Test - void shouldReturnErrorWhenCreateOrWithNullValues() { - assertThrows(NullPointerException.class, () -> DocumentCondition.or((DocumentCondition[]) null)); - } - - - @Test - void shouldAppendAnd() { - DocumentCondition eq = DocumentCondition.eq(Document.of("name", "otavio")); - DocumentCondition gt = DocumentCondition.gt(Document.of("age", 10)); - DocumentCondition and = DocumentCondition.and(eq, gt); - assertEquals(Condition.AND, and.condition()); - List conditions = and.document().get(new TypeReference<>() { - }); - assertThat(conditions).contains(eq, gt); - } - - @Test - void shouldAppendOr() { - DocumentCondition eq = DocumentCondition.eq(Document.of("name", "otavio")); - DocumentCondition gt = DocumentCondition.gt(Document.of("age", 10)); - DocumentCondition and = DocumentCondition.or(eq, gt); - assertEquals(Condition.OR, and.condition()); - List conditions = and.document().get(new TypeReference<>() { - }); - assertThat(conditions).contains(eq, gt); - } - - @Test - void shouldAnd() { - DocumentCondition eq = DocumentCondition.eq(Document.of("name", "otavio")); - DocumentCondition gt = DocumentCondition.gt(Document.of("age", 10)); - DocumentCondition lte = DocumentCondition.lte(Document.of("salary", 10_000.00)); - - DocumentCondition and = eq.and(gt); - List conditions = and.document().get(new TypeReference<>() { - }); - assertEquals(Condition.AND, and.condition()); - assertThat(conditions).contains(eq, gt); - DocumentCondition result = and.and(lte); - - assertEquals(Condition.AND, result.condition()); - assertThat(result.document().get(new TypeReference>() { - })).contains(eq, gt, lte); - - } - - @Test - void shouldOr() { - DocumentCondition eq = DocumentCondition.eq(Document.of("name", "otavio")); - DocumentCondition gt = DocumentCondition.gt(Document.of("age", 10)); - DocumentCondition lte = DocumentCondition.lte(Document.of("salary", 10_000.00)); - - DocumentCondition or = eq.or(gt); - List conditions = or.document().get(new TypeReference<>() { - }); - assertEquals(Condition.OR, or.condition()); - assertThat(conditions).contains(eq, gt); - DocumentCondition result = or.or(lte); - - assertEquals(Condition.OR, result.condition()); - assertThat(result.document().get(new TypeReference>() { - })).contains(eq, gt, lte); - - } - - @Test - void shouldNegate() { - DocumentCondition eq = DocumentCondition.eq(Document.of("name", "otavio")); - DocumentCondition negate = eq.negate(); - assertEquals(Condition.NOT, negate.condition()); - DocumentCondition condition = negate.document().get(DocumentCondition.class); - assertEquals(eq, condition); - } - - @Test - void shouldAffirmDoubleNegate() { - DocumentCondition eq = DocumentCondition.eq(Document.of("name", "otavio")); - DocumentCondition affirm = eq.negate().negate(); - assertEquals(eq.condition(), affirm.condition()); - - } - - @Test - void shouldReturnErrorWhenBetweenIsNull() { - assertThrows(NullPointerException.class, () -> DocumentCondition.between(null)); - } - - @Test - void shouldReturnErrorWhenBetweenIsNotIterable() { - assertThrows(IllegalArgumentException.class, () -> { - Document document = Document.of("age", 12); - DocumentCondition.between(document); - }); - } - - @Test - void shouldReturnErrorWhenIterableHasOneElement() { - assertThrows(IllegalArgumentException.class, () -> { - Document document = Document.of("age", Collections.singleton(12)); - DocumentCondition.between(document); - }); - } - - @Test - void shouldReturnErrorWhenIterableHasMoreThanTwoElement2() { - assertThrows(IllegalArgumentException.class, () -> { - Document document = Document.of("age", Arrays.asList(12, 12, 12)); - DocumentCondition.between(document); - }); - } - - @Test - void shouldReturnBetween() { - Document document = Document.of("age", Arrays.asList(12, 13)); - DocumentCondition between = DocumentCondition.between(document); - assertEquals(Condition.BETWEEN, between.condition()); - Iterable integers = between.document().get(new TypeReference<>() { - }); - assertThat(integers).contains(12, 13); - } - - @Test - void shouldReturnErrorWhenInConditionIsInvalid() { - assertThrows(NullPointerException.class, () -> DocumentCondition.in(null)); - assertThrows(IllegalArgumentException.class, () -> DocumentCondition.in(Document.of("value", 10))); - } - - @Test - void shouldReturnInClause() { - Document column = Document.of("age", Arrays.asList(12, 13)); - DocumentCondition in = DocumentCondition.in(column); - assertEquals(Condition.IN, in.condition()); - Iterable integers = in.document().get(new TypeReference<>() { - }); - assertThat(integers).contains(12, 13); - } -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DocumentEntityTest.java b/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DocumentEntityTest.java deleted file mode 100644 index 114ecb401..000000000 --- a/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DocumentEntityTest.java +++ /dev/null @@ -1,451 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ - -package org.eclipse.jnosql.communication.document; - -import org.assertj.core.api.SoftAssertions; -import org.eclipse.jnosql.communication.TypeReference; -import org.eclipse.jnosql.communication.Value; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Optional; - -import static java.util.Arrays.asList; -import static java.util.Collections.singletonList; -import static java.util.Collections.singletonMap; -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotSame; -import static org.junit.jupiter.api.Assertions.assertTrue; - - -class DocumentEntityTest { - - @Test - void shouldReturnErrorWhenNameIsNull() { - Assertions.assertThrows(NullPointerException.class, () -> DocumentEntity.of(null)); - } - - @Test - void shouldReturnErrorWhenDocumentsIsNull() { - Assertions.assertThrows(NullPointerException.class, () -> DocumentEntity.of("entity", null)); - } - - @Test - void shouldReturnOneDocument() { - DocumentEntity entity = DocumentEntity.of("entity"); - assertEquals(Integer.valueOf(0), Integer.valueOf(entity.size())); - assertTrue(entity.isEmpty()); - - entity.add(Document.of("name", "name")); - entity.add(Document.of("name2", Value.of("name2"))); - assertFalse(entity.isEmpty()); - assertEquals(Integer.valueOf(2), Integer.valueOf(entity.size())); - assertFalse(DocumentEntity.of("entity", singletonList(Document.of("name", "name"))).isEmpty()); - } - - @Test - void shouldDoCopy() { - DocumentEntity entity = DocumentEntity.of("entity", singletonList(Document.of("name", "name"))); - DocumentEntity copy = entity.copy(); - assertNotSame(entity, copy); - assertEquals(entity, copy); - - } - - @Test - void shouldFindDocument() { - Document document = Document.of("name", "name"); - DocumentEntity entity = DocumentEntity.of("entity", singletonList(document)); - Optional name = entity.find("name"); - Optional notfound = entity.find("not_found"); - assertTrue(name.isPresent()); - assertFalse(notfound.isPresent()); - assertEquals(document, name.get()); - } - - @Test - void shouldReturnErrorWhenFindDocumentIsNull() { - Assertions.assertThrows(NullPointerException.class, () -> { - Document document = Document.of("name", "name"); - DocumentEntity entity = DocumentEntity.of("entity", singletonList(document)); - entity.find(null); - }); - } - - @Test - void shouldRemoveDocumentByName() { - Document document = Document.of("name", "name"); - DocumentEntity entity = DocumentEntity.of("entity", singletonList(document)); - assertTrue(entity.remove("name")); - assertTrue(entity.isEmpty()); - } - - @Test - void shouldConvertToMap() { - Document document = Document.of("name", "name"); - DocumentEntity entity = DocumentEntity.of("entity", singletonList(document)); - Map result = entity.toMap(); - assertFalse(result.isEmpty()); - assertEquals(Integer.valueOf(1), Integer.valueOf(result.size())); - assertEquals(document.name(), result.keySet().stream().findAny().get()); - - } - - @Test - void shouldConvertSubColumnToMap() { - Document document = Document.of("name", "name"); - DocumentEntity entity = DocumentEntity.of("entity", singletonList(Document.of("sub", document))); - Map result = entity.toMap(); - assertFalse(result.isEmpty()); - assertEquals(Integer.valueOf(1), Integer.valueOf(result.size())); - Map map = (Map) result.get("sub"); - assertEquals("name", map.get("name")); - } - - @Test - void shouldConvertSubDocumentListToMap() { - DocumentEntity entity = DocumentEntity.of("entity"); - entity.add(Document.of("_id", "id")); - List documents = asList(Document.of("name", "Ada"), Document.of("type", "type"), - Document.of("information", "ada@lovelace.com")); - - entity.add(Document.of("contacts", documents)); - Map result = entity.toMap(); - assertEquals("id", result.get("_id")); - List> contacts = (List>) result.get("contacts"); - assertEquals(3, contacts.size()); - assertThat(contacts).contains(singletonMap("name", "Ada"), singletonMap("type", "type"), - singletonMap("information", "ada@lovelace.com")); - - } - - @Test - void shouldConvertSubDocumentListToMap2() { - DocumentEntity entity = DocumentEntity.of("entity"); - entity.add(Document.of("_id", "id")); - List> documents = new ArrayList<>(); - documents.add(asList(Document.of("name", "Ada"), Document.of("type", "type"), - Document.of("information", "ada@lovelace.com"))); - - entity.add(Document.of("contacts", documents)); - Map result = entity.toMap(); - assertEquals("id", result.get("_id")); - List>> contacts = (List>>) result.get("contacts"); - assertEquals(1, contacts.size()); - List> maps = contacts.get(0); - assertEquals(3, maps.size()); - assertThat(maps).contains(singletonMap("name", "Ada"), singletonMap("type", "type"), - singletonMap("information", "ada@lovelace.com")); - - } - - @Test - void shouldShouldCreateANewInstance() { - String name = "name"; - DocumentEntity entity = new DocumentEntity(name); - assertEquals(name, entity.name()); - } - - @Test - void shouldCreateAnEmptyEntity() { - DocumentEntity entity = new DocumentEntity("name"); - assertTrue(entity.isEmpty()); - } - - @Test - void shouldReturnAnErrorWhenAddANullDocument() { - Assertions.assertThrows(NullPointerException.class, () -> { - DocumentEntity entity = new DocumentEntity("name"); - entity.add(null); - }); - } - - @Test - void shouldAddANewDocument() { - DocumentEntity entity = new DocumentEntity("name"); - entity.add(Document.of("document", 12)); - assertFalse(entity.isEmpty()); - assertEquals(1, entity.size()); - } - - @Test - void shouldReturnErrorWhenAddAnNullIterable() { - Assertions.assertThrows(NullPointerException.class, () -> { - DocumentEntity entity = new DocumentEntity("name"); - entity.addAll(null); - }); - } - - @Test - void shouldAddAllDocuments() { - DocumentEntity entity = new DocumentEntity("name"); - entity.addAll(asList(Document.of("name", 12), Document.of("value", "value"))); - assertFalse(entity.isEmpty()); - assertEquals(2, entity.size()); - } - - - @Test - void shouldNotFindDocument() { - DocumentEntity entity = new DocumentEntity("name"); - Optional document = entity.find("name"); - assertFalse(document.isPresent()); - } - - @Test - void shouldFindValue() { - Document document = Document.of("name", "name"); - DocumentEntity entity = DocumentEntity.of("entity", singletonList(document)); - Optional name = entity.find("name", String.class); - Assertions.assertNotNull(name); - Assertions.assertTrue(name.isPresent()); - Assertions.assertEquals("name", name.orElse("")); - } - - @Test - void shouldNotFindValue() { - Document document = Document.of("name", "name"); - DocumentEntity entity = DocumentEntity.of("entity", singletonList(document)); - Optional name = entity.find("not_found", String.class); - Assertions.assertNotNull(name); - Assertions.assertFalse(name.isPresent()); - } - - @Test - void shouldFindTypeSupplier() { - Document document = Document.of("name", "name"); - DocumentEntity entity = DocumentEntity.of("entity", singletonList(document)); - List names = entity.find("name", new TypeReference>() {}) - .orElse(Collections.emptyList()); - Assertions.assertNotNull(names); - assertFalse(names.isEmpty()); - assertThat(names).contains("name"); - } - - @Test - void shouldNotFindTypeSupplier() { - Document document = Document.of("name", "name"); - DocumentEntity entity = DocumentEntity.of("entity", singletonList(document)); - List names = entity.find("not_find", new TypeReference>() {}) - .orElse(Collections.emptyList()); - Assertions.assertNotNull(names); - Assertions.assertTrue(names.isEmpty()); - } - - @Test - void shouldRemoveByName() { - DocumentEntity entity = new DocumentEntity("name"); - entity.add(Document.of("value", 32D)); - assertTrue(entity.remove("value")); - assertTrue(entity.isEmpty()); - } - - @Test - void shouldNotRemoveByName() { - DocumentEntity entity = new DocumentEntity("name"); - entity.add(Document.of("value", 32D)); - - assertFalse(entity.remove("value1")); - assertFalse(entity.isEmpty()); - } - - @Test - void shouldReturnErrorWhenRemoveByNameIsNull() { - Assertions.assertThrows(NullPointerException.class, () -> { - DocumentEntity entity = new DocumentEntity("name"); - entity.remove(null); - }); - } - - - @Test - void shouldAddDocumentAsNameAndObject() { - DocumentEntity entity = new DocumentEntity("documentCollection"); - entity.add("name", 10); - assertEquals(1, entity.size()); - Optional name = entity.find("name"); - assertTrue(name.isPresent()); - assertEquals(10, name.get().get()); - } - - @Test - void shouldAddDocumentAsNameAndValue() { - DocumentEntity entity = new DocumentEntity("documentCollection"); - entity.add("name", Value.of(10)); - assertEquals(1, entity.size()); - Optional name = entity.find("name"); - assertTrue(name.isPresent()); - assertEquals(10, name.get().get()); - } - - @Test - void shouldReturnWhenAddDocumentsObjectWhenHasNullObject() { - DocumentEntity entity = new DocumentEntity("documentCollection"); - entity.add("name", null); - Document name = entity.find("name").orElseThrow(); - SoftAssertions.assertSoftly(softly -> { - softly.assertThat(name.name()).isEqualTo("name"); - softly.assertThat(name.get()).isNull(); - }); - } - - @Test - void shouldReturnErrorWhenAddDocumentsObjectWhenHasNullDocumentName() { - Assertions.assertThrows(NullPointerException.class, () -> { - DocumentEntity entity = new DocumentEntity("documentCollection"); - entity.add(null, 10); - }); - } - - @Test - void shouldReturnErrorWhenAddDocumentsValueWhenHasNullDocumentName() { - Assertions.assertThrows(NullPointerException.class, () -> { - DocumentEntity entity = new DocumentEntity("documentCollection"); - entity.add(null, Value.of(12)); - }); - } - - - @Test - void shouldAvoidDuplicatedDocument() { - DocumentEntity entity = new DocumentEntity("documentCollection"); - entity.add("name", 10); - entity.add("name", 13); - assertEquals(1, entity.size()); - Optional document = entity.find("name"); - assertEquals(Document.of("name", 13), document.get()); - } - - @Test - void shouldAvoidDuplicatedDocumentWhenAddList() { - List documents = asList(Document.of("name", 10), Document.of("name", 13)); - DocumentEntity entity = new DocumentEntity("documentCollection"); - entity.addAll(documents); - assertEquals(1, entity.size()); - assertEquals(1, DocumentEntity.of("documentCollection", documents).size()); - } - - @Test - void shouldReturnsTheDocumentNames() { - List documents = asList(Document.of("name", 10), Document.of("name2", 11), - Document.of("name3", 12), Document.of("name4", 13), - Document.of("name5", 14), Document.of("name5", 16)); - - DocumentEntity collection = DocumentEntity.of("documentCollection", documents); - assertThat(collection.getDocumentNames()).contains("name", "name2", "name3", "name4", "name5"); - - } - - @Test - void shouldReturnsTheDocumentValues() { - List documents = asList(Document.of("name", 10), Document.of("name2", 11), - Document.of("name3", 12), Document.of("name4", 13), - Document.of("name5", 14), Document.of("name5", 16)); - - DocumentEntity collection = DocumentEntity.of("documentCollection", documents); - assertThat(collection.getValues()).contains(Value.of(10), Value.of(11), Value.of(12), - Value.of(13), Value.of(16)); - } - - @Test - void shouldReturnTrueWhenContainsElement() { - List documents = asList(Document.of("name", 10), Document.of("name2", 11), - Document.of("name3", 12), Document.of("name4", 13), - Document.of("name5", 14), Document.of("name5", 16)); - - DocumentEntity collection = DocumentEntity.of("documentCollection", documents); - - assertTrue(collection.contains("name")); - assertTrue(collection.contains("name2")); - assertTrue(collection.contains("name3")); - assertTrue(collection.contains("name4")); - assertTrue(collection.contains("name5")); - } - - @Test - void shouldReturnFalseWhenDoesNotContainElement() { - List documents = asList(Document.of("name", 10), Document.of("name2", 11), - Document.of("name3", 12), Document.of("name4", 13), - Document.of("name5", 14), Document.of("name5", 16)); - - DocumentEntity collection = DocumentEntity.of("documentCollection", documents); - - assertFalse(collection.contains("name6")); - assertFalse(collection.contains("name7")); - assertFalse(collection.contains("name8")); - assertFalse(collection.contains("name9")); - assertFalse(collection.contains("name10")); - } - - @Test - void shouldRemoveAllElementsWhenUseClearMethod() { - List documents = asList(Document.of("name", 10), Document.of("name2", 11), - Document.of("name3", 12), Document.of("name4", 13), - Document.of("name5", 14), Document.of("name5", 16)); - - DocumentEntity collection = DocumentEntity.of("documentCollection", documents); - - assertFalse(collection.isEmpty()); - collection.clear(); - assertTrue(collection.isEmpty()); - } - - @Test - void shouldHashCode(){ - List documents = List.of(Document.of("name", 10)); - var collection = DocumentEntity.of("documentCollection", documents); - var collection2 = DocumentEntity.of("documentCollection", documents); - assertThat(collection.hashCode()).isEqualTo(collection2.hashCode()); - } - - @Test - void shouldEquals(){ - List documents = List.of(Document.of("name", 10)); - var collection = DocumentEntity.of("documentCollection", documents); - var collection2 = DocumentEntity.of("documentCollection", documents); - assertThat(collection).isEqualTo(collection2); - assertThat(collection).isEqualTo(collection); - assertThat(collection).isNotEqualTo("collection"); - } - - @Test - void shouldToString(){ - List documents = List.of(Document.of("name", 10)); - var collection = DocumentEntity.of("documentCollection", documents); - assertThat(collection.toString()).isEqualTo("DefaultDocumentEntity{documents={name=DefaultDocument" + - "[name=name, value=DefaultValue[value=10]]}, name='documentCollection'}"); - } - - @Test - void shouldCreateNull(){ - DocumentEntity entity = DocumentEntity.of("entity"); - entity.addNull("name"); - Document name = entity.find("name").orElseThrow(); - SoftAssertions.assertSoftly(softly -> { - softly.assertThat(name.name()).isEqualTo("name"); - softly.assertThat(name.get()).isNull(); - }); - } - -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DocumentParamsTest.java b/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DocumentParamsTest.java deleted file mode 100644 index d641a1dc5..000000000 --- a/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DocumentParamsTest.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * Maximillian Arruda - * - */ -package org.eclipse.jnosql.communication.document; - -import org.eclipse.jnosql.communication.Params; -import org.eclipse.jnosql.communication.Value; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -class DocumentParamsTest { - - @Test - void shouldSetParameter() { - Params params = Params.newParams(); - Value name = params.add("name"); - Document document = Document.of("name", name); - params.bind("name", "Ada Lovelace"); - - assertEquals("Ada Lovelace", document.get()); - - params.bind("name", "Diana"); - assertEquals("Diana", document.get()); - } - -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DocumentQueryBuilderTest.java b/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DocumentQueryBuilderTest.java deleted file mode 100644 index 7ee00b268..000000000 --- a/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DocumentQueryBuilderTest.java +++ /dev/null @@ -1,362 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - -import org.eclipse.jnosql.communication.Condition; -import jakarta.data.Sort; -import jakarta.data.Direction; -import org.eclipse.jnosql.communication.TypeReference; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.mockito.ArgumentCaptor; -import org.mockito.Mockito; - -import java.util.Arrays; -import java.util.List; -import java.util.Optional; -import java.util.stream.Stream; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.eclipse.jnosql.communication.document.DocumentCondition.eq; -import static org.eclipse.jnosql.communication.document.DocumentQuery.builder; -import static org.junit.jupiter.api.Assertions.*; - -class DocumentQueryBuilderTest { - @Test - void shouldReturnErrorWhenHasNullElementInSelect() { - assertThrows(NullPointerException.class, () -> builder("document", "document'", null)); - } - - @Test - void shouldBuilder() { - String documentCollection = "documentCollection"; - DocumentQuery query = builder().from(documentCollection).build(); - assertTrue(query.documents().isEmpty()); - assertFalse(query.condition().isPresent()); - assertEquals(documentCollection, query.name()); - } - - @Test - void shouldSelectDocument() { - String documentCollection = "documentCollection"; - DocumentQuery query = builder("document", "document2").from(documentCollection).build(); - assertThat(query.documents()).contains("document", "document2"); - assertFalse(query.condition().isPresent()); - assertEquals(documentCollection, query.name()); - } - - @Test - void shouldReturnErrorWhenFromIsNull() { - assertThrows(NullPointerException.class, () -> builder().from(null)); - } - - - @Test - void shouldSelectOrderAsc() { - String documentCollection = "documentCollection"; - DocumentQuery query = builder().from(documentCollection).sort(Sort.asc("name")).build(); - assertTrue(query.documents().isEmpty()); - assertFalse(query.condition().isPresent()); - assertEquals(documentCollection, query.name()); - assertThat(query.sorts()).contains(Sort.of("name", Direction.ASC, false)); - } - - @Test - void shouldSelectOrderDesc() { - String documentCollection = "documentCollection"; - DocumentQuery query = builder().from(documentCollection).sort(Sort.desc("name")).build(); - assertTrue(query.documents().isEmpty()); - assertFalse(query.condition().isPresent()); - assertEquals(documentCollection, query.name()); - assertThat(query.sorts()).contains(Sort.of("name", Direction.DESC, false)); - } - - - @Test - void shouldReturnErrorSelectWhenOrderIsNull() { - assertThrows(NullPointerException.class,() -> { - String documentCollection = "documentCollection"; - builder().from(documentCollection).sort((Sort) null); - }); - } - - @Test - void shouldSelectLimit() { - String documentCollection = "documentCollection"; - DocumentQuery query = builder().from(documentCollection).limit(10).build(); - assertTrue(query.documents().isEmpty()); - assertFalse(query.condition().isPresent()); - assertEquals(documentCollection, query.name()); - assertEquals(10L, query.limit()); - } - - @Test - void shouldReturnErrorWhenLimitIsNegative() { - String documentCollection = "documentCollection"; - Assertions.assertThrows(IllegalArgumentException.class, () -> builder().from(documentCollection).limit(-1)); - } - - @Test - void shouldSelectSkip() { - String documentCollection = "documentCollection"; - DocumentQuery query = builder().from(documentCollection).skip(10).build(); - assertTrue(query.documents().isEmpty()); - assertFalse(query.condition().isPresent()); - assertEquals(documentCollection, query.name()); - assertEquals(10L, query.skip()); - } - - @Test - void shouldReturnErrorWhenSkipIsNegative() { - String documentCollection = "documentCollection"; - Assertions.assertThrows(IllegalArgumentException.class, () -> builder().from(documentCollection).skip(-1)); - } - - @Test - void shouldSelectWhereNameEq() { - String documentCollection = "documentCollection"; - String name = "Ada Lovelace"; - - DocumentQuery query = builder().from(documentCollection) - .where(eq("name", name)) - .build(); - DocumentCondition condition = query.condition().get(); - - Document document = condition.document(); - - assertTrue(query.documents().isEmpty()); - assertEquals(documentCollection, query.name()); - assertEquals(Condition.EQUALS, condition.condition()); - assertEquals("name", document.name()); - assertEquals(name, document.get()); - - } - - @Test - void shouldSelectWhereNameLike() { - String documentCollection = "documentCollection"; - String name = "Ada Lovelace"; - DocumentQuery query = builder().from(documentCollection) - .where(DocumentCondition.like("name", name)) - .build(); - DocumentCondition condition = query.condition().get(); - - Document document = condition.document(); - - assertTrue(query.documents().isEmpty()); - assertEquals(documentCollection, query.name()); - assertEquals(Condition.LIKE, condition.condition()); - assertEquals("name", document.name()); - assertEquals(name, document.get()); - } - - @Test - void shouldSelectWhereNameGt() { - String documentCollection = "documentCollection"; - Number value = 10; - - DocumentQuery query = builder().from(documentCollection).where(DocumentCondition.gt("name", 10)) - .build(); - - DocumentCondition condition = query.condition().get(); - - Document document = condition.document(); - - assertTrue(query.documents().isEmpty()); - assertEquals(documentCollection, query.name()); - assertEquals(Condition.GREATER_THAN, condition.condition()); - assertEquals("name", document.name()); - assertEquals(value, document.get()); - } - - @Test - void shouldSelectWhereNameGte() { - String documentCollection = "documentCollection"; - Number value = 10; - DocumentCondition gteName = DocumentCondition.gte("name", value); - DocumentQuery query = builder().from(documentCollection).where(gteName).build(); - DocumentCondition condition = query.condition().get(); - - Document document = condition.document(); - - assertTrue(query.documents().isEmpty()); - assertEquals(documentCollection, query.name()); - assertEquals(Condition.GREATER_EQUALS_THAN, condition.condition()); - assertEquals("name", document.name()); - assertEquals(value, document.get()); - } - - @Test - void shouldSelectWhereNameLt() { - String documentCollection = "documentCollection"; - Number value = 10; - - DocumentQuery query = builder().from(documentCollection).where(DocumentCondition.lt("name", value)) - .build(); - DocumentCondition condition = query.condition().get(); - - Document document = condition.document(); - - assertTrue(query.documents().isEmpty()); - assertEquals(documentCollection, query.name()); - assertEquals(Condition.LESSER_THAN, condition.condition()); - assertEquals("name", document.name()); - assertEquals(value, document.get()); - } - - @Test - void shouldSelectWhereNameLte() { - String documentCollection = "documentCollection"; - Number value = 10; - DocumentQuery query = builder().from(documentCollection).where(DocumentCondition.lte("name", value)) - .build(); - DocumentCondition condition = query.condition().get(); - - Document document = condition.document(); - - assertTrue(query.documents().isEmpty()); - assertEquals(documentCollection, query.name()); - assertEquals(Condition.LESSER_EQUALS_THAN, condition.condition()); - assertEquals("name", document.name()); - assertEquals(value, document.get()); - } - - @Test - void shouldSelectWhereNameBetween() { - String documentCollection = "documentCollection"; - Number valueA = 10; - Number valueB = 20; - - DocumentQuery query = builder().from(documentCollection) - .where(DocumentCondition.between("name", Arrays.asList(valueA, valueB))) - .build(); - DocumentCondition condition = query.condition().get(); - - Document document = condition.document(); - - assertTrue(query.documents().isEmpty()); - assertEquals(documentCollection, query.name()); - assertEquals(Condition.BETWEEN, condition.condition()); - assertEquals("name", document.name()); - assertThat(document.get(new TypeReference>() { - })).contains(10, 20); - } - - @Test - void shouldSelectWhereNameNot() { - String documentCollection = "documentCollection"; - String name = "Ada Lovelace"; - - DocumentQuery query = builder().from(documentCollection).where(eq("name", name).negate()) - .build(); - DocumentCondition condition = query.condition().get(); - - Document column = condition.document(); - DocumentCondition negate = column.get(DocumentCondition.class); - assertTrue(query.documents().isEmpty()); - assertEquals(documentCollection, query.name()); - assertEquals(Condition.NOT, condition.condition()); - assertEquals(Condition.EQUALS, negate.condition()); - assertEquals("name", negate.document().name()); - assertEquals(name, negate.document().get()); - } - - - @Test - void shouldSelectWhereNameAnd() { - String documentCollection = "documentCollection"; - String name = "Ada Lovelace"; - DocumentCondition nameEqualsAda = eq("name", name); - DocumentCondition ageOlderTen = DocumentCondition.gt("age", 10); - DocumentQuery query = builder().from(documentCollection) - .where(DocumentCondition.and(nameEqualsAda, ageOlderTen)) - .build(); - DocumentCondition condition = query.condition().get(); - - Document document = condition.document(); - List conditions = document.get(new TypeReference<>() { - }); - assertEquals(Condition.AND, condition.condition()); - org.assertj.core.api.Assertions.assertThat(conditions).contains(eq(Document.of("name", name)), - DocumentCondition.gt(Document.of("age", 10))); - } - - @Test - void shouldSelectWhereNameOr() { - String documentCollection = "documentCollection"; - String name = "Ada Lovelace"; - DocumentCondition nameEqualsAda = eq("name", name); - DocumentCondition ageOlderTen = DocumentCondition.gt("age", 10); - DocumentQuery query = builder().from(documentCollection).where(DocumentCondition.or(nameEqualsAda, ageOlderTen)) - .build(); - DocumentCondition condition = query.condition().get(); - - Document document = condition.document(); - List conditions = document.get(new TypeReference<>() { - }); - assertEquals(Condition.OR, condition.condition()); - org.assertj.core.api.Assertions.assertThat(conditions).contains(eq(Document.of("name", name)), - DocumentCondition.gt(Document.of("age", 10))); - } - - - @Test - void shouldSelectNegate() { - String collection = "collection"; - DocumentCondition nameNotEqualsLucas = eq("name", "Lucas").negate(); - DocumentQuery query = builder().from(collection) - .where(nameNotEqualsLucas).build(); - - DocumentCondition condition = query.condition().orElseThrow(RuntimeException::new); - assertEquals(collection, query.name()); - Document column = condition.document(); - List conditions = column.get(new TypeReference<>() { - }); - - assertEquals(Condition.NOT, condition.condition()); - org.assertj.core.api.Assertions.assertThat(conditions).contains(eq(Document.of("name", "Lucas"))); - - } - - - @Test - void shouldExecuteManager() { - DocumentManager manager = Mockito.mock(DocumentManager.class); - ArgumentCaptor queryCaptor = ArgumentCaptor.forClass(DefaultDocumentQuery.class); - String collection = "collection"; - Stream entities = builder().from(collection).getResult(manager); - Mockito.verify(manager).select(queryCaptor.capture()); - checkQuery(queryCaptor, collection); - } - - @Test - void shouldExecuteSingleResultManager() { - DocumentManager manager = Mockito.mock(DocumentManager.class); - ArgumentCaptor queryCaptor = ArgumentCaptor.forClass(DefaultDocumentQuery.class); - String collection = "collection"; - Optional entities = builder().from(collection).getSingleResult(manager); - Mockito.verify(manager).singleResult(queryCaptor.capture()); - checkQuery(queryCaptor, collection); - } - - private void checkQuery(ArgumentCaptor queryCaptor, String collection) { - DocumentQuery query = queryCaptor.getValue(); - assertTrue(query.documents().isEmpty()); - assertFalse(query.condition().isPresent()); - assertEquals(collection, query.name()); - } -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DocumentQueryParserTest.java b/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DocumentQueryParserTest.java deleted file mode 100644 index eeaa6a88f..000000000 --- a/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DocumentQueryParserTest.java +++ /dev/null @@ -1,232 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - -import org.eclipse.jnosql.communication.Condition; -import jakarta.data.exceptions.NonUniqueResultException; -import org.eclipse.jnosql.communication.QueryException; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; -import org.mockito.ArgumentCaptor; -import org.mockito.Mockito; - -import java.util.Optional; -import java.util.stream.Stream; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; - - -class DocumentQueryParserTest { - - private final DocumentQueryParser parser = new DocumentQueryParser(); - - private final DocumentManager manager = Mockito.mock(DocumentManager.class); - - @Test - void shouldReturnNPEWhenThereIsNullParameter() { - - assertThrows(NullPointerException.class, () -> parser.query(null, manager, DocumentObserverParser.EMPTY)); - assertThrows(NullPointerException.class, () -> parser.query("select * from God", null, DocumentObserverParser.EMPTY)); - } - - @Test - void shouldReturnErrorWhenHasInvalidQuery() { - - assertThrows(QueryException.class, () -> parser.query("inva", manager, DocumentObserverParser.EMPTY)); - assertThrows(QueryException.class, () -> parser.query("invalid", manager, DocumentObserverParser.EMPTY)); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God"}) - void shouldReturnParserQuery(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DefaultDocumentQuery.class); - parser.query(query, manager, DocumentObserverParser.EMPTY); - Mockito.verify(manager).select(captor.capture()); - DefaultDocumentQuery documentQuery = captor.getValue(); - - assertTrue(documentQuery.documents().isEmpty()); - assertTrue(documentQuery.sorts().isEmpty()); - assertEquals(0L, documentQuery.limit()); - assertEquals(0L, documentQuery.skip()); - assertEquals("God", documentQuery.name()); - assertFalse(documentQuery.condition().isPresent()); - - } - - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God"}) - void shouldReturnParserQuery1(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentDeleteQuery.class); - parser.query(query, manager, DocumentObserverParser.EMPTY); - Mockito.verify(manager).delete(captor.capture()); - DocumentDeleteQuery documentQuery = captor.getValue(); - - assertTrue(documentQuery.documents().isEmpty()); - assertEquals("God", documentQuery.name()); - assertFalse(documentQuery.condition().isPresent()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"insert God (name = \"Diana\")"}) - void shouldReturnParserQuery2(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentEntity.class); - parser.query(query, manager, DocumentObserverParser.EMPTY); - Mockito.verify(manager).insert(captor.capture()); - DocumentEntity entity = captor.getValue(); - - - assertEquals("God", entity.name()); - assertEquals(Document.of("name", "Diana"), entity.find("name").get()); - } - - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"update God (name = \"Diana\")"}) - void shouldReturnParserQuery3(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentEntity.class); - parser.query(query, manager, DocumentObserverParser.EMPTY); - Mockito.verify(manager).update(captor.capture()); - DocumentEntity entity = captor.getValue(); - - - assertEquals("God", entity.name()); - assertEquals(Document.of("name", "Diana"), entity.find("name").get()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"delete from God where age = @age"}) - void shouldExecutePrepareStatement(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentDeleteQuery.class); - - DocumentPreparedStatement prepare = parser.prepare(query, manager, DocumentObserverParser.EMPTY); - prepare.bind("age", 12); - prepare.result(); - Mockito.verify(manager).delete(captor.capture()); - DocumentDeleteQuery documentQuery = captor.getValue(); - DocumentCondition documentCondition = documentQuery.condition().get(); - Document document = documentCondition.document(); - assertEquals(Condition.EQUALS, documentCondition.condition()); - assertEquals("age", document.name()); - assertEquals(12, document.get()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"insert God (name = @name)"}) - void shouldExecutePrepareStatement1(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentEntity.class); - DocumentPreparedStatement prepare = parser.prepare(query, manager, DocumentObserverParser.EMPTY); - prepare.bind("name", "Diana"); - prepare.result(); - Mockito.verify(manager).insert(captor.capture()); - DocumentEntity entity = captor.getValue(); - assertEquals("God", entity.name()); - assertEquals(Document.of("name", "Diana"), entity.find("name").get()); - - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where age = @age"}) - void shouldExecutePrepareStatement2(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DefaultDocumentQuery.class); - - DocumentPreparedStatement prepare = parser.prepare(query, manager, DocumentObserverParser.EMPTY); - prepare.bind("age", 12); - prepare.result(); - Mockito.verify(manager).select(captor.capture()); - DefaultDocumentQuery documentQuery = captor.getValue(); - DocumentCondition documentCondition = documentQuery.condition().get(); - Document document = documentCondition.document(); - assertEquals(Condition.EQUALS, documentCondition.condition()); - assertEquals("age", document.name()); - assertEquals(12, document.get()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"update God (name = @name)"}) - void shouldExecutePrepareStatement3(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentEntity.class); - DocumentPreparedStatement prepare = parser.prepare(query, manager, DocumentObserverParser.EMPTY); - prepare.bind("name", "Diana"); - prepare.result(); - Mockito.verify(manager).update(captor.capture()); - DocumentEntity entity = captor.getValue(); - assertEquals("God", entity.name()); - assertEquals(Document.of("name", "Diana"), entity.find("name").get()); - - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where age = @age"}) - void shouldSingleResult(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DefaultDocumentQuery.class); - - Mockito.when(manager.select(Mockito.any(DefaultDocumentQuery.class))) - .thenReturn(Stream.of(Mockito.mock(DocumentEntity.class))); - - DocumentPreparedStatement prepare = parser.prepare(query, manager, DocumentObserverParser.EMPTY); - prepare.bind("age", 12); - final Optional result = prepare.singleResult(); - Mockito.verify(manager).select(captor.capture()); - DefaultDocumentQuery columnQuery = captor.getValue(); - DocumentCondition columnCondition = columnQuery.condition().get(); - Document column = columnCondition.document(); - assertEquals(Condition.EQUALS, columnCondition.condition()); - assertEquals("age", column.name()); - assertEquals(12, column.get()); - assertTrue(result.isPresent()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where age = @age"}) - void shouldReturnEmptySingleResult(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DefaultDocumentQuery.class); - - Mockito.when(manager.select(Mockito.any(DefaultDocumentQuery.class))) - .thenReturn(Stream.empty()); - - DocumentPreparedStatement prepare = parser.prepare(query, manager, DocumentObserverParser.EMPTY); - prepare.bind("age", 12); - final Optional result = prepare.singleResult(); - Mockito.verify(manager).select(captor.capture()); - DefaultDocumentQuery columnQuery = captor.getValue(); - DocumentCondition columnCondition = columnQuery.condition().get(); - Document column = columnCondition.document(); - assertEquals(Condition.EQUALS, columnCondition.condition()); - assertEquals("age", column.name()); - assertEquals(12, column.get()); - assertFalse(result.isPresent()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where age = @age"}) - void shouldReturnErrorSingleResult(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentQuery.class); - - Mockito.when(manager.select(Mockito.any(DefaultDocumentQuery.class))) - .thenReturn(Stream.of(Mockito.mock(DocumentEntity.class), Mockito.mock(DocumentEntity.class))); - - DocumentPreparedStatement prepare = parser.prepare(query, manager, DocumentObserverParser.EMPTY); - prepare.bind("age", 12); - assertThrows(NonUniqueResultException.class, prepare::singleResult); - } - -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DocumentTest.java b/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DocumentTest.java deleted file mode 100644 index 6de2cbf42..000000000 --- a/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DocumentTest.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - -import org.assertj.core.api.SoftAssertions; -import org.eclipse.jnosql.communication.TypeReference; -import org.eclipse.jnosql.communication.Value; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.util.List; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; - - -public class DocumentTest { - - - private static final Value DEFAULT_VALUE = Value.of(12); - - @Test - public void shouldReturnNameWhenNameIsNull() { - Assertions.assertThrows(NullPointerException.class, () -> Document.of(null, DEFAULT_VALUE)); - } - - @Test - public void shouldReturnNameWhenValueIsNull() { - Document document = Document.of("Name", null); - SoftAssertions.assertSoftly(soft -> { - soft.assertThat(document.name()).isEqualTo("Name"); - soft.assertThat(document.value().isNull()).isTrue(); - }); - - } - - @Test - public void shouldCreateAnDocumentInstance() { - String name = "name"; - Document document = Document.of(name, DEFAULT_VALUE); - assertNotNull(document); - assertEquals(name, document.name()); - assertEquals(DEFAULT_VALUE, document.value()); - } - - @Test - public void shouldBeEquals() { - assertEquals(Document.of("name", DEFAULT_VALUE), Document.of("name", DEFAULT_VALUE)); - } - - @Test - public void shouldReturnGetObject() { - Value value = Value.of("text"); - Document document = Document.of("name", value); - assertEquals(value.get(), document.get()); - } - - @Test - public void shouldReturnGetClass() { - Value value = Value.of("text"); - Document document = Document.of("name", value); - assertEquals(value.get(String.class), document.get(String.class)); - } - - - @Test - public void shouldReturnGetType() { - Value value = Value.of("text"); - Document document = Document.of("name", value); - TypeReference> typeReference = new TypeReference<>() { - }; - assertEquals(value.get(typeReference), document.get(typeReference)); - } -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DocumentsTest.java b/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DocumentsTest.java deleted file mode 100644 index 8a8a0db89..000000000 --- a/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/DocumentsTest.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - -import org.junit.jupiter.api.Test; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import static java.util.Collections.singletonList; -import static java.util.Collections.singletonMap; -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; - - -public class DocumentsTest { - @Test - void shouldCreateDocument() { - Document column = Documents.of("name", "Ada"); - assertEquals("name", column.name()); - assertEquals("Ada", column.get()); - } - - @Test - void shouldCreateColumnsFromMap() { - Map map = singletonMap("name", "Ada"); - List documents = Documents.of(map); - assertFalse(documents.isEmpty()); - assertThat(documents).contains(Document.of("name", "Ada")); - } - - @Test - void shouldCreateRecursiveMap() { - List>> list = new ArrayList<>(); - Map map = singletonMap("mobile", "55 1234-4567"); - list.add(singletonList(map)); - - List documents = Documents.of(singletonMap("contact", list)); - assertEquals(1, documents.size()); - Document document = documents.get(0); - assertEquals("contact", document.name()); - List> result = (List>) document.get(); - assertEquals(Document.of("mobile", "55 1234-4567"), result.get(0).get(0)); - - } -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/InsertQueryParserTest.java b/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/InsertQueryParserTest.java deleted file mode 100644 index 7f3b9acc2..000000000 --- a/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/InsertQueryParserTest.java +++ /dev/null @@ -1,259 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - -import org.assertj.core.api.Assertions; -import org.eclipse.jnosql.communication.QueryException; -import org.eclipse.jnosql.communication.TypeReference; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; -import org.mockito.ArgumentCaptor; -import org.mockito.Mockito; - -import java.math.BigDecimal; -import java.time.Duration; -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - -class InsertQueryParserTest { - - private final InsertQueryParser parser = new InsertQueryParser(); - - private final DocumentManager manager = Mockito.mock(DocumentManager.class); - - private final DocumentObserverParser observer = new DocumentObserverParser() { - }; - - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"insert God (name = \"Diana\")"}) - void shouldReturnParserQuery(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentEntity.class); - parser.query(query, manager, observer); - Mockito.verify(manager).insert(captor.capture()); - DocumentEntity entity = captor.getValue(); - - - assertEquals("God", entity.name()); - assertEquals(Document.of("name", "Diana"), entity.find("name").get()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"insert God (age = 30, name = \"Artemis\")"}) - void shouldReturnParserQuery1(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentEntity.class); - parser.query(query, manager, observer); - Mockito.verify(manager).insert(captor.capture()); - DocumentEntity entity = captor.getValue(); - - assertEquals("God", entity.name()); - assertEquals(Document.of("name", "Artemis"), entity.find("name").get()); - assertEquals(Document.of("age", 30L), entity.find("age").get()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"insert God (name = \"Diana\") 10 day"}) - void shouldReturnParserQuery2(String query) { - - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentEntity.class); - ArgumentCaptor durationCaptor = ArgumentCaptor.forClass(Duration.class); - - parser.query(query, manager, observer); - Mockito.verify(manager).insert(captor.capture(), durationCaptor.capture()); - DocumentEntity entity = captor.getValue(); - Duration duration = durationCaptor.getValue(); - - assertEquals("God", entity.name()); - assertEquals(Document.of("name", "Diana"), entity.find("name").get()); - assertEquals(Duration.ofDays(10L), duration); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"insert God (name = \"Diana\") 10 hour"}) - void shouldReturnParserQuery3(String query) { - - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentEntity.class); - ArgumentCaptor durationCaptor = ArgumentCaptor.forClass(Duration.class); - - parser.query(query, manager, observer); - Mockito.verify(manager).insert(captor.capture(), durationCaptor.capture()); - DocumentEntity entity = captor.getValue(); - Duration duration = durationCaptor.getValue(); - - assertEquals("God", entity.name()); - assertEquals(Document.of("name", "Diana"), entity.find("name").get()); - assertEquals(Duration.ofHours(10L), duration); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"insert God (name = \"Diana\") 10 minute"}) - void shouldReturnParserQuery4(String query) { - - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentEntity.class); - ArgumentCaptor durationCaptor = ArgumentCaptor.forClass(Duration.class); - - parser.query(query, manager, observer); - Mockito.verify(manager).insert(captor.capture(), durationCaptor.capture()); - DocumentEntity entity = captor.getValue(); - Duration duration = durationCaptor.getValue(); - - assertEquals("God", entity.name()); - assertEquals(Document.of("name", "Diana"), entity.find("name").get()); - assertEquals(Duration.ofMinutes(10L), duration); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"insert God (name = \"Diana\") 10 second"}) - void shouldReturnParserQuery5(String query) { - - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentEntity.class); - ArgumentCaptor durationCaptor = ArgumentCaptor.forClass(Duration.class); - - parser.query(query, manager, observer); - Mockito.verify(manager).insert(captor.capture(), durationCaptor.capture()); - DocumentEntity entity = captor.getValue(); - Duration duration = durationCaptor.getValue(); - - assertEquals("God", entity.name()); - assertEquals(Document.of("name", "Diana"), entity.find("name").get()); - assertEquals(Duration.ofSeconds(10L), duration); - } - - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"insert God (name = \"Diana\") 10 millisecond"}) - void shouldReturnParserQuery6(String query) { - - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentEntity.class); - ArgumentCaptor durationCaptor = ArgumentCaptor.forClass(Duration.class); - - parser.query(query, manager, observer); - Mockito.verify(manager).insert(captor.capture(), durationCaptor.capture()); - DocumentEntity entity = captor.getValue(); - Duration duration = durationCaptor.getValue(); - - assertEquals("God", entity.name()); - assertEquals(Document.of("name", "Diana"), entity.find("name").get()); - assertEquals(Duration.ofMillis(10L), duration); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"insert God (name = \"Diana\") 10 nanosecond"}) - void shouldReturnParserQuery7(String query) { - - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentEntity.class); - ArgumentCaptor durationCaptor = ArgumentCaptor.forClass(Duration.class); - - parser.query(query, manager, observer); - Mockito.verify(manager).insert(captor.capture(), durationCaptor.capture()); - DocumentEntity entity = captor.getValue(); - Duration duration = durationCaptor.getValue(); - - assertEquals("God", entity.name()); - assertEquals(Document.of("name", "Diana"), entity.find("name").get()); - assertEquals(Duration.ofNanos(10L), duration); - } - - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"insert Person {\"name\":\"Ada Lovelace\"}"}) - void shouldReturnParserQuery8(String query) { - - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentEntity.class); - - parser.query(query, manager, observer); - Mockito.verify(manager).insert(captor.capture()); - DocumentEntity entity = captor.getValue(); - - assertEquals("Person", entity.name()); - assertEquals(Document.of("name", "Ada Lovelace"), entity.find("name").get()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"insert Person {\"name\":\"Ada Lovelace\"} 10 nanosecond"}) - void shouldReturnParserQuery9(String query) { - ArgumentCaptor durationCaptor = ArgumentCaptor.forClass(Duration.class); - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentEntity.class); - - parser.query(query, manager, observer); - Mockito.verify(manager).insert(captor.capture(), durationCaptor.capture()); - DocumentEntity entity = captor.getValue(); - Duration duration = durationCaptor.getValue(); - - assertEquals("Person", entity.name()); - assertEquals(Document.of("name", "Ada Lovelace"), entity.find("name").get()); - assertEquals(Duration.ofNanos(10L), duration); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"insert Person {\"name\": \"Ada Lovelace\", \"age\": 12, \"sibling\":" + - " [\"Ana\" ,\"Maria\"]," + - " \"address\":{\"country\": \"United Kingdom\", \"city\": \"London\"}}"}) - void shouldReturnParserQuery10(String query) { - - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentEntity.class); - - parser.query(query, manager, observer); - Mockito.verify(manager).insert(captor.capture()); - DocumentEntity entity = captor.getValue(); - List siblings = entity.find("sibling").get().get(new TypeReference<>() { - }); - List address = entity.find("address").get().get(new TypeReference<>() { - }); - assertEquals("Person", entity.name()); - assertEquals(Document.of("name", "Ada Lovelace"), entity.find("name").get()); - assertEquals(Document.of("age", BigDecimal.valueOf(12)), entity.find("age").get()); - assertThat(siblings).contains("Ana", "Maria"); - Assertions.assertThat(address).contains( - Document.of("country", "United Kingdom"), - Document.of("city", "London")); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"insert God (name = @name)"}) - void shouldReturnErrorWhenShouldUsePrepareStatement(String query) { - - assertThrows(QueryException.class, () -> parser.query(query, manager, observer)); - } - - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"insert God (name = @name)"}) - void shouldReturnErrorWhenDoesNotBindBeforeExecuteQuery(String query) { - - DocumentPreparedStatement prepare = parser.prepare(query, manager, observer); - assertThrows(QueryException.class, prepare::result); - } - - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"insert God (name = @name)"}) - void shouldExecutePrepareStatement(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentEntity.class); - DocumentPreparedStatement prepare = parser.prepare(query, manager, observer); - prepare.bind("name", "Diana"); - prepare.result(); - Mockito.verify(manager).insert(captor.capture()); - DocumentEntity entity = captor.getValue(); - assertEquals("God", entity.name()); - assertEquals(Document.of("name", "Diana"), entity.find("name").get()); - - } -} \ No newline at end of file diff --git a/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/SelectQueryParserTest.java b/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/SelectQueryParserTest.java deleted file mode 100644 index 37a3e9d11..000000000 --- a/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/SelectQueryParserTest.java +++ /dev/null @@ -1,513 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - -import org.assertj.core.api.Assertions; -import org.eclipse.jnosql.communication.Condition; -import org.eclipse.jnosql.communication.QueryException; -import jakarta.data.Sort; -import jakarta.data.Direction; -import org.eclipse.jnosql.communication.TypeReference; -import org.eclipse.jnosql.communication.Value; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; -import org.mockito.ArgumentCaptor; -import org.mockito.Mockito; - -import java.util.Arrays; -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.eclipse.jnosql.communication.document.DocumentCondition.eq; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; - -class SelectQueryParserTest { - - private final SelectQueryParser parser = new SelectQueryParser(); - - private final DocumentManager documentCollection = Mockito.mock(DocumentManager.class); - - private final DocumentObserverParser observer = new DocumentObserverParser() { - }; - - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select name, address from God"}) - void shouldReturnParserQuery1(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DefaultDocumentQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).select(captor.capture()); - DefaultDocumentQuery documentQuery = captor.getValue(); - - assertThat(documentQuery.documents()).contains("name", "address"); - assertTrue(documentQuery.sorts().isEmpty()); - assertEquals(0L, documentQuery.limit()); - assertEquals(0L, documentQuery.skip()); - assertEquals("God", documentQuery.name()); - assertFalse(documentQuery.condition().isPresent()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God order by name"}) - void shouldReturnParserQuery3(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DefaultDocumentQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).select(captor.capture()); - DefaultDocumentQuery documentQuery = captor.getValue(); - - assertTrue(documentQuery.documents().isEmpty()); - assertThat(documentQuery.sorts()).contains(Sort.of("name", Direction.ASC, false)); - assertEquals(0L, documentQuery.limit()); - assertEquals(0L, documentQuery.skip()); - assertEquals("God", documentQuery.name()); - assertFalse(documentQuery.condition().isPresent()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God order by name asc"}) - void shouldReturnParserQuery4(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DefaultDocumentQuery.class); - DocumentObserverParser observer = new DocumentObserverParser() { - }; - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).select(captor.capture()); - DefaultDocumentQuery documentQuery = captor.getValue(); - - assertTrue(documentQuery.documents().isEmpty()); - assertThat(documentQuery.sorts()).contains(Sort.of("name", Direction.ASC, false)); - assertEquals(0L, documentQuery.limit()); - assertEquals(0L, documentQuery.skip()); - assertEquals("God", documentQuery.name()); - assertFalse(documentQuery.condition().isPresent()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God order by name desc"}) - void shouldReturnParserQuery5(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DefaultDocumentQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).select(captor.capture()); - DefaultDocumentQuery documentQuery = captor.getValue(); - - assertTrue(documentQuery.documents().isEmpty()); - assertThat(documentQuery.sorts()).contains(Sort.of("name", Direction.DESC, false)); - assertEquals(0L, documentQuery.limit()); - assertEquals(0L, documentQuery.skip()); - assertEquals("God", documentQuery.name()); - assertFalse(documentQuery.condition().isPresent()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God order by name desc age asc"}) - void shouldReturnParserQuery6(String query) { - - ArgumentCaptor captor = ArgumentCaptor.forClass(DefaultDocumentQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).select(captor.capture()); - DefaultDocumentQuery documentQuery = captor.getValue(); - - assertTrue(documentQuery.documents().isEmpty()); - assertThat(documentQuery.sorts()).contains(Sort.of("name", Direction.DESC, false), - Sort.of("age", Direction.ASC, false)); - assertEquals(0L, documentQuery.limit()); - assertEquals(0L, documentQuery.skip()); - assertEquals("God", documentQuery.name()); - assertFalse(documentQuery.condition().isPresent()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God skip 12"}) - void shouldReturnParserQuery7(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DefaultDocumentQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).select(captor.capture()); - DefaultDocumentQuery documentQuery = captor.getValue(); - - checkBaseQuery(documentQuery, 0L, 12L); - assertFalse(documentQuery.condition().isPresent()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God limit 12"}) - void shouldReturnParserQuery8(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DefaultDocumentQuery.class); - DocumentObserverParser observer = new DocumentObserverParser() { - }; - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).select(captor.capture()); - DefaultDocumentQuery documentQuery = captor.getValue(); - - checkBaseQuery(documentQuery, 12L, 0L); - assertFalse(documentQuery.condition().isPresent()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God skip 10 limit 12"}) - void shouldReturnParserQuery9(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DefaultDocumentQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).select(captor.capture()); - DefaultDocumentQuery documentQuery = captor.getValue(); - - assertTrue(documentQuery.documents().isEmpty()); - assertTrue(documentQuery.sorts().isEmpty()); - assertEquals(12L, documentQuery.limit()); - assertEquals(10L, documentQuery.skip()); - assertEquals("God", documentQuery.name()); - assertFalse(documentQuery.condition().isPresent()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where age = 10"}) - void shouldReturnParserQuery10(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DefaultDocumentQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).select(captor.capture()); - DefaultDocumentQuery documentQuery = captor.getValue(); - - checkBaseQuery(documentQuery, 0L, 0L); - assertTrue(documentQuery.condition().isPresent()); - DocumentCondition condition = documentQuery.condition().get(); - - assertEquals(Condition.EQUALS, condition.condition()); - assertEquals(Document.of("age", 10L), condition.document()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where stamina > 10.23"}) - void shouldReturnParserQuery11(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DefaultDocumentQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).select(captor.capture()); - DefaultDocumentQuery documentQuery = captor.getValue(); - - checkBaseQuery(documentQuery, 0L, 0L); - assertTrue(documentQuery.condition().isPresent()); - DocumentCondition condition = documentQuery.condition().get(); - - assertEquals(Condition.GREATER_THAN, condition.condition()); - assertEquals(Document.of("stamina", 10.23), condition.document()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where stamina >= -10.23"}) - void shouldReturnParserQuery12(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DefaultDocumentQuery.class); - DocumentObserverParser observer = new DocumentObserverParser() { - }; - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).select(captor.capture()); - DefaultDocumentQuery documentQuery = captor.getValue(); - - checkBaseQuery(documentQuery, 0L, 0L); - assertTrue(documentQuery.condition().isPresent()); - DocumentCondition condition = documentQuery.condition().get(); - - assertEquals(Condition.GREATER_EQUALS_THAN, condition.condition()); - assertEquals(Document.of("stamina", -10.23), condition.document()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where stamina <= -10.23"}) - void shouldReturnParserQuery13(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DefaultDocumentQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).select(captor.capture()); - DefaultDocumentQuery documentQuery = captor.getValue(); - - checkBaseQuery(documentQuery, 0L, 0L); - assertTrue(documentQuery.condition().isPresent()); - DocumentCondition condition = documentQuery.condition().get(); - - assertEquals(Condition.LESSER_EQUALS_THAN, condition.condition()); - assertEquals(Document.of("stamina", -10.23), condition.document()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where stamina < -10.23"}) - void shouldReturnParserQuery14(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DefaultDocumentQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).select(captor.capture()); - DefaultDocumentQuery documentQuery = captor.getValue(); - - checkBaseQuery(documentQuery, 0L, 0L); - assertTrue(documentQuery.condition().isPresent()); - DocumentCondition condition = documentQuery.condition().get(); - - assertEquals(Condition.LESSER_THAN, condition.condition()); - assertEquals(Document.of("stamina", -10.23), condition.document()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where age between 10 and 30"}) - void shouldReturnParserQuery15(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DefaultDocumentQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).select(captor.capture()); - DefaultDocumentQuery documentQuery = captor.getValue(); - - checkBaseQuery(documentQuery, 0L, 0L); - assertTrue(documentQuery.condition().isPresent()); - DocumentCondition condition = documentQuery.condition().get(); - - assertEquals(Condition.BETWEEN, condition.condition()); - assertEquals(Document.of("age", Arrays.asList(10L, 30L)), condition.document()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where name = \"diana\""}) - void shouldReturnParserQuery16(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DefaultDocumentQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).select(captor.capture()); - DefaultDocumentQuery documentQuery = captor.getValue(); - - checkBaseQuery(documentQuery, 0L, 0L); - assertTrue(documentQuery.condition().isPresent()); - DocumentCondition condition = documentQuery.condition().get(); - - assertEquals(Condition.EQUALS, condition.condition()); - assertEquals(Document.of("name", "diana"), condition.document()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where siblings = {\"apollo\": \"Brother\", \"Zeus\": \"Father\"}"}) - void shouldReturnParserQuery18(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DefaultDocumentQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).select(captor.capture()); - DefaultDocumentQuery documentQuery = captor.getValue(); - - checkBaseQuery(documentQuery, 0L, 0L); - assertTrue(documentQuery.condition().isPresent()); - DocumentCondition condition = documentQuery.condition().get(); - - assertEquals(Condition.EQUALS, condition.condition()); - Document document = condition.document(); - List documents = document.get(new TypeReference<>() { - }); - Assertions.assertThat(documents).contains(Document.of("apollo", "Brother"), - Document.of("Zeus", "Father")); - assertEquals("siblings", document.name()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where age = convert(12, java.lang.Integer)"}) - void shouldReturnParserQuery19(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DefaultDocumentQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).select(captor.capture()); - DefaultDocumentQuery documentQuery = captor.getValue(); - - checkBaseQuery(documentQuery, 0L, 0L); - assertTrue(documentQuery.condition().isPresent()); - DocumentCondition condition = documentQuery.condition().get(); - Document document = condition.document(); - assertEquals(Condition.EQUALS, condition.condition()); - assertEquals("age", document.name()); - assertEquals(Value.of(12), document.value()); - - - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where name in (\"Ada\", \"Apollo\")"}) - void shouldReturnParserQuery20(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DefaultDocumentQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).select(captor.capture()); - DefaultDocumentQuery documentQuery = captor.getValue(); - - checkBaseQuery(documentQuery, 0L, 0L); - assertTrue(documentQuery.condition().isPresent()); - DocumentCondition condition = documentQuery.condition().get(); - Document document = condition.document(); - assertEquals(Condition.IN, condition.condition()); - assertEquals("name", document.name()); - List values = document.get(new TypeReference<>() { - }); - assertThat(values).contains("Ada", "Apollo"); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where name like \"Ada\""}) - void shouldReturnParserQuery21(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DefaultDocumentQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).select(captor.capture()); - DefaultDocumentQuery documentQuery = captor.getValue(); - - checkBaseQuery(documentQuery, 0L, 0L); - assertTrue(documentQuery.condition().isPresent()); - DocumentCondition condition = documentQuery.condition().get(); - Document document = condition.document(); - assertEquals(Condition.LIKE, condition.condition()); - assertEquals("name", document.name()); - assertEquals("Ada", document.get()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where name not like \"Ada\""}) - void shouldReturnParserQuery22(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DefaultDocumentQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).select(captor.capture()); - DefaultDocumentQuery documentQuery = captor.getValue(); - - checkBaseQuery(documentQuery, 0L, 0L); - assertTrue(documentQuery.condition().isPresent()); - DocumentCondition condition = documentQuery.condition().get(); - Document document = condition.document(); - assertEquals(Condition.NOT, condition.condition()); - List conditions = document.get(new TypeReference<>() { - }); - DocumentCondition documentCondition = conditions.get(0); - assertEquals(Condition.LIKE, documentCondition.condition()); - assertEquals(Document.of("name", "Ada"), documentCondition.document()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where name = \"Ada\" and age = 20"}) - void shouldReturnParserQuery23(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DefaultDocumentQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).select(captor.capture()); - DefaultDocumentQuery documentQuery = captor.getValue(); - - checkBaseQuery(documentQuery, 0L, 0L); - assertTrue(documentQuery.condition().isPresent()); - DocumentCondition condition = documentQuery.condition().get(); - Document document = condition.document(); - assertEquals(Condition.AND, condition.condition()); - List conditions = document.get(new TypeReference<>() { - }); - Assertions.assertThat(conditions).contains(eq(Document.of("name", "Ada")), - eq(Document.of("age", 20L))); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where name = \"Ada\" or age = 20"}) - void shouldReturnParserQuery24(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DefaultDocumentQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).select(captor.capture()); - DefaultDocumentQuery documentQuery = captor.getValue(); - - checkBaseQuery(documentQuery, 0L, 0L); - assertTrue(documentQuery.condition().isPresent()); - DocumentCondition condition = documentQuery.condition().get(); - Document document = condition.document(); - assertEquals(Condition.OR, condition.condition()); - List conditions = document.get(new TypeReference<>() { - }); - Assertions.assertThat(conditions).contains(eq(Document.of("name", "Ada")), - eq(Document.of("age", 20L))); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where name = \"Ada\" and age = 20 or" + - " siblings = {\"apollo\": \"Brother\", \"Zeus\": \"Father\"}"}) - void shouldReturnParserQuery25(String query) { - - ArgumentCaptor captor = ArgumentCaptor.forClass(DefaultDocumentQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).select(captor.capture()); - DefaultDocumentQuery documentQuery = captor.getValue(); - - checkBaseQuery(documentQuery, 0L, 0L); - assertTrue(documentQuery.condition().isPresent()); - DocumentCondition condition = documentQuery.condition().get(); - Document document = condition.document(); - assertEquals(Condition.AND, condition.condition()); - List conditions = document.get(new TypeReference<>() { - }); - assertEquals(Condition.EQUALS, conditions.get(0).condition()); - assertEquals(Condition.EQUALS, conditions.get(1).condition()); - assertEquals(Condition.OR, conditions.get(2).condition()); - - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where name = \"Ada\" and age = 20 or" + - " siblings = {\"apollo\": \"Brother\", \"Zeus\": \"Father\"} and birthday =" + - " convert(\"2007-12-03\", java.time.LocalDate)"}) - void shouldReturnParserQuery26(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DefaultDocumentQuery.class); - parser.query(query, documentCollection, observer); - Mockito.verify(documentCollection).select(captor.capture()); - DefaultDocumentQuery documentQuery = captor.getValue(); - - checkBaseQuery(documentQuery, 0L, 0L); - assertTrue(documentQuery.condition().isPresent()); - DocumentCondition condition = documentQuery.condition().get(); - Document document = condition.document(); - assertEquals(Condition.AND, condition.condition()); - List conditions = document.get(new TypeReference<>() { - }); - assertEquals(Condition.EQUALS, conditions.get(0).condition()); - assertEquals(Condition.EQUALS, conditions.get(1).condition()); - assertEquals(Condition.OR, conditions.get(2).condition()); - assertEquals(Condition.EQUALS, conditions.get(3).condition()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where age = @age"}) - void shouldReturnErrorWhenIsQueryWithParam(String query) { - - assertThrows(QueryException.class, () -> parser.query(query, documentCollection, observer)); - - - } - - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where age = @age"}) - void shouldReturnErrorWhenDontBindParameters(String query) { - - DocumentPreparedStatement prepare = parser.prepare(query, documentCollection, observer); - assertThrows(QueryException.class, prepare::result); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"select * from God where age = @age"}) - void shouldExecutePrepareStatement(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DefaultDocumentQuery.class); - - DocumentPreparedStatement prepare = parser.prepare(query, documentCollection, observer); - prepare.bind("age", 12); - prepare.result(); - Mockito.verify(documentCollection).select(captor.capture()); - DefaultDocumentQuery documentQuery = captor.getValue(); - DocumentCondition documentCondition = documentQuery.condition().get(); - Document document = documentCondition.document(); - assertEquals(Condition.EQUALS, documentCondition.condition()); - assertEquals("age", document.name()); - assertEquals(12, document.get()); - } - - private void checkBaseQuery(DefaultDocumentQuery documentQuery, long limit, long skip) { - assertTrue(documentQuery.documents().isEmpty()); - assertTrue(documentQuery.sorts().isEmpty()); - assertEquals(limit, documentQuery.limit()); - assertEquals(skip, documentQuery.skip()); - assertEquals("God", documentQuery.name()); - } -} diff --git a/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/UpdateQueryParserTest.java b/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/UpdateQueryParserTest.java deleted file mode 100644 index 3e3d087fd..000000000 --- a/jnosql-communication/jnosql-communication-document/src/test/java/org/eclipse/jnosql/communication/document/UpdateQueryParserTest.java +++ /dev/null @@ -1,138 +0,0 @@ -/* - * - * Copyright (c) 2022 Contributors to the Eclipse Foundation - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Apache License v2.0 which accompanies this distribution. - * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html - * and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. - * - * You may elect to redistribute this code under either of these licenses. - * - * Contributors: - * - * Otavio Santana - * - */ -package org.eclipse.jnosql.communication.document; - -import org.assertj.core.api.Assertions; -import org.eclipse.jnosql.communication.QueryException; -import org.eclipse.jnosql.communication.TypeReference; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; -import org.mockito.ArgumentCaptor; -import org.mockito.Mockito; - -import java.math.BigDecimal; -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - -class UpdateQueryParserTest { - - private final UpdateQueryParser parser = new UpdateQueryParser(); - - private final DocumentManager manager = Mockito.mock(DocumentManager.class); - - private final DocumentObserverParser observer = new DocumentObserverParser() { - }; - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"update God (name = \"Diana\")"}) - void shouldReturnParserQuery(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentEntity.class); - parser.query(query, manager, observer); - Mockito.verify(manager).update(captor.capture()); - DocumentEntity entity = captor.getValue(); - - - assertEquals("God", entity.name()); - assertEquals(Document.of("name", "Diana"), entity.find("name").get()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"update God (age = 30, name = \"Artemis\")"}) - void shouldReturnParserQuery1(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentEntity.class); - parser.query(query, manager, observer); - Mockito.verify(manager).update(captor.capture()); - DocumentEntity entity = captor.getValue(); - - assertEquals("God", entity.name()); - assertEquals(Document.of("name", "Artemis"), entity.find("name").get()); - assertEquals(Document.of("age", 30L), entity.find("age").get()); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"update God (name = @name)"}) - void shouldReturnParserQuery2(String query) { - - assertThrows(QueryException.class, () -> parser.query(query, manager, observer)); - } - - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"update Person {\"name\":\"Ada Lovelace\"}"}) - void shouldReturnParserQuery3(String query) { - - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentEntity.class); - - parser.query(query, manager, observer); - Mockito.verify(manager).update(captor.capture()); - DocumentEntity entity = captor.getValue(); - - assertEquals("Person", entity.name()); - assertEquals(Document.of("name", "Ada Lovelace"), entity.find("name").get()); - } - - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"update Person {\"name\": \"Ada Lovelace\", \"age\": 12, \"sibling\":" + - " [\"Ana\" ,\"Maria\"]," + - " \"address\":{\"country\": \"United Kingdom\", \"city\": \"London\"}}"}) - void shouldReturnParserQuery4(String query) { - - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentEntity.class); - - parser.query(query, manager, observer); - Mockito.verify(manager).update(captor.capture()); - DocumentEntity entity = captor.getValue(); - List siblings = entity.find("sibling").get().get(new TypeReference<>() { - }); - List address = entity.find("address").get().get(new TypeReference<>() { - }); - assertEquals("Person", entity.name()); - assertEquals(Document.of("name", "Ada Lovelace"), entity.find("name").get()); - assertEquals(Document.of("age", BigDecimal.valueOf(12)), entity.find("age").get()); - assertThat(siblings).contains("Ana", "Maria"); - Assertions.assertThat(address).contains( - Document.of("country", "United Kingdom"), - Document.of("city", "London")); - } - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"update God (name = @name)"}) - void shouldReturnErrorWhenDoesNotBindBeforeExecuteQuery(String query) { - - DocumentPreparedStatement prepare = parser.prepare(query, manager, observer); - assertThrows(QueryException.class, prepare::result); - } - - - @ParameterizedTest(name = "Should parser the query {0}") - @ValueSource(strings = {"update God (name = @name)"}) - void shouldExecutePrepareStatement(String query) { - ArgumentCaptor captor = ArgumentCaptor.forClass(DocumentEntity.class); - DocumentPreparedStatement prepare = parser.prepare(query, manager, observer); - prepare.bind("name", "Diana"); - prepare.result(); - Mockito.verify(manager).update(captor.capture()); - DocumentEntity entity = captor.getValue(); - assertEquals("God", entity.name()); - assertEquals(Document.of("name", "Diana"), entity.find("name").get()); - - } -} \ No newline at end of file