diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..8f19212 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,19 @@ +name: Build Artifact + +on: + pull_request: + branches: + - '*' + +jobs: + build: + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v2 + - name: Set up Maven Central Repository + uses: actions/setup-java@v2 + with: + java-version: '15' + distribution: 'adopt' + - name: Build package + run: mvn --batch-mode clean package \ No newline at end of file diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..14e50ed --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,22 @@ +name: Deploy Artifact + +on: + push: + branches: + - 'master' + - 'main' + +jobs: + deploy: + runs-on: ubuntu-20.04 + steps: + - name: Checkout Repo + uses: actions/checkout@v2 + + - name: Deploy package + uses: valitydev/action-deploy-jdk-package@v1.0.9 + with: + server-username: ${{ secrets.OSSRH_USERNAME }} + server-password: ${{ secrets.OSSRH_TOKEN }} + deploy-secret-key: ${{ secrets.OSSRH_GPG_SECRET_KEY }} + deploy-secret-key-password: ${{ secrets.OSSRH_GPG_SECRET_KEY_PASSWORD }} \ No newline at end of file diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 4a5266f..0000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "build_utils"] - path = build_utils - url = git@github.com:rbkmoney/build_utils.git diff --git a/Jenkinsfile b/Jenkinsfile deleted file mode 100644 index f20ea03..0000000 --- a/Jenkinsfile +++ /dev/null @@ -1,13 +0,0 @@ -#!groovy -build('magista_dsl', 'docker-host') { - checkoutRepo() - loadBuildUtils() - - def javaLibPipeline - runStage('load JavaLib pipeline') { - javaLibPipeline = load("build_utils/jenkins_lib/pipeJavaLib.groovy") - } - - def buildImageTag = "fcf116dd775cc2e91bffb6a36835754e3f2d5321" - javaLibPipeline(buildImageTag) -} \ No newline at end of file diff --git a/build_utils b/build_utils deleted file mode 160000 index e131872..0000000 --- a/build_utils +++ /dev/null @@ -1 +0,0 @@ -Subproject commit e1318727d4d0c3e48f5122bf3197158b6695f50e diff --git a/pom.xml b/pom.xml index 857955e..6173020 100644 --- a/pom.xml +++ b/pom.xml @@ -5,9 +5,9 @@ 4.0.0 - com.rbkmoney - parent - 1.0.5 + dev.vality + library-parent-pom + 1.0.0 magista-dsl diff --git a/src/main/java/com/rbkmoney/magista/dsl/builder/AbstractQueryBuilder.java b/src/main/java/com/rbkmoney/magista/dsl/builder/AbstractQueryBuilder.java deleted file mode 100644 index d0e0006..0000000 --- a/src/main/java/com/rbkmoney/magista/dsl/builder/AbstractQueryBuilder.java +++ /dev/null @@ -1,89 +0,0 @@ -package com.rbkmoney.magista.dsl.builder; - -import com.rbkmoney.magista.dsl.*; -import com.rbkmoney.magista.dsl.parser.QueryPart; - -import java.util.Collections; -import java.util.List; -import java.util.function.BiFunction; -import java.util.function.Function; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -public abstract class AbstractQueryBuilder implements QueryBuilder { - - protected Stream getMatchedPartsStream(Object descriptor, List queryParts) { - return queryParts.stream().filter(queryPart -> descriptor.equals(queryPart.getDescriptor())); - } - - - protected CompositeQuery createCompositeQuery( - Object descriptor, - QueryParameters derivedParameters, - List childQueries, - Function> execFunction, - BiFunction, QueryResult> parallelExecFunction - ) { - return BaseCompositeQuery.newInstance(descriptor, new QueryParameters(Collections.emptyMap(), derivedParameters), childQueries, execFunction, parallelExecFunction); - } - - protected CompositeQuery createCompositeQuery( - Object descriptor, - QueryParameters derivedParameters, - List childQueries - ) { - return createCompositeQuery( - descriptor, derivedParameters, childQueries, - context -> new BaseQueryResult<>( - () -> childQueries.stream().map(query -> query.execute(context)), - () -> childQueries.stream().map(query -> query.execute(context)).collect(Collectors.toList()) - ), - (context, queryResults) -> new BaseQueryResult<>( - () -> queryResults.stream(), - () -> queryResults - ) - - ); - } - - protected CompositeQuery createCompositeQuery( - Object descriptor, - List childQueries, - Function> execFunction, - BiFunction, QueryResult> parallelExecFunction - ) { - return createCompositeQuery(descriptor, new QueryParameters(Collections.emptyMap(), null), childQueries, execFunction, parallelExecFunction); - } - - protected QueryParameters getParameters(QueryPart queryPart) { - return queryPart == null ? null : queryPart.getParameters(); - } - - protected List buildQueries(Object matchDescriptor, List queryParts, Function queryCreator) { - List matchedParts = getMatchedPartsStream(matchDescriptor, queryParts).collect(Collectors.toList()); - return matchedParts.stream().map(queryPart -> queryCreator.apply(queryPart)).collect(Collectors.toList()); - } - - protected Query buildSingleQuery(Object matchDescriptor, List queryParts, Function queryCreator) { - List queries = buildQueries(matchDescriptor, queryParts, queryCreator); - if (queries.size() == 0) { - throw new QueryBuilderException("No queries found in referred data"); - } else if (queries.size() > 1) { - throw new QueryBuilderException("Only one query expected"); - } else { - return queries.get(0); - } - } - - protected Query buildAndWrapQueries( - Object matchDescriptor, - List queryParts, - Function queryCreator, - QueryParameters parentParameters, - Function> execFunction, - BiFunction, QueryResult> parallelExecFunction - ) { - List queries = buildQueries(matchDescriptor, queryParts, queryCreator); - return createCompositeQuery(queries.get(0).getDescriptor(), parentParameters, queries, execFunction, parallelExecFunction); - } -} diff --git a/src/main/java/com/rbkmoney/magista/dsl/builder/QueryBuilder.java b/src/main/java/com/rbkmoney/magista/dsl/builder/QueryBuilder.java deleted file mode 100644 index f951de1..0000000 --- a/src/main/java/com/rbkmoney/magista/dsl/builder/QueryBuilder.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.rbkmoney.magista.dsl.builder; - -import com.rbkmoney.magista.dsl.Query; -import com.rbkmoney.magista.dsl.parser.QueryPart; - -import java.util.List; - -public interface QueryBuilder { - - Query buildQuery(List queryParts, String continuationToken, QueryPart parentQueryPart, QueryBuilder baseBuilder) throws QueryBuilderException; - - boolean apply(List queryParts, QueryPart parent); -} diff --git a/src/main/java/com/rbkmoney/magista/dsl/parser/QueryParser.java b/src/main/java/com/rbkmoney/magista/dsl/parser/QueryParser.java deleted file mode 100644 index 1906cf1..0000000 --- a/src/main/java/com/rbkmoney/magista/dsl/parser/QueryParser.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.rbkmoney.magista.dsl.parser; - - -import java.util.List; - -public interface QueryParser { - List parseQuery(Src source, QueryPart parent) throws QueryParserException; - boolean apply(Src source, QueryPart parent); -} diff --git a/src/main/java/com/rbkmoney/magista/dsl/BadTokenException.java b/src/main/java/dev/vality/magista/dsl/BadTokenException.java similarity index 91% rename from src/main/java/com/rbkmoney/magista/dsl/BadTokenException.java rename to src/main/java/dev/vality/magista/dsl/BadTokenException.java index 6d646f0..3b12973 100644 --- a/src/main/java/com/rbkmoney/magista/dsl/BadTokenException.java +++ b/src/main/java/dev/vality/magista/dsl/BadTokenException.java @@ -1,4 +1,4 @@ -package com.rbkmoney.magista.dsl; +package dev.vality.magista.dsl; public class BadTokenException extends IllegalArgumentException { diff --git a/src/main/java/com/rbkmoney/magista/dsl/BaseCompositeQuery.java b/src/main/java/dev/vality/magista/dsl/BaseCompositeQuery.java similarity index 59% rename from src/main/java/com/rbkmoney/magista/dsl/BaseCompositeQuery.java rename to src/main/java/dev/vality/magista/dsl/BaseCompositeQuery.java index bbfc3a8..9206429 100644 --- a/src/main/java/com/rbkmoney/magista/dsl/BaseCompositeQuery.java +++ b/src/main/java/dev/vality/magista/dsl/BaseCompositeQuery.java @@ -1,4 +1,4 @@ -package com.rbkmoney.magista.dsl; +package dev.vality.magista.dsl; import java.util.List; @@ -7,22 +7,13 @@ import java.util.function.Function; import java.util.stream.Collectors; public class BaseCompositeQuery extends BaseQuery implements CompositeQuery { - private List queries; private final Function> execFunction; private final BiFunction, QueryResult> parallelExecFunction; + private List queries; - public static BaseCompositeQuery newInstance(Object descriptor, QueryParameters params, List queries, Function> execFunction) { - return newInstance(descriptor, params, queries, execFunction, null); - } - - public static BaseCompositeQuery newInstance(Object descriptor, QueryParameters params, List queries, Function> execFunction, BiFunction, QueryResult> parallelExecFunction) { - BaseCompositeQuery compositeQuery = new BaseCompositeQuery<>(descriptor, params, execFunction, parallelExecFunction); - compositeQuery.setChildQueries(queries); - return compositeQuery; - } - - - public BaseCompositeQuery(Object descriptor, QueryParameters params, Function> execFunction, BiFunction, QueryResult> parallelExecFunction) { + public BaseCompositeQuery(Object descriptor, QueryParameters params, + Function> execFunction, + BiFunction, QueryResult> parallelExecFunction) { super(descriptor, params); if (execFunction == null) { throw new NullPointerException("Null exec function is not allowed"); @@ -31,10 +22,32 @@ public class BaseCompositeQuery extends BaseQuery implements Compo this.parallelExecFunction = parallelExecFunction; } + public static BaseCompositeQuery newInstance(Object descriptor, QueryParameters params, + List queries, + Function> execFunction) { + return newInstance(descriptor, params, queries, execFunction, null); + } + + public static BaseCompositeQuery newInstance(Object descriptor, QueryParameters params, + List queries, + Function> execFunction, + BiFunction, + QueryResult> parallelExecFunction) { + BaseCompositeQuery compositeQuery = + new BaseCompositeQuery<>(descriptor, params, execFunction, parallelExecFunction); + compositeQuery.setChildQueries(queries); + return compositeQuery; + } + public List getChildQueries() { return queries; } + protected void setChildQueries(List queries) { + this.queries = queries.stream().peek(query -> query.setParentQuery(this)).collect(Collectors.toList()); + } + public boolean isParallel() { return parallelExecFunction != null; } @@ -45,15 +58,12 @@ public class BaseCompositeQuery extends BaseQuery implements Compo } @Override - public QueryResult execute(QueryContext context, List collectedResults) throws QueryExecutionException { + public QueryResult execute(QueryContext context, List collectedResults) + throws QueryExecutionException { if (parallelExecFunction != null) { return parallelExecFunction.apply(context, collectedResults); } else { return CompositeQuery.super.execute(context, collectedResults); } } - - protected void setChildQueries(List queries) { - this.queries = queries.stream().peek(query -> query.setParentQuery(this)).collect(Collectors.toList()); - } } diff --git a/src/main/java/com/rbkmoney/magista/dsl/BaseFunction.java b/src/main/java/dev/vality/magista/dsl/BaseFunction.java similarity index 91% rename from src/main/java/com/rbkmoney/magista/dsl/BaseFunction.java rename to src/main/java/dev/vality/magista/dsl/BaseFunction.java index 9f8b6d5..b5e653a 100644 --- a/src/main/java/com/rbkmoney/magista/dsl/BaseFunction.java +++ b/src/main/java/dev/vality/magista/dsl/BaseFunction.java @@ -1,4 +1,4 @@ -package com.rbkmoney.magista.dsl; +package dev.vality.magista.dsl; public abstract class BaseFunction extends BaseQuery implements FunctionQuery { diff --git a/src/main/java/com/rbkmoney/magista/dsl/BaseQuery.java b/src/main/java/dev/vality/magista/dsl/BaseQuery.java similarity index 97% rename from src/main/java/com/rbkmoney/magista/dsl/BaseQuery.java rename to src/main/java/dev/vality/magista/dsl/BaseQuery.java index 1f1fdb2..6e421c6 100644 --- a/src/main/java/com/rbkmoney/magista/dsl/BaseQuery.java +++ b/src/main/java/dev/vality/magista/dsl/BaseQuery.java @@ -1,4 +1,4 @@ -package com.rbkmoney.magista.dsl; +package dev.vality.magista.dsl; public abstract class BaseQuery implements Query { private final Object descriptor; diff --git a/src/main/java/com/rbkmoney/magista/dsl/BaseQueryResult.java b/src/main/java/dev/vality/magista/dsl/BaseQueryResult.java similarity index 94% rename from src/main/java/com/rbkmoney/magista/dsl/BaseQueryResult.java rename to src/main/java/dev/vality/magista/dsl/BaseQueryResult.java index 4e7cb83..1d04b06 100644 --- a/src/main/java/com/rbkmoney/magista/dsl/BaseQueryResult.java +++ b/src/main/java/dev/vality/magista/dsl/BaseQueryResult.java @@ -1,4 +1,4 @@ -package com.rbkmoney.magista.dsl; +package dev.vality.magista.dsl; import java.util.function.Supplier; import java.util.stream.Stream; diff --git a/src/main/java/com/rbkmoney/magista/dsl/BaseQueryValidator.java b/src/main/java/dev/vality/magista/dsl/BaseQueryValidator.java similarity index 82% rename from src/main/java/com/rbkmoney/magista/dsl/BaseQueryValidator.java rename to src/main/java/dev/vality/magista/dsl/BaseQueryValidator.java index f42c040..268b533 100644 --- a/src/main/java/com/rbkmoney/magista/dsl/BaseQueryValidator.java +++ b/src/main/java/dev/vality/magista/dsl/BaseQueryValidator.java @@ -1,4 +1,4 @@ -package com.rbkmoney.magista.dsl; +package dev.vality.magista.dsl; import java.time.Instant; import java.time.temporal.TemporalAccessor; @@ -21,9 +21,11 @@ public abstract class BaseQueryValidator implements QueryValidator { } } - protected T checkParamsType(QueryParameters parameters, Class expectedType) { + protected T checkParamsType(QueryParameters parameters, + Class expectedType) { if (!expectedType.isAssignableFrom(parameters.getClass())) { - checkParamsResult(true, "Parameters has wrong type:" + parameters.getClass() + ", expected: " + expectedType); + checkParamsResult(true, + "Parameters has wrong type:" + parameters.getClass() + ", expected: " + expectedType); } return (T) parameters; } diff --git a/src/main/java/com/rbkmoney/magista/dsl/CompositeQuery.java b/src/main/java/dev/vality/magista/dsl/CompositeQuery.java similarity index 75% rename from src/main/java/com/rbkmoney/magista/dsl/CompositeQuery.java rename to src/main/java/dev/vality/magista/dsl/CompositeQuery.java index b5af4bb..6e5ba5a 100644 --- a/src/main/java/com/rbkmoney/magista/dsl/CompositeQuery.java +++ b/src/main/java/dev/vality/magista/dsl/CompositeQuery.java @@ -1,4 +1,4 @@ -package com.rbkmoney.magista.dsl; +package dev.vality.magista.dsl; import java.util.List; @@ -9,7 +9,8 @@ public interface CompositeQuery extends Query { return false; } - default QueryResult execute(QueryContext context, List collectedResults) throws QueryExecutionException { + default QueryResult execute(QueryContext context, List collectedResults) + throws QueryExecutionException { throw new UnsupportedOperationException("Explicit implementation required"); } diff --git a/src/main/java/com/rbkmoney/magista/dsl/FunctionQuery.java b/src/main/java/dev/vality/magista/dsl/FunctionQuery.java similarity index 71% rename from src/main/java/com/rbkmoney/magista/dsl/FunctionQuery.java rename to src/main/java/dev/vality/magista/dsl/FunctionQuery.java index fedc616..2e16e03 100644 --- a/src/main/java/com/rbkmoney/magista/dsl/FunctionQuery.java +++ b/src/main/java/dev/vality/magista/dsl/FunctionQuery.java @@ -1,4 +1,4 @@ -package com.rbkmoney.magista.dsl; +package dev.vality.magista.dsl; public interface FunctionQuery extends Query { diff --git a/src/main/java/com/rbkmoney/magista/dsl/PagedBaseFunction.java b/src/main/java/dev/vality/magista/dsl/PagedBaseFunction.java similarity index 76% rename from src/main/java/com/rbkmoney/magista/dsl/PagedBaseFunction.java rename to src/main/java/dev/vality/magista/dsl/PagedBaseFunction.java index 0285fbf..c8f569f 100644 --- a/src/main/java/com/rbkmoney/magista/dsl/PagedBaseFunction.java +++ b/src/main/java/dev/vality/magista/dsl/PagedBaseFunction.java @@ -1,15 +1,13 @@ -package com.rbkmoney.magista.dsl; +package dev.vality.magista.dsl; import java.util.Map; import java.util.Optional; -import static com.rbkmoney.magista.dsl.Parameters.SIZE_PARAMETER; - public abstract class PagedBaseFunction extends BaseFunction { public static final int MAX_SIZE_VALUE = 1000; - private String continuationToken; + private final String continuationToken; public PagedBaseFunction(Object descriptor, QueryParameters params, String name, String continuationToken) { super(descriptor, params, name); @@ -35,8 +33,7 @@ public abstract class PagedBaseFunction extends BaseFunction { } public Integer getSize() { - return Optional.ofNullable(getIntParameter(SIZE_PARAMETER, true)) - .orElse(MAX_SIZE_VALUE); + return Optional.ofNullable(getIntParameter(Parameters.SIZE_PARAMETER, true)).orElse(MAX_SIZE_VALUE); } } @@ -46,7 +43,8 @@ public abstract class PagedBaseFunction extends BaseFunction { public void validateQuery(Query query) throws IllegalArgumentException { super.validateQuery(query); if (query instanceof PagedBaseFunction) { - validateContinuationToken(query.getQueryParameters(), ((PagedBaseFunction) query).getContinuationToken()); + validateContinuationToken(query.getQueryParameters(), + ((PagedBaseFunction) query).getContinuationToken()); } } @@ -55,15 +53,12 @@ public abstract class PagedBaseFunction extends BaseFunction { super.validateParameters(parameters); PagedBaseParameters pagedBaseParameters = super.checkParamsType(parameters, PagedBaseParameters.class); checkParamsResult(pagedBaseParameters.getSize() > MAX_SIZE_VALUE, - String.format( - "Size must be less or equals to %d but was %d", - MAX_SIZE_VALUE, - pagedBaseParameters.getSize() - ) - ); + String.format("Size must be less or equals to %d but was %d", MAX_SIZE_VALUE, + pagedBaseParameters.getSize())); } - private void validateContinuationToken(QueryParameters queryParameters, String continuationToken) throws BadTokenException { + private void validateContinuationToken(QueryParameters queryParameters, String continuationToken) + throws BadTokenException { try { TokenUtil.validateToken(queryParameters, continuationToken); } catch (IllegalArgumentException ex) { diff --git a/src/main/java/com/rbkmoney/magista/dsl/Parameters.java b/src/main/java/dev/vality/magista/dsl/Parameters.java similarity index 80% rename from src/main/java/com/rbkmoney/magista/dsl/Parameters.java rename to src/main/java/dev/vality/magista/dsl/Parameters.java index 5a0b6e7..db2b656 100644 --- a/src/main/java/com/rbkmoney/magista/dsl/Parameters.java +++ b/src/main/java/dev/vality/magista/dsl/Parameters.java @@ -1,4 +1,4 @@ -package com.rbkmoney.magista.dsl; +package dev.vality.magista.dsl; public class Parameters { public static final String QUERY_PARAMETER = "query"; diff --git a/src/main/java/com/rbkmoney/magista/dsl/Query.java b/src/main/java/dev/vality/magista/dsl/Query.java similarity index 88% rename from src/main/java/com/rbkmoney/magista/dsl/Query.java rename to src/main/java/dev/vality/magista/dsl/Query.java index 1b61c09..9773f17 100644 --- a/src/main/java/com/rbkmoney/magista/dsl/Query.java +++ b/src/main/java/dev/vality/magista/dsl/Query.java @@ -1,4 +1,4 @@ -package com.rbkmoney.magista.dsl; +package dev.vality.magista.dsl; public interface Query { Object getDescriptor(); diff --git a/src/main/java/com/rbkmoney/magista/dsl/QueryContext.java b/src/main/java/dev/vality/magista/dsl/QueryContext.java similarity index 50% rename from src/main/java/com/rbkmoney/magista/dsl/QueryContext.java rename to src/main/java/dev/vality/magista/dsl/QueryContext.java index 41818e7..d02fbd9 100644 --- a/src/main/java/com/rbkmoney/magista/dsl/QueryContext.java +++ b/src/main/java/dev/vality/magista/dsl/QueryContext.java @@ -1,4 +1,4 @@ -package com.rbkmoney.magista.dsl; +package dev.vality.magista.dsl; public interface QueryContext { } diff --git a/src/main/java/com/rbkmoney/magista/dsl/QueryContextFactory.java b/src/main/java/dev/vality/magista/dsl/QueryContextFactory.java similarity index 68% rename from src/main/java/com/rbkmoney/magista/dsl/QueryContextFactory.java rename to src/main/java/dev/vality/magista/dsl/QueryContextFactory.java index 5e355d4..86eb2bb 100644 --- a/src/main/java/com/rbkmoney/magista/dsl/QueryContextFactory.java +++ b/src/main/java/dev/vality/magista/dsl/QueryContextFactory.java @@ -1,4 +1,4 @@ -package com.rbkmoney.magista.dsl; +package dev.vality.magista.dsl; public interface QueryContextFactory { QueryContext getContext(); diff --git a/src/main/java/com/rbkmoney/magista/dsl/QueryExecutionException.java b/src/main/java/dev/vality/magista/dsl/QueryExecutionException.java similarity index 80% rename from src/main/java/com/rbkmoney/magista/dsl/QueryExecutionException.java rename to src/main/java/dev/vality/magista/dsl/QueryExecutionException.java index 5b630e3..344c3c3 100644 --- a/src/main/java/com/rbkmoney/magista/dsl/QueryExecutionException.java +++ b/src/main/java/dev/vality/magista/dsl/QueryExecutionException.java @@ -1,4 +1,4 @@ -package com.rbkmoney.magista.dsl; +package dev.vality.magista.dsl; public class QueryExecutionException extends QueryProcessingException { public QueryExecutionException() { @@ -16,7 +16,8 @@ public class QueryExecutionException extends QueryProcessingException { super(cause); } - public QueryExecutionException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + public QueryExecutionException(String message, Throwable cause, boolean enableSuppression, + boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } } diff --git a/src/main/java/com/rbkmoney/magista/dsl/QueryParameters.java b/src/main/java/dev/vality/magista/dsl/QueryParameters.java similarity index 85% rename from src/main/java/com/rbkmoney/magista/dsl/QueryParameters.java rename to src/main/java/dev/vality/magista/dsl/QueryParameters.java index ee48744..8d5e53b 100644 --- a/src/main/java/com/rbkmoney/magista/dsl/QueryParameters.java +++ b/src/main/java/dev/vality/magista/dsl/QueryParameters.java @@ -1,4 +1,4 @@ -package com.rbkmoney.magista.dsl; +package dev.vality.magista.dsl; import com.rbkmoney.geck.common.util.TypeUtil; @@ -9,10 +9,6 @@ import java.util.Map; import java.util.Objects; public class QueryParameters { - public interface QueryParametersRef { - T newInstance(Map parameters, QueryParameters derivedParameters); - } - private final Map parameters; private final QueryParameters derivedParameters; @@ -26,7 +22,8 @@ public class QueryParameters { } public Object getParameter(String key, boolean deepSearch) { - return parameters.getOrDefault(key, deepSearch && derivedParameters != null ? derivedParameters.getParameter(key, deepSearch) : null); + return parameters.getOrDefault(key, + deepSearch && derivedParameters != null ? derivedParameters.getParameter(key, deepSearch) : null); } public T removeParameters(QueryParametersRef parametersRef, String... keys) { @@ -99,11 +96,15 @@ public class QueryParameters { @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } QueryParameters that = (QueryParameters) o; - return Objects.equals(parameters, that.parameters) && - Objects.equals(derivedParameters, that.derivedParameters); + return Objects.equals(parameters, that.parameters) + && Objects.equals(derivedParameters, that.derivedParameters); } @Override @@ -113,9 +114,11 @@ public class QueryParameters { @Override public String toString() { - return "QueryParameters{" + - "parameters=" + parameters + - ", derivedParameters=" + (derivedParameters == null ? "null" : "notnull") + - '}'; + return "QueryParameters{" + "parameters=" + parameters + ", derivedParameters=" + + (derivedParameters == null ? "null" : "notnull") + '}'; + } + + public interface QueryParametersRef { + T newInstance(Map parameters, QueryParameters derivedParameters); } } diff --git a/src/main/java/com/rbkmoney/magista/dsl/QueryProcessingException.java b/src/main/java/dev/vality/magista/dsl/QueryProcessingException.java similarity index 80% rename from src/main/java/com/rbkmoney/magista/dsl/QueryProcessingException.java rename to src/main/java/dev/vality/magista/dsl/QueryProcessingException.java index 5cca402..7ee1ee5 100644 --- a/src/main/java/com/rbkmoney/magista/dsl/QueryProcessingException.java +++ b/src/main/java/dev/vality/magista/dsl/QueryProcessingException.java @@ -1,4 +1,4 @@ -package com.rbkmoney.magista.dsl; +package dev.vality.magista.dsl; public class QueryProcessingException extends RuntimeException { public QueryProcessingException() { @@ -16,7 +16,8 @@ public class QueryProcessingException extends RuntimeException { super(cause); } - public QueryProcessingException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + public QueryProcessingException(String message, Throwable cause, boolean enableSuppression, + boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } } diff --git a/src/main/java/com/rbkmoney/magista/dsl/QueryProcessor.java b/src/main/java/dev/vality/magista/dsl/QueryProcessor.java similarity index 78% rename from src/main/java/com/rbkmoney/magista/dsl/QueryProcessor.java rename to src/main/java/dev/vality/magista/dsl/QueryProcessor.java index f466807..a422c62 100644 --- a/src/main/java/com/rbkmoney/magista/dsl/QueryProcessor.java +++ b/src/main/java/dev/vality/magista/dsl/QueryProcessor.java @@ -1,4 +1,4 @@ -package com.rbkmoney.magista.dsl; +package dev.vality.magista.dsl; public interface QueryProcessor { R processQuery(S source) throws BadTokenException, QueryProcessingException; diff --git a/src/main/java/com/rbkmoney/magista/dsl/QueryResult.java b/src/main/java/dev/vality/magista/dsl/QueryResult.java similarity index 80% rename from src/main/java/com/rbkmoney/magista/dsl/QueryResult.java rename to src/main/java/dev/vality/magista/dsl/QueryResult.java index b00cc85..8c24f28 100644 --- a/src/main/java/com/rbkmoney/magista/dsl/QueryResult.java +++ b/src/main/java/dev/vality/magista/dsl/QueryResult.java @@ -1,4 +1,4 @@ -package com.rbkmoney.magista.dsl; +package dev.vality.magista.dsl; import java.util.stream.Stream; diff --git a/src/main/java/com/rbkmoney/magista/dsl/QueryValidator.java b/src/main/java/dev/vality/magista/dsl/QueryValidator.java similarity index 88% rename from src/main/java/com/rbkmoney/magista/dsl/QueryValidator.java rename to src/main/java/dev/vality/magista/dsl/QueryValidator.java index 9497b83..375355f 100644 --- a/src/main/java/com/rbkmoney/magista/dsl/QueryValidator.java +++ b/src/main/java/dev/vality/magista/dsl/QueryValidator.java @@ -1,4 +1,4 @@ -package com.rbkmoney.magista.dsl; +package dev.vality.magista.dsl; public interface QueryValidator { void validateParameters(QueryParameters parameters) throws IllegalArgumentException; diff --git a/src/main/java/com/rbkmoney/magista/dsl/RootQuery.java b/src/main/java/dev/vality/magista/dsl/RootQuery.java similarity index 67% rename from src/main/java/com/rbkmoney/magista/dsl/RootQuery.java rename to src/main/java/dev/vality/magista/dsl/RootQuery.java index 96da474..65f7d3c 100644 --- a/src/main/java/com/rbkmoney/magista/dsl/RootQuery.java +++ b/src/main/java/dev/vality/magista/dsl/RootQuery.java @@ -1,11 +1,11 @@ -package com.rbkmoney.magista.dsl; +package dev.vality.magista.dsl; -import com.rbkmoney.magista.dsl.builder.AbstractQueryBuilder; -import com.rbkmoney.magista.dsl.builder.QueryBuilder; -import com.rbkmoney.magista.dsl.builder.QueryBuilderException; -import com.rbkmoney.magista.dsl.parser.AbstractQueryParser; -import com.rbkmoney.magista.dsl.parser.QueryParserException; -import com.rbkmoney.magista.dsl.parser.QueryPart; +import dev.vality.magista.dsl.builder.AbstractQueryBuilder; +import dev.vality.magista.dsl.builder.QueryBuilder; +import dev.vality.magista.dsl.builder.QueryBuilderException; +import dev.vality.magista.dsl.parser.AbstractQueryParser; +import dev.vality.magista.dsl.parser.QueryParserException; +import dev.vality.magista.dsl.parser.QueryPart; import java.util.Collection; import java.util.List; @@ -14,8 +14,6 @@ import java.util.Optional; import java.util.stream.Collectors; import java.util.stream.Stream; -import static com.rbkmoney.magista.dsl.Parameters.QUERY_PARAMETER; - public class RootQuery extends BaseQuery { private final Query childQuery; @@ -39,12 +37,12 @@ public class RootQuery extends BaseQuery { } public Map getQuery() { - Object obj = getParameter(QUERY_PARAMETER, false); + Object obj = getParameter(Parameters.QUERY_PARAMETER, false); return obj instanceof Map ? (Map) obj : null; } public QueryParameters getRestParameters() { - return removeParameters(QueryParameters::new, QUERY_PARAMETER); + return removeParameters(QueryParameters::new, Parameters.QUERY_PARAMETER); } } @@ -56,7 +54,7 @@ public class RootQuery extends BaseQuery { super.validateParameters(parameters); RootParameters rootParameters = super.checkParamsType(parameters, RootParameters.class); if (rootParameters.getQuery() == null) { - checkParamsResult(true, QUERY_PARAMETER, DEFAULT_ERR_MSG_STRING); + checkParamsResult(true, Parameters.QUERY_PARAMETER, DEFAULT_ERR_MSG_STRING); } } @@ -65,14 +63,14 @@ public class RootQuery extends BaseQuery { if (query instanceof RootQuery) { Query childQuery = ((RootQuery) query).childQuery; if (childQuery instanceof CompositeQuery) { - Optional childQueries = Optional.ofNullable(((CompositeQuery) childQuery).getChildQueries()); - checkParamsResult( - !childQueries.isPresent() || childQueries.get().isEmpty(), - "Request must contain at least one query" - ); + Optional childQueries = + Optional.ofNullable(((CompositeQuery) childQuery).getChildQueries()); + checkParamsResult(!childQueries.isPresent() || childQueries.get().isEmpty(), + "Request must contain at least one query"); } } else if (query instanceof CompositeQuery) { - checkParamsResult(true, "Request can't hold more than one query, received count: " + ((CompositeQuery) query).getChildQueries().size()); + checkParamsResult(true, "Request can't hold more than one query, received count: " + + ((CompositeQuery) query).getChildQueries().size()); } else { assert false : "No other types expected here"; } @@ -80,15 +78,20 @@ public class RootQuery extends BaseQuery { } public static class RootParser extends AbstractQueryParser { - private RootValidator validator = new RootValidator(); + private final RootValidator validator = new RootValidator(); + + public static String getMainDescriptor() { + return Parameters.QUERY_PARAMETER; + } @Override public List parseQuery(Map source, QueryPart parent) throws QueryParserException { RootParameters parameters = getValidatedParameters(source, parent, RootParameters::new, validator); return Stream.of( - new QueryPart(getMainDescriptor(), new QueryParameters(parameters.getQuery(), parameters), parent), - new QueryPart(QueryPart.DEFAULT_DESCRIPTOR, parameters.getRestParameters(), parent)) + new QueryPart(getMainDescriptor(), + new QueryParameters(parameters.getQuery(), parameters), parent), + new QueryPart(QueryPart.DEFAULT_DESCRIPTOR, parameters.getRestParameters(), parent)) .collect(Collectors.toList()); } @@ -96,24 +99,23 @@ public class RootQuery extends BaseQuery { public boolean apply(Map source, QueryPart parent) { return parent == null; } - - public static String getMainDescriptor() { - return QUERY_PARAMETER; - } } public static class RootBuilder extends AbstractQueryBuilder { - private RootValidator validator = new RootValidator(); + private final RootValidator validator = new RootValidator(); @Override - public Query buildQuery(List queryParts, String continuationToken, QueryPart parentQueryPart, QueryBuilder baseBuilder) throws QueryBuilderException { - Query resultQuery = buildSingleQuery(RootParser.getMainDescriptor(), queryParts, queryPart -> createQuery(queryPart, continuationToken, baseBuilder)); + public Query buildQuery(List queryParts, String continuationToken, QueryPart parentQueryPart, + QueryBuilder baseBuilder) throws QueryBuilderException { + Query resultQuery = buildSingleQuery(RootParser.getMainDescriptor(), queryParts, + queryPart -> createQuery(queryPart, continuationToken, baseBuilder)); validator.validateQuery(resultQuery); return resultQuery; } private RootQuery createQuery(QueryPart queryPart, String continuationToken, QueryBuilder baseBuilder) { - Query childQuery = baseBuilder.buildQuery(queryPart.getChildren(), continuationToken, queryPart, baseBuilder); + Query childQuery = + baseBuilder.buildQuery(queryPart.getChildren(), continuationToken, queryPart, baseBuilder); RootQuery rootQuery = new RootQuery(queryPart.getDescriptor(), queryPart.getParameters(), childQuery); childQuery.setParentQuery(rootQuery); return rootQuery; diff --git a/src/main/java/com/rbkmoney/magista/dsl/TokenUtil.java b/src/main/java/dev/vality/magista/dsl/TokenUtil.java similarity index 95% rename from src/main/java/com/rbkmoney/magista/dsl/TokenUtil.java rename to src/main/java/dev/vality/magista/dsl/TokenUtil.java index 30b417d..d8cff6e 100644 --- a/src/main/java/com/rbkmoney/magista/dsl/TokenUtil.java +++ b/src/main/java/dev/vality/magista/dsl/TokenUtil.java @@ -1,4 +1,4 @@ -package com.rbkmoney.magista.dsl; +package dev.vality.magista.dsl; import java.util.Optional; import java.util.UUID; diff --git a/src/main/java/dev/vality/magista/dsl/builder/AbstractQueryBuilder.java b/src/main/java/dev/vality/magista/dsl/builder/AbstractQueryBuilder.java new file mode 100644 index 0000000..e690758 --- /dev/null +++ b/src/main/java/dev/vality/magista/dsl/builder/AbstractQueryBuilder.java @@ -0,0 +1,82 @@ +package dev.vality.magista.dsl.builder; + +import dev.vality.magista.dsl.*; +import dev.vality.magista.dsl.parser.QueryPart; + +import java.util.Collections; +import java.util.List; +import java.util.function.BiFunction; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public abstract class AbstractQueryBuilder implements QueryBuilder { + + protected Stream getMatchedPartsStream(Object descriptor, List queryParts) { + return queryParts.stream().filter(queryPart -> descriptor.equals(queryPart.getDescriptor())); + } + + + protected CompositeQuery createCompositeQuery(Object descriptor, QueryParameters derivedParameters, + List childQueries, + Function> execFunction, + BiFunction, + QueryResult> parallelExecFunction) { + return BaseCompositeQuery.newInstance(descriptor, + new QueryParameters(Collections.emptyMap(), derivedParameters), childQueries, execFunction, + parallelExecFunction); + } + + protected CompositeQuery createCompositeQuery(Object descriptor, QueryParameters derivedParameters, + List childQueries) { + return createCompositeQuery(descriptor, derivedParameters, childQueries, + context -> new BaseQueryResult<>(() -> childQueries.stream().map(query -> query.execute(context)), + () -> childQueries.stream().map(query -> query.execute(context)).collect(Collectors.toList())), + (context, queryResults) -> new BaseQueryResult<>(() -> queryResults.stream(), () -> queryResults) + + ); + } + + protected CompositeQuery createCompositeQuery(Object descriptor, List childQueries, + Function> execFunction, + BiFunction, + QueryResult> parallelExecFunction) { + return createCompositeQuery(descriptor, new QueryParameters(Collections.emptyMap(), null), + childQueries, execFunction, parallelExecFunction); + } + + protected QueryParameters getParameters(QueryPart queryPart) { + return queryPart == null ? null : queryPart.getParameters(); + } + + protected List buildQueries(Object matchDescriptor, List queryParts, + Function queryCreator) { + List matchedParts = getMatchedPartsStream(matchDescriptor, queryParts).collect(Collectors.toList()); + return matchedParts.stream().map(queryPart -> queryCreator.apply(queryPart)).collect(Collectors.toList()); + } + + protected Query buildSingleQuery(Object matchDescriptor, List queryParts, + Function queryCreator) { + List queries = buildQueries(matchDescriptor, queryParts, queryCreator); + if (queries.size() == 0) { + throw new QueryBuilderException("No queries found in referred data"); + } else if (queries.size() > 1) { + throw new QueryBuilderException("Only one query expected"); + } else { + return queries.get(0); + } + } + + protected Query buildAndWrapQueries(Object matchDescriptor, List queryParts, + Function queryCreator, + QueryParameters parentParameters, + Function> execFunction, + BiFunction, + QueryResult> parallelExecFunction) { + List queries = buildQueries(matchDescriptor, queryParts, queryCreator); + return createCompositeQuery(queries.get(0).getDescriptor(), parentParameters, queries, execFunction, + parallelExecFunction); + } +} diff --git a/src/main/java/com/rbkmoney/magista/dsl/builder/BaseQueryBuilder.java b/src/main/java/dev/vality/magista/dsl/builder/BaseQueryBuilder.java similarity index 65% rename from src/main/java/com/rbkmoney/magista/dsl/builder/BaseQueryBuilder.java rename to src/main/java/dev/vality/magista/dsl/builder/BaseQueryBuilder.java index 74cc44e..b531163 100644 --- a/src/main/java/com/rbkmoney/magista/dsl/builder/BaseQueryBuilder.java +++ b/src/main/java/dev/vality/magista/dsl/builder/BaseQueryBuilder.java @@ -1,8 +1,8 @@ -package com.rbkmoney.magista.dsl.builder; +package dev.vality.magista.dsl.builder; -import com.rbkmoney.magista.dsl.BadTokenException; -import com.rbkmoney.magista.dsl.Query; -import com.rbkmoney.magista.dsl.parser.QueryPart; +import dev.vality.magista.dsl.BadTokenException; +import dev.vality.magista.dsl.Query; +import dev.vality.magista.dsl.parser.QueryPart; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -22,15 +22,13 @@ public abstract class BaseQueryBuilder implements QueryBuilder { } @Override - public Query buildQuery(List queryParts, String continuationToken, QueryPart parentQueryPart, QueryBuilder baseBuilder) throws QueryBuilderException { + public Query buildQuery(List queryParts, String continuationToken, QueryPart parentQueryPart, + QueryBuilder baseBuilder) throws QueryBuilderException { try { - List queries = queryParts.isEmpty() ? Collections.emptyList() : builders.stream() - .filter( - builder -> builder.apply(queryParts, parentQueryPart)) - .map( - builder -> builder.buildQuery(queryParts, continuationToken, parentQueryPart, baseBuilder == null ? this : baseBuilder) - ) - .collect(Collectors.toList()); + List queries = queryParts.isEmpty() ? Collections.emptyList() : + builders.stream().filter(builder -> builder.apply(queryParts, parentQueryPart)) + .map(builder -> builder.buildQuery(queryParts, continuationToken, parentQueryPart, + baseBuilder == null ? this : baseBuilder)).collect(Collectors.toList()); if (queries.size() > 1) { throw new QueryBuilderException("Build result has more than one query"); } else if (queries.size() == 1) { diff --git a/src/main/java/dev/vality/magista/dsl/builder/QueryBuilder.java b/src/main/java/dev/vality/magista/dsl/builder/QueryBuilder.java new file mode 100644 index 0000000..0fcacee --- /dev/null +++ b/src/main/java/dev/vality/magista/dsl/builder/QueryBuilder.java @@ -0,0 +1,14 @@ +package dev.vality.magista.dsl.builder; + +import dev.vality.magista.dsl.Query; +import dev.vality.magista.dsl.parser.QueryPart; + +import java.util.List; + +public interface QueryBuilder { + + Query buildQuery(List queryParts, String continuationToken, QueryPart parentQueryPart, + QueryBuilder baseBuilder) throws QueryBuilderException; + + boolean apply(List queryParts, QueryPart parent); +} diff --git a/src/main/java/com/rbkmoney/magista/dsl/builder/QueryBuilderException.java b/src/main/java/dev/vality/magista/dsl/builder/QueryBuilderException.java similarity index 73% rename from src/main/java/com/rbkmoney/magista/dsl/builder/QueryBuilderException.java rename to src/main/java/dev/vality/magista/dsl/builder/QueryBuilderException.java index 1cb921a..6fc3cd0 100644 --- a/src/main/java/com/rbkmoney/magista/dsl/builder/QueryBuilderException.java +++ b/src/main/java/dev/vality/magista/dsl/builder/QueryBuilderException.java @@ -1,6 +1,6 @@ -package com.rbkmoney.magista.dsl.builder; +package dev.vality.magista.dsl.builder; -import com.rbkmoney.magista.dsl.QueryProcessingException; +import dev.vality.magista.dsl.QueryProcessingException; public class QueryBuilderException extends QueryProcessingException { public QueryBuilderException() { @@ -18,7 +18,8 @@ public class QueryBuilderException extends QueryProcessingException { super(cause); } - public QueryBuilderException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + public QueryBuilderException(String message, Throwable cause, boolean enableSuppression, + boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } } diff --git a/src/main/java/com/rbkmoney/magista/dsl/parser/AbstractQueryParser.java b/src/main/java/dev/vality/magista/dsl/parser/AbstractQueryParser.java similarity index 59% rename from src/main/java/com/rbkmoney/magista/dsl/parser/AbstractQueryParser.java rename to src/main/java/dev/vality/magista/dsl/parser/AbstractQueryParser.java index 70199be..380c75b 100644 --- a/src/main/java/com/rbkmoney/magista/dsl/parser/AbstractQueryParser.java +++ b/src/main/java/dev/vality/magista/dsl/parser/AbstractQueryParser.java @@ -1,7 +1,7 @@ -package com.rbkmoney.magista.dsl.parser; +package dev.vality.magista.dsl.parser; -import com.rbkmoney.magista.dsl.QueryParameters; -import com.rbkmoney.magista.dsl.QueryValidator; +import dev.vality.magista.dsl.QueryParameters; +import dev.vality.magista.dsl.QueryValidator; import java.util.Map; @@ -11,7 +11,10 @@ public abstract class AbstractQueryParser implements QueryParser T getValidatedParameters(Map source, QueryPart parent, QueryParameters.QueryParametersRef parametersRef, QueryValidator validator) throws QueryParserException { + protected T getValidatedParameters(Map source, QueryPart parent, + QueryParameters.QueryParametersRef parametersRef, + QueryValidator validator) + throws QueryParserException { try { T parameters = parametersRef.newInstance(source, getParameters(parent)); validator.validateParameters(parameters); diff --git a/src/main/java/com/rbkmoney/magista/dsl/parser/BaseQueryParser.java b/src/main/java/dev/vality/magista/dsl/parser/BaseQueryParser.java similarity index 97% rename from src/main/java/com/rbkmoney/magista/dsl/parser/BaseQueryParser.java rename to src/main/java/dev/vality/magista/dsl/parser/BaseQueryParser.java index af6cf10..b2141fd 100644 --- a/src/main/java/com/rbkmoney/magista/dsl/parser/BaseQueryParser.java +++ b/src/main/java/dev/vality/magista/dsl/parser/BaseQueryParser.java @@ -1,4 +1,4 @@ -package com.rbkmoney.magista.dsl.parser; +package dev.vality.magista.dsl.parser; import java.util.ArrayList; import java.util.Collections; diff --git a/src/main/java/com/rbkmoney/magista/dsl/parser/JsonQueryParser.java b/src/main/java/dev/vality/magista/dsl/parser/JsonQueryParser.java similarity index 90% rename from src/main/java/com/rbkmoney/magista/dsl/parser/JsonQueryParser.java rename to src/main/java/dev/vality/magista/dsl/parser/JsonQueryParser.java index a06a473..dfaedc9 100644 --- a/src/main/java/com/rbkmoney/magista/dsl/parser/JsonQueryParser.java +++ b/src/main/java/dev/vality/magista/dsl/parser/JsonQueryParser.java @@ -1,4 +1,4 @@ -package com.rbkmoney.magista.dsl.parser; +package dev.vality.magista.dsl.parser; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; @@ -15,7 +15,7 @@ public class JsonQueryParser implements QueryParser { private QueryParser> queryPartParser; - public JsonQueryParser withQueryParser(QueryParser> queryPartParser){ + public JsonQueryParser withQueryParser(QueryParser> queryPartParser) { this.queryPartParser = queryPartParser; return this; } @@ -32,7 +32,7 @@ public class JsonQueryParser implements QueryParser { return queryPartParser.parseQuery(jsonMap, parent); } catch (IOException e) { - throw new QueryParserException("Failed to parse received json request: "+e.getMessage(), e); + throw new QueryParserException("Failed to parse received json request: " + e.getMessage(), e); } } @@ -47,6 +47,7 @@ public class JsonQueryParser implements QueryParser { } protected Map parseJsonMap(String data) throws IOException { - return getMapper().readerFor(new TypeReference>() {}).readValue(data); + return getMapper().readerFor(new TypeReference>() { + }).readValue(data); } } diff --git a/src/main/java/dev/vality/magista/dsl/parser/QueryParser.java b/src/main/java/dev/vality/magista/dsl/parser/QueryParser.java new file mode 100644 index 0000000..d962998 --- /dev/null +++ b/src/main/java/dev/vality/magista/dsl/parser/QueryParser.java @@ -0,0 +1,10 @@ +package dev.vality.magista.dsl.parser; + + +import java.util.List; + +public interface QueryParser { + List parseQuery(T source, QueryPart parent) throws QueryParserException; + + boolean apply(T source, QueryPart parent); +} diff --git a/src/main/java/com/rbkmoney/magista/dsl/parser/QueryParserException.java b/src/main/java/dev/vality/magista/dsl/parser/QueryParserException.java similarity index 73% rename from src/main/java/com/rbkmoney/magista/dsl/parser/QueryParserException.java rename to src/main/java/dev/vality/magista/dsl/parser/QueryParserException.java index add5c59..06b30ff 100644 --- a/src/main/java/com/rbkmoney/magista/dsl/parser/QueryParserException.java +++ b/src/main/java/dev/vality/magista/dsl/parser/QueryParserException.java @@ -1,6 +1,6 @@ -package com.rbkmoney.magista.dsl.parser; +package dev.vality.magista.dsl.parser; -import com.rbkmoney.magista.dsl.QueryExecutionException; +import dev.vality.magista.dsl.QueryExecutionException; public class QueryParserException extends QueryExecutionException { public QueryParserException() { @@ -18,7 +18,8 @@ public class QueryParserException extends QueryExecutionException { super(cause); } - public QueryParserException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + public QueryParserException(String message, Throwable cause, boolean enableSuppression, + boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } } diff --git a/src/main/java/com/rbkmoney/magista/dsl/parser/QueryPart.java b/src/main/java/dev/vality/magista/dsl/parser/QueryPart.java similarity index 79% rename from src/main/java/com/rbkmoney/magista/dsl/parser/QueryPart.java rename to src/main/java/dev/vality/magista/dsl/parser/QueryPart.java index 2c19e30..d077612 100644 --- a/src/main/java/com/rbkmoney/magista/dsl/parser/QueryPart.java +++ b/src/main/java/dev/vality/magista/dsl/parser/QueryPart.java @@ -1,6 +1,6 @@ -package com.rbkmoney.magista.dsl.parser; +package dev.vality.magista.dsl.parser; -import com.rbkmoney.magista.dsl.QueryParameters; +import dev.vality.magista.dsl.QueryParameters; import java.util.List; @@ -8,8 +8,8 @@ public class QueryPart { public static final Object DEFAULT_DESCRIPTOR = "default_descriptor"; private final Object descriptor; private final QueryParameters parameters; - private List children; private final QueryPart parent; + private List children; public QueryPart(Object descriptor, QueryParameters parameters) { this(descriptor, parameters, null); @@ -45,16 +45,13 @@ public class QueryPart { } public boolean isEmpty() { - return descriptor.equals(DEFAULT_DESCRIPTOR) && parameters.getParametersMap().isEmpty() && (children == null || children.isEmpty()); + return descriptor.equals(DEFAULT_DESCRIPTOR) && parameters.getParametersMap().isEmpty() + && (children == null || children.isEmpty()); } @Override public String toString() { - return "QueryPart{" + - "descriptor=" + descriptor + - ", parameters=" + parameters + - ", children=" + children + - ", parent=" + (parent == null ? "null" : "notnull") + - '}'; + return "QueryPart{" + "descriptor=" + descriptor + ", parameters=" + parameters + ", children=" + children + + ", parent=" + (parent == null ? "null" : "notnull") + '}'; } } diff --git a/src/test/java/com/rbkmoney/magista/dsl/SearchDao.java b/src/test/java/com/rbkmoney/magista/dsl/SearchDao.java deleted file mode 100644 index 8f5c50f..0000000 --- a/src/test/java/com/rbkmoney/magista/dsl/SearchDao.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.rbkmoney.magista.dsl; - -import java.util.*; -import java.util.stream.Collectors; - -public class SearchDao { - - public static final List allDummies = Arrays.asList( - new Dummy(1L, "kek", "2016-03-22T06:12:27Z", "2016-03-22T06:12:27Z"), - new Dummy(2L, "kek", "2016-03-22T06:12:27Z", "2016-03-22T06:12:27Z"), - new Dummy(3L, "kek", "2016-03-22T06:12:27Z", "2016-03-22T06:12:27Z"), - new Dummy(4L, "kek", "2016-03-22T06:12:27Z", "2016-03-22T06:12:27Z") - ); - public Collection> getDummies(String id) { - return allDummies.stream().filter(d -> id.equals(d.getId().toString())).map(d -> new AbstractMap.SimpleEntry<>(d.getId(), d)).collect(Collectors.toList()); - } -} diff --git a/src/test/java/com/rbkmoney/magista/dsl/DummiesFunction.java b/src/test/java/dev/vality/magista/dsl/DummiesFunction.java similarity index 61% rename from src/test/java/com/rbkmoney/magista/dsl/DummiesFunction.java rename to src/test/java/dev/vality/magista/dsl/DummiesFunction.java index 17396e0..9ef0fc0 100644 --- a/src/test/java/com/rbkmoney/magista/dsl/DummiesFunction.java +++ b/src/test/java/dev/vality/magista/dsl/DummiesFunction.java @@ -1,11 +1,11 @@ -package com.rbkmoney.magista.dsl; +package dev.vality.magista.dsl; -import com.rbkmoney.magista.dsl.builder.AbstractQueryBuilder; -import com.rbkmoney.magista.dsl.builder.QueryBuilder; -import com.rbkmoney.magista.dsl.builder.QueryBuilderException; -import com.rbkmoney.magista.dsl.parser.AbstractQueryParser; -import com.rbkmoney.magista.dsl.parser.QueryParserException; -import com.rbkmoney.magista.dsl.parser.QueryPart; +import dev.vality.magista.dsl.builder.AbstractQueryBuilder; +import dev.vality.magista.dsl.builder.QueryBuilder; +import dev.vality.magista.dsl.builder.QueryBuilderException; +import dev.vality.magista.dsl.parser.AbstractQueryParser; +import dev.vality.magista.dsl.parser.QueryParserException; +import dev.vality.magista.dsl.parser.QueryPart; import java.time.temporal.TemporalAccessor; import java.util.Arrays; @@ -15,46 +15,53 @@ import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream; -public class DummiesFunction extends PagedBaseFunction, StatTestResponse> implements CompositeQuery, StatTestResponse> { +public class DummiesFunction extends PagedBaseFunction, StatTestResponse> + implements CompositeQuery, StatTestResponse> { public static final String FUNC_NAME = "dummies"; private final CompositeQuery> subquery; - private DummiesFunction(Object descriptor, QueryParameters params, String continuationToken, CompositeQuery> subquery) { + private DummiesFunction(Object descriptor, QueryParameters params, String continuationToken, + CompositeQuery> subquery) { super(descriptor, params, FUNC_NAME, continuationToken); this.subquery = subquery; } + private static DummiesFunction createDummiesFunction(Object descriptor, QueryParameters queryParameters, + String continuationToken, + CompositeQuery> subquery) { + DummiesFunction dummiesFunction = new DummiesFunction(descriptor, queryParameters, continuationToken, subquery); + subquery.setParentQuery(dummiesFunction); + return dummiesFunction; + } + @Override - public QueryResult, StatTestResponse> execute(QueryContext context) throws QueryExecutionException { + public QueryResult, StatTestResponse> execute(QueryContext context) + throws QueryExecutionException { QueryResult> collectedResults = subquery.execute(context); return execute(context, collectedResults.getCollectedStream()); } @Override - public QueryResult, StatTestResponse> execute(QueryContext context, List collectedResults) throws QueryExecutionException { - QueryResult, List>> dummiesResult = (QueryResult, List>>) collectedResults.get(0); + public QueryResult, StatTestResponse> execute(QueryContext context, + List collectedResults) + throws QueryExecutionException { + QueryResult, List>> dummiesResult = + (QueryResult, List>>) collectedResults.get(0); - return new BaseQueryResult<>( - () -> dummiesResult.getDataStream(), - () -> { - List statResponseData = dummiesResult.getDataStream() - .map(dummyEntry -> dummyEntry.getValue()).collect(Collectors.toList()); - StatTestResponse statResponse = new StatTestResponse(statResponseData); - List> dummyStats = dummiesResult.getCollectedStream(); - if (!dummiesResult.getCollectedStream().isEmpty() && getQueryParameters().getSize() == dummyStats.size()) { - statResponse.setContinuationToken( - TokenUtil.buildToken( - getQueryParameters(), - dummyStats.get(dummyStats.size() - 1).getKey() - ) - ); - } - return statResponse; - } - ); + return new BaseQueryResult<>(() -> dummiesResult.getDataStream(), () -> { + List statResponseData = + dummiesResult.getDataStream().map(dummyEntry -> dummyEntry.getValue()).collect(Collectors.toList()); + StatTestResponse statResponse = new StatTestResponse(statResponseData); + List> dummyStats = dummiesResult.getCollectedStream(); + if (!dummiesResult.getCollectedStream().isEmpty() && getQueryParameters().getSize() == dummyStats.size()) { + statResponse.setContinuationToken( + TokenUtil.buildToken(getQueryParameters(), dummyStats.get(dummyStats.size() - 1).getKey())); + } + return statResponse; + }); } @Override @@ -122,51 +129,47 @@ public class DummiesFunction extends PagedBaseFunction, S } public static class DummiesParser extends AbstractQueryParser { - private DummiesValidator validator = new DummiesValidator(); + private final DummiesValidator validator = new DummiesValidator(); + + public static String getMainDescriptor() { + return FUNC_NAME; + } @Override public List parseQuery(Map source, QueryPart parent) throws QueryParserException { Map funcSource = (Map) source.get(FUNC_NAME); TestParameters parameters = getValidatedParameters(funcSource, parent, TestParameters::new, validator); - return Stream.of( - new QueryPart(FUNC_NAME, parameters, parent) - ) - .collect(Collectors.toList()); + return Stream.of(new QueryPart(FUNC_NAME, parameters, parent)).collect(Collectors.toList()); } @Override public boolean apply(Map source, QueryPart parent) { - return parent != null - && RootQuery.RootParser.getMainDescriptor().equals(parent.getDescriptor()) + return parent != null && RootQuery.RootParser.getMainDescriptor().equals(parent.getDescriptor()) && (source.get(FUNC_NAME) instanceof Map); } - - public static String getMainDescriptor() { - return FUNC_NAME; - } } public static class DummiesBuilder extends AbstractQueryBuilder { - private DummiesValidator validator = new DummiesValidator(); + private final DummiesValidator validator = new DummiesValidator(); @Override - public Query buildQuery(List queryParts, String continuationToken, QueryPart parentQueryPart, QueryBuilder baseBuilder) throws QueryBuilderException { - Query resultQuery = buildSingleQuery(DummiesParser.getMainDescriptor(), queryParts, queryPart -> createQuery(queryPart, continuationToken)); + public Query buildQuery(List queryParts, String continuationToken, QueryPart parentQueryPart, + QueryBuilder baseBuilder) throws QueryBuilderException { + Query resultQuery = buildSingleQuery(DummiesParser.getMainDescriptor(), queryParts, + queryPart -> createQuery(queryPart, continuationToken)); validator.validateQuery(resultQuery); return resultQuery; } private CompositeQuery createQuery(QueryPart queryPart, String continuationToken) { List queries = Arrays.asList( - new GetDataFunction(queryPart.getDescriptor() + ":" + GetDataFunction.FUNC_NAME, queryPart.getParameters(), continuationToken) - ); - CompositeQuery> compositeQuery = createCompositeQuery( - queryPart.getDescriptor(), - getParameters(queryPart.getParent()), - queries - ); - return createDummiesFunction(queryPart.getDescriptor(), queryPart.getParameters(), continuationToken, compositeQuery); + new GetDataFunction(queryPart.getDescriptor() + ":" + GetDataFunction.FUNC_NAME, + queryPart.getParameters(), continuationToken)); + CompositeQuery> compositeQuery = + createCompositeQuery(queryPart.getDescriptor(), getParameters(queryPart.getParent()), queries); + return createDummiesFunction(queryPart.getDescriptor(), queryPart.getParameters(), continuationToken, + compositeQuery); } @Override @@ -175,13 +178,8 @@ public class DummiesFunction extends PagedBaseFunction, S } } - private static DummiesFunction createDummiesFunction(Object descriptor, QueryParameters queryParameters, String continuationToken, CompositeQuery> subquery) { - DummiesFunction dummiesFunction = new DummiesFunction(descriptor, queryParameters, continuationToken, subquery); - subquery.setParentQuery(dummiesFunction); - return dummiesFunction; - } - - private static class GetDataFunction extends PagedBaseFunction, Collection>> { + private static class GetDataFunction + extends PagedBaseFunction, Collection>> { private static final String FUNC_NAME = DummiesFunction.FUNC_NAME + "_data"; public GetDataFunction(Object descriptor, QueryParameters params, String continuationToken) { @@ -193,12 +191,12 @@ public class DummiesFunction extends PagedBaseFunction, S } @Override - public QueryResult, Collection>> execute(QueryContext context) throws QueryExecutionException { + public QueryResult, Collection>> execute(QueryContext context) + throws QueryExecutionException { TestQueryContext functionContext = getContext(context); - TestParameters parameters = new TestParameters(getQueryParameters(), getQueryParameters().getDerivedParameters()); - Collection> result = functionContext.getSearchDao().getDummies( - parameters.getId() - ); + TestParameters parameters = + new TestParameters(getQueryParameters(), getQueryParameters().getDerivedParameters()); + Collection> result = functionContext.getSearchDao().getDummies(parameters.getId()); return new BaseQueryResult<>(() -> result.stream(), () -> result); } diff --git a/src/test/java/com/rbkmoney/magista/dsl/Dummy.java b/src/test/java/dev/vality/magista/dsl/Dummy.java similarity index 96% rename from src/test/java/com/rbkmoney/magista/dsl/Dummy.java rename to src/test/java/dev/vality/magista/dsl/Dummy.java index 219d445..edc7b1c 100644 --- a/src/test/java/com/rbkmoney/magista/dsl/Dummy.java +++ b/src/test/java/dev/vality/magista/dsl/Dummy.java @@ -1,4 +1,4 @@ -package com.rbkmoney.magista.dsl; +package dev.vality.magista.dsl; public class Dummy { private Long id; diff --git a/src/test/java/dev/vality/magista/dsl/SearchDao.java b/src/test/java/dev/vality/magista/dsl/SearchDao.java new file mode 100644 index 0000000..9a65696 --- /dev/null +++ b/src/test/java/dev/vality/magista/dsl/SearchDao.java @@ -0,0 +1,18 @@ +package dev.vality.magista.dsl; + +import java.util.*; +import java.util.stream.Collectors; + +public class SearchDao { + + public static final List allDummies = + Arrays.asList(new Dummy(1L, "kek", "2016-03-22T06:12:27Z", "2016-03-22T06:12:27Z"), + new Dummy(2L, "kek", "2016-03-22T06:12:27Z", "2016-03-22T06:12:27Z"), + new Dummy(3L, "kek", "2016-03-22T06:12:27Z", "2016-03-22T06:12:27Z"), + new Dummy(4L, "kek", "2016-03-22T06:12:27Z", "2016-03-22T06:12:27Z")); + + public Collection> getDummies(String id) { + return allDummies.stream().filter(d -> id.equals(d.getId().toString())) + .map(d -> new AbstractMap.SimpleEntry<>(d.getId(), d)).collect(Collectors.toList()); + } +} diff --git a/src/test/java/com/rbkmoney/magista/dsl/StatTestRequest.java b/src/test/java/dev/vality/magista/dsl/StatTestRequest.java similarity index 93% rename from src/test/java/com/rbkmoney/magista/dsl/StatTestRequest.java rename to src/test/java/dev/vality/magista/dsl/StatTestRequest.java index 7875357..e93e52c 100644 --- a/src/test/java/com/rbkmoney/magista/dsl/StatTestRequest.java +++ b/src/test/java/dev/vality/magista/dsl/StatTestRequest.java @@ -1,4 +1,4 @@ -package com.rbkmoney.magista.dsl; +package dev.vality.magista.dsl; public class StatTestRequest { diff --git a/src/test/java/com/rbkmoney/magista/dsl/StatTestResponse.java b/src/test/java/dev/vality/magista/dsl/StatTestResponse.java similarity index 95% rename from src/test/java/com/rbkmoney/magista/dsl/StatTestResponse.java rename to src/test/java/dev/vality/magista/dsl/StatTestResponse.java index 96b1199..4a2092b 100644 --- a/src/test/java/com/rbkmoney/magista/dsl/StatTestResponse.java +++ b/src/test/java/dev/vality/magista/dsl/StatTestResponse.java @@ -1,4 +1,4 @@ -package com.rbkmoney.magista.dsl; +package dev.vality.magista.dsl; import java.util.List; diff --git a/src/test/java/com/rbkmoney/magista/dsl/StatTestResponseData.java b/src/test/java/dev/vality/magista/dsl/StatTestResponseData.java similarity index 91% rename from src/test/java/com/rbkmoney/magista/dsl/StatTestResponseData.java rename to src/test/java/dev/vality/magista/dsl/StatTestResponseData.java index f2f6396..133e5ee 100644 --- a/src/test/java/com/rbkmoney/magista/dsl/StatTestResponseData.java +++ b/src/test/java/dev/vality/magista/dsl/StatTestResponseData.java @@ -1,4 +1,4 @@ -package com.rbkmoney.magista.dsl; +package dev.vality.magista.dsl; import java.util.List; diff --git a/src/test/java/com/rbkmoney/magista/dsl/TestQueryContext.java b/src/test/java/dev/vality/magista/dsl/TestQueryContext.java similarity index 88% rename from src/test/java/com/rbkmoney/magista/dsl/TestQueryContext.java rename to src/test/java/dev/vality/magista/dsl/TestQueryContext.java index 389ff14..6059683 100644 --- a/src/test/java/com/rbkmoney/magista/dsl/TestQueryContext.java +++ b/src/test/java/dev/vality/magista/dsl/TestQueryContext.java @@ -1,4 +1,4 @@ -package com.rbkmoney.magista.dsl; +package dev.vality.magista.dsl; public class TestQueryContext implements QueryContext { diff --git a/src/test/java/com/rbkmoney/magista/dsl/builder/QueryBuilderImplTest.java b/src/test/java/dev/vality/magista/dsl/builder/QueryBuilderImplTest.java similarity index 50% rename from src/test/java/com/rbkmoney/magista/dsl/builder/QueryBuilderImplTest.java rename to src/test/java/dev/vality/magista/dsl/builder/QueryBuilderImplTest.java index 90d0803..52f9ad6 100644 --- a/src/test/java/com/rbkmoney/magista/dsl/builder/QueryBuilderImplTest.java +++ b/src/test/java/dev/vality/magista/dsl/builder/QueryBuilderImplTest.java @@ -1,13 +1,13 @@ -package com.rbkmoney.magista.dsl.builder; +package dev.vality.magista.dsl.builder; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.ObjectMapper; -import com.rbkmoney.magista.dsl.DummiesFunction; -import com.rbkmoney.magista.dsl.Query; -import com.rbkmoney.magista.dsl.RootQuery; -import com.rbkmoney.magista.dsl.parser.BaseQueryParser; -import com.rbkmoney.magista.dsl.parser.JsonQueryParser; -import com.rbkmoney.magista.dsl.parser.QueryPart; +import dev.vality.magista.dsl.DummiesFunction; +import dev.vality.magista.dsl.Query; +import dev.vality.magista.dsl.RootQuery; +import dev.vality.magista.dsl.parser.BaseQueryParser; +import dev.vality.magista.dsl.parser.JsonQueryParser; +import dev.vality.magista.dsl.parser.QueryPart; import org.junit.Test; import java.util.Arrays; @@ -25,23 +25,27 @@ public class QueryBuilderImplTest { mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); return mapper; } - }.withQueryParser(new BaseQueryParser(Arrays.asList(new RootQuery.RootParser(), new DummiesFunction.DummiesParser())) { - @Override - public boolean apply(Map source, QueryPart parent) { - return true; - } - }); + }.withQueryParser( + new BaseQueryParser(Arrays.asList(new RootQuery.RootParser(), new DummiesFunction.DummiesParser())) { + @Override + public boolean apply(Map source, QueryPart parent) { + return true; + } + }); - private QueryBuilder builder = new BaseQueryBuilder(Arrays.asList(new RootQuery.RootBuilder(), new DummiesFunction.DummiesBuilder())) { - @Override - public boolean apply(List queryParts, QueryPart parent) { - return true; - } - }; + private final QueryBuilder builder = + new BaseQueryBuilder(Arrays.asList(new RootQuery.RootBuilder(), new DummiesFunction.DummiesBuilder())) { + @Override + public boolean apply(List queryParts, QueryPart parent) { + return true; + } + }; @Test public void test() { - String json = "{'query': {'dummies': {'id': '1','kek':'kek','from_time': '2016-03-22T00:12:00Z','to_time': '2016-03-22T01:12:00Z'}}}"; + String json = + "{'query': {'dummies': {'id': '1','kek':'kek','from_time': '2016-03-22T00:12:00Z'," + + "'to_time': '2016-03-22T01:12:00Z'}}}"; Query query = buildQuery(json); assertTrue(query instanceof RootQuery); query.getDescriptor(); diff --git a/src/test/java/com/rbkmoney/magista/dsl/parser/JsonQueryParserTest.java b/src/test/java/dev/vality/magista/dsl/parser/JsonQueryParserTest.java similarity index 57% rename from src/test/java/com/rbkmoney/magista/dsl/parser/JsonQueryParserTest.java rename to src/test/java/dev/vality/magista/dsl/parser/JsonQueryParserTest.java index 3b3ef84..3aa3536 100644 --- a/src/test/java/com/rbkmoney/magista/dsl/parser/JsonQueryParserTest.java +++ b/src/test/java/dev/vality/magista/dsl/parser/JsonQueryParserTest.java @@ -1,9 +1,9 @@ -package com.rbkmoney.magista.dsl.parser; +package dev.vality.magista.dsl.parser; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.ObjectMapper; -import com.rbkmoney.magista.dsl.DummiesFunction; -import com.rbkmoney.magista.dsl.RootQuery; +import dev.vality.magista.dsl.DummiesFunction; +import dev.vality.magista.dsl.RootQuery; import org.junit.Test; import java.util.Arrays; @@ -22,19 +22,21 @@ public class JsonQueryParserTest { mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); return mapper; } - }.withQueryParser(new BaseQueryParser(Arrays.asList(new RootQuery.RootParser(), new DummiesFunction.DummiesParser())) { - @Override - public boolean apply(Map source, QueryPart parent) { - return true; - } - }); + }.withQueryParser( + new BaseQueryParser(Arrays.asList(new RootQuery.RootParser(), new DummiesFunction.DummiesParser())) { + @Override + public boolean apply(Map source, QueryPart parent) { + return true; + } + }); @Test public void testNoFunctionParse() { String json = "{'query': {'keksiki': {}}}"; List queryParts = parser.parseQuery(json); assertEquals("root query", 1, queryParts.size()); - assertEquals("root query has 0 parameter - no recognized function names", 0, queryParts.get(0).getChildren().size()); + assertEquals("root query has 0 parameter - no recognized function names", 0, + queryParts.get(0).getChildren().size()); } @@ -47,19 +49,25 @@ public class JsonQueryParserTest { @Test(expected = QueryParserException.class) public void testKekParseError() { - String json = "{'query': {'dummies': {'id': '1','kek': 'kekekekkekekekke','from_time': '2016-03-22T00:12:00Z','to_time': '2016-03-22T01:12:00Z'}}}"; + String json = + "{'query': {'dummies': {'id': '1','kek': 'kekekekkekekekke','from_time': '2016-03-22T00:12:00Z'," + + "'to_time': '2016-03-22T01:12:00Z'}}}"; parser.parseQuery(json); } @Test public void testHappyPath() { - String json = "{'query': {'dummies': {'id': '1','kek': 'kek','from_time': '2016-03-22T00:12:00Z','to_time': '2016-03-22T01:12:00Z'}}}"; + String json = + "{'query': {'dummies': {'id': '1','kek': 'kek','from_time': '2016-03-22T00:12:00Z'," + + "'to_time': '2016-03-22T01:12:00Z'}}}"; parser.parseQuery(json); } @Test(expected = QueryParserException.class) public void testTimeParseError() { - String json = "{'query': {'dummies': {'id': '1','kek': '2','from_time': '2016-03-22T00:12:00Z','to_time': '2016-03-22T00:00:00Z'}}}"; + String json = + "{'query': {'dummies': {'id': '1','kek': '2','from_time': '2016-03-22T00:12:00Z'," + + "'to_time': '2016-03-22T00:00:00Z'}}}"; parser.parseQuery(json); } } diff --git a/src/test/java/com/rbkmoney/magista/dsl/search/DummiesSearchQueryTest.java b/src/test/java/dev/vality/magista/dsl/search/DummiesSearchQueryTest.java similarity index 57% rename from src/test/java/com/rbkmoney/magista/dsl/search/DummiesSearchQueryTest.java rename to src/test/java/dev/vality/magista/dsl/search/DummiesSearchQueryTest.java index 15a5465..7de3728 100644 --- a/src/test/java/com/rbkmoney/magista/dsl/search/DummiesSearchQueryTest.java +++ b/src/test/java/dev/vality/magista/dsl/search/DummiesSearchQueryTest.java @@ -1,11 +1,11 @@ -package com.rbkmoney.magista.dsl.search; +package dev.vality.magista.dsl.search; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.ObjectMapper; -import com.rbkmoney.magista.dsl.*; -import com.rbkmoney.magista.dsl.builder.BaseQueryBuilder; -import com.rbkmoney.magista.dsl.builder.QueryBuilder; -import com.rbkmoney.magista.dsl.parser.*; +import dev.vality.magista.dsl.*; +import dev.vality.magista.dsl.builder.BaseQueryBuilder; +import dev.vality.magista.dsl.builder.QueryBuilder; +import dev.vality.magista.dsl.parser.*; import org.junit.Test; import java.util.Arrays; @@ -17,18 +17,20 @@ import static org.junit.Assert.assertEquals; public class DummiesSearchQueryTest { - private final BaseQueryParser queryPartParser = new BaseQueryParser(Arrays.asList(new RootQuery.RootParser(), new DummiesFunction.DummiesParser())) { - @Override - public boolean apply(Map source, QueryPart parent) { - return true; - } - }; - private final BaseQueryBuilder baseQueryBuilder = new BaseQueryBuilder(Arrays.asList(new RootQuery.RootBuilder(), new DummiesFunction.DummiesBuilder())) { - @Override - public boolean apply(List queryParts, QueryPart parent) { - return true; - } - }; + private final BaseQueryParser queryPartParser = + new BaseQueryParser(Arrays.asList(new RootQuery.RootParser(), new DummiesFunction.DummiesParser())) { + @Override + public boolean apply(Map source, QueryPart parent) { + return true; + } + }; + private final BaseQueryBuilder baseQueryBuilder = + new BaseQueryBuilder(Arrays.asList(new RootQuery.RootBuilder(), new DummiesFunction.DummiesBuilder())) { + @Override + public boolean apply(List queryParts, QueryPart parent) { + return true; + } + }; private final JsonQueryParser jsonQueryParser = new JsonQueryParser() { @Override protected ObjectMapper getMapper() { @@ -38,11 +40,14 @@ public class DummiesSearchQueryTest { } }.withQueryParser(queryPartParser); - QueryProcessorImpl queryProcessor = new QueryProcessorImpl(jsonQueryParser, baseQueryBuilder, new TestQueryContext(new SearchDao())); + QueryProcessorImpl queryProcessor = + new QueryProcessorImpl(jsonQueryParser, baseQueryBuilder, new TestQueryContext(new SearchDao())); @Test public void testPayments() { - String json = "{'query': {'dummies': {'id': '1','kek': 'kek','from_time': '2016-03-22T00:12:00Z','to_time': '2016-03-22T01:12:00Z'}}}"; + String json = + "{'query': {'dummies': {'id': '1','kek': 'kek','from_time': '2016-03-22T00:12:00Z'," + + "'to_time': '2016-03-22T01:12:00Z'}}}"; StatTestResponse statResponse = queryProcessor.processQuery(new StatTestRequest(json)); assertEquals(1, statResponse.getStatTestResponseData().getDummies().size()); } @@ -56,7 +61,9 @@ public class DummiesSearchQueryTest { @Test(expected = BadTokenException.class) public void testBadToken() { - String json = "{'query': {'dummies': {'id': '1','kek': 'kek','from_time': '2016-03-22T00:12:00Z','to_time': '2016-03-22T01:12:00Z'}}}"; + String json = + "{'query': {'dummies': {'id': '1','kek': 'kek','from_time': '2016-03-22T00:12:00Z'," + + "'to_time': '2016-03-22T01:12:00Z'}}}"; StatTestRequest statRequest = new StatTestRequest(json); statRequest.setContinuationToken(UUID.randomUUID().toString()); queryProcessor.processQuery(statRequest); @@ -64,18 +71,20 @@ public class DummiesSearchQueryTest { class QueryProcessorImpl implements QueryProcessor { - private QueryParser sourceParser; - private QueryBuilder queryBuilder; - private QueryContext queryContext; + private final QueryParser sourceParser; + private final QueryBuilder queryBuilder; + private final QueryContext queryContext; - public QueryProcessorImpl(QueryParser sourceParser, QueryBuilder queryBuilder, QueryContext queryContext) { + public QueryProcessorImpl(QueryParser sourceParser, QueryBuilder queryBuilder, + QueryContext queryContext) { this.sourceParser = sourceParser; this.queryBuilder = queryBuilder; this.queryContext = queryContext; } @Override - public StatTestResponse processQuery(StatTestRequest source) throws BadTokenException, QueryProcessingException { + public StatTestResponse processQuery(StatTestRequest source) + throws BadTokenException, QueryProcessingException { List queryParts = sourceParser.parseQuery(source.getDsl(), null); Query query = queryBuilder.buildQuery(queryParts, source.getContinuationToken(), null, null); QueryResult queryResult = query.execute(queryContext); diff --git a/src/test/resources/log4j.properties b/src/test/resources/log4j.properties index 5010c1f..9a0976a 100644 --- a/src/test/resources/log4j.properties +++ b/src/test/resources/log4j.properties @@ -1,5 +1,5 @@ #log4j.rootLogger=ALL, stdout -log4j.logger.com.rbkmoney=INFO, stdout +log4j.logger.dev.vality=INFO, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout