Project

General

Profile

« Previous | Next » 

Revision 02f8291a

Added by Patrick Plitzner almost 8 years ago

fix git commit

View differences:

eu.etaxonomy.taxeditor.test/src/test/java/eu/etaxonomy/taxeditor/session/CdmEntitySessionAwareTest.java
1
// $Id$
2
/**
3
 * Copyright (C) 2014 EDIT
4
 * European Distributed Institute of Taxonomy
5
 * http://www.e-taxonomy.eu
6
 *
7
 * The contents of this file are subject to the Mozilla Public License Version 1.1
8
 * See LICENSE.TXT at the top of this package for the full license terms.
9
 */
10
package eu.etaxonomy.taxeditor.session;
11

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

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

  
27
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
28
import eu.etaxonomy.cdm.api.service.DeleteResult;
29
import eu.etaxonomy.cdm.api.service.ICommonService;
30
import eu.etaxonomy.cdm.api.service.IPolytomousKeyNodeService;
31
import eu.etaxonomy.cdm.api.service.IPolytomousKeyService;
32
import eu.etaxonomy.cdm.api.service.ITaxonService;
33
import eu.etaxonomy.cdm.api.service.ITermService;
34
import eu.etaxonomy.cdm.api.service.IUserService;
35
import eu.etaxonomy.cdm.api.service.IVocabularyService;
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.KeyStatement;
49
import eu.etaxonomy.cdm.model.description.PolytomousKey;
50
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
51
import eu.etaxonomy.cdm.model.media.Rights;
52
import eu.etaxonomy.cdm.model.taxon.Taxon;
53
import eu.etaxonomy.taxeditor.httpinvoker.RemotingSessionAwareTest;
54
import eu.etaxonomy.taxeditor.httpinvoker.TestThread;
55

  
56

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

  
65
    private static final Logger logger = Logger.getLogger(CdmEntitySessionAwareTest.class);
66

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

  
73

  
74

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

  
83
    //Language english = Language.getLanguageFromUuid(Language.uuidEnglish);
84

  
85

  
86

  
87
    @BeforeClass
88
    public static void initializePolytomousKeyTest() {
89
    }
90

  
91

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

  
101

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

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

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

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

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

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

  
136
        // TO DO : Added tests for Annotations , Markers
137
    }
138

  
139

  
140

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

  
146

  
147
        Assert.assertEquals(rootNode.getId(), 2750);
148

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

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

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

  
175
    @Test
176
    public void addGrandChildPolytomousKeyNode() {
177

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

  
183
        polytomousKeyService.merge(pKey, true);
184
        grandChildNode = pKey.getRoot().getChildAt(0).getChildAt(0);
185
        Assert.assertTrue(0 != grandChildNode.getId());
186
        KeyStatement ks = KeyStatement.NewInstance("test");
187
        grandChildNode.setStatement(ks);
188
        polytomousKeyService.merge(pKey, true);
189

  
190
        //grandChildNode = pKey.getRoot().getChildAt(0).getChildAt(0);
191
        //KeyStatement ks = grandChildNode.getStatement();
192
        Assert.assertTrue(0 != ks.getId());
193

  
194
    }
195

  
196
    @Test
197
    public void addGreatGrandChildPolytomousKeyNode() {
198

  
199
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
200
        PolytomousKeyNode rootChildNode = pKey.getRoot().getChildAt(0);
201
        PolytomousKeyNode grandChildNode = PolytomousKeyNode.NewInstance();
202
        rootChildNode.addChild(grandChildNode);
203

  
204

  
205
        polytomousKeyNodeService.merge(grandChildNode);
206

  
207

  
208
        Assert.assertFalse(pKey.getRoot().getChildAt(0).getChildAt(0).getId() == 0);
209
    }
210

  
211
    @Test
