|
1 | 1 | package io.teaql.core; |
2 | 2 |
|
3 | 3 | import org.junit.Test; |
| 4 | +import java.lang.reflect.Proxy; |
4 | 5 | import java.util.Set; |
5 | 6 |
|
6 | 7 | import static org.junit.Assert.*; |
@@ -409,6 +410,89 @@ public void testEntityMutationLedgerIntegrationExtras() { |
409 | 410 | assertEquals("test_comment", e.getComment()); |
410 | 411 | } |
411 | 412 |
|
| 413 | + @Test |
| 414 | + public void testAddRelationUsesInvokingContextMetadata() { |
| 415 | + io.teaql.core.meta.EntityDescriptor single = new io.teaql.core.meta.EntityDescriptor(); |
| 416 | + single.setType("TestEntity"); |
| 417 | + io.teaql.core.meta.PropertyDescriptor singleProperty = |
| 418 | + new io.teaql.core.meta.PropertyDescriptor(); |
| 419 | + singleProperty.setName("entityRel"); |
| 420 | + singleProperty.setType(new io.teaql.core.meta.SimplePropertyType(Entity.class)); |
| 421 | + io.teaql.core.meta.PropertyDescriptor scalarProperty = |
| 422 | + new io.teaql.core.meta.PropertyDescriptor(); |
| 423 | + scalarProperty.setName("label"); |
| 424 | + scalarProperty.setType(new io.teaql.core.meta.SimplePropertyType(String.class)); |
| 425 | + single.setProperties(java.util.List.of(singleProperty, scalarProperty)); |
| 426 | + io.teaql.core.meta.SimpleEntityMetaFactory singleMetadata = |
| 427 | + new io.teaql.core.meta.SimpleEntityMetaFactory(); |
| 428 | + singleMetadata.register(single); |
| 429 | + |
| 430 | + io.teaql.core.meta.EntityDescriptor many = new io.teaql.core.meta.EntityDescriptor(); |
| 431 | + many.setType("TestEntity"); |
| 432 | + io.teaql.core.meta.PropertyDescriptor manyProperty = |
| 433 | + new io.teaql.core.meta.PropertyDescriptor(); |
| 434 | + manyProperty.setName("listRel"); |
| 435 | + manyProperty.setType(new io.teaql.core.meta.SimplePropertyType(SmartList.class)); |
| 436 | + many.setProperties(java.util.List.of(manyProperty)); |
| 437 | + io.teaql.core.meta.SimpleEntityMetaFactory manyMetadata = |
| 438 | + new io.teaql.core.meta.SimpleEntityMetaFactory(); |
| 439 | + manyMetadata.register(many); |
| 440 | + |
| 441 | + UserContext singleContext = contextWithMetadata(singleMetadata); |
| 442 | + UserContext manyContext = contextWithMetadata(manyMetadata); |
| 443 | + io.teaql.core.meta.EntityMetaFactory previous = |
| 444 | + io.teaql.core.meta.EntityMetaFactory.get(); |
| 445 | + try { |
| 446 | + io.teaql.core.meta.EntityMetaFactory.registerGlobal(singleMetadata); |
| 447 | + TestEntity manyEntity = new TestEntity(); |
| 448 | + TestEntity manyChild = new TestEntity(); |
| 449 | + ((Entity) manyEntity).addRelation(manyContext, "listRel", manyChild); |
| 450 | + assertEquals(1, manyEntity.<SmartList<?>>getProperty("listRel").size()); |
| 451 | + assertSame(manyChild, manyEntity.<SmartList<?>>getProperty("listRel").get(0)); |
| 452 | + |
| 453 | + io.teaql.core.meta.EntityMetaFactory.registerGlobal(null); |
| 454 | + TestEntity singleEntity = new TestEntity(); |
| 455 | + TestEntity singleChild = new TestEntity(); |
| 456 | + ((Entity) singleEntity).addRelation(singleContext, "entityRel", singleChild); |
| 457 | + assertSame(singleChild, singleEntity.getProperty("entityRel")); |
| 458 | + |
| 459 | + TeaQLRuntimeException missingEntity = assertThrows(TeaQLRuntimeException.class, |
| 460 | + () -> new TestEntity().addRelation( |
| 461 | + contextWithMetadata(new io.teaql.core.meta.SimpleEntityMetaFactory()), |
| 462 | + "entityRel", singleChild)); |
| 463 | + assertTrue(missingEntity.getMessage().contains("TestEntity")); |
| 464 | + IllegalArgumentException missingRelation = assertThrows(IllegalArgumentException.class, |
| 465 | + () -> singleEntity.addRelation(singleContext, "missingRel", singleChild)); |
| 466 | + assertTrue(missingRelation.getMessage().contains("TestEntity.missingRel")); |
| 467 | + IllegalArgumentException scalarRelation = assertThrows(IllegalArgumentException.class, |
| 468 | + () -> singleEntity.addRelation(singleContext, "label", singleChild)); |
| 469 | + assertTrue(scalarRelation.getMessage().contains("not a relation")); |
| 470 | + |
| 471 | + try { |
| 472 | + singleEntity.addRelation("entityRel", singleChild); |
| 473 | + fail("Legacy no-context attachment must not silently pick another model"); |
| 474 | + } catch (IllegalStateException expected) { |
| 475 | + assertTrue(expected.getMessage().contains("addRelation(context")); |
| 476 | + } |
| 477 | + } finally { |
| 478 | + io.teaql.core.meta.EntityMetaFactory.registerGlobal(previous); |
| 479 | + } |
| 480 | + } |
| 481 | + |
| 482 | + private static UserContext contextWithMetadata( |
| 483 | + io.teaql.core.meta.EntityMetaFactory metadata) { |
| 484 | + return (UserContext) Proxy.newProxyInstance( |
| 485 | + UserContext.class.getClassLoader(), |
| 486 | + new Class<?>[] {UserContext.class}, |
| 487 | + (proxy, method, args) -> { |
| 488 | + if ("capability".equals(method.getName()) |
| 489 | + && args[0] == io.teaql.core.meta.EntityMetaFactory.class) { |
| 490 | + return metadata; |
| 491 | + } |
| 492 | + throw new UnsupportedOperationException(method.getName()); |
| 493 | + }); |
| 494 | + } |
| 495 | + |
412 | 496 | @Test |
413 | 497 | public void testAddRelationAndOthers() { |
414 | 498 | io.teaql.core.meta.EntityDescriptor desc = new io.teaql.core.meta.EntityDescriptor(); |
|
0 commit comments