From 02f8291a6489b8cb0bdce05694942e1bdcd0e475 Mon Sep 17 00:00:00 2001 From: Patrick Plitzner Date: Tue, 24 May 2016 15:32:18 +0200 Subject: [PATCH] fix git commit --- .../session/CdmEntitySessionAwareTest.java | 1082 ++++++++--------- 1 file changed, 541 insertions(+), 541 deletions(-) diff --git a/eu.etaxonomy.taxeditor.test/src/test/java/eu/etaxonomy/taxeditor/session/CdmEntitySessionAwareTest.java b/eu.etaxonomy.taxeditor.test/src/test/java/eu/etaxonomy/taxeditor/session/CdmEntitySessionAwareTest.java index 8b98b6fe3..2cae0cd09 100644 --- a/eu.etaxonomy.taxeditor.test/src/test/java/eu/etaxonomy/taxeditor/session/CdmEntitySessionAwareTest.java +++ b/eu.etaxonomy.taxeditor.test/src/test/java/eu/etaxonomy/taxeditor/session/CdmEntitySessionAwareTest.java @@ -1,541 +1,541 @@ -// $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 pKeys = polytomousKeyService.list(PolytomousKey.class, null, null, null, null); - Iterator 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 taxonomicScope = pKey.getTaxonomicScope(); - Iterator tsItr = taxonomicScope.iterator(); - Taxon taxon = tsItr.next(); - - Assert.assertEquals("Achillea sec. Cyprus", taxon.getTitleCache()); - Assert.assertEquals(tsItr.hasNext(), false); - - List 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 exts = pKey.getExtensions(); - Iterator extItr = exts.iterator(); - Extension ext = extItr.next(); - Assert.assertEquals(ext.getValue(), "http://test.com"); - - Set rights = pKey.getRights(); - Iterator rightsItr = rights.iterator(); - Rights right = rightsItr.next(); - Assert.assertEquals(right.getText(),"Rights Text Test"); - - Set sources = pKey.getSources(); - Iterator 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 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 label1 = pkeynode.getQuestion().getLabel(); - label1.size(); - - - Map 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 ids = new HashSet(); - ids.add(2753); - ids.add(2754); - ids.add(2751); - List 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 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> 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(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 vocs = new ArrayList(); - - 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 so1 = new MockSessionOwner(); - 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 so2 = new MockSessionOwner(); - ICdmEntitySession newActiveSession = getCdmEntitySessionManager().newSession(so2, true); - Assert.assertFalse(activeSession.equals(newActiveSession)); - thread.unblock(); - - } -} +// $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 pKeys = polytomousKeyService.list(PolytomousKey.class, null, null, null, null); + Iterator 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 taxonomicScope = pKey.getTaxonomicScope(); + Iterator tsItr = taxonomicScope.iterator(); + Taxon taxon = tsItr.next(); + + Assert.assertEquals("Achillea sec. Cyprus", taxon.getTitleCache()); + Assert.assertEquals(tsItr.hasNext(), false); + + List 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 exts = pKey.getExtensions(); + Iterator extItr = exts.iterator(); + Extension ext = extItr.next(); + Assert.assertEquals(ext.getValue(), "http://test.com"); + + Set rights = pKey.getRights(); + Iterator rightsItr = rights.iterator(); + Rights right = rightsItr.next(); + Assert.assertEquals(right.getText(),"Rights Text Test"); + + Set sources = pKey.getSources(); + Iterator 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 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 label1 = pkeynode.getQuestion().getLabel(); + label1.size(); + + + Map 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 ids = new HashSet(); + ids.add(2753); + ids.add(2754); + ids.add(2751); + List 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 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> 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(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 vocs = new ArrayList(); + + 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 so1 = new MockSessionOwner(); + 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 so2 = new MockSessionOwner(); + ICdmEntitySession newActiveSession = getCdmEntitySessionManager().newSession(so2, true); + Assert.assertFalse(activeSession.equals(newActiveSession)); + thread.unblock(); + + } +} -- 2.34.1