001/* 002 * Copyright 2011 The Kuali Foundation. 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.ole.docstore.xstream.ingest; 017 018import org.apache.commons.io.FileUtils; 019import org.junit.Assert; 020import org.junit.Test; 021import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes; 022import org.kuali.ole.docstore.model.xmlpojo.ingest.Content; 023import org.kuali.ole.docstore.model.xmlpojo.ingest.Request; 024import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument; 025import org.kuali.ole.docstore.model.xstream.ingest.RequestHandler; 026import org.kuali.ole.docstore.xstream.BaseTestCase; 027import org.kuali.ole.utility.DateTimeUtil; 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030 031import java.io.BufferedReader; 032import java.io.File; 033import java.io.FileReader; 034import java.io.IOException; 035import java.net.URISyntaxException; 036import java.net.URL; 037import java.text.DecimalFormat; 038import java.text.SimpleDateFormat; 039import java.util.ArrayList; 040import java.util.Date; 041import java.util.Iterator; 042import java.util.List; 043 044import static junit.framework.Assert.assertNotNull; 045 046/** 047 * Created by IntelliJ IDEA. 048 * User: pvsubrah 049 * Date: 9/7/11 050 * Time: 1:16 PM 051 * To change this template use File | Settings | File Templates. 052 */ 053public class RequestHandler_UT extends BaseTestCase { 054 //private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RequestHandler_UT.class); 055 private static final Logger LOG = LoggerFactory.getLogger(RequestHandler_UT.class); 056 057 @Test 058 public void testDublinUnqualifiedXML() throws URISyntaxException, IOException { 059 RequestHandler requestSerializer = new RequestHandler(); 060 URL resource = getClass().getResource("/org/kuali/ole/unqDublin.xml"); 061 File file = new File(resource.toURI()); 062 Request request = requestSerializer.toObject(readFile(file)); 063 List<RequestDocument> requestDocuments = request.getRequestDocuments(); 064 Assert.assertNotNull(requestDocuments); 065 for (Iterator<RequestDocument> iterator = requestDocuments.iterator(); iterator.hasNext(); ) { 066 RequestDocument ingestDocument = iterator.next(); 067 Assert.assertNotNull(ingestDocument); 068 String category = ingestDocument.getCategory(); 069 Assert.assertNotNull(category); 070 String type = ingestDocument.getType(); 071 Assert.assertNotNull(type); 072 String format = ingestDocument.getFormat(); 073 Assert.assertNotNull(format); 074 String content = ingestDocument.getContent().getContent(); 075 LOG.info("content-->" + content); 076 Assert.assertNotNull(content); 077 LOG.info("\n"); 078 } 079 String xml = requestSerializer.toXML(request); 080 LOG.info("xml-->" + xml); 081 Assert.assertNotNull(request); 082 083 084 } 085 086 @Test 087 public void convertXMLToReqObject() throws Exception { 088 RequestHandler requestSerializer = new RequestHandler(); 089 URL resource = getClass().getResource("/org/kuali/ole/sample-link.xml"); 090 File file = new File(resource.toURI()); 091 Request request = requestSerializer.toObject(readFile(file)); 092 List<RequestDocument> requestDocuments = request.getRequestDocuments(); 093 Assert.assertNotNull(requestDocuments); 094 for (Iterator<RequestDocument> iterator = requestDocuments.iterator(); iterator.hasNext(); ) { 095 RequestDocument ingestDocument = iterator.next(); 096 Assert.assertNotNull(ingestDocument); 097 String category = ingestDocument.getCategory(); 098 Assert.assertNotNull(category); 099 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}