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