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.junit.Assert;
019import org.junit.Test;
020import org.kuali.ole.docstore.model.xmlpojo.ingest.*;
021import org.kuali.ole.docstore.model.xstream.ingest.ResponseHandler;
022import org.kuali.ole.docstore.xstream.BaseTestCase;
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025
026import java.io.BufferedReader;
027import java.io.File;
028import java.io.FileReader;
029import java.io.IOException;
030import java.net.URL;
031import java.util.ArrayList;
032import java.util.Iterator;
033import java.util.List;
034
035import static junit.framework.Assert.assertNotNull;
036
037/**
038 * Created by IntelliJ IDEA.
039 * User: pvsubrah
040 * Date: 9/13/11
041 * Time: 10:59 PM
042 * To change this template use File | Settings | File Templates.
043 */
044public class ResponseHandler_UT extends BaseTestCase {
045    // private static final Logger LOG = Logger.getLogger(ResponseHandler_UT.class);
046    private static final Logger LOG = LoggerFactory.getLogger(ResponseHandler_UT.class);
047
048    @Test
049    public void convertResponseXMLToReqObject() throws Exception {
050        ResponseHandler responseHandler = new ResponseHandler();
051        URL resource = getClass().getResource("/org/kuali/ole/response.xml");
052        File file = new File(resource.toURI());
053        Response response = responseHandler.toObject(readFile(file));
054        assertNotNull(response);
055        LOG.info(response.getUser());
056        List<ResponseDocument> responseDocuments = response.getDocuments();
057        Assert.assertNotNull(responseDocuments);
058        LOG.info("responseDocuments size" + responseDocuments.size());
059        for (Iterator<ResponseDocument> iterator = responseDocuments.iterator(); iterator.hasNext(); ) {
060            ResponseDocument document = iterator.next();
061            Assert.assertNotNull(document);
062            LOG.info("document" + document);
063            String status = document.getStatus();
064            LOG.info("status" + status);
065            String statusMessage = document.getStatusMessage();
066            LOG.info("statusMessage" + statusMessage);
067            String id = document.getId();
068            LOG.info("id" + id);
069            String category = document.getCategory();
070            String documentMimeType = document.getDocumentMimeType();
071            LOG.info("documentMimeType" + documentMimeType);
072            String documentName = document.getDocumentName();
073            LOG.info("documentName" + documentName);
074            String documentTitle = document.getDocumentTitle();
075            LOG.info("documentTitle" + documentTitle);
076            LOG.info("category" + category);
077            Assert.assertNotNull(category);
078            String type = document.getType();
079            Assert.assertNotNull(type);
080            String format = document.getFormat();
081            Assert.assertNotNull(format);
082            LOG.info("\n");
083            List<ResponseDocument> linkedDocuments = document.getLinkedDocuments();
084            for (Iterator<ResponseDocument> iterator1 = linkedDocuments.iterator(); iterator1.hasNext(); ) {
085                ResponseDocument linkdocument = iterator1.next();
086                Assert.assertNotNull(linkdocument);
087                LOG.info("linked document" + linkdocument);
088                String linkId = linkdocument.getId();
089                LOG.info("linked id" + id);
090                Assert.assertNotNull(linkId);
091                String linkCategory = linkdocument.getCategory();
092                LOG.info("linked category" + category);
093                Assert.assertNotNull(linkCategory);
094                String linkType = linkdocument.getType();
095                LOG.info("linked Type" + linkType);
096                Assert.assertNotNull(linkType);
097                String linkFormat = linkdocument.getFormat();
098                LOG.info("linked formate" + linkFormat);
099                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}