View Javadoc
1   package org.kuali.ole.docstore.discovery;
2   
3   import groovy.json.JsonBuilder;
4   import org.apache.commons.io.FileUtils;
5   import org.junit.Test;
6   import org.kuali.ole.base.BaseTestCase;
7   import org.kuali.ole.docstore.common.document.content.instance.Instance;
8   import org.kuali.ole.docstore.common.document.content.instance.InstanceCollection;
9   import org.kuali.ole.docstore.discovery.circulation.XmlContentHandler;
10  import org.kuali.ole.docstore.discovery.circulation.json.CircInstance;
11  import org.kuali.ole.docstore.discovery.circulation.json.InstanceRecord;
12  
13  import java.io.File;
14  import java.util.ArrayList;
15  import java.util.Iterator;
16  import java.util.List;
17  
18  import static junit.framework.Assert.assertNotNull;
19  
20  /**
21   * Created with IntelliJ IDEA.
22   * User: pvsubrah
23   * Date: 5/22/13
24   * Time: 10:29 AM
25   * To change this template use File | Settings | File Templates.
26   */
27  public class DocstoreCricInfoRetrieveService_UT extends BaseTestCase {
28  
29      @Test
30      public void testJSONStringForSimpleCollection() throws Exception {
31          Person person = new Person();
32          person.setName("Mock.A");
33          ArrayList<String> cars = new ArrayList<String>();
34          cars.add("cadiallac");
35          cars.add("chevy");
36          person.setCars(cars);
37  
38          JsonBuilder jsonBuilder = new JsonBuilder(person);
39          String jsonString = jsonBuilder.toPrettyString();
40          System.out.println(jsonString);
41  
42      }
43  
44      @Test
45      public void generateJSONForCircInstance() throws Exception {
46          String responseXML = FileUtils.readFileToString(new File(getClass().getResource("instancecollection-response.xml").toURI()));
47          XmlContentHandler xmlContentHandler = new XmlContentHandler();
48          Object unmarshalledObject = xmlContentHandler.unmarshalXMLContent(InstanceCollection.class, responseXML);
49          CircInstance circInstance = new CircInstance();
50          List<Instance> instanceList = ((InstanceCollection) unmarshalledObject).getInstance();
51          for (Iterator<Instance> iterator = instanceList.iterator(); iterator.hasNext(); ) {
52              Instance instance = iterator.next();
53              ArrayList<org.kuali.ole.docstore.discovery.circulation.json.InstanceCollection> instanceCollection = new ArrayList<org.kuali.ole.docstore.discovery.circulation.json.InstanceCollection>();
54              org.kuali.ole.docstore.discovery.circulation.json.InstanceCollection col1 = new org.kuali.ole.docstore.discovery.circulation.json.InstanceCollection();
55              InstanceRecord instanceRecord = new InstanceRecord();
56              instanceRecord.setExtension(instance.getExtension());
57              instanceRecord.setResourceIdentifier(instance.getResourceIdentifier());
58              instanceRecord.setSourceHoldings(instance.getSourceHoldings());
59              instanceRecord.setItems(instance.getItems().getItem());
60              instanceRecord.setFormerResourceIdentifier(instance.getFormerResourceIdentifier());
61              instanceRecord.setInstanceIdentifier(instance.getInstanceIdentifier());
62              instanceCollection.add(col1);
63              circInstance.setInstanceCollection(instanceCollection);
64          }
65          String json = xmlContentHandler.marshalToJSON(circInstance);
66          System.out.println(json);
67      }
68  
69      private class Person {
70          private String name;
71          private List<String> cars;
72  
73          public void setName(String name) {
74              this.name = name;
75          }
76  
77          public String getName() {
78              return name;
79          }
80  
81          public void setCars(List<String> cars) {
82              this.cars = cars;
83          }
84      }
85  }