View Javadoc

1   package org.kuali.ole.docstore.discovery.service;
2   
3   import junit.framework.Assert;
4   import org.apache.solr.client.solrj.SolrQuery;
5   import org.apache.solr.client.solrj.SolrServer;
6   import org.apache.solr.client.solrj.response.QueryResponse;
7   import org.apache.solr.common.SolrDocument;
8   import org.apache.solr.common.SolrDocumentList;
9   import org.junit.After;
10  import org.junit.Before;
11  import org.junit.Ignore;
12  import org.junit.Test;
13  import org.kuali.ole.docstore.common.document.content.instance.Instance;
14  import org.kuali.ole.docstore.common.document.content.instance.InstanceCollection;
15  import org.kuali.ole.docstore.common.document.content.instance.OleHoldings;
16  import org.kuali.ole.docstore.discovery.BaseTestCase;
17  import org.kuali.ole.docstore.discovery.model.SearchCondition;
18  import org.kuali.ole.docstore.discovery.model.SearchParams;
19  import org.kuali.ole.docstore.indexer.solr.DocumentIndexerManagerFactory;
20  import org.kuali.ole.docstore.indexer.solr.IndexerService;
21  import org.kuali.ole.docstore.model.bo.*;
22  import org.kuali.ole.docstore.model.enums.DocCategory;
23  import org.kuali.ole.docstore.model.enums.DocFormat;
24  import org.kuali.ole.docstore.model.enums.DocType;
25  import org.kuali.ole.docstore.model.xmlpojo.ingest.Content;
26  import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
27  import org.kuali.ole.docstore.common.document.content.instance.Item;
28  import org.kuali.ole.docstore.model.xstream.ingest.RequestHandler;
29  import org.kuali.ole.docstore.common.document.content.instance.xstream.InstanceOlemlRecordProcessor;
30  import org.mockito.MockitoAnnotations;
31  
32  import java.io.File;
33  import java.net.URL;
34  import java.util.*;
35  
36  import static junit.framework.Assert.assertNotNull;
37  import static org.junit.Assert.assertEquals;
38  
39  /**
40   * User: tirumalesh.b
41   * Date: 16/1/12 Time: 2:58 PM
42   */
43  public class QueryService_UT extends BaseTestCase {
44  
45      private QueryService queryService = null;
46      //private IndexerService indexerService = null;
47  //    protected Server server = null;
48  
49      @Before
50      public void setUp() throws Exception {
51          super.setUp();
52  //        System.setProperty(SolrServerManager.SOLR_HOME, "../OLE-SOLR");
53          //System.setProperty(IndexerServiceImpl.SOLR_SERVER_EMBEDDED, "true");
54          queryService = ServiceLocator.getQueryService();
55          //indexerService = ServiceLocator.getIndexerService();
56          MockitoAnnotations.initMocks(this);
57          // TODO: Make sure discovery webapp is running.
58          //startJetty();
59          // http://ptrthomas.wordpress.com/2009/01/24/how-to-start-and-stop-jetty-revisited/
60          // http://docs.codehaus.org/display/JETTY/Embedding+Jetty
61          // http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty
62      }
63  
64      @After
65      public void tearDown() throws Exception {
66          //stopJetty();
67      }
68  
69  
70      public void testQueryField() throws Exception {
71          String inputFile = "/bib/bib/marc/MarcRecord.xml";
72          indexFile(DocCategory.WORK.getCode(), DocType.BIB.getDescription(), DocFormat.MARC.getCode(), inputFile);
73          Map<String, String> queryParams = new HashMap<String, String>();
74          queryParams.put("Author_search", "Test-Author");
75          SearchParams searchParams = buildSearchParams(queryParams);
76  
77          String value = queryService.queryField(searchParams, "Author_display");
78          System.out.println("value " + value);
79          assertEquals(value, "Test-Author");
80          cleanUpData();
81      }
82  
83      public void testBuildQueryForDoc() {
84          OleDocument od = new OleDocument();
85          queryService.buildQueryForDoc(od);
86      }
87  
88      public void testGetUUIDList() {
89          queryService.getUUIDList(null, null);
90  
91      }
92  
93      @Test
94      public void testGetTitleValues() {
95          queryService.getTitleValues(null);
96      }
97  
98      @Test
99      public void testQueryForDocs() throws Exception {
100         //Index sample file
101         cleanUpData();
102         String inputFile = "/bib/bib/marc/MarcRecord.xml";
103         indexFile(DocCategory.WORK.getCode(), DocType.BIB.getDescription(), DocFormat.MARC.getCode(), inputFile);
104         testForSingleRecord();
105         testForMultipleRecords();
106         //Clean up Indexed data
107         cleanUpData();
108     }
109 
110     public void searchForBibByInstance() throws Exception {
111         cleanUpData();
112         QueryResponse response;
113         String instanceRequestFile = "/work/instance/oleml/Instance-Request.xml";
114         URL resource = getClass().getResource(instanceRequestFile);
115         File file = new File(resource.toURI());
116         String instanceRequestContent = readFile(file);
117         RequestHandler requestHandler = new RequestHandler();
118         org.kuali.ole.docstore.model.xmlpojo.ingest.Request request = requestHandler.toObject(instanceRequestContent);
119         List<RequestDocument> requestDocumentList = request.getRequestDocuments();
120         for (RequestDocument requestDocument : requestDocumentList) {
121             modifyRequestDocument(requestDocument);
122             getIndexerService(requestDocument).indexDocument(requestDocument);
123             response = searchBibRecord(DocCategory.WORK.getCode(), DocType.BIB.getDescription(),
124                     DocFormat.MARC.getCode(), "id", requestDocument.getUuid());
125             SolrDocumentList solrDocumentList = response.getResults();
126             assertEquals(1, solrDocumentList.getNumFound());
127             Object title = solrDocumentList.get(0).getFieldValue("Title_display");
128             Object author = solrDocumentList.get(0).getFieldValue("Author_display");
129             List<String> instanceIdentifier = (List<String>) solrDocumentList.get(0)
130                     .getFieldValue("instanceIdentifier");
131             WorkBibDocument workBibDocument = new WorkBibDocument();
132             WorkInstanceDocument workInstanceDocument = new WorkInstanceDocument();
133             workInstanceDocument.setInstanceIdentifier(instanceIdentifier.get(0));
134             workBibDocument.setInstanceDocument(workInstanceDocument);
135             queryService.queryForDocs(workBibDocument);
136             List<OleDocument> oleDocumentsList = queryService.queryForDocs(workBibDocument);
137             for (OleDocument oleDocument : oleDocumentsList) {
138                 workBibDocument = (WorkBibDocument) oleDocument;
139                 assertEquals(workBibDocument.getTitles(), title);
140                 assertEquals(workBibDocument.getAuthors(), author);
141             }
142         }
143     }
144 
145     public SearchParams buildSearchParams(Map<String, String> queryParams) {
146         SearchParams searchParams = new SearchParams();
147         searchParams.setDocCategory(DocCategory.WORK.getCode());
148         searchParams.setDocType(DocType.BIB.getDescription());
149         searchParams.setDocFormat(DocFormat.MARC.getCode());
150         searchParams.setResultPageSize("10");
151         searchParams.setResultFromIndex("1");
152         searchParams.setResultPageIndex("1");
153 
154 
155         List<SearchCondition> searchConditionList = new ArrayList<SearchCondition>();
156         searchParams.setSearchFieldsList(searchConditionList);
157         if (null != queryParams) {
158             for (Iterator<String> keys = queryParams.keySet().iterator(); keys.hasNext(); ) {
159                 String key = keys.next();
160                 String value = queryParams.get(key);
161                 SearchCondition searchCondition = new SearchCondition();
162                 searchCondition.setDocField(key);
163                 searchCondition.setSearchScope("AND");
164                 searchCondition.setSearchText(value);
165                 if (searchConditionList.size() > 0) {
166                     searchCondition.setOperator("AND");
167                 }
168                 searchConditionList.add(searchCondition);
169             }
170         }
171         return searchParams;
172     }
173 
174     protected void indexFile(String docCategory, String docType, String docFormat, String inputRecordsFile) throws Exception {
175         URL resource = getClass().getResource(inputRecordsFile);
176         File file = new File(resource.toURI());
177         String filePath = file.getAbsolutePath();
178         String output = getIndexerService(docCategory, docType, docFormat).indexDocumentsFromFileBySolrDoc(docCategory, docType, docFormat, filePath);
179         System.out.println("indexFile output " + output);
180     }
181 
182     private List<Object> getSolrRecordsForQueryField(String fieldName, String fieldValue) throws Exception {
183         SolrDocument doc;
184         List<Object> records = new ArrayList<Object>();
185         String args = fieldName + ":" + fieldValue;
186         QueryResponse response = executeQuery(args);
187         SolrDocumentList solrDocumentList = response.getResults();
188         for (int i = 0; i < solrDocumentList.size(); i++) {
189             doc = solrDocumentList.get(i);
190             records.add(doc);
191         }
192         return records;
193     }
194 
195     private void testForSingleRecord() throws Exception {
196         //Get Solr Response
197         List<Object> solrResponse = getSolrRecordsForQueryField("Title_display", "Test-Title");
198         List<String> title = (List<String>) ((SolrDocument) solrResponse.get(0))
199                 .getFieldValue("Title_display");
200 
201         //Set values to OleDocument
202         OleDocument oleDocument = new WorkBibDocument();
203         ((WorkBibDocument) oleDocument).setTitles(title);
204         List<OleDocument> docs = queryService.queryForDocs(oleDocument);
205         assertEquals(1, docs.size());
206         System.out.println("Title original" + ((WorkBibDocument) docs.get(0)).getTitles() + "expected " + title);
207         assert (((WorkBibDocument) docs.get(0)).getTitles().equals(title));
208     }
209 
210     private void testForMultipleRecords() throws Exception {
211         //Get Solr Response
212         List<Object> solrResponse = getSolrRecordsForQueryField("Author_display", "Test-Author");
213         List<String> author = (List<String>) ((SolrDocument) solrResponse
214                 .get(0)).getFieldValue("Author_display");
215 
216         //Set values to OleDocument
217         OleDocument oleDocument = new WorkBibDocument();
218         ((WorkBibDocument) oleDocument).setAuthors(author);
219         List<OleDocument> docs = queryService.queryForDocs(oleDocument);
220         assertEquals(2, docs.size());
221         System.out.println("Author original" + ((WorkBibDocument) docs.get(0)).getAuthors() + "expected " + author);
222         assert (((WorkBibDocument) docs.get(0)).getAuthors().equals(author));
223     }
224 
225     private static int count = 0;
226 
227     public void modifyRequestDocument(RequestDocument requestDocument) {
228         count++;
229         int oleItemCount = 0;
230         String marcID = String.valueOf(Math.round(Math.random())+1);
231         String oleHoldingID = String.valueOf(Math.round(Math.random())+1);
232         String oleItemID = "";
233         String instanceID = String.valueOf(Math.round(Math.random())+1);
234         requestDocument.setUuid(marcID);
235         InstanceOlemlRecordProcessor instanceOlemlRecordProcessor = new InstanceOlemlRecordProcessor();
236         List<RequestDocument> linkedRequestDocumentsModified = new ArrayList<RequestDocument>();
237         Content content = null;
238         for (RequestDocument req : requestDocument.getLinkedRequestDocuments()) {
239             InstanceCollection ic = instanceOlemlRecordProcessor.fromXML(req.getContent().getContent());
240             List<Instance> oleInstances = ic.getInstance();
241             req.getContent().setContentObject(ic);
242             req.setUuid(instanceID);
243             for (Instance oleInstance : oleInstances) {
244                 oleInstance.setInstanceIdentifier(instanceID);
245                 OleHoldings holding = oleInstance.getOleHoldings();
246                 holding.setHoldingsIdentifier(oleHoldingID);
247                 for (Item oleItem : oleInstance.getItems().getItem()) {
248                     oleItemCount++;
249                     oleItemID = String.valueOf(Math.round(Math.random()));
250                     oleItem.setItemIdentifier(oleItemID);
251                 }
252             }
253         }
254 
255     }
256 
257     public QueryResponse searchBibRecord(String docCat, String docType, String docFormat, String fieldNameIdentifier,
258                                          String fieldValueIdentifier) throws Exception {
259         return searchRecord(docCat, docType, docFormat, fieldNameIdentifier, fieldValueIdentifier);
260     }
261 
262     public QueryResponse searchRecord(String docCat, String docType, String docFormat, String fieldName,
263                                       String fieldValue) throws Exception {
264         LOG.info("fieldNameIdentifier " + fieldName);
265         String identifier_args = "(" + fieldName + ":" + fieldValue + ")";
266         String docCategory_args = "(DocCategory" + ":" + docCat + ")";
267         String docType_args = "(DocType" + ":" + docType + ")";
268         String docFormat_args = "(DocFormat" + ":" + docFormat + ")";
269         String args = identifier_args + "AND" + docCategory_args + "AND" + docType_args + "AND" + docFormat_args;
270         SolrServer solr = SolrServerManager.getInstance().getSolrServer();
271         SolrQuery query = new SolrQuery();
272         LOG.info("args " + args);
273         query.setQuery(args);
274         QueryResponse response = solr.query(query);
275         LOG.info("response " + response);
276         return response;
277     }
278 
279     @Test
280     public void testGetBibDocuments() throws Exception {
281         SearchParams searchParams = new SearchParams();
282         searchParams.setDocCategory("work");
283         searchParams.setDocType("bibliographic");
284         searchParams.setDocFormat("marc");
285         List<SearchCondition> searchConditionList = new ArrayList<SearchCondition>();
286         SearchCondition searchCondition = new SearchCondition();
287         searchCondition.setSearchText("Carl");
288         searchCondition.setOperator("NOT");
289         searchCondition.setDocField("Author_search");
290         searchCondition.setSearchScope("OR");
291         searchConditionList.add(searchCondition);
292         searchCondition = new SearchCondition();
293         searchCondition.setSearchText("Sandburg");
294         searchCondition.setOperator("NOT");
295         searchCondition.setDocField("Author_search");
296         searchCondition.setSearchScope("OR");
297         searchConditionList.add(searchCondition);
298         searchParams.setSearchFieldsList(searchConditionList);
299         List<WorkBibDocument> workBibDocumentList = queryService.getBibDocuments(searchParams);
300         System.out.println("docs-->" + workBibDocumentList.size());
301     }
302 
303     @Test
304     public void testGetHoldingsDocuments() throws Exception {
305         SearchParams searchParams = new SearchParams();
306         searchParams.setDocCategory("work");
307         searchParams.setDocType("holdings");
308         searchParams.setDocFormat("oleml");
309         List<WorkHoldingsDocument> workHoldingsDocuments = queryService.getHoldingDocuments(searchParams);
310         System.out.println("docs-->" + workHoldingsDocuments.size());
311     }
312 
313     @Test
314     public void testGetItemDocuments() throws Exception {
315         SearchParams searchParams = new SearchParams();
316         searchParams.setDocCategory("work");
317         searchParams.setDocType("item");
318         searchParams.setDocFormat("oleml");
319         List<WorkItemDocument> workItemDocuments = queryService.getItemDocuments(searchParams);
320         System.out.println("docs-->" + workItemDocuments.size());
321     }
322 
323     @Ignore
324     @Test
325     public void testCallNumberBrowseMatchIndex() throws Exception {
326         cleanUpData();
327         String inputFile = "/sample-request.xml";
328         indexDocument(inputFile);
329         LOG.info("Record count:" + getRecordCount());
330         CallNumberBrowseParams callNumberBrowseParams = new CallNumberBrowseParams("", "LCC", "HD", 0, 0, 0, 0, 0, "item");
331         queryService.initCallNumberBrowse(callNumberBrowseParams);
332         Assert.assertEquals(23, callNumberBrowseParams.getMatchIndex());
333     }
334 
335     @Ignore
336     @Test
337     public void testBrowseCallNumbers() throws Exception {
338         cleanUpData();
339         String inputFile = "/sample-request.xml";
340         indexDocument(inputFile);
341         CallNumberBrowseParams callNumberBrowseParams = new CallNumberBrowseParams("", "LCC", "", 10, 18, 0, 0, 0, "item");
342         List<WorkBibDocument> workBibDocuments = queryService.browseCallNumbers(callNumberBrowseParams);
343         String itemCallNumber1 = workBibDocuments.get(0).getWorkInstanceDocumentList().get(0).getItemDocumentList().get(0).getCallNumber();
344         String itemCallNumber10 = workBibDocuments.get(9).getWorkInstanceDocumentList().get(0).getItemDocumentList().get(0).getCallNumber();
345         assertNotNull(workBibDocuments);
346         assertEquals("E175.5.A15 A3 2007 v.1 1996", itemCallNumber1.trim());
347         assertEquals("HQ1815.5 .A176 v.1 2006 no.2", itemCallNumber10.trim());
348     }
349 
350 
351     public void indexDocument(String inputFile) throws Exception {
352         cleanUpData();
353         QueryResponse response;
354         URL resource = getClass().getResource(inputFile);
355         File file = new File(resource.toURI());
356         String instanceRequestContent = readFile(file);
357         RequestHandler requestHandler = new RequestHandler();
358         org.kuali.ole.docstore.model.xmlpojo.ingest.Request request = requestHandler.toObject(instanceRequestContent);
359         List<RequestDocument> requestDocumentList = request.getRequestDocuments();
360         LOG.info("requestDocuments size " + requestDocumentList.size());
361         for (RequestDocument requestDocument : requestDocumentList) {
362             modifyRequestDocument(requestDocument);
363             getIndexerService(requestDocument).indexDocument(requestDocument);
364             SolrDocumentList solrDocumentList = null;
365             response = searchBibRecord(DocCategory.WORK.getCode(), DocType.BIB.getDescription(),
366                     DocFormat.MARC.getCode(), "id", requestDocument.getUuid());
367             // LOG.info("searchBibRecord : response " + response);
368             solrDocumentList = response.getResults();
369 
370            // assertEquals(1, solrDocumentList.getNumFound());
371             LOG.info("getNumFound marc " + solrDocumentList.getNumFound());
372             for (RequestDocument requestDocumentLinks : requestDocument.getLinkedRequestDocuments()) {
373                 InstanceCollection ic = (InstanceCollection) requestDocumentLinks.getContent().getContentObject();
374                 for (Instance oleInstance : ic.getInstance()) {
375                     for (Item oleItem : oleInstance.getItems().getItem()) {
376                         response = searchItemsRecord(DocCategory.WORK.getCode(), "item",
377                                 DocFormat.OLEML.getCode(), "id", oleItem.getItemIdentifier());
378                         solrDocumentList = response.getResults();
379                         LOG.info("getNumFound oleitem " + solrDocumentList.getNumFound());
380                         assertEquals(1, solrDocumentList.getNumFound());
381                     }
382                 }
383             }
384         }
385     }
386 
387     public QueryResponse searchInstanceRecord(String docCat, String docType, String docFormat, String fieldNameIdentifier,
388                                               String fieldValueIdentifier) throws Exception {
389         return searchRecord(docCat, docType, docFormat, fieldNameIdentifier, fieldValueIdentifier);
390 
391     }
392 
393     public QueryResponse searchHoldingRecord(String docCat, String docType, String docFormat, String fieldNameIdentifier,
394                                              String fieldValueIdentifier) throws Exception {
395         return searchRecord(docCat, docType, docFormat, fieldNameIdentifier, fieldValueIdentifier);
396 
397     }
398 
399     public QueryResponse searchItemsRecord(String docCat, String docType, String docFormat, String fieldNameIdentifier,
400                                            String fieldValueIdentifier) throws Exception {
401         return searchRecord(docCat, docType, docFormat, fieldNameIdentifier, fieldValueIdentifier);
402 
403     }
404 
405     @Test
406     public void testIsFieldValueExists() {
407         try {
408             cleanUpData();
409             String barcode = null;
410             String itemId = null;
411             String itemId2 = null;
412             String barCodeField = "ItemBarcode_display";
413             String instanceRequestFile = "/work/instance/oleml/Instance-Request.xml";
414             URL resource = getClass().getResource(instanceRequestFile);
415             File file = new File(resource.toURI());
416             String instanceRequestContent = readFile(file);
417             RequestHandler requestHandler = new RequestHandler();
418             org.kuali.ole.docstore.model.xmlpojo.ingest.Request request = requestHandler.toObject(instanceRequestContent);
419             List<RequestDocument> requestDocumentList = request.getRequestDocuments();
420             RequestDocument requestDocument = requestDocumentList.get(0);
421             modifyRequestDocument(requestDocument);
422 
423             // Verifying for item barcode before ingesting a new item record
424             boolean isFieldExists = queryService.isFieldValueExists(DocType.INSTANCE.getDescription(), barCodeField, "1111", null);
425             Assert.assertEquals("item barcode do not exists in solr", false, isFieldExists);
426 
427             indexRequestDocument(requestDocument, barcode, itemId);
428             assertEquals("1111", barcode);
429 
430             // Verifying for same item barcode after ingesting a item record
431             isFieldExists = queryService.isFieldValueExists(DocType.INSTANCE.getDescription(), barCodeField, barcode, null);
432             Assert.assertEquals("item barcode exists in solr", true, isFieldExists);
433 
434             // verifying for item barcode to update item with same barcode
435             isFieldExists = queryService.isFieldValueExists(DocType.INSTANCE.getDescription(), barCodeField, barcode, itemId);
436             Assert.assertEquals("updating an item with the same barcode(with out updating barcode value)", false, isFieldExists);
437 
438             requestDocument = requestDocumentList.get(1);
439             modifyRequestDocument(requestDocument);
440 
441             // Verifying for item barcode before ingesting a new item record
442             isFieldExists = queryService.isFieldValueExists(DocType.INSTANCE.getDescription(), barCodeField, "2222", null);
443             Assert.assertEquals("item barcode do not exists in solr", false, isFieldExists);
444 
445             indexRequestDocument(requestDocument, barcode, itemId2);
446             assertEquals("2222", barcode);
447 
448             // Verifying for item barcode after ingesting a item record (trying to ingest item record with existing barcode)
449             isFieldExists = queryService.isFieldValueExists(DocType.INSTANCE.getDescription(), barCodeField, "2222", null);
450             Assert.assertEquals("item barcode exists in solr", true, isFieldExists);
451 
452             // update item barcode to 1111 for the second item record and verify its updated value in solr
453             isFieldExists = queryService.isFieldValueExists(DocType.INSTANCE.getDescription(), barCodeField, "1111", itemId2);
454             Assert.assertEquals("updating an item with the other barcode value that exists with other record in solr", true, isFieldExists);
455 
456 
457         } catch (Exception e) {
458             LOG.debug("exception " + e);
459         }
460     }
461 
462     private void indexRequestDocument(RequestDocument requestDocument, String barcode, String itemId) throws Exception {
463         QueryResponse response;
464         getIndexerService(requestDocument).indexDocument(requestDocument);
465         response = searchBibRecord(DocCategory.WORK.getCode(), DocType.INSTANCE.getDescription(),
466                 DocFormat.OLEML.getCode(), "id", requestDocument.getLinkedRequestDocuments().get(0).getUuid());
467         SolrDocumentList solrDocumentList = response.getResults();
468         //assertEquals(1, solrDocumentList.getNumFound());
469         List<String> itemIdentifier = (List<String>) solrDocumentList.get(0).getFieldValue("itemIdentifier");
470         itemId = itemIdentifier.get(0);
471         response = searchBibRecord(DocCategory.WORK.getCode(), DocType.ITEM.getDescription(), DocFormat.OLEML.getCode(), "id", itemId);
472         solrDocumentList = response.getResults();
473         barcode = (String) solrDocumentList.get(0).getFieldValue("ItemBarcode_display");
474     }
475 
476     private IndexerService getIndexerService(RequestDocument requestDocument) {
477         IndexerService indexerService = DocumentIndexerManagerFactory.getInstance().getDocumentIndexManager(requestDocument.getCategory(), requestDocument.getType(), requestDocument.getFormat());
478         return indexerService;
479     }
480 
481     private IndexerService getIndexerService(String category, String type, String format) {
482         IndexerService indexerService = DocumentIndexerManagerFactory.getInstance().getDocumentIndexManager(category, type, format);
483         return indexerService;
484     }
485 
486 }