Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2067 Support for boolean value for filter in aggregation metric #2068

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import javax.annotation.Nullable;

import org.bson.BsonArray;
import org.bson.BsonBoolean;
import org.bson.BsonDocument;
import org.bson.BsonDouble;
import org.bson.BsonInt64;
import org.bson.BsonString;
import org.bson.BsonValue;
Expand Down Expand Up @@ -194,10 +196,14 @@ private BsonValue resolveValue(final Object value) {
return new BsonInt64((Long) value);
} else if (value instanceof Integer) {
return new BsonInt64((Integer) value);
} else if (value instanceof Double) {
return new BsonDouble((Double) value);
} else if (value instanceof String) {
return new BsonString((String) value);
} else if (value instanceof ArrayList) {
return new BsonArray((ArrayList) value);
} else if (value instanceof Boolean) {
return new BsonBoolean((Boolean) value);
} else {
throw new IllegalArgumentException("Unsupported value type: " + value.getClass());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*
*/

package org.eclipse.ditto.thingsearch.service.persistence.read.criteria.visitors;

import java.util.Map;

import org.bson.BsonDocument;
import org.bson.conversions.Bson;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.rql.parser.RqlPredicateParser;
import org.eclipse.ditto.rql.query.criteria.Criteria;
import org.eclipse.ditto.rql.query.expression.ThingsFieldExpressionFactory;
import org.eclipse.ditto.rql.query.filter.QueryFilterCriteriaFactory;
import org.json.JSONArray;
import org.json.JSONException;
import org.junit.Test;
import org.skyscreamer.jsonassert.Customization;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.skyscreamer.jsonassert.comparator.CustomComparator;

public class CreateBsonAggregationPredicateVisitorTest {


@Test
public void aggregationCondFromRqlFilterToBson() throws JSONException {
final String rqlFilter = "and(eq(attributes/Info/gateway,true),gt(_created,\"2024-02-15T09:00\"),lt(features/ConnectionStatus/properties/status/readyUntil/,time:now),not(exists(features/GatewayServices)))";
final QueryFilterCriteriaFactory criteriaFactory =
QueryFilterCriteriaFactory.of(ThingsFieldExpressionFactory.of(Map.of(
"_created", "/_created"
)),
RqlPredicateParser.getInstance());
final Criteria criteria = criteriaFactory.filterCriteria(rqlFilter, DittoHeaders.empty());
final Bson bson = CreateBsonAggregationVisitor.sudoApply(criteria);
final CustomComparator comparator = new CustomComparator(JSONCompareMode.LENIENT,
new Customization("$lt", (o1, o2) -> {
if (o1 instanceof JSONArray array1 && o2 instanceof JSONArray array2) {
try {
return (array1.length() == array2.length()) &&
array1.getString(0).equals(array2.getString(0));
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
return false;
}));
final String expectedCond =
"{\"$and\": [{\"$eq\": [\"$t.attributes.Info.gateway\", true]}, {\"$gt\": [\"$t._created\", \"2024-02-15T09:00\"]}, {\"$lt\": [\"$t.features.ConnectionStatus.properties.status.readyUntil\", \"2024-01-01T00:00:00.000000Z\"]}, {\"$nor\": [{\"t.features.GatewayServices\": {\"$exists\": true}}]}]}";
final BsonDocument expected = BsonDocument.parse(expectedCond);
JSONAssert.assertEquals(expected.toJson(), bson.toBsonDocument().toJson(), comparator);
}
}
Loading