|
1 | 1 | package com.here.naksha.lib.handlers.util; |
2 | 2 |
|
3 | 3 | import com.here.naksha.lib.core.lambdas.F1; |
| 4 | +import java.util.stream.Stream; |
4 | 5 | import naksha.base.StringList; |
5 | 6 | import naksha.model.request.RequestQuery; |
6 | 7 | import naksha.model.request.query.*; |
7 | 8 | import org.junit.jupiter.api.Test; |
8 | 9 |
|
9 | 10 | import java.util.List; |
10 | 11 | import java.util.Set; |
| 12 | +import org.junit.jupiter.params.ParameterizedTest; |
| 13 | +import org.junit.jupiter.params.provider.MethodSource; |
11 | 14 |
|
12 | 15 | import static com.here.naksha.lib.handlers.util.PropertyOperationUtil.disablePQueriesInRequest; |
13 | 16 | import static org.junit.jupiter.api.Assertions.*; |
@@ -149,7 +152,7 @@ void shouldComposedPQueryInRequest() { |
149 | 152 | // Given: request with dummy query with POp: |
150 | 153 | // - query all speed limits of 60 AND "car allowed" signs |
151 | 154 | // - "car allowed signs": signs with type `car_allowed` or value that is NOT `can_not_allowed` |
152 | | - IPropertyQuery typeIsSpeedLimit = new PNot(new PQuery(new Property("sign", "type"), StringOp.EQUALS, "speed_limit")); |
| 155 | + PQuery typeIsSpeedLimit = new PQuery(new Property("sign", "type"), StringOp.EQUALS, "speed_limit"); |
153 | 156 | PQuery valueIs60 = new PQuery(new Property("sign", "value"), DoubleOp.EQ, 60.0); |
154 | 157 | PQuery typeIsCarAllowed = new PQuery(new Property("sign", "type"), StringOp.EQUALS, "car_allowed"); |
155 | 158 | PQuery valueIsCarNotAllowed = new PQuery(new Property("sign", "value"), StringOp.EQUALS, "car_not_allowed"); |
@@ -230,6 +233,206 @@ void shouldDisableSimpleNot() { |
230 | 233 | assertNull(query.getProperties()); |
231 | 234 | } |
232 | 235 |
|
| 236 | + @Test |
| 237 | + void shouldRemoveDisabledChildFromAndAndKeepOther() { |
| 238 | + // Given |
| 239 | + PQuery a = new PQuery(new Property("a"), StringOp.EQUALS, "1"); |
| 240 | + PQuery b = new PQuery(new Property("b"), StringOp.EQUALS, "2"); |
| 241 | + |
| 242 | + RequestQuery query = new RequestQuery(); |
| 243 | + query.setProperties(new PAnd(a, b)); |
| 244 | + |
| 245 | + // When |
| 246 | + Set<PQuery> disabled = disablePQueriesInRequest(query, pQueryMatchesPath("a")); |
| 247 | + |
| 248 | + // Then |
| 249 | + assertEquals(Set.of(a), disabled); |
| 250 | + assertEquals(b, query.getProperties()); |
| 251 | + } |
| 252 | + |
| 253 | + @Test |
| 254 | + void shouldRemoveDisabledChildFromOrAndKeepOther() { |
| 255 | + // Given |
| 256 | + PQuery a = new PQuery(new Property("a"), StringOp.EQUALS, "1"); |
| 257 | + PQuery b = new PQuery(new Property("b"), StringOp.EQUALS, "2"); |
| 258 | + |
| 259 | + RequestQuery query = new RequestQuery(); |
| 260 | + query.setProperties(new POr(a, b)); |
| 261 | + |
| 262 | + // When |
| 263 | + Set<PQuery> disabled = disablePQueriesInRequest(query, pQueryMatchesPath("a")); |
| 264 | + |
| 265 | + // Then |
| 266 | + assertEquals(Set.of(a), disabled); |
| 267 | + assertEquals(b, query.getProperties()); |
| 268 | + } |
| 269 | + |
| 270 | + @Test |
| 271 | + void shouldReduceAndToFalseWhenFalseRemains() { |
| 272 | + // Given |
| 273 | + PQuery a = new PQuery(new Property("a"), StringOp.EQUALS, "1"); |
| 274 | + |
| 275 | + RequestQuery query = new RequestQuery(); |
| 276 | + query.setProperties(new PAnd(a, PFalse.INSTANCE)); |
| 277 | + |
| 278 | + // When |
| 279 | + disablePQueriesInRequest(query, pQueryMatchesPath("a")); |
| 280 | + |
| 281 | + // Then |
| 282 | + assertEquals(PFalse.INSTANCE, query.getProperties()); |
| 283 | + } |
| 284 | + |
| 285 | + @Test |
| 286 | + void shouldReduceOrToTrueWhenTrueRemains() { |
| 287 | + // Given |
| 288 | + PQuery a = new PQuery(new Property("a"), StringOp.EQUALS, "1"); |
| 289 | + |
| 290 | + RequestQuery query = new RequestQuery(); |
| 291 | + query.setProperties(new POr(a, PTrue.INSTANCE)); |
| 292 | + |
| 293 | + // When |
| 294 | + disablePQueriesInRequest(query, pQueryMatchesPath("a")); |
| 295 | + |
| 296 | + // Then |
| 297 | + assertNull(query.getProperties()); |
| 298 | + } |
| 299 | + |
| 300 | + @Test |
| 301 | + void shouldRemoveNotWhenInnerQueryDisabled() { |
| 302 | + // Given |
| 303 | + PQuery inner = new PQuery(new Property("a"), StringOp.EQUALS, "1"); |
| 304 | + PNot root = new PNot(inner); |
| 305 | + |
| 306 | + RequestQuery query = new RequestQuery(); |
| 307 | + query.setProperties(root); |
| 308 | + |
| 309 | + // When |
| 310 | + Set<PQuery> disabled = disablePQueriesInRequest(query, pQueryMatchesPath("a")); |
| 311 | + |
| 312 | + // Then |
| 313 | + assertEquals(Set.of(inner), disabled); |
| 314 | + assertNull(query.getProperties()); |
| 315 | + } |
| 316 | + |
| 317 | + @Test |
| 318 | + void shouldHandleNotAndWithOneDisabledChild() { |
| 319 | + // Given |
| 320 | + PQuery a = new PQuery(new Property("a"), StringOp.EQUALS, "1"); |
| 321 | + PQuery b = new PQuery(new Property("b"), StringOp.EQUALS, "2"); |
| 322 | + |
| 323 | + RequestQuery query = new RequestQuery(); |
| 324 | + query.setProperties(new PNot(new PAnd(a, b))); |
| 325 | + |
| 326 | + // When |
| 327 | + Set<PQuery> disabled = disablePQueriesInRequest(query, pQueryMatchesPath("a")); |
| 328 | + |
| 329 | + // Then |
| 330 | + assertEquals(Set.of(a), disabled); |
| 331 | + IPropertyQuery result = query.getProperties(); |
| 332 | + assertInstanceOf(PNot.class, result); |
| 333 | + assertEquals(b, ((PNot) result).getQuery()); |
| 334 | + } |
| 335 | + |
| 336 | + @Test |
| 337 | + void shouldCollapseNestedAndOrCorrectly() { |
| 338 | + // Given |
| 339 | + PQuery a = new PQuery(new Property("a"), StringOp.EQUALS, "1"); |
| 340 | + PQuery b = new PQuery(new Property("b"), StringOp.EQUALS, "2"); |
| 341 | + PQuery c = new PQuery(new Property("c"), StringOp.EQUALS, "3"); |
| 342 | + |
| 343 | + RequestQuery query = new RequestQuery(); |
| 344 | + query.setProperties(new PAnd( |
| 345 | + new POr(a, b), |
| 346 | + c |
| 347 | + )); |
| 348 | + |
| 349 | + // When |
| 350 | + Set<PQuery> disabled = disablePQueriesInRequest(query, pQueryMatchesPath("a")); |
| 351 | + |
| 352 | + // Then |
| 353 | + assertEquals(Set.of(a), disabled); |
| 354 | + |
| 355 | + IPropertyQuery result = query.getProperties(); |
| 356 | + assertInstanceOf(PAnd.class, result); |
| 357 | + PAnd and = (PAnd) result; |
| 358 | + |
| 359 | + assertEquals(2, and.size()); |
| 360 | + assertInstanceOf(PQuery.class, and.get(1)); |
| 361 | + } |
| 362 | + |
| 363 | + @Test |
| 364 | + void shouldNotDuplicateDisabledQueries() { |
| 365 | + // Given |
| 366 | + PQuery a = new PQuery(new Property("a"), StringOp.EQUALS, "1"); |
| 367 | + |
| 368 | + RequestQuery query = new RequestQuery(); |
| 369 | + query.setProperties(new PAnd(a, a)); |
| 370 | + |
| 371 | + // When |
| 372 | + Set<PQuery> disabled = disablePQueriesInRequest(query, pQueryMatchesPath("a")); |
| 373 | + |
| 374 | + // Then |
| 375 | + assertEquals(1, disabled.size()); |
| 376 | + assertTrue(disabled.contains(a)); |
| 377 | + } |
| 378 | + |
| 379 | + @ParameterizedTest |
| 380 | + @MethodSource("andTruthTable") |
| 381 | + void truthTable_and(IPropertyQuery left, IPropertyQuery right, IPropertyQuery expected) { |
| 382 | + RequestQuery query = new RequestQuery(); |
| 383 | + query.setProperties(new PAnd(left, right)); |
| 384 | + |
| 385 | + disablePQueriesInRequest(query, pq -> true); |
| 386 | + |
| 387 | + assertEquals(expected, query.getProperties()); |
| 388 | + } |
| 389 | + |
| 390 | + @ParameterizedTest |
| 391 | + @MethodSource("orTruthTable") |
| 392 | + void truthTable_or(IPropertyQuery left, IPropertyQuery right, IPropertyQuery expected) { |
| 393 | + RequestQuery query = new RequestQuery(); |
| 394 | + query.setProperties(new POr(left, right)); |
| 395 | + |
| 396 | + disablePQueriesInRequest(query, pq -> true); |
| 397 | + |
| 398 | + assertEquals(expected, query.getProperties()); |
| 399 | + } |
| 400 | + |
| 401 | + @ParameterizedTest |
| 402 | + @MethodSource("notTruthTable") |
| 403 | + void truthTable_not(IPropertyQuery inner, IPropertyQuery expected) { |
| 404 | + RequestQuery query = new RequestQuery(); |
| 405 | + query.setProperties(new PNot(inner)); |
| 406 | + |
| 407 | + disablePQueriesInRequest(query, pq -> true); |
| 408 | + |
| 409 | + assertEquals(expected, query.getProperties()); |
| 410 | + } |
| 411 | + |
| 412 | + private static Stream<Object[]> notTruthTable() { |
| 413 | + return Stream.of( |
| 414 | + new Object[]{PTrue.INSTANCE, PFalse.INSTANCE}, |
| 415 | + new Object[]{PFalse.INSTANCE, null} |
| 416 | + ); |
| 417 | + } |
| 418 | + |
| 419 | + private static Stream<Object[]> andTruthTable() { |
| 420 | + return Stream.of( |
| 421 | + new Object[]{PTrue.INSTANCE, PTrue.INSTANCE, null}, |
| 422 | + new Object[]{PTrue.INSTANCE, PFalse.INSTANCE, PFalse.INSTANCE}, |
| 423 | + new Object[]{PFalse.INSTANCE, PTrue.INSTANCE, PFalse.INSTANCE}, |
| 424 | + new Object[]{PFalse.INSTANCE, PFalse.INSTANCE, PFalse.INSTANCE} |
| 425 | + ); |
| 426 | + } |
| 427 | + |
| 428 | + private static Stream<Object[]> orTruthTable() { |
| 429 | + return Stream.of( |
| 430 | + new Object[]{PTrue.INSTANCE, PTrue.INSTANCE, null}, |
| 431 | + new Object[]{PTrue.INSTANCE, PFalse.INSTANCE, null}, |
| 432 | + new Object[]{PFalse.INSTANCE, PTrue.INSTANCE, null}, |
| 433 | + new Object[]{PFalse.INSTANCE, PFalse.INSTANCE, PFalse.INSTANCE} |
| 434 | + ); |
| 435 | + } |
233 | 436 | private F1<Boolean, PQuery> pQueryMatchesPath(String... expectedPath) { |
234 | 437 | return pQuery -> { |
235 | 438 | StringList queryPath = pQuery.getProperty().getPath(); |
|
0 commit comments