001/**
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.client.utils;
017
018import static com.hp.hpl.jena.graph.NodeFactory.createURI;
019import static com.hp.hpl.jena.graph.Triple.create;
020import static org.mockito.MockitoAnnotations.initMocks;
021import static org.mockito.Mockito.when;
022
023import java.io.IOException;
024import java.util.Iterator;
025
026import org.fcrepo.client.FedoraException;
027import org.fcrepo.kernel.api.RdfLexicon;
028import org.junit.Before;
029import org.junit.Test;
030import org.mockito.Mock;
031
032import com.hp.hpl.jena.graph.Graph;
033import com.hp.hpl.jena.graph.Node;
034import com.hp.hpl.jena.graph.Triple;
035import com.hp.hpl.jena.rdf.model.ResourceFactory;
036
037import static org.junit.Assert.assertTrue;
038import static org.junit.Assert.assertEquals;
039/**
040 *
041 * @author lsitu
042 *
043 */
044public class RDFSinkFilterTest {
045
046    @Mock
047    private Iterator<Triple> mockTriples;
048
049    private String testDateValue = "2014-08-14T15:11:30.118Z";
050    private String testMixinType = RdfLexicon.REPOSITORY_NAMESPACE + "test-mixin-type";
051    private final Triple testCreatedDateTriple =
052            create(createURI(RdfLexicon.REPOSITORY_NAMESPACE + "test"), RdfLexicon.CREATED_DATE.asNode(),
053                    ResourceFactory.createPlainLiteral(testDateValue).asNode());
054    private final Triple testLastModifiedDateTriple =
055            create(createURI(RdfLexicon.REPOSITORY_NAMESPACE + "test"), RdfLexicon.LAST_MODIFIED_DATE.asNode(),
056                    ResourceFactory.createPlainLiteral(testDateValue).asNode());
057    private final Triple testMixinTriple =
058            create(createURI(RdfLexicon.REPOSITORY_NAMESPACE + "test"),
059                    RdfLexicon.HAS_MIXIN_TYPE.asNode(), createURI(testMixinType));
060    private final Triple testIsWritable =
061            create(createURI(RdfLexicon.REPOSITORY_NAMESPACE + "test"),
062                    RdfLexicon.WRITABLE.asNode(), ResourceFactory.createPlainLiteral("true").asNode());
063
064    @Before
065    public void setUp() throws IOException {
066        initMocks(this);
067        when(mockTriples.hasNext()).thenReturn(true, true, true, true, false);
068        when(mockTriples.next()).thenReturn(testCreatedDateTriple, testLastModifiedDateTriple,
069                testMixinTriple, testIsWritable);
070    }
071
072    @Test
073    public void testGetAllTriples() throws FedoraException {
074        final Graph graph = RDFSinkFilter.filterTriples(mockTriples, Node.ANY);
075        assertEquals("The number of triples doesn't matched.", 4, graph.size());
076        assertTrue(graph.contains(testCreatedDateTriple));
077        assertTrue(graph.contains(testLastModifiedDateTriple));
078        assertTrue(graph.contains(testMixinTriple));
079        assertTrue(graph.contains(testIsWritable));
080    }
081
082    @Test
083    public void testFilterTriples() throws FedoraException {
084        final Graph graph = RDFSinkFilter.filterTriples(mockTriples, RdfLexicon.CREATED_DATE.asNode());
085        assertEquals("The number of triples doesn't matched.", graph.size(), 1);
086        assertTrue(graph.contains(testCreatedDateTriple));
087    }
088}