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.xstream.ingest;
17  
18  import org.junit.Assert;
19  import org.junit.Test;
20  import org.kuali.ole.docstore.model.xmlpojo.ingest.Content;
21  import org.kuali.ole.docstore.model.xmlpojo.ingest.Response;
22  import org.kuali.ole.docstore.model.xmlpojo.ingest.ResponseDocument;
23  import org.kuali.ole.docstore.model.xstream.ingest.ResponseHandler;
24  import org.kuali.ole.docstore.xstream.BaseTestCase;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  import java.io.BufferedReader;
29  import java.io.File;
30  import java.io.FileReader;
31  import java.io.IOException;
32  import java.net.URL;
33  import java.util.ArrayList;
34  import java.util.Iterator;
35  import java.util.List;
36  
37  import static junit.framework.Assert.assertNotNull;
38  
39  /**
40   * Created by IntelliJ IDEA.
41   * User: pvsubrah
42   * Date: 9/13/11
43   * Time: 10:59 PM
44   * To change this template use File | Settings | File Templates.
45   */
46  public class ResponseHandler_UT extends BaseTestCase {
47      // private static final Logger LOG = Logger.getLogger(ResponseHandler_UT.class);
48      private static final Logger LOG = LoggerFactory.getLogger(ResponseHandler_UT.class);
49  
50      @Test
51      public void convertResponseXMLToReqObject() throws Exception {
52          ResponseHandler responseHandler = new ResponseHandler();
53          URL resource = getClass().getResource("/org/kuali/ole/response.xml");
54          File file = new File(resource.toURI());
55          Response response = responseHandler.toObject(readFile(file));
56          assertNotNull(response);
57          LOG.info(response.getUser());
58          List<ResponseDocument> responseDocuments = response.getDocuments();
59          Assert.assertNotNull(responseDocuments);
60          LOG.info("responseDocuments size" + responseDocuments.size());
61          for (Iterator<ResponseDocument> iterator = responseDocuments.iterator(); iterator.hasNext();) {
62              ResponseDocument document = iterator.next();
63              Assert.assertNotNull(document);
64              LOG.info("document" + document);
65              String status=document.getStatus();
66              LOG.info("status"+status);
67              String statusMessage=document.getStatusMessage();
68              LOG.info("statusMessage"+statusMessage);
69              String id = document.getId();
70              LOG.info("id" + id);
71              String category = document.getCategory();
72              String documentMimeType=document.getDocumentMimeType();
73              LOG.info("documentMimeType"+documentMimeType);
74              String documentName=document.getDocumentName();
75              LOG.info("documentName"+documentName);
76              String documentTitle= document.getDocumentTitle();
77              LOG.info("documentTitle"+documentTitle);
78              LOG.info("category" + category);
79              Assert.assertNotNull(category);
80              String type = document.getType();
81              Assert.assertNotNull(type);
82              String format = document.getFormat();
83              Assert.assertNotNull(format);
84              LOG.info("\n");
85              List<ResponseDocument> linkedDocuments = document.getLinkedDocuments();
86              for (Iterator<ResponseDocument> iterator1 = linkedDocuments.iterator(); iterator1.hasNext();) {
87                  ResponseDocument linkdocument = iterator1.next();
88                  Assert.assertNotNull(linkdocument);
89                  LOG.info("linked document" + linkdocument);
90                  String linkId = linkdocument.getId();
91                  LOG.info("linked id" + id);
92                  Assert.assertNotNull(linkId);
93                  String linkCategory = linkdocument.getCategory();
94                  LOG.info("linked category" + category);
95                  Assert.assertNotNull(linkCategory);
96                  String linkType = linkdocument.getType();
97                  LOG.info("linked Type" + linkType);
98                  Assert.assertNotNull(linkType);
99                  String linkFormat = linkdocument.getFormat();
100                 LOG.info("linked formate" + linkFormat);
101                 Assert.assertNotNull(linkFormat);
102 
103             }
104         }
105     }
106 
107     @Test
108     public void generateResponse() throws Exception {
109         ResponseHandler responseHandler = new ResponseHandler();
110         Response response = new Response();
111         response.setUser("mockUser");
112         response.setOperation("mockOperation");
113         response.setStatus("Success/Failure");
114         response.setStatusMessage("Document ingested. | Document could not be ingested. Reason:...");
115         response.setMessage("info required");
116         ResponseDocument responseDocument1 = new ResponseDocument();
117         responseDocument1.setId("1");
118         responseDocument1.setCategory("work");
119         responseDocument1.setType("Biblographic");
120         responseDocument1.setFormat("oleml");
121         responseDocument1.setUuid("uuid-1");
122         Content content = new Content();
123         content.setContent("oleml content");
124         responseDocument1.setContent(content);
125 
126         ResponseDocument linkedresponseDoc1 = new ResponseDocument();
127         linkedresponseDoc1.setId("linkId3");
128         linkedresponseDoc1.setCategory("work");
129         linkedresponseDoc1.setType("instance");
130         linkedresponseDoc1.setFormat("oleml");
131         Content linkedContent1 = new Content();
132         linkedContent1.setContent("some Instance content");
133         linkedresponseDoc1.setContent(linkedContent1);
134         linkedresponseDoc1.setUuid("instance uuid-3");
135         responseDocument1.addLinkedDocument(linkedresponseDoc1);
136 
137         ResponseDocument linkedHoldingResponseDoc = new ResponseDocument();
138         linkedHoldingResponseDoc.setId("linkId Holding 3");
139         linkedHoldingResponseDoc.setCategory("work");
140         linkedHoldingResponseDoc.setType("holding");
141         linkedHoldingResponseDoc.setFormat("oleml");
142         Content linkedHoldingContent = new Content();
143         linkedHoldingContent.setContent("some holding content");
144         linkedHoldingResponseDoc.setContent(linkedHoldingContent);
145         linkedHoldingResponseDoc.setUuid("holding uuid-3");
146         responseDocument1.addLinkedInstanseDocument(linkedHoldingResponseDoc);
147 
148         ResponseDocument linkedItemResponseDoc = new ResponseDocument();
149         linkedItemResponseDoc.setId("linkId Item 3");
150         linkedItemResponseDoc.setCategory("work");
151         linkedItemResponseDoc.setType("item");
152         linkedItemResponseDoc.setFormat("oleml");
153         Content linkedItemContent = new Content();
154         linkedItemContent.setContent("some Item content");
155         linkedItemResponseDoc.setContent(linkedItemContent);
156         linkedItemResponseDoc.setUuid("item uuid-3");
157         responseDocument1.addLinkedInstanseDocument(linkedItemResponseDoc);
158 
159         ResponseDocument responseDocument2 = new ResponseDocument();
160         responseDocument2.setId("2");
161         responseDocument2.setCategory("work");
162         responseDocument2.setType("bibliographic");
163         responseDocument2.setFormat("marc");
164         responseDocument2.setUuid("uuid-2");
165         Content content2 = new Content();
166         content2.setContent("content");
167         responseDocument2.setContent(content2);
168         ArrayList<ResponseDocument> responseDocument = new ArrayList<ResponseDocument>();
169         responseDocument.add(responseDocument1);
170         responseDocument.add(responseDocument2);
171         response.setDocuments(responseDocument);
172         ResponseDocument linkedresponseDoc2 = new ResponseDocument();
173         linkedresponseDoc2.setId("linkId4");
174         linkedresponseDoc2.setCategory("work");
175         linkedresponseDoc2.setType("instance");
176         linkedresponseDoc2.setFormat("oleml");
177         Content linkedContent2 = new Content();
178         linkedContent2.setContent("somecontent");
179         linkedresponseDoc2.setUuid("instance uuid-4");
180         linkedresponseDoc2.setContent(linkedContent1);
181         responseDocument2.addLinkedDocument(linkedresponseDoc2);
182 
183         String xmlResponse = responseHandler.toXML(response);
184         assertNotNull(xmlResponse);
185         LOG.info(xmlResponse);
186     }
187 
188     private String readFile(File file) throws IOException {
189         BufferedReader reader = new BufferedReader(new FileReader(file));
190         String line = null;
191         StringBuilder stringBuilder = new StringBuilder();
192         String ls = System.getProperty("line.separator");
193         while ((line = reader.readLine()) != null) {
194             stringBuilder.append(line);
195             stringBuilder.append(ls);
196         }
197         return stringBuilder.toString();
198     }
199 }