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