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.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
48
49
50 public class XMLIngesterNavIT extends AdminTmplMthdSTNavBase {
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
76 @Override
77 protected String getLinkLocator() {
78 return "XML Ingester";
79 }
80
81 @Override
82 public String getUserName() {
83 return "admin";
84 }
85
86 @Override
87 public void setUp() throws Exception {
88 super.setUp();
89
90 cfg = new Configuration();
91 cfg.setTemplateLoader(new ClassTemplateLoader(getClass().getClassLoader().getClass(), DIR_TMPL));
92 }
93
94 private List<File> buildFileUploadList() throws Exception {
95 List<File> fileUploadList = new ArrayList<File>();
96 try {
97
98 Properties props = loadProperties(PROPS_LOCATION, DEFAULT_PROPS_LOCATION);
99 if(props.get("userIncludeDTSinPrefix") != null
100 && "true".equalsIgnoreCase((String) props.get("userIncludeDTSinPrefix"))) {
101 props.setProperty("userPrefix", "" + props.get("userPrefix") + ITUtil.DTS);
102 }
103 systemPropertiesOverride(props);
104
105
106 fileUploadList.add(
107 writeTemplateToFile(
108 folder.newFile("loadtest-users.xml"), cfg.getTemplate(TMPL_USER_CONTENT), props));
109 fileUploadList.add(
110 writeTemplateToFile(
111 folder.newFile("loadtest-group.xml"), cfg.getTemplate(TMPL_GROUP_CONTENT), props));
112 } catch( Exception e) {
113 throw new Exception("Unable to generate files for upload", e);
114 }
115 return fileUploadList;
116 }
117
118
119
120
121
122 private void systemPropertiesOverride(Properties props) {
123 Enumeration<?> names = props.propertyNames();
124 Object nameObject;
125 String name;
126 while (names.hasMoreElements()) {
127 nameObject = names.nextElement();
128 if (nameObject instanceof String) {
129 name = (String)nameObject;
130 props.setProperty(name, System.getProperty("XMLIngester." + name, props.getProperty(name)));
131 }
132 }
133 }
134
135
136
137
138
139
140 @Test
141 public void testXMLIngesterSuccessfulFileUpload() throws Exception {
142 List<File> fileUploadList = buildFileUploadList();
143 gotoMenuLinkLocator();
144 int cnt = 0;
145 for(File file : fileUploadList) {
146 String path = file.getAbsolutePath().toString();
147 driver.findElement(By.name("file[" + cnt + "]")).sendKeys(path);
148 cnt++;
149 }
150 waitAndClickByXpath("//*[@id='imageField']");
151
152
153 for(File file: fileUploadList) {
154 assertTextPresent("Ingested xml doc: " + file.getName());
155 }
156 passed();
157 }
158
159
160
161
162
163
164
165 private Properties loadProperties(String fileLocation, String resourceLocation) throws IOException {
166 Properties props = new Properties();
167 InputStream in = null;
168 if(fileLocation != null) {
169 in = new FileInputStream(fileLocation);
170 } else {
171 in = getClass().getClassLoader().getResourceAsStream(resourceLocation);
172 }
173 if(in != null) {
174 props.load(in);
175 in.close();
176 }
177 return props;
178 }
179
180
181
182
183
184
185
186
187
188
189
190 private File writeTemplateToFile(File file, Template template, Properties props) throws IOException, TemplateException {
191 String output = FreeMarkerTemplateUtils.processTemplateIntoString(template, props);
192 LOG.debug("Generated File Output: " + output);
193 FileUtils.writeStringToFile(file, output);
194 return file;
195 }
196 }