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.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
43
44
45
46
47
48
49 public class XmlIngester extends AdminTmplMthdAftNavBase {
50
51 protected final Logger LOG = Logger.getLogger(getClass());
52
53
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
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
67 @Override
68 public void testCreateNewCancel() throws Exception {}
69
70 @Ignore
71 @Override
72 public void testEditCancel() throws Exception {}
73
74 @Override
75 protected String getBookmarkUrl() {
76 return null;
77 }
78
79 @Override
80 protected String getLinkLocator() {
81 return "XML Ingester";
82 }
83
84 @Override
85 public String getUserName() {
86 return "admin";
87 }
88
89 @Override
90 public void testSetUp() {
91 super.testSetUp();
92
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
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
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
123
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
140
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
156 for(File file: fileUploadList) {
157 assertTextPresent("Ingested xml doc: " + file.getName());
158 }
159 passed();
160 }
161
162
163
164
165
166
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
185
186
187
188
189
190
191
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 }