1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.docstore.service;
17
18 import junit.framework.Assert;
19 import org.apache.commons.io.FileUtils;
20 import org.apache.solr.client.solrj.response.QueryResponse;
21 import org.junit.After;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.kuali.ole.BaseTestCase;
25 import org.kuali.ole.RepositoryBrowser;
26 import org.kuali.ole.RepositoryManager;
27 import org.kuali.ole.docstore.model.xmlpojo.ingest.Request;
28 import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
29 import org.kuali.ole.docstore.model.xmlpojo.ingest.Response;
30 import org.kuali.ole.docstore.model.xmlpojo.ingest.ResponseDocument;
31 import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.InstanceCollection;
32 import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.OleHoldings;
33 import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.SourceHoldings;
34 import org.kuali.ole.docstore.model.xstream.ingest.RequestHandler;
35 import org.kuali.ole.pojo.OleException;
36 import org.kuali.ole.repository.CheckoutManager;
37 import org.kuali.ole.repository.NodeHandler;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 import javax.jcr.Node;
42 import javax.jcr.RepositoryException;
43 import javax.jcr.Session;
44 import java.io.File;
45 import java.net.URL;
46 import java.util.ArrayList;
47 import java.util.List;
48 import java.util.regex.Matcher;
49 import java.util.regex.Pattern;
50
51 import static junit.framework.Assert.fail;
52 import static org.junit.Assert.assertEquals;
53 import static org.junit.Assert.assertNotNull;
54
55
56
57
58
59
60
61 public class IngestNIndexHandlerService_UT
62 extends BaseTestCase {
63
64 private IngestNIndexHandlerService ingestNIndexHandlerService;
65 private static final Logger LOG = LoggerFactory.getLogger(IngestNIndexHandlerService_UT.class);
66
67 public IngestNIndexHandlerService_UT() {
68 ingestNIndexHandlerService = new IngestNIndexHandlerService();
69 ingestNIndexHandlerService.setRequestHandler(new RequestHandler());
70 ingestNIndexHandlerService.setDocumentIndexer(new DocumentIndexer());
71 ingestNIndexHandlerService.setDocumentIngester(new DocumentIngester());
72 }
73
74
75
76
77
78
79 @Before
80 public void setUp() throws Exception {
81 super.setUp();
82 }
83
84
85
86
87
88
89 @After
90 public void tearDown() throws Exception {
91 }
92
93
94
95
96 @Test
97 public final void testMarcDocumentIngest() {
98 try {
99 URL resource = getClass().getResource("/org/kuali/ole/repository/request.xml");
100 File file = new File(resource.toURI());
101 String fileContent = FileUtils.readFileToString(file);
102 RequestHandler requestHandler = new RequestHandler();
103 Request request = requestHandler.toObject(fileContent);
104 Response xmlResponse = ingestNIndexHandlerService.ingestNIndexRequestDocuments(request);
105 LOG.info("xmlResponse=" + xmlResponse);
106 for (RequestDocument requestDocument : request.getRequestDocuments()) {
107 LOG.info("Doc UUID: " + requestDocument.getUuid());
108 if (requestDocument.getUuid() == null) {
109 fail("Instance Document Not Ingested.");
110 }
111 }
112 }
113 catch (Exception e) {
114 LOG.info(e.getMessage());
115 fail("Failed due to: " + e);
116 }
117
118 }
119
120
121 @Test
122 public final void testInstanceDocumentIngest() {
123 try {
124 URL resource = getClass().getResource("/org/kuali/ole/repository/requestInstance.xml");
125 File file = new File(resource.toURI());
126 String fileContent = FileUtils.readFileToString(file);
127 RequestHandler requestHandler = new RequestHandler();
128 Request request = requestHandler.toObject(fileContent);
129 Response xmlResponse = ingestNIndexHandlerService.ingestNIndexRequestDocuments(request);
130 LOG.info("xmlResponse=" + xmlResponse);
131 for (RequestDocument requestDocument : request.getRequestDocuments()) {
132 LOG.info("Doc UUID: " + requestDocument.getUuid());
133 if (requestDocument.getUuid() == null) {
134 fail("Instance Document Not Ingested.");
135 }
136 }
137 }
138 catch (Exception e) {
139 LOG.info(e.getMessage());
140 fail("Failed due to: " + e);
141 }
142
143 }
144
145 @Test
146 public final void testInstanceSourceHoldingDocumentIngest() {
147 try {
148 URL resource = getClass().getResource("/org/kuali/ole/repository/requestInstanceSourceHolding.xml");
149 File file = new File(resource.toURI());
150 String fileContent = FileUtils.readFileToString(file);
151 RequestHandler requestHandler = new RequestHandler();
152 Request request = requestHandler.toObject(fileContent);
153 Response xmlResponse = ingestNIndexHandlerService.ingestNIndexRequestDocuments(request);
154 LOG.info("xmlResponse=" + xmlResponse);
155 RepositoryBrowser repositoryBrowser = new RepositoryBrowser();
156
157 for (RequestDocument requestDocument : request.getRequestDocuments()) {
158 LOG.info("Doc UUID: " + requestDocument.getUuid());
159
160 InstanceCollection instanceCollection = (InstanceCollection) requestDocument.getLinkedRequestDocuments()
161 .get(0).getContent()
162 .getContentObject();
163
164 SourceHoldings sourceHoldings = instanceCollection.getInstance().get(0).getSourceHoldings();
165 String sourceHoldingId = sourceHoldings.getHoldingsIdentifier();
166 LOG.info(sourceHoldingId);
167 CheckoutManager checkoutManager = new CheckoutManager();
168 String sourceHoldingContent = checkoutManager.checkOut(sourceHoldingId, "mockUser", "checkout");
169 System.out.println("sourceHoldingContent \n" + sourceHoldingContent);
170 if (requestDocument.getUuid() == null) {
171 fail("Instance Document Not Ingested.");
172 }
173 }
174 }
175 catch (Exception e) {
176 LOG.info(e.getMessage());
177 fail("Failed due to: " + e);
178 }
179
180 }
181
182
183 @Test
184 public void testDublinCoreDocumentIngest() throws Exception {
185
186 URL resource = getClass().getResource("/org/kuali/ole/repository/Bib-Bib-DublinQ-Request.xml");
187 File file = new File(resource.toURI());
188 String fileContent = FileUtils.readFileToString(file);
189 RequestHandler requestHandler = new RequestHandler();
190 Request request = requestHandler.toObject(fileContent);
191 Response xmlResponse = ingestNIndexHandlerService.ingestNIndexRequestDocuments(request);
192 LOG.info("xmlResponse=" + xmlResponse);
193 for (RequestDocument requestDocument : request.getRequestDocuments()) {
194 LOG.info("Doc UUID: " + requestDocument.getUuid());
195 if (requestDocument.getUuid() == null) {
196 fail("Instance Document Not Ingested.");
197 }
198 }
199 }
200
201 @Test
202 public void testDublinUnqualifiedDocumentIngest() throws Exception {
203
204 URL resource = getClass().getResource("/org/kuali/ole/repository/Bib-Bib-Dublin-Unqualified-Request.xml");
205 File file = new File(resource.toURI());
206 String fileContent = FileUtils.readFileToString(file);
207 RequestHandler requestHandler = new RequestHandler();
208 Request request = requestHandler.toObject(fileContent);
209 Response xmlResponse = ingestNIndexHandlerService.ingestNIndexRequestDocuments(request);
210 LOG.info("xmlResponse=" + xmlResponse);
211 for (RequestDocument requestDocument : request.getRequestDocuments()) {
212 LOG.info("Doc UUID: " + requestDocument.getUuid());
213 if (requestDocument.getUuid() == null) {
214 fail("Instance Document Not Ingested.");
215 }
216 }
217
218
219 }
220
221
222
223
224 @Test
225 public final void testMarcIngestForBulk() {
226 try {
227 Session session = null;
228 URL resource = getClass().getResource("/org/kuali/ole/repository/request.xml");
229 File file = new File(resource.toURI());
230 String fileContent = FileUtils.readFileToString(file);
231 RequestHandler requestHandler = new RequestHandler();
232 Request request = requestHandler.toObject(fileContent);
233 request.getRequestDocuments().get(0).getLinkedRequestDocuments().clear();
234 request.getRequestDocuments().get(1).getLinkedRequestDocuments().clear();
235 if (session == null) {
236 session = RepositoryManager.getRepositoryManager()
237 .getSession(request.getUser(), request.getOperation());
238 }
239 List<String> ingestedIds = ingestNIndexHandlerService.bulkIngestNIndex(request, session);
240 LOG.info("Ingested Ids : " + ingestedIds);
241 if (ingestedIds == null || ingestedIds.size() == 0) {
242 fail("Ingest And Index Failed, Because No Documents Ingested.");
243 }
244 for (RequestDocument requestDocument : request.getRequestDocuments()) {
245 LOG.info("Doc UUID: " + requestDocument.getUuid());
246 if (requestDocument.getUuid() == null) {
247 fail("Instance Document Not Ingested.");
248 }
249 }
250 }
251 catch (Exception e) {
252 LOG.info(e.getMessage());
253 fail("Failed due to: " + e);
254 }
255 }
256
257 @Test
258 public final void testPatronDocumentIngest() {
259 try {
260 URL resource = getClass().getResource("/org/kuali/ole/repository/request-patron.xml");
261 File file = new File(resource.toURI());
262 String fileContent = FileUtils.readFileToString(file);
263 RequestHandler requestHandler = new RequestHandler();
264 Request request = requestHandler.toObject(fileContent);
265 String reqContent = null;
266 for (RequestDocument requestDocument : request.getRequestDocuments()) {
267 reqContent = requestDocument.getContent().getContent();
268 }
269 Response resp = ingestNIndexHandlerService.ingestNIndexRequestDocuments(request);
270 checkIndexForPatronRecord(resp);
271 checkIngestForPatronRecord(resp, reqContent);
272 }
273 catch (Exception e) {
274 LOG.info(e.getMessage());
275 fail("Failed due to: " + e);
276 }
277 }
278
279 private void checkIngestForPatronRecord(Response resp, String reqContent) throws OleException, RepositoryException {
280 for (ResponseDocument responseDocument : resp.getDocuments()) {
281 String uuid = responseDocument.getUuid();
282 Session session = null;
283 session = RepositoryManager.getRepositoryManager().getSession("chuntley", "bulkIngest");
284 Node patronFileNode = new NodeHandler().getNodeByUUID(session, uuid);
285 Node patronFormatNode = patronFileNode.getParent();
286 String formatName = patronFormatNode.getName();
287 assertEquals("oleml", formatName);
288 Node patronTypeNode = patronFormatNode.getParent();
289 String typeName = patronTypeNode.getName();
290 assertEquals("patron", typeName);
291 Node patronCatNode = patronTypeNode.getParent();
292 String catName = patronCatNode.getName();
293 assertEquals("security", catName);
294 Node contentNode = patronFileNode.getNode("jcr:content");
295 String content = contentNode.getProperty("jcr:data").getValue().getString();
296 assertEquals(content, reqContent);
297 RepositoryManager.getRepositoryManager().logout(session);
298 }
299
300 }
301
302 private void checkIndexForPatronRecord(Response resp) {
303 for (ResponseDocument responseDocument : resp.getDocuments()) {
304 String uuid = responseDocument.getUuid();
305 QueryResponse queryResponse = ServiceLocator.getIndexerService()
306 .searchBibRecord(responseDocument.getCategory(),
307 responseDocument.getType(),
308 responseDocument.getFormat(), "id", uuid,
309 "RecordNumber_search");
310 if (System.getProperty("OLE_DOCSTORE_USE_DISCOVERY").equalsIgnoreCase(Boolean.TRUE.toString())) {
311 long numFound = queryResponse.getResults().getNumFound();
312 assertEquals(1, numFound);
313 List solrInstIdList = new ArrayList();
314 solrInstIdList = (List) queryResponse.getResults().get(0).getFieldValue("RecordNumber_search");
315 assertNotNull(solrInstIdList.get(0));
316 Assert.assertEquals(solrInstIdList.get(0), "00100055U");
317 }
318 }
319 }
320
321 @Test
322 public final void testMarcIngestWithSpecialChars() {
323 List<String> bibIds = new ArrayList<String>();
324 List<String> instanceIds = new ArrayList<String>();
325 try {
326
327 URL resource = getClass().getResource("/org/kuali/ole/repository/request.xml");
328 File file = new File(resource.toURI());
329 String input = FileUtils.readFileToString(file);
330 StringBuffer stringBuffer = new StringBuffer();
331 String regex = "Sandburg, Carl";
332 String replace = "San~X1< 9+&!5#%^,&*(2)>{6}[8]!?H";
333 Pattern pattern = Pattern.compile(regex);
334 Matcher matcher = pattern.matcher(input);
335 while (matcher.find()) {
336 matcher.appendReplacement(stringBuffer, replace);
337 }
338 matcher.appendTail(stringBuffer);
339 String inputFile = stringBuffer.toString();
340 RequestHandler rh = new RequestHandler();
341 Request request = rh.toObject(inputFile);
342 Response response = ingestNIndexHandlerService.ingestNIndexRequestDocuments(request);
343 for (ResponseDocument resDoc : response.getDocuments()) {
344 bibIds.add(resDoc.getUuid());
345 for (ResponseDocument linkedDoc : resDoc.getLinkedDocuments()) {
346 instanceIds.add(linkedDoc.getUuid());
347 }
348 }
349 LOG.info("Bib Ids:" + bibIds);
350 LOG.info("Instance ids:" + instanceIds);
351 CheckoutManager checkoutManager = new CheckoutManager();
352 for (String bibId : bibIds) {
353 String checkedOutContent = checkoutManager.checkOut(bibId, "mockUser", "checkout");
354 LOG.info("Bib Id:" + bibId);
355 LOG.info("checkedOutContent for Bibliographic " + checkedOutContent);
356 }
357 for (String instanceId : instanceIds) {
358 String checkedOutContent = checkoutManager.checkOut(instanceId, "mockUser", "checkout");
359 LOG.info("Instance Id:" + instanceId);
360 LOG.info("checkedOutContent fro Instance " + checkedOutContent);
361 }
362
363 }
364 catch (Exception e) {
365 LOG.info(e.getMessage());
366 }
367 }
368
369 @Test
370 public final void testLicenseOnixPLDocumentIngest() {
371 try {
372 URL resource = getClass().getResource("/org/kuali/ole/repository/request-license-onixpl.xml");
373 File file = new File(resource.toURI());
374 String fileContent = FileUtils.readFileToString(file);
375 RequestHandler requestHandler = new RequestHandler();
376 Request request = requestHandler.toObject(fileContent);
377 String reqContent = null;
378 for (RequestDocument requestDocument : request.getRequestDocuments()) {
379 reqContent = requestDocument.getContent().getContent();
380 }
381 Response resp = ingestNIndexHandlerService.ingestNIndexRequestDocuments(request);
382 LOG.info("License Onix PL response Ststus " + resp.getStatus());
383 for (RequestDocument requestDocument : request.getRequestDocuments()) {
384 LOG.info("License Onix uuid: " + requestDocument.getUuid());
385 if (requestDocument.getUuid() == null) {
386 fail("Instance Document Not Ingested.");
387 }
388 CheckoutManager checkoutManager = new CheckoutManager();
389 String checkedOutContent = checkoutManager.checkOut(requestDocument.getUuid(), "mockUser", "checkout");
390 Assert.assertEquals(reqContent, checkedOutContent);
391 }
392
393
394 }
395 catch (Exception e) {
396 LOG.info(e.getMessage());
397 fail("Failed due to: " + e);
398 }
399 }
400
401 @Test
402 public final void testLicensePDFDocumentIngest() {
403 try {
404 URL resource = getClass().getResource("/org/kuali/ole/repository/request-license-pdf.xml");
405 File file = new File(resource.toURI());
406 String fileContent = FileUtils.readFileToString(file);
407 RequestHandler requestHandler = new RequestHandler();
408 Request request = requestHandler.toObject(fileContent);
409 for (RequestDocument requestDocument : request.getRequestDocuments()) {
410 String reqContent = requestDocument.getContent().getContent();
411 LOG.info("reqContent" + reqContent);
412 }
413 Response resp = ingestNIndexHandlerService.ingestNIndexRequestDocuments(request);
414 LOG.info("License PDF response Ststus " + resp);
415 for (RequestDocument requestDocument : request.getRequestDocuments()) {
416 LOG.info("License PDF UUID: " + requestDocument.getUuid());
417 if (requestDocument.getUuid() == null) {
418 fail("Instance Document Not Ingested.");
419 }
420 CheckoutManager checkoutManager = new CheckoutManager();
421 String checkedOutContent = checkoutManager.checkOut(requestDocument.getUuid(), "mockUser", "checkout");
422 Assert.assertNotNull(checkedOutContent);
423 }
424 }
425 catch (Exception e) {
426 LOG.info(e.getMessage());
427 fail("Failed due to: " + e);
428 }
429 }
430 }