View Javadoc

1   /*
2    * Copyright 2011 The Kuali Foundation.
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.docstore.service;
17  
18  import org.apache.commons.io.FileUtils;
19  import org.junit.After;
20  import org.junit.Before;
21  import org.junit.Test;
22  import org.kuali.ole.BaseTestCase;
23  import org.kuali.ole.RepositoryManager;
24  import org.kuali.ole.docstore.model.xmlpojo.ingest.Request;
25  import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
26  import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.ControlField;
27  import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.WorkBibMarcRecord;
28  import org.kuali.ole.docstore.model.xstream.ingest.RequestHandler;
29  import org.kuali.ole.docstore.model.xstream.work.bib.marc.WorkBibMarcRecordProcessor;
30  import org.kuali.ole.repository.NodeHandler;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  import javax.jcr.*;
35  import java.io.File;
36  import java.net.URL;
37  import java.util.ArrayList;
38  import java.util.List;
39  
40  import static org.junit.Assert.fail;
41  
42  /**
43   * Class to DocumentIngester_UT.
44   *
45   * @author Rajesh Chowdary K
46   * @created Feb 23, 2012
47   */
48  public class DocumentIngester_UT
49          extends BaseTestCase {
50      private static final Logger LOG = LoggerFactory.getLogger(DocumentIngester_UT.class);
51  
52  
53      private DocumentIngester      documentIngester = new DocumentIngester();
54      private List<RequestDocument> reqDocuments     = new ArrayList<RequestDocument>();
55      private Request req;
56      private Session session;
57  
58      /**
59       * Method to setUp
60       *
61       * @throws java.lang.Exception
62       */
63      @Before
64      public void setUp() throws Exception {
65          URL resource = getClass().getResource("/org/kuali/ole/repository/request.xml");
66          File file = new File(resource.toURI());
67          String fileContent = FileUtils.readFileToString(file);
68          RequestHandler rh = new RequestHandler();
69          req = rh.toObject(fileContent);
70          reqDocuments = req.getRequestDocuments();
71          session = RepositoryManager.getRepositoryManager().getSession(req.getUser(), req.getOperation());
72      }
73  
74      /**
75       * Method to tearDown
76       *
77       * @throws java.lang.Exception
78       */
79      @After
80      public void tearDown() throws Exception {
81          if (session != null) {
82              session.logout();
83          }
84      }
85  
86      /**
87       * Test method for {@link org.kuali.ole.docstore.service.DocumentIngester#//ingestBibRequestDocument(org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument, javax.jcr.Session)}.
88       */
89      @Test
90      public final void testIngestBibRequestDocument() {
91          try {
92              documentIngester.ingestBibDocument(reqDocuments.get(0), session, null);
93              LOG.info("UUID OF Ingested Record: " + reqDocuments.get(0).getUuid());
94              if (reqDocuments.get(0).getUuid() == null) {
95                  fail("Not Ingested.");
96              }
97          }
98          catch (Exception e) {
99              LOG.info(e.getMessage());
100             fail("Failed :  Test : ingestBibRequestDocument(): " + e);
101         }
102 
103     }
104 
105     /**
106      * Test method for {@link org.kuali.ole.docstore.service.DocumentIngester#ingestBibNLinkedInstanceRequestDocuments(org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument, javax.jcr.Session)}.
107      */
108 
109     public final void testIngestBibNLinkedInstanceRequestDocuments() {
110         try {
111             RequestDocument bibDoc = reqDocuments.get(0);
112             List<String> ingestIDs = documentIngester.ingestBibNLinkedInstanceRequestDocuments(bibDoc, session);
113             session.save();
114             NodeHandler nh = new NodeHandler();
115             Node bibNode = nh.getNodeByUUID(session, bibDoc.getUuid());
116             if (bibNode == null) {
117                 fail("Docuement Ingest Failed.");
118             }
119             String bibIdentifier = bibNode.getIdentifier();
120             WorkBibMarcRecordProcessor marcProcessor = new WorkBibMarcRecordProcessor();
121             WorkBibMarcRecord marcRec = marcProcessor.fromXML(bibDoc.getContent().getContent()).getRecords().get(0);
122             boolean isBibFound = false;
123             for (ControlField cf : marcRec.getControlFields()) {
124                 if (cf.getTag().equals("001")) {
125                     if (cf.getValue().equals(bibIdentifier)) {
126                         isBibFound = true;
127                     }
128                 }
129             }
130             // if (!isBibFound)
131             // fail("Bib Identifier Not found in Marc as CF 001");
132 
133             PropertyIterator props = bibNode.getProperties("instanceIdentifier");
134             while (props.hasNext()) {
135                 String instanceIdentifier = ((Property) props.next()).getString();
136                 LOG.info("Instance Identifier: " + instanceIdentifier);
137                 Node instanceNode = nh.getNodeByUUID(session, instanceIdentifier);
138                 LOG.info("Instance Node: " + instanceNode);
139                 boolean isBibIdentifierFound = false;
140                 PropertyIterator instanceNodeProps = instanceNode.getProperties("bibIdentifier");
141                 LOG.info("bibIdentifier : Props: " + instanceIdentifier);
142                 while (instanceNodeProps.hasNext()) {
143                     if (((Property) instanceNodeProps.next()).getString().equals(bibDoc.getUuid())) {
144                         isBibIdentifierFound = true;
145                     }
146                 }
147 
148                 if (!isBibIdentifierFound) {
149                     fail("No Matching Bib Identifier Found in Instance Node.");
150                 }
151 
152                 int holdingsNodesCount = 0;
153                 int itemNodesCount = 0;
154                 NodeIterator children = instanceNode.getNodes();
155                 while (children.hasNext()) {
156                     Node child = (Node) children.next();
157                     PropertyIterator childProps = child.getProperties();
158                     LOG.info("Child Node Props: " + childProps);
159 
160                     while (childProps.hasNext()) {
161                         Property prop = (Property) childProps.next();
162                         LOG.info("Prop: " + prop);
163                     }
164 
165                 }
166 
167             }
168 
169             /*
170              * Verify the following:
171              * 4. Verify bib info
172              * a. Find bib node.
173              * b. Get its uuid as bibIdentifier.
174              * c. Get its content. Verify that 001 field in xml content is = bibIdentifier.
175              * d. Get instanceIdentifier property.
176              * e. Use it to get instanceNode.
177              * 5. Verify instance info.
178              * a. Get bibIdentifier property of instanceNode.
179              * b. Verify it is same as the one in 4.b
180              * c. Get children of instanceNode.
181              * d. Verify that there is one holdings node and two itemNodes.
182              * e. Get the xml content of holdings node. Verify that it has holdingsIdentifier=uuid of holdings node.
183              * f. Get the xml contents of item nodes. Verify that they have itemIdentifier=uuid of item node.
184              */
185         }
186         catch (Exception e) {
187             LOG.info(e.getMessage());
188             fail("Failed :  Test : ingestBibNLinkedInstanceRequestDocuments(): " + e);
189         }
190     }
191 
192     /**
193      * Test method for {@link org.kuali.ole.docstore.service.DocumentIngester#//ingestInstaceRequestDocument(org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument, javax.jcr.Session)}.
194      */
195 
196     public final void testIngestInstaceRequestDocument() {
197         try {
198             RequestDocument instanceReqDocument = reqDocuments.get(0).getLinkedRequestDocuments().get(0);
199             List<String> ingestedDocs = documentIngester
200                     .ingestInstaceRequestDocumentForBulk(instanceReqDocument, session, null);
201             for (String ingestedDoc : ingestedDocs) {
202                 LOG.info("Instance Document UUID: " + ingestedDoc);
203                 if (ingestedDoc == null) {
204                     fail("Instance Document Not Ingested.");
205                 }
206             }
207         }
208         catch (Exception e) {
209             LOG.info(e.getMessage());
210             fail("Failed :  Test : ingestInstaceRequestDocument(): " + e);
211         }
212     }
213 
214     /**
215      * Test method for {@link org.kuali.ole.docstore.service.DocumentIngester#rollbackDocStoreIngestedData(javax.jcr.Session, java.util.List)}.
216      */
217 
218     public final void testRollbackDocStoreIngestedData() {
219         List<String> uuids = new ArrayList<String>();
220         try {
221             RequestDocument instanceReqDocument = reqDocuments.get(0).getLinkedRequestDocuments().get(0);
222             uuids = documentIngester.ingestInstaceRequestDocumentForBulk(instanceReqDocument, session, null);
223             throw new Exception("Test Throw");
224         }
225         catch (Exception e) {
226             try {
227                 documentIngester.rollbackDocStoreIngestedData(session, reqDocuments);
228             }
229             catch (Exception ex) {
230                 fail("Failed :  Test : rollbackDocStoreIngestedData(): " + ex);
231             }
232         }
233     }
234 
235 }