Merge pull request #1 from valitydev/ft/TECHDEBT-32/build

Build imp
This commit is contained in:
Egor Cherniak 2022-01-13 16:18:16 +03:00 committed by GitHub
commit 309f4e2ba8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
52 changed files with 466 additions and 411 deletions

19
.github/workflows/build.yml vendored Normal file
View File

@ -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

22
.github/workflows/deploy.yml vendored Normal file
View File

@ -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 }}

3
.gitmodules vendored
View File

@ -1,3 +0,0 @@
[submodule "build_utils"]
path = build_utils
url = git@github.com:rbkmoney/build_utils.git

13
Jenkinsfile vendored
View File

@ -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)
}

@ -1 +0,0 @@
Subproject commit e1318727d4d0c3e48f5122bf3197158b6695f50e

View File

@ -5,9 +5,9 @@
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.rbkmoney</groupId>
<artifactId>parent</artifactId>
<version>1.0.5</version>
<groupId>dev.vality</groupId>
<artifactId>library-parent-pom</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>magista-dsl</artifactId>

View File

@ -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<QueryPart> getMatchedPartsStream(Object descriptor, List<QueryPart> queryParts) {
return queryParts.stream().filter(queryPart -> descriptor.equals(queryPart.getDescriptor()));
}
protected <T, CT> CompositeQuery<T, CT> createCompositeQuery(
Object descriptor,
QueryParameters derivedParameters,
List<Query> childQueries,
Function<QueryContext, QueryResult<T, CT>> execFunction,
BiFunction<QueryContext, List<QueryResult>, QueryResult<T, CT>> parallelExecFunction
) {
return BaseCompositeQuery.newInstance(descriptor, new QueryParameters(Collections.emptyMap(), derivedParameters), childQueries, execFunction, parallelExecFunction);
}
protected CompositeQuery createCompositeQuery(
Object descriptor,
QueryParameters derivedParameters,
List<Query> 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 <T, CT> CompositeQuery<T, CT> createCompositeQuery(
Object descriptor,
List<Query> childQueries,
Function<QueryContext, QueryResult<T, CT>> execFunction,
BiFunction<QueryContext, List<QueryResult>, QueryResult<T, CT>> 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<Query> buildQueries(Object matchDescriptor, List<QueryPart> queryParts, Function<QueryPart, Query> queryCreator) {
List<QueryPart> matchedParts = getMatchedPartsStream(matchDescriptor, queryParts).collect(Collectors.toList());
return matchedParts.stream().map(queryPart -> queryCreator.apply(queryPart)).collect(Collectors.toList());
}
protected Query buildSingleQuery(Object matchDescriptor, List<QueryPart> queryParts, Function<QueryPart, Query> queryCreator) {
List<Query> 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 <T, CT> Query<T, CT> buildAndWrapQueries(
Object matchDescriptor,
List<QueryPart> queryParts,
Function<QueryPart, Query> queryCreator,
QueryParameters parentParameters,
Function<QueryContext, QueryResult<T, CT>> execFunction,
BiFunction<QueryContext, List<QueryResult>, QueryResult<T, CT>> parallelExecFunction
) {
List<Query> queries = buildQueries(matchDescriptor, queryParts, queryCreator);
return createCompositeQuery(queries.get(0).getDescriptor(), parentParameters, queries, execFunction, parallelExecFunction);
}
}

View File

@ -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<QueryPart> queryParts, String continuationToken, QueryPart parentQueryPart, QueryBuilder baseBuilder) throws QueryBuilderException;
boolean apply(List<QueryPart> queryParts, QueryPart parent);
}

View File

@ -1,9 +0,0 @@
package com.rbkmoney.magista.dsl.parser;
import java.util.List;
public interface QueryParser<Src> {
List<QueryPart> parseQuery(Src source, QueryPart parent) throws QueryParserException;
boolean apply(Src source, QueryPart parent);
}

View File

