From 63e1c6006836f53de5ffd280590796ae7729c724 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andreas=20M=C3=BCller?= Date: Fri, 29 Jul 2022 13:24:10 +0200 Subject: [PATCH] ref #10101 add saveDetached tests to PolytomousKeyNodeServiceTest (and replace save by merge in other tests) --- .../service/PolytomousKeyNodeServiceTest.java | 73 ++++++++++++++++++- 1 file changed, 69 insertions(+), 4 deletions(-) diff --git a/cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/PolytomousKeyNodeServiceTest.java b/cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/PolytomousKeyNodeServiceTest.java index e6d2734bb9..05e428c682 100644 --- a/cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/PolytomousKeyNodeServiceTest.java +++ b/cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/PolytomousKeyNodeServiceTest.java @@ -93,7 +93,7 @@ public class PolytomousKeyNodeServiceTest extends CdmTransactionalIntegrationTes @Test //8127 //5536 //10101 @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDBDataSet.xml") - public final void testMerge(){ + public final void testMergeDetached(){ //create key with 2 child nodes PolytomousKey key = PolytomousKey.NewTitledInstance("TestPolytomousKey"); @@ -101,7 +101,7 @@ public class PolytomousKeyNodeServiceTest extends CdmTransactionalIntegrationTes PolytomousKeyNode child2 = PolytomousKeyNode.NewInstance("Test statement child2"); key.getRoot().addChild(child1); key.getRoot().addChild(child2); - keyService.save(key); + keyService.merge(key); commitAndStartNewTransaction(); //load root node and make it detached @@ -162,7 +162,7 @@ public class PolytomousKeyNodeServiceTest extends CdmTransactionalIntegrationTes @Test @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDBDataSet.xml") - public void testMergeWithMove() { + public void testMergeDetachedWithMove() { //create key with 2 child nodes PolytomousKey key = PolytomousKey.NewTitledInstance("Move test PolytomousKey"); @@ -170,7 +170,7 @@ public class PolytomousKeyNodeServiceTest extends CdmTransactionalIntegrationTes PolytomousKeyNode child2 = PolytomousKeyNode.NewInstance("Test statement child2"); key.getRoot().addChild(child1); key.getRoot().addChild(child2); - keyService.save(key); + keyService.merge(key); commitAndStartNewTransaction(); //load root node and make it detached @@ -202,6 +202,71 @@ public class PolytomousKeyNodeServiceTest extends CdmTransactionalIntegrationTes Assert.assertEquals("Should be root + 2 children + 1 grandchild", 4, service.count(PolytomousKeyNode.class)); } + @Test //10101 same as testMerge but with saveOrUpdate + @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDBDataSet.xml") + public final void testSaveDetached(){ + + //create key with 2 child nodes + PolytomousKey key = PolytomousKey.NewTitledInstance("TestPolytomousKey"); + PolytomousKeyNode child1 = PolytomousKeyNode.NewInstance("Test statement child1"); + PolytomousKeyNode child2 = PolytomousKeyNode.NewInstance("Test statement child2"); + key.getRoot().addChild(child1); + key.getRoot().addChild(child2); + keyService.save(key); + commitAndStartNewTransaction(); + + //load root node and make it detached + PolytomousKeyNode rootNode = service.find(key.getRoot().getUuid()); + rootNode.getChildren().get(0); //initialize children + commitAndStartNewTransaction(); //detach + + //replace nodes and merge + PolytomousKeyNode childToRemove = rootNode.getChildren().get(0); + rootNode.removeChild(childToRemove); + PolytomousKeyNode child3 = PolytomousKeyNode.NewInstance("Test statement child3"); + rootNode.addChild(child3); + service.saveOrUpdate(rootNode); //TODO childToRemove + service.delete(childToRemove.getUuid(), false); + commitAndStartNewTransaction(tableNames); + + //test result + rootNode = service.find(key.getRoot().getUuid()); + rootNode.getChildren(); + Assert.assertEquals(2, rootNode.getChildren().size()); + Assert.assertEquals(child2.getUuid(), rootNode.getChildren().get(0).getUuid()); + Assert.assertEquals(child3.getUuid(), rootNode.getChildren().get(1).getUuid()); + Assert.assertEquals("Should be root + 2 children", 3, service.count(PolytomousKeyNode.class)); + commitAndStartNewTransaction(); + +// System.out.println("NEXT"); + //same with key + //load root node and make it detached + PolytomousKey keyLoaded = keyService.find(key.getUuid()); + rootNode = keyLoaded.getRoot(); + rootNode.getChildren().get(0); //initialize children + commitAndStartNewTransaction(); //detach + + //replace nodes and merge + childToRemove = rootNode.getChildren().get(0); + rootNode.removeChild(childToRemove); + PolytomousKeyNode child4 = PolytomousKeyNode.NewInstance("Test statement child4"); + rootNode.addChild(child4); + + @SuppressWarnings("unused") + UUID mergedKey = keyService.saveOrUpdate(keyLoaded); + service.delete(childToRemove.getUuid(), false); + + commitAndStartNewTransaction(tableNames); + + rootNode = service.find(key.getRoot().getUuid()); + rootNode.getChildren(); + Assert.assertEquals(2, rootNode.getChildren().size()); + Assert.assertEquals(child3.getUuid(), rootNode.getChildren().get(0).getUuid()); + Assert.assertEquals(child4.getUuid(), rootNode.getChildren().get(1).getUuid()); + Assert.assertEquals("Should be root + 2 children", 3, service.count(PolytomousKeyNode.class)); + } + + @Override public void createTestDataSet() throws FileNotFoundException {} } \ No newline at end of file -- 2.34.1