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