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.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
44
45
46
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
60
61
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
76
77
78
79 @After
80 public void tearDown() throws Exception {
81 if (session != null) {
82 session.logout();
83 }
84 }
85
86
87
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
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
131
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185 }
186 catch (Exception e) {
187 LOG.info(e.getMessage());
188 fail("Failed : Test : ingestBibNLinkedInstanceRequestDocuments(): " + e);
189 }
190 }
191
192
193
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
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 }