Project

General

Profile

Download (22.9 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2014 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
6
 * The contents of this file are subject to the Mozilla Public License Version 1.1
7
 * See LICENSE.TXT at the top of this package for the full license terms.
8
 */
9
package eu.etaxonomy.taxeditor.session;
10

    
11
import java.util.ArrayList;
12
import java.util.HashSet;
13
import java.util.Iterator;
14
import java.util.List;
15
import java.util.Map;
16
import java.util.Set;
17
import java.util.UUID;
18

    
19
import org.apache.log4j.Logger;
20
import org.junit.Assert;
21
import org.junit.BeforeClass;
22
import org.junit.Ignore;
23
import org.junit.Test;
24
import org.unitils.dbunit.annotation.DataSet;
25

    
26
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
27
import eu.etaxonomy.cdm.api.service.DeleteResult;
28
import eu.etaxonomy.cdm.api.service.ICommonService;
29
import eu.etaxonomy.cdm.api.service.IPolytomousKeyNodeService;
30
import eu.etaxonomy.cdm.api.service.IPolytomousKeyService;
31
import eu.etaxonomy.cdm.api.service.ITaxonService;
32
import eu.etaxonomy.cdm.api.service.ITermService;
33
import eu.etaxonomy.cdm.api.service.IUserService;
34
import eu.etaxonomy.cdm.api.service.IVocabularyService;
35
import eu.etaxonomy.cdm.api.service.pager.Pager;
36
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
37
import eu.etaxonomy.cdm.model.agent.AgentBase;
38
import eu.etaxonomy.cdm.model.agent.Person;
39
import eu.etaxonomy.cdm.model.common.CdmBase;
40
import eu.etaxonomy.cdm.model.common.Credit;
41
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
42
import eu.etaxonomy.cdm.model.common.Extension;
43
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
44
import eu.etaxonomy.cdm.model.common.Language;
45
import eu.etaxonomy.cdm.model.common.LanguageString;
46
import eu.etaxonomy.cdm.model.common.TermType;
47
import eu.etaxonomy.cdm.model.common.TermVocabulary;
48
import eu.etaxonomy.cdm.model.description.Feature;
49
import eu.etaxonomy.cdm.model.description.KeyStatement;
50
import eu.etaxonomy.cdm.model.description.PolytomousKey;
51
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
52
import eu.etaxonomy.cdm.model.media.Rights;
53
import eu.etaxonomy.cdm.model.taxon.Taxon;
54
import eu.etaxonomy.cdm.persistence.dto.MergeResult;
55
import eu.etaxonomy.taxeditor.httpinvoker.RemotingSessionAwareTest;
56
import eu.etaxonomy.taxeditor.httpinvoker.TestThread;
57

    
58

    
59
/**
60
 * @author cmathew
61
 * @date 7 Oct 2014
62
 *
63
 */
64
@DataSet
65
public class CdmEntitySessionAwareTest extends RemotingSessionAwareTest {
66

    
67
    private static final Logger logger = Logger.getLogger(CdmEntitySessionAwareTest.class);
68

    
69
    UUID polytomousKeyUuid = UUID.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66");
70
    UUID subKeyUuid = UUID.fromString("9d8bf4f6-a70a-4b80-8556-2ccfb436ff01");
71
    //UUID polytomousKeyUuid = UUID.fromString("bab66772-2c83-428a-bb6d-655d12ac6097");
72
    UUID taxon1Uuid = UUID.fromString("2b336df7-29e8-4f79-985f-66502739d22f");
73
    UUID personUuid = UUID.fromString("945d08f2-eb92-45b6-9252-6275ea6d338b");
74

    
75

    
76

    
77
    IPolytomousKeyService polytomousKeyService = getRemoteApplicationController().getPolytomousKeyService();
78
    IPolytomousKeyNodeService polytomousKeyNodeService = getRemoteApplicationController().getPolytomousKeyNodeService();
79
    ICommonService commonService = getRemoteApplicationController().getCommonService();
80
    ITaxonService taxonService = getRemoteApplicationController().getTaxonService();
81
    IVocabularyService vocabularyService = getRemoteApplicationController().getVocabularyService();
82
    ITermService termService = getRemoteApplicationController().getTermService();
83
    IUserService userService = getRemoteApplicationController().getUserService();
84

    
85
    //Language english = Language.getLanguageFromUuid(Language.uuidEnglish);
86

    
87

    
88

    
89
    @BeforeClass
90
    public static void initializePolytomousKeyTest() {
91
    }
92

    
93

    
94
    @Test
95
    @Ignore
96
    public void readAllPolytomousKeys() {
97
        List<PolytomousKey> pKeys = polytomousKeyService.list(PolytomousKey.class, null, null, null, null);
98
        Iterator<PolytomousKey> pKeysItr = pKeys.iterator();
99
        Assert.assertEquals(pKeysItr.next().getUuid(),UUID.fromString("9d8bf4f6-a70a-4b80-8556-2ccfb436ff01"));
100
        Assert.assertEquals(pKeysItr.next().getUuid(),UUID.fromString("0d53ba20-7de4-4baa-bd8a-401048447d66"));
101
    }
102

    
103

    
104
    @Test
105
    //@DataSet("PolytomousKeyTest.readPolytmousKeyData.xml")
106
    public void readPolytmousKeyData() {
107
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
108
        Set<Taxon> taxonomicScope = pKey.getTaxonomicScope();
109
        Iterator<Taxon> tsItr = taxonomicScope.iterator();
110
        Taxon taxon = tsItr.next();
111

    
112
        Assert.assertEquals("Achillea sec. Cyprus", taxon.getTitleCache());
113
        Assert.assertEquals(tsItr.hasNext(), false);
114

    
115
        List<Credit> credits = pKey.getCredits();
116
        AgentBase agent = credits.get(0).getAgent();
117
        Assert.assertEquals(agent.getId(),4809);
118
        Assert.assertEquals(agent.getTitleCache(),"R. A. Graham");
119
        Assert.assertEquals(credits.get(0).getText(),"Credits Text Test");
120

    
121
        Set<Extension> exts = pKey.getExtensions();
122
        Iterator<Extension> extItr = exts.iterator();
123
        Extension ext = extItr.next();
124
        Assert.assertEquals(ext.getValue(), "http://test.com");
125

    
126
        Set<Rights> rights = pKey.getRights();
127
        Iterator<Rights> rightsItr = rights.iterator();
128
        Rights right = rightsItr.next();
129
        Assert.assertEquals(right.getText(),"Rights Text Test");
130

    
131
        Set<IdentifiableSource> sources = pKey.getSources();
132
        Iterator<IdentifiableSource> sourcesItr = sources.iterator();
133
        IdentifiableSource source = sourcesItr.next();
134
        Assert.assertEquals(source.getId(), 23710);
135
        source = sourcesItr.next();
136
        Assert.assertEquals(source.getId(), 23711);
137

    
138
        // TO DO : Added tests for Annotations , Markers
139
    }
140

    
141

    
142

    
143
    @Test
144
    public void readPolytomousKeyDataFromNodes() {
145
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
146
        PolytomousKeyNode rootNode = pKey.getRoot();
147

    
148

    
149
        Assert.assertEquals(rootNode.getId(), 2750);
150

    
151
        Assert.assertEquals(rootNode.getChildAt(0).getId(), 2751);
152
        Assert.assertEquals(rootNode.getChildAt(0).getParent().getId(), rootNode.getId());
153
        Assert.assertEquals(rootNode.getKey().getId(), pKey.getId());
154
        Integer sortIndex = (Integer)getFieldValueViaReflection(rootNode.getChildAt(0), "sortIndex");
155
        Assert.assertEquals(sortIndex, new Integer(0));
156
        String statement = "Capitula without ligulate ray-florets; leaves entire or subentire";
157
        Assert.assertEquals(rootNode.getChildAt(0).getStatement().getLabelText(Language.ENGLISH()), statement);
158

    
159
        Assert.assertEquals(rootNode.getChildAt(1).getId(), 2753);
160
        Assert.assertEquals(rootNode.getChildAt(1).getParent().getId(), rootNode.getId());
161
        Assert.assertEquals(rootNode.getChildAt(1).getKey().getId(), pKey.getId());
162
        sortIndex = (Integer)getFieldValueViaReflection(rootNode.getChildAt(1), "sortIndex");
163
        Assert.assertEquals(sortIndex, new Integer(1));
164
        statement = "Capitula with ligulate ray-florets; leaves pinnatisect";
165
        Assert.assertEquals(rootNode.getChildAt(1).getStatement().getLabelText(Language.ENGLISH()), statement);
166

    
167
        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getId(), 2754);
168
        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getParent().getId(), rootNode.getChildAt(1).getId());
169
        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getKey().getId(), pKey.getId());
