View Javadoc
1   package org.kuali.ole.docstore.service;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.junit.After;
5   import org.junit.Before;
6   import org.junit.Ignore;
7   import org.junit.Test;
8   import org.kuali.ole.BaseTestCase;
9   import org.kuali.ole.RepositoryManager;
10  import org.kuali.ole.docstore.OleDocStoreException;
11  import org.kuali.ole.docstore.discovery.service.ServiceLocator;
12  import org.kuali.ole.docstore.model.enums.DocCategory;
13  import org.kuali.ole.docstore.model.enums.DocFormat;
14  import org.kuali.ole.docstore.model.enums.DocType;
15  import org.kuali.ole.docstore.model.xmlpojo.ingest.*;
16  import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.WorkBibMarcRecord;
17  import org.kuali.ole.docstore.model.xstream.ingest.RequestHandler;
18  import org.kuali.ole.docstore.model.xstream.ingest.ResponseHandler;
19  import org.kuali.ole.docstore.model.xstream.work.bib.marc.WorkBibMarcRecordProcessor;
20  import org.kuali.ole.docstore.process.batch.BulkProcessRequest;
21  import org.kuali.ole.docstore.service.impl.OleWebServiceProviderImpl;
22  import org.kuali.ole.docstore.transaction.TransactionManager;
23  import org.kuali.ole.docstore.utility.BatchIngestStatistics;
24  import org.kuali.ole.docstore.utility.BulkIngestStatistics;
25  import org.kuali.ole.docstore.utility.FileIngestStatistics;
26  import org.kuali.ole.pojo.OleException;
27  import org.kuali.ole.repository.NodeHandler;
28  import org.kuali.rice.core.api.config.property.ConfigContext;
29  import org.kuali.rice.krad.service.BusinessObjectService;
30  import org.mockito.Mock;
31  import org.mockito.Mockito;
32  import org.mockito.MockitoAnnotations;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  import javax.jcr.Node;
37  import javax.jcr.Session;
38  import java.io.BufferedReader;
39  import java.io.File;
40  import java.io.FileReader;
41  import java.io.IOException;
42  import java.net.URISyntaxException;
43  import java.net.URL;
44  import java.util.ArrayList;
45  import java.util.List;
46  
47  import static org.junit.Assert.assertEquals;
48  import static org.junit.Assert.assertNotNull;
49  import static org.junit.Assert.assertNull;
50  
51  /**
52   * Created with IntelliJ IDEA.
53   * User: ?
54   * Date: 1/8/13
55   * Time: 12:17 PM
56   * To change this template use File | Settings | File Templates.
57   */
58  @Ignore
59  @Deprecated
60  public class DocumentServiceImpl_UT
61          extends BaseTestCase {
62      private Response response = null;
63      private Logger logger = LoggerFactory.getLogger(DocumentServiceImpl_UT.class);
64      @Mock
65      private TransactionManager mockTransactionManager;
66      @Mock
67      private BusinessObjectService mockBusinessObjectService;
68  
69  
70      @Before
71      public void setUp() throws Exception{
72          super.setUp();
73          MockitoAnnotations.initMocks(this);
74      }
75  
76  
77      @After
78      public void tearDown() throws Exception {
79      }
80  
81      @Test
82      public void testProcessForIngest() throws Exception {
83          DocumentServiceImpl d = BeanLocator.getDocumentServiceImpl();
84          Request request = getRequestObjectIngest();
85          logger.debug("req xml:" + new RequestHandler().toXML(request));
86          Response response = d.process(request);
87          //logger.debug("uuid-->" + response.getDocuments().get(0).getUuid());
88          logger.debug("response " + response);
89          String responseString = new ResponseHandler().toXML(response);
90          logger.debug("responseString " + responseString);
91      }
92  
93  
94      @Test
95      public void testProcessForCheckin() throws Exception {
96          String existingUUIDToCheckIn = null;
97          Request request1 = getRequestObjectIngest();
98          request1.setOperation("ingest");
99          DocumentServiceImpl d = BeanLocator.getDocumentServiceImpl();
100         d.setTransactionManager(mockTransactionManager);
101         Response response1 = d.process(request1);
102        // existingUUIDToCheckIn = response1.getDocuments().get(0).getUuid();
103         String path = "/org/kuali/ole/repository/checkInRequest.xml";
104         URL resource = getClass().getResource(path);
105         File file = new File(resource.toURI());
106         RequestHandler requestHandler = new RequestHandler();
107         Request request = requestHandler.toObject(readFile(file));
108         //request.getRequestDocuments().get(0).setId(existingUUIDToCheckIn);
109         Response response = d.process(request);
110         logger.debug("response " + response);
111         String responseString = new ResponseHandler().toXML(response);
112         logger.debug("responseString " + responseString);
113         List<RequestDocument> requestDocuments = request.getRequestDocuments();
114         for (RequestDocument rd : requestDocuments) {
115             if ("bibliographic".equalsIgnoreCase(rd.getType()) && "marc".equalsIgnoreCase(rd.getFormat())) {
116                 WorkBibMarcRecordProcessor recordProcessor = new WorkBibMarcRecordProcessor();
117                 WorkBibMarcRecord workBibMarcRecord = recordProcessor.fromXML(rd.getContent().getContent()).getRecords()
118                         .get(0);
119                 System.out
120                         .println("getCode " + workBibMarcRecord.getDataFields().get(0).getSubFields().get(0).getCode());
121                 System.out
122                         .println("getValue " + workBibMarcRecord.getDataFields().get(0).getSubFields().get(0).getValue());
123                 for (ResponseDocument responseDocument : response.getDocuments()) {
124                     if ("bibliographic".equalsIgnoreCase(responseDocument.getType()) && "marc"
125                             .equalsIgnoreCase(responseDocument.getFormat())) {
126                         rd.setUuid(responseDocument.getUuid());
127                     }
128                 }
129                 logger.debug("getUuid " + rd.getUuid());
130             }
131 
132         }
133     }
134 
135     @Test
136     public void testProcessForCheckOut() throws Exception {
137         DocumentServiceImpl d = BeanLocator.getDocumentServiceImpl();
138         Request request = getRequestObjectIngest();
139         Response responseIngest = d.process(request);
140         String responseString = new ResponseHandler().toXML(responseIngest);
141         logger.debug("responseString Ingest " + responseString);
142         String marcUUID = responseIngest.getDocuments().get(0).getUuid();
143         String instanceUUID = responseIngest.getDocuments().get(1).getUuid();
144         String dublinUUID = responseIngest.getDocuments().get(2).getUuid();
145         String dublinUnqUUID = responseIngest.getDocuments().get(3).getUuid();
146 
147 
148         //Checkout Marc
149         request = getRequest(marcUUID, null, null, DocFormat.MARC.getDescription());
150         Response response = d.process(request);
151         responseString = new ResponseHandler().toXML(response);
152         logger.debug("checkOut content of Marc " + responseString);
153 
154         //heckout Instance
155         request = getRequest(instanceUUID, null, null, DocFormat.OLEML.getDescription());
156         response = d.process(request);
157         responseString = new ResponseHandler().toXML(response);
158         logger.debug("checkOut content of Instance " + responseString);
159 
160 
161         //Checkout Dublin
162         request = getRequest(dublinUUID, null, null, DocFormat.DUBLIN_CORE.getDescription());
163         response = d.process(request);
164         responseString = new ResponseHandler().toXML(response);
165         logger.debug("checkOut content of Dublin " + responseString);
166 
167         //Checkout Dublin Unq
168         request = getRequest(dublinUnqUUID, null, null, DocFormat.DUBLIN_UNQUALIFIED.getDescription());
169         response = d.process(request);
170         responseString = new ResponseHandler().toXML(response);
171         logger.debug("checkOut content of Dublin Unq " + responseString);
172 
173         //Ivalid MARC UUID checkout
174         try {
175             request = getRequest("111", null, null, DocFormat.MARC.getDescription());
176         } catch (Exception e) {
177             logger.debug("Invalid UUID" , e );
178         }
179 
180 
181         //Ivalid Dublin UUID checkout
182         try {
183             request = getRequest("112", null, null, DocFormat.DUBLIN_CORE.getDescription());
184         } catch (Exception e) {
185             logger.debug("Invalid UUID" , e);
186         }
187 
188         //Ivalid Dublin Unq UUID checkout
189         try {
190             request = getRequest("113", null, null, DocFormat.DUBLIN_UNQUALIFIED.getDescription());
191             request = getRequest("114", null, null, DocFormat.OLEML.getDescription());
192         } catch (Exception e) {
193             logger.debug("Invalid UUID" , e);
194         }
195 
196         //Ivalid OLEML UUID checkout
197         try {
198             request = getRequest("114", null, null, DocFormat.OLEML.getDescription());
199         } catch (Exception e) {
200             logger.debug("Invalid UUID" , e);
201         }
202     }
203 
204     private Request getRequest(String uuid, String userId, String action, String docFormat)
205             throws OleDocStoreException, OleException {
206 
207         Node nodeByUUID = null;
208         Session session = null;
209         Request req = new Request();
210         if (action == null || "".equalsIgnoreCase(action)) {
211             action = "checkOut";
212         }
213         if (userId == null || "".equalsIgnoreCase(action)) {
214             userId = "checkOutUser";
215         }
216         try {
217             session = RepositoryManager.getRepositoryManager().getSession(userId, action);
218         } catch (Exception e) {
219             logger.debug("Exception while creating a session" + e );
220             throw new OleDocStoreException(e.getMessage(), e);
221         }
222         String cat = null;
223         String type = null;
224         String format = null;
225         try {
226             NodeHandler nodeHandler = new NodeHandler();
227             nodeByUUID = nodeHandler.getNodeByUUID(session, uuid);
228             String nodePath = nodeByUUID.getPath();
229             String[] splitLine = nodePath.split("/");
230 
231             if (splitLine != null && splitLine.length >= 4) {
232                 cat = splitLine[1];
233                 type = splitLine[2];
234                 format = splitLine[3];
235             } else {
236                 if (docFormat.equalsIgnoreCase(DocFormat.MARC.getDescription())) {
237                     throw new OleDocStoreException(" This is not a valid UUID ");
238                 } else if (docFormat.equalsIgnoreCase(DocFormat.DUBLIN_CORE.getDescription())) {
239                     throw new OleDocStoreException("Invalid UUID", new Throwable(""));
240                 } else if (docFormat.equalsIgnoreCase(DocFormat.DUBLIN_UNQUALIFIED.getDescription())) {
241                     throw new OleDocStoreException(new Throwable(""));
242                 } else {
243                     throw new OleDocStoreException();
244                 }
245             }
246             if (docFormat == null || "".equalsIgnoreCase(docFormat)) {
247                 docFormat = format;
248             }
249             req.setUser(userId);
250             req.setOperation(action);
251             List<RequestDocument> reqDocList = new ArrayList<RequestDocument>();
252             RequestDocument reqDoc = buildRequest(cat, type, format, uuid);
253             reqDocList.add(reqDoc);
254             req.setRequestDocuments(reqDocList);
255         } catch (Exception e) {
256             logger.debug(e.getMessage() ,  e );
257             throw new OleDocStoreException(e);
258         } finally {
259             session.logout();
260         }
261         return req;
262     }
263 
264     private RequestDocument buildRequest(String cat, String type, String format, String uuid) {
265         RequestDocument reqDoc = new RequestDocument();
266         reqDoc.setCategory(cat);
267         reqDoc.setType(type);
268         reqDoc.setFormat(format);
269         reqDoc.setUuid(uuid);
270         return reqDoc;
271 
272     }
273 
274 
275     public void testProcessForDelete() throws Exception {
276         DocumentServiceImpl d = BeanLocator.getDocumentServiceImpl();
277         Request request = getRequestObjectIngest();
278         Response responseIngest = d.process(request);
279         String responseString = new ResponseHandler().toXML(responseIngest);
280         logger.debug("Ingest Response" + responseString);
281         String marcUUID = responseIngest.getDocuments().get(0).getUuid();
282         String instanceUUID = responseIngest.getDocuments().get(1).getUuid();
283         String dublinUUID = responseIngest.getDocuments().get(2).getUuid();
284         String dublinUnqUUID = responseIngest.getDocuments().get(3).getUuid();
285 
286         String UUIDsToDelete = marcUUID + "," + instanceUUID + "," + dublinUUID + "," + dublinUnqUUID;
287         request = buildRequestForDelete(marcUUID, "", "delete");
288         logger.debug("delete req xml:" + new RequestHandler().toXML(request));
289         logger.debug("request for delete:" + request);
290         response = d.process(request);
291 
292         responseString = new ResponseHandler().toXML(response);
293         logger.debug("response:" + responseString);
294 
295     }
296 
297 
298     private Request buildRequestForDelete(String ids, String identifierType, String operation) throws Exception {
299         String[] Id = ids.split(",");
300         List<String> idList = new ArrayList<String>();
301         List<String> uuidList = new ArrayList<String>();
302         Request request = null;
303         for (int i = 0; i < Id.length; i++) {
304             idList.add(Id[i]);
305             logger.debug("adding -->" + idList);
306         }
307         if ((!StringUtils.isBlank(identifierType)) && (identifierType.equalsIgnoreCase("SCN") || identifierType
308                 .equalsIgnoreCase("ISBN"))) {
309             uuidList = ServiceLocator.getQueryService().getUUIDList(idList, identifierType);
310         } else {
311             uuidList = idList;
312         }
313         request = identifyDeleteableDocuments(uuidList, operation);
314         return request;
315     }
316 
317     public Request identifyDeleteableDocuments(List<String> uuidsList, String operation) throws Exception {
318         Request dsRequest = new Request();
319         dsRequest.setUser("ole-khuntley");
320         dsRequest.setOperation(operation);
321 
322         List<RequestDocument> requestDocuments = null;
323         Response response = null;
324         String uuidsNotInOle = null;
325         String serviceURL = null;
326         StringBuilder uuidsSB = null;
327         // Build a csv of UUIDs of the documents to be deleted.
328         uuidsSB = new StringBuilder();
329         for (String uuid : uuidsList) {
330             uuidsSB.append(uuid).append(",");
331         }
332         serviceURL = ConfigContext.getCurrentContextConfig().getProperty("uuidCheckServiceURL");
333         logger.debug(" uuidCheckServiceURL --------> " + serviceURL);
334         //        uuidsNotInOle = uuidsSB.substring(0, uuidsSB.length() - 1);
335 
336         OleWebServiceProvider oleWebServiceProvider = new OleWebServiceProviderImpl();
337 
338         OleUuidCheckWebService oleUuidCheckWebService = (OleUuidCheckWebService) oleWebServiceProvider
339                 .getService("org.kuali.ole.service.OleUuidCheckWebService", "oleUuidCheckWebService", serviceURL);
340 
341         try {
342             uuidsNotInOle = oleUuidCheckWebService.checkUuidExsistence(uuidsSB.substring(0, uuidsSB.length() - 1));
343         } catch (Exception e) {
344             logger.debug("Unable to connect OLE Server" , e);
345         }
346 
347         logger.debug("response uuids from OLE " + uuidsNotInOle);
348         // If the UUIDs do not exist in OLE, delete them from docstore.
349         if ((uuidsNotInOle != null) && (uuidsNotInOle.length() > 0)) {
350             String[] uuids = StringUtils.split(uuidsNotInOle, ",");
351             requestDocuments = new ArrayList<RequestDocument>();
352             for (String id : uuids) {
353                 RequestDocument requestDocument = new RequestDocument();
354                 requestDocument.setCategory(DocCategory.WORK.getCode());
355                 requestDocument.setFormat(DocFormat.MARC.getCode());
356                 requestDocument.setType(DocType.BIB.getDescription());
357                 requestDocument.setUuid(id);
358                 requestDocument.setOperation(dsRequest.getOperation());
359                 requestDocuments.add(requestDocument);
360             }
361             dsRequest.setRequestDocuments(requestDocuments);
362         }
363         return dsRequest;
364     }
365 
366     private Request getRequestObjectIngest() throws URISyntaxException, IOException {
367         String path = "/org/kuali/ole/repository/request-new.xml";
368         URL resource = getClass().getResource(path);
369         File file = new File(resource.toURI());
370         RequestHandler requestHandler = new RequestHandler();
371         Request request = requestHandler.toObject(readFile(file));
372         return request;
373     }
374 
375     @Test
376     public void testBibStatus() throws Exception {
377         try {
378             String path = "/org/kuali/ole/repository/IngestRequestWithAdditionalAttributes.xml";
379             Request request = getRequestObject(path);
380             //Ingesting new record
381             AdditionalAttributes additionalAttributes = new AdditionalAttributes();
382             request.getRequestDocuments().get(0).setAdditionalAttributes(additionalAttributes);
383             String reqContent = new RequestHandler().toXML(request);
384             logger.debug("req content:" + reqContent);
385             Response response = DocumentServiceImpl.getInstance().process(request);
386             String responseContent = new ResponseHandler().toXML(response);
387             logger.debug("responseContent:" + responseContent);
388             String marcUUID = response.getDocuments().get(0).getUuid();
389             String instanceUUID = response.getDocuments().get(0).getLinkedDocuments().get(0).getUuid();
390             //            logger.debug("testMarcAdditionalAttributes response " + new ResponseHandler().toXML(response));
391             additionalAttributes = response.getDocuments().get(0).getAdditionalAttributes();
392             assertNotNull(additionalAttributes.getAttribute(AdditionalAttributes.DATE_ENTERED));
393             assertNotNull(additionalAttributes.getAttribute(AdditionalAttributes.CREATED_BY));
394             assertNull(additionalAttributes.getAttribute(AdditionalAttributes.LAST_UPDATED));
395             assertNull(additionalAttributes.getAttribute(AdditionalAttributes.UPDATED_BY));
396             assertNull(additionalAttributes.getAttribute(AdditionalAttributes.STATUS_UPDATED_BY));
397             assertNull(additionalAttributes.getAttribute(AdditionalAttributes.STATUS_UPDATED_ON));
398 
399             //Updating marc record fields other than status field
400             request.getRequestDocuments().get(0).setAdditionalAttributes(additionalAttributes);
401             request.setOperation("checkIn");
402             request.getRequestDocuments().get(0).setId(marcUUID);
403             request.getRequestDocuments().get(0).getLinkedRequestDocuments().get(0).setId(instanceUUID);
404             response = DocumentServiceImpl.getInstance().process(request);
405             responseContent = new ResponseHandler().toXML(response);
406             logger.debug("responseContent:" + responseContent);
407             additionalAttributes = response.getDocuments().get(0).getAdditionalAttributes();
408             assertNotNull(additionalAttributes.getAttribute(AdditionalAttributes.DATE_ENTERED));
409             assertNotNull(additionalAttributes.getAttribute(AdditionalAttributes.CREATED_BY));
410             assertNotNull(additionalAttributes.getAttribute(AdditionalAttributes.LAST_UPDATED));
411             assertNotNull(additionalAttributes.getAttribute(AdditionalAttributes.UPDATED_BY));
412             assertNull(additionalAttributes.getAttribute(AdditionalAttributes.STATUS_UPDATED_BY));
413             assertNull(additionalAttributes.getAttribute(AdditionalAttributes.STATUS_UPDATED_ON));
414 
415 
416             //Updating marc record after changing status field
417             additionalAttributes.setAttribute(AdditionalAttributes.STATUS, "None");
418             request.getRequestDocuments().get(0).setAdditionalAttributes(additionalAttributes);
419             request.setOperation("checkIn");
420             request.getRequestDocuments().get(0).setId(marcUUID);
421             request.getRequestDocuments().get(0).getLinkedRequestDocuments().get(0).setId(instanceUUID);
422             response = DocumentServiceImpl.getInstance().process(request);
423             additionalAttributes = response.getDocuments().get(0).getAdditionalAttributes();
424             assertNotNull(additionalAttributes.getAttribute(AdditionalAttributes.DATE_ENTERED));
425             assertNotNull(additionalAttributes.getAttribute(AdditionalAttributes.CREATED_BY));
426             assertNotNull(additionalAttributes.getAttribute(AdditionalAttributes.LAST_UPDATED));
427             assertNotNull(additionalAttributes.getAttribute(AdditionalAttributes.UPDATED_BY));
428             assertNotNull(additionalAttributes.getAttribute(AdditionalAttributes.STATUS_UPDATED_BY));
429             assertNotNull(additionalAttributes.getAttribute(AdditionalAttributes.STATUS_UPDATED_ON));
430             assertEquals("None", additionalAttributes.getAttribute(AdditionalAttributes.STATUS));
431             assertEquals("ole-khuntley", additionalAttributes.getAttribute(AdditionalAttributes.STATUS_UPDATED_BY));
432             responseContent = new ResponseHandler().toXML(response);
433             logger.debug("responseContent:" + responseContent);
434         } catch (Exception e) {
435             logger.error("Exception:" + e.getMessage(), e);
436         }
437 
438     }
439 
440     @Test
441     public void testIngestProcessWithMarcAdditionalAttributes() {
442         try {
443             String path = "/org/kuali/ole/repository/IngestRequestWithAdditionalAttributes.xml";
444             Request request = getRequestObject(path);
445 
446             String reqContent = new RequestHandler().toXML(request);
447             logger.debug("req content:" + reqContent);
448             Request request1 = new RequestHandler().toObject(reqContent);
449             logger.debug("request xml:" + new RequestHandler().toXML(request1));
450 
451             Response response = DocumentServiceImpl.getInstance().process(request);
452             logger.debug("testMarcAdditionalAttributes response " + new ResponseHandler().toXML(response));
453             String responseContent = new ResponseHandler().toXML(response);
454             logger.debug("responseContent:" + responseContent);
455         } catch (Exception e) {
456             logger.error(e.getMessage() ,  e );
457         }
458 
459     }
460 
461     @Test
462     public void testCheckInProcessWithMarcAdditionalAttributes() throws Exception {
463         String path = "/org/kuali/ole/repository/IngestRequestWithAdditionalAttributes.xml";
464         Request requestIngest = getRequestObject(path);
465 
466         Response responseIngest = DocumentServiceImpl.getInstance().process(requestIngest);
467         logger.debug("Ingest response--------- " + new ResponseHandler().toXML(responseIngest));
468         String marcUUID = responseIngest.getDocuments().get(0).getUuid();
469         path = "/org/kuali/ole/repository/checkInRequestWithAdditionalAttributes.xml";
470         Request request = getRequestObject(path);
471         request.getRequestDocuments().get(0).setId(marcUUID);
472 
473         logger.debug("uuid to update:" + request.getRequestDocuments().get(0).getId());
474         Response response = DocumentServiceImpl.getInstance().process(request);
475 
476         logger.debug("CheckIn response " + new ResponseHandler().toXML(response));
477     }
478 
479     @Test
480     public void testCheckOutProcessWithMarcAdditionalAttributes() throws Exception {
481         DocumentServiceImpl documentService = BeanLocator.getDocumentServiceImpl();
482         String path = "/org/kuali/ole/repository/IngestRequestWithAdditionalAttributes.xml";
483         Request requestIngest = getRequestObject(path);
484         Response responseIngest = documentService.process(requestIngest);
485         logger.debug(new ResponseHandler().toXML(responseIngest));
486         String marcUUID = responseIngest.getDocuments().get(0).getUuid();
487         String responseString = "";
488 
489         //Checkout Marc
490         Request request = getRequest(marcUUID, null, null, DocFormat.MARC.getDescription());
491         logger.debug("req xml:" + new RequestHandler().toXML(request));
492         Response response = documentService.process(request);
493 
494         responseString = new ResponseHandler().toXML(response);
495 
496         response = new ResponseHandler().toObject(responseString);
497         logger.debug("content:" + response.getDocuments().get(0).getContent().getContent());
498         logger.debug("checkOut content of Marc " + responseString);
499 
500     }
501 
502     private Request getRequestObject(String resourceLocation) throws URISyntaxException, IOException {
503         URL resource = getClass().getResource(resourceLocation);
504         File file = new File(resource.toURI());
505         RequestHandler requestHandler = new RequestHandler();
506         Request request = requestHandler.toObject(readFile(file));
507         return request;
508     }
509 
510     @Test
511     public void testBulkIngestStandardDocFormat() throws Exception {
512         String path = "/org/kuali/ole/repository/1Marc.xml";
513         File inputDir = new File(this.getClass().getResource(path).toURI());
514         BulkProcessRequest bulkProcessRequest = new BulkProcessRequest();
515         bulkProcessRequest.setDocCategory(DocCategory.WORK.getDescription());
516         bulkProcessRequest.setDocType(DocType.BIB.getDescription());
517         bulkProcessRequest.setDocFormat(DocFormat.MARC.getDescription());
518         bulkProcessRequest.setOperation(BulkProcessRequest.BulkProcessOperation.INGEST);
519         bulkProcessRequest.setAction(BulkProcessRequest.BulkProcessAction.START);
520         bulkProcessRequest.setDataFormat(BulkProcessRequest.BulkIngestDataFormat.STANDARD);
521         bulkProcessRequest.setStatus(BulkProcessRequest.BulkProcessStatus.STARTED);
522         bulkProcessRequest.setDoIndex(true);
523         bulkProcessRequest.setCommitSize(1000);
524         bulkProcessRequest.setDataFolder("bulkIngest");
525         bulkProcessRequest.setBulkIngestFolder(inputDir.getAbsolutePath());
526         assertNotNull(bulkProcessRequest);
527         BeanLocator.getDocumentServiceImpl().bulkProcess(bulkProcessRequest);
528         logger.info("BulkProcessStatus: " + bulkProcessRequest.getStatus());
529         logger.info("is DoIndex: " + bulkProcessRequest.isDoIndex());
530         logger.info("CommitSize: " + bulkProcessRequest.getCommitSize());
531     }
532 
533     @Test
534     public void testBulkIngestStatistics() throws Exception {
535         BulkProcessRequest bulkProcessRequest = new BulkProcessRequest();
536         String path = "/org/kuali/ole/bulkIngest/bulkIngest-Work-Bib-Marc-20.xml";
537         File inputDir = new File(this.getClass().getResource(path).toURI());
538         bulkProcessRequest.setUser("ole-khuntley");
539         bulkProcessRequest.setDocCategory(DocCategory.WORK.getDescription());
540         bulkProcessRequest.setDocType(DocType.BIB.getDescription());
541         bulkProcessRequest.setDocFormat(DocFormat.MARC.getDescription());
542         bulkProcessRequest.setOperation(BulkProcessRequest.BulkProcessOperation.INGEST);
543         bulkProcessRequest.setAction(BulkProcessRequest.BulkProcessAction.STATUS);
544         bulkProcessRequest.setDataFormat(BulkProcessRequest.BulkIngestDataFormat.STANDARD);
545         bulkProcessRequest.setDataFolder("bulkIngest");
546         bulkProcessRequest.setBulkIngestFolder(inputDir.getAbsolutePath());
547         bulkProcessRequest.setBulkIngestStatistics(getBulkIngestStatistics());
548         assertNotNull(bulkProcessRequest);
549         try {
550             BeanLocator.getDocumentServiceImpl().bulkProcess(bulkProcessRequest);
551         } catch (Exception e) {
552             logger.debug("Exception due to:" + e.getMessage() , e);
553             logger.info("Exception due to:" + e.getMessage(), e);
554         }
555     }
556 
557     @Test
558     public void testClearBulkIngestStatistics() throws Exception {
559         BulkProcessRequest bulkProcessRequest = new BulkProcessRequest();
560         String path = "/org/kuali/ole/bulkIngest/bulkIngest-Work-Bib-Marc-20.xml";
561         File inputDir = new File(this.getClass().getResource(path).toURI());
562         bulkProcessRequest.setUser("ole-khuntley");
563         bulkProcessRequest.setDocCategory(DocCategory.WORK.getDescription());
564         bulkProcessRequest.setDocType(DocType.BIB.getDescription());
565         bulkProcessRequest.setDocFormat(DocFormat.MARC.getDescription());
566         bulkProcessRequest.setOperation(BulkProcessRequest.BulkProcessOperation.INGEST);
567         bulkProcessRequest.setAction(BulkProcessRequest.BulkProcessAction.STATUS);
568         bulkProcessRequest.setDataFormat(BulkProcessRequest.BulkIngestDataFormat.STANDARD);
569         bulkProcessRequest.setDataFolder("bulkIngest");
570         bulkProcessRequest.setBulkIngestFolder(inputDir.getAbsolutePath());
571         bulkProcessRequest.setBulkIngestStatistics(getBulkIngestStatistics());
572         assertNotNull(bulkProcessRequest);
573         logger.info("Json String before clearing:" + bulkProcessRequest.getBulkIngestStatistics().getJsonString());
574         DocumentServiceImpl documentService = BeanLocator.getDocumentServiceImpl();
575         documentService.setBulkIngestRequest(bulkProcessRequest);
576         documentService.bulkProcess(bulkProcessRequest);
577         bulkProcessRequest.setAction(BulkProcessRequest.BulkProcessAction.CLEAR);
578         BeanLocator.getDocumentServiceImpl().bulkProcess(bulkProcessRequest);
579         logger.info("Json String after clearing:" + bulkProcessRequest.getBulkIngestStatistics().getJsonString());
580 
581     }
582 
583 
584     @Test
585     public void testStopBulkIngestProcess() throws Exception {
586         BulkProcessRequest bulkProcessRequest = new BulkProcessRequest();
587         String path = "/org/kuali/ole/bulkIngest/bulkIngest-Work-Bib-Marc-20.xml";
588         File inputDir = new File(this.getClass().getResource(path).toURI());
589         bulkProcessRequest.setUser("ole-khuntley");
590         bulkProcessRequest.setDocCategory(DocCategory.WORK.getDescription());
591         bulkProcessRequest.setDocType(DocType.BIB.getDescription());
592         bulkProcessRequest.setDocFormat(DocFormat.MARC.getDescription());
593         bulkProcessRequest.setOperation(BulkProcessRequest.BulkProcessOperation.INGEST);
594         bulkProcessRequest.setAction(BulkProcessRequest.BulkProcessAction.STOP);
595         bulkProcessRequest.setDataFormat(BulkProcessRequest.BulkIngestDataFormat.STANDARD);
596         bulkProcessRequest.setDataFolder("bulkIngest");
597         bulkProcessRequest.setBulkIngestFolder(inputDir.getAbsolutePath());
598         assertNotNull(bulkProcessRequest);
599         BeanLocator.getDocumentServiceImpl().bulkProcess(bulkProcessRequest);
600     }
601 
602 
603     public BulkIngestStatistics getBulkIngestStatistics() {
604         BulkIngestStatistics bulkIngestStatistics = BulkIngestStatistics.getInstance();
605         bulkIngestStatistics.setFirstBatch(true);
606         bulkIngestStatistics.setLastBatch(true);
607         bulkIngestStatistics.setBatchSize("10");
608         bulkIngestStatistics.setFileRecCount(2);
609         bulkIngestStatistics.setCommitRecCount(1);
610         List<BatchIngestStatistics> batchIngestStatisticsList = new ArrayList<BatchIngestStatistics>();
611         List<FileIngestStatistics> fileIngestStatisticsList = new ArrayList<FileIngestStatistics>();
612         FileIngestStatistics fileIngestStatistics = bulkIngestStatistics.startFile();
613         StringBuilder sb = new StringBuilder();
614         fileIngestStatistics.setFileName("sample1.txt");
615         fileIngestStatistics.setFileStatus("");
616         BatchIngestStatistics batchIngestStatistics = fileIngestStatistics.startBatch();
617         long timeToConvertStringToReqObj = 1200;
618         long timeToCreateNodesInJcr = 3555;
619         long timeToSaveJcrSession = 1222;
620         long ingestingTime = 3400;
621         long timeToConvertXmlToPojo = 2000;
622         long timeToConvertToSolrInputDocs = 1400;
623         long timeToIndexSolrInputDocs = 1860;
624         long timeToSolrCommit = 1700;
625         long timeToSolrOptimize = 2500;
626         long indexingTime = 2200;
627         long ingestNIndexTotalTime = 1400;
628         long batchTime = 2500;
629         String batchStartTime = "0:0:1.333";
630         String batchEndTime = "0:0:5.222";
631         long startTime = 2200;
632         long endTime = 3200;
633         batchIngestStatistics.setTimeToConvertStringToReqObj(timeToConvertStringToReqObj);
634         batchIngestStatistics.setTimeToCreateNodesInJcr(timeToCreateNodesInJcr);
635         batchIngestStatistics.setTimeToSaveJcrSession(timeToSaveJcrSession);
636         batchIngestStatistics.setIngestingTime(ingestingTime);
637         batchIngestStatistics.setTimeToConvertXmlToPojo(timeToConvertXmlToPojo);
638         batchIngestStatistics.setTimeToConvertToSolrInputDocs(timeToConvertToSolrInputDocs);
639         batchIngestStatistics.setTimeToIndexSolrInputDocs(timeToIndexSolrInputDocs);
640         batchIngestStatistics.setTimeToSolrCommit(timeToSolrCommit);
641         batchIngestStatistics.setTimeToSolrOptimize(timeToSolrOptimize);
642         batchIngestStatistics.setIndexingTime(indexingTime);
643         batchIngestStatistics.setIngestNIndexTotalTime(ingestNIndexTotalTime);
644         batchIngestStatistics.setBatchTime(batchTime);
645         batchIngestStatisticsList.add(batchIngestStatistics);
646         batchIngestStatistics.setBatchStartTime(batchStartTime);
647         batchIngestStatistics.setBatchEndTime(batchEndTime);
648         batchIngestStatistics.buildBatchMetric(sb, batchIngestStatistics);
649         fileIngestStatistics.setBatchStatisticsList(batchIngestStatisticsList);
650         bulkIngestStatistics.setFileIngestStatistics(fileIngestStatistics);
651         return bulkIngestStatistics;
652     }
653 
654     public String readFile(File file) throws IOException {
655         BufferedReader reader = new BufferedReader(new FileReader(file));
656         String line = null;
657         StringBuilder stringBuilder = new StringBuilder();
658         String ls = System.getProperty("line.separator");
659         while ((line = reader.readLine()) != null) {
660             stringBuilder.append(line);
661             stringBuilder.append(ls);
662         }
663         return stringBuilder.toString();
664     }
665 }