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.List;
39  import java.util.Properties;
40  
41  /**
42   * tests uploads of new users and group
43   *
44   * @author Kuali Rice Team (rice.collab@kuali.org)
45   */
46  public class XMLIngesterLegacyIT extends AdminMenuLegacyITBase {
47  
48      protected final Logger LOG = Logger.getLogger(getClass());
49  
50      // File generation
51      private Configuration cfg;
52      private String PROPS_LOCATION = System.getProperty("xmlingester.props.location", null);
53      private String DEFAULT_PROPS_LOCATION = "XML/xmlingester.properties";
54  
55      // Templates for File Generation
56      private static final String DIR_TMPL = "/XML/";
57      private static final String TMPL_USER_CONTENT = "SimpleUserContent.ftl";
58      private static final String TMPL_GROUP_CONTENT = "SimpleGroupContent.ftl";
59  
60      @Rule
61      public TemporaryFolder folder= new TemporaryFolder();
62  
63      @Ignore
64      @Override
65      public void testCreateNewCancel() throws Exception {}
66  
67      @Ignore
68      @Override
69      public void testEditCancel() throws Exception {}
70  
71  
72      @Override
73      protected String getLinkLocator() {
74          return "XML Ingester";
75      }
76  
77      @Override
78      public String getUserName() {
79          return "admin"; // xml ingestion requires admin permissions
80      }
81  
82      @Override
83      public void setUp() throws Exception {
84          super.setUp();
85          // generated load users and group resources
86          cfg = new Configuration();
87          cfg.setTemplateLoader(new ClassTemplateLoader(getClass().getClassLoader().getClass(), DIR_TMPL));
88      }
89  
90      private List<File> buildFileUploadList() throws Exception {
91          List<File> fileUploadList = new ArrayList<File>();
92          try {
93              // update properties with timestamp value if includeDTSinPrefix is true
94              Properties props = loadProperties(PROPS_LOCATION, DEFAULT_PROPS_LOCATION);
95              if(props.get("userIncludeDTSinPrefix") != null
96                      && "true".equalsIgnoreCase((String) props.get("userIncludeDTSinPrefix"))) {
97                  props.setProperty("userPrefix", "" + props.get("userPrefix") + ITUtil.DTS);
98              }
99  
100             // build files and add to array
101             fileUploadList.add(
102                     writeTemplateToFile(
103                             folder.newFile("loadtest-users.xml"), cfg.getTemplate(TMPL_USER_CONTENT), props));
104             fileUploadList.add(
105                     writeTemplateToFile(
106                             folder.newFile("loadtest-group.xml"), cfg.getTemplate(TMPL_GROUP_CONTENT), props));
107         } catch( Exception e) {
108             throw new Exception("Unable to generate files for upload", e);
109         }
110         return fileUploadList;
111     }
112 
113 
114     /**
115      * Based on load user and groups manual tests; dynamically generates user and group file
116      * and loads into the xml ingester screen
117      *
118      */
119     @Test
120     public void testXMLIngesterSuccessfulFileUpload() throws Exception {
121         List<File> fileUploadList = buildFileUploadList();
122         gotoMenuLinkLocator();
123         int cnt = 0;
124         for(File file : fileUploadList) {
125             String path = file.getAbsolutePath().toString();
126             driver.findElement(By.name("file[" + cnt + "]")).sendKeys(path);
127             cnt++;
128         }
129         waitAndClickByXpath("//*[@id='imageField']");
130 
131         // confirm all files were uploaded successfully
132         for(File file: fileUploadList) {
133             assertTextPresent("Ingested xml doc: " + file.getName());
134         }
135         passed();
136     }
137 
138     /**
139      * Loads properties from user defined properties file, if not available uses resource file
140      *
141      * @return
142      * @throws IOException
143      */
144     private Properties loadProperties(String fileLocation, String resourceLocation) throws IOException {
145         Properties props = new Properties();
146         InputStream in = null;
147         if(fileLocation != null) {
148             in = new FileInputStream(fileLocation);
149         } else {
150             in = getClass().getClassLoader().getResourceAsStream(resourceLocation);
151         }
152         if(in != null) {
153             props.load(in);
154             in.close();
155         }
156         return props;
157     }
158 
159     /**
160      * writes processed template  to file
161      *
162      * @param file
163      * @param template
164      * @param props
165      * @return
166      * @throws IOException
167      * @throws TemplateException
168      */
169     private File writeTemplateToFile(File file, Template template, Properties props) throws IOException, TemplateException {
170         String output = FreeMarkerTemplateUtils.processTemplateIntoString(template, props);
171         LOG.debug("Generated File Output: " + output);
172         FileUtils.writeStringToFile(file, output);
173         return file;
174     }
175 }