fix git commit
authorPatrick Plitzner <p.plitzner@bgbm.org>
Tue, 24 May 2016 13:32:18 +0000 (15:32 +0200)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Tue, 24 May 2016 13:32:18 +0000 (15:32 +0200)
eu.etaxonomy.taxeditor.test/src/test/java/eu/etaxonomy/taxeditor/session/CdmEntitySessionAwareTest.java

index 8b98b6fe317919150759b7595daf93ea9aad9f3a..2cae0cd09b2fdebe943d88ecf1f32944df3a6e3c 100644 (file)
-// $Id$\r
-/**\r
- * Copyright (C) 2014 EDIT\r
- * European Distributed Institute of Taxonomy\r
- * http://www.e-taxonomy.eu\r
- *\r
- * The contents of this file are subject to the Mozilla Public License Version 1.1\r
- * See LICENSE.TXT at the top of this package for the full license terms.\r
- */\r
-package eu.etaxonomy.taxeditor.session;\r
-\r
-import java.util.ArrayList;\r
-import java.util.HashSet;\r
-import java.util.Iterator;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.Set;\r
-import java.util.UUID;\r
-\r
-import org.apache.log4j.Logger;\r
-import org.junit.Assert;\r
-import org.junit.BeforeClass;\r
-import org.junit.Ignore;\r
-import org.junit.Test;\r
-import org.unitils.dbunit.annotation.DataSet;\r
-\r
-import eu.etaxonomy.cdm.api.application.CdmApplicationState;\r
-import eu.etaxonomy.cdm.api.service.DeleteResult;\r
-import eu.etaxonomy.cdm.api.service.ICommonService;\r
-import eu.etaxonomy.cdm.api.service.IPolytomousKeyNodeService;\r
-import eu.etaxonomy.cdm.api.service.IPolytomousKeyService;\r
-import eu.etaxonomy.cdm.api.service.ITaxonService;\r
-import eu.etaxonomy.cdm.api.service.ITermService;\r
-import eu.etaxonomy.cdm.api.service.IUserService;\r
-import eu.etaxonomy.cdm.api.service.IVocabularyService;\r
-import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;\r
-import eu.etaxonomy.cdm.model.agent.AgentBase;\r
-import eu.etaxonomy.cdm.model.agent.Person;\r
-import eu.etaxonomy.cdm.model.common.CdmBase;\r
-import eu.etaxonomy.cdm.model.common.Credit;\r
-import eu.etaxonomy.cdm.model.common.DefinedTermBase;\r
-import eu.etaxonomy.cdm.model.common.Extension;\r
-import eu.etaxonomy.cdm.model.common.IdentifiableSource;\r
-import eu.etaxonomy.cdm.model.common.Language;\r
-import eu.etaxonomy.cdm.model.common.LanguageString;\r
-import eu.etaxonomy.cdm.model.common.TermType;\r
-import eu.etaxonomy.cdm.model.common.TermVocabulary;\r
-import eu.etaxonomy.cdm.model.description.KeyStatement;\r
-import eu.etaxonomy.cdm.model.description.PolytomousKey;\r
-import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;\r
-import eu.etaxonomy.cdm.model.media.Rights;\r
-import eu.etaxonomy.cdm.model.taxon.Taxon;\r
-import eu.etaxonomy.taxeditor.httpinvoker.RemotingSessionAwareTest;\r
-import eu.etaxonomy.taxeditor.httpinvoker.TestThread;\r
-\r
-\r
-/**\r
- * @author cmathew\r
- * @date 7 Oct 2014\r
- *\r
- */\r
-@DataSet\r
-public class CdmEntitySessionAwareTest extends RemotingSessionAwareTest {\r
-\r
-    private static final Logger logger = Logger.getLogger(CdmEntitySessionAwareTest.class);\r
-\r
-    UUID polytomousKeyUuid = UUID.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66");\r
-    UUID subKeyUuid = UUID.fromString("9d8bf4f6-a70a-4b80-8556-2ccfb436ff01");\r
-    //UUID polytomousKeyUuid = UUID.fromString("bab66772-2c83-428a-bb6d-655d12ac6097");\r
-    UUID taxon1Uuid = UUID.fromString("2b336df7-29e8-4f79-985f-66502739d22f");\r
-    UUID personUuid = UUID.fromString("945d08f2-eb92-45b6-9252-6275ea6d338b");\r
-\r
-\r
-\r
-    IPolytomousKeyService polytomousKeyService = getRemoteApplicationController().getPolytomousKeyService();\r
-    IPolytomousKeyNodeService polytomousKeyNodeService = getRemoteApplicationController().getPolytomousKeyNodeService();\r
-    ICommonService commonService = getRemoteApplicationController().getCommonService();\r
-    ITaxonService taxonService = getRemoteApplicationController().getTaxonService();\r
-    IVocabularyService vocabularyService = getRemoteApplicationController().getVocabularyService();\r
-    ITermService termService = getRemoteApplicationController().getTermService();\r
-    IUserService userService = getRemoteApplicationController().getUserService();\r
-\r
-    //Language english = Language.getLanguageFromUuid(Language.uuidEnglish);\r
-\r
-\r
-\r
-    @BeforeClass\r
-    public static void initializePolytomousKeyTest() {\r
-    }\r
-\r
-\r
-    @Test\r
-    @Ignore\r
-    public void readAllPolytomousKeys() {\r
-        List<PolytomousKey> pKeys = polytomousKeyService.list(PolytomousKey.class, null, null, null, null);\r
-        Iterator<PolytomousKey> pKeysItr = pKeys.iterator();\r
-        Assert.assertEquals(pKeysItr.next().getUuid(),UUID.fromString("9d8bf4f6-a70a-4b80-8556-2ccfb436ff01"));\r
-        Assert.assertEquals(pKeysItr.next().getUuid(),UUID.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66"));\r
-    }\r
-\r
-\r
-    @Test\r
-    //@DataSet("PolytomousKeyTest.readPolytmousKeyData.xml")\r
-    public void readPolytmousKeyData() {\r
-        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);\r
-        Set<Taxon> taxonomicScope = pKey.getTaxonomicScope();\r
-        Iterator<Taxon> tsItr = taxonomicScope.iterator();\r
-        Taxon taxon = tsItr.next();\r
-\r
-        Assert.assertEquals("Achillea sec. Cyprus", taxon.getTitleCache());\r
-        Assert.assertEquals(tsItr.hasNext(), false);\r
-\r
-        List<Credit> credits = pKey.getCredits();\r
-        AgentBase agent = credits.get(0).getAgent();\r
-        Assert.assertEquals(agent.getId(),4809);\r
-        Assert.assertEquals(agent.getTitleCache(),"R. A. Graham");\r
-        Assert.assertEquals(credits.get(0).getText(),"Credits Text Test");\r
-\r
-        Set<Extension> exts = pKey.getExtensions();\r
-        Iterator<Extension> extItr = exts.iterator();\r
-        Extension ext = extItr.next();\r
-        Assert.assertEquals(ext.getValue(), "http://test.com");\r
-\r
-        Set<Rights> rights = pKey.getRights();\r
-        Iterator<Rights> rightsItr = rights.iterator();\r
-        Rights right = rightsItr.next();\r
-        Assert.assertEquals(right.getText(),"Rights Text Test");\r
-\r
-        Set<IdentifiableSource> sources = pKey.getSources();\r
-        Iterator<IdentifiableSource> sourcesItr = sources.iterator();\r
-        IdentifiableSource source = sourcesItr.next();\r
-        Assert.assertEquals(source.getId(), 23710);\r
-        source = sourcesItr.next();\r
-        Assert.assertEquals(source.getId(), 23711);\r
-\r
-        // TO DO : Added tests for Annotations , Markers\r
-    }\r
-\r
-\r
-\r
-    @Test\r
-    public void readPolytomousKeyDataFromNodes() {\r
-        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);\r
-        PolytomousKeyNode rootNode = pKey.getRoot();\r
-\r
-\r
-        Assert.assertEquals(rootNode.getId(), 2750);\r
-\r
-        Assert.assertEquals(rootNode.getChildAt(0).getId(), 2751);\r
-        Assert.assertEquals(rootNode.getChildAt(0).getParent().getId(), rootNode.getId());\r
-        Assert.assertEquals(rootNode.getKey().getId(), pKey.getId());\r
-        Integer sortIndex = (Integer)getFieldValueViaReflection(rootNode.getChildAt(0), "sortIndex");\r
-        Assert.assertEquals(sortIndex, new Integer(0));\r
-        String statement = "Capitula without ligulate ray-florets; leaves entire or subentire";\r
-        Assert.assertEquals(rootNode.getChildAt(0).getStatement().getLabelText(Language.ENGLISH()), statement);\r
-\r
-        Assert.assertEquals(rootNode.getChildAt(1).getId(), 2753);\r
-        Assert.assertEquals(rootNode.getChildAt(1).getParent().getId(), rootNode.getId());\r
-        Assert.assertEquals(rootNode.getChildAt(1).getKey().getId(), pKey.getId());\r
-        sortIndex = (Integer)getFieldValueViaReflection(rootNode.getChildAt(1), "sortIndex");\r
-        Assert.assertEquals(sortIndex, new Integer(1));\r
-        statement = "Capitula with ligulate ray-florets; leaves pinnatisect";\r
-        Assert.assertEquals(rootNode.getChildAt(1).getStatement().getLabelText(Language.ENGLISH()), statement);\r
-\r
-        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getId(), 2754);\r
-        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getParent().getId(), rootNode.getChildAt(1).getId());\r
-        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getKey().getId(), pKey.getId());\r
-        sortIndex = (Integer)getFieldValueViaReflection(rootNode.getChildAt(1).getChildAt(0), "sortIndex");\r
-        Assert.assertEquals(sortIndex, new Integer(0));\r
-        statement = "Ray-florets yellow";\r
-        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getStatement().getLabelText(Language.ENGLISH()), statement);\r
-        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getChildAt(0).getTaxon().getTitleCache(), "Achillea arabica Kotschy sec. Cyprus");\r
-    }\r
-\r
-    @Test\r
-    public void addGrandChildPolytomousKeyNode() {\r
-\r
-        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);\r
-        PolytomousKeyNode rootChildNode = pKey.getRoot().getChildAt(0);\r
-        PolytomousKeyNode grandChildNode = PolytomousKeyNode.NewInstance();\r
-        rootChildNode.addChild(grandChildNode);\r
-\r
-        polytomousKeyService.merge(pKey, true);\r
-        grandChildNode = pKey.getRoot().getChildAt(0).getChildAt(0);\r
-        Assert.assertTrue(0 != grandChildNode.getId());\r
-        KeyStatement ks = KeyStatement.NewInstance("test");\r
-        grandChildNode.setStatement(ks);\r
-        polytomousKeyService.merge(pKey, true);\r
-\r
-        //grandChildNode = pKey.getRoot().getChildAt(0).getChildAt(0);\r
-        //KeyStatement ks = grandChildNode.getStatement();\r
-        Assert.assertTrue(0 != ks.getId());\r
-\r
-    }\r
-\r
-    @Test\r
-    public void addGreatGrandChildPolytomousKeyNode() {\r
-\r
-        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);\r
-        PolytomousKeyNode rootChildNode = pKey.getRoot().getChildAt(0);\r
-        PolytomousKeyNode grandChildNode = PolytomousKeyNode.NewInstance();\r
-        rootChildNode.addChild(grandChildNode);\r
-\r
-\r
-        polytomousKeyNodeService.merge(grandChildNode);\r
-\r
-\r
-        Assert.assertFalse(pKey.getRoot().getChildAt(0).getChildAt(0).getId() == 0);\r
-    }\r
-\r
-    @Test\r
-    public void savePolytomousKeyNodeData() {\r
-        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);\r
-\r
-        PolytomousKeyNode pkeynode = pkey.getRoot();\r
-        String newQuestion = "New Question";\r
-        String newStatement = "New Statement";\r
-\r
-        Assert.assertEquals("Question 1",pkeynode.getQuestion().getLabel().get(Language.ENGLISH()).getText());\r
-        for(PolytomousKeyNode node : pkeynode.getChildren()) {\r
-\r
-            node.setQuestion(null);\r
-            node.setStatement(null);\r
-        }\r
-        //FIXME:Remoting Add tests for feature after fixing problem\r
-\r
-        //Feature feature = pkeynode.getFeature();\r
-        //Assert.assertEquals(feature.getTitleCache(),"Systematics");\r
-        //pkeynode.setFeature(null);\r
-\r
-        Assert.assertEquals(pkeynode.getChildAt(0).getModifyingText().get(Language.ENGLISH()).getText(),"Modifying Text 1a");\r
-        String modifyingText = "Modifying Text 1a updated";\r
-\r
-        //pkeynode.getChildAt(0).putModifyingText(Language.ENGLISH(), modifyingText);\r
-\r
-        Assert.assertEquals(pkeynode.getChildAt(0).getSubkey().getId(),751);\r
-        Assert.assertEquals("Asphodeline", pkeynode.getChildAt(0).getSubkey().getTitleCache());\r
-        Assert.assertNull(pkeynode.getChildAt(1).getTaxon());\r
-        Taxon taxon = CdmBase.deproxy(taxonService.find(taxon1Uuid),Taxon.class);\r
-        pkeynode.getChildAt(1).setTaxon(taxon);\r
-\r
-        polytomousKeyService.merge(pkey);\r
-\r
-        pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);\r
-\r
-\r
-        pkeynode = pkey.getRoot();\r
-        for(PolytomousKeyNode node : pkeynode.getChildren()) {\r
-            Assert.assertNull(node.getQuestion());\r
-            node.setQuestion(KeyStatement.NewInstance(Language.ENGLISH(),newQuestion));\r
-            Assert.assertNull(node.getStatement());\r
-            node.setStatement(KeyStatement.NewInstance(Language.ENGLISH(),newStatement));\r
-        }\r
-\r
-        Assert.assertEquals(pkeynode.getChildAt(1).getTaxon(), taxon);\r
-\r
-        polytomousKeyService.merge(pkey);\r
-\r
-        pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);\r
-\r
-        pkeynode = pkey.getRoot();\r
-        for(PolytomousKeyNode node : pkeynode.getChildren()) {\r
-            Assert.assertNotNull(node.getQuestion());\r
-            Map<Language, LanguageString> label = node.getQuestion().getLabel();\r
-            Assert.assertEquals(newQuestion, label.get(Language.ENGLISH()).getText());\r
-            Assert.assertNotNull(node.getStatement());\r
-            Assert.assertEquals(newStatement, node.getStatement().getLabel(Language.ENGLISH()).getText());\r
-        }\r
-        //Assert.assertEquals(pkeynode.getFeature().getId(), feature.getId());\r
-\r
-    }\r
-\r
-\r
-    @Test\r
-    public void savePolytomousKeyNodeDataWithSameSubKey() {\r
-\r
-        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);\r
-\r
-\r
-        PolytomousKeyNode pkeynode = pkey.getRoot();\r
-\r
-        PolytomousKey subkey1 = CdmBase.deproxy(pkeynode.getChildAt(0).getSubkey(), PolytomousKey.class);\r
-        String subkey1title = subkey1.getTitleCache();\r
-        subkey1.setTitleCache(subkey1title + "test", true);\r
-\r
-\r
-        PolytomousKey subkey2 = CdmBase.deproxy(pkeynode.getChildAt(1).getChildAt(0).getSubkey(), PolytomousKey.class);\r
-        String subkey2title = subkey2.getTitleCache();\r
-        subkey2.setTitleCache(subkey2title + "test", true);\r
-\r
-        Assert.assertSame(subkey1, subkey2);\r
-\r
-        polytomousKeyService.merge(pkey);\r
-    }\r
-\r
-    @Test\r
-    public void savePolytomousKeyNodeDataWithSameSubKeyUsingService() {\r
-\r
-        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);\r
-\r
-\r
-        PolytomousKeyNode pkeynode = pkey.getRoot();\r
-//because of the check for null values in getChildren it isn't a persistent list anymore\r
-  //     PersistentCollection children = (PersistentCollection) pkeynode.getChildren();\r
-        PolytomousKeyNode childNode0 = (PolytomousKeyNode)commonService.get(pkeynode.getUuid(), "children", 0);\r
-        PolytomousKey subkey1 = CdmBase.deproxy(childNode0.getSubkey(),PolytomousKey.class);\r
-        String subkey1title = subkey1.getTitleCache();\r
-        subkey1.setTitleCache(subkey1title + "test", true);\r
-\r
-        PolytomousKeyNode childNode1 = (PolytomousKeyNode)commonService.get(pkeynode.getUuid(), "children", 1);\r
-        PolytomousKey subkey2 = CdmBase.deproxy(childNode1.getSubkey(),PolytomousKey.class);\r
-        String subkey2title = subkey2.getTitleCache();\r
-        subkey2.setTitleCache(subkey2title + "test", true);\r
-\r
-        Assert.assertNotSame(childNode0, childNode1);\r
-\r
-        Assert.assertSame(subkey1, subkey2);\r
-\r
-        polytomousKeyService.merge(pkey);\r
-    }\r
-\r
-\r
-\r
-    @Test\r
-    public void savePolytomousKeyNodeDataWithSameLanguageInLabel() {\r
-\r
-        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);\r
-        PolytomousKeyNode pkeynode = pkey.getRoot();\r
-\r
-        Map<Language, LanguageString> label1 = pkeynode.getQuestion().getLabel();\r
-        label1.size();\r
-\r
-\r
-        Map<Language, LanguageString> label2 = pkeynode.getChildAt(0).getStatement().getLabel();\r
-        label2.size();\r
-\r
-\r
-        polytomousKeyService.merge(pkey);\r
-    }\r
-\r
-    @Test\r
-    public void deleteSubKeyInPolytomousSubKeyWithoutInitializing() {\r
-        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);\r
-\r
-\r
-        PolytomousKeyNode rootNode = pKey.getRoot();\r
-\r
-        PolytomousKeyNode childNode = rootNode.getChildAt(0);\r
-        PolytomousKey subKey = HibernateProxyHelper.deproxy( childNode.getSubkey(), PolytomousKey.class);;\r
-\r
-\r
-        DeleteResult result = polytomousKeyService.delete(subKey.getUuid());\r
-        //A used subKey can't be deleted\r
-        if (result.isOk()){\r
-            Assert.fail();\r
-        }\r
-\r
-\r
-        Set<Integer> ids = new HashSet<Integer>();\r
-        ids.add(2753);\r
-        ids.add(2754);\r
-        ids.add(2751);\r
-        List<PolytomousKeyNode> nodes = polytomousKeyNodeService.findById(ids);\r
-        for (PolytomousKeyNode child:nodes){\r
-            child.setSubkey(null);\r
-           // polytomousKeyNodeService.merge(child);\r
-        }\r
-        polytomousKeyNodeService.merge(nodes, true);\r
-        result = polytomousKeyService.delete(subKey.getUuid());\r
-        if (!result.isOk()){\r
-            Assert.fail();\r
-        }\r
-\r
-\r
-\r
-\r
-        // retrieving subkey shows its null\r
-        subKey = CdmBase.deproxy(polytomousKeyService.find(subKeyUuid),PolytomousKey.class);\r
-        Assert.assertNull(subKey);\r
-\r
-        pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);\r
-\r
-\r
-    }\r
-\r
-\r
-    @Test\r
-    public void deleteSubKeyInPolytomousNode() {\r
-        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);\r
-\r
-\r
-        PolytomousKeyNode rootNode = pKey.getRoot();\r
-        List<PolytomousKeyNode> children = rootNode.getChildren();\r
-        PolytomousKeyNode child = rootNode.getChildAt(0);\r
-        polytomousKeyNodeService.delete(child.getUuid(), true);\r
-\r
-        pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);\r
-\r
-        rootNode = pKey.getRoot();\r
-        children = rootNode.getChildren();\r
-        Assert.assertFalse(children.contains(child));\r
-    }\r
-\r
-    @Test\r
-    public void deleteSubKeyInPolytomousSubKeyAfterInitializing() {\r
-        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);\r
-\r
-\r
-        PolytomousKeyNode rootNode = pKey.getRoot();\r
-        PolytomousKeyNode child = rootNode.getChildAt(0);\r
-        PolytomousKey subKey = child.getSubkey();\r
-        // this call initializes the subkey\r
-        subKey.getRoot();\r
-\r
-        polytomousKeyService.delete(subKey);\r
-\r
-        // since subKey was initialized before the delete, it will be\r
-        // accesible even after the delete. This behaviour is similar\r
-        // to hibernate session delete where the deleted object is\r
-        // made transient, but not deleted from the object graph\r
-\r
-        PolytomousKeyNode subKeyRootNode = subKey.getRoot();\r
-        Assert.assertNotNull(subKey);\r
-    }\r
-\r
-    @Test\r
-    public void saveNewTermVocabulary() {\r
-        TermVocabulary termVocabulary =\r
-                TermVocabulary.NewInstance(TermType.Feature,\r
-                        null,\r
-                        "Untitled",\r
-                        null,\r
-                        null);\r
-        try {\r
-            List<TermVocabulary<DefinedTermBase>> vocabularies = vocabularyService.findByTermType(TermType.Feature);\r
-\r
-            for(TermVocabulary vocab : vocabularies) {\r
-                vocab.getTermsOrderedByLabels(Language.ENGLISH());\r
-            }\r
-\r
-            termVocabulary = vocabularyService.merge(termVocabulary);\r
-            vocabularies.add(termVocabulary);\r
-            termVocabulary.setLabel("Test");\r
-            vocabularyService.merge(new ArrayList<TermVocabulary>(vocabularies));\r
-        } finally {\r
-            vocabularyService.delete(termVocabulary);\r
-        }\r
-    }\r
-\r
-    @Test\r
-    public void saveNewTerm() {\r
-        UUID vocNameFeatureUuid = UUID.fromString("fa7ca3eef-4092-49e1-beec-ed5096193e5e");\r
-        UUID vocFeatureUuid = UUID.fromString("b187d555-f06f-4d65-9e53-da7c93f8eaa8");\r
-        DefinedTermBase newTerm = TermType.Feature.getEmptyDefinedTermBase();\r
-        newTerm.setLabel("CreateTest");\r
-\r
-        try {\r
-\r
-            Assert.assertNotNull(newTerm);\r
-            TermVocabulary vocNameFeature = vocabularyService.find(vocNameFeatureUuid);\r
-            TermVocabulary vocFeature = vocabularyService.find(vocFeatureUuid);\r
-\r
-            List<TermVocabulary> vocs = new ArrayList<TermVocabulary>();\r
-\r
-            vocs.add(vocNameFeature);\r
-            vocs.add(vocFeature);\r
-\r
-            vocNameFeature.addTerm(newTerm);\r
-\r
-            vocs = vocabularyService.merge(vocs);\r
-            for(TermVocabulary voc : vocs) {\r
-                if(voc.getUuid().equals(vocNameFeatureUuid)) {\r
-                    vocNameFeature = voc;\r
-                }\r
-            }\r
-\r
-            Assert.assertTrue(vocNameFeature.getTerms().contains(newTerm));\r
-\r
-            for(Object obj : vocNameFeature.getTerms()) {\r
-                DefinedTermBase dtb = (DefinedTermBase)obj;\r
-                if("CreateTest".equals(dtb.getLabel())) {\r
-                    newTerm = dtb;\r
-                    Assert.assertNotNull(dtb.getCreatedBy());\r
-                    Assert.assertNotNull(dtb.getCreated());\r
-                } else {\r
-                    Assert.assertNull(dtb.getCreatedBy());\r
-                }\r
-            }\r
-            newTerm.setLabel("UpdateTest");\r
-            newTerm = termService.merge(newTerm);\r
-            Assert.assertNotNull(newTerm.getUpdatedBy());\r
-            Assert.assertNotNull(newTerm.getUpdated());\r
-\r
-            Assert.assertNull(vocNameFeature.getCreatedBy());\r
-            Assert.assertNull(vocFeature.getCreatedBy());\r
-        } finally {\r
-            if(termService.find(newTerm.getUuid()) != null) {\r
-                termService.delete(newTerm.getUuid());\r
-            }\r
-        }\r
-    }\r
-\r
-\r
-    @Test\r
-    public void updatePerson() {\r
-        // Test for #5138\r
-        Person person = (Person) CdmApplicationState.getCurrentAppConfig().getAgentService().load(personUuid);\r
-        person.setFirstname("Me");\r
-        CdmApplicationState.getCurrentAppConfig().getAgentService().merge(person);\r
-    }\r
-\r
-\r
-    @Test\r
-    public void createPerson() {\r
-        // Test for #5138\r
-        Person person = Person.NewInstance();\r
-        person = (Person) CdmApplicationState.getCurrentAppConfig().getAgentService().merge(person);\r
-        person = (Person) CdmApplicationState.getCurrentAppConfig().getAgentService().load(person.getUuid());\r
-        person.setFirstname("Some");\r
-        CdmApplicationState.getCurrentAppConfig().getAgentService().merge(person);\r
-    }\r
-\r
-    @Test\r
-    public void testThreadLocalActiveSession() throws InterruptedException {\r
-        MockSessionOwner<CdmBase> so1 = new MockSessionOwner<CdmBase>();\r
-        final ICdmEntitySession activeSession = getCdmEntitySessionManager().newSession(so1, true);\r
-        TestThread thread = new TestThread(true) {\r
-            @Override\r
-            public void doRun() throws InterruptedException {\r
-                ICdmEntitySession threadLocalActiveSession = getCdmEntitySessionManager().getActiveSession();\r
-                Assert.assertEquals(threadLocalActiveSession, activeSession);\r
-            }\r
-        };\r
-        invokeThread(thread);\r
-        MockSessionOwner<CdmBase> so2 = new MockSessionOwner<CdmBase>();\r
-        ICdmEntitySession newActiveSession = getCdmEntitySessionManager().newSession(so2, true);\r
-        Assert.assertFalse(activeSession.equals(newActiveSession));\r
-        thread.unblock();\r
-\r
-    }\r
-}\r
+// $Id$
+/**
+ * Copyright (C) 2014 EDIT
+ * European Distributed Institute of Taxonomy
+ * http://www.e-taxonomy.eu
+ *
+ * The contents of this file are subject to the Mozilla Public License Version 1.1
+ * See LICENSE.TXT at the top of this package for the full license terms.
+ */
+package eu.etaxonomy.taxeditor.session;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+
+import org.apache.log4j.Logger;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.unitils.dbunit.annotation.DataSet;
+
+import eu.etaxonomy.cdm.api.application.CdmApplicationState;
+import eu.etaxonomy.cdm.api.service.DeleteResult;
+import eu.etaxonomy.cdm.api.service.ICommonService;
+import eu.etaxonomy.cdm.api.service.IPolytomousKeyNodeService;
+import eu.etaxonomy.cdm.api.service.IPolytomousKeyService;
+import eu.etaxonomy.cdm.api.service.ITaxonService;
+import eu.etaxonomy.cdm.api.service.ITermService;
+import eu.etaxonomy.cdm.api.service.IUserService;
+import eu.etaxonomy.cdm.api.service.IVocabularyService;
+import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
+import eu.etaxonomy.cdm.model.agent.AgentBase;
+import eu.etaxonomy.cdm.model.agent.Person;
+import eu.etaxonomy.cdm.model.common.CdmBase;
+import eu.etaxonomy.cdm.model.common.Credit;
+import eu.etaxonomy.cdm.model.common.DefinedTermBase;
+import eu.etaxonomy.cdm.model.common.Extension;
+import eu.etaxonomy.cdm.model.common.IdentifiableSource;
+import eu.etaxonomy.cdm.model.common.Language;
+import eu.etaxonomy.cdm.model.common.LanguageString;
+import eu.etaxonomy.cdm.model.common.TermType;
+import eu.etaxonomy.cdm.model.common.TermVocabulary;
+import eu.etaxonomy.cdm.model.description.KeyStatement;
+import eu.etaxonomy.cdm.model.description.PolytomousKey;
+import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
+import eu.etaxonomy.cdm.model.media.Rights;
+import eu.etaxonomy.cdm.model.taxon.Taxon;
+import eu.etaxonomy.taxeditor.httpinvoker.RemotingSessionAwareTest;
+import eu.etaxonomy.taxeditor.httpinvoker.TestThread;
+
+
+/**
+ * @author cmathew
+ * @date 7 Oct 2014
+ *
+ */
+@DataSet
+public class CdmEntitySessionAwareTest extends RemotingSessionAwareTest {
+
+    private static final Logger logger = Logger.getLogger(CdmEntitySessionAwareTest.class);
+
+    UUID polytomousKeyUuid = UUID.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66");
+    UUID subKeyUuid = UUID.fromString("9d8bf4f6-a70a-4b80-8556-2ccfb436ff01");
+    //UUID polytomousKeyUuid = UUID.fromString("bab66772-2c83-428a-bb6d-655d12ac6097");
+    UUID taxon1Uuid = UUID.fromString("2b336df7-29e8-4f79-985f-66502739d22f");
+    UUID personUuid = UUID.fromString("945d08f2-eb92-45b6-9252-6275ea6d338b");
+
+
+
+    IPolytomousKeyService polytomousKeyService = getRemoteApplicationController().getPolytomousKeyService();
+    IPolytomousKeyNodeService polytomousKeyNodeService = getRemoteApplicationController().getPolytomousKeyNodeService();
+    ICommonService commonService = getRemoteApplicationController().getCommonService();
+    ITaxonService taxonService = getRemoteApplicationController().getTaxonService();
+    IVocabularyService vocabularyService = getRemoteApplicationController().getVocabularyService();
+    ITermService termService = getRemoteApplicationController().getTermService();
+    IUserService userService = getRemoteApplicationController().getUserService();
+
+    //Language english = Language.getLanguageFromUuid(Language.uuidEnglish);
+
+
+
+    @BeforeClass
+    public static void initializePolytomousKeyTest() {
+    }
+
+
+    @Test
+    @Ignore
+    public void readAllPolytomousKeys() {
+        List<PolytomousKey> pKeys = polytomousKeyService.list(PolytomousKey.class, null, null, null, null);
+        Iterator<PolytomousKey> pKeysItr = pKeys.iterator();
+        Assert.assertEquals(pKeysItr.next().getUuid(),UUID.fromString("9d8bf4f6-a70a-4b80-8556-2ccfb436ff01"));
+        Assert.assertEquals(pKeysItr.next().getUuid(),UUID.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66"));
+    }
+
+
+    @Test
+    //@DataSet("PolytomousKeyTest.readPolytmousKeyData.xml")
+    public void readPolytmousKeyData() {
+        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
+        Set<Taxon> taxonomicScope = pKey.getTaxonomicScope();
+        Iterator<Taxon> tsItr = taxonomicScope.iterator();
+        Taxon taxon = tsItr.next();
+
+        Assert.assertEquals("Achillea sec. Cyprus", taxon.getTitleCache());
+        Assert.assertEquals(tsItr.hasNext(), false);
+
+        List<Credit> credits = pKey.getCredits();
+        AgentBase agent = credits.get(0).getAgent();
+        Assert.assertEquals(agent.getId(),4809);
+        Assert.assertEquals(agent.getTitleCache(),"R. A. Graham");
+        Assert.assertEquals(credits.get(0).getText(),"Credits Text Test");
+
+        Set<Extension> exts = pKey.getExtensions();
+        Iterator<Extension> extItr = exts.iterator();
+        Extension ext = extItr.next();
+        Assert.assertEquals(ext.getValue(), "http://test.com");
+
+        Set<Rights> rights = pKey.getRights();
+        Iterator<Rights> rightsItr = rights.iterator();
+        Rights right = rightsItr.next();
+        Assert.assertEquals(right.getText(),"Rights Text Test");
+
+        Set<IdentifiableSource> sources = pKey.getSources();
+        Iterator<IdentifiableSource> sourcesItr = sources.iterator();
+        IdentifiableSource source = sourcesItr.next();
+        Assert.assertEquals(source.getId(), 23710);
+        source = sourcesItr.next();
+        Assert.assertEquals(source.getId(), 23711);
+
+        // TO DO : Added tests for Annotations , Markers
+    }
+
+
+
+    @Test
+    public void readPolytomousKeyDataFromNodes() {
+        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
+        PolytomousKeyNode rootNode = pKey.getRoot();
+
+
+        Assert.assertEquals(rootNode.getId(), 2750);
+
+        Assert.assertEquals(rootNode.getChildAt(0).getId(), 2751);
+        Assert.assertEquals(rootNode.getChildAt(0).getParent().getId(), rootNode.getId());
+        Assert.assertEquals(rootNode.getKey().getId(), pKey.getId());
+        Integer sortIndex = (Integer)getFieldValueViaReflection(rootNode.getChildAt(0), "sortIndex");
+        Assert.assertEquals(sortIndex, new Integer(0));
+        String statement = "Capitula without ligulate ray-florets; leaves entire or subentire";
+        Assert.assertEquals(rootNode.getChildAt(0).getStatement().getLabelText(Language.ENGLISH()), statement);
+
+        Assert.assertEquals(rootNode.getChildAt(1).getId(), 2753);
+        Assert.assertEquals(rootNode.getChildAt(1).getParent().getId(), rootNode.getId());
+        Assert.assertEquals(rootNode.getChildAt(1).getKey().getId(), pKey.getId());
+        sortIndex = (Integer)getFieldValueViaReflection(rootNode.getChildAt(1), "sortIndex");
+        Assert.assertEquals(sortIndex, new Integer(1));
+        statement = "Capitula with ligulate ray-florets; leaves pinnatisect";
+        Assert.assertEquals(rootNode.getChildAt(1).getStatement().getLabelText(Language.ENGLISH()), statement);
+
+        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getId(), 2754);
+        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getParent().getId(), rootNode.getChildAt(1).getId());
+        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getKey().getId(), pKey.getId());
+        sortIndex = (Integer)getFieldValueViaReflection(rootNode.getChildAt(1).getChildAt(0), "sortIndex");
+        Assert.assertEquals(sortIndex, new Integer(0));
+        statement = "Ray-florets yellow";
+        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getStatement().getLabelText(Language.ENGLISH()), statement);
+        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getChildAt(0).getTaxon().getTitleCache(), "Achillea arabica Kotschy sec. Cyprus");
+    }
+
+    @Test
+    public void addGrandChildPolytomousKeyNode() {
+
+        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
+        PolytomousKeyNode rootChildNode = pKey.getRoot().getChildAt(0);
+        PolytomousKeyNode grandChildNode = PolytomousKeyNode.NewInstance();
+        rootChildNode.addChild(grandChildNode);
+
+        polytomousKeyService.merge(pKey, true);
+        grandChildNode = pKey.getRoot().getChildAt(0).getChildAt(0);
+        Assert.assertTrue(0 != grandChildNode.getId());
+        KeyStatement ks = KeyStatement.NewInstance("test");
+        grandChildNode.setStatement(ks);
+        polytomousKeyService.merge(pKey, true);
+
+        //grandChildNode = pKey.getRoot().getChildAt(0).getChildAt(0);
+        //KeyStatement ks = grandChildNode.getStatement();
+        Assert.assertTrue(0 != ks.getId());
+
+    }
+
+    @Test
+    public void addGreatGrandChildPolytomousKeyNode() {
+
+        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
+        PolytomousKeyNode rootChildNode = pKey.getRoot().getChildAt(0);
+        PolytomousKeyNode grandChildNode = PolytomousKeyNode.NewInstance();
+        rootChildNode.addChild(grandChildNode);
+
+
+        polytomousKeyNodeService.merge(grandChildNode);
+
+
+        Assert.assertFalse(pKey.getRoot().getChildAt(0).getChildAt(0).getId() == 0);
+    }
+
+    @Test
+    public void savePolytomousKeyNodeData() {
+        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
+
+        PolytomousKeyNode pkeynode = pkey.getRoot();
+        String newQuestion = "New Question";
+        String newStatement = "New Statement";
+
+        Assert.assertEquals("Question 1",pkeynode.getQuestion().getLabel().get(Language.ENGLISH()).getText());
+        for(PolytomousKeyNode node : pkeynode.getChildren()) {
+
+            node.setQuestion(null);
+            node.setStatement(null);
+        }
+        //FIXME:Remoting Add tests for feature after fixing problem
+
+        //Feature feature = pkeynode.getFeature();
+        //Assert.assertEquals(feature.getTitleCache(),"Systematics");
+        //pkeynode.setFeature(null);
+
+        Assert.assertEquals(pkeynode.getChildAt(0).getModifyingText().get(Language.ENGLISH()).getText(),"Modifying Text 1a");
+        String modifyingText = "Modifying Text 1a updated";
+
+        //pkeynode.getChildAt(0).putModifyingText(Language.ENGLISH(), modifyingText);
+
+        Assert.assertEquals(pkeynode.getChildAt(0).getSubkey().getId(),751);
+        Assert.assertEquals("Asphodeline", pkeynode.getChildAt(0).getSubkey().getTitleCache());
+        Assert.assertNull(pkeynode.getChildAt(1).getTaxon());
+        Taxon taxon = CdmBase.deproxy(taxonService.find(taxon1Uuid),Taxon.class);
+        pkeynode.getChildAt(1).setTaxon(taxon);
+
+        polytomousKeyService.merge(pkey);
+
+        pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
+
+
+        pkeynode = pkey.getRoot();
+        for(PolytomousKeyNode node : pkeynode.getChildren()) {
+            Assert.assertNull(node.getQuestion());
+            node.setQuestion(KeyStatement.NewInstance(Language.ENGLISH(),newQuestion));
+            Assert.assertNull(node.getStatement());
+            node.setStatement(KeyStatement.NewInstance(Language.ENGLISH(),newStatement));
+        }
+
+        Assert.assertEquals(pkeynode.getChildAt(1).getTaxon(), taxon);
+
+        polytomousKeyService.merge(pkey);
+
+        pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
+
+        pkeynode = pkey.getRoot();
+        for(PolytomousKeyNode node : pkeynode.getChildren()) {
+            Assert.assertNotNull(node.getQuestion());
+            Map<Language, LanguageString> label = node.getQuestion().getLabel();
+            Assert.assertEquals(newQuestion, label.get(Language.ENGLISH()).getText());
+            Assert.assertNotNull(node.getStatement());
+            Assert.assertEquals(newStatement, node.getStatement().getLabel(Language.ENGLISH()).getText());
+        }
+        //Assert.assertEquals(pkeynode.getFeature().getId(), feature.getId());
+
+    }
+
+
+    @Test
+    public void savePolytomousKeyNodeDataWithSameSubKey() {
+
+        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
+
+
+        PolytomousKeyNode pkeynode = pkey.getRoot();
+
+        PolytomousKey subkey1 = CdmBase.deproxy(pkeynode.getChildAt(0).getSubkey(), PolytomousKey.class);
+        String subkey1title = subkey1.getTitleCache();
+        subkey1.setTitleCache(subkey1title + "test", true);
+
+
+        PolytomousKey subkey2 = CdmBase.deproxy(pkeynode.getChildAt(1).getChildAt(0).getSubkey(), PolytomousKey.class);
+        String subkey2title = subkey2.getTitleCache();
+        subkey2.setTitleCache(subkey2title + "test", true);
+
+        Assert.assertSame(subkey1, subkey2);
+
+        polytomousKeyService.merge(pkey);
+    }
+
+    @Test
+    public void savePolytomousKeyNodeDataWithSameSubKeyUsingService() {
+
+        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
+
+
+        PolytomousKeyNode pkeynode = pkey.getRoot();
+//because of the check for null values in getChildren it isn't a persistent list anymore
+  //     PersistentCollection children = (PersistentCollection) pkeynode.getChildren();
+        PolytomousKeyNode childNode0 = (PolytomousKeyNode)commonService.get(pkeynode.getUuid(), "children", 0);
+        PolytomousKey subkey1 = CdmBase.deproxy(childNode0.getSubkey(),PolytomousKey.class);
+        String subkey1title = subkey1.getTitleCache();
+        subkey1.setTitleCache(subkey1title + "test", true);
+
+        PolytomousKeyNode childNode1 = (PolytomousKeyNode)commonService.get(pkeynode.getUuid(), "children", 1);
+        PolytomousKey subkey2 = CdmBase.deproxy(childNode1.getSubkey(),PolytomousKey.class);
+        String subkey2title = subkey2.getTitleCache();
+        subkey2.setTitleCache(subkey2title + "test", true);
+
+        Assert.assertNotSame(childNode0, childNode1);
+
+        Assert.assertSame(subkey1, subkey2);
+
+        polytomousKeyService.merge(pkey);
+    }
+
+
+
+    @Test
+    public void savePolytomousKeyNodeDataWithSameLanguageInLabel() {
+
+        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
+        PolytomousKeyNode pkeynode = pkey.getRoot();
+
+        Map<Language, LanguageString> label1 = pkeynode.getQuestion().getLabel();
+        label1.size();
+
+
+        Map<Language, LanguageString> label2 = pkeynode.getChildAt(0).getStatement().getLabel();
+        label2.size();
+
+
+        polytomousKeyService.merge(pkey);
+    }
+
+    @Test
+    public void deleteSubKeyInPolytomousSubKeyWithoutInitializing() {
+        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
+
+
+        PolytomousKeyNode rootNode = pKey.getRoot();
+
+        PolytomousKeyNode childNode = rootNode.getChildAt(0);
+        PolytomousKey subKey = HibernateProxyHelper.deproxy( childNode.getSubkey(), PolytomousKey.class);;
+
+
+        DeleteResult result = polytomousKeyService.delete(subKey.getUuid());
+        //A used subKey can't be deleted
+        if (result.isOk()){
+            Assert.fail();
+        }
+
+
+        Set<Integer> ids = new HashSet<Integer>();
+        ids.add(2753);
+        ids.add(2754);
+        ids.add(2751);
+        List<PolytomousKeyNode> nodes = polytomousKeyNodeService.findById(ids);
+        for (PolytomousKeyNode child:nodes){
+            child.setSubkey(null);
+           // polytomousKeyNodeService.merge(child);
+        }
+        polytomousKeyNodeService.merge(nodes, true);
+        result = polytomousKeyService.delete(subKey.getUuid());
+        if (!result.isOk()){
+            Assert.fail();
+        }
+
+
+
+
+        // retrieving subkey shows its null
+        subKey = CdmBase.deproxy(polytomousKeyService.find(subKeyUuid),PolytomousKey.class);
+        Assert.assertNull(subKey);
+
+        pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
+
+
+    }
+
+
+    @Test
+    public void deleteSubKeyInPolytomousNode() {
+        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
+
+
+        PolytomousKeyNode rootNode = pKey.getRoot();
+        List<PolytomousKeyNode> children = rootNode.getChildren();
+        PolytomousKeyNode child = rootNode.getChildAt(0);
+        polytomousKeyNodeService.delete(child.getUuid(), true);
+
+        pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
+
+        rootNode = pKey.getRoot();
+        children = rootNode.getChildren();
+        Assert.assertFalse(children.contains(child));
+    }
+
+    @Test
+    public void deleteSubKeyInPolytomousSubKeyAfterInitializing() {
+        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
+
+
+        PolytomousKeyNode rootNode = pKey.getRoot();
+        PolytomousKeyNode child = rootNode.getChildAt(0);
+        PolytomousKey subKey = child.getSubkey();
+        // this call initializes the subkey
+        subKey.getRoot();
+
+        polytomousKeyService.delete(subKey);
+
+        // since subKey was initialized before the delete, it will be
+        // accesible even after the delete. This behaviour is similar
+        // to hibernate session delete where the deleted object is
+        // made transient, but not deleted from the object graph
+
+        PolytomousKeyNode subKeyRootNode = subKey.getRoot();
+        Assert.assertNotNull(subKey);
+    }
+
+    @Test
+    public void saveNewTermVocabulary() {
+        TermVocabulary termVocabulary =
+                TermVocabulary.NewInstance(TermType.Feature,
+                        null,
+                        "Untitled",
+                        null,
+                        null);
+        try {
+            List<TermVocabulary<DefinedTermBase>> vocabularies = vocabularyService.findByTermType(TermType.Feature);
+
+            for(TermVocabulary vocab : vocabularies) {
+                vocab.getTermsOrderedByLabels(Language.ENGLISH());
+            }
+
+            termVocabulary = vocabularyService.merge(termVocabulary);
+            vocabularies.add(termVocabulary);
+            termVocabulary.setLabel("Test");
+            vocabularyService.merge(new ArrayList<TermVocabulary>(vocabularies));
+        } finally {
+            vocabularyService.delete(termVocabulary);
+        }
+    }
+
+    @Test
+    public void saveNewTerm() {
+        UUID vocNameFeatureUuid = UUID.fromString("fa7ca3eef-4092-49e1-beec-ed5096193e5e");
+        UUID vocFeatureUuid = UUID.fromString("b187d555-f06f-4d65-9e53-da7c93f8eaa8");
+        DefinedTermBase newTerm = TermType.Feature.getEmptyDefinedTermBase();
+        newTerm.setLabel("CreateTest");
+
+        try {
+
+            Assert.assertNotNull(newTerm);
+            TermVocabulary vocNameFeature = vocabularyService.find(vocNameFeatureUuid);
+            TermVocabulary vocFeature = vocabularyService.find(vocFeatureUuid);
+
+            List<TermVocabulary> vocs = new ArrayList<TermVocabulary>();
+
+            vocs.add(vocNameFeature);
+            vocs.add(vocFeature);
+
+            vocNameFeature.addTerm(newTerm);
+
+            vocs = vocabularyService.merge(vocs);
+            for(TermVocabulary voc : vocs) {
+                if(voc.getUuid().equals(vocNameFeatureUuid)) {
+                    vocNameFeature = voc;
+                }
+            }
+
+            Assert.assertTrue(vocNameFeature.getTerms().contains(newTerm));
+
+            for(Object obj : vocNameFeature.getTerms()) {
+                DefinedTermBase dtb = (DefinedTermBase)obj;
+                if("CreateTest".equals(dtb.getLabel())) {
+                    newTerm = dtb;
+                    Assert.assertNotNull(dtb.getCreatedBy());
+                    Assert.assertNotNull(dtb.getCreated());
+                } else {
+                    Assert.assertNull(dtb.getCreatedBy());
+                }
+            }
+            newTerm.setLabel("UpdateTest");
+            newTerm = termService.merge(newTerm);
+            Assert.assertNotNull(newTerm.getUpdatedBy());
+            Assert.assertNotNull(newTerm.getUpdated());
+
+            Assert.assertNull(vocNameFeature.getCreatedBy());
+            Assert.assertNull(vocFeature.getCreatedBy());
+        } finally {
+            if(termService.find(newTerm.getUuid()) != null) {
+                termService.delete(newTerm.getUuid());
+            }
+        }
+    }
+
+
+    @Test
+    public void updatePerson() {
+        // Test for #5138
+        Person person = (Person) CdmApplicationState.getCurrentAppConfig().getAgentService().load(personUuid);
+        person.setFirstname("Me");
+        CdmApplicationState.getCurrentAppConfig().getAgentService().merge(person);
+    }
+
+
+    @Test
+    public void createPerson() {
+        // Test for #5138
+        Person person = Person.NewInstance();
+        person = (Person) CdmApplicationState.getCurrentAppConfig().getAgentService().merge(person);
+        person = (Person) CdmApplicationState.getCurrentAppConfig().getAgentService().load(person.getUuid());
+        person.setFirstname("Some");
+        CdmApplicationState.getCurrentAppConfig().getAgentService().merge(person);
+    }
+
+    @Test
+    public void testThreadLocalActiveSession() throws InterruptedException {
+        MockSessionOwner<CdmBase> so1 = new MockSessionOwner<CdmBase>();
+        final ICdmEntitySession activeSession = getCdmEntitySessionManager().newSession(so1, true);
+        TestThread thread = new TestThread(true) {
+            @Override
+            public void doRun() throws InterruptedException {
+                ICdmEntitySession threadLocalActiveSession = getCdmEntitySessionManager().getActiveSession();
+                Assert.assertEquals(threadLocalActiveSession, activeSession);
+            }
+        };
+        invokeThread(thread);
+        MockSessionOwner<CdmBase> so2 = new MockSessionOwner<CdmBase>();
+        ICdmEntitySession newActiveSession = getCdmEntitySessionManager().newSession(so2, true);
+        Assert.assertFalse(activeSession.equals(newActiveSession));
+        thread.unblock();
+
+    }
+}