1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole;
17
18 import org.apache.commons.io.FileUtils;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.kuali.ole.docstore.model.enums.DocCategory;
22 import org.kuali.ole.docstore.model.enums.DocFormat;
23 import org.kuali.ole.docstore.model.enums.DocType;
24 import org.kuali.ole.docstore.model.repopojo.FolderNode;
25 import org.kuali.ole.docstore.model.repopojo.RepositoryData;
26 import org.kuali.ole.docstore.model.xmlpojo.ingest.Request;
27 import org.kuali.ole.docstore.model.xmlpojo.ingest.Response;
28 import org.kuali.ole.docstore.model.xstream.ingest.RequestHandler;
29 import org.kuali.ole.docstore.service.BeanLocator;
30 import org.kuali.ole.docstore.service.IngestNIndexHandlerService;
31 import org.kuali.ole.pojo.OleException;
32 import org.mockito.MockitoAnnotations;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import javax.jcr.Node;
37 import javax.jcr.RepositoryException;
38 import javax.jcr.Session;
39 import java.io.File;
40 import java.net.URL;
41 import java.util.Iterator;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.Set;
45
46 import static junit.framework.Assert.assertNotNull;
47 import static org.junit.Assert.assertTrue;
48
49
50
51
52
53
54
55
56 public class RepositoryBrowser_UT
57 extends BaseTestCase {
58
59 private static final Logger LOG = LoggerFactory
60 .getLogger(RepositoryBrowser_UT.class);
61 private IngestNIndexHandlerService ingestNIndexHandlerService = BeanLocator
62 .getIngestNIndexHandlerService();
63
64 @Before
65 public void setUp() throws Exception {
66 super.setUp();
67 MockitoAnnotations.initMocks(this);
68 }
69
70 @Test
71 public void testBrowseRepository() throws Exception {
72 RepositoryBrowser repositoryBrowser = new RepositoryBrowser();
73 List<OleDocStoreData> oleDocStoreDatas = repositoryBrowser.browseDataSetup();
74 for (Iterator<OleDocStoreData> iterator = oleDocStoreDatas.iterator(); iterator.hasNext(); ) {
75 OleDocStoreData oleDocStoreData = iterator.next();
76 LOG.info(oleDocStoreData.getCategory());
77 Map<String, List<String>> typeFormatMap = oleDocStoreData.getTypeFormatMap();
78 Set<String> keys = typeFormatMap.keySet();
79 for (Iterator<String> stringIterator = keys.iterator(); stringIterator.hasNext(); ) {
80 String docType = stringIterator.next();
81 LOG.info(docType + ": " + typeFormatMap.get(docType));
82 }
83 }
84 }
85
86 @Test
87 public void testGetRandomUUIDs() throws Exception {
88 URL url = this.getClass().getResource("repository/request.xml");
89 File file = new File(url.toURI());
90
91 String docStoreDocuments = ingestNIndexHandlerService.ingestNIndexRequestDocuments(readFile(file));
92 assertTrue(!docStoreDocuments.isEmpty());
93 RepositoryBrowser repositoryBrowser = new RepositoryBrowser();
94 List<String> uuiDs = repositoryBrowser
95 .getUUIDs(DocCategory.WORK.getCode(), DocType.BIB.getDescription(), DocFormat.MARC.getCode(), 1);
96 assertTrue(!uuiDs.isEmpty());
97 for (Iterator<String> iterator = uuiDs.iterator(); iterator.hasNext(); ) {
98 String uuid = iterator.next();
99 LOG.info(uuid);
100 }
101 }
102
103 @Test
104 public void testRepositoryNodeCount() throws Exception {
105
106
107
108
109
110
111
112 RepositoryBrowser repositoryBrowser = new RepositoryBrowser();
113 List<OleDocStoreData> oleDocStoreDataList = repositoryBrowser.getFilesCount();
114 assertNotNull(oleDocStoreDataList);
115 for (Iterator<OleDocStoreData> iterator = oleDocStoreDataList.iterator(); iterator.hasNext(); ) {
116 OleDocStoreData oleDocStoreData = iterator.next();
117 Map<String, Map<String, Long>> typeFormatMapWithNodeCount = oleDocStoreData.getTypeFormatMapWithNodeCount();
118 for (Iterator<String> oleDocStoreDataIterator = typeFormatMapWithNodeCount.keySet()
119 .iterator(); oleDocStoreDataIterator
120 .hasNext(); ) {
121 String key = oleDocStoreDataIterator.next();
122 LOG.info(key + ": " + typeFormatMapWithNodeCount.get(key));
123 }
124 }
125 }
126
127 @Test
128 public void repoDump() throws RepositoryException, OleException {
129 RepositoryBrowser repositoryBrowser = new RepositoryBrowser();
130 String dump = repositoryBrowser.getRepositoryDump();
131 LOG.info("dump" + dump);
132 }
133
134 @Test
135 public void getRepositoryDumpRange() throws Exception {
136 RepositoryBrowser repositoryBrowser = new RepositoryBrowser();
137 String category = DocCategory.WORK.getCode();
138 String type = DocType.BIB.getDescription();
139 String format = DocFormat.MARC.getCode();
140 int fromIndex = 1;
141 int Count = 1;
142 cleanRepository(category, type);
143 String beforedump = repositoryBrowser.getRepositoryDump();
144 LOG.info("beforedump" + beforedump);
145 loadBulkMarcRecords(DocCategory.WORK.getCode(), DocType.BIB.getDescription(), DocFormat.MARC.getCode(),
146 "/org/kuali/ole/bulkhandler/OLE-Bib-bulkIngest-IU-Set1-split.xml");
147 String dump = repositoryBrowser.getRepositoryDump();
148 LOG.info("dump" + dump);
149 String dumpRange = repositoryBrowser.getRepositoryRangeDump(category, type, format, fromIndex, Count);
150 LOG.info("dumpRange" + dumpRange);
151 }
152
153 public void cleanRepository(String category, String type) throws OleException, RepositoryException {
154 RepositoryManager repositoryManager = RepositoryManager.getRepositoryManager();
155 Session session = repositoryManager.getSession("mockUser", "test");
156 Node rootNode = session.getRootNode();
157 Node catNode = rootNode.getNode(category);
158 Node typeNode = catNode.getNode(type);
159 for (Iterator<Node> typeiterator = typeNode.getNodes(); typeiterator.hasNext(); ) {
160 Node formatNode = typeiterator.next();
161 if (!formatNode.getName().equals("jcr:system")) {
162 formatNode.remove();
163 }
164 }
165 session.save();
166 repositoryManager.logout(session);
167 }
168
169 private void loadBulkMarcRecords(String category, String type, String format, String inputXml) throws Exception {
170 File file = new File(getClass().getResource("/org/kuali/ole/repository/request.xml").toURI());
171 String input = FileUtils.readFileToString(file);
172 RequestHandler rh = new RequestHandler();
173 Request request = rh.toObject(input);
174 Response response = ingestNIndexHandlerService.ingestNIndexRequestDocuments(request);
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189 }
190
191 @Test
192 public void loadRepository() throws OleException, RepositoryException {
193 RepositoryManager oleRepositoryManager = RepositoryManager.getRepositoryManager();
194 RepositoryData repositoryData = new RepositoryData();
195 RepositoryBrowser repositoryBrowser = new RepositoryBrowser();
196 FolderNode folderNode = repositoryBrowser
197 .computeNodeCount(oleRepositoryManager.getSession().getRootNode(), repositoryData);
198 String repoDump = repositoryData.getRepositoryDump(repositoryData);
199 LOG.info("repoDump" + repoDump);
200 }
201
202 }