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.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
45
46
47
48
49
50
51 public class XMLIngesterNavIT extends AdminTmplMthdSTNavBase {
52
53 protected final Logger LOG = Logger.getLogger(getClass());
54
55
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
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";
89 }
90
91 @Override
92 public void testSetUp() {
93 super.testSetUp();
94
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
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
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
125
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
142
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
158 for(File file: fileUploadList) {
159 assertTextPresent("Ingested xml doc: " + file.getName());
160 }
161 passed();
162 }
163
164
165
166
167
168
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
187
188
189
190
191
192
193
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 }