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.transformations; 019 020import static com.hp.hpl.jena.graph.NodeFactory.createLiteral; 021import static com.hp.hpl.jena.graph.NodeFactory.createURI; 022import static com.hp.hpl.jena.graph.Triple.create; 023import static java.util.stream.Stream.of; 024import static org.fcrepo.transform.transformations.LDPathTransform.CONFIGURATION_FOLDER; 025import static org.fcrepo.transform.transformations.LDPathTransform.getResourceTransform; 026import static org.junit.Assert.assertEquals; 027import static org.junit.Assert.assertTrue; 028import static org.mockito.Mockito.mock; 029import static org.mockito.Mockito.when; 030import static org.mockito.MockitoAnnotations.initMocks; 031import static org.fcrepo.kernel.api.RdfLexicon.REPOSITORY_NAMESPACE; 032 033import java.io.ByteArrayInputStream; 034import java.io.InputStream; 035import java.net.URI; 036import java.util.ArrayList; 037import java.util.Arrays; 038import java.util.Collection; 039import java.util.Map; 040import java.util.List; 041import java.util.stream.Stream; 042 043import javax.jcr.NamespaceRegistry; 044import javax.jcr.Node; 045import javax.jcr.RepositoryException; 046import javax.jcr.Session; 047import javax.jcr.Workspace; 048import javax.jcr.nodetype.NodeType; 049import javax.ws.rs.core.UriBuilder; 050 051import org.fcrepo.kernel.api.RdfStream; 052import org.fcrepo.kernel.api.models.FedoraBinary; 053import org.fcrepo.kernel.api.models.FedoraResource; 054import org.fcrepo.kernel.api.rdf.DefaultRdfStream; 055import org.fcrepo.kernel.api.services.NodeService; 056import org.fcrepo.kernel.modeshape.FedoraResourceImpl; 057import org.fcrepo.transform.TransformNotFoundException; 058 059import org.junit.Before; 060import org.junit.Test; 061import org.mockito.Mock; 062 063/** 064 * <p>LDPathTransformTest class.</p> 065 * 066 * @author cbeer 067 */ 068public class LDPathTransformTest { 069 070 @Mock 071 private Node mockNode; 072 073 @Mock 074 private FedoraResourceImpl mockResource; 075 076 @Mock 077 private Session mockSession; 078 079 @Mock 080 private InputStream mockInputStream; 081 082 @Mock 083 private NodeType mockNodeType; 084 085 @Mock 086 private NodeService mockNodeService; 087 088 @Mock 089 private Workspace mockWorkspace; 090 091 @Mock 092 private NamespaceRegistry mockRegistry; 093 094 private LDPathTransform testObj; 095 096 @Before 097 public void setUp() throws RepositoryException { 098 initMocks(this); 099 100 when(mockResource.getNode()).thenReturn(mockNode); 101 when(mockNode.getSession()).thenReturn(mockSession); 102 when(mockSession.getWorkspace()).thenReturn(mockWorkspace); 103 when(mockWorkspace.getNamespaceRegistry()).thenReturn(mockRegistry); 104 105 } 106 107 @Test(expected = TransformNotFoundException.class) 108 public void testGetNodeTypeSpecificLdpathProgramForMissingProgram() throws RepositoryException { 109 when(mockRegistry.getPrefixes()).thenReturn(new String[] { "fedora" }); 110 when(mockRegistry.getURIs()).thenReturn(new String[] { REPOSITORY_NAMESPACE }); 111 when(mockRegistry.getPrefix(REPOSITORY_NAMESPACE)).thenReturn("fedora"); 112 113 114 final FedoraResource mockConfigNode = mock(FedoraResource.class); 115 when(mockNodeService.find(mockSession, CONFIGURATION_FOLDER + "some-program")) 116 .thenReturn(mockConfigNode); 117 when(mockConfigNode.getPath()).thenReturn(CONFIGURATION_FOLDER + "some-program"); 118 when(mockConfigNode.getChildren()).thenReturn(Stream.of()); 119 120 final URI mockRdfType = UriBuilder.fromUri(REPOSITORY_NAMESPACE + "Resource").build(); 121 final List<URI> rdfTypes = new ArrayList<URI>(); 122 rdfTypes.add(mockRdfType); 123 when(mockResource.getTypes()).thenReturn(rdfTypes); 124 125 getResourceTransform(mockResource, mockSession, mockNodeService, "some-program"); 126 } 127 128 @Test 129 public void testGetNodeTypeSpecificLdpathProgramForNodeTypeProgram() throws RepositoryException { 130 final String customNsUri = "http://example-custom/type#"; 131 final String customNsPrefix = "custom"; 132 133 when(mockRegistry.getPrefixes()).thenReturn(new String[] { customNsPrefix }); 134 when(mockRegistry.getURIs()).thenReturn(new String[] { customNsUri }); 135 when(mockRegistry.getPrefix(customNsUri)).thenReturn(customNsPrefix); 136 137 final FedoraResource mockConfigNode = mock(FedoraResource.class); 138 when(mockNodeService.find(mockSession, CONFIGURATION_FOLDER + "some-program")) 139 .thenReturn(mockConfigNode); 140 when(mockConfigNode.getPath()).thenReturn(CONFIGURATION_FOLDER + "some-program"); 141 final FedoraBinary mockChildConfig = mock(FedoraBinary.class); 142 when(mockChildConfig.getPath()) 143 .thenReturn(CONFIGURATION_FOLDER + "some-program/" + customNsPrefix + ":type"); 144 when(mockConfigNode.getChildren()).thenReturn(Stream.of(mockChildConfig)); 145 146 final URI mockRdfType = UriBuilder.fromUri(customNsUri + "type").build(); 147 when(mockResource.getTypes()).thenReturn(Arrays.asList(mockRdfType)); 148 149 when(mockChildConfig.getContent()).thenReturn(mockInputStream); 150 151 final LDPathTransform nodeTypeSpecificLdpathProgramStream = 152 getResourceTransform(mockResource, mockSession, mockNodeService, "some-program"); 153 154 assertEquals(new LDPathTransform(mockInputStream), nodeTypeSpecificLdpathProgramStream); 155 } 156 157 @Test 158 public void testProgramQuery() { 159 160 final RdfStream rdfStream = new DefaultRdfStream(createURI("abc"), of( 161 create(createURI("abc"), 162 createURI("http://purl.org/dc/elements/1.1/title"), 163 createLiteral("some-title")))); 164 final InputStream testReader = new ByteArrayInputStream("title = dc:title :: xsd:string ;".getBytes()); 165 166 testObj = new LDPathTransform(testReader); 167 final List<Map<String,Collection<Object>>> stringCollectionMapList = testObj.apply(rdfStream); 168 final Map<String,Collection<Object>> stringCollectionMap = stringCollectionMapList.get(0); 169 170 assert(stringCollectionMap != null); 171 assertEquals(1, stringCollectionMap.size()); 172 assertEquals(1, stringCollectionMap.get("title").size()); 173 assertTrue(stringCollectionMap.get("title").contains("some-title")); 174 } 175}