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