Switch to side-by-side view

--- a
+++ b/src/test/java/org/sba_research/timbus/kb/timbus/KnowledgeBaseIntegrationTest.java
@@ -0,0 +1,216 @@
+/**
+ * Copyright (c) 2013/2014 Verein zur Foerderung der IT-Sicherheit in Oesterreich (SBA).
+ * The work has been developed in the TIMBUS Project and the above-mentioned are Members of the TIMBUS Consortium.
+ * TIMBUS is supported by the European Union under the 7th Framework Programme for research and technological
+ * development and demonstration activities (FP7/2007-2013) under grant agreement no. 269940.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
+ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including without
+ * limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTIBITLY, or FITNESS FOR A PARTICULAR
+ * PURPOSE. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise,
+ * unless required by applicable law or agreed to in writing, shall any Contributor be liable for damages, including
+ * any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this
+ * License or out of the use or inability to use the Work.
+ * See the License for the specific language governing permissions and limitation under the License.
+ */
+package org.sba_research.timbus.kb.timbus;
+
+import org.apache.commons.lang3.tuple.ImmutablePair;
+import org.apache.commons.lang3.tuple.Pair;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.sba_research.timbus.kb.FormatMigrationOption;
+import org.sba_research.timbus.kb.IntegrationTests;
+import org.sbaresearch.owl.JenaQueryFacade;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.hamcrest.Matchers.greaterThan;
+import static org.junit.Assert.assertThat;
+
+@Category(IntegrationTests.class)
+public class KnowledgeBaseIntegrationTest {
+
+    public static final String ONTOLOGY_FILE_NAME = "/toolKB_instance.owl";
+
+    @Test
+    public void getAlternativeTools_toolWithAlternatives_shouldReturnTools() {
+        List<String> expected = new ArrayList<String>() {{
+            add("http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB_instance.owl#Aldus-Freehand-4");
+        }};
+        TimbusToolKnowledgeBase knowledgeBase = createKnowledgeBase();
+        List<String> actual = knowledgeBase.getAlternativeTools("Aldus-Freehand-3");
+        assertThat(actual, containsInAnyOrder(expected.toArray()));
+        assertThat(actual.size(), is(expected.size()));
+    }
+
+    @Test
+    public void getAlternativeTools_unknownTool_shouldReturnEmptyList() {
+        @SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
+        List<String> expected = new ArrayList<>();
+        TimbusToolKnowledgeBase knowledgeBase = createKnowledgeBase();
+        List<String> actual = knowledgeBase.getAlternativeTools("Aldus-Freehand-23");
+        assertThat(actual.size(), is(expected.size()));
+    }
+
+    @Test
+    public void getProvidingTools_unknownAction_shouldReturnEmptyList() {
+        @SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
+        List<String> expected = new ArrayList<>();
+        TimbusToolKnowledgeBase knowledgeBase = createKnowledgeBase();
+        List<String> actual = knowledgeBase.getProvidingTools("spawn_magic_lolcat");
+        assertThat(actual.size(), is(expected.size()));
+    }
+
+    @Test
+    public void getProvidingTools_actionWithProvidingTools_shouldReturnTools() {
+        List<String> expected = new ArrayList<String>() {{
+            add("http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB_instance.owl#Pdq-Lite");
+            add("http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB_instance.owl#Adobe-Acrobat");
+        }};
+        TimbusToolKnowledgeBase knowledgeBase = createKnowledgeBase();
+        List<String> actual = knowledgeBase.getProvidingTools("action_read_Portable-Document-Format");
+        assertThat(actual, containsInAnyOrder(expected.toArray()));
+        assertThat(actual.size(), is(expected.size()));
+    }
+
+    @Test
+    public void getSupportingTools_byFormatPairReadFormatOnly_shouldReturnTools() {
+        final String ns = "http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB_instance.owl#";
+        List<String> expected = new ArrayList<String>() {{
+            add(ns + "OmniWeb");
+            add(ns + "Microsoft-Word");
+            add(ns + "Lynx");
+            add(ns + "Camino");
+            add(ns + "Safari");
+            add(ns + "Shiira");
+            add(ns + "Amaya");
+            add(ns + "Firefox");
+            add(ns + "Opera");
+            add(ns + "Web");
+            add(ns + "Internet-Explorer");
+            add(ns + "Konqueror");
+            add(ns + "Google-Chrome");
+        }};
+        TimbusToolKnowledgeBase knowledgeBase = createKnowledgeBase();
+        Pair<String, String> formats = new ImmutablePair<>("fmt/100", null);
+        List<String> actual = knowledgeBase.getSupportingTools(formats);
+        assertThat(actual, containsInAnyOrder(expected.toArray()));
+        assertThat(actual.size(), is(expected.size()));
+    }
+
+    @Test
+    public void getSupportingTools_byFormatPairWriteFormatOnly_shouldReturnTools() {
+        final String ns = "http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB_instance.owl#";
+        List<String> expected = new ArrayList<String>() {{
+            add(ns + "Aptana-IDE");
+            add(ns + "Microsoft-Word");
+            add(ns + "Dreamweaver");
+            add(ns + "Amaya");
+            add(ns + "Bluefish");
+        }};
+        TimbusToolKnowledgeBase knowledgeBase = createKnowledgeBase();
+        Pair<String, String> formats = new ImmutablePair<>(null, "fmt/100");
+        List<String> actual = knowledgeBase.getSupportingTools(formats);
+        assertThat(actual, containsInAnyOrder(expected.toArray()));
+        assertThat(actual.size(), is(expected.size()));
+    }
+
+    @Test
+    public void getSupportingTools_byFormatPair_shouldReturnTools() {
+        final String ns = "http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB_instance.owl#";
+        List<String> expected = new ArrayList<String>() {{
+            add(ns + "Microsoft-Word");
+            add(ns + "Word");
+            add(ns + "Framebuilder");
+            add(ns + "Wordstar");
+            add(ns + "Adobe-FrameMaker");
+            add(ns + "Wordperfect");
+        }};
+        TimbusToolKnowledgeBase knowledgeBase = createKnowledgeBase();
+        Pair<String, String> formats = new ImmutablePair<>("fmt/37", "fmt/37");
+        List<String> actual = knowledgeBase.getSupportingTools(formats);
+        assertThat(actual, containsInAnyOrder(expected.toArray()));
+        assertThat(actual.size(), is(expected.size()));
+    }
+
+    @Test
+    public void getAllFormatIDs_validKnowledgebase_shouldReturnIDs() {
+        TimbusToolKnowledgeBase knowledgeBase = createKnowledgeBase();
+        List<String> actualIDs = knowledgeBase.getAllFormatIDs();
+        assertThat(actualIDs.size(), is(302));
+        assertThat(actualIDs.contains("fmt/100"), is(true));
+    }
+
+    @Test
+    public void getFormatName_validFormatID_shouldReturnName() {
+        TimbusToolKnowledgeBase knowledgeBase = createKnowledgeBase();
+        List<String> actual = knowledgeBase.getFormatName("fmt/100");
+        assertThat(actual, containsInAnyOrder(
+                "http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB_instance.owl#XHTML",
+                "http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB_instance.owl#HTML-File-Format"));
+    }
+
+    @Test
+    public void getReadingTools_validFormat_shouldReturnTool() {
+        TimbusToolKnowledgeBase knowledgeBase = createKnowledgeBase();
+        List<String> actual = knowledgeBase.getReadingTools("fmt/50");
+        assertThat(actual, containsInAnyOrder("http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB_instance.owl#TextEdit"));
+    }
+
+    @Test
+    @Ignore("this is just an example to get a list of file format alternatives")
+    public void getAlternativeFormats_validKnowledgebase_shouldReturnFormats() {
+        TimbusToolKnowledgeBase knowledgeBase = createKnowledgeBase();
+        String alternatives = "";
+        List<String> done = new ArrayList<>();
+        for (String format : knowledgeBase.getAllFormatIDs()) {
+            @SuppressWarnings("deprecation")
+            List<String> actual = JenaQueryFacade.removeNamespace(knowledgeBase.getAlternativeFormats(format));
+            if (actual.size() >= 2) {
+                String formatNames = JenaQueryFacade.removeNamespace(knowledgeBase.getFormatName(format)).toString();
+                if (done.contains(formatNames)) continue;
+                alternatives += formatNames + "(" + JenaQueryFacade.removeNamespace(knowledgeBase.getReadingTools(format)) + "):\n";
+                alternatives += actual.toString() + "\n\n";
+                done.add(formatNames);
+            }
+            assertThat(actual.size(), greaterThan(0));
+        }
+        System.out.println(alternatives);
+    }
+
+    @Test
+    public void getFormatMigrationOptions_invalidFormatID_shouldReturnEmptyList() {
+        TimbusToolKnowledgeBase knowledgeBase = createKnowledgeBase();
+        assertThat(knowledgeBase.getFormatMigrationOption("fmt/4223").size(), is(0));
+    }
+
+    @Test
+    public void getFormatMigrationOptions_validFormatID_shouldReturnFormatAndTools() {
+        final String ns = "http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB_instance.owl#";
+        List<FormatMigrationOption> expected = new ArrayList<FormatMigrationOption>() {{
+            add(new FormatMigrationOption(ns + "Ape", ns + "Winamp"));
+            add(new FormatMigrationOption(ns + "Wsz", ns + "Winamp"));
+            add(new FormatMigrationOption(ns + "PLS", ns + "Winamp"));
+            add(new FormatMigrationOption(ns + "Mjf", ns + "Winamp"));
+            add(new FormatMigrationOption(ns + "Nsv", ns + "Winamp"));
+            add(new FormatMigrationOption(ns + "Wpl", ns + "Windows-Media-Player"));
+            add(new FormatMigrationOption(ns + "Wmp", ns + "Windows-Media-Player"));
+            add(new FormatMigrationOption(ns + "Wmx", ns + "Windows-Media-Player"));
+        }};
+        TimbusToolKnowledgeBase knowledgeBase = createKnowledgeBase();
+        List<FormatMigrationOption> actual = knowledgeBase.getFormatMigrationOption("fmt/134");
+        assertThat(actual, containsInAnyOrder(expected.toArray()));
+    }
+
+    private TimbusToolKnowledgeBase createKnowledgeBase() {
+        return new TimbusToolKnowledgeBase(new JenaQueryFacade(JenaQueryFacade.getModelFromPath(getClass().getResource(ONTOLOGY_FILE_NAME).getFile(), true)));
+    }
+}
+