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