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