001package org.kuali.ole.docstore.repository;
002
003import org.apache.commons.io.FileUtils;
004import org.junit.Before;
005import org.junit.Test;
006import org.kuali.ole.BaseTestCase;
007import org.kuali.ole.RepositoryManager;
008import org.kuali.ole.docstore.model.xmlpojo.ingest.Request;
009import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
010import org.kuali.ole.docstore.model.xstream.ingest.RequestHandler;
011import org.kuali.ole.docstore.service.DocumentIndexer;
012import org.kuali.ole.docstore.service.DocumentIngester;
013import org.kuali.ole.docstore.service.IngestNIndexHandlerService;
014
015import org.mockito.Mock;
016import org.mockito.Mockito;
017import org.mockito.MockitoAnnotations;
018import org.slf4j.Logger;
019import org.slf4j.LoggerFactory;
020
021import javax.jcr.Node;
022import javax.jcr.NodeIterator;
023import javax.jcr.Session;
024import java.io.File;
025import java.util.Iterator;
026import java.util.Map;
027
028import static junit.framework.Assert.assertNotNull;
029import static org.junit.Assert.assertTrue;
030
031/**
032 * Created with IntelliJ IDEA.
033 * User: ?
034 * Date: 12/21/12
035 * Time: 5:06 PM
036 * To change this template use File | Settings | File Templates.
037 */
038public class NodeCountManager_UT extends BaseTestCase {
039
040    private static Logger logger = LoggerFactory.getLogger(NodeCountManager_UT.class);
041    private Map<String, Long> nodeCountMap = null;
042    @Mock
043    private RepositoryManager mockRepositoryManager;
044    @Mock
045    private Session mockSession;
046    @Mock
047    private Node mockRootNode;
048    @Mock
049    private NodeIterator mockNodeIterator;
050    @Mock
051    private Node mockChildNode;
052
053    @Before
054    public void setUp() throws Exception{
055        super.setUp();
056        MockitoAnnotations.initMocks(this);
057
058        Mockito.when(mockRepositoryManager.getSession(Mockito.anyString(), Mockito.anyString())).thenReturn(mockSession);
059        Mockito.when(mockSession.getRootNode()).thenReturn(mockRootNode);
060        Mockito.when(mockRootNode.hasProperty("nodeType")).thenReturn(true);
061        Mockito.when(mockRootNode.getNodes()).thenReturn(mockNodeIterator);
062        Mockito.when(mockNodeIterator.hasNext()).thenReturn(true);
063        Mockito.when(mockNodeIterator.next()).thenReturn(mockChildNode);
064        Mockito.when(mockChildNode.getName()).thenReturn("MockFile");
065    }
066
067    @Test
068    public void testUpdateNodeCount() throws Exception {
069        NodeCountManager nodeCountManager = NodeCountManager.getNodeCountManager();
070        nodeCountManager.setRepositoryManager(mockRepositoryManager);
071        CustomNodeManager customNodeManager = CustomNodeManager.getInstance();
072        nodeCountMap = nodeCountManager.generateNodeCountMap();
073        assertNotNull(nodeCountMap);
074        assertTrue(!nodeCountMap.isEmpty());
075
076        //TODO: This section isn't complete, and not sure what its doing.
077//        File inputDir = new File(this.getClass().getResource("/org/kuali/ole/repository/requestInstance.xml").toURI());
078//        String input = FileUtils.readFileToString(inputDir);
079//        RequestHandler requestHandler = new RequestHandler();
080//        Request request = requestHandler.toObject(input);
081//        IngestNIndexHandlerService ingestNIndexHandlerService = new IngestNIndexHandlerService();
082//        ingestNIndexHandlerService.setRequestHandler(requestHandler);
083//        ingestNIndexHandlerService.setDocumentIndexer(new DocumentIndexer());
084//        ingestNIndexHandlerService.setDocumentIngester(new DocumentIngester());
085//        ingestNIndexHandlerService.ingestNIndexRequestDocuments(request);
086//        RequestDocument requestDocument = request.getRequestDocuments().get(0);
087//        Session session = RepositoryManager.getRepositoryManager().getSession(requestDocument.getUser(), requestDocument.getOperation());
088//        Node node = customNodeManager.getNodeByUUID(session, requestDocument.getUuid());
089//        nodeCountManager.updateNodeCount(node, 6);
090    }
091}