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.kernel.impl.models; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertTrue; 022import static org.mockito.Mockito.mock; 023import static org.mockito.Mockito.when; 024 025import java.util.UUID; 026import java.util.stream.Collectors; 027import java.util.stream.Stream; 028 029import org.fcrepo.kernel.api.Transaction; 030import org.fcrepo.kernel.api.identifiers.FedoraId; 031import org.fcrepo.kernel.api.models.Binary; 032import org.fcrepo.kernel.api.models.Container; 033import org.fcrepo.kernel.api.models.ResourceFactory; 034import org.fcrepo.persistence.api.PersistentStorageSessionManager; 035import org.junit.Before; 036import org.junit.Test; 037import org.junit.runner.RunWith; 038import org.mockito.Mock; 039import org.mockito.MockitoAnnotations; 040import org.springframework.test.context.ContextConfiguration; 041import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 042 043/** 044 * @author bbpennel 045 */ 046@RunWith(SpringJUnit4ClassRunner.class) 047@ContextConfiguration("/containmentIndexTest.xml") 048public class ContainerImplTest { 049 @Mock 050 private PersistentStorageSessionManager sessionManager; 051 @Mock 052 private ResourceFactory resourceFactory; 053 @Mock 054 private Transaction transaction; 055 056 private final static String TX_ID = "transacted"; 057 058 private FedoraId fedoraId; 059 060 @Before 061 public void setup() throws Exception { 062 MockitoAnnotations.openMocks(this); 063 when(transaction.getId()).thenReturn(UUID.randomUUID().toString()); 064 when(transaction.isShortLived()).thenReturn(true); 065 fedoraId = FedoraId.create(UUID.randomUUID().toString()); 066 } 067 068 @Test 069 public void getChildren_WithChildren() { 070 final var child1 = mock(Container.class); 071 final var child2 = mock(Binary.class); 072 final var childrenStream = Stream.of(child1, child2); 073 074 when(resourceFactory.getChildren(transaction, fedoraId)).thenReturn(childrenStream); 075 076 final Container container = new ContainerImpl(fedoraId, transaction, sessionManager, resourceFactory); 077 078 final var resultStream = container.getChildren(); 079 final var childrenList = resultStream.collect(Collectors.toList()); 080 assertEquals(2, childrenList.size()); 081 082 assertTrue(childrenList.stream().anyMatch(c -> c instanceof Container)); 083 assertTrue(childrenList.stream().anyMatch(c -> c instanceof Binary)); 084 } 085}