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