Project

General

Profile

Download (7.41 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.BeforeClass;
20
import org.junit.Test;
21
import org.unitils.dbunit.annotation.DataSet;
22
import org.unitils.dbunit.annotation.DataSets;
23

    
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.test.unitils.CleanSweepInsertLoadStrategy;
28
import eu.etaxonomy.cdm.vaadin.CdmVaadinBaseTest;
29
import eu.etaxonomy.cdm.vaadin.component.taxon.EditConceptRelationshipPresenter;
30
import eu.etaxonomy.cdm.vaadin.container.CdmSQLContainer;
31
import eu.etaxonomy.cdm.vaadin.container.IdUuidName;
32
import eu.etaxonomy.cdm.vaadin.util.CdmSpringContextHelper;
33

    
34
/**
35
 * @author cmathew
36
 * @since 13 Apr 2015
37
 *
38
 */
39
@DataSets({
40
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class),
41
    @DataSet("/eu/etaxonomy/cdm/database/FirstData_UsersAndPermissions.xml")
42
})
43
public class EditConceptRelationshipPresenterTest extends CdmVaadinBaseTest {
44

    
45
    private static final Logger logger = Logger.getLogger(EditConceptRelationshipPresenterTest.class);
46
    public static EditConceptRelationshipPresenter ecrp;
47

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

    
52
    @BeforeClass
53
    public static void init() {
54
        ecrp = new EditConceptRelationshipPresenter();
55
    }
56

    
57
    @Test
58
    public void testLoadTaxonRelationshipTypeContainer() throws SQLException {
59
        CdmSQLContainer container = ecrp.loadTaxonRelationshipTypeContainer();
60
        Assert.assertEquals(30, container.size());
61
    }
62

    
63

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

    
70
        IdUuidName trId =ecrp.createRelationship(fromTaxonUuid, relTypeUuid, toTaxonUuid);
71

    
72
        Taxon taxon = CdmBase.deproxy(CdmSpringContextHelper.getTaxonService().load(fromTaxonUuid,FROM_TAXON_INIT_STRATEGY),Taxon.class);
73
        Assert.assertEquals(1, taxon.getRelationsFromThisTaxon().size());
74

    
75
        Assert.assertEquals(trId.getUuid(),taxon.getRelationsFromThisTaxon().iterator().next().getUuid());
76
    }
77

    
78
    @Test
79
    public void testUpdateRelationship() {
80
        UUID fromTaxonUuid = UUID.fromString("666b484f-dc1e-4578-b404-86bc6d2e47fa");
81
        // RelType "Not Congruent to" id=864 before: (935)
82
        UUID taxonRelUuid = UUID.fromString("9634d870-bab1-4fdc-8845-c7e71aa8dc6b");
83
        UUID newToTaxonUuid = UUID.fromString("5004a8e7-b907-4744-b67e-44ccb057ab3b");
84
        // RelType "Contradiction"
85
        UUID newRelTypeUuid = UUID.fromString("a8f03491-2ad6-4fae-a04c-2a4c117a2e9b");
86

    
87
        Taxon taxon = CdmBase.deproxy(CdmSpringContextHelper.getTaxonService().load(fromTaxonUuid,FROM_TAXON_INIT_STRATEGY),Taxon.class);
88
        TaxonRelationship tr = getFromRelwithUuid(taxon, taxonRelUuid);
89

    
90
        UUID oldToTaxonUuid = tr.getToTaxon().getUuid();
91
        UUID oldRelTypeUuid = tr.getType().getUuid();
92

    
93
        Assert.assertNotNull(tr);
94
        Assert.assertNotEquals(newToTaxonUuid, oldToTaxonUuid);
95
        Assert.assertNotEquals(newRelTypeUuid, oldRelTypeUuid);
96

    
97
        // change both to taxon and relationship type
98
        ecrp.updateRelationship(fromTaxonUuid, taxonRelUuid, newRelTypeUuid, newToTaxonUuid);
99
        taxon = CdmBase.deproxy(CdmSpringContextHelper.getTaxonService().load(fromTaxonUuid,FROM_TAXON_INIT_STRATEGY),Taxon.class);
100
        tr = getFromRelwithUuid(taxon, taxonRelUuid);
101

    
102
        Assert.assertNotNull(tr);
103
        Assert.assertEquals(newToTaxonUuid, tr.getToTaxon().getUuid());
104
        Assert.assertEquals(newRelTypeUuid, tr.getType().getUuid());
105

    
106
        // reset old values
107
        ecrp.updateRelationship(fromTaxonUuid, taxonRelUuid, oldRelTypeUuid, oldToTaxonUuid);
108

    
109
        // change only relationship type
110
        ecrp.updateRelationship(fromTaxonUuid, taxonRelUuid, newRelTypeUuid, null);
111
        taxon = CdmBase.deproxy(CdmSpringContextHelper.getTaxonService().load(fromTaxonUuid,FROM_TAXON_INIT_STRATEGY),Taxon.class);
112
        tr = getFromRelwithUuid(taxon, taxonRelUuid);
113

    
114
        Assert.assertNotNull(tr);
115
        Assert.assertEquals(oldToTaxonUuid, tr.getToTaxon().getUuid());
116
        Assert.assertEquals(newRelTypeUuid, tr.getType().getUuid());
117

    
118
        // reset old values
119
        ecrp.updateRelationship(fromTaxonUuid, taxonRelUuid, oldRelTypeUuid, oldToTaxonUuid);
120

    
121
        // change only to taxon
122
        ecrp.updateRelationship(fromTaxonUuid, taxonRelUuid, null, newToTaxonUuid);
123
        taxon = CdmBase.deproxy(CdmSpringContextHelper.getTaxonService().load(fromTaxonUuid,FROM_TAXON_INIT_STRATEGY),Taxon.class);
124
        tr = getFromRelwithUuid(taxon, taxonRelUuid);
125

    
126
        Assert.assertNotNull(tr);
127
        Assert.assertEquals(newToTaxonUuid, tr.getToTaxon().getUuid());
128
        Assert.assertEquals(oldRelTypeUuid, tr.getType().getUuid());
129

    
130
    }
