View Javadoc
1   /**
2    * Copyright 2005-2015 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  
66          for (File file : fileToUpload) {
67              String path = file.getAbsolutePath().toString();
68              driver.findElement(By.name("file[" + cnt + "]")).sendKeys(path);
69              cnt++;
70          }
71  
72          waitAndClickById("imageField");
73      }
74  
75      /**
76       * Divides fileUploadList from resources into sublists to match the maximum number of file
77       * upload components available on XML Ingester Screen
78       *
79       */
80      private List<List<File>> getSubListsForFile(List<File> fileList, final int L) {
81          List<List<File>> subLists = new ArrayList<List<File>>();
82          final int N = fileList.size();
83          for (int i = 0; i < N; i += L) {
84              subLists.add(new ArrayList<File>(fileList.subList(i, Math.min(N, i + L))));
85          }
86  
87          return subLists;
88      }
89  
90      protected void setUpResourceDir(String resourceDir) {
91          try {
92              setUpFiles("src/it/resources/" + resourceDir);
93          } catch (Exception e) {
94              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.");
95              try {
96                  setUpResourceFiles(resourceDir);
97              } catch (Exception e1) {
98                  e1.printStackTrace();
99                  jiraAwareFail("Problems loading files as resources " + e1.getMessage());
100             }
101         }
102     }
103 
104     protected void setUpResourceFiles(String resourceDir) throws Exception {
105         String[] resources = getResourceListing(getClass(), resourceDir);
106         fileUploadList = new ArrayList<File>();
107 
108         for (String resource : resources) {
109             InputStream inputStream = getClass().getResourceAsStream(resource);
110             File file = new File(System.getProperty("java.io.tmpdir") + File.separator + resource);
111             OutputStream outputStream = new FileOutputStream(file);
112             IOUtils.copy(inputStream, outputStream);
113             outputStream.close();
114             fileUploadList.add(file);
115         }
116         Collections.sort(fileUploadList);
117     }
118 
119     protected String[] getResourceListing(Class clazz, String pathStartsWith) throws Exception {
120         String classPath = clazz.getName().replace(".", "/")+".class";
121         URL dirUrl = clazz.getClassLoader().getResource(classPath);
122 
123         if (!"jar".equals(dirUrl.getProtocol())) {
124             throw new UnsupportedOperationException("Cannot list files for URL " + dirUrl);
125         }
126 
127         String jarPath = dirUrl.getPath().substring(5, dirUrl.getPath().indexOf("!")); //strip out only the JAR file
128         JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
129         Enumeration<JarEntry> entries = jar.entries();
130         Set<String> result = new HashSet<String>();
131 
132         while(entries.hasMoreElements()) {
133             String entry = entries.nextElement().getName();
134             if (entry.startsWith(pathStartsWith) && !entry.endsWith("/")) { //filter according to the pathStartsWith skipping directories
135                 result.add(entry);
136             }
137         }
138 
139         return result.toArray(new String[result.size()]);
140     }
141 
142     protected void setUpFiles(String path) throws Exception {
143         fileUploadList = new ArrayList<File>();
144 
145         File dir = new File(path);
146 
147         if (dir != null && dir.listFiles().length > 0) {
148             Integer i = 1;
149             for (File file : dir.listFiles()) {
150                 if (file.getName().endsWith(".xml") && !file.getName().equalsIgnoreCase("sample-app-config.xml")) {
151                     fileUploadList.add(file);
152                 }
153                 i++;
154             }
155             Collections.sort(fileUploadList);
156         } else {
157             throw new Exception("----Resources not found----");
158         }
159     }
160 
161     protected void testEdocLiteIngestion() throws Exception {
162         testXmlIngesterSuccessfulFileUpload();
163 
164         Thread.sleep(2000);
165         driver.switchTo().defaultContent();
166         waitAndClickByLinkText("Main Menu");
167         waitAndClickByLinkText("eDoc Lite");
168 
169         selectFrameIframePortlet();
170         waitIsVisible(By.cssSelector("input.tinybutton:nth-child(1)")); // why name methodToCall.search fails?
171         waitAndClick(By.cssSelector("input.tinybutton:nth-child(1)"));
172         Thread.sleep(2000);
173         driver.switchTo().defaultContent();
174         Thread.sleep(1000);
175         waitIsVisible(By.className("exportlinks"));
176         selectFrameIframePortlet();
177     }
178 
179     /**
180      * Uploads file available from fileUploadList through XML Ingester.
181      * Uploads each sublist from main fileUploadList if size greater than 10.
182      *
183      */
184     public void testXmlIngesterSuccessfulFileUpload() throws Exception {
185         if (fileUploadList == null && fileUploadList.isEmpty()) {
186             return;
187         }
188 
189         if (fileUploadList.size() > 10) {
190             List<List<File>> subLists = getSubListsForFile(fileUploadList, 10);
191             for (List<File> fileSet : subLists) {
192                 fileIngester(fileSet);
193                 for (File file : fileSet) {
194                     checkMessages(file);
195                 }
196             }
197         } else {
198             fileIngester(fileUploadList);
199             for (File file : fileUploadList) {
200                 checkMessages(file);
201             }
202         }
203     }
204 
205     private void checkMessages(File file) throws InterruptedException {
206         waitIsVisible(By.className("error")); // messages appear in error too.
207         if (!isTextPresent("without allowOverwrite set")) { // docs should still be present
208             // from previous run, if not we'll fail when we assert they exist.
209             // xml ingestion can take a long, long time
210             waitForTextPresent("Ingested xml doc: " + file.getName(), 360);
211         }
212     }
213 }