View Javadoc
1   /*
2    * Copyright 2012 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.web;
17  
18  import static org.junit.Assert.fail;
19  
20  import java.io.BufferedReader;
21  import java.io.File;
22  import java.io.FileOutputStream;
23  import java.io.FileReader;
24  import java.io.InputStream;
25  import java.util.List;
26  
27  import org.apache.commons.io.FileUtils;
28  import org.apache.commons.io.IOUtils;
29  import org.apache.http.HttpEntity;
30  import org.apache.http.HttpResponse;
31  import org.apache.http.client.HttpClient;
32  import org.apache.http.client.methods.HttpPost;
33  import org.apache.http.entity.mime.MultipartEntity;
34  import org.apache.http.entity.mime.content.FileBody;
35  import org.apache.http.impl.client.DefaultHttpClient;
36  import org.junit.After;
37  import org.junit.Before;
38  import org.junit.Ignore;
39  import org.junit.Test;
40  import org.kuali.ole.docstore.model.xmlpojo.ingest.Request;
41  import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
42  import org.kuali.ole.docstore.model.xmlpojo.ingest.Response;
43  import org.kuali.ole.docstore.model.xmlpojo.ingest.ResponseDocument;
44  import org.kuali.ole.docstore.model.xstream.ingest.RequestHandler;
45  import org.kuali.ole.docstore.model.xstream.ingest.ResponseHandler;
46  import org.kuali.ole.utility.CompressUtils;
47  import org.slf4j.Logger;
48  import org.slf4j.LoggerFactory;
49  
50  /**
51   * Class to DocumentServlet_UT.
52   * 
53   * @author Rajesh Chowdary K
54   * @created May 25, 2012
55   */
56  @Ignore
57  public class DocumentServlet_AT {
58  
59      private Logger         logger         = LoggerFactory.getLogger(DocumentServlet_AT.class);
60      private String         url            = "http://localhost:9080/oledocstore/document";
61      private File           inputDir       = null;
62      private Response       response       = null;
63      private RequestHandler requestHandler = new RequestHandler();
64  
65      /**
66       * Method to setUp
67       * 
68       * @throws java.lang.Exception
69       */
70      @Before
71      public void setUp() throws Exception {
72      }
73  
74      /**
75       * Method to tearDown
76       * 
77       * @throws java.lang.Exception
78       */
79      @After
80      public void tearDown() throws Exception {
81      }
82  
83      @Test
84      public void test() {
85          //fail("Not yet implemented");
86      }
87  
88      /**
89       * Method creates the bag from the test case folder then sends to the document store application and captures the response
90       */
91      @Test
92      public void testMultiPartIngestForWorkLicense() {
93          try {
94              inputDir = new File(this.getClass().getResource("license/ingest").toURI());
95              HttpPost httpPost = new HttpPost(url);
96              HttpClient httpclient = new DefaultHttpClient();
97              CompressUtils compressUtils = new CompressUtils();
98              File zipFile = compressUtils.createZippedBagFile(inputDir);
99              FileBody uploadFilePart = new FileBody(zipFile);
100             MultipartEntity reqEntity = new MultipartEntity();
101             reqEntity.addPart("upload-file", uploadFilePart);
102             httpPost.setEntity(reqEntity);
103             HttpResponse response = httpclient.execute(httpPost);
104             zipFile.delete();
105             logger.info("STATUS : " + response.getStatusLine());
106             HttpEntity respEntity = response.getEntity();
107             InputStream outcome = respEntity.getContent();
108             File respFile = File.createTempFile("DocStore Ingest-", "-Response File.zip");
109             IOUtils.copy(outcome, new FileOutputStream(respFile));
110             List<File> resp = compressUtils.getAllFilesList(compressUtils.extractZippedBagFile(respFile.getAbsolutePath(), null));
111             for (File file : resp) {
112                 if (file.getName().equalsIgnoreCase("response.xml")) {
113                     BufferedReader br = new BufferedReader(new FileReader(file));
114                     logger.info("RESPONSE: " + file.getName());
115                     String line = null;
116                     while ((line = br.readLine()) != null)
117                         logger.info(line);
118                     this.response = new ResponseHandler().toObject(FileUtils.readFileToString(file));
119                 } else
120                     logger.info("File : " + file.getName());
121             }
122         } catch (Exception e) {
123             logger.error(e.getMessage(), e);
124             //fail("Failed Test: ");
125 
126         }
127     }
128 
129     @Test
130     public void testMultiPartCheckOutForWorkLicense() {
131         try {
132             if (response == null)
133                 this.testMultiPartIngestForWorkLicense();
134             Request request = new Request();
135             request.setUser(response.getUser());
136             request.setOperation("checkOut");
137             for (ResponseDocument doc : response.getDocuments()) {
138                 RequestDocument rd = new RequestDocument();
139                 rd.setId(doc.getId());
140                 rd.setCategory(doc.getCategory());
141                 rd.setType(doc.getType());
142                 rd.setFormat(doc.getFormat());
143                 rd.setUuid(doc.getUuid());
144                 request.getRequestDocuments().add(rd);
145             }
146             String xml = requestHandler.toXML(request);
147 
148             File folder = File.createTempFile("ds-", "-inp");
149             folder.delete();
150             folder.mkdirs();
151             FileOutputStream fos = new FileOutputStream(new File(folder.getAbsolutePath() + File.separator + "request.xml"));
152             IOUtils.copy(IOUtils.toInputStream(xml), fos);
153             fos.close();
154             HttpPost httpPost = new HttpPost(url);
155             HttpClient httpclient = new DefaultHttpClient();
156             CompressUtils compressUtils = new CompressUtils();
157             File zipFile = compressUtils.createZippedBagFile(folder);
158             FileBody uploadFilePart = new FileBody(zipFile);
159             MultipartEntity reqEntity = new MultipartEntity();
160             reqEntity.addPart("upload-file", uploadFilePart);
161             httpPost.setEntity(reqEntity);
162             HttpResponse response = httpclient.execute(httpPost);
163             zipFile.delete();
164             logger.info("STATUS : " + response.getStatusLine());
165             HttpEntity respEntity = response.getEntity();
166             InputStream outcome = respEntity.getContent();
167             File respFile = File.createTempFile("DocStore CheckOut-", "-Response File.zip");
168             IOUtils.copy(outcome, new FileOutputStream(respFile));
169             List<File> resp = compressUtils.getAllFilesList(compressUtils.extractZippedBagFile(respFile.getAbsolutePath(), null));
170             for (File file : resp) {
171                 if (file.getName().equalsIgnoreCase("response.xml")) {
172                     BufferedReader br = new BufferedReader(new FileReader(file));
173                     logger.info("RESPONSE: " + file.getName());
174                     String line = null;
175                     while ((line = br.readLine()) != null)
176                         logger.info(line);
177                 } else
178                     logger.info("File : " + file.getName());
179             }
180         } catch (Exception e) {
181             logger.error(e.getMessage(), e);
182             fail("Failed Test: ");
183 
184         }
185     }
186 
187     @Test
188     public void testMultiPartStaticCheckOutForWorkLicense() {
189         try {
190             inputDir = new File(this.getClass().getResource("license/checkout").toURI());
191             HttpPost httpPost = new HttpPost(url);
192             HttpClient httpclient = new DefaultHttpClient();
193             CompressUtils compressUtils = new CompressUtils();
194             File zipFile = compressUtils.createZippedBagFile(inputDir);
195             FileBody uploadFilePart = new FileBody(zipFile);
196             MultipartEntity reqEntity = new MultipartEntity();
197             reqEntity.addPart("upload-file", uploadFilePart);
198             httpPost.setEntity(reqEntity);
199             HttpResponse response = httpclient.execute(httpPost);
200             zipFile.delete();
201             logger.info("STATUS : " + response.getStatusLine());
202             HttpEntity respEntity = response.getEntity();
203             InputStream outcome = respEntity.getContent();
204             File respFile = File.createTempFile("DocStore CheckOut-", "-Response File.zip");
205             logger.info("Response is at loc.: " + respFile.getAbsolutePath());
206             IOUtils.copy(outcome, new FileOutputStream(respFile));
207             List<File> resp = compressUtils.getAllFilesList(compressUtils.extractZippedBagFile(respFile.getAbsolutePath(), null));
208             for (File file : resp) {
209                 if (file.getName().equalsIgnoreCase("response.xml")) {
210                     BufferedReader br = new BufferedReader(new FileReader(file));
211                     logger.info("RESPONSE: " + file.getName());
212                     String line = null;
213                     while ((line = br.readLine()) != null)
214                         logger.info(line);
215                 } else
216                     logger.info("File : " + file.getName());
217             }
218         } catch (Exception e) {
219             logger.error(e.getMessage(), e);
220             fail("Failed Test: ");
221 
222         }
223     }
224 
225     @Test
226     public void testMultiPartCheckInForWorkLicense() {
227         try {
228             if (response == null)
229                 this.testMultiPartIngestForWorkLicense();
230             inputDir = new File(this.getClass().getResource("license/checkin").toURI());
231             File reqFile = new File(inputDir.getAbsolutePath() + File.separator + "request.xml");
232             Request checkinReq = requestHandler.toObject(FileUtils.readFileToString(reqFile));
233             for (RequestDocument rd : checkinReq.getRequestDocuments())
234                 for (ResponseDocument resDoc : response.getDocuments())
235                     if (rd.getCategory().equalsIgnoreCase(resDoc.getCategory()) && rd.getType().equalsIgnoreCase(resDoc.getType())
236                             && rd.getFormat().equalsIgnoreCase(resDoc.getFormat())) {
237                         rd.setUuid(resDoc.getUuid());
238                         break;
239                     }
240             reqFile.delete();
241             reqFile.createNewFile();
242             FileUtils.write(reqFile, requestHandler.toXML(checkinReq));
243             HttpPost httpPost = new HttpPost(url);
244             HttpClient httpclient = new DefaultHttpClient();
245             CompressUtils compressUtils = new CompressUtils();
246             File zipFile = compressUtils.createZippedBagFile(inputDir);
247             FileBody uploadFilePart = new FileBody(zipFile);
248             MultipartEntity reqEntity = new MultipartEntity();
249             reqEntity.addPart("upload-file", uploadFilePart);
250             httpPost.setEntity(reqEntity);
251             HttpResponse response = httpclient.execute(httpPost);
252             zipFile.delete();
253             logger.info("STATUS : " + response.getStatusLine());
254             HttpEntity respEntity = response.getEntity();
255             InputStream outcome = respEntity.getContent();
256             File respFile = File.createTempFile("DocStore CheckIn-", "-Response File.zip");
257             logger.info("Response is at loc.: " + respFile.getAbsolutePath());
258             IOUtils.copy(outcome, new FileOutputStream(respFile));
259             List<File> resp = compressUtils.getAllFilesList(compressUtils.extractZippedBagFile(respFile.getAbsolutePath(), null));
260             for (File file : resp) {
261                 BufferedReader br = new BufferedReader(new FileReader(file));
262                 logger.info("RESPONSE: " + file.getName());
263                 String line = null;
264                 while ((line = br.readLine()) != null) {
265                     logger.info(line);
266                 }
267             }
268         } catch (Exception e) {
269             logger.error(e.getMessage(), e);
270             fail("Failed Test: ");
271 
272         }
273     }
274 
275     @Test
276     public void testMultiPartDeleteForWorkLicense() {
277         try {
278             if (response == null)
279                 this.testMultiPartIngestForWorkLicense();
280             inputDir = new File(this.getClass().getResource("license/delete").toURI());
281             File reqFile = new File(inputDir.getAbsolutePath() + File.separator + "request.xml");
282             Request checkinReq = requestHandler.toObject(FileUtils.readFileToString(reqFile));
283             for (RequestDocument rd : checkinReq.getRequestDocuments())
284                 for (ResponseDocument resDoc : response.getDocuments())
285                     if (rd.getCategory().equalsIgnoreCase(resDoc.getCategory()) && rd.getType().equalsIgnoreCase(resDoc.getType())
286                             && rd.getFormat().equalsIgnoreCase(resDoc.getFormat())) {
287                         rd.setUuid(resDoc.getUuid());
288                         break;
289                     }
290             reqFile.delete();
291             reqFile.createNewFile();
292             FileUtils.write(reqFile, requestHandler.toXML(checkinReq));
293 
294             HttpPost httpPost = new HttpPost(url);
295             HttpClient httpclient = new DefaultHttpClient();
296             CompressUtils compressUtils = new CompressUtils();
297             File zipFile = compressUtils.createZippedBagFile(inputDir);
298             FileBody uploadFilePart = new FileBody(zipFile);
299             MultipartEntity reqEntity = new MultipartEntity();
300             reqEntity.addPart("upload-file", uploadFilePart);
301             httpPost.setEntity(reqEntity);
302             HttpResponse response = httpclient.execute(httpPost);
303             zipFile.delete();
304             logger.info("STATUS : " + response.getStatusLine());
305             HttpEntity respEntity = response.getEntity();
306             InputStream outcome = respEntity.getContent();
307             File respFile = File.createTempFile("DocStore Delete-", "-Response File.zip");
308             logger.info("Response is at loc.: " + respFile.getAbsolutePath());
309             IOUtils.copy(outcome, new FileOutputStream(respFile));
310             List<File> resp = compressUtils.getAllFilesList(compressUtils.extractZippedBagFile(respFile.getAbsolutePath(), null));
311             for (File file : resp) {
312                 BufferedReader br = new BufferedReader(new FileReader(file));
313                 logger.info("RESPONSE: " + file.getName());
314                 String line = null;
315                 while ((line = br.readLine()) != null) {
316                     logger.info(line);
317                 }
318             }
319         } catch (Exception e) {
320             logger.error(e.getMessage(), e);
321             fail("Failed Test: ");
322 
323         }
324     }
325 
326     public static void main(String args[]) {
327         DocumentServlet_AT dsClient = new DocumentServlet_AT();
328         try {
329             dsClient.testMultiPartIngestForWorkLicense();
330         } catch (Exception e) {
331 
332         }
333     }
334 }