212
    public void savePolytomousKeyNodeData() {
213
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
214

  
215
        PolytomousKeyNode pkeynode = pkey.getRoot();
216
        String newQuestion = "New Question";
217
        String newStatement = "New Statement";
218

  
219
        Assert.assertEquals("Question 1",pkeynode.getQuestion().getLabel().get(Language.ENGLISH()).getText());
220
        for(PolytomousKeyNode node : pkeynode.getChildren()) {
221

  
222
            node.setQuestion(null);
223
            node.setStatement(null);
224
        }
225
        //FIXME:Remoting Add tests for feature after fixing problem
226

  
227
        //Feature feature = pkeynode.getFeature();
228
        //Assert.assertEquals(feature.getTitleCache(),"Systematics");
229
        //pkeynode.setFeature(null);
230

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

  
234
        //pkeynode.getChildAt(0).putModifyingText(Language.ENGLISH(), modifyingText);
235

  
236
        Assert.assertEquals(pkeynode.getChildAt(0).getSubkey().getId(),751);
237
        Assert.assertEquals("Asphodeline", pkeynode.getChildAt(0).getSubkey().getTitleCache());
238
        Assert.assertNull(pkeynode.getChildAt(1).getTaxon());
239
        Taxon taxon = CdmBase.deproxy(taxonService.find(taxon1Uuid),Taxon.class);
240
        pkeynode.getChildAt(1).setTaxon(taxon);
241

  
242
        polytomousKeyService.merge(pkey);
243

  
244
        pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
245

  
246

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

  
255
        Assert.assertEquals(pkeynode.getChildAt(1).getTaxon(), taxon);
256

  
257
        polytomousKeyService.merge(pkey);
258

  
259
        pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
260

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

  
271
    }
272

  
273

  
274
    @Test
275
    public void savePolytomousKeyNodeDataWithSameSubKey() {
276

  
277
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
278

  
279

  
280
        PolytomousKeyNode pkeynode = pkey.getRoot();
281

  
282
        PolytomousKey subkey1 = CdmBase.deproxy(pkeynode.getChildAt(0).getSubkey(), PolytomousKey.class);
283
        String subkey1title = subkey1.getTitleCache();
284
        subkey1.setTitleCache(subkey1title + "test", true);
285

  
286

  
287
        PolytomousKey subkey2 = CdmBase.deproxy(pkeynode.getChildAt(1).getChildAt(0).getSubkey(), PolytomousKey.class);
288
        String subkey2title = subkey2.getTitleCache();
289
        subkey2.setTitleCache(subkey2title + "test", true);
290

  
291
        Assert.assertSame(subkey1, subkey2);
292

  
293
        polytomousKeyService.merge(pkey);
294
    }
295

  
296
    @Test
297
    public void savePolytomousKeyNodeDataWithSameSubKeyUsingService() {
298

  
299
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
300

  
301

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

  
310
        PolytomousKeyNode childNode1 = (PolytomousKeyNode)commonService.get(pkeynode.getUuid(), "children", 1);
311
        PolytomousKey subkey2 = CdmBase.deproxy(childNode1.getSubkey(),PolytomousKey.class);
312
        String subkey2title = subkey2.getTitleCache();
313
        subkey2.setTitleCache(subkey2title + "test", true);
314

  
315
        Assert.assertNotSame(childNode0, childNode1);
316

  
317
        Assert.assertSame(subkey1, subkey2);
318

  
319
        polytomousKeyService.merge(pkey);
320
    }
321

  
322

  
323

  
324
    @Test
325
    public void savePolytomousKeyNodeDataWithSameLanguageInLabel() {
326

  
327
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
328
        PolytomousKeyNode pkeynode = pkey.getRoot();
329

  
330
        Map<Language, LanguageString> label1 = pkeynode.getQuestion().getLabel();
331
        label1.size();
332

  
333

  
334
        Map<Language, LanguageString> label2 = pkeynode.getChildAt(0).getStatement().getLabel();
335
        label2.size();
336

  
337

  
338
        polytomousKeyService.merge(pkey);
339
    }
340

  
341
    @Test
