View Javadoc

1   /*
2    * Copyright 2011 The Kuali Foundation.
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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.Ignore;
24  import org.junit.Test;
25  import org.kuali.ole.BaseTestCase;
26  import org.kuali.ole.RepositoryBrowser;
27  import org.kuali.ole.RepositoryManager;
28  import org.kuali.ole.docstore.model.xmlpojo.ingest.Request;
29  import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
30  import org.kuali.ole.docstore.model.xmlpojo.ingest.Response;
31  import org.kuali.ole.docstore.model.xmlpojo.ingest.ResponseDocument;
32  import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.InstanceCollection;
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.docstore.process.BulkIngestTimeManager;
36  import org.kuali.ole.pojo.OleException;
37  import org.kuali.ole.repository.CheckoutManager;
38  import org.kuali.ole.repository.NodeHandler;
39  import org.mockito.Mock;
40  import org.mockito.Mockito;
41  import org.mockito.MockitoAnnotations;
42  import org.slf4j.Logger;
43  import org.slf4j.LoggerFactory;
44  
45  import javax.jcr.Node;
46  import javax.jcr.NodeIterator;
47  import javax.jcr.RepositoryException;
48  import javax.jcr.Session;
49  import java.io.File;
50  import java.net.URL;
51  import java.util.ArrayList;
52  import java.util.List;
53  import java.util.regex.Matcher;
54  import java.util.regex.Pattern;
55  
56  import static junit.framework.Assert.fail;
57  import static org.junit.Assert.assertEquals;
58  import static org.junit.Assert.assertNotNull;
59  
60  /**
61   * Class to test IngestNIndexHandlerService.
62   *
63   * @author Rajesh Chowdary K
64   * @created Feb 20, 2012
65   */
66  
67  public class IngestNIndexHandlerService_UT
68          extends BaseTestCase {
69  
70      private IngestNIndexHandlerService ingestNIndexHandlerService;
71      private static final Logger LOG = LoggerFactory.getLogger(IngestNIndexHandlerService_UT.class);
72  
73      public IngestNIndexHandlerService_UT() {
74          ingestNIndexHandlerService = new IngestNIndexHandlerService();
75          ingestNIndexHandlerService.setRequestHandler(new RequestHandler());
76          ingestNIndexHandlerService.setDocumentIndexer(new DocumentIndexer());
77          ingestNIndexHandlerService.setDocumentIngester(new DocumentIngester());
78      }
79  
80      /*
81       * (non-Javadoc)
82       *
83       * @see org.kuali.ole.BaseTestCase#setUp()
84       */
85  
86      @Mock
87      private RepositoryManager mockRepositoryManager;
88      @Mock
89      private Session mockSession;
90      @Mock
91      private Node mockRootNode;
92      @Mock
93      private NodeIterator mockNodeIterator;
94      @Mock
95      private Node mockChildNode;
96  
97      @Before
98      public void setUp() throws Exception {
99          super.setUp();
100         MockitoAnnotations.initMocks(this);
101         System.getProperties().put("app.environment", "local");
102         Mockito.when(mockRepositoryManager.getSession(Mockito.anyString(), Mockito.anyString())).thenReturn(mockSession);
103         Mockito.when(mockSession.getRootNode()).thenReturn(mockRootNode);
104         Mockito.when(mockRootNode.hasProperty("nodeType")).thenReturn(true);
105         Mockito.when(mockRootNode.getNodes()).thenReturn(mockNodeIterator);
106         Mockito.when(mockNodeIterator.hasNext()).thenReturn(true);
107         Mockito.when(mockNodeIterator.next()).thenReturn(mockChildNode);
108         Mockito.when(mockChildNode.getName()).thenReturn("MockFile");
109     }
110 
111     /**
112      * Method to tearDown
113      *
114      * @throws java.lang.Exception
115      */
116     @After
117     public void tearDown() throws Exception {
118     }
119 
120     /**
121      * Test method for {@link org.kuali.ole.docstore.service.IngestNIndexHandlerService#ingestNIndexRequestDocuments(java.lang.String)}.
122      */
123     @Ignore
124     @Test
125     public void testDocumentIndexer() throws Exception {
126         DocumentIndexer documentIndexer = new DocumentIndexer();
127         documentIndexer.optimizeSolr();
128         documentIndexer.optimizeSolr(true, false);
129         URL resource = getClass().getResource("/org/kuali/ole/repository/request.xml");
130         File file = new File(resource.toURI());
131         String fileContent = FileUtils.readFileToString(file);
132         RequestHandler requestHandler = new RequestHandler();
133         Request request = requestHandler.toObject(fileContent);
134         ingestNIndexHandlerService.setRepositoryManager(mockRepositoryManager);
135         Response xmlResponse = ingestNIndexHandlerService.ingestNIndexRequestDocuments(request);
136         RequestDocument requestDocument = request.getRequestDocuments().get(0);
137         documentIndexer.indexDocument(requestDocument);
138         List list = new ArrayList();
139         list.add(requestDocument);
140         documentIndexer.indexDocuments(list);
141         documentIndexer.indexDocumentsForBulk(list, true);
142         documentIndexer.rollbackIndexedData(list);
143     }
144     @Ignore
145     @Test
146     public final void testMarcDocumentIngest() {
147         try {
148             URL resource = getClass().getResource("/org/kuali/ole/repository/request.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             for (RequestDocument requestDocument : request.getRequestDocuments()) {
156                 LOG.info("Doc UUID: " + requestDocument.getUuid());
157                 if (requestDocument.getUuid() == null) {
158                     fail("Instance Document Not Ingested.");
159                 }
160             }
161         } catch (Exception e) {
162             LOG.info(e.getMessage() , e );
163             fail("Failed due to: " + e);
164         }
165 
166     }
167 
168     @Ignore
169     @Test
170     public final void testInstanceDocumentIngest() {
171         try {
172             URL resource = getClass().getResource("/org/kuali/ole/repository/requestInstance.xml");
173             File file = new File(resource.toURI());
174             String fileContent = FileUtils.readFileToString(file);
175             RequestHandler requestHandler = new RequestHandler();
176             Request request = requestHandler.toObject(fileContent);
177             Response xmlResponse = ingestNIndexHandlerService.ingestNIndexRequestDocuments(request);
178             LOG.info("xmlResponse=" + xmlResponse);
179             for (RequestDocument requestDocument : request.getRequestDocuments()) {
180                 LOG.info("Doc UUID: " + requestDocument.getUuid());
181                 if (requestDocument.getUuid() == null) {
182                     fail("Instance Document Not Ingested.");
183                 }
184             }
185         } catch (Exception e) {
186             LOG.info(e.getMessage() , e);
187             fail("Failed due to: " + e);
188         }
189     }
190     @Ignore
191     @Test
192     public final void testInstanceSourceHoldingDocumentIngest() {
193         try {
194             URL resource = getClass().getResource("/org/kuali/ole/repository/requestInstanceSourceHolding.xml");
195             File file = new File(resource.toURI());
196             String fileContent = FileUtils.readFileToString(file);
197             RequestHandler requestHandler = new RequestHandler();
198             Request request = requestHandler.toObject(fileContent);
199             Response xmlResponse = ingestNIndexHandlerService.ingestNIndexRequestDocuments(request);
200             LOG.info("xmlResponse=" + xmlResponse);
201             RepositoryBrowser repositoryBrowser = new RepositoryBrowser();
202             //               System.out.println("Repo Dump from JCR \n"+repositoryBrowser.getRepositoryDump());
203             for (RequestDocument requestDocument : request.getRequestDocuments()) {
204                 LOG.info("Doc UUID: " + requestDocument.getUuid());
205 
206                 InstanceCollection instanceCollection = (InstanceCollection) requestDocument.getLinkedRequestDocuments()
207                         .get(0).getContent()
208                         .getContentObject();
209 
210                 SourceHoldings sourceHoldings = instanceCollection.getInstance().get(0).getSourceHoldings();
211                 String sourceHoldingId = sourceHoldings.getHoldingsIdentifier();
212                 LOG.info(sourceHoldingId);
213                 CheckoutManager checkoutManager = new CheckoutManager();
214                 String sourceHoldingContent = checkoutManager.checkOut(sourceHoldingId, "mockUser", "checkout");
215                 System.out.println("sourceHoldingContent \n" + sourceHoldingContent);
216                 if (requestDocument.getUuid() == null) {
217                     fail("Instance Document Not Ingested.");
218                 }
219             }
220         } catch (Exception e) {
221             LOG.info(e.getMessage() , e);
222             fail("Failed due to: " + e);
223         }
224 
225     }
226 
227     @Ignore
228     @Test
229     public void testDublinCoreDocumentIngest() throws Exception {
230 
231         URL resource = getClass().getResource("/org/kuali/ole/repository/Bib-Bib-DublinQ-Request.xml");
232         File file = new File(resource.toURI());
233         String fileContent = FileUtils.readFileToString(file);
234         RequestHandler requestHandler = new RequestHandler();
235         Request request = requestHandler.toObject(fileContent);
236         Response xmlResponse = ingestNIndexHandlerService.ingestNIndexRequestDocuments(request);
237         LOG.info("xmlResponse=" + xmlResponse);
238         for (RequestDocument requestDocument : request.getRequestDocuments()) {
239             LOG.info("Doc UUID: " + requestDocument.getUuid());
240             if (requestDocument.getUuid() == null) {
241                 fail("Instance Document Not Ingested.");
242             }
243         }
244     }
245     @Ignore
246     @Test
247     public void testDublinUnqualifiedDocumentIngest() throws Exception {
248 
249         URL resource = getClass().getResource("/org/kuali/ole/repository/Bib-Bib-Dublin-Unqualified-Request.xml");
250         File file = new File(resource.toURI());
251         String fileContent = FileUtils.readFileToString(file);
252         RequestHandler requestHandler = new RequestHandler();
253         Request request = requestHandler.toObject(fileContent);
254         Response xmlResponse = ingestNIndexHandlerService.ingestNIndexRequestDocuments(request);
255         LOG.info("xmlResponse=" + xmlResponse);
256         for (RequestDocument requestDocument : request.getRequestDocuments()) {
257             LOG.info("Doc UUID: " + requestDocument.getUuid());
258             if (requestDocument.getUuid() == null) {
259                 fail("Instance Document Not Ingested.");
260             }
261         }
262 
263 
264     }
265 
266     /**
267      * Test method for {@link org.kuali.ole.docstore.service.IngestNIndexHandlerService#ingestNIndexRequestDocuments(java.lang.String)}.
268      */
269     @Ignore
270     @Test
271     public final void testMarcIngestForBulk() {
272         try {
273             Session session = null;
274             URL resource = getClass().getResource("/org/kuali/ole/repository/request.xml");
275             File file = new File(resource.toURI());
276             String fileContent = FileUtils.readFileToString(file);
277             RequestHandler requestHandler = new RequestHandler();
278             Request request = requestHandler.toObject(fileContent);
279             request.getRequestDocuments().get(0).getLinkedRequestDocuments().clear();
280             request.getRequestDocuments().get(1).getLinkedRequestDocuments().clear();
281             if (session == null) {
282                 session = RepositoryManager.getRepositoryManager()
283                         .getSession(request.getUser(), request.getOperation());
284             }
285             List<String> ingestedIds = ingestNIndexHandlerService.bulkIngestNIndex(request, session);
286             LOG.info("Ingested Ids : " + ingestedIds);
287             if (ingestedIds == null || ingestedIds.size() == 0) {
288                 fail("Ingest And Index Failed, Because No Documents Ingested.");
289             }
290             for (RequestDocument requestDocument : request.getRequestDocuments()) {
291                 LOG.info("Doc UUID: " + requestDocument.getUuid());
292                 if (requestDocument.getUuid() == null) {
293                     fail("Instance Document Not Ingested.");
294                 }
295             }
296         } catch (Exception e) {
297             LOG.info(e.getMessage() , e);
298             fail("Failed due to: " + e);
299         }
300     }
301     @Ignore
302     @Test
303     public final void testPatronDocumentIngest() {
304         try {
305             URL resource = getClass().getResource("/org/kuali/ole/repository/request-patron.xml");
306             File file = new File(resource.toURI());
307             String fileContent = FileUtils.readFileToString(file);
308             RequestHandler requestHandler = new RequestHandler();
309             Request request = requestHandler.toObject(fileContent);
310             String reqContent = null;
311             for (RequestDocument requestDocument : request.getRequestDocuments()) {
312                 reqContent = requestDocument.getContent().getContent();
313             }
314             Response resp = ingestNIndexHandlerService.ingestNIndexRequestDocuments(request);
315             checkIndexForPatronRecord(resp);
316             checkIngestForPatronRecord(resp, reqContent);
317         } catch (Exception e) {
318             LOG.info(e.getMessage() , e);
319             fail("Failed due to: " + e);
320         }
321     }
322 
323     private void checkIngestForPatronRecord(Response resp, String reqContent) throws OleException, RepositoryException {
324         for (ResponseDocument responseDocument : resp.getDocuments()) {
325             String uuid = responseDocument.getUuid();
326             Session session = null;
327             session = RepositoryManager.getRepositoryManager().getSession("chuntley", "bulkIngest");
328             Node patronFileNode = new NodeHandler().getNodeByUUID(session, uuid);
329             Node patronFormatNode = patronFileNode.getParent();
330             String formatName = patronFormatNode.getName();
331             assertEquals("oleml", formatName);
332             Node patronTypeNode = patronFormatNode.getParent();
333             String typeName = patronTypeNode.getName();
334             assertEquals("patron", typeName);
335             Node patronCatNode = patronTypeNode.getParent();
336             String catName = patronCatNode.getName();
337             assertEquals("security", catName);
338             Node contentNode = patronFileNode.getNode("jcr:content");
339             String content = contentNode.getProperty("jcr:data").getValue().getString();
340             assertEquals(content, reqContent);
341             RepositoryManager.getRepositoryManager().logout(session);
342         }
343 
344     }
345 
346     private void checkIndexForPatronRecord(Response resp) {
347         for (ResponseDocument responseDocument : resp.getDocuments()) {
348             String uuid = responseDocument.getUuid();
349             QueryResponse queryResponse = ServiceLocator.getIndexerService()
350                     .searchBibRecord(responseDocument.getCategory(),
351                             responseDocument.getType(),
352                             responseDocument.getFormat(), "id", uuid,
353                             "RecordNumber_search");
354             if (System.getProperty("OLE_DOCSTORE_USE_DISCOVERY").equalsIgnoreCase(Boolean.TRUE.toString())) {
355                 long numFound = queryResponse.getResults().getNumFound();
356                 assertEquals(1, numFound);
357                 List solrInstIdList = new ArrayList();
358                 solrInstIdList = (List) queryResponse.getResults().get(0).getFieldValue("RecordNumber_search");
359                 assertNotNull(solrInstIdList.get(0));
360                 Assert.assertEquals(solrInstIdList.get(0), "00100055U");
361             }
362         }
363     }
364     @Ignore
365     @Test
366     public final void testMarcIngestWithSpecialChars() {
367         List<String> bibIds = new ArrayList<String>();
368         List<String> instanceIds = new ArrayList<String>();
369         try {
370 
371             URL resource = getClass().getResource("/org/kuali/ole/repository/request.xml");
372             File file = new File(resource.toURI());
373             String input = FileUtils.readFileToString(file);
374             StringBuffer stringBuffer = new StringBuffer();
375             String regex = "Sandburg, Carl";
376             String replace = "San~X1< 9+&!5#%^,&*(2)>{6}[8]!?H";
377             Pattern pattern = Pattern.compile(regex);
378             Matcher matcher = pattern.matcher(input);
379             while (matcher.find()) {
380                 matcher.appendReplacement(stringBuffer, replace);
381             }
382             matcher.appendTail(stringBuffer);
383             String inputFile = stringBuffer.toString();
384             RequestHandler rh = new RequestHandler();
385             Request request = rh.toObject(inputFile);
386             Response response = ingestNIndexHandlerService.ingestNIndexRequestDocuments(request);
387             for (ResponseDocument resDoc : response.getDocuments()) {
388                 bibIds.add(resDoc.getUuid());
389                 for (ResponseDocument linkedDoc : resDoc.getLinkedDocuments()) {
390                     instanceIds.add(linkedDoc.getUuid());
391                 }
392             }
393             LOG.info("Bib Ids:" + bibIds);
394             LOG.info("Instance ids:" + instanceIds);
395             CheckoutManager checkoutManager = new CheckoutManager();
396             for (String bibId : bibIds) {
397                 String checkedOutContent = checkoutManager.checkOut(bibId, "mockUser", "checkout");
398                 LOG.info("Bib Id:" + bibId);
399                 LOG.info("checkedOutContent for Bibliographic " + checkedOutContent);
400             }
401             for (String instanceId : instanceIds) {
402                 String checkedOutContent = checkoutManager.checkOut(instanceId, "mockUser", "checkout");
403                 LOG.info("Instance Id:" + instanceId);
404                 LOG.info("checkedOutContent fro Instance " + checkedOutContent);
405             }
406 
407         } catch (Exception e) {
408             LOG.info(e.getMessage() , e);
409         }
410     }
411     @Ignore
412     @Test
413     public final void testLicenseOnixPLDocumentIngest() {
414         try {
415             URL resource = getClass().getResource("/org/kuali/ole/repository/request-license-onixpl.xml");
416             File file = new File(resource.toURI());
417             String fileContent = FileUtils.readFileToString(file);
418             RequestHandler requestHandler = new RequestHandler();
419             Request request = requestHandler.toObject(fileContent);
420             String reqContent = null;
421             for (RequestDocument requestDocument : request.getRequestDocuments()) {
422                 reqContent = requestDocument.getContent().getContent();
423             }
424             Response resp = ingestNIndexHandlerService.ingestNIndexRequestDocuments(request);
425             LOG.info("License Onix PL response Ststus  " + resp.getStatus());
426             for (RequestDocument requestDocument : request.getRequestDocuments()) {
427                 LOG.info("License Onix uuid: " + requestDocument.getUuid());
428                 if (requestDocument.getUuid() == null) {
429                     fail("Instance Document Not Ingested.");
430                 }
431                 CheckoutManager checkoutManager = new CheckoutManager();
432                 String checkedOutContent = checkoutManager.checkOut(requestDocument.getUuid(), "mockUser", "checkout");
433                 Assert.assertEquals(reqContent, checkedOutContent);
434             }
435             //            RepositoryBrowser repositoryBrowser = new RepositoryBrowser();
436             //           LOG.info(repositoryBrowser.getRepositoryDump());
437         } catch (Exception e) {
438             LOG.info(e.getMessage() , e);
439             fail("Failed due to: " + e);
440         }
441     }
442     @Ignore
443     @Test
444     public final void testLicensePDFDocumentIngest() {
445         try {
446             URL resource = getClass().getResource("/org/kuali/ole/repository/request-license-pdf.xml");
447             File file = new File(resource.toURI());
448             String fileContent = FileUtils.readFileToString(file);
449             RequestHandler requestHandler = new RequestHandler();
450             Request request = requestHandler.toObject(fileContent);
451             for (RequestDocument requestDocument : request.getRequestDocuments()) {
452                 String reqContent = requestDocument.getContent().getContent();
453                 LOG.info("reqContent" + reqContent);
454             }
455             Response resp = ingestNIndexHandlerService.ingestNIndexRequestDocuments(request);
456             LOG.info("License PDF response Ststus " + resp);
457             for (RequestDocument requestDocument : request.getRequestDocuments()) {
458                 LOG.info("License PDF  UUID: " + requestDocument.getUuid());
459                 if (requestDocument.getUuid() == null) {
460                     fail("Instance Document Not Ingested.");
461                 }
462                 CheckoutManager checkoutManager = new CheckoutManager();
463                 String checkedOutContent = checkoutManager.checkOut(requestDocument.getUuid(), "mockUser", "checkout");
464                 Assert.assertNotNull(checkedOutContent);
465             }
466         } catch (Exception e) {
467             LOG.info(e.getMessage() , e);
468             fail("Failed due to: " + e);
469         }
470     }
471     @Ignore
472     @Test
473     public void testBulkIngestTimeManager() {
474         BulkIngestTimeManager bulkIngestTimeManager = new BulkIngestTimeManager();
475         bulkIngestTimeManager.reset();
476         bulkIngestTimeManager.toString();
477     }
478 }