170
        sortIndex = (Integer)getFieldValueViaReflection(rootNode.getChildAt(1).getChildAt(0), "sortIndex");
171
        Assert.assertEquals(sortIndex, new Integer(0));
172
        statement = "Ray-florets yellow";
173
        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getStatement().getLabelText(Language.ENGLISH()), statement);
174
        Assert.assertEquals(rootNode.getChildAt(1).getChildAt(0).getChildAt(0).getTaxon().getTitleCache(), "Achillea arabica Kotschy sec. Cyprus");
175
    }
176

    
177
    @Test
178
    @Ignore
179
    public void addGrandChildPolytomousKeyNode() {
180

    
181
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
182
        PolytomousKeyNode rootChildNode = pKey.getRoot().getChildAt(0);
183
        PolytomousKeyNode grandChildNode = PolytomousKeyNode.NewInstance();
184
        rootChildNode.addChild(grandChildNode);
185

    
186
        MergeResult<PolytomousKey> result = polytomousKeyService.merge(pKey, true);
187
        pKey = result.getMergedEntity();
188
        grandChildNode = pKey.getRoot().getChildAt(0).getChildAt(0);
189
        Assert.assertTrue(0 != grandChildNode.getId());
190
        KeyStatement ks = KeyStatement.NewInstance("test");
191
        grandChildNode.setStatement(ks);
192
        polytomousKeyService.merge(pKey, true);
193

    
194
        //grandChildNode = pKey.getRoot().getChildAt(0).getChildAt(0);
195
        //KeyStatement ks = grandChildNode.getStatement();
196
        Assert.assertTrue(0 != ks.getId());
197

    
198
    }
