Skip to content

Commit b35d77e

Browse files
authored
Merge pull request #2068 from bosch-io/bugfix/aggregation-metrics
#2067 Support for boolean value for filter in aggregation metric
2 parents b3fdfc8 + 7270578 commit b35d77e

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

thingsearch/service/src/main/java/org/eclipse/ditto/thingsearch/service/persistence/read/criteria/visitors/CreateBsonAggregationPredicateVisitor.java

+6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
import javax.annotation.Nullable;
2424

2525
import org.bson.BsonArray;
26+
import org.bson.BsonBoolean;
2627
import org.bson.BsonDocument;
28+
import org.bson.BsonDouble;
2729
import org.bson.BsonInt64;
2830
import org.bson.BsonString;
2931
import org.bson.BsonValue;
@@ -194,10 +196,14 @@ private BsonValue resolveValue(final Object value) {
194196
return new BsonInt64((Long) value);
195197
} else if (value instanceof Integer) {
196198
return new BsonInt64((Integer) value);
199+
} else if (value instanceof Double) {
200+
return new BsonDouble((Double) value);
197201
} else if (value instanceof String) {
198202
return new BsonString((String) value);
199203
} else if (value instanceof ArrayList) {
200204
return new BsonArray((ArrayList) value);
205+
} else if (value instanceof Boolean) {
206+
return new BsonBoolean((Boolean) value);
201207
} else {
202208
throw new IllegalArgumentException("Unsupported value type: " + value.getClass());
203209
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2024 Contributors to the Eclipse Foundation
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information regarding copyright ownership.
6+
*
7+
* This program and the accompanying materials are made available under the
8+
* terms of the Eclipse Public License 2.0 which is available at
9+
* http://www.eclipse.org/legal/epl-2.0
10+
*
11+
* SPDX-License-Identifier: EPL-2.0
12+
*
13+
*/
14+
15+
package org.eclipse.ditto.thingsearch.service.persistence.read.criteria.visitors;
16+
17+
import java.util.Map;
18+
19+
import org.bson.BsonDocument;
20+
import org.bson.conversions.Bson;
21+
import org.eclipse.ditto.base.model.headers.DittoHeaders;
22+
import org.eclipse.ditto.rql.parser.RqlPredicateParser;
23+
import org.eclipse.ditto.rql.query.criteria.Criteria;
24+
import org.eclipse.ditto.rql.query.expression.ThingsFieldExpressionFactory;
25+
import org.eclipse.ditto.rql.query.filter.QueryFilterCriteriaFactory;
26+
import org.json.JSONArray;
27+
import org.json.JSONException;
28+
import org.junit.Test;
29+
import org.skyscreamer.jsonassert.Customization;
30+
import org.skyscreamer.jsonassert.JSONAssert;
31+
import org.skyscreamer.jsonassert.JSONCompareMode;
32+
import org.skyscreamer.jsonassert.comparator.CustomComparator;
33+
34+
public class CreateBsonAggregationPredicateVisitorTest {
35+
36+
37+
@Test
38+
public void aggregationCondFromRqlFilterToBson() throws JSONException {
39+
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)))";
40+
final QueryFilterCriteriaFactory criteriaFactory =
41+
QueryFilterCriteriaFactory.of(ThingsFieldExpressionFactory.of(Map.of(
42+
"_created", "/_created"
43+
)),
44+
RqlPredicateParser.getInstance());
45+
final Criteria criteria = criteriaFactory.filterCriteria(rqlFilter, DittoHeaders.empty());
46+
final Bson bson = CreateBsonAggregationVisitor.sudoApply(criteria);
47+
final CustomComparator comparator = new CustomComparator(JSONCompareMode.LENIENT,
48+
new Customization("$lt", (o1, o2) -> {
49+
if (o1 instanceof JSONArray array1 && o2 instanceof JSONArray array2) {
50+
try {
51+
return (array1.length() == array2.length()) &&
52+
array1.getString(0).equals(array2.getString(0));
53+
} catch (JSONException e) {
54+
throw new RuntimeException(e);
55+
}
56+
}
57+
return false;
58+
}));
59+
final String expectedCond =
60+
"{\"$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}}]}]}";
61+
final BsonDocument expected = BsonDocument.parse(expectedCond);
62+
JSONAssert.assertEquals(expected.toJson(), bson.toBsonDocument().toJson(), comparator);
63+
}
64+
}

0 commit comments

Comments
 (0)