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