199

    
200
    @Test
201
    public void addGreatGrandChildPolytomousKeyNode() {
202

    
203
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
204
        PolytomousKeyNode rootChildNode = pKey.getRoot().getChildAt(0);
205
        PolytomousKeyNode grandChildNode = PolytomousKeyNode.NewInstance();
206
        rootChildNode.addChild(grandChildNode);
207

    
208

    
209
        polytomousKeyNodeService.merge(grandChildNode, true);
210

    
211

    
212
        Assert.assertFalse(pKey.getRoot().getChildAt(0).getChildAt(0).getId() == 0);
213
    }
214

    
215
    @Test
216
    public void savePolytomousKeyNodeData() {
217
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
218

    
219
        PolytomousKeyNode pkeynode = pkey.getRoot();
220
        String newQuestion = "New Question";
221
        String newStatement = "New Statement";
222

    
223
        Assert.assertEquals("Question 1",pkeynode.getQuestion().getLabel().get(Language.ENGLISH()).getText());
224
        for(PolytomousKeyNode node : pkeynode.getChildren()) {
225

    
226
            node.setQuestion(null);
227
            node.setStatement(null);
228
        }
229
        //FIXME:Remoting Add tests for feature after fixing problem
230

    
231
        //Feature feature = pkeynode.getFeature();
232
        //Assert.assertEquals(feature.getTitleCache(),"Systematics");
233
        //pkeynode.setFeature(null);
234

    
235
        Assert.assertEquals(pkeynode.getChildAt(0).getModifyingText().get(Language.ENGLISH()).getText(),"Modifying Text 1a");
236
        String modifyingText = "Modifying Text 1a updated";
237

    
238
        //pkeynode.getChildAt(0).putModifyingText(Language.ENGLISH(), modifyingText);
239

    
240
        Assert.assertEquals(pkeynode.getChildAt(0).getSubkey().getId(),751);
241
        Assert.assertEquals("Asphodeline", pkeynode.getChildAt(0).getSubkey().getTitleCache());
242
        Assert.assertNull(pkeynode.getChildAt(1).getTaxon());
243
        Taxon taxon = CdmBase.deproxy(taxonService.find(taxon1Uuid),Taxon.class);
244
        pkeynode.getChildAt(1).setTaxon(taxon);
245

    
246
        polytomousKeyService.merge(pkey, true);
247

    
248
        pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
249

    
250

    
251
        pkeynode = pkey.getRoot();
252
        for(PolytomousKeyNode node : pkeynode.getChildren()) {
253
            Assert.assertNull(node.getQuestion());
254
            node.setQuestion(KeyStatement.NewInstance(Language.ENGLISH(),newQuestion));
255
            Assert.assertNull(node.getStatement());
256
            node.setStatement(KeyStatement.NewInstance(Language.ENGLISH(),newStatement));
257
        }
258

    
259
        Assert.assertEquals(pkeynode.getChildAt(1).getTaxon(), taxon);
260

    
261
        polytomousKeyService.merge(pkey, true);
262

    
263
        pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
264

    
265
        pkeynode = pkey.getRoot();
266
        for(PolytomousKeyNode node : pkeynode.getChildren()) {
267
            Assert.assertNotNull(node.getQuestion());
268
            Map<Language, LanguageString> label = node.getQuestion().getLabel();
269
            Assert.assertEquals(newQuestion, label.get(Language.ENGLISH()).getText());
270
            Assert.assertNotNull(node.getStatement());
271
            Assert.assertEquals(newStatement, node.getStatement().getLabel(Language.ENGLISH()).getText());
272
        }
273
        //Assert.assertEquals(pkeynode.getFeature().getId(), feature.getId());
274

    
275
    }
