1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package edu.sampleu.admin;
17
18 import org.apache.commons.io.IOUtils;
19 import org.openqa.selenium.By;
20
21 import java.io.File;
22 import java.io.FileOutputStream;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.net.URL;
26 import java.net.URLDecoder;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.Enumeration;
30 import java.util.HashSet;
31 import java.util.List;
32 import java.util.Set;
33 import java.util.jar.JarEntry;
34 import java.util.jar.JarFile;
35
36
37
38
39
40 public abstract class EdocLiteXmlIngesterBase extends AdminTmplMthdAftNavBase {
41
42 protected List<File> fileUploadList;
43
44 @Override
45 protected String getBookmarkUrl() {
46 return null;
47 }
48
49
50
51
52
53
54 @Override
55 protected String getLinkLocator() {
56 return "XML Ingester";
57 }
58
59
60
61
62
63 private void fileIngester(List<File> fileToUpload) throws Exception {
64 int cnt = 0;
65 for (File file : fileToUpload) {
66 String path = file.getAbsolutePath().toString();
67 driver.findElement(By.name("file[" + cnt + "]")).sendKeys(path);
68 cnt++;
69 }
70 waitAndClickById("imageField");
71 }
72
73
74
75
76
77
78 private List<List<File>> getSubListsForFile(List<File> fileList, final int L) {
79 List<List<File>> subLists = new ArrayList<List<File>>();
80 final int N = fileList.size();
81 for (int i = 0; i < N; i += L) {
82 subLists.add(new ArrayList<File>(fileList.subList(i, Math.min(N, i + L))));
83 }
84 return subLists;
85 }
86
87 protected void setUpResourceDir(String resourceDir) {
88 try {
89 setUpFiles("src/it/resources/" + resourceDir);
90 } catch (Exception e) {
91 System.out.println("Problem loading files from filesystem ( " + e.getMessage() + "). If running from Intellij make sure working directory is rice-middleware/sampleapp attempt to load as resource.");
92 try {
93 setUpResourceFiles(resourceDir);
94 } catch (Exception e1) {
95 e1.printStackTrace();
96 jiraAwareFail("Problems loading files as resources " + e1.getMessage());
97 }
98 }
99 }
100
101 protected void setUpResourceFiles(String resourceDir) throws Exception {
102 String[] resources = getResourceListing(getClass(), resourceDir);
103 fileUploadList = new ArrayList<File>();
104
105 for (String resource : resources) {
106 InputStream inputStream = getClass().getResourceAsStream(resource);
107 File file = new File(System.getProperty("java.io.tmpdir") + File.separator + resource);
108 OutputStream outputStream = new FileOutputStream(file);
109 IOUtils.copy(inputStream, outputStream);
110 outputStream.close();
111 fileUploadList.add(file);
112 }
113 Collections.sort(fileUploadList);
114 }
115
116 protected String[] getResourceListing(Class clazz, String pathStartsWith) throws Exception {
117 String classPath = clazz.getName().replace(".", "/")+".class";
118 URL dirUrl = clazz.getClassLoader().getResource(classPath);
119
120 if (!"jar".equals(dirUrl.getProtocol())) {
121 throw new UnsupportedOperationException("Cannot list files for URL " + dirUrl);
122 }
123
124 String jarPath = dirUrl.getPath().substring(5, dirUrl.getPath().indexOf("!"));
125 JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
126 Enumeration<JarEntry> entries = jar.entries();
127 Set<String> result = new HashSet<String>();
128
129 while(entries.hasMoreElements()) {
130 String entry = entries.nextElement().getName();
131 if (entry.startsWith(pathStartsWith) && !entry.endsWith("/")) {
132 result.add(entry);
133 }
134 }
135
136 return result.toArray(new String[result.size()]);
137 }
138
139 protected void setUpFiles(String path) throws Exception {
140 fileUploadList = new ArrayList<File>();
141
142 File dir = new File(path);
143
144 if (dir != null && dir.listFiles().length > 0) {
145 Integer i = 1;
146 for (File file : dir.listFiles()) {
147 if (file.getName().endsWith(".xml")) {
148 if (!file.getName().equalsIgnoreCase("sample-app-config.xml"))
149 fileUploadList.add(file);
150 }
151 i++;
152 }
153 Collections.sort(fileUploadList);
154 } else {
155 throw new Exception("----Resources not found----");
156 }
157 }
158
159 protected void testEdocLiteIngestion() throws Exception {
160 testXmlIngesterSuccessfulFileUpload();
161
162 Thread.sleep(2000);
163 driver.switchTo().defaultContent();
164 waitAndClickByLinkText("Main Menu");
165 waitAndClickByLinkText("eDoc Lite");
166
167 selectFrameIframePortlet();
168 waitIsVisible(By.cssSelector("input.tinybutton:nth-child(1)"));
169 waitAndClick(By.cssSelector("input.tinybutton:nth-child(1)"));
170 Thread.sleep(2000);
171 driver.switchTo().defaultContent();
172 Thread.sleep(1000);
173 waitIsVisible(By.className("exportlinks"));
174 selectFrameIframePortlet();
175 }
176
177
178
179
180
181
182 public void testXmlIngesterSuccessfulFileUpload() throws Exception {
183 if (fileUploadList == null && fileUploadList.isEmpty()) {
184 return;
185 }
186 if (fileUploadList.size() > 10) {
187 List<List<File>> subLists = getSubListsForFile(fileUploadList, 10);
188 for (List<File> fileSet : subLists) {
189 fileIngester(fileSet);
190 for (File file : fileSet) {
191 checkMessages(file);
192 }
193 }
194 } else {
195 fileIngester(fileUploadList);
196 for (File file : fileUploadList) {
197 checkMessages(file);
198 }
199 }
200 }
201
202 private void checkMessages(File file) throws InterruptedException {
203 waitIsVisible(By.className("error"));
204 if (!isTextPresent("without allowOverwrite set")) {
205
206
207 waitForTextPresent("Ingested xml doc: " + file.getName(), 360);
208 }
209 }
210 }