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.transform.http;
019
020import static com.hp.hpl.jena.graph.NodeFactory.createURI;
021import static java.util.stream.Stream.empty;
022import static org.apache.jena.riot.WebContent.contentTypeSPARQLQuery;
023import static org.fcrepo.kernel.api.RequiredRdfContext.LDP_CONTAINMENT;
024import static org.fcrepo.kernel.api.RequiredRdfContext.LDP_MEMBERSHIP;
025import static org.fcrepo.kernel.api.RequiredRdfContext.PROPERTIES;
026import static org.fcrepo.kernel.api.RequiredRdfContext.SERVER_MANAGED;
027import static org.fcrepo.http.commons.test.util.TestHelpers.getUriInfoImpl;
028import static org.fcrepo.http.commons.test.util.TestHelpers.mockSession;
029import static org.mockito.Matchers.any;
030import static org.mockito.Matchers.eq;
031import static org.mockito.Mockito.doReturn;
032import static org.mockito.Mockito.spy;
033import static org.mockito.Mockito.verify;
034import static org.mockito.Mockito.when;
035import static org.mockito.MockitoAnnotations.initMocks;
036import static org.springframework.test.util.ReflectionTestUtils.setField;
037
038import java.io.ByteArrayInputStream;
039import java.io.InputStream;
040
041import javax.jcr.Node;
042import javax.jcr.Session;
043import javax.ws.rs.core.MediaType;
044import javax.ws.rs.core.UriInfo;
045
046import org.fcrepo.kernel.api.RdfStream;
047import org.fcrepo.kernel.api.identifiers.IdentifierConverter;
048import org.fcrepo.kernel.api.rdf.DefaultRdfStream;
049import org.fcrepo.kernel.api.services.NodeService;
050import org.fcrepo.kernel.modeshape.FedoraResourceImpl;
051import org.fcrepo.transform.Transformation;
052import org.fcrepo.transform.TransformationFactory;
053import org.junit.Before;
054import org.junit.Test;
055import org.mockito.Mock;
056
057/**
058 * <p>FedoraTransformTest class.</p>
059 *
060 * @author cbeer
061 */
062public class FedoraTransformTest {
063
064    @Mock
065    NodeService mockNodeService;
066
067    @Mock
068    FedoraResourceImpl mockResource;
069
070    @Mock
071    Node mockNode;
072
073
074    FedoraTransform testObj;
075
076    private Session mockSession;
077    private UriInfo uriInfo;
078
079    @Mock
080    private TransformationFactory mockTransformationFactory;
081
082    @Mock
083    Transformation<Object> mockTransform;
084
085    @Before
086    public void setUp() {
087        initMocks(this);
088        testObj = spy(new FedoraTransform("testObject"));
089        setField(testObj, "nodeService", mockNodeService);
090        setField(testObj, "transformationFactory", mockTransformationFactory);
091
092        this.uriInfo = getUriInfoImpl();
093        setField(testObj, "uriInfo", uriInfo);
094        mockSession = mockSession(testObj);
095        setField(testObj, "session", mockSession);
096
097        when(mockResource.getNode()).thenReturn(mockNode);
098        when(mockResource.getPath()).thenReturn("/testObject");
099        doReturn(mockResource).when(testObj).getResourceFromPath("testObject");
100    }
101
102    @Test
103    @SuppressWarnings("unchecked")
104    public void testEvaluateTransform() {
105
106        when(mockResource.getTriples(any(IdentifierConverter.class), eq(PROPERTIES)))
107            .thenReturn(new DefaultRdfStream(createURI("abc"), empty()));
108
109        when(mockResource.getTriples(any(IdentifierConverter.class), eq(SERVER_MANAGED)))
110            .thenReturn(new DefaultRdfStream(createURI("abc"), empty()));
111
112        when(mockResource.getTriples(any(IdentifierConverter.class), eq(LDP_MEMBERSHIP)))
113            .thenReturn(new DefaultRdfStream(createURI("abc"), empty()));
114
115        when(mockResource.getTriples(any(IdentifierConverter.class), eq(LDP_CONTAINMENT)))
116            .thenReturn(new DefaultRdfStream(createURI("abc"), empty()));
117
118        final InputStream query = new ByteArrayInputStream(("SELECT ?title WHERE\n" +
119                "{\n" +
120                "  <http://example.org/book/book1> <http://purl.org/dc/elements/1.1/title> ?title .\n" +
121                "} ").getBytes());
122
123        when(mockTransformationFactory.getTransform(MediaType.valueOf(contentTypeSPARQLQuery), query)).thenReturn(
124                mockTransform);
125
126        testObj.evaluateTransform(MediaType.valueOf(contentTypeSPARQLQuery), query);
127
128        verify(mockTransform).apply(any(RdfStream.class));
129    }
130
131
132}