342
    public void deleteSubKeyInPolytomousSubKeyWithoutInitializing() {
343
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
344

  
345

  
346
        PolytomousKeyNode rootNode = pKey.getRoot();
347

  
348
        PolytomousKeyNode childNode = rootNode.getChildAt(0);
349
        PolytomousKey subKey = HibernateProxyHelper.deproxy( childNode.getSubkey(), PolytomousKey.class);;
350

  
351

  
352
        DeleteResult result = polytomousKeyService.delete(subKey.getUuid());
353
        //A used subKey can't be deleted
354
        if (result.isOk()){
355
            Assert.fail();
356
        }
357

  
358

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

  
374

  
375

  
376

  
377
        // retrieving subkey shows its null
378
        subKey = CdmBase.deproxy(polytomousKeyService.find(subKeyUuid),PolytomousKey.class);
379
        Assert.assertNull(subKey);
380

  
381
        pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
382

  
383

  
384
    }
385

  
386

  
387
    @Test
388
    public void deleteSubKeyInPolytomousNode() {
389
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
390

  
391

  
392
        PolytomousKeyNode rootNode = pKey.getRoot();
393
        List<PolytomousKeyNode> children = rootNode.getChildren();
394
        PolytomousKeyNode child = rootNode.getChildAt(0);
395
        polytomousKeyNodeService.delete(child.getUuid(), true);
396

  
397
        pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
398

  
399
        rootNode = pKey.getRoot();
400
        children = rootNode.getChildren();
401
        Assert.assertFalse(children.contains(child));
402
    }
403

  
404
    @Test
405
    public void deleteSubKeyInPolytomousSubKeyAfterInitializing() {
406
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
407

  
408

  
409
        PolytomousKeyNode rootNode = pKey.getRoot();
410
        PolytomousKeyNode child = rootNode.getChildAt(0);
411
        PolytomousKey subKey = child.getSubkey();
412
        // this call initializes the subkey
413
        subKey.getRoot();
414

  
415
        polytomousKeyService.delete(subKey);
416

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

  
422
        PolytomousKeyNode subKeyRootNode = subKey.getRoot();
423
        Assert.assertNotNull(subKey);
424
    }
425

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

  
437
            for(TermVocabulary vocab : vocabularies) {
438
                vocab.getTermsOrderedByLabels(Language.ENGLISH());
439
            }
440

  
441
            termVocabulary = vocabularyService.merge(termVocabulary);
442
            vocabularies.add(termVocabulary);
443
            termVocabulary.setLabel("Test");
444
            vocabularyService.merge(new ArrayList<TermVocabulary>(vocabularies));
445
        } finally {
446
            vocabularyService.delete(termVocabulary);
447
        }
448
    }
449

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

  
457
        try {
458

  
459
            Assert.assertNotNull(newTerm);
460
            TermVocabulary vocNameFeature = vocabularyService.find(vocNameFeatureUuid);
461
            TermVocabulary vocFeature = vocabularyService.find(vocFeatureUuid);
462

  
463
            List<TermVocabulary> vocs = new ArrayList<TermVocabulary>();
464

  
465
            vocs.add(vocNameFeature);
466
            vocs.add(vocFeature);
467

  
468
            vocNameFeature.addTerm(newTerm);
469

  
470
            vocs = vocabularyService.merge(vocs);
471
            for(TermVocabulary voc : vocs) {
472
                if(voc.getUuid().equals(vocNameFeatureUuid)) {
473
                    vocNameFeature = voc;
474
                }
475
            }
476

  
477
            Assert.assertTrue(vocNameFeature.getTerms().contains(newTerm));
478

  
479
            for(Object obj : vocNameFeature.getTerms()) {
480
                DefinedTermBase dtb = (DefinedTermBase)obj;
481
                if("CreateTest".equals(dtb.getLabel())) {
482
                    newTerm = dtb;
483
                    Assert.assertNotNull(dtb.getCreatedBy());
484
                    Assert.assertNotNull(dtb.getCreated());
485
                } else {
486
                    Assert.assertNull(dtb.getCreatedBy());
487
                }
488
            }
489
            newTerm.setLabel("UpdateTest");
490
            newTerm = termService.merge(newTerm);
491
            Assert.assertNotNull(newTerm.getUpdatedBy());
492
            Assert.assertNotNull(newTerm.getUpdated());
493

  
494
            Assert.assertNull(vocNameFeature.getCreatedBy());
495
            Assert.assertNull(vocFeature.getCreatedBy());
496
        } finally {
497
            if(termService.find(newTerm.getUuid()) != null) {
498
                termService.delete(newTerm.getUuid());
499
            }
500
        }
