001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006package org.fcrepo.kernel.api.rdf;
007
008import static org.junit.Assert.assertEquals;
009import java.io.File;
010import java.io.IOException;
011import java.util.HashMap;
012import java.util.Map;
013
014import org.apache.commons.io.FileUtils;
015import org.junit.Before;
016import org.junit.Rule;
017import org.junit.Test;
018import org.junit.rules.TemporaryFolder;
019
020/**
021 * @author bbpennel
022 */
023public class RdfNamespaceRegistryTest {
024
025    @Rule
026    public TemporaryFolder tmpDir = new TemporaryFolder();
027
028    private RdfNamespaceRegistry registry;
029
030    private File registryFile;
031
032    @Before
033    public void init() throws Exception {
034        registryFile = tmpDir.newFile();
035
036        registry = new RdfNamespaceRegistry();
037
038    }
039
040    @Test
041    public void testGetNamespaces() {
042        final Map<String, String> namespaces = new HashMap<>();
043        namespaces.put("ldp", "http://www.w3.org/ns/ldp#");
044
045        registry.setNamespaces(namespaces);
046        assertEquals("http://www.w3.org/ns/ldp#", registry.getNamespaces().get("ldp"));
047    }
048
049    @Test
050    public void testGetNamespacesNoFile() throws Exception {
051        registry.init();
052
053        assertEquals(0, registry.getNamespaces().size());
054    }
055
056    @Test(expected = IOException.class)
057    public void testLoadFileDoesNotExist() throws Exception {
058        final String configPath = registryFile.getAbsolutePath();
059        registryFile.delete();
060
061        registry.setConfigPath(configPath);
062        registry.init();
063    }
064
065    @Test
066    public void testGetNamespacesFromFile() throws Exception {
067        final String yaml = "ldp: http://www.w3.org/ns/ldp#\n" +
068                            "memento: http://mementoweb.org/ns#\n";
069        FileUtils.write(registryFile, yaml, "UTF-8");
070
071        registry.setConfigPath(registryFile.getAbsolutePath());
072        registry.init();
073
074        final Map<String, String> namespaces = registry.getNamespaces();
075        assertEquals("Incorrect number of namespace mappings", 2, namespaces.size());
076        assertEquals("http://www.w3.org/ns/ldp#", namespaces.get("ldp"));
077        assertEquals("http://mementoweb.org/ns#", namespaces.get("memento"));
078    }
079
080    @Test(expected = IOException.class)
081    public void testLoadBadFile() throws Exception {
082        final String yaml = "uri_array:\n" +
083                            "  - http://www.w3.org/ns/ldp#\n";
084        FileUtils.write(registryFile, yaml, "UTF-8");
085
086        registry.setConfigPath(registryFile.getAbsolutePath());
087        registry.init();
088    }
089}