View Javadoc
1   package org.kuali.ole.repository;
2   
3   import org.apache.commons.io.FileUtils;
4   import org.apache.solr.client.solrj.response.QueryResponse;
5   import org.junit.Assert;
6   import org.junit.Before;
7   import org.junit.Ignore;
8   import org.junit.Test;
9   import org.kuali.ole.BaseTestCase;
10  import org.kuali.ole.RepositoryManager;
11  import org.kuali.ole.docstore.model.xmlpojo.ingest.*;
12  import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.DataField;
13  import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.SubField;
14  import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.WorkBibMarcRecord;
15  import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.WorkBibMarcRecords;
16  import org.kuali.ole.docstore.model.xstream.ingest.RequestHandler;
17  import org.kuali.ole.docstore.model.xstream.work.bib.marc.WorkBibMarcRecordProcessor;
18  import org.kuali.ole.docstore.service.BeanLocator;
19  import org.kuali.ole.docstore.service.IngestNIndexHandlerService;
20  import org.kuali.ole.docstore.service.ServiceLocator;
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  
24  import javax.jcr.Node;
25  import javax.jcr.Session;
26  import java.io.File;
27  import java.net.URL;
28  import java.util.ArrayList;
29  import java.util.Iterator;
30  import java.util.List;
31  
32  import static junit.framework.Assert.assertNotNull;
33  
34  /**
35   * Created by IntelliJ IDEA.
36   * User: Pranitha
37   * Date: 3/9/12
38   * Time: 11:09 AM
39   * To change this template use File | Settings | File Templates.
40   */
41  @Ignore
42  @Deprecated
43  public class CheckinManager_AT
44          extends BaseTestCase {
45      private static final Logger LOG = LoggerFactory
46              .getLogger(CheckinManager_AT.class);
47      private IngestNIndexHandlerService ingestNIndexHandlerService = BeanLocator
48              .getIngestNIndexHandlerService();
49  
50      @Before
51      public void setUp() throws Exception {
52          super.setUp();
53      }
54  
55      @Test
56      public void checkInRecord() throws Exception {
57          CheckoutManager checkoutManager = new CheckoutManager();
58          NodeHandler nodeHandler = new NodeHandler();
59          String author = null;
60          String instIdValue = null;
61          // input record
62          URL resource = getClass().getResource("/org/kuali/ole/repository/request.xml");
63          File file = new File(resource.toURI());
64          String fileContent = FileUtils.readFileToString(file);
65  
66          ResponseDocument responseDocument = new ResponseDocument();
67          RequestHandler requestHandler = new RequestHandler();
68          Request request = requestHandler.toObject(fileContent);
69          // Ingest & Index input rec
70          Response xmlResponse = ingestNIndexHandlerService.ingestNIndexRequestDocuments(request);
71          responseDocument = xmlResponse.getDocuments().get(0);
72          String uuid = responseDocument.getUuid();
73          Session session = RepositoryManager.getRepositoryManager().getSession("ole-khuntley", "batchIngest");
74          instIdValue = "6cecc116-a5c6-4240-994a-ba652a8ecde4";
75          //content of marc rec
76          String checkedOutContent = checkoutManager.checkOut(uuid, "mockUser", "checkout");
77          assertNotNull(checkedOutContent);
78          //updating marc content
79          List<WorkBibMarcRecord> updatedMarcRecs = new ArrayList<WorkBibMarcRecord>();
80          WorkBibMarcRecordProcessor workBibMarcRecordProcessor = new WorkBibMarcRecordProcessor();
81          workBibMarcRecordProcessor.fromXML(checkedOutContent);
82          WorkBibMarcRecords workBibMarcRecords = workBibMarcRecordProcessor.fromXML(checkedOutContent);
83          for (Iterator<WorkBibMarcRecord> iterator = workBibMarcRecords.getRecords().iterator(); iterator.hasNext(); ) {
84              WorkBibMarcRecord rec = iterator.next();
85              DataField dataField = rec.getDataFieldForTag("100");
86              for (SubField subField : dataField.getSubFields()) {
87                  author = subField.getValue();
88                  author = author + "Updated version for author";
89                  subField.setValue(author);
90              }
91              updatedMarcRecs.add(rec);
92          }
93          workBibMarcRecords = new WorkBibMarcRecords();
94          workBibMarcRecords.setRecords(updatedMarcRecs);
95          workBibMarcRecordProcessor = new WorkBibMarcRecordProcessor();
96          String updatedContent = workBibMarcRecordProcessor.toXml(workBibMarcRecords);
97          updatedContent = updatedContent.replace("list", "collection");
98          RequestDocument updatedReqDoc = new RequestDocument();
99          buildRequestDoc(updatedReqDoc, responseDocument, updatedContent, instIdValue);
100         //checking in record with updated content and links
101         CheckinManager checkinManager = new CheckinManager();
102         String updatedVersion = checkinManager.updateContent(updatedReqDoc);
103         checkedOutContent = checkoutManager.checkOut(uuid, "mockUser", "checkout");
104         assertNotNull(checkedOutContent);
105         Assert.assertEquals("Content matches with version updated record", checkedOutContent, updatedContent);
106         Node bibNode = nodeHandler.getNodeByUUID(session, uuid);
107         String instanceIdentifier = bibNode.getProperty("instanceIdentifier").getValue().getString();
108        // Assert.assertEquals("Instance Identifier matches Docstore value", instanceIdentifier, instIdValue);
109         // getting instance identifier value from Solr
110         QueryResponse queryResponse = ServiceLocator.getIndexerService().searchBibRecord(responseDocument.getCategory(),
111                 responseDocument.getType(),
112                 responseDocument.getFormat(),
113                 "id", uuid,
114                 "instanceIdentifier");
115         //LOG.info("queryResponse" + queryResponse);
116         List solrInstIdList = new ArrayList();
117         solrInstIdList = (List) queryResponse.getResults().get(0).getFieldValue("instanceIdentifier");
118         Assert.assertEquals("Instance Identifier matches search result", instIdValue, solrInstIdList.get(0));
119     }
120 
121 
122     private void buildRequestDoc(RequestDocument updatedReqDoc, ResponseDocument responseDocument,
123                                  String updatedContent, String instIdValue) {
124         updatedReqDoc.setCategory(responseDocument.getCategory());
125         updatedReqDoc.setType(responseDocument.getType());
126         updatedReqDoc.setFormat(responseDocument.getFormat());
127         updatedReqDoc.setId(responseDocument.getUuid());
128         Content updatedCont = new Content();
129         updatedCont.setContent(updatedContent);
130         updatedReqDoc.setContent(updatedCont);
131         AdditionalAttributes addAtt = new AdditionalAttributes();
132         addAtt.setDateEntered("12-12-2-2010");
133         addAtt.setLastUpdated("12-12-2-2010");
134         addAtt.setFastAddFlag("true");
135         addAtt.setSupressFromPublic("false");
136         updatedReqDoc.setAdditionalAttributes(addAtt);
137         updatedReqDoc.setUuid(responseDocument.getUuid());
138 
139     }
140 }