Project

General

Profile

1
package eu.dnetlib.utils.ontologies;
2

    
3
import com.google.gson.Gson;
4
import org.apache.commons.lang3.StringUtils;
5
import org.junit.Assert;
6
import org.junit.Test;
7

    
8
import java.io.IOException;
9
import java.io.InputStream;
10

    
11
import static org.junit.Assert.*;
12

    
13
/**
14
 * Created by claudio on 12/12/2016.
15
 */
16
public class OntologyLoaderTest {
17

    
18
    private String basePath = "/eu/dnetlib/test/profiles/OntologyDSResources/OntologyDSResourceType/";
19

    
20
    @Test
21
    public void testLoadOntologyFromCp() {
22

    
23
        final InputStream i = getClass().getResourceAsStream(basePath + "result_result_relations.xml");
24

    
25
        Ontology o = OntologyLoader.loadOntologyFromCp(i);
26
        checkOntology(o);
27

    
28
        String supplement = o.inverseOf("isSupplementedBy");
29
        assertEquals(supplement, "isSupplementTo");
30

    
31
        String part = o.inverseOf("isPartOf");
32
        assertEquals(part, "hasPart");
33
    }
34

    
35
    @Test
36
    public void testLoadOntologiesFromCp() throws IOException {
37

    
38
        OntologyLoader.loadOntologiesFromCp().values().forEach(o -> checkOntology(o));
39
    }
40

    
41
    @Test
42
    public void testLoadOntologiesSerialization() throws IOException {
43

    
44
        final Ontologies o = OntologyLoader.loadOntologiesFromCp();
45
        assertNotNull(o);
46

    
47
        final String json = o.toJson(true);
48

    
49
        assertFalse(StringUtils.isBlank(json));
50
        assertFalse("{}".equals(json.trim()));
51

    
52
        //System.out.println(json);
53

    
54
        assertTrue(StringUtils.isNoneBlank(json));
55

    
56
        final Ontologies o1 = new Gson().fromJson(json, Ontologies.class);
57

    
58
        assertNotNull(o1);
59

    
60
        o1.entrySet().forEach(e -> checkOntology(e.getValue()));
61
    }
62

    
63
    private void checkOntology(Ontology o) {
64
        Assert.assertNotNull(o);
65
        Assert.assertTrue(StringUtils.isNotBlank(o.getCode()));
66
        Assert.assertTrue(StringUtils.isNotBlank(o.getDescription()));
67
        Assert.assertNotNull(o.getTerms().values());
68

    
69
        o.getTerms().values().forEach(it -> {
70
            Assert.assertTrue(StringUtils.isNotBlank(it.getCode()));
71
            Assert.assertTrue(StringUtils.isNotBlank(it.getEncoding()));
72
            Assert.assertTrue(StringUtils.isNotBlank(it.getEnglishName()));
73
            Assert.assertTrue(StringUtils.isNotBlank(it.getNativeName()));
74
            Assert.assertTrue(StringUtils.isNotBlank(it.getInverseCode()));
75
            Assert.assertNotNull(o.getTerms().get(it.getInverseCode()));
76
        });
77
    }
78
}
    (1-1/1)