501
    }
502

  
503

  
504
    @Test
505
    public void updatePerson() {
506
        // Test for #5138
507
        Person person = (Person) CdmApplicationState.getCurrentAppConfig().getAgentService().load(personUuid);
508
        person.setFirstname("Me");
509
        CdmApplicationState.getCurrentAppConfig().getAgentService().merge(person);
510
    }
511

  
512

  
513
    @Test
514
    public void createPerson() {
515
        // Test for #5138
516
        Person person = Person.NewInstance();
517
        person = (Person) CdmApplicationState.getCurrentAppConfig().getAgentService().merge(person);
518
        person = (Person) CdmApplicationState.getCurrentAppConfig().getAgentService().load(person.getUuid());
519
        person.setFirstname("Some");
520
        CdmApplicationState.getCurrentAppConfig().getAgentService().merge(person);
521
    }
522

  
523
    @Test
524
    public void testThreadLocalActiveSession() throws InterruptedException {
525
        MockSessionOwner<CdmBase> so1 = new MockSessionOwner<CdmBase>();
526
        final ICdmEntitySession activeSession = getCdmEntitySessionManager().newSession(so1, true);
527
        TestThread thread = new TestThread(true) {
528
            @Override
529
            public void doRun() throws InterruptedException {
530
                ICdmEntitySession threadLocalActiveSession = getCdmEntitySessionManager().getActiveSession();
531
                Assert.assertEquals(threadLocalActiveSession, activeSession);
532
            }
533
        };
534
        invokeThread(thread);
535
        MockSessionOwner<CdmBase> so2 = new MockSessionOwner<CdmBase>();
536
        ICdmEntitySession newActiveSession = getCdmEntitySessionManager().newSession(so2, true);
537
        Assert.assertFalse(activeSession.equals(newActiveSession));
538
        thread.unblock();
539

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

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

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

  
27
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
28
import eu.etaxonomy.cdm.api.service.DeleteResult;
29
import eu.etaxonomy.cdm.api.service.ICommonService;
30
import eu.etaxonomy.cdm.api.service.IPolytomousKeyNodeService;
31
import eu.etaxonomy.cdm.api.service.IPolytomousKeyService;
32
import eu.etaxonomy.cdm.api.service.ITaxonService;
33
import eu.etaxonomy.cdm.api.service.ITermService;
34
import eu.etaxonomy.cdm.api.service.IUserService;
35
import eu.etaxonomy.cdm.api.service.IVocabularyService;
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.KeyStatement;
49
import eu.etaxonomy.cdm.model.description.PolytomousKey;
50
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
51
import eu.etaxonomy.cdm.model.media.Rights;
52
import eu.etaxonomy.cdm.model.taxon.Taxon;
53
import eu.etaxonomy.taxeditor.httpinvoker.RemotingSessionAwareTest;
54
import eu.etaxonomy.taxeditor.httpinvoker.TestThread;
55

  
56

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

  
65
    private static final Logger logger = Logger.getLogger(CdmEntitySessionAwareTest.class);
66

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

  
73

  
74

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

  
83
    //Language english = Language.getLanguageFromUuid(Language.uuidEnglish);
84

  
85

  
86

  
87
    @BeforeClass
88
    public static void initializePolytomousKeyTest() {
89
    }
90

  
91

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

  
101

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

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

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

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

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

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

  
136
        // TO DO : Added tests for Annotations , Markers
137
    }
138

  
139

  
140

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

  
146

  
147
        Assert.assertEquals(rootNode.getId(), 2750);
148

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

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

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

  
175
    @Test