276

    
277

    
278
    @Test
279
    public void savePolytomousKeyNodeDataWithSameSubKey() {
280

    
281
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
282

    
283

    
284
        PolytomousKeyNode pkeynode = pkey.getRoot();
285

    
286
        PolytomousKey subkey1 = CdmBase.deproxy(pkeynode.getChildAt(0).getSubkey(), PolytomousKey.class);
287
        String subkey1title = subkey1.getTitleCache();
288
        subkey1.setTitleCache(subkey1title + "test", true);
289

    
290

    
291
        PolytomousKey subkey2 = CdmBase.deproxy(pkeynode.getChildAt(1).getChildAt(0).getSubkey(), PolytomousKey.class);
292
        String subkey2title = subkey2.getTitleCache();
293
        subkey2.setTitleCache(subkey2title + "test", true);
294

    
295
        Assert.assertSame(subkey1, subkey2);
296

    
297
        polytomousKeyService.merge(pkey, true);
298
    }
299

    
300
    @Test
301
    public void savePolytomousKeyNodeDataWithSameSubKeyUsingService() {
302

    
303
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
304

    
305

    
306
        PolytomousKeyNode pkeynode = pkey.getRoot();
307
//because of the check for null values in getChildren it isn't a persistent list anymore
308
  //     PersistentCollection children = (PersistentCollection) pkeynode.getChildren();
309
        PolytomousKeyNode childNode0 = (PolytomousKeyNode)commonService.get(pkeynode.getUuid(), "children", 0);
310
        PolytomousKey subkey1 = CdmBase.deproxy(childNode0.getSubkey(),PolytomousKey.class);
311
        String subkey1title = subkey1.getTitleCache();
312
        subkey1.setTitleCache(subkey1title + "test", true);
313

    
314
        PolytomousKeyNode childNode1 = (PolytomousKeyNode)commonService.get(pkeynode.getUuid(), "children", 1);
315
        PolytomousKey subkey2 = CdmBase.deproxy(childNode1.getSubkey(),PolytomousKey.class);
316
        String subkey2title = subkey2.getTitleCache();
317
        subkey2.setTitleCache(subkey2title + "test", true);
318

    
319
        Assert.assertNotSame(childNode0, childNode1);
320

    
321
        Assert.assertSame(subkey1, subkey2);
322

    
323
        polytomousKeyService.merge(pkey, true);
324
    }
325

    
326

    
327

    
328
    @Test
329
    public void savePolytomousKeyNodeDataWithSameLanguageInLabel() {
330

    
331
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
332
        PolytomousKeyNode pkeynode = pkey.getRoot();
333

    
334
        Map<Language, LanguageString> label1 = pkeynode.getQuestion().getLabel();
335
        label1.size();
336

    
337

    
338
        Map<Language, LanguageString> label2 = pkeynode.getChildAt(0).getStatement().getLabel();
339
        label2.size();
340

    
341

    
342
        polytomousKeyService.merge(pkey, true);
343
    }
344

    
345
    @Test
