Project

General

Profile

Download (7.44 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
@DataSets({
39
    @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class),
40
    @DataSet("/eu/etaxonomy/cdm/database/FirstData_UsersAndPermissions.xml")
41
})
42
public class EditConceptRelationshipPresenterTest extends CdmVaadinBaseTest {
43

    
44
    @SuppressWarnings("unused")
45
    private static final Logger logger = Logger.getLogger(EditConceptRelationshipPresenterTest.class);
46

    
47
    public static EditConceptRelationshipPresenter ecrp;
48

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

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

    
58
    @Test
59
    public void testLoadTaxonRelationshipTypeContainer() throws SQLException {
60
        CdmSQLContainer container = ecrp.loadTaxonRelationshipTypeContainer();
61
        Assert.assertEquals(30, container.size());
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
    @Test
132
    public void testDeleteRelationship() {
133
        UUID fromTaxonUuid = UUID.fromString("5f713f69-e03e-4a11-8a55-700fbbf44805");
134

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

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

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

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

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

    
154

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

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

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

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