View Javadoc

1   /**
2    * Copyright 2005-2011 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.samplu.admin.test;
17  
18  import edu.samplu.common.AdminMenuLegacyITBase;
19  import edu.samplu.common.ITUtil;
20  import freemarker.cache.ClassTemplateLoader;
21  import freemarker.template.Configuration;
22  import freemarker.template.Template;
23  import freemarker.template.TemplateException;
24  import org.apache.commons.io.FileUtils;
25  import org.apache.log4j.Logger;
26  import org.junit.Ignore;
27  import org.junit.Rule;
28  import org.junit.Test;
29  import org.junit.rules.TemporaryFolder;
30  import org.openqa.selenium.By;
31  import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
32  
33  import java.io.File;
34  import java.io.FileInputStream;
35  import java.io.IOException;
36  import java.io.InputStream;
37  import java.util.ArrayList;
38  import java.util.Enumeration;
39  import java.util.List;
40  import java.util.Properties;
41  
42  /**
43   * tests uploads of new users and group
44   *
45   * @author Kuali Rice Team (rice.collab@kuali.org)
46   */
47  public class XMLIngesterLegacyIT extends AdminMenuLegacyITBase {
48  
49      protected final Logger LOG = Logger.getLogger(getClass());
50  
51      // File generation
52      private Configuration cfg;
53      private String PROPS_LOCATION = System.getProperty("xmlingester.props.location", null);
54      private String DEFAULT_PROPS_LOCATION = "XML/xmlingester.properties";
55  
56      // Templates for File Generation
57      private static final String DIR_TMPL = "/XML/";
58      private static final String TMPL_USER_CONTENT = "SimpleUserContent.ftl";
59      private static final String TMPL_GROUP_CONTENT = "SimpleGroupContent.ftl";
60  
61      @Rule
62      public TemporaryFolder folder= new TemporaryFolder();
63  
64      @Ignore
65      @Override
66      public void testCreateNewCancel() throws Exception {}
67  
68      @Ignore
69      @Override
70      public void testEditCancel() throws Exception {}
71  
72  
73      @Override
74      protected String getLinkLocator() {
75          return "XML Ingester";
76      }
77  
78      @Override
79      public String getUserName() {
80          return "admin"; // xml ingestion requires admin permissions
81      }
82  
83      @Override
84      public void setUp() throws Exception {
85          super.setUp();
86          // generated load users and group resources
87          cfg = new Configuration();
88          cfg.setTemplateLoader(new ClassTemplateLoader(getClass().getClassLoader().getClass(), DIR_TMPL));
89      }
90  
91      private List<File> buildFileUploadList() throws Exception {
92          List<File> fileUploadList = new ArrayList<File>();
93          try {
94              // update properties with timestamp value if includeDTSinPrefix is true
95              Properties props = loadProperties(PROPS_LOCATION, DEFAULT_PROPS_LOCATION);
96              if(props.get("userIncludeDTSinPrefix") != null
97                      && "true".equalsIgnoreCase((String) props.get("userIncludeDTSinPrefix"))) {
98                  props.setProperty("userPrefix", "" + props.get("userPrefix") + ITUtil.DTS);
99              }
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 }