346
    public void deleteSubKeyInPolytomousSubKeyWithoutInitializing() {
347
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
348

    
349

    
350
        PolytomousKeyNode rootNode = pKey.getRoot();
351

    
352
        PolytomousKeyNode childNode = rootNode.getChildAt(0);
353
        PolytomousKey subKey = HibernateProxyHelper.deproxy( childNode.getSubkey(), PolytomousKey.class);;
354

    
355

    
356
        DeleteResult result = polytomousKeyService.delete(subKey.getUuid());
357
        //A used subKey can't be deleted
358
        if (result.isOk()){
359
            Assert.fail();
360
        }
361

    
362

    
363
        Set<Integer> ids = new HashSet<Integer>();
364
        ids.add(2753);
365
        ids.add(2754);
366
        ids.add(2751);
367
        List<PolytomousKeyNode> nodes = polytomousKeyNodeService.findById(ids);
368
        for (PolytomousKeyNode child:nodes){
369
            child.setSubkey(null);
370
           // polytomousKeyNodeService.merge(child);
371
        }
372
        polytomousKeyNodeService.merge(nodes, true);
373
        result = polytomousKeyService.delete(subKey.getUuid());
374
        if (!result.isOk()){
375
            Assert.fail();
376
        }
377

    
378

    
379

    
380

    
381
        // retrieving subkey shows its null
382
        subKey = CdmBase.deproxy(polytomousKeyService.find(subKeyUuid),PolytomousKey.class);
383
        Assert.assertNull(subKey);
384

    
385
        pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
386

    
387

    
388
    }
389

    
390

    
391
    @Test
392
    public void deleteSubKeyInPolytomousNode() {
393
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
394

    
395

    
396
        PolytomousKeyNode rootNode = pKey.getRoot();
397
        List<PolytomousKeyNode> children = rootNode.getChildren();
398
        PolytomousKeyNode child = rootNode.getChildAt(0);
399
        polytomousKeyNodeService.delete(child.getUuid(), true);
400

    
401
        pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
402

    
403
        rootNode = pKey.getRoot();
404
        children = rootNode.getChildren();
405
        Assert.assertFalse(children.contains(child));
406
    }
407

    
408
    @Test
409
    public void deleteSubKeyInPolytomousSubKeyAfterInitializing() {
410
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
411

    
412

    
413
        PolytomousKeyNode rootNode = pKey.getRoot();
414
        PolytomousKeyNode child = rootNode.getChildAt(0);
415
        PolytomousKey subKey = child.getSubkey();
416
        // this call initializes the subkey
417
        subKey.getRoot();
418

    
419
        polytomousKeyService.delete(subKey);
420

    
421
        // since subKey was initialized before the delete, it will be
422
        // accesible even after the delete. This behaviour is similar
423
        // to hibernate session delete where the deleted object is
424
        // made transient, but not deleted from the object graph
425

    
426
        PolytomousKeyNode subKeyRootNode = subKey.getRoot();
427
        Assert.assertNotNull(subKey);
428
    }
429

    
430
    @Test
431
    public void saveNewTermVocabulary() {
432
        TermVocabulary termVocabulary =
433
                TermVocabulary.NewInstance(TermType.Feature,
434
                        null,
435
                        "Untitled",
436
                        null,
437
                        null);
438
        try {
439
            List<TermVocabulary<DefinedTermBase>> vocabularies = vocabularyService.findByTermType(TermType.Feature);
440

    
441
            for(TermVocabulary vocab : vocabularies) {
442
                vocab.getTermsOrderedByLabels(Language.ENGLISH());
443
            }
444

    
445
            termVocabulary = vocabularyService.merge(termVocabulary);
446
            vocabularies.add(termVocabulary);
447
            termVocabulary.setLabel("Test");
448
            vocabularyService.merge(new ArrayList<TermVocabulary>(vocabularies));
449
        } finally {
450
            vocabularyService.delete(termVocabulary);
451
        }
452
    }
453

    
454
    @Test
