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.client.impl; 017 018import static com.hp.hpl.jena.graph.Factory.createDefaultGraph; 019import static com.hp.hpl.jena.graph.NodeFactory.createLiteral; 020import static com.hp.hpl.jena.graph.NodeFactory.createURI; 021import static com.hp.hpl.jena.graph.Triple.create; 022import static org.mockito.Matchers.anyString; 023import static org.mockito.Matchers.eq; 024import static org.mockito.MockitoAnnotations.initMocks; 025import static org.mockito.Mockito.never; 026import static org.mockito.Mockito.verify; 027import static org.mockito.Mockito.when; 028 029import static org.fcrepo.kernel.api.RdfLexicon.CONTAINS; 030import static org.fcrepo.kernel.api.RdfLexicon.HAS_MIXIN_TYPE; 031 032import static org.junit.Assert.assertFalse; 033import static org.junit.Assert.assertTrue; 034import static org.junit.Assert.assertEquals; 035 036import java.util.Collection; 037 038import com.hp.hpl.jena.graph.Graph; 039import com.hp.hpl.jena.graph.Node; 040 041import org.fcrepo.client.FedoraDatastream; 042import org.fcrepo.client.FedoraException; 043import org.fcrepo.client.FedoraObject; 044import org.fcrepo.client.FedoraResource; 045import org.fcrepo.client.utils.HttpHelper; 046 047import org.junit.Before; 048import org.junit.Test; 049import org.mockito.Mock; 050 051/** 052 * Object Impl test. 053 * @author escowles 054 * @since 2014-10-08 055 */ 056public class FedoraObjectImplTest { 057 058 @Mock 059 FedoraRepositoryImpl mockRepository; 060 061 @Mock 062 HttpHelper mockHelper; 063 064 private FedoraObject object; 065 066 private String objectPath = "/test/object"; 067 private String objectWithoutChildrenPath = "/test/objectWithoutChildren"; 068 private String datastreamChildPath = "/test/object/ds1"; 069 private String objectChildPath = "/test/object/obj1"; 070 private String customChildPath = "/test/object/custom1"; 071 private String repositoryURL = "http://localhost:8080/rest"; 072 073 private Node objectSubj = createURI(repositoryURL + objectPath); 074 private Node objectChildSubj = createURI(repositoryURL + objectChildPath); 075 private Node customChildSubj = createURI(repositoryURL + customChildPath); 076 private Node datastreamChildSubj = createURI(repositoryURL + datastreamChildPath); 077 private FedoraObjectImpl objectWithChildren; 078 private FedoraObjectImpl objectWithoutChildren; 079 private FedoraObject objectChild; 080 private FedoraObject customChild; 081 private FedoraDatastream datastreamChild; 082 083 private static String BINARY = "fedora:Binary"; 084 private static String CONTAINER = "fedora:Container"; 085 private static String CUSTOM = "fedora:Custom"; 086 private static String BOGUS_MIXIN = "bogus:mixin"; 087 088 @Before 089 public void setUp() throws FedoraException { 090 initMocks(this); 091 092 objectWithChildren = new FedoraObjectImpl(mockRepository, mockHelper, objectPath); 093 objectWithoutChildren = new FedoraObjectImpl(mockRepository, mockHelper, objectWithoutChildrenPath); 094 objectChild = new FedoraObjectImpl(mockRepository, mockHelper, objectChildPath); 095 customChild = new FedoraObjectImpl(mockRepository, mockHelper, customChildPath); 096 datastreamChild = new FedoraDatastreamImpl(mockRepository, mockHelper, datastreamChildPath); 097 098 final Graph graph = createDefaultGraph(); 099 graph.add( create(objectSubj, CONTAINS.asNode(), datastreamChildSubj) ); 100 graph.add( create(objectSubj, CONTAINS.asNode(), objectChildSubj) ); 101 graph.add( create(objectSubj, CONTAINS.asNode(), customChildSubj) ); 102 graph.add( create(datastreamChildSubj, HAS_MIXIN_TYPE.asNode(), createLiteral(BINARY)) ); 103 graph.add( create(objectChildSubj, HAS_MIXIN_TYPE.asNode(), createLiteral(CONTAINER)) ); 104 graph.add( create(customChildSubj, HAS_MIXIN_TYPE.asNode(), createLiteral(CUSTOM)) ); 105 objectWithChildren.setGraph( graph ); 106 objectWithoutChildren.setGraph( createDefaultGraph() ); 107 108 when(mockRepository.getRepositoryUrl()).thenReturn(repositoryURL); 109 when(mockRepository.getObject(eq(objectPath))).thenReturn(objectWithChildren); 110 when(mockRepository.getObject(eq(objectChildPath))).thenReturn(objectChild); 111 when(mockRepository.getObject(eq(customChildPath))).thenReturn(customChild); 112 when(mockRepository.getDatastream(eq(datastreamChildPath))).thenReturn(datastreamChild); 113 } 114 115 @Test 116 public void testGetChildren() throws FedoraException { 117 final Collection<FedoraResource> children = objectWithChildren.getChildren(null); 118 verify(mockRepository).getObject(objectChildPath); 119 verify(mockRepository).getObject(customChildPath); 120 verify(mockRepository).getDatastream(datastreamChildPath); 121 assertTrue( children.contains(objectChild) ); 122 assertTrue( children.contains(customChild) ); 123 assertTrue( children.contains(datastreamChild) ); 124 } 125 126 @Test 127 public void testGetChildrenObjects() throws FedoraException { 128 final Collection<FedoraResource> children = objectWithChildren.getChildren(CONTAINER); 129 verify(mockRepository).getObject(objectChildPath); 130 verify(mockRepository, never()).getObject(customChildPath); 131 verify(mockRepository, never()).getDatastream(datastreamChildPath); 132 assertTrue( children.contains(objectChild) ); 133 assertFalse( children.contains(customChild) ); 134 assertFalse( children.contains(datastreamChild) ); 135 } 136 137 @Test 138 public void testGetChildrenCustom() throws FedoraException { 139 final Collection<FedoraResource> children = objectWithChildren.getChildren(CUSTOM); 140 verify(mockRepository, never()).getObject(objectChildPath); 141 verify(mockRepository).getObject(customChildPath); 142 verify(mockRepository, never()).getDatastream(datastreamChildPath); 143 assertFalse( children.contains(objectChild) ); 144 assertTrue( children.contains(customChild) ); 145 assertFalse( children.contains(datastreamChild) ); 146 } 147 148 @Test 149 public void testGetChildrenDatastreams() throws FedoraException { 150 final Collection<FedoraResource> children = objectWithChildren.getChildren(BINARY); 151 verify(mockRepository, never()).getObject(objectChildPath); 152 verify(mockRepository, never()).getObject(customChildPath); 153 verify(mockRepository).getDatastream(datastreamChildPath); 154 assertFalse( children.contains(objectChild) ); 155 assertFalse( children.contains(customChild) ); 156 assertTrue( children.contains(datastreamChild) ); 157 } 158 159 @Test 160 public void testGetChildrenNoChildren() throws FedoraException { 161 final Collection<FedoraResource> children = objectWithoutChildren.getChildren(null); 162 verify(mockRepository, never()).getObject(anyString()); 163 verify(mockRepository, never()).getDatastream(anyString()); 164 assertEquals( 0, children.size() ); 165 } 166 167 @Test 168 public void testGetChildrenNoMatch() throws FedoraException { 169 final Collection<FedoraResource> children = objectWithChildren.getChildren(BOGUS_MIXIN); 170 verify(mockRepository, never()).getObject(anyString()); 171 verify(mockRepository, never()).getDatastream(anyString()); 172 assertEquals( 0, children.size() ); 173 } 174}