176
    public void addGrandChildPolytomousKeyNode() {
177

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

  
183
        polytomousKeyService.merge(pKey, true);
184
        grandChildNode = pKey.getRoot().getChildAt(0).getChildAt(0);
185
        Assert.assertTrue(0 != grandChildNode.getId());
186
        KeyStatement ks = KeyStatement.NewInstance("test");
187
        grandChildNode.setStatement(ks);
188
        polytomousKeyService.merge(pKey, true);
189

  
190
        //grandChildNode = pKey.getRoot().getChildAt(0).getChildAt(0);
191
        //KeyStatement ks = grandChildNode.getStatement();
192
        Assert.assertTrue(0 != ks.getId());
193

  
194
    }
195

  
196
    @Test
197
    public void addGreatGrandChildPolytomousKeyNode() {
198

  
199
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
200
        PolytomousKeyNode rootChildNode = pKey.getRoot().getChildAt(0);
201
        PolytomousKeyNode grandChildNode = PolytomousKeyNode.NewInstance();
202
        rootChildNode.addChild(grandChildNode);
203

  
204

  
205
        polytomousKeyNodeService.merge(grandChildNode);
206

  
207

  
208
        Assert.assertFalse(pKey.getRoot().getChildAt(0).getChildAt(0).getId() == 0);
209
    }
210

  
211
    @Test
212
    public void savePolytomousKeyNodeData() {
213
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
214

  
215
        PolytomousKeyNode pkeynode = pkey.getRoot();
216
        String newQuestion = "New Question";
217
        String newStatement = "New Statement";
218

  
219
        Assert.assertEquals("Question 1",pkeynode.getQuestion().getLabel().get(Language.ENGLISH()).getText());
220
        for(PolytomousKeyNode node : pkeynode.getChildren()) {
221

  
222
            node.setQuestion(null);
223
            node.setStatement(null);
224
        }
225
        //FIXME:Remoting Add tests for feature after fixing problem
226

  
227
        //Feature feature = pkeynode.getFeature();
228
        //Assert.assertEquals(feature.getTitleCache(),"Systematics");
229
        //pkeynode.setFeature(null);
230

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

  
234
        //pkeynode.getChildAt(0).putModifyingText(Language.ENGLISH(), modifyingText);
235

  
236
        Assert.assertEquals(pkeynode.getChildAt(0).getSubkey().getId(),751);
237
        Assert.assertEquals("Asphodeline", pkeynode.getChildAt(0).getSubkey().getTitleCache());
238
        Assert.assertNull(pkeynode.getChildAt(1).getTaxon());
239
        Taxon taxon = CdmBase.deproxy(taxonService.find(taxon1Uuid),Taxon.class);
240
        pkeynode.getChildAt(1).setTaxon(taxon);
241

  
242
        polytomousKeyService.merge(pkey);
243

  
244
        pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
245

  
246

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

  
255
        Assert.assertEquals(pkeynode.getChildAt(1).getTaxon(), taxon);
256

  
257
        polytomousKeyService.merge(pkey);
258

  
259
        pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
260

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

  
271
    }
272

  
273

  
274
    @Test
275
    public void savePolytomousKeyNodeDataWithSameSubKey() {
276

  
277
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
278

  
279

  
280
        PolytomousKeyNode pkeynode = pkey.getRoot();
281

  
282
        PolytomousKey subkey1 = CdmBase.deproxy(pkeynode.getChildAt(0).getSubkey(), PolytomousKey.class);
283
        String subkey1title = subkey1.getTitleCache();
284
        subkey1.setTitleCache(subkey1title + "test", true);
285

  
286

  
287
        PolytomousKey subkey2 = CdmBase.deproxy(pkeynode.getChildAt(1).getChildAt(0).getSubkey(), PolytomousKey.class);
288
        String subkey2title = subkey2.getTitleCache();
289
        subkey2.setTitleCache(subkey2title + "test", true);
290

  
291
        Assert.assertSame(subkey1, subkey2);
292

  
293
        polytomousKeyService.merge(pkey);
294
    }
295

  
296
    @Test
