View Javadoc
1   /**
2    * Copyright 2005-2014 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 edu.sampleu.admin;
17  
18  import org.apache.commons.io.IOUtils;
19  import org.openqa.selenium.By;
20  
21  import java.io.File;
22  import java.io.FileOutputStream;
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  import java.net.URL;
26  import java.net.URLDecoder;
27  import java.util.ArrayList;
28  import java.util.Collections;
29  import java.util.Enumeration;
30  import java.util.HashSet;
31  import java.util.List;
32  import java.util.Set;
33  import java.util.jar.JarEntry;
34  import java.util.jar.JarFile;
35  
36  /**
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   */
39  
40  public abstract class EdocLiteXmlIngesterBase extends AdminTmplMthdAftNavBase {
41      // values set by default for repeatable testing; left as configurable for load tests
42      protected List<File> fileUploadList;
43  
44      @Override
45      protected String getBookmarkUrl() {
46          return null; // no bookmark test yet
47      }
48  
49      /**
50       * This overridden method ...
51       *
52       * @see edu.sampleu.common.NavTemplateMethodAftBase#getLinkLocator()
53       */
54      @Override
55      protected String getLinkLocator() {
56          return "XML Ingester";
57      }
58  
59      /**
60       * Performs Ingesting files to fileupload component and asserts succesful ingestion.
61       *
62       */
63      private void fileIngester(List<File> fileToUpload) throws Exception {
64          int cnt = 0;
65          for (File file : fileToUpload) {
66              String path = file.getAbsolutePath().toString();
67              driver.findElement(By.name("file[" + cnt + "]")).sendKeys(path);
68              cnt++;
69          }
70          waitAndClickById("imageField");
71      }
72  
73      /**
74       * Divides fileUploadList from resources into sublists to match the maximum number of file
75       * upload components available on XML Ingester Screen
76       *
77       */
78      private List<List<File>> getSubListsForFile(List<File> fileList, final int L) {
79          List<List<File>> subLists = new ArrayList<List<File>>();
80          final int N = fileList.size();
81          for (int i = 0; i < N; i += L) {
82              subLists.add(new ArrayList<File>(fileList.subList(i, Math.min(N, i + L))));
83          }
84          return subLists;
85      }
86  
87      protected void setUpResourceDir(String resourceDir) {
88          try {
89              setUpFiles("src/it/resources/" + resourceDir);
90          } catch (Exception e) {
91              System.out.println("Problem loading files from filesystem ( " + e.getMessage() + "). If running from Intellij make sure working directory is rice-middleware/sampleapp attempt to load as resource.");
92              try {
93                  setUpResourceFiles(resourceDir);
94              } catch (Exception e1) {
95                  e1.printStackTrace();
96                  jiraAwareFail("Problems loading files as resources " + e1.getMessage());
97              }
98          }
99      }
100 
101     protected void setUpResourceFiles(String resourceDir) throws Exception {
102         String[] resources = getResourceListing(getClass(), resourceDir);
103         fileUploadList = new ArrayList<File>();
104 
105         for (String resource : resources) {
106             InputStream inputStream = getClass().getResourceAsStream(resource);
107             File file = new File(System.getProperty("java.io.tmpdir") + File.separator + resource);
108             OutputStream outputStream = new FileOutputStream(file);
109             IOUtils.copy(inputStream, outputStream);
110             outputStream.close();
111             fileUploadList.add(file);
112         }
113         Collections.sort(fileUploadList);
114     }
115 
116     protected String[] getResourceListing(Class clazz, String pathStartsWith) throws Exception {
117         String classPath = clazz.getName().replace(".", "/")+".class";
118         URL dirUrl = clazz.getClassLoader().getResource(classPath);
119 
120         if (!"jar".equals(dirUrl.getProtocol())) {
121             throw new UnsupportedOperationException("Cannot list files for URL " + dirUrl);
122         }
123 
124         String jarPath = dirUrl.getPath().substring(5, dirUrl.getPath().indexOf("!")); //strip out only the JAR file
125         JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
126         Enumeration<JarEntry> entries = jar.entries();
127         Set<String> result = new HashSet<String>();
128 
129         while(entries.hasMoreElements()) {
130             String entry = entries.nextElement().getName();
131             if (entry.startsWith(pathStartsWith) && !entry.endsWith("/")) { //filter according to the pathStartsWith skipping directories
132                 result.add(entry);
133             }
134         }
135 
136         return result.toArray(new String[result.size()]);
137     }
138 
139     protected void setUpFiles(String path) throws Exception {
140         fileUploadList = new ArrayList<File>();
141 
142         File dir = new File(path);
143 
144         if (dir != null && dir.listFiles().length > 0) {
145             Integer i = 1;
146             for (File file : dir.listFiles()) {
147                 if (file.getName().endsWith(".xml")) {
148                     if (!file.getName().equalsIgnoreCase("sample-app-config.xml"))
149                         fileUploadList.add(file);
150                 }
151                 i++;
152             }
153             Collections.sort(fileUploadList);
154         } else {
155             throw new Exception("----Resources not found----");
156         }
157     }
158 
159     protected void testEdocLiteIngestion() throws Exception {
160         testXmlIngesterSuccessfulFileUpload();
161 
162         Thread.sleep(2000);
163         driver.switchTo().defaultContent();
164         waitAndClickByLinkText("Main Menu");
165         waitAndClickByLinkText("eDoc Lite");
166 
167         selectFrameIframePortlet();
168         waitIsVisible(By.cssSelector("input.tinybutton:nth-child(1)")); // why name methodToCall.search fails?
169         waitAndClick(By.cssSelector("input.tinybutton:nth-child(1)"));
170         Thread.sleep(2000);
171         driver.switchTo().defaultContent();
172         Thread.sleep(1000);
173         waitIsVisible(By.className("exportlinks"));
174         selectFrameIframePortlet();
175     }
176 
177     /**
178      * Uploads file available from fileUploadList through XML Ingester.
179      * Uploads each sublist from main fileUploadList if size greater than 10.
180      *
181      */
182     public void testXmlIngesterSuccessfulFileUpload() throws Exception {
183         if (fileUploadList == null && fileUploadList.isEmpty()) {
184             return;
185         }
186         if (fileUploadList.size() > 10) {
187             List<List<File>> subLists = getSubListsForFile(fileUploadList, 10);
188             for (List<File> fileSet : subLists) {
189                 fileIngester(fileSet);
190                 for (File file : fileSet) {
191                     checkMessages(file);
192                 }
193             }
194         } else {
195             fileIngester(fileUploadList);
196             for (File file : fileUploadList) {
197                 checkMessages(file);
198             }
199         }
200     }
201 
202     private void checkMessages(File file) throws InterruptedException {
203         waitIsVisible(By.className("error")); // messages appear in error too.
204         if (!isTextPresent("without allowOverwrite set")) { // docs should still be present
205             // from previous run, if not we'll fail when we assert they exist.
206             // xml ingestion can take a long, long time
207             waitForTextPresent("Ingested xml doc: " + file.getName(), 360);
208         }
209     }
210 }