001 /** 002 * Copyright 2005-2011 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.samplu.admin.test; 017 018 import edu.samplu.common.AdminMenuLegacyITBase; 019 import edu.samplu.common.ITUtil; 020 import freemarker.cache.ClassTemplateLoader; 021 import freemarker.template.Configuration; 022 import freemarker.template.Template; 023 import freemarker.template.TemplateException; 024 import org.apache.commons.io.FileUtils; 025 import org.apache.log4j.Logger; 026 import org.junit.Ignore; 027 import org.junit.Rule; 028 import org.junit.Test; 029 import org.junit.rules.TemporaryFolder; 030 import org.openqa.selenium.By; 031 import org.springframework.ui.freemarker.FreeMarkerTemplateUtils; 032 033 import java.io.File; 034 import java.io.FileInputStream; 035 import java.io.IOException; 036 import java.io.InputStream; 037 import java.util.ArrayList; 038 import java.util.Enumeration; 039 import java.util.List; 040 import java.util.Properties; 041 042 /** 043 * tests uploads of new users and group 044 * 045 * @author Kuali Rice Team (rice.collab@kuali.org) 046 */ 047 public class XMLIngesterLegacyIT extends AdminMenuLegacyITBase { 048 049 protected final Logger LOG = Logger.getLogger(getClass()); 050 051 // File generation 052 private Configuration cfg; 053 private String PROPS_LOCATION = System.getProperty("xmlingester.props.location", null); 054 private String DEFAULT_PROPS_LOCATION = "XML/xmlingester.properties"; 055 056 // Templates for File Generation 057 private static final String DIR_TMPL = "/XML/"; 058 private static final String TMPL_USER_CONTENT = "SimpleUserContent.ftl"; 059 private static final String TMPL_GROUP_CONTENT = "SimpleGroupContent.ftl"; 060 061 @Rule 062 public TemporaryFolder folder= new TemporaryFolder(); 063 064 @Ignore 065 @Override 066 public void testCreateNewCancel() throws Exception {} 067 068 @Ignore 069 @Override 070 public void testEditCancel() throws Exception {} 071 072 073 @Override 074 protected String getLinkLocator() { 075 return "XML Ingester"; 076 } 077 078 @Override 079 public String getUserName() { 080 return "admin"; // xml ingestion requires admin permissions 081 } 082 083 @Override 084 public void setUp() throws Exception { 085 super.setUp(); 086 // generated load users and group resources 087 cfg = new Configuration(); 088 cfg.setTemplateLoader(new ClassTemplateLoader(getClass().getClassLoader().getClass(), DIR_TMPL)); 089 } 090 091 private List<File> buildFileUploadList() throws Exception { 092 List<File> fileUploadList = new ArrayList<File>(); 093 try { 094 // update properties with timestamp value if includeDTSinPrefix is true 095 Properties props = loadProperties(PROPS_LOCATION, DEFAULT_PROPS_LOCATION); 096 if(props.get("userIncludeDTSinPrefix") != null 097 && "true".equalsIgnoreCase((String) props.get("userIncludeDTSinPrefix"))) { 098 props.setProperty("userPrefix", "" + props.get("userPrefix") + ITUtil.DTS); 099 } 100 systemPropertiesOverride(props); 101 102 // build files and add to array 103 fileUploadList.add( 104 writeTemplateToFile( 105 folder.newFile("loadtest-users.xml"), cfg.getTemplate(TMPL_USER_CONTENT), props)); 106 fileUploadList.add( 107 writeTemplateToFile( 108 folder.newFile("loadtest-group.xml"), cfg.getTemplate(TMPL_GROUP_CONTENT), props)); 109 } catch( Exception e) { 110 throw new Exception("Unable to generate files for upload", e); 111 } 112 return fileUploadList; 113 } 114 115 /** 116 * -DXMLIngester.userCnt=176 will override the userCnt in property files. 117 * @param props 118 */ 119 private void systemPropertiesOverride(Properties props) { 120 Enumeration<?> names = props.propertyNames(); 121 Object nameObject; 122 String name; 123 while (names.hasMoreElements()) { 124 nameObject = names.nextElement(); 125 if (nameObject instanceof String) { 126 name = (String)nameObject; 127 props.setProperty(name, System.getProperty("XMLIngester." + name, props.getProperty(name))); 128 } 129 } 130 } 131 132 /** 133 * Based on load user and groups manual tests; dynamically generates user and group file 134 * and loads into the xml ingester screen 135 * 136 */ 137 @Test 138 public void testXMLIngesterSuccessfulFileUpload() throws Exception { 139 List<File> fileUploadList = buildFileUploadList(); 140 gotoMenuLinkLocator(); 141 int cnt = 0; 142 for(File file : fileUploadList) { 143 String path = file.getAbsolutePath().toString(); 144 driver.findElement(By.name("file[" + cnt + "]")).sendKeys(path); 145 cnt++; 146 } 147 waitAndClickByXpath("//*[@id='imageField']"); 148 149 // confirm all files were uploaded successfully 150 for(File file: fileUploadList) { 151 assertTextPresent("Ingested xml doc: " + file.getName()); 152 } 153 passed(); 154 } 155 156 /** 157 * Loads properties from user defined properties file, if not available uses resource file 158 * 159 * @return 160 * @throws IOException 161 */ 162 private Properties loadProperties(String fileLocation, String resourceLocation) throws IOException { 163 Properties props = new Properties(); 164 InputStream in = null; 165 if(fileLocation != null) { 166 in = new FileInputStream(fileLocation); 167 } else { 168 in = getClass().getClassLoader().getResourceAsStream(resourceLocation); 169 } 170 if(in != null) { 171 props.load(in); 172 in.close(); 173 } 174 return props; 175 } 176 177 /** 178 * writes processed template to file 179 * 180 * @param file 181 * @param template 182 * @param props 183 * @return 184 * @throws IOException 185 * @throws TemplateException 186 */ 187 private File writeTemplateToFile(File file, Template template, Properties props) throws IOException, TemplateException { 188 String output = FreeMarkerTemplateUtils.processTemplateIntoString(template, props); 189 LOG.debug("Generated File Output: " + output); 190 FileUtils.writeStringToFile(file, output); 191 return file; 192 } 193 }