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.http.commons.api.rdf;
019
020import static com.google.common.collect.ImmutableBiMap.of;
021import static com.hp.hpl.jena.graph.NodeFactory.createURI;
022import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
023import static org.junit.Assert.assertTrue;
024import static org.mockito.Matchers.eq;
025import static org.mockito.Mockito.verify;
026import static org.mockito.Mockito.when;
027import java.util.Map;
028
029import javax.ws.rs.core.UriInfo;
030
031import org.fcrepo.kernel.api.identifiers.IdentifierConverter;
032import org.fcrepo.kernel.api.models.FedoraResource;
033import org.fcrepo.kernel.api.rdf.DefaultRdfStream;
034import org.fcrepo.kernel.api.RdfStream;
035import org.fcrepo.kernel.modeshape.FedoraResourceImpl;
036import org.junit.Before;
037import org.junit.Test;
038import org.junit.runner.RunWith;
039import org.mockito.Mock;
040import org.mockito.runners.MockitoJUnitRunner;
041import org.springframework.context.ApplicationContext;
042
043import com.hp.hpl.jena.rdf.model.Resource;
044
045/**
046 * <p>HttpTripleUtilTest class.</p>
047 *
048 * @author awoods
049 */
050@RunWith(MockitoJUnitRunner.class)
051public class HttpTripleUtilTest {
052
053    private HttpTripleUtil testObj;
054
055
056    @Mock
057    private UriInfo mockUriInfo;
058
059    @Mock
060    private IdentifierConverter<Resource,FedoraResource> mockSubjects;
061
062    @Mock
063    private UriAwareResourceModelFactory mockBean1;
064
065    @Mock
066    private UriAwareResourceModelFactory mockBean2;
067
068    @Mock
069    private ApplicationContext mockContext;
070
071    @Mock
072    private FedoraResourceImpl mockResource;
073
074    @Before
075    public void setUp() {
076        testObj = new HttpTripleUtil();
077        testObj.setApplicationContext(mockContext);
078    }
079
080    @Test
081    public void shouldAddTriplesFromRegisteredBeans() {
082        final Map<String, UriAwareResourceModelFactory> mockBeans = of("doesnt", mockBean1, "matter", mockBean2);
083        when(mockContext.getBeansOfType(UriAwareResourceModelFactory.class)).thenReturn(mockBeans);
084        when(mockBean1.createModelForResource(eq(mockResource), eq(mockUriInfo), eq(mockSubjects))).thenReturn(
085                createDefaultModel());
086        when(mockBean2.createModelForResource(eq(mockResource), eq(mockUriInfo), eq(mockSubjects))).thenReturn(
087                createDefaultModel());
088
089        try (final RdfStream rdfStream = new DefaultRdfStream(createURI("info:subject"))) {
090
091            assertTrue(testObj.addHttpComponentModelsForResourceToStream(rdfStream, mockResource, mockUriInfo,
092                    mockSubjects).count() >= 0);
093
094            verify(mockBean1).createModelForResource(eq(mockResource), eq(mockUriInfo), eq(mockSubjects));
095            verify(mockBean2).createModelForResource(eq(mockResource), eq(mockUriInfo), eq(mockSubjects));
096        }
097    }
098}