001 /** 002 * Copyright 2005-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package edu.sampleu.admin; 017 018 import org.apache.commons.io.IOUtils; 019 import org.openqa.selenium.By; 020 021 import java.io.File; 022 import java.io.FileOutputStream; 023 import java.io.InputStream; 024 import java.io.OutputStream; 025 import java.net.URL; 026 import java.net.URLDecoder; 027 import java.util.ArrayList; 028 import java.util.Collections; 029 import java.util.Enumeration; 030 import java.util.HashSet; 031 import java.util.List; 032 import java.util.Set; 033 import java.util.jar.JarEntry; 034 import java.util.jar.JarFile; 035 036 /** 037 * @author Kuali Rice Team (rice.collab@kuali.org) 038 */ 039 040 public abstract class EdocLiteXmlIngesterBase extends AdminTmplMthdAftNavBase { 041 // values set by default for repeatable testing; left as configurable for load tests 042 protected List<File> fileUploadList; 043 044 @Override 045 protected String getBookmarkUrl() { 046 return null; // no bookmark test yet 047 } 048 049 /** 050 * This overridden method ... 051 * 052 * @see edu.sampleu.common.NavTemplateMethodAftBase#getLinkLocator() 053 */ 054 @Override 055 protected String getLinkLocator() { 056 return "XML Ingester"; 057 } 058 059 /** 060 * Performs Ingesting files to fileupload component and asserts succesful ingestion. 061 * 062 */ 063 private void fileIngester(List<File> fileToUpload) throws Exception { 064 int cnt = 0; 065 for (File file : fileToUpload) { 066 String path = file.getAbsolutePath().toString(); 067 driver.findElement(By.name("file[" + cnt + "]")).sendKeys(path); 068 cnt++; 069 } 070 waitAndClickById("imageField"); 071 } 072 073 /** 074 * Divides fileUploadList from resources into sublists to match the maximum number of file 075 * upload components available on XML Ingester Screen 076 * 077 */ 078 private List<List<File>> getSubListsForFile(List<File> fileList, final int L) { 079 List<List<File>> subLists = new ArrayList<List<File>>(); 080 final int N = fileList.size(); 081 for (int i = 0; i < N; i += L) { 082 subLists.add(new ArrayList<File>(fileList.subList(i, Math.min(N, i + L)))); 083 } 084 return subLists; 085 } 086 087 protected void setUpResourceDir(String resourceDir) { 088 try { 089 setUpFiles("src/it/resources/" + resourceDir); 090 } catch (Exception e) { 091 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."); 092 try { 093 setUpResourceFiles(resourceDir); 094 } catch (Exception e1) { 095 e1.printStackTrace(); 096 jiraAwareFail("Problems loading files as resources " + e1.getMessage()); 097 } 098 } 099 } 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 }