View Javadoc
1   package org.kuali.ole.docstore.xstream.work.oleml;
2   
3   import junit.framework.Assert;
4   import org.junit.Before;
5   import org.junit.Test;
6   import org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.*;
7   import org.kuali.ole.docstore.model.xstream.work.oleml.WorkEInstanceOlemlRecordProcessor;
8   import org.kuali.ole.docstore.xstream.BaseTestCase;
9   import org.slf4j.Logger;
10  import org.slf4j.LoggerFactory;
11  
12  import java.io.BufferedReader;
13  import java.io.File;
14  import java.io.FileReader;
15  import java.io.IOException;
16  import java.net.URL;
17  import java.util.ArrayList;
18  import java.util.List;
19  
20  import static org.junit.Assert.assertNotNull;
21  
22  /**
23   * Created with IntelliJ IDEA.
24   * User: Sreekanth
25   * Date: 7/19/13
26   * Time: 12:09 PM
27   * To change this template use File | Settings | File Templates.
28   */
29  public class WorkEInstanceOlemlRecordProcessor_UT
30          extends BaseTestCase {
31  
32      private static final Logger LOG = LoggerFactory.getLogger(WorkEInstanceOlemlRecordProcessor_UT.class);
33      InstanceCollection instanceCollection = null;
34      private EInstance eInstance = null;
35      private EHoldings eHoldings = null;
36      String sampleXmlFilePath = "/org/kuali/ole/ole-einstance-latestv1.xsd.xml";
37  
38      @Before
39      public void setUp() {
40  
41          ObjectFactory objectFactory = new ObjectFactory();
42          instanceCollection = new InstanceCollection();
43          eHoldings = objectFactory.createEHoldings();
44          List<FormerIdentifier> formerResourceIdentifier = null;
45          List<EInstance> oleInstanceList = new ArrayList<EInstance>();
46          ExtentOfOwnership extentOfOwnership = objectFactory.createExtentOfOwnership();
47          eInstance = objectFactory.createEInstance();
48          eInstance.setInstanceIdentifier("mockInstacneIdentifier");
49          List<String> resourceIdentifiers = eInstance.getResourceIdentifier();
50          if (resourceIdentifiers.isEmpty()) {
51              LOG.info("empty values");
52              resourceIdentifiers.add("a1000003");
53          }
54          //  eInstance.setResourceIdentifier(resourceIdentifiers);
55  
56          List<FormerIdentifier> formerIdentifierList = eInstance.getFormerResourceIdentifier();
57  
58          if (formerIdentifierList.isEmpty()) {
59              FormerIdentifier formerIdentifier = objectFactory.createFormerIdentifier();
60              Identifier identifier = objectFactory.createIdentifier();
61              identifier.setIdentifierValue("1000003-2");
62              identifier.setSource("IUHoldings");
63              LOG.info("Identifier Val:" + identifier.getIdentifierValue());
64              LOG.info("Source:" + identifier.getSource());
65  
66              formerIdentifier.setIdentifier(identifier);
67              formerIdentifier.setIdentifierType("Ty 001");
68              formerIdentifierList.add(formerIdentifier);
69              LOG.info("Indentifier Source:" + formerIdentifier.getIdentifier().getSource());
70              LOG.info("Indentifier Type:" + formerIdentifier.getIdentifierType());
71          }
72  
73          // eInstance.setFormerResourceIdentifier(formerIdentifierList);
74  
75  
76          formerIdentifierList = eInstance.getFormerResourceIdentifier();
77  
78          if (formerIdentifierList.isEmpty()) {
79              FormerIdentifier formerIdentifier = objectFactory.createFormerIdentifier();
80              Identifier identifier = objectFactory.createIdentifier();
81              identifier.setIdentifierValue("1000003-2");
82              identifier.setSource("IUHoldings");
83              LOG.info("Identifier Val:" + identifier.getIdentifierValue());
84              LOG.info("Source:" + identifier.getSource());
85  
86              formerIdentifier.setIdentifier(identifier);
87              formerIdentifier.setIdentifierType("Ty 001");
88              formerIdentifierList.add(formerIdentifier);
89              LOG.info("Indentifier Source:" + formerIdentifier.getIdentifier().getSource());
90              LOG.info("Indentifier Type:" + formerIdentifier.getIdentifierType());
91          }
92          List<String> statisticalCodeList = new ArrayList<>();
93  
94  
95          statisticalCodeList.add("a1000003");
96  
97          eHoldings.setStatisticalSearchingCode("");
98          TypeOrSource typeOrSource = new TypeOrSource();
99          typeOrSource.setPointer("P");
100         typeOrSource.setText("T");
101         List<Note> noteList = new ArrayList<>();
102         Note note = objectFactory.createNote();
103         note.setType("NT");
104         note.setValue("NT");
105         noteList.add(note);
106 //        eHoldings.setNote(noteList);
107 
108         eHoldings = objectFactory.createEHoldings();
109         eHoldings.setHoldingsIdentifier("30004-1");
110         note = new Note();
111         note.setValue("Completed");
112         List<ExtentOfOwnership> extentOfOwnershipList = new ArrayList<>();
113         extentOfOwnershipList.add(extentOfOwnership);
114 
115         extentOfOwnership = new ExtentOfOwnership();
116         extentOfOwnershipList.add(extentOfOwnership);
117         note = new Note();
118         note.setType("AST");
119         note.setType("AST");
120         eHoldings.getNote().add(note);
121 
122         CallNumber callNumber = objectFactory.createCallNumber();
123         callNumber.setNumber("8008");
124         callNumber.setPrefix("IT");
125         eHoldings.setCallNumber(callNumber);
126         assertNotNull(eHoldings.getCallNumber());
127         eHoldings.setCallNumber(callNumber);
128         eHoldings.setPrimary("true");
129 
130 
131         List<String> shelvingOrderList = new ArrayList<>();
132         ShelvingOrder shelvingOrder = objectFactory.createShelvingOrder();
133         shelvingOrderList.add("SC002");
134         shelvingOrderList.add("SC002");
135 
136 
137 //        callNumber.setShelvingOrder(shelvingOrderList);
138 
139 //        assertNotNull(callNumber.getShelvingOrder());
140 
141 
142 //        extentOfOwnershipList = eHoldings.getExtentOfOwnership();
143         for (ExtentOfOwnership extentOfOwnership1 : extentOfOwnershipList) {
144         }
145         LOG.info("Holdings Identifier:" + eHoldings.getHoldingsIdentifier());
146         LOG.info("Number:" + eHoldings.getCallNumber().getNumber());
147         LOG.info("Prefix:" + eHoldings.getCallNumber().getPrefix());
148         LOG.info("Primary:" + eHoldings.getPrimary());
149 
150         List<Coverage> coverageList = new ArrayList<>();
151         List<Coverages> coveragesList = new ArrayList<>();
152 
153         Coverages coverages = new Coverages();
154         Coverage coverage = objectFactory.createCoverage();
155         List<String> coverageEndDateList = new ArrayList<>();
156         coverageEndDateList.add("1995");
157 //        coverage.setCoverageEndDate(coverageEndDateList);
158         coverageList.add(coverage);
159 //        coverages.setCoverage(coverageList);
160         coveragesList.add(coverages);
161 //        extentOfOwnership.setCoverages(coveragesList);
162         extentOfOwnershipList.add(extentOfOwnership);
163 //        eHoldings.setExtentOfOwnership(extentOfOwnershipList); /
164 
165         eInstance.setEHoldings(eHoldings);
166         //eInstance = objectFactory.createEInstance();
167         // eInstance.setResourceIdentifier(statisticalCEodeList);
168         oleInstanceList.add(eInstance);
169         instanceCollection.getEInstance().addAll(oleInstanceList);
170         Assert.assertNotNull(objectFactory.createInstanceCollection(instanceCollection));
171     }
172 
173 
174     public void tearDown() {
175         LOG.info("Running tearDown()");
176     }
177 
178     @Test
179     public void testGenerateXML() throws Exception {
180         WorkEInstanceOlemlRecordProcessor oleInstanceConverter = new WorkEInstanceOlemlRecordProcessor();
181         String xml = oleInstanceConverter.toXML(instanceCollection);
182         assertNotNull(xml);
183         LOG.info(xml);
184         InstanceCollection instanceCollection1 = oleInstanceConverter.fromXML(xml);
185         assertNotNull(instanceCollection1);
186         LOG.info("instanceCollection" + instanceCollection1);
187 
188         LOG.info("Regained XML: \n" + oleInstanceConverter.toXML(instanceCollection1));
189     }
190 
191 
192     @Test
193     public void testGenerateObjectFromXML() throws Exception {
194         WorkEInstanceOlemlRecordProcessor oleInstanceConverter = new WorkEInstanceOlemlRecordProcessor();
195         URL url = getClass().getResource(sampleXmlFilePath);
196         File file = new File(url.toURI());
197         instanceCollection = oleInstanceConverter.fromXML(readFile(file));
198         LOG.info("entered");
199         for (EInstance inst : instanceCollection.getEInstance()) {
200             LOG.info("Instance Identifier:" + inst.getInstanceIdentifier());
201             LOG.info("Resource Identifier:" + inst.getResourceIdentifier());
202             LOG.info("Former Resource Identifier:" + inst.getResourceIdentifier());
203             LOG.info(inst.getEHoldings().getPrimary());
204         }
205         String xml = oleInstanceConverter.toXML(instanceCollection);
206         LOG.info("xml-->" + xml);
207     }
208 
209     @Test
210     public void testFromXMLAndToXmlInstanceCollection() throws Exception {
211         WorkEInstanceOlemlRecordProcessor oleInstanceConverter = new WorkEInstanceOlemlRecordProcessor();
212         URL url = getClass().getResource(sampleXmlFilePath);
213         File file = new File(url.toURI());
214         InstanceCollection instanceCollection = oleInstanceConverter.fromXML(readFile(file));
215         assertNotNull(instanceCollection);
216         String xml = oleInstanceConverter.toXML(instanceCollection);
217         LOG.info("Generated XML : " + xml);
218         LOG.info("GENERATED XML: ");
219         LOG.info(xml);
220         assertNotNull(xml);
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 
235 
236 }
237