1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.Assert;
25 import org.junit.Ignore;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.TemporaryFolder;
29 import org.kuali.rice.testtools.selenium.AutomatedFunctionalTestUtils;
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
44
45
46
47
48
49
50 public class XmlIngester extends AdminTmplMthdAftNavBase {
51
52 protected final Logger LOG = Logger.getLogger(getClass());
53
54
55 private Configuration cfg;
56 private String PROPS_LOCATION = System.getProperty("xmlingester.props.location", null);
57 private String DEFAULT_PROPS_LOCATION = "XML/xmlingester.properties";
58
59
60 private static final String DIR_TMPL = "/XML/";
61 private static final String TMPL_USER_CONTENT = "SimpleUserContent.ftl";
62 private static final String TMPL_GROUP_CONTENT = "SimpleGroupContent.ftl";
63
64 @Rule
65 public TemporaryFolder folder= new TemporaryFolder();
66
67 @Ignore
68 @Override
69 public void testCreateNewCancel() throws Exception {}
70
71 @Ignore
72 @Override
73 public void testEditCancel() throws Exception {}
74
75 @Override
76 protected String getBookmarkUrl() {
77 return null;
78 }
79
80 @Override
81 protected String getLinkLocator() {
82 return "XML Ingester";
83 }
84
85 @Override
86 public String getUserName() {
87 return "admin";
88 }
89
90 @Override
91 public void testSetUp() {
92 super.testSetUp();
93
94 cfg = new Configuration();
95 cfg.setTemplateLoader(new ClassTemplateLoader(getClass().getClassLoader().getClass(), DIR_TMPL));
96 }
97
98 private List<File> buildFileUploadList() throws Exception {
99 List<File> fileUploadList = new ArrayList<File>();
100 try {
101
102 Properties props = loadProperties(PROPS_LOCATION, DEFAULT_PROPS_LOCATION);
103 if(props.get("userIncludeDTSinPrefix") != null
104 && "true".equalsIgnoreCase((String) props.get("userIncludeDTSinPrefix"))) {
105 props.setProperty("userPrefix", "" + props.get("userPrefix") + AutomatedFunctionalTestUtils.DTS);
106 }
107 systemPropertiesOverride(props);
108
109
110 fileUploadList.add(
111 writeTemplateToFile(
112 folder.newFile("loadtest-users.xml"), cfg.getTemplate(TMPL_USER_CONTENT), props));
113 fileUploadList.add(
114 writeTemplateToFile(
115 folder.newFile("loadtest-group.xml"), cfg.getTemplate(TMPL_GROUP_CONTENT), props));
116 } catch( Exception e) {
117 throw new Exception("Unable to generate files for upload", e);
118 }
119 return fileUploadList;
120 }
121
122
123
124
125
126 private void systemPropertiesOverride(Properties props) {
127 Enumeration<?> names = props.propertyNames();
128 Object nameObject;
129 String name;
130 while (names.hasMoreElements()) {
131 nameObject = names.nextElement();
132 if (nameObject instanceof String) {
133 name = (String)nameObject;
134 props.setProperty(name, System.getProperty("XMLIngester." + name, props.getProperty(name)));
135 }
136 }
137 }
138
139
140
141
142
143
144 @Test
145 public void testXMLIngesterSuccessfulFileUpload() throws Exception {
146 List<File> fileUploadList = buildFileUploadList();
147 navigate();
148 int cnt = 0;
149 for(File file : fileUploadList) {
150 String path = file.getAbsolutePath().toString();
151 driver.findElement(By.name("file[" + cnt + "]")).sendKeys(path);
152 cnt++;
153 }
154 waitAndClickByXpath("//*[@id='imageField']");
155
156
157 for(File file: fileUploadList) {
158 assertTextPresent("Ingested xml doc: " + file.getName());
159 }
160 passed();
161 }
162
163
164
165
166
167
168
169 private Properties loadProperties(String fileLocation, String resourceLocation) throws IOException {
170 Properties props = new Properties();
171 InputStream in = null;
172 if(fileLocation != null) {
173 in = new FileInputStream(fileLocation);
174 } else {
175 in = getClass().getClassLoader().getResourceAsStream(resourceLocation);
176 }
177 if(in != null) {
178 props.load(in);
179 in.close();
180 }
181 return props;
182 }
183
184
185
186
187
188
189
190
191
192
193
194 private File writeTemplateToFile(File file, Template template, Properties props) throws IOException, TemplateException {
195 String output = FreeMarkerTemplateUtils.processTemplateIntoString(template, props);
196 LOG.debug("Generated File Output: " + output);
197 FileUtils.writeStringToFile(file, output);
198 return file;
199 }
200 }