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