131

    
132
    @Test
133
    public void testDeleteRelationship() {
134
        UUID fromTaxonUuid = UUID.fromString("5f713f69-e03e-4a11-8a55-700fbbf44805");
135

    
136
        // RelType "Not Included in" id=865 before (924)
137
        UUID taxonRelUuid = UUID.fromString("cac9fa65-9b15-445f-80e4-56f77952f7ec");
138

    
139
        ecrp.deleteRelationship(fromTaxonUuid, taxonRelUuid);
140
        Taxon taxon = CdmBase.deproxy(CdmSpringContextHelper.getTaxonService().load(fromTaxonUuid,FROM_TAXON_INIT_STRATEGY),Taxon.class);
141
        Assert.assertEquals(4, taxon.getRelationsFromThisTaxon().size());
142

    
143
        TaxonRelationship tr = getFromRelwithUuid(taxon, taxonRelUuid);
144
        Assert.assertNull(tr);
145

    
146
        fromTaxonUuid = UUID.fromString("666b484f-dc1e-4578-b404-86bc6d2e47fa");
147
        // Reltype "Includes or Overlaps or Excludes" id = 866 (before: 934)
148
        taxonRelUuid = UUID.fromString("9634d870-bab1-4fdc-8845-c7e71aa8dc6b");
149

    
150
        ecrp.deleteRelationship(fromTaxonUuid, taxonRelUuid);
151
        taxon = CdmBase.deproxy(CdmSpringContextHelper.getTaxonService().load(fromTaxonUuid,FROM_TAXON_INIT_STRATEGY),Taxon.class);
152
        Assert.assertEquals(0, taxon.getRelationsFromThisTaxon().size());
153
    }
154

    
155

    
156
    @Test
157
    public void testGetRelTypeToTaxonIunMap() {
158
        UUID fromTaxonUuid = UUID.fromString("5f713f69-e03e-4a11-8a55-700fbbf44805");
159
        UUID taxonRelUuid = UUID.fromString("cc761030-38d2-4b5d-954d-32329c0ea106");
160
        Map<String, IdUuidName> map = ecrp.getRelTypeToTaxonIunMap(fromTaxonUuid, taxonRelUuid);
161

    
162
        IdUuidName relTypeIun = map.get(EditConceptRelationshipPresenter.REL_TYPE_KEY);
163
        Assert.assertEquals(865, relTypeIun.getId());
164

    
165
        IdUuidName toTaxonIun = map.get(EditConceptRelationshipPresenter.TO_TAXON_KEY);
166
        Assert.assertEquals(20, toTaxonIun.getId());
167
    }
168

    
169
    public TaxonRelationship getFromRelwithUuid(Taxon taxon, UUID taxonRelUuid) {
170
        for(TaxonRelationship tr : taxon.getRelationsFromThisTaxon()) {
171
            if(tr.getUuid().equals(taxonRelUuid)) {
172
                return tr;
173
            }
174
        }
175
        return null;
176
    }
177
}
(3-3/5)