@ -1,4 +1,4 @@
package com.rbkmoney.magista.dsl;
package dev.vality.magista.dsl;
public class BadTokenException extends IllegalArgumentException {

View File

@ -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<T, CT> extends BaseQuery<T, CT> implements CompositeQuery<T, CT> {
private List<Query> queries;
private final Function<QueryContext, QueryResult<T, CT>> execFunction;
private final BiFunction<QueryContext, List<QueryResult>, QueryResult<T, CT>> parallelExecFunction;
private List<Query> queries;
public static <T, CT> BaseCompositeQuery<T, CT> newInstance(Object descriptor, QueryParameters params, List<Query> queries, Function<QueryContext, QueryResult<T, CT>> execFunction) {
return newInstance(descriptor, params, queries, execFunction, null);
}
public static <T, CT> BaseCompositeQuery<T, CT> newInstance(Object descriptor, QueryParameters params, List<Query> queries, Function<QueryContext, QueryResult<T, CT>> execFunction, BiFunction<QueryContext, List<QueryResult>, QueryResult<T, CT>> parallelExecFunction) {
BaseCompositeQuery<T, CT> compositeQuery = new BaseCompositeQuery<>(descriptor, params, execFunction, parallelExecFunction);
compositeQuery.setChildQueries(queries);
return compositeQuery;
}
public BaseCompositeQuery(Object descriptor, QueryParameters params, Function<QueryContext, QueryResult<T, CT>> execFunction, BiFunction<QueryContext, List<QueryResult>, QueryResult<T, CT>> parallelExecFunction) {
public BaseCompositeQuery(Object descriptor, QueryParameters params,
Function<QueryContext, QueryResult<T, CT>> execFunction,
BiFunction<QueryContext, List<QueryResult>, QueryResult<T, CT>> parallelExecFunction) {
super(descriptor, params);
if (execFunction == null) {
throw new NullPointerException("Null exec function is not allowed");
@ -31,10 +22,32 @@ public class BaseCompositeQuery<T, CT> extends BaseQuery<T, CT> implements Compo
this.parallelExecFunction = parallelExecFunction;
}
public static <T, CT> BaseCompositeQuery<T, CT> newInstance(Object descriptor, QueryParameters params,
List<Query> queries,
Function<QueryContext,
QueryResult<T, CT>> execFunction) {
return newInstance(descriptor, params, queries, execFunction, null);
}
public static <T, CT> BaseCompositeQuery<T, CT> newInstance(Object descriptor, QueryParameters params,
List<Query> queries,
Function<QueryContext, QueryResult<T, CT>> execFunction,
BiFunction<QueryContext, List<QueryResult>,
QueryResult<T, CT>> parallelExecFunction) {
BaseCompositeQuery<T, CT> compositeQuery =
new BaseCompositeQuery<>(descriptor, params, execFunction, parallelExecFunction);
compositeQuery.setChildQueries(queries);
return compositeQuery;
}
public List<Query> getChildQueries() {
return queries;
}
protected void setChildQueries(List<Query> 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<T, CT> extends BaseQuery<T, CT> implements Compo
}
@Override
public QueryResult<T, CT> execute(QueryContext context, List<QueryResult> collectedResults) throws QueryExecutionException {
public QueryResult<T, CT> execute(QueryContext context, List<QueryResult> collectedResults)
throws QueryExecutionException {
if (parallelExecFunction != null) {
return parallelExecFunction.apply(context, collectedResults);
} else {
return CompositeQuery.super.execute(context, collectedResults);
}
}
protected void setChildQueries(List<Query> queries) {
this.queries = queries.stream().peek(query -> query.setParentQuery(this)).collect(Collectors.toList());
}
}

View File

@ -1,4 +1,4 @@
package com.rbkmoney.magista.dsl;
package dev.vality.magista.dsl;
public abstract class BaseFunction<T, CT> extends BaseQuery<T, CT> implements FunctionQuery<T, CT> {

View File

@ -1,4 +1,4 @@
package com.rbkmoney.magista.dsl;
package dev.vality.magista.dsl;
public abstract class BaseQuery<T, CT> implements Query<T, CT> {
private final Object descriptor;

View File

@ -1,4 +1,4 @@
package com.rbkmoney.magista.dsl;
package dev.vality.magista.dsl;
import java.util.function.Supplier;
import java.util.stream.Stream;

View File

@ -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 extends QueryParameters> T checkParamsType(QueryParameters parameters, Class<? extends QueryParameters> expectedType) {
protected <T extends QueryParameters> T checkParamsType(QueryParameters parameters,
Class<? extends QueryParameters> 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;
}

View File

@ -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<T, CT> extends Query<T, CT> {
return false;
}
default QueryResult<T, CT> execute(QueryContext context, List<QueryResult> collectedResults) throws QueryExecutionException {
default QueryResult<T, CT> execute(QueryContext context, List<QueryResult> collectedResults)
throws QueryExecutionException {
throw new UnsupportedOperationException("Explicit implementation required");
}

View File

@ -1,4 +1,4 @@
package com.rbkmoney.magista.dsl;
package dev.vality.magista.dsl;
public interface FunctionQuery<T, CT> extends Query<T, CT> {

View File

@ -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<T, CT> extends BaseFunction<T, CT> {
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<T, CT> extends BaseFunction<T, CT> {
}
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<T, CT> extends BaseFunction<T, CT> {
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<T, CT> extends BaseFunction<T, CT> {
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) {

View File

@ -1,4 +1,4 @@
package com.rbkmoney.magista.dsl;
package dev.vality.magista.dsl;
public class Parameters {
public static final String QUERY_PARAMETER = "query";

View File

@ -1,4 +1,4 @@
package com.rbkmoney.magista.dsl;
package dev.vality.magista.dsl;
public interface Query<T, CT> {
Object getDescriptor();

View File

@ -1,4 +1,4 @@
package com.rbkmoney.magista.dsl;
package dev.vality.magista.dsl;
public interface QueryContext {
}

View File

@ -1,4 +1,4 @@
package com.rbkmoney.magista.dsl;
package dev.vality.magista.dsl;
public interface QueryContextFactory {
QueryContext getContext();

View File

@ -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);
}
}

View File

@ -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 extends QueryParameters> {
T newInstance(Map<String, Object> parameters, QueryParameters derivedParameters);
}
private final Map<String, Object> 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 extends QueryParameters> T removeParameters(QueryParametersRef<T> 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 extends QueryParameters> {
T newInstance(Map<String, Object> parameters, QueryParameters derivedParameters);
}
}

View File

@ -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);
}
}

View File

@ -1,4 +1,4 @@
package com.rbkmoney.magista.dsl;
package dev.vality.magista.dsl;
public interface QueryProcessor<S, R> {
R processQuery(S source) throws BadTokenException, QueryProcessingException;

View File

@ -1,4 +1,4 @@
package com.rbkmoney.magista.dsl;
package dev.vality.magista.dsl;
import java.util.stream.Stream;

View File

@ -1,4 +1,4 @@
package com.rbkmoney.magista.dsl;
package dev.vality.magista.dsl;
public interface QueryValidator {
void validateParameters(QueryParameters parameters) throws IllegalArgumentException;

View File

@ -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<? extends Collection> childQueries = Optional.ofNullable(((CompositeQuery) childQuery).getChildQueries());
checkParamsResult(
!childQueries.isPresent() || childQueries.get().isEmpty(),
"Request must contain at least one query"
);
Optional<? extends Collection> 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<QueryPart> parseQuery(Map<String, Object> 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<QueryPart> queryParts, String continuationToken, QueryPart parentQueryPart, QueryBuilder baseBuilder) throws QueryBuilderException {
Query resultQuery = buildSingleQuery(RootParser.getMainDescriptor(), queryParts, queryPart -> createQuery(queryPart, continuationToken, baseBuilder));
public Query buildQuery(List<QueryPart> 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;

View File

@ -1,4 +1,4 @@
package com.rbkmoney.magista.dsl;
package dev.vality.magista.dsl;
import java.util.Optional;
import java.util.UUID;

View File

@ -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<QueryPart> getMatchedPartsStream(Object descriptor, List<QueryPart> queryParts) {
return queryParts.stream().filter(queryPart -> descriptor.equals(queryPart.getDescriptor()));
}
protected <T, CT> CompositeQuery<T, CT> createCompositeQuery(Object descriptor, QueryParameters derivedParameters,
List<Query> childQueries,
Function<QueryContext,
QueryResult<T, CT>> execFunction,
BiFunction<QueryContext, List<QueryResult>,
QueryResult<T, CT>> parallelExecFunction) {
return BaseCompositeQuery.newInstance(descriptor,
new QueryParameters(Collections.emptyMap(), derivedParameters), childQueries, execFunction,
parallelExecFunction);
}
protected CompositeQuery createCompositeQuery(Object descriptor, QueryParameters derivedParameters,
List<Query> 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 <T, CT> CompositeQuery<T, CT> createCompositeQuery(Object descriptor, List<Query> childQueries,
Function<QueryContext,
QueryResult<T, CT>> execFunction,
BiFunction<QueryContext, List<QueryResult>,
QueryResult<T, CT>> 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<Query> buildQueries(Object matchDescriptor, List<QueryPart> queryParts,
Function<QueryPart, Query> queryCreator) {
List<QueryPart> matchedParts = getMatchedPartsStream(matchDescriptor, queryParts).collect(Collectors.toList());
return matchedParts.stream().map(queryPart -> queryCreator.apply(queryPart)).collect(Collectors.toList());
}
protected Query buildSingleQuery(Object matchDescriptor, List<QueryPart> queryParts,
Function<QueryPart, Query> queryCreator) {
List<Query> 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 <T, CT> Query<T, CT> buildAndWrapQueries(Object matchDescriptor, List<QueryPart> queryParts,
Function<QueryPart, Query> queryCreator,
QueryParameters parentParameters,
Function<QueryContext, QueryResult<T, CT>> execFunction,
BiFunction<QueryContext, List<QueryResult>,
QueryResult<T, CT>> parallelExecFunction) {
List<Query> queries = buildQueries(matchDescriptor, queryParts, queryCreator);
return createCompositeQuery(queries.get(0).getDescriptor(), parentParameters, queries, execFunction,
parallelExecFunction);
}
}

View File

@ -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<QueryPart> queryParts, String continuationToken, QueryPart parentQueryPart, QueryBuilder baseBuilder) throws QueryBuilderException {
public Query buildQuery(List<QueryPart> queryParts, String continuationToken, QueryPart parentQueryPart,
QueryBuilder baseBuilder) throws QueryBuilderException {
try {
List<Query> 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<Query> 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) {

View File

@ -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<QueryPart> queryParts, String continuationToken, QueryPart parentQueryPart,
QueryBuilder baseBuilder) throws QueryBuilderException;
boolean apply(List<QueryPart> queryParts, QueryPart parent);
}

View File

@ -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);
}
}

View File

@ -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<Map<String, Obj
return queryPart == null ? null : queryPart.getParameters();
}
protected <T extends QueryParameters> T getValidatedParameters(Map<String, Object> source, QueryPart parent, QueryParameters.QueryParametersRef<T> parametersRef, QueryValidator validator) throws QueryParserException {
protected <T extends QueryParameters> T getValidatedParameters(Map<String, Object> source, QueryPart parent,
QueryParameters.QueryParametersRef<T> parametersRef,
QueryValidator validator)
throws QueryParserException {
try {
T parameters = parametersRef.newInstance(source, getParameters(parent));
validator.validateParameters(parameters);

View File

@ -1,4 +1,4 @@
package com.rbkmoney.magista.dsl.parser;
package dev.vality.magista.dsl.parser;
import java.util.ArrayList;
import java.util.Collections;

View File

@ -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<String> {
private QueryParser<Map<String, Object>> queryPartParser;
public JsonQueryParser withQueryParser(QueryParser<Map<String, Object>> queryPartParser){
public JsonQueryParser withQueryParser(QueryParser<Map<String, Object>> queryPartParser) {
this.queryPartParser = queryPartParser;
return this;
}
@ -32,7 +32,7 @@ public class JsonQueryParser implements QueryParser<String> {
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<String> {
}
protected Map<String, Object> parseJsonMap(String data) throws IOException {
return getMapper().readerFor(new TypeReference<Map<String, Object>>() {}).readValue(data);
return getMapper().readerFor(new TypeReference<Map<String, Object>>() {
}).readValue(data);
}
}

View File

@ -0,0 +1,10 @@
package dev.vality.magista.dsl.parser;
import java.util.List;
public interface QueryParser<T> {
List<QueryPart> parseQuery(T source, QueryPart parent) throws QueryParserException;
boolean apply(T source, QueryPart parent);
}

View File

@ -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);
}
}

View File

@ -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<QueryPart> children;
private final QueryPart parent;
private List<QueryPart> 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") + '}';
}
}

View File

@ -1,17 +0,0 @@
package com.rbkmoney.magista.dsl;
import java.util.*;
import java.util.stream.Collectors;
public class SearchDao {
public static final List<Dummy> 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<Map.Entry<Long, Dummy>> getDummies(String id) {
return allDummies.stream().filter(d -> id.equals(d.getId().toString())).map(d -> new AbstractMap.SimpleEntry<>(d.getId(), d)).collect(Collectors.toList());
}
}

View File

@ -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<Map.Entry<Long, Dummy>, StatTestResponse> implements CompositeQuery<Map.Entry<Long, Dummy>, StatTestResponse> {
public class DummiesFunction extends PagedBaseFunction<Map.Entry<Long, Dummy>, StatTestResponse>
implements CompositeQuery<Map.Entry<Long, Dummy>, StatTestResponse> {
public static final String FUNC_NAME = "dummies";
private final CompositeQuery<QueryResult, List<QueryResult>> subquery;
private DummiesFunction(Object descriptor, QueryParameters params, String continuationToken, CompositeQuery<QueryResult, List<QueryResult>> subquery) {
private DummiesFunction(Object descriptor, QueryParameters params, String continuationToken,
CompositeQuery<QueryResult, List<QueryResult>> subquery) {
super(descriptor, params, FUNC_NAME, continuationToken);
this.subquery = subquery;
}
private static DummiesFunction createDummiesFunction(Object descriptor, QueryParameters queryParameters,
String continuationToken,
CompositeQuery<QueryResult, List<QueryResult>> subquery) {
DummiesFunction dummiesFunction = new DummiesFunction(descriptor, queryParameters, continuationToken, subquery);
subquery.setParentQuery(dummiesFunction);
return dummiesFunction;
}
@Override
public QueryResult<Map.Entry<Long, Dummy>, StatTestResponse> execute(QueryContext context) throws QueryExecutionException {
public QueryResult<Map.Entry<Long, Dummy>, StatTestResponse> execute(QueryContext context)
throws QueryExecutionException {
QueryResult<QueryResult, List<QueryResult>> collectedResults = subquery.execute(context);
return execute(context, collectedResults.getCollectedStream());
}
@Override
public QueryResult<Map.Entry<Long, Dummy>, StatTestResponse> execute(QueryContext context, List<QueryResult> collectedResults) throws QueryExecutionException {
QueryResult<Map.Entry<Long, Dummy>, List<Map.Entry<Long, Dummy>>> dummiesResult = (QueryResult<Map.Entry<Long, Dummy>, List<Map.Entry<Long, Dummy>>>) collectedResults.get(0);
public QueryResult<Map.Entry<Long, Dummy>, StatTestResponse> execute(QueryContext context,
List<QueryResult> collectedResults)
throws QueryExecutionException {
QueryResult<Map.Entry<Long, Dummy>, List<Map.Entry<Long, Dummy>>> dummiesResult =
(QueryResult<Map.Entry<Long, Dummy>, List<Map.Entry<Long, Dummy>>>) collectedResults.get(0);
return new BaseQueryResult<>(
() -> dummiesResult.getDataStream(),
() -> {
List<Dummy> statResponseData = dummiesResult.getDataStream()
.map(dummyEntry -> dummyEntry.getValue()).collect(Collectors.toList());
StatTestResponse statResponse = new StatTestResponse(statResponseData);
List<Map.Entry<Long, Dummy>> 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<Dummy> statResponseData =
dummiesResult.getDataStream().map(dummyEntry -> dummyEntry.getValue()).collect(Collectors.toList());
StatTestResponse statResponse = new StatTestResponse(statResponseData);
List<Map.Entry<Long, Dummy>> 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<Map.Entry<Long, Dummy>, 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<QueryPart> parseQuery(Map<String, Object> source, QueryPart parent) throws QueryParserException {
Map<String, Object> 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<QueryPart> queryParts, String continuationToken, QueryPart parentQueryPart, QueryBuilder baseBuilder) throws QueryBuilderException {
Query resultQuery = buildSingleQuery(DummiesParser.getMainDescriptor(), queryParts, queryPart -> createQuery(queryPart, continuationToken));
public Query buildQuery(List<QueryPart> 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<Query> queries = Arrays.asList(
new GetDataFunction(queryPart.getDescriptor() + ":" + GetDataFunction.FUNC_NAME, queryPart.getParameters(), continuationToken)
);
CompositeQuery<QueryResult, List<QueryResult>> 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<QueryResult, List<QueryResult>> 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<Map.Entry<Long, Dummy>, S
}
}
private static DummiesFunction createDummiesFunction(Object descriptor, QueryParameters queryParameters, String continuationToken, CompositeQuery<QueryResult, List<QueryResult>> subquery) {
DummiesFunction dummiesFunction = new DummiesFunction(descriptor, queryParameters, continuationToken, subquery);
subquery.setParentQuery(dummiesFunction);
return dummiesFunction;
}
private static class GetDataFunction extends PagedBaseFunction<Map.Entry<Long, Dummy>, Collection<Map.Entry<Long, Dummy>>> {
private static class GetDataFunction
extends PagedBaseFunction<Map.Entry<Long, Dummy>, Collection<Map.Entry<Long, Dummy>>> {
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<Map.Entry<Long, Dummy>, S
}
@Override
public QueryResult<Map.Entry<Long, Dummy>, Collection<Map.Entry<Long, Dummy>>> execute(QueryContext context) throws QueryExecutionException {
public QueryResult<Map.Entry<Long, Dummy>, Collection<Map.Entry<Long, Dummy>>> execute(QueryContext context)
throws QueryExecutionException {
TestQueryContext functionContext = getContext(context);
TestParameters parameters = new TestParameters(getQueryParameters(), getQueryParameters().getDerivedParameters());
Collection<Map.Entry<Long, Dummy>> result = functionContext.getSearchDao().getDummies(
parameters.getId()
);
TestParameters parameters =
new TestParameters(getQueryParameters(), getQueryParameters().getDerivedParameters());
Collection<Map.Entry<Long, Dummy>> result = functionContext.getSearchDao().getDummies(parameters.getId());
return new BaseQueryResult<>(() -> result.stream(), () -> result);
}

View File

@ -1,4 +1,4 @@
package com.rbkmoney.magista.dsl;
package dev.vality.magista.dsl;
public class Dummy {
private Long id;

View File

@ -0,0 +1,18 @@
package dev.vality.magista.dsl;
import java.util.*;
import java.util.stream.Collectors;
public class SearchDao {
public static final List<Dummy> 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<Map.Entry<Long, Dummy>> getDummies(String id) {
return allDummies.stream().filter(d -> id.equals(d.getId().toString()))
.map(d -> new AbstractMap.SimpleEntry<>(d.getId(), d)).collect(Collectors.toList());
}
}

View File

@ -1,4 +1,4 @@
package com.rbkmoney.magista.dsl;
package dev.vality.magista.dsl;
public class StatTestRequest {

View File

@ -1,4 +1,4 @@
package com.rbkmoney.magista.dsl;
package dev.vality.magista.dsl;
import java.util.List;

View File

@ -1,4 +1,4 @@
package com.rbkmoney.magista.dsl;
package dev.vality.magista.dsl;
import java.util.List;

View File

@ -1,4 +1,4 @@
package com.rbkmoney.magista.dsl;
package dev.vality.magista.dsl;
public class TestQueryContext implements QueryContext {

View File

@ -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<String, Object> source, QueryPart parent) {
return true;
}
});
}.withQueryParser(
new BaseQueryParser(Arrays.asList(new RootQuery.RootParser(), new DummiesFunction.DummiesParser())) {
@Override
public boolean apply(Map<String, Object> source, QueryPart parent) {
return true;
}
});
private QueryBuilder builder = new BaseQueryBuilder(Arrays.asList(new RootQuery.RootBuilder(), new DummiesFunction.DummiesBuilder())) {
@Override
public boolean apply(List<QueryPart> queryParts, QueryPart parent) {
return true;
}
};
private final QueryBuilder builder =
new BaseQueryBuilder(Arrays.asList(new RootQuery.RootBuilder(), new DummiesFunction.DummiesBuilder())) {
@Override
public boolean apply(List<QueryPart> 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();

View File

@ -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<String, Object> source, QueryPart parent) {
return true;
}
});
}.withQueryParser(
new BaseQueryParser(Arrays.asList(new RootQuery.RootParser(), new DummiesFunction.DummiesParser())) {
@Override
public boolean apply(Map<String, Object> source, QueryPart parent) {
return true;
}
});
@Test
public void testNoFunctionParse() {
String json = "{'query': {'keksiki': {}}}";
List<QueryPart> 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);
}
}

View File

@ -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<String, Object> source, QueryPart parent) {
return true;
}
};
private final BaseQueryBuilder baseQueryBuilder = new BaseQueryBuilder(Arrays.asList(new RootQuery.RootBuilder(), new DummiesFunction.DummiesBuilder())) {
@Override
public boolean apply(List<QueryPart> queryParts, QueryPart parent) {
return true;
}
};
private final BaseQueryParser queryPartParser =
new BaseQueryParser(Arrays.asList(new RootQuery.RootParser(), new DummiesFunction.DummiesParser())) {
@Override
public boolean apply(Map<String, Object> source, QueryPart parent) {
return true;
}
};
private final BaseQueryBuilder baseQueryBuilder =
new BaseQueryBuilder(Arrays.asList(new RootQuery.RootBuilder(), new DummiesFunction.DummiesBuilder())) {
@Override
public boolean apply(List<QueryPart> 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<StatTestRequest, StatTestResponse> {
private QueryParser<String> sourceParser;
private QueryBuilder queryBuilder;
private QueryContext queryContext;
private final QueryParser<String> sourceParser;
private final QueryBuilder queryBuilder;
private final QueryContext queryContext;
public QueryProcessorImpl(QueryParser<String> sourceParser, QueryBuilder queryBuilder, QueryContext queryContext) {
public QueryProcessorImpl(QueryParser<String> 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<QueryPart> queryParts = sourceParser.parseQuery(source.getDsl(), null);
Query query = queryBuilder.buildQuery(queryParts, source.getContinuationToken(), null, null);
QueryResult queryResult = query.execute(queryContext);

View File

@ -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