1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.docstore.xstream.ingest;
17
18 import org.apache.commons.io.FileUtils;
19 import org.junit.Assert;
20 import org.junit.Test;
21 import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes;
22 import org.kuali.ole.docstore.model.xmlpojo.ingest.Content;
23 import org.kuali.ole.docstore.model.xmlpojo.ingest.Request;
24 import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
25 import org.kuali.ole.docstore.model.xstream.ingest.RequestHandler;
26 import org.kuali.ole.docstore.xstream.BaseTestCase;
27 import org.kuali.ole.utility.DateTimeUtil;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import java.io.BufferedReader;
32 import java.io.File;
33 import java.io.FileReader;
34 import java.io.IOException;
35 import java.net.URISyntaxException;
36 import java.net.URL;
37 import java.text.DecimalFormat;
38 import java.text.SimpleDateFormat;
39 import java.util.ArrayList;
40 import java.util.Date;
41 import java.util.Iterator;
42 import java.util.List;
43
44 import static junit.framework.Assert.assertNotNull;
45
46
47
48
49
50
51
52
53 public class RequestHandler_UT extends BaseTestCase {
54
55 private static final Logger LOG = LoggerFactory.getLogger(RequestHandler_UT.class);
56
57 @Test
58 public void testDublinUnqualifiedXML() throws URISyntaxException, IOException {
59 RequestHandler requestSerializer = new RequestHandler();
60 URL resource = getClass().getResource("/org/kuali/ole/unqDublin.xml");
61 File file = new File(resource.toURI());
62 Request request = requestSerializer.toObject(readFile(file));
63 List<RequestDocument> requestDocuments = request.getRequestDocuments();
64 Assert.assertNotNull(requestDocuments);
65 for (Iterator<RequestDocument> iterator = requestDocuments.iterator(); iterator.hasNext(); ) {
66 RequestDocument ingestDocument = iterator.next();
67 Assert.assertNotNull(ingestDocument);
68 String category = ingestDocument.getCategory();
69 Assert.assertNotNull(category);
70 String type = ingestDocument.getType();
71 Assert.assertNotNull(type);
72 String format = ingestDocument.getFormat();
73 Assert.assertNotNull(format);
74 String content = ingestDocument.getContent().getContent();
75 LOG.info("content-->" + content);
76 Assert.assertNotNull(content);
77 LOG.info("\n");
78 }
79 String xml = requestSerializer.toXML(request);
80 LOG.info("xml-->" + xml);
81 Assert.assertNotNull(request);
82
83
84 }
85
86 @Test
87 public void convertXMLToReqObject() throws Exception {
88 RequestHandler requestSerializer = new RequestHandler();
89 URL resource = getClass().getResource("/org/kuali/ole/sample-link.xml");
90 File file = new File(resource.toURI());
91 Request request = requestSerializer.toObject(readFile(file));
92 List<RequestDocument> requestDocuments = request.getRequestDocuments();
93 Assert.assertNotNull(requestDocuments);
94 for (Iterator<RequestDocument> iterator = requestDocuments.iterator(); iterator.hasNext(); ) {
95 RequestDocument ingestDocument = iterator.next();
96 Assert.assertNotNull(ingestDocument);
97 String category = ingestDocument.getCategory();
98 Assert.assertNotNull(category);
99 String type = ingestDocument.getType();
100 Assert.assertNotNull(type);
101 String format = ingestDocument.getFormat();
102 Assert.assertNotNull(format);
103 String content = ingestDocument.getContent().getContent();
104 Assert.assertNotNull(content);
105 LOG.info("\n");
106 }
107 Assert.assertNotNull(request);
108 String xml = requestSerializer.toXML(request);
109 LOG.info("request-->" + xml);
110
111 }
112
113 @Test
114 public void endToEndTestFromNToXML() throws Exception {
115 RequestHandler requestSerializer = new RequestHandler();
116 URL resource = getClass().getResource("/org/kuali/ole/docstore/model/xstream/ingest/request-instance.xml");
117 File file = new File(resource.toURI());
118 Request request = requestSerializer.toObject(readFile(file));
119 LOG.info(requestSerializer.toXML(request));
120 }
121
122 @Test
123 public void convertReqObjectToXML() throws Exception {
124 Request requestObject = new Request();
125
126 RequestDocument requestDocument1 = new RequestDocument();
127 List<RequestDocument> linkedRequestDocuments = new ArrayList<RequestDocument>();
128 requestDocument1.setId("2");
129 requestDocument1.setCategory("work");
130 requestDocument1.setType("instance");
131 requestDocument1.setFormat("oleml");
132 requestDocument1.setUser("user");
133 assertNotNull(requestDocument1.getUser());
134 requestDocument1.setUuid("1111");
135 assertNotNull(requestDocument1.getUuid());
136 requestDocument1.setOperation("read");
137 assertNotNull(requestDocument1.getOperation());
138 Content content = new Content();
139 content.setContent("instance content");
140 requestDocument1.setContent(content);
141 AdditionalAttributes additionalAttributes = new AdditionalAttributes();
142 additionalAttributes.setDateEntered("mockDate");
143 additionalAttributes.setSupressFromPublic("false");
144 additionalAttributes.setLastUpdated("mockDate");
145 additionalAttributes.setFastAddFlag("false");
146 requestDocument1.setAdditionalAttributes(additionalAttributes);
147 RequestDocument linkedRequestDoc1 = new RequestDocument();
148 linkedRequestDoc1.setId("linkId1");
149 linkedRequestDoc1.setCategory("work");
150 linkedRequestDoc1.setType("instance");
151 linkedRequestDoc1.setFormat("oleml");
152 linkedRequestDoc1.setAdditionalAttributes(additionalAttributes);
153 Content linkedContent1 = new Content();
154 linkedContent1.setContent("somecontent");
155 linkedRequestDoc1.setContent(linkedContent1);
156 requestDocument1.addLinkedRequestDocument(linkedRequestDoc1);
157 RequestDocument requestDocument2 = new RequestDocument();
158
159 requestDocument2.setId("1");
160 requestDocument2.setCategory("work");
161 requestDocument2.setType("bibliographic");
162 requestDocument2.setFormat("marc");
163 Content content1 = new Content();
164 content1.setContent("marc content");
165 requestDocument2.setContent(content1);
166 AdditionalAttributes additionalAttributes1 = new AdditionalAttributes();
167 additionalAttributes1.setDateEntered("mockDate");
168 additionalAttributes1.setSupressFromPublic("false");
169 additionalAttributes1.setLastUpdated("mockDate");
170 additionalAttributes1.setFastAddFlag("false");
171 requestDocument2.setAdditionalAttributes(additionalAttributes1);
172 RequestDocument linkedRequestDoc2 = new RequestDocument();
173 linkedRequestDoc2.setId("linkId2");
174 linkedRequestDoc2.setCategory("work");
175 linkedRequestDoc2.setType("instance");
176 linkedRequestDoc2.setFormat("oleml");
177 linkedRequestDoc2.setAdditionalAttributes(additionalAttributes1);
178 Content linkedContent = new Content();
179 linkedContent.setContent("somecontent");
180 linkedRequestDoc2.setContent(linkedContent);
181 requestDocument2.addLinkedRequestDocument(linkedRequestDoc2);
182 ArrayList<RequestDocument> requestDocuments = new ArrayList<RequestDocument>();
183 requestDocuments.add(requestDocument1);
184 requestDocuments.add(requestDocument2);
185 RequestDocument requestDocument3 = (RequestDocument) requestDocument1.clone();
186 requestDocument3.setLinkedRequestDocuments(requestDocuments);
187 requestObject.setRequestDocuments(requestDocuments);
188 RequestHandler requestHandler = new RequestHandler();
189 String xml = requestHandler.toXML(requestObject);
190 Assert.assertNotNull(xml);
191 LOG.info(xml);
192 }
193
194
195 @Test
196 public void convertReqObjectWithOLEDataToXML() throws Exception {
197 Request requestObject = new Request();
198 requestObject.setUser("admin");
199 assertNotNull(requestObject.getUser());
200 requestObject.setOperation(Request.Operation.bulkIngest.toString());
201 assertNotNull(requestObject.getOperation());
202 URL resource = null;
203 File file = null;
204 RequestDocument requestDocument1 = new RequestDocument();
205 requestDocument1.setId("2");
206 requestDocument1.setCategory("work");
207 requestDocument1.setType("instance");
208 requestDocument1.setFormat("oleml");
209 requestDocument1.setUser("admin");
210
211 Content content = new Content();
212 resource = getClass().getResource("/org/kuali/ole/instance.xml");
213 file = new File(resource.toURI());
214 String instanceContent = FileUtils.readFileToString(file);
215 content.setContent(instanceContent);
216 requestDocument1.setContent(content);
217 AdditionalAttributes additionalAttributes = new AdditionalAttributes();
218 additionalAttributes.setDateEntered("mockDate");
219 additionalAttributes.setSupressFromPublic("false");
220 additionalAttributes.setLastUpdated("mockDate");
221 additionalAttributes.setFastAddFlag("false");
222 requestDocument1.setAdditionalAttributes(additionalAttributes);
223 RequestDocument requestDocument2 = new RequestDocument();
224 requestDocument2.setId("1");
225 requestDocument2.setCategory("work");
226 requestDocument2.setType("bibliographic");
227 requestDocument2.setFormat("marc");
228 Content content1 = new Content();
229 resource = getClass().getResource("/org/kuali/ole/input-marc.xml");
230 file = new File(resource.toURI());
231 String marcContent = FileUtils.readFileToString(file);
232 content1.setContent(marcContent);
233 requestDocument2.setContent(content1);
234 AdditionalAttributes additionalAttributes1 = new AdditionalAttributes();
235 additionalAttributes1.setDateEntered("mockDate");
236 additionalAttributes1.setSupressFromPublic("false");
237 additionalAttributes1.setLastUpdated("mockDate");
238 additionalAttributes1.setFastAddFlag("false");
239 requestDocument2.setAdditionalAttributes(additionalAttributes1);
240 ArrayList<RequestDocument> requestDocuments = new ArrayList<RequestDocument>();
241 requestDocuments.add(requestDocument1);
242 requestDocuments.add(requestDocument2);
243 requestObject.setRequestDocuments(requestDocuments);
244 RequestHandler requestHandler = new RequestHandler();
245 String xmlString = requestHandler.toXML(requestObject);
246 LOG.info(xmlString);
247 Assert.assertNotNull(xmlString);
248
249 Request newRequest = requestHandler.toObject(xmlString);
250 LOG.info("requestDocument size in req object " + requestObject.getRequestDocuments().size());
251 LOG.info("requestDocument size in new req object " + newRequest.getRequestDocuments().size());
252 Assert.assertEquals(requestObject.getRequestDocuments().size(), newRequest.getRequestDocuments().size());
253 Assert.assertEquals(requestObject.getRequestDocuments().get(0).getCategory(),
254 newRequest.getRequestDocuments().get(0).getCategory());
255
256 }
257
258
259 private String readFile(File file) throws IOException {
260 BufferedReader reader = new BufferedReader(new FileReader(file));
261 String line = null;
262 StringBuilder stringBuilder = new StringBuilder();
263 String ls = System.getProperty("line.separator");
264 while ((line = reader.readLine()) != null) {
265 stringBuilder.append(line);
266 stringBuilder.append(ls);
267 }
268 return stringBuilder.toString();
269 }
270 }