1 package org.kuali.ole.docstore.service;
2
3 import org.apache.commons.io.FileUtils;
4 import org.apache.solr.client.solrj.response.QueryResponse;
5 import org.junit.After;
6 import org.junit.Before;
7 import org.junit.Test;
8 import org.kuali.ole.BaseTestCase;
9 import org.kuali.ole.docstore.discovery.service.IndexerService;
10 import org.kuali.ole.docstore.discovery.service.SolrServerManager;
11 import org.kuali.ole.docstore.discovery.util.HttpPostUtil;
12 import org.kuali.ole.docstore.model.xmlpojo.ingest.Request;
13 import org.kuali.ole.docstore.model.xmlpojo.ingest.Response;
14 import org.kuali.ole.docstore.model.xmlpojo.ingest.ResponseDocument;
15 import org.kuali.ole.docstore.model.xstream.ingest.RequestHandler;
16 import org.kuali.ole.docstore.model.xstream.ingest.ResponseHandler;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 import java.io.File;
21 import java.net.URL;
22 import java.util.ArrayList;
23 import java.util.Date;
24 import java.util.List;
25
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.fail;
28
29
30
31
32
33
34
35
36 public class DocumentIngester_AT
37 extends BaseTestCase {
38
39 private IndexerService indexerService = null;
40 public static final String FAILURE = "failure";
41 private static final Logger LOG = LoggerFactory
42 .getLogger(DocumentIngester_AT.class);
43 private IngestNIndexHandlerService ingestNIndexHandlerService = BeanLocator
44 .getIngestNIndexHandlerService();
45
46 @Before
47 public void setUp() throws Exception {
48 indexerService = ServiceLocator.getIndexerService();
49
50 }
51
52 @After
53 public void tearDown() throws Exception {
54 }
55
56 @Test
57 public void testBibBatchIngest() throws Exception {
58
59 try {
60 cleanUpData();
61 URL resource = getClass().getResource("/org/kuali/ole/repository/request.xml");
62 File file = new File(resource.toURI());
63 String fileContent = FileUtils.readFileToString(file);
64 RequestHandler requestHandler = new RequestHandler();
65 Request request = requestHandler.toObject(fileContent);
66 Response response = ingestNIndexHandlerService.ingestNIndexRequestDocuments(request);
67 ResponseHandler responseHandler = new ResponseHandler();
68 String result = responseHandler.toXML(response);
69 LOG.info("response=" + result);
70 ResponseDocument responseDocument = new ResponseDocument();
71 List<ResponseDocument> responseDocumentList = new ArrayList<ResponseDocument>();
72 responseDocumentList = response.getDocuments();
73 responseDocument = responseDocumentList.get(0);
74 String bibUuid = responseDocument.getUuid();
75 LOG.info("uuid of Bib record-->" + bibUuid);
76 assertEquals(bibUuid, responseDocumentList.get(0).getUuid());
77 String instanceUuid = responseDocumentList.get(0).getLinkedDocuments().get(0).getUuid();
78 LOG.info("instance Uuid-->" + instanceUuid);
79 QueryResponse queryResponse = indexerService
80 .searchBibRecord("work", "bibliographic", "marc", "instanceIdentifier", instanceUuid, "");
81 String id = (String) queryResponse.getResults().get(0).getFieldValue("id");
82 LOG.info("id-->" + id);
83 assertEquals(bibUuid, id);
84 LOG.info("queryResponse->" + queryResponse);
85 QueryResponse queryResponse1 = indexerService
86 .searchBibRecord("work", "instance", "oleml", "id", instanceUuid, "");
87 LOG.info("queryResponse1-->" + queryResponse1);
88
89 }
90 catch (Exception e) {
91 LOG.info(e.getMessage());
92 fail("Failed due to: " + e);
93 }
94 cleanUpData();
95
96 }
97
98 private void cleanUpData() throws Exception {
99 StringBuffer buffer = new StringBuffer();
100 buffer.append(SolrServerManager.getInstance().getSolrCoreURL());
101 buffer.append("/update/");
102 String params = "stream.body=<delete><query>*:*</query></delete>&commit=true";
103 LOG.info("delete URL-->" + buffer.toString() + params);
104 HttpPostUtil.postData(buffer.toString(), params);
105 }
106
107 protected String buildFailureMsg() {
108 return FAILURE + "-ErrorID:" + getErrorID();
109 }
110
111 protected String getErrorID() {
112 return String.valueOf(new Date().getTime());
113 }
114
115
116 }