View Javadoc

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