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 freemarker.cache.ClassTemplateLoader;
19  import freemarker.template.Configuration;
20  import freemarker.template.Template;
21  import freemarker.template.TemplateException;
22  import org.apache.commons.io.FileUtils;
23  import org.apache.log4j.Logger;
24  import org.junit.Ignore;
25  import org.junit.Rule;
26  import org.junit.Test;
27  import org.junit.rules.TemporaryFolder;
28  import org.kuali.rice.testtools.selenium.AutomatedFunctionalTestUtils;
29  import org.openqa.selenium.By;
30  import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
31  
32  import java.io.File;
33  import java.io.FileInputStream;
34  import java.io.IOException;
35  import java.io.InputStream;
36  import java.util.ArrayList;
37  import java.util.Enumeration;
38  import java.util.List;
39  import java.util.Properties;
40  
41  /**
42   * tests uploads of new users and group
43   *
44   * <pre>mvn -f rice-middleware/sampleapp/pom.xml -Pstests failsafe:integration-test -Dremote.public.url=env7.rice.kuali.org -Dit.test=XMLIngester -DXMLIngester.groupId=2008 -DXMLIngester.userIncludeDTSinPrefix=false -DXMLIngester.userCntBegin=0 -DXMLIngester.userCnt=600  -DXMLIngester.userPrefix=loadtester -Dremote.driver.dontTearDown=y</pre>
45   *
46   * @deprecated XMLIngesterSTJUnitNavGen
47   * @author Kuali Rice Team (rice.collab@kuali.org)
48   */
49  public class XmlIngester extends AdminTmplMthdAftNavBase {
50  
51      protected final Logger LOG = Logger.getLogger(getClass());
52  
53      // File generation
54      private Configuration cfg;
55      private String PROPS_LOCATION = System.getProperty("xmlingester.props.location", null);
56      private String DEFAULT_PROPS_LOCATION = "XML/xmlingester.properties";
57  
58      // Templates for File Generation
59      private static final String DIR_TMPL = "/XML/";
60      private static final String TMPL_USER_CONTENT = "SimpleUserContent.ftl";
61      private static final String TMPL_GROUP_CONTENT = "SimpleGroupContent.ftl";
62  
63      @Rule
64      public TemporaryFolder folder= new TemporaryFolder();
65  
66      @Ignore // empty test, XML Ingester does not have Create New Cancel functionality
67      @Override
68      public void testCreateNewCancel() throws Exception {}
69  
70      @Ignore // empty test, XML Ingester does not have Edit Cancel functionality
71      @Override
72      public void testEditCancel() throws Exception {}
73  
74      @Override
75      protected String getBookmarkUrl() {
76          return null; // no bookmark test yet
77      }
78  
79      @Override
80      protected String getLinkLocator() {
81          return "XML Ingester";
82      }
83  
84      @Override
85      public String getUserName() {
86          return "admin"; // xml ingestion requires admin permissions
87      }
88  
89      @Override
90      public void testSetUp() {
91          super.testSetUp();
92          // generated load users and group resources
93          cfg = new Configuration();
94          cfg.setTemplateLoader(new ClassTemplateLoader(getClass().getClassLoader().getClass(), DIR_TMPL));
95      }
96  
97      private List<File> buildFileUploadList() throws Exception {
98          List<File> fileUploadList = new ArrayList<File>();
99          try {
100             // update properties with timestamp value if includeDTSinPrefix is true
101             Properties props = loadProperties(PROPS_LOCATION, DEFAULT_PROPS_LOCATION);
102             if(props.get("userIncludeDTSinPrefix") != null
103                     && "true".equalsIgnoreCase((String) props.get("userIncludeDTSinPrefix"))) {
104                 props.setProperty("userPrefix", "" + props.get("userPrefix") + AutomatedFunctionalTestUtils.DTS);
105             }
106             systemPropertiesOverride(props);
107 
108             // build files and add to array
109             fileUploadList.add(
110                     writeTemplateToFile(
111                             folder.newFile("loadtest-users.xml"), cfg.getTemplate(TMPL_USER_CONTENT), props));
112             fileUploadList.add(
113                     writeTemplateToFile(
114                             folder.newFile("loadtest-group.xml"), cfg.getTemplate(TMPL_GROUP_CONTENT), props));
115         } catch( Exception e) {
116             throw new Exception("Unable to generate files for upload", e);
117         }
118         return fileUploadList;
119     }
120 
121     /**
122      * -DXMLIngester.userCnt=176 will override the userCnt in property files.
123      * @param props
124      */
125     private void systemPropertiesOverride(Properties props) {
126         Enumeration<?> names = props.propertyNames();
127         Object nameObject;
128         String name;
129         while (names.hasMoreElements()) {
130             nameObject = names.nextElement();
131             if (nameObject instanceof String) {
132                 name = (String)nameObject;
133                 props.setProperty(name, System.getProperty("XMLIngester." + name, props.getProperty(name)));
134             }
135         }
136     }
137 
138     /**
139      * Based on load user and groups manual tests; dynamically generates user and group file
140      * and loads into the xml ingester screen
141      *
142      */
143     @Test
144     public void testXMLIngesterSuccessfulFileUploadNav() throws Exception {
145         List<File> fileUploadList = buildFileUploadList();
146         navigate();
147         int cnt = 0;
148         for(File file : fileUploadList) {
149             String path = file.getAbsolutePath().toString();
150             driver.findElement(By.name("file[" + cnt + "]")).sendKeys(path);
151             cnt++;
152         }
153         waitAndClickByXpath("//*[@id='imageField']");
154 
155         // confirm all files were uploaded successfully
156         for(File file: fileUploadList) {
157             assertTextPresent("Ingested xml doc: " + file.getName());
158         }
159         passed();
160     }
161 
162     /**
163      * Loads properties from user defined properties file, if not available uses resource file
164      *
165      * @return
166      * @throws IOException
167      */
168     private Properties loadProperties(String fileLocation, String resourceLocation) throws IOException {
169         Properties props = new Properties();
170         InputStream in = null;
171         if(fileLocation != null) {
172             in = new FileInputStream(fileLocation);
173         } else {
174             in = getClass().getClassLoader().getResourceAsStream(resourceLocation);
175         }
176         if(in != null) {
177             props.load(in);
178             in.close();
179         }
180         return props;
181     }
182 
183     /**
184      * writes processed template  to file
185      *
186      * @param file
187      * @param template
188      * @param props
189      * @return
190      * @throws IOException
191      * @throws TemplateException
192      */
193     private File writeTemplateToFile(File file, Template template, Properties props) throws IOException, TemplateException {
194         String output = FreeMarkerTemplateUtils.processTemplateIntoString(template, props);
195         LOG.debug("Generated File Output: " + output);
196         FileUtils.writeStringToFile(file, output);
197         return file;
198     }
199 }