Project

General

Profile

Download (6.75 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2015 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.cdm.vaadin.presenter;
10

    
11
import java.sql.SQLException;
12
import java.util.Arrays;
13
import java.util.List;
14
import java.util.Map;
15
import java.util.UUID;
16

    
17
import org.apache.log4j.Logger;
18
import org.junit.Assert;
19
import org.junit.Test;
20
import org.unitils.dbunit.annotation.DataSet;
21
import org.unitils.spring.annotation.SpringBeanByType;
22

    
23
import eu.etaxonomy.cdm.api.application.CdmRepository;
24
import eu.etaxonomy.cdm.model.common.CdmBase;
25
import eu.etaxonomy.cdm.model.taxon.Taxon;
26
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
27
import eu.etaxonomy.cdm.vaadin.CdmVaadinBaseTest;
28
import eu.etaxonomy.cdm.vaadin.container.CdmSQLContainer;
29
import eu.etaxonomy.cdm.vaadin.container.IdUuidName;
30

    
31
/**
32
 * @author cmathew
33
 * @date 13 Apr 2015
34
 *
35
 */
36
@DataSet
37
public class EditConceptRelationshipPresenterTest extends CdmVaadinBaseTest {
38

    
39
    @SpringBeanByType
40
    private CdmRepository cdmRepo = null;
41

    
42
    @SpringBeanByType
43
    private EditConceptRelationshipPresenter ecrp = null;
44

    
45
    private static final Logger logger = Logger.getLogger(EditConceptRelationshipPresenterTest.class);
46

    
47

    
48
    private static List<String> FROM_TAXON_INIT_STRATEGY = Arrays.asList(new String []{
49
            "relationsFromThisTaxon"
50
    });
51

    
52
    @Test
53
    public void testLoadTaxonRelationshipTypeContainer() throws SQLException {
54
        CdmSQLContainer container = ecrp.loadTaxonRelationshipTypeContainer();
55
        Assert.assertEquals(29, container.size());
56
    }
57

    
58

    
59
    @Test
60
    public void testCreateRelationship() {
61
        UUID fromTaxonUuid = UUID.fromString("77e7d93e-75c6-4dd4-850d-7b5809654378");
62
        UUID toTaxonUuid = UUID.fromString("5004a8e7-b907-4744-b67e-44ccb057ab3b");
63
        UUID relTypeUuid = UUID.fromString("60974c98-64ab-4574-bb5c-c110f6db634d");
64

    
65
        IdUuidName trId =ecrp.createRelationship(fromTaxonUuid, relTypeUuid, toTaxonUuid);
66

    
67
        Taxon taxon = CdmBase.deproxy(cdmRepo.getTaxonService().load(fromTaxonUuid,FROM_TAXON_INIT_STRATEGY),Taxon.class);
68
        Assert.assertEquals(1, taxon.getRelationsFromThisTaxon().size());
69

    
70
        Assert.assertEquals(trId.getUuid(),taxon.getRelationsFromThisTaxon().iterator().next().getUuid());
71
    }
72

    
73
    @Test
74
    public void testUpdateRelationship() {
75
        UUID fromTaxonUuid = UUID.fromString("666b484f-dc1e-4578-b404-86bc6d2e47fa");
76
        UUID taxonRelUuid = UUID.fromString("9634d870-bab1-4fdc-8845-c7e71aa8dc6b");
77
        UUID newToTaxonUuid = UUID.fromString("5004a8e7-b907-4744-b67e-44ccb057ab3b");
78
        UUID newRelTypeUuid = UUID.fromString("a8f03491-2ad6-4fae-a04c-2a4c117a2e9b");
79

    
80
        Taxon taxon = CdmBase.deproxy(cdmRepo.getTaxonService().load(fromTaxonUuid,FROM_TAXON_INIT_STRATEGY),Taxon.class);
81
        TaxonRelationship tr = getFromRelwithUuid(taxon, taxonRelUuid);
82

    
83
        UUID oldToTaxonUuid = tr.getToTaxon().getUuid();
84
        UUID oldRelTypeUuid = tr.getType().getUuid();
85

    
86
        Assert.assertNotNull(tr);
87
        Assert.assertNotEquals(newToTaxonUuid, oldToTaxonUuid);
88
        Assert.assertNotEquals(newRelTypeUuid, oldRelTypeUuid);
89

    
90
        // change both to taxon and relationship type
91
        ecrp.updateRelationship(fromTaxonUuid, taxonRelUuid, newRelTypeUuid, newToTaxonUuid);
92
        taxon = CdmBase.deproxy(cdmRepo.getTaxonService().load(fromTaxonUuid,FROM_TAXON_INIT_STRATEGY),Taxon.class);
93
        tr = getFromRelwithUuid(taxon, taxonRelUuid);
94

    
95
        Assert.assertNotNull(tr);
96
        Assert.assertEquals(newToTaxonUuid, tr.getToTaxon().getUuid());
97
        Assert.assertEquals(newRelTypeUuid, tr.getType().getUuid());
98

    
99
        // reset old values
100
        ecrp.updateRelationship(fromTaxonUuid, taxonRelUuid, oldRelTypeUuid, oldToTaxonUuid);
101

    
102
        // change only relationship type
103
        ecrp.updateRelationship(fromTaxonUuid, taxonRelUuid, newRelTypeUuid, null);
104
        taxon = CdmBase.deproxy(cdmRepo.getTaxonService().load(fromTaxonUuid,FROM_TAXON_INIT_STRATEGY),Taxon.class);
105
        tr = getFromRelwithUuid(taxon, taxonRelUuid);
106

    
107
        Assert.assertNotNull(tr);
108
        Assert.assertEquals(oldToTaxonUuid, tr.getToTaxon().getUuid());
109
        Assert.assertEquals(newRelTypeUuid, tr.getType().getUuid());
110

    
111
        // reset old values
112
        ecrp.updateRelationship(fromTaxonUuid, taxonRelUuid, oldRelTypeUuid, oldToTaxonUuid);
113

    
114
        // change only to taxon
115
        ecrp.updateRelationship(fromTaxonUuid, taxonRelUuid, null, newToTaxonUuid);
116
        taxon = CdmBase.deproxy(cdmRepo.getTaxonService().load(fromTaxonUuid,FROM_TAXON_INIT_STRATEGY),Taxon.class);
117
        tr = getFromRelwithUuid(taxon, taxonRelUuid);
118

    
119
        Assert.assertNotNull(tr);
120
        Assert.assertEquals(newToTaxonUuid, tr.getToTaxon().getUuid());
121
        Assert.assertEquals(oldRelTypeUuid, tr.getType().getUuid());
122

    
123
    }
124

    
125
    @Test
126
    public void testDeleteRelationship() {
127
        UUID fromTaxonUuid = UUID.fromString("5f713f69-e03e-4a11-8a55-700fbbf44805");
128
        UUID taxonRelUuid = UUID.fromString("cac9fa65-9b15-445f-80e4-56f77952f7ec");
129

    
130
        ecrp.deleteRelationship(fromTaxonUuid, taxonRelUuid);
131
        Taxon taxon = CdmBase.deproxy(cdmRepo.getTaxonService().load(fromTaxonUuid,FROM_TAXON_INIT_STRATEGY),Taxon.class);
132
        Assert.assertEquals(4, taxon.getRelationsFromThisTaxon().size());
133

    
134
        TaxonRelationship tr = getFromRelwithUuid(taxon, taxonRelUuid);
135
        Assert.assertNull(tr);
136

    
137
        fromTaxonUuid = UUID.fromString("666b484f-dc1e-4578-b404-86bc6d2e47fa");
138
        taxonRelUuid = UUID.fromString("9634d870-bab1-4fdc-8845-c7e71aa8dc6b");
139

    
140
        ecrp.deleteRelationship(fromTaxonUuid, taxonRelUuid);
141
        taxon = CdmBase.deproxy(cdmRepo.getTaxonService().load(fromTaxonUuid,FROM_TAXON_INIT_STRATEGY),Taxon.class);
142
        Assert.assertEquals(0, taxon.getRelationsFromThisTaxon().size());
143
    }
144

    
145

    
146
    @Test
147
    public void testGetRelTypeToTaxonIunMap() {
148
        UUID fromTaxonUuid = UUID.fromString("5f713f69-e03e-4a11-8a55-700fbbf44805");
149
        UUID taxonRelUuid = UUID.fromString("cc761030-38d2-4b5d-954d-32329c0ea106");
150
        Map<String, IdUuidName> map = ecrp.getRelTypeToTaxonIunMap(fromTaxonUuid, taxonRelUuid);
151

    
152
        IdUuidName relTypeIun = map.get(EditConceptRelationshipPresenter.REL_TYPE_KEY);
153
        Assert.assertEquals(924, relTypeIun.getId());
154

    
155
        IdUuidName toTaxonIun = map.get(EditConceptRelationshipPresenter.TO_TAXON_KEY);
156
        Assert.assertEquals(20, toTaxonIun.getId());
157
    }
158

    
159
    public TaxonRelationship getFromRelwithUuid(Taxon taxon, UUID taxonRelUuid) {
160
        for(TaxonRelationship tr : taxon.getRelationsFromThisTaxon()) {
161
            if(tr.getUuid().equals(taxonRelUuid)) {
162
                return tr;
163
            }
164
        }
165
        return null;
166
    }
167
}
(3-3/5)