297
    public void savePolytomousKeyNodeDataWithSameSubKeyUsingService() {
298

  
299
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
300

  
301

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

  
310
        PolytomousKeyNode childNode1 = (PolytomousKeyNode)commonService.get(pkeynode.getUuid(), "children", 1);
311
        PolytomousKey subkey2 = CdmBase.deproxy(childNode1.getSubkey(),PolytomousKey.class);
312
        String subkey2title = subkey2.getTitleCache();
313
        subkey2.setTitleCache(subkey2title + "test", true);
314

  
315
        Assert.assertNotSame(childNode0, childNode1);
316

  
317
        Assert.assertSame(subkey1, subkey2);
318

  
319
        polytomousKeyService.merge(pkey);
320
    }
321

  
322

  
323

  
324
    @Test
325
    public void savePolytomousKeyNodeDataWithSameLanguageInLabel() {
326

  
327
        PolytomousKey pkey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
328
        PolytomousKeyNode pkeynode = pkey.getRoot();
329

  
330
        Map<Language, LanguageString> label1 = pkeynode.getQuestion().getLabel();
331
        label1.size();
332

  
333

  
334
        Map<Language, LanguageString> label2 = pkeynode.getChildAt(0).getStatement().getLabel();
335
        label2.size();
336

  
337

  
338
        polytomousKeyService.merge(pkey);
339
    }
340

  
341
    @Test
342
    public void deleteSubKeyInPolytomousSubKeyWithoutInitializing() {
343
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
344

  
345

  
346
        PolytomousKeyNode rootNode = pKey.getRoot();
347

  
348
        PolytomousKeyNode childNode = rootNode.getChildAt(0);
349
        PolytomousKey subKey = HibernateProxyHelper.deproxy( childNode.getSubkey(), PolytomousKey.class);;
350

  
351

  
352
        DeleteResult result = polytomousKeyService.delete(subKey.getUuid());
353
        //A used subKey can't be deleted
354
        if (result.isOk()){
355
            Assert.fail();
356
        }
357

  
358

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

  
374

  
375

  
376

  
377
        // retrieving subkey shows its null
378
        subKey = CdmBase.deproxy(polytomousKeyService.find(subKeyUuid),PolytomousKey.class);
379
        Assert.assertNull(subKey);
380

  
381
        pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
382

  
383

  
384
    }
385

  
386

  
387
    @Test
388
    public void deleteSubKeyInPolytomousNode() {
389
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
390

  
391

  
392
        PolytomousKeyNode rootNode = pKey.getRoot();
393
        List<PolytomousKeyNode> children = rootNode.getChildren();
394
        PolytomousKeyNode child = rootNode.getChildAt(0);
395
        polytomousKeyNodeService.delete(child.getUuid(), true);
396

  
397
        pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
398

  
399
        rootNode = pKey.getRoot();
400
        children = rootNode.getChildren();
401
        Assert.assertFalse(children.contains(child));
402
    }
403

  
404
    @Test
405
    public void deleteSubKeyInPolytomousSubKeyAfterInitializing() {
406
        PolytomousKey pKey = CdmBase.deproxy(polytomousKeyService.find(polytomousKeyUuid),PolytomousKey.class);
407

  
408

  
409
        PolytomousKeyNode rootNode = pKey.getRoot();
410
        PolytomousKeyNode child = rootNode.getChildAt(0);
411
        PolytomousKey subKey = child.getSubkey();
412
        // this call initializes the subkey
413
        subKey.getRoot();
414

  
415
        polytomousKeyService.delete(subKey);
416

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

  
422
        PolytomousKeyNode subKeyRootNode = subKey.getRoot();
423
        Assert.assertNotNull(subKey);
424
    }
425

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

  
437
            for(TermVocabulary vocab : vocabularies) {
438
                vocab.getTermsOrderedByLabels(Language.ENGLISH());
439
            }
440

  
441
            termVocabulary = vocabularyService.merge(termVocabulary);
442
            vocabularies.add(termVocabulary);
443
            termVocabulary.setLabel("Test");
