xref: /aosp_15_r20/cts/tests/appsearch/src/com/android/cts/appsearch/external/ast/operators/ComparatorNodeCtsTest.java (revision b7c941bb3fa97aba169d73cee0bed2de8ac964bf)
1 /*
2  * Copyright 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.app.appsearch.cts.ast.operators;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.junit.Assert.assertThrows;
22 
23 import android.app.appsearch.PropertyPath;
24 import android.app.appsearch.ast.operators.ComparatorNode;
25 import android.app.appsearch.testutil.AppSearchTestUtils;
26 import android.platform.test.annotations.RequiresFlagsEnabled;
27 
28 import com.android.appsearch.flags.Flags;
29 
30 import org.junit.Rule;
31 import org.junit.Test;
32 import org.junit.rules.RuleChain;
33 
34 import java.util.List;
35 
36 @RequiresFlagsEnabled(Flags.FLAG_ENABLE_ABSTRACT_SYNTAX_TREES)
37 public class ComparatorNodeCtsTest {
38     @Rule public final RuleChain mRuleChain = AppSearchTestUtils.createCommonTestRules();
39 
40     @Test
testEquals_identical()41     public void testEquals_identical() {
42         PropertyPath propertyPathOne = new PropertyPath("example.property.path");
43         int valueOne = 5;
44         ComparatorNode equalsNodeOne =
45                 new ComparatorNode(ComparatorNode.EQUALS, propertyPathOne, valueOne);
46 
47         PropertyPath propertyPathTwo = new PropertyPath("example.property.path");
48         int valueTwo = 5;
49         ComparatorNode equalsNodeTwo =
50                 new ComparatorNode(ComparatorNode.EQUALS, propertyPathTwo, valueTwo);
51 
52         assertThat(equalsNodeOne).isEqualTo(equalsNodeTwo);
53         assertThat(equalsNodeOne.hashCode()).isEqualTo(equalsNodeTwo.hashCode());
54     }
55 
56     @Test
testConstructor_correctConstruction()57     public void testConstructor_correctConstruction() {
58         List<PropertyPath.PathSegment> pathSegmentList =
59                 List.of(
60                         PropertyPath.PathSegment.create("example"),
61                         PropertyPath.PathSegment.create("property"),
62                         PropertyPath.PathSegment.create("path"));
63         PropertyPath propertyPath = new PropertyPath(pathSegmentList);
64 
65         int value = 5;
66 
67         ComparatorNode equalsNode = new ComparatorNode(ComparatorNode.EQUALS, propertyPath, value);
68 
69         assertThat(equalsNode.getComparator()).isEqualTo(ComparatorNode.EQUALS);
70         assertThat(equalsNode.getPropertyPath()).isEqualTo(propertyPath);
71         assertThat(equalsNode.getValue()).isEqualTo(value);
72     }
73 
74     @Test
testConstructor_throwsOnInvalidComparator()75     public void testConstructor_throwsOnInvalidComparator() {
76         List<PropertyPath.PathSegment> pathSegmentList =
77                 List.of(
78                         PropertyPath.PathSegment.create("example"),
79                         PropertyPath.PathSegment.create("property"),
80                         PropertyPath.PathSegment.create("path"));
81         PropertyPath propertyPath = new PropertyPath(pathSegmentList);
82 
83         int value = 5;
84 
85         IllegalArgumentException thrownLow =
86                 assertThrows(
87                         IllegalArgumentException.class,
88                         () -> new ComparatorNode(-1, propertyPath, value));
89         assertThat(thrownLow).hasMessageThat().contains("Comparator intDef");
90         assertThat(thrownLow).hasMessageThat().contains("too low");
91 
92         IllegalArgumentException thrownHigh =
93                 assertThrows(
94                         IllegalArgumentException.class,
95                         () -> new ComparatorNode(1000, propertyPath, value));
96         assertThat(thrownHigh).hasMessageThat().contains("Comparator intDef");
97         assertThat(thrownHigh).hasMessageThat().contains("too high");
98     }
99 
100     @Test
testConstructor_throwsOnNullPointer()101     public void testConstructor_throwsOnNullPointer() {
102         PropertyPath nullPropertyPath = null;
103         int value = 5;
104 
105         assertThrows(
106                 NullPointerException.class,
107                 () -> new ComparatorNode(ComparatorNode.GREATER_EQUALS, nullPropertyPath, value));
108     }
109 
110     @Test
testGetChildren_returnsEmptyList()111     public void testGetChildren_returnsEmptyList() {
112         List<PropertyPath.PathSegment> pathSegmentList =
113                 List.of(
114                         PropertyPath.PathSegment.create("example"),
115                         PropertyPath.PathSegment.create("property"),
116                         PropertyPath.PathSegment.create("path"));
117         PropertyPath propertyPath = new PropertyPath(pathSegmentList);
118 
119         int value = 5;
120 
121         ComparatorNode lessThanNode =
122                 new ComparatorNode(ComparatorNode.LESS_THAN, propertyPath, value);
123 
124         assertThat(lessThanNode.getChildren().isEmpty()).isTrue();
125     }
126 
127     @Test
testSetComparator_throwsOnInvalidComparator()128     public void testSetComparator_throwsOnInvalidComparator() {
129         List<PropertyPath.PathSegment> pathSegmentList =
130                 List.of(
131                         PropertyPath.PathSegment.create("example"),
132                         PropertyPath.PathSegment.create("property"),
133                         PropertyPath.PathSegment.create("path"));
134         PropertyPath propertyPath = new PropertyPath(pathSegmentList);
135 
136         int value = 5;
137 
138         IllegalArgumentException thrownLow =
139                 assertThrows(
140                         IllegalArgumentException.class,
141                         () ->
142                                 new ComparatorNode(ComparatorNode.GREATER_THAN, propertyPath, value)
143                                         .setComparator(-1));
144         assertThat(thrownLow).hasMessageThat().contains("Comparator intDef");
145         assertThat(thrownLow).hasMessageThat().contains("too low");
146 
147         IllegalArgumentException thrownHigh =
148                 assertThrows(
149                         IllegalArgumentException.class,
150                         () ->
151                                 new ComparatorNode(ComparatorNode.GREATER_THAN, propertyPath, value)
152                                         .setComparator(1000));
153         assertThat(thrownHigh).hasMessageThat().contains("Comparator intDef");
154         assertThat(thrownHigh).hasMessageThat().contains("too high");
155     }
156 
157     @Test
testSetPropertyPath_throwsOnNullPropertyPath()158     public void testSetPropertyPath_throwsOnNullPropertyPath() {
159         List<PropertyPath.PathSegment> pathSegmentList =
160                 List.of(
161                         PropertyPath.PathSegment.create("example"),
162                         PropertyPath.PathSegment.create("property"),
163                         PropertyPath.PathSegment.create("path"));
164         PropertyPath propertyPath = new PropertyPath(pathSegmentList);
165 
166         int value = 5;
167 
168         assertThrows(
169                 NullPointerException.class,
170                 () ->
171                         new ComparatorNode(ComparatorNode.LESS_EQUALS, propertyPath, value)
172                                 .setPropertyPath(null));
173     }
174 
175     @Test
testToString_equals_returnsCorrectString()176     public void testToString_equals_returnsCorrectString() {
177         List<PropertyPath.PathSegment> pathSegmentList =
178                 List.of(
179                         PropertyPath.PathSegment.create("example"),
180                         PropertyPath.PathSegment.create("property"),
181                         PropertyPath.PathSegment.create("path"));
182         PropertyPath propertyPath = new PropertyPath(pathSegmentList);
183         int value = 0;
184 
185         ComparatorNode comparatorNode =
186                 new ComparatorNode(ComparatorNode.EQUALS, propertyPath, value);
187 
188         assertThat(comparatorNode.toString()).isEqualTo("(example.property.path == 0)");
189     }
190 
191     @Test
testToString_lessThan_returnsCorrectString()192     public void testToString_lessThan_returnsCorrectString() {
193         List<PropertyPath.PathSegment> pathSegmentList =
194                 List.of(
195                         PropertyPath.PathSegment.create("example"),
196                         PropertyPath.PathSegment.create("property"),
197                         PropertyPath.PathSegment.create("path"));
198         PropertyPath propertyPath = new PropertyPath(pathSegmentList);
199         int value = 0;
200 
201         ComparatorNode comparatorNode =
202                 new ComparatorNode(ComparatorNode.LESS_THAN, propertyPath, value);
203 
204         assertThat(comparatorNode.toString()).isEqualTo("(example.property.path < 0)");
205     }
206 
207     @Test
testToString_lessEquals_returnsCorrectString()208     public void testToString_lessEquals_returnsCorrectString() {
209         List<PropertyPath.PathSegment> pathSegmentList =
210                 List.of(
211                         PropertyPath.PathSegment.create("example"),
212                         PropertyPath.PathSegment.create("property"),
213                         PropertyPath.PathSegment.create("path"));
214         PropertyPath propertyPath = new PropertyPath(pathSegmentList);
215         int value = 0;
216 
217         ComparatorNode comparatorNode =
218                 new ComparatorNode(ComparatorNode.LESS_EQUALS, propertyPath, value);
219 
220         assertThat(comparatorNode.toString()).isEqualTo("(example.property.path <= 0)");
221     }
222 
223     @Test
testToString_greaterThan_returnsCorrectString()224     public void testToString_greaterThan_returnsCorrectString() {
225         List<PropertyPath.PathSegment> pathSegmentList =
226                 List.of(
227                         PropertyPath.PathSegment.create("example"),
228                         PropertyPath.PathSegment.create("property"),
229                         PropertyPath.PathSegment.create("path"));
230         PropertyPath propertyPath = new PropertyPath(pathSegmentList);
231         int value = 0;
232 
233         ComparatorNode comparatorNode =
234                 new ComparatorNode(ComparatorNode.GREATER_THAN, propertyPath, value);
235 
236         assertThat(comparatorNode.toString()).isEqualTo("(example.property.path > 0)");
237     }
238 
239     @Test
testToString_greaterEquals_returnsCorrectString()240     public void testToString_greaterEquals_returnsCorrectString() {
241         List<PropertyPath.PathSegment> pathSegmentList =
242                 List.of(
243                         PropertyPath.PathSegment.create("example"),
244                         PropertyPath.PathSegment.create("property"),
245                         PropertyPath.PathSegment.create("path"));
246         PropertyPath propertyPath = new PropertyPath(pathSegmentList);
247         int value = 0;
248 
249         ComparatorNode comparatorNode =
250                 new ComparatorNode(ComparatorNode.GREATER_EQUALS, propertyPath, value);
251 
252         assertThat(comparatorNode.toString()).isEqualTo("(example.property.path >= 0)");
253     }
254 }
255