a b/src/test/java/org/sba_research/timbus/kb/timbus/KnowledgeBaseIntegrationTest.java
1
/**
2
 * Copyright (c) 2013/2014 Verein zur Foerderung der IT-Sicherheit in Oesterreich (SBA).
3
 * The work has been developed in the TIMBUS Project and the above-mentioned are Members of the TIMBUS Consortium.
4
 * TIMBUS is supported by the European Union under the 7th Framework Programme for research and technological
5
 * development and demonstration activities (FP7/2007-2013) under grant agreement no. 269940.
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
8
 * the License. You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
9
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including without
11
 * limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTIBITLY, or FITNESS FOR A PARTICULAR
12
 * PURPOSE. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise,
13
 * unless required by applicable law or agreed to in writing, shall any Contributor be liable for damages, including
14
 * any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this
15
 * License or out of the use or inability to use the Work.
16
 * See the License for the specific language governing permissions and limitation under the License.
17
 */
18
package org.sba_research.timbus.kb.timbus;
19
20
import org.apache.commons.lang3.tuple.ImmutablePair;
21
import org.apache.commons.lang3.tuple.Pair;
22
import org.junit.Ignore;
23
import org.junit.Test;
24
import org.junit.experimental.categories.Category;
25
import org.sba_research.timbus.kb.FormatMigrationOption;
26
import org.sba_research.timbus.kb.IntegrationTests;
27
import org.sbaresearch.owl.JenaQueryFacade;
28
29
import java.util.ArrayList;
30
import java.util.List;
31
32
import static org.hamcrest.CoreMatchers.is;
33
import static org.hamcrest.Matchers.containsInAnyOrder;
34
import static org.hamcrest.Matchers.greaterThan;
35
import static org.junit.Assert.assertThat;
36
37
@Category(IntegrationTests.class)
38
public class KnowledgeBaseIntegrationTest {
39
40
    public static final String ONTOLOGY_FILE_NAME = "/toolKB_instance.owl";
41
42
    @Test
43
    public void getAlternativeTools_toolWithAlternatives_shouldReturnTools() {
44
        List<String> expected = new ArrayList<String>() {{
45
            add("http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB_instance.owl#Aldus-Freehand-4");
46
        }};
47
        TimbusToolKnowledgeBase knowledgeBase = createKnowledgeBase();
48
        List<String> actual = knowledgeBase.getAlternativeTools("Aldus-Freehand-3");
49
        assertThat(actual, containsInAnyOrder(expected.toArray()));
50
        assertThat(actual.size(), is(expected.size()));
51
    }
52
53
    @Test
54
    public void getAlternativeTools_unknownTool_shouldReturnEmptyList() {
55
        @SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
56
        List<String> expected = new ArrayList<>();
57
        TimbusToolKnowledgeBase knowledgeBase = createKnowledgeBase();
58
        List<String> actual = knowledgeBase.getAlternativeTools("Aldus-Freehand-23");
59
        assertThat(actual.size(), is(expected.size()));
60
    }
61
62
    @Test
63
    public void getProvidingTools_unknownAction_shouldReturnEmptyList() {
64
        @SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
65
        List<String> expected = new ArrayList<>();
66
        TimbusToolKnowledgeBase knowledgeBase = createKnowledgeBase();
67
        List<String> actual = knowledgeBase.getProvidingTools("spawn_magic_lolcat");
68
        assertThat(actual.size(), is(expected.size()));
69
    }
70
71
    @Test
72
    public void getProvidingTools_actionWithProvidingTools_shouldReturnTools() {
73
        List<String> expected = new ArrayList<String>() {{
74
            add("http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB_instance.owl#Pdq-Lite");
75
            add("http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB_instance.owl#Adobe-Acrobat");
76
        }};
77
        TimbusToolKnowledgeBase knowledgeBase = createKnowledgeBase();
78
        List<String> actual = knowledgeBase.getProvidingTools("action_read_Portable-Document-Format");
79
        assertThat(actual, containsInAnyOrder(expected.toArray()));
80
        assertThat(actual.size(), is(expected.size()));
81
    }
82
83
    @Test
84
    public void getSupportingTools_byFormatPairReadFormatOnly_shouldReturnTools() {
85
        final String ns = "http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB_instance.owl#";
86
        List<String> expected = new ArrayList<String>() {{
87
            add(ns + "OmniWeb");
88
            add(ns + "Microsoft-Word");
89
            add(ns + "Lynx");
90
            add(ns + "Camino");
91
            add(ns + "Safari");
92
            add(ns + "Shiira");
93
            add(ns + "Amaya");
94
            add(ns + "Firefox");
95
            add(ns + "Opera");
96
            add(ns + "Web");
97
            add(ns + "Internet-Explorer");
98
            add(ns + "Konqueror");
99
            add(ns + "Google-Chrome");
100
        }};
101
        TimbusToolKnowledgeBase knowledgeBase = createKnowledgeBase();
102
        Pair<String, String> formats = new ImmutablePair<>("fmt/100", null);
103
        List<String> actual = knowledgeBase.getSupportingTools(formats);
104
        assertThat(actual, containsInAnyOrder(expected.toArray()));
105
        assertThat(actual.size(), is(expected.size()));
106
    }
107
108
    @Test
109
    public void getSupportingTools_byFormatPairWriteFormatOnly_shouldReturnTools() {
110
        final String ns = "http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB_instance.owl#";
111
        List<String> expected = new ArrayList<String>() {{
112
            add(ns + "Aptana-IDE");
113
            add(ns + "Microsoft-Word");
114
            add(ns + "Dreamweaver");
115
            add(ns + "Amaya");
116
            add(ns + "Bluefish");
117
        }};
118
        TimbusToolKnowledgeBase knowledgeBase = createKnowledgeBase();
119
        Pair<String, String> formats = new ImmutablePair<>(null, "fmt/100");
120
        List<String> actual = knowledgeBase.getSupportingTools(formats);
121
        assertThat(actual, containsInAnyOrder(expected.toArray()));
122
        assertThat(actual.size(), is(expected.size()));
123
    }
124
125
    @Test
126
    public void getSupportingTools_byFormatPair_shouldReturnTools() {
127
        final String ns = "http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB_instance.owl#";
128
        List<String> expected = new ArrayList<String>() {{
129
            add(ns + "Microsoft-Word");
130
            add(ns + "Word");
131
            add(ns + "Framebuilder");
132
            add(ns + "Wordstar");
133
            add(ns + "Adobe-FrameMaker");
134
            add(ns + "Wordperfect");
135
        }};
136
        TimbusToolKnowledgeBase knowledgeBase = createKnowledgeBase();
137
        Pair<String, String> formats = new ImmutablePair<>("fmt/37", "fmt/37");
138
        List<String> actual = knowledgeBase.getSupportingTools(formats);
139
        assertThat(actual, containsInAnyOrder(expected.toArray()));
140
        assertThat(actual.size(), is(expected.size()));
141
    }
142
143
    @Test
144
    public void getAllFormatIDs_validKnowledgebase_shouldReturnIDs() {
145
        TimbusToolKnowledgeBase knowledgeBase = createKnowledgeBase();
146
        List<String> actualIDs = knowledgeBase.getAllFormatIDs();
147
        assertThat(actualIDs.size(), is(302));
148
        assertThat(actualIDs.contains("fmt/100"), is(true));
149
    }
150
151
    @Test
152
    public void getFormatName_validFormatID_shouldReturnName() {
153
        TimbusToolKnowledgeBase knowledgeBase = createKnowledgeBase();
154
        List<String> actual = knowledgeBase.getFormatName("fmt/100");
155
        assertThat(actual, containsInAnyOrder(
156
                "http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB_instance.owl#XHTML",
157
                "http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB_instance.owl#HTML-File-Format"));
158
    }
159
160
    @Test
161
    public void getReadingTools_validFormat_shouldReturnTool() {
162
        TimbusToolKnowledgeBase knowledgeBase = createKnowledgeBase();
163
        List<String> actual = knowledgeBase.getReadingTools("fmt/50");
164
        assertThat(actual, containsInAnyOrder("http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB_instance.owl#TextEdit"));
165
    }
166
167
    @Test
168
    @Ignore("this is just an example to get a list of file format alternatives")
169
    public void getAlternativeFormats_validKnowledgebase_shouldReturnFormats() {
170
        TimbusToolKnowledgeBase knowledgeBase = createKnowledgeBase();
171
        String alternatives = "";
172
        List<String> done = new ArrayList<>();
173
        for (String format : knowledgeBase.getAllFormatIDs()) {
174
            @SuppressWarnings("deprecation")
175
            List<String> actual = JenaQueryFacade.removeNamespace(knowledgeBase.getAlternativeFormats(format));
176
            if (actual.size() >= 2) {
177
                String formatNames = JenaQueryFacade.removeNamespace(knowledgeBase.getFormatName(format)).toString();
178
                if (done.contains(formatNames)) continue;
179
                alternatives += formatNames + "(" + JenaQueryFacade.removeNamespace(knowledgeBase.getReadingTools(format)) + "):\n";
180
                alternatives += actual.toString() + "\n\n";
181
                done.add(formatNames);
182
            }
183
            assertThat(actual.size(), greaterThan(0));
184
        }
185
        System.out.println(alternatives);
186
    }
187
188
    @Test
189
    public void getFormatMigrationOptions_invalidFormatID_shouldReturnEmptyList() {
190
        TimbusToolKnowledgeBase knowledgeBase = createKnowledgeBase();
191
        assertThat(knowledgeBase.getFormatMigrationOption("fmt/4223").size(), is(0));
192
    }
193
194
    @Test
195
    public void getFormatMigrationOptions_validFormatID_shouldReturnFormatAndTools() {
196
        final String ns = "http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB_instance.owl#";
197
        List<FormatMigrationOption> expected = new ArrayList<FormatMigrationOption>() {{
198
            add(new FormatMigrationOption(ns + "Ape", ns + "Winamp"));
199
            add(new FormatMigrationOption(ns + "Wsz", ns + "Winamp"));
200
            add(new FormatMigrationOption(ns + "PLS", ns + "Winamp"));
201
            add(new FormatMigrationOption(ns + "Mjf", ns + "Winamp"));
202
            add(new FormatMigrationOption(ns + "Nsv", ns + "Winamp"));
203
            add(new FormatMigrationOption(ns + "Wpl", ns + "Windows-Media-Player"));
204
            add(new FormatMigrationOption(ns + "Wmp", ns + "Windows-Media-Player"));
205
            add(new FormatMigrationOption(ns + "Wmx", ns + "Windows-Media-Player"));
206
        }};
207
        TimbusToolKnowledgeBase knowledgeBase = createKnowledgeBase();
208
        List<FormatMigrationOption> actual = knowledgeBase.getFormatMigrationOption("fmt/134");
209
        assertThat(actual, containsInAnyOrder(expected.toArray()));
210
    }
211
212
    private TimbusToolKnowledgeBase createKnowledgeBase() {
213
        return new TimbusToolKnowledgeBase(new JenaQueryFacade(JenaQueryFacade.getModelFromPath(getClass().getResource(ONTOLOGY_FILE_NAME).getFile(), true)));
214
    }
215
}
216