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