455
    public void saveNewTerm() {
456
        UUID vocNameFeatureUuid = UUID.fromString("fa7ca3eef-4092-49e1-beec-ed5096193e5e");
457
        UUID vocFeatureUuid = UUID.fromString("b187d555-f06f-4d65-9e53-da7c93f8eaa8");
458
        DefinedTermBase newTerm = TermType.Feature.getEmptyDefinedTermBase();
459
        newTerm.setLabel("CreateTest");
460

    
461
        try {
462
        	List<Feature> features = termService.list(Feature.class, 100, 0, null, null);
463
        	List<UUID> updateTerms = new ArrayList<UUID>();
464
        	for (Feature feature:features){
465
        		if (feature.getLabel().equals("UpdateTest")){
466
        			updateTerms.add(feature.getUuid());
467
        		}
468
        	}
469
        	termService.delete(updateTerms);
470
        	
471
//        	 if(termService.findByRepresentationText("UpdateTest", Feature.class, null, null) != null) {
472
//        		Pager<Feature> terms =  termService.findByRepresentationText("UpdateTest", Feature.class, null, null);
473
//        		if (!terms.getRecords().isEmpty()){
474
//        			termService.delete(terms.getRecords().get(0).getUuid());
475
//        		}
476
//             }
477

    
478
            Assert.assertNotNull(newTerm);
479
            TermVocabulary vocNameFeature = vocabularyService.find(vocNameFeatureUuid);
480
            TermVocabulary vocFeature = vocabularyService.find(vocFeatureUuid);
481

    
482
            List<TermVocabulary> vocs = new ArrayList<TermVocabulary>();
483

    
484
            vocs.add(vocNameFeature);
485
            vocs.add(vocFeature);
486

    
487
            vocNameFeature.addTerm(newTerm);
488

    
489
            List<MergeResult<TermVocabulary>> mergeResults = vocabularyService.merge(vocs, true);
490
            
491
            for(MergeResult<TermVocabulary> result : mergeResults){
492
            	TermVocabulary voc = result.getMergedEntity();
493
                if(voc.getUuid().equals(vocNameFeatureUuid)) {
494
                    vocNameFeature = voc;
495
                }
496
            }
497

    
498
            Assert.assertTrue(vocNameFeature.getTerms().contains(newTerm));
499

    
500
            for(Object obj : vocNameFeature.getTerms()) {
501
                DefinedTermBase dtb = (DefinedTermBase)obj;
502
                if("CreateTest".equals(dtb.getLabel())) {
503
                    newTerm = dtb;
504
                    Assert.assertNotNull(dtb.getCreatedBy());
505
                    Assert.assertNotNull(dtb.getCreated());
506
                } else {
507
                	System.err.println(dtb.getLabel() + " " + dtb.getCreatedBy() + " " + dtb.getUuid());
508
                    Assert.assertNull(dtb.getCreatedBy());
509
                }
510
            }
511
            newTerm.setLabel("UpdateTest");
512
            newTerm = termService.merge(newTerm);
513
            Assert.assertNotNull(newTerm.getUpdatedBy());
514
            Assert.assertNotNull(newTerm.getUpdated());
515

    
516
            Assert.assertNull(vocNameFeature.getCreatedBy());
517
            Assert.assertNull(vocFeature.getCreatedBy());
518
        } finally {
519
            if(termService.find(newTerm.getUuid()) != null) {
520
                termService.delete(newTerm.getUuid());
521
            }
522
        }
523
    }
524

    
525

    
526
    @Test
527
    public void updatePerson() {
528
        // Test for #5138
529
        Person person = (Person) CdmApplicationState.getCurrentAppConfig().getAgentService().load(personUuid);
530
        person.setFirstname("Me");
531
        CdmApplicationState.getCurrentAppConfig().getAgentService().merge(person, true);
532
    }
533

    
534

    
535
    @Test
536
    public void createPerson() {
537
        // Test for #5138
538
        Person person = Person.NewInstance();
539
        person = (Person) CdmApplicationState.getCurrentAppConfig().getAgentService().merge(person);
540
        person = (Person) CdmApplicationState.getCurrentAppConfig().getAgentService().load(person.getUuid());
541
        person.setFirstname("Some");
542
        CdmApplicationState.getCurrentAppConfig().getAgentService().merge(person, true);
543
    }
544

    
545
    @Test
546
    public void testThreadLocalActiveSession() throws InterruptedException {
547
        MockSessionOwner<CdmBase> so1 = new MockSessionOwner<CdmBase>();
548
        final ICdmEntitySession activeSession = getCdmEntitySessionManager().newSession(so1, true);
549
        TestThread thread = new TestThread(true) {
550
            @Override
551
            public void doRun() throws InterruptedException {
552
                ICdmEntitySession threadLocalActiveSession = getCdmEntitySessionManager().getActiveSession();
553
                Assert.assertEquals(threadLocalActiveSession, activeSession);
554
            }
555
        };
556
        invokeThread(thread);
557
        MockSessionOwner<CdmBase> so2 = new MockSessionOwner<CdmBase>();
558
        ICdmEntitySession newActiveSession = getCdmEntitySessionManager().newSession(so2, true);
559
        Assert.assertFalse(activeSession.equals(newActiveSession));
560
        thread.unblock();
561

    
562
    }
563
}
(2-2/4)