001package org.kuali.ole.docstore.discovery;
002
003import groovy.json.JsonBuilder;
004import org.apache.commons.io.FileUtils;
005import org.junit.Test;
006import org.kuali.ole.base.BaseTestCase;
007import org.kuali.ole.docstore.common.document.content.instance.Instance;
008import org.kuali.ole.docstore.common.document.content.instance.InstanceCollection;
009import org.kuali.ole.docstore.discovery.circulation.XmlContentHandler;
010import org.kuali.ole.docstore.discovery.circulation.json.CircInstance;
011import org.kuali.ole.docstore.discovery.circulation.json.InstanceRecord;
012
013import java.io.File;
014import java.util.ArrayList;
015import java.util.Iterator;
016import java.util.List;
017
018import static junit.framework.Assert.assertNotNull;
019
020/**
021 * Created with IntelliJ IDEA.
022 * User: pvsubrah
023 * Date: 5/22/13
024 * Time: 10:29 AM
025 * To change this template use File | Settings | File Templates.
026 */
027public class DocstoreCricInfoRetrieveService_UT extends BaseTestCase {
028
029    @Test
030    public void testJSONStringForSimpleCollection() throws Exception {
031        Person person = new Person();
032        person.setName("Mock.A");
033        ArrayList<String> cars = new ArrayList<String>();
034        cars.add("cadiallac");
035        cars.add("chevy");
036        person.setCars(cars);
037
038        JsonBuilder jsonBuilder = new JsonBuilder(person);
039        String jsonString = jsonBuilder.toPrettyString();
040        System.out.println(jsonString);
041
042    }
043
044    @Test
045    public void generateJSONForCircInstance() throws Exception {
046        String responseXML = FileUtils.readFileToString(new File(getClass().getResource("instancecollection-response.xml").toURI()));
047        XmlContentHandler xmlContentHandler = new XmlContentHandler();
048        Object unmarshalledObject = xmlContentHandler.unmarshalXMLContent(InstanceCollection.class, responseXML);
049        CircInstance circInstance = new CircInstance();
050        List<Instance> instanceList = ((InstanceCollection) unmarshalledObject).getInstance();
051        for (Iterator<Instance> iterator = instanceList.iterator(); iterator.hasNext(); ) {
052            Instance instance = iterator.next();
053            ArrayList<org.kuali.ole.docstore.discovery.circulation.json.InstanceCollection> instanceCollection = new ArrayList<org.kuali.ole.docstore.discovery.circulation.json.InstanceCollection>();
054            org.kuali.ole.docstore.discovery.circulation.json.InstanceCollection col1 = new org.kuali.ole.docstore.discovery.circulation.json.InstanceCollection();
055            InstanceRecord instanceRecord = new InstanceRecord();
056            instanceRecord.setExtension(instance.getExtension());
057            instanceRecord.setResourceIdentifier(instance.getResourceIdentifier());
058            instanceRecord.setSourceHoldings(instance.getSourceHoldings());
059            instanceRecord.setItems(instance.getItems().getItem());
060            instanceRecord.setFormerResourceIdentifier(instance.getFormerResourceIdentifier());
061            instanceRecord.setInstanceIdentifier(instance.getInstanceIdentifier());
062            instanceCollection.add(col1);
063            circInstance.setInstanceCollection(instanceCollection);
064        }
065        String json = xmlContentHandler.marshalToJSON(circInstance);
066        System.out.println(json);
067    }
068
069    private class Person {
070        private String name;
071        private List<String> cars;
072
073        public void setName(String name) {
074            this.name = name;
075        }
076
077        public String getName() {
078            return name;
079        }
080
081        public void setCars(List<String> cars) {
082            this.cars = cars;
083        }
084    }
085}