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