1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
45
46
47
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
63
64
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
79
80
81
82 @After
83 public void tearDown() throws Exception {
84 if (session != null) {
85 session.logout();
86 }
87 }
88
89
90
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
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
133
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187 } catch (Exception e) {
188 LOG.info(e.getMessage() , e);
189 fail("Failed : Test : ingestBibNLinkedInstanceRequestDocuments(): " + e);
190 }
191 }
192
193
194
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
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 }