001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.kernel.api.rdf;
019
020import static org.junit.Assert.assertEquals;
021import java.io.File;
022import java.io.IOException;
023import java.util.HashMap;
024import java.util.Map;
025
026import org.apache.commons.io.FileUtils;
027import org.junit.Before;
028import org.junit.Rule;
029import org.junit.Test;
030import org.junit.rules.TemporaryFolder;
031
032/**
033 * @author bbpennel
034 */
035public class RdfNamespaceRegistryTest {
036
037    @Rule
038    public TemporaryFolder tmpDir = new TemporaryFolder();
039
040    private RdfNamespaceRegistry registry;
041
042    private File registryFile;
043
044    @Before
045    public void init() throws Exception {
046        registryFile = tmpDir.newFile();
047
048        registry = new RdfNamespaceRegistry();
049
050    }
051
052    @Test
053    public void testGetNamespaces() {
054        final Map<String, String> namespaces = new HashMap<>();
055        namespaces.put("ldp", "http://www.w3.org/ns/ldp#");
056
057        registry.setNamespaces(namespaces);
058        assertEquals("http://www.w3.org/ns/ldp#", registry.getNamespaces().get("ldp"));
059    }
060
061    @Test
062    public void testGetNamespacesNoFile() throws Exception {
063        registry.init();
064
065        assertEquals(0, registry.getNamespaces().size());
066    }
067
068    @Test(expected = IOException.class)
069    public void testLoadFileDoesNotExist() throws Exception {
070        final String configPath = registryFile.getAbsolutePath();
071        registryFile.delete();
072
073        registry.setConfigPath(configPath);
074        registry.init();
075    }
076
077    @Test
078    public void testGetNamespacesFromFile() throws Exception {
079        final String yaml = "ldp: http://www.w3.org/ns/ldp#\n" +
080                            "memento: http://mementoweb.org/ns#\n";
081        FileUtils.write(registryFile, yaml, "UTF-8");
082
083        registry.setConfigPath(registryFile.getAbsolutePath());
084        registry.init();
085
086        final Map<String, String> namespaces = registry.getNamespaces();
087        assertEquals("Incorrect number of namespace mappings", 2, namespaces.size());
088        assertEquals("http://www.w3.org/ns/ldp#", namespaces.get("ldp"));
089        assertEquals("http://mementoweb.org/ns#", namespaces.get("memento"));
090    }
091
092    @Test(expected = IOException.class)
093    public void testLoadBadFile() throws Exception {
094        final String yaml = "uri_array:\n" +
095                            "  - http://www.w3.org/ns/ldp#\n";
096        FileUtils.write(registryFile, yaml, "UTF-8");
097
098        registry.setConfigPath(registryFile.getAbsolutePath());
099        registry.init();
100    }
101}