444
            vocabularyService.merge(new ArrayList<TermVocabulary>(vocabularies));
445
        } finally {
446
            vocabularyService.delete(termVocabulary);
447
        }
448
    }
449

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

  
457
        try {
458

  
459
            Assert.assertNotNull(newTerm);
460
            TermVocabulary vocNameFeature = vocabularyService.find(vocNameFeatureUuid);
461
            TermVocabulary vocFeature = vocabularyService.find(vocFeatureUuid);
462

  
463
            List<TermVocabulary> vocs = new ArrayList<TermVocabulary>();
464

  
465
            vocs.add(vocNameFeature);
466
            vocs.add(vocFeature);
467

  
468
            vocNameFeature.addTerm(newTerm);
469

  
470
            vocs = vocabularyService.merge(vocs);
471
            for(TermVocabulary voc : vocs) {
472
                if(voc.getUuid().equals(vocNameFeatureUuid)) {
473
                    vocNameFeature = voc;
474
                }
475
            }
476

  
477
            Assert.assertTrue(vocNameFeature.getTerms().contains(newTerm));
478

  
479
            for(Object obj : vocNameFeature.getTerms()) {
480
                DefinedTermBase dtb = (DefinedTermBase)obj;
481
                if("CreateTest".equals(dtb.getLabel())) {
482
                    newTerm = dtb;
483
                    Assert.assertNotNull(dtb.getCreatedBy());
484
                    Assert.assertNotNull(dtb.getCreated());
485
                } else {
486
                    Assert.assertNull(dtb.getCreatedBy());
487
                }
488
            }
489
            newTerm.setLabel("UpdateTest");
490
            newTerm = termService.merge(newTerm);
491
            Assert.assertNotNull(newTerm.getUpdatedBy());
492
            Assert.assertNotNull(newTerm.getUpdated());
493

  
494
            Assert.assertNull(vocNameFeature.getCreatedBy());
495
            Assert.assertNull(vocFeature.getCreatedBy());
496
        } finally {
497
            if(termService.find(newTerm.getUuid()) != null) {
498
                termService.delete(newTerm.getUuid());
499
            }
500
        }
501
    }
502

  
503

  
504
    @Test
505
    public void updatePerson() {
506
        // Test for #5138
507
        Person person = (Person) CdmApplicationState.getCurrentAppConfig().getAgentService().load(personUuid);
508
        person.setFirstname("Me");
509
        CdmApplicationState.getCurrentAppConfig().getAgentService().merge(person);
510
    }
511

  
512

  
513
    @Test
514
    public void createPerson() {
515
        // Test for #5138
516
        Person person = Person.NewInstance();
517
        person = (Person) CdmApplicationState.getCurrentAppConfig().getAgentService().merge(person);
518
        person = (Person) CdmApplicationState.getCurrentAppConfig().getAgentService().load(person.getUuid());
519
        person.setFirstname("Some");
520
        CdmApplicationState.getCurrentAppConfig().getAgentService().merge(person);
521
    }
522

  
523
    @Test
524
    public void testThreadLocalActiveSession() throws InterruptedException {
525
        MockSessionOwner<CdmBase> so1 = new MockSessionOwner<CdmBase>();
526
        final ICdmEntitySession activeSession = getCdmEntitySessionManager().newSession(so1, true);
527
        TestThread thread = new TestThread(true) {
528
            @Override
529
            public void doRun() throws InterruptedException {
530
                ICdmEntitySession threadLocalActiveSession = getCdmEntitySessionManager().getActiveSession();
531
                Assert.assertEquals(threadLocalActiveSession, activeSession);
532
            }
533
        };
534
        invokeThread(thread);
535
        MockSessionOwner<CdmBase> so2 = new MockSessionOwner<CdmBase>();
536
        ICdmEntitySession newActiveSession = getCdmEntitySessionManager().newSession(so2, true);
537
        Assert.assertFalse(activeSession.equals(newActiveSession));
538
        thread.unblock();
539

  
540
    }
541
}

Also available in: Unified diff