View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.labs.inquiries;
17  
18  import org.apache.commons.io.IOUtils;
19  import org.apache.commons.lang.StringUtils;
20  import org.junit.Test;
21  import org.kuali.rice.testtools.selenium.AutomatedFunctionalTestUtils;
22  import org.kuali.rice.testtools.selenium.WebDriverUtils;
23  import org.openqa.selenium.By;
24  
25  import java.io.File;
26  import java.io.FileOutputStream;
27  import java.io.InputStream;
28  import java.io.OutputStream;
29  import java.net.URL;
30  import java.net.URLDecoder;
31  import java.util.ArrayList;
32  import java.util.Collections;
33  import java.util.Enumeration;
34  import java.util.HashSet;
35  import java.util.List;
36  import java.util.Set;
37  import java.util.jar.JarEntry;
38  import java.util.jar.JarFile;
39  
40  /**
41   * @author Kuali Rice Team (rice.collab@kuali.org)
42   */
43  public class LabsInquiryDataObjectAttachmentAft extends LabsInquiryBase {
44  
45      /**
46       * /kr-krad/kradsampleapp?viewId=KradInquirySample-PageR6C3
47       */
48      public static final String BOOKMARK_URL = "/kr-krad/kradsampleapp?viewId=KradInquirySample-PageR6C3";
49      
50      protected List<File> fileUploadList;
51  
52      @Override
53      protected String getBookmarkUrl() {
54          return BOOKMARK_URL;
55      }
56  
57      @Override
58      protected void navigate() throws Exception {
59      	navigateToInquiry("Inquiry - Data Object Attachments");
60      }
61  
62      protected void testInquiryforDataObjectWithAttachment() throws Exception {
63          String attachmentId = createDataObjectAttacmentDocument();
64          driver.navigate().back();
65          waitAndClickByLinkText("Travel Attachment Lookup");
66          waitAndTypeByName("lookupCriteria[id]", attachmentId);
67          waitAndClickButtonByText("Search");
68          waitAndClickLinkContainingText("attachment");
69          gotoLightBox();
70          waitForElementPresentByXpath("//button[contains(text(),'download attachment')]");
71          waitAndClickButtonByExactText("download attachment");
72      }
73  
74      protected void testInquiryforDataObjectWithAttachmentCollection() throws Exception {
75          String number = createDataObjectAttacmentCollectionDocument();
76          driver.navigate().back();
77          waitAndClickByLinkText("Travel Attachment Collection Lookup");
78          waitAndTypeByName("lookupCriteria[number]", number);
79          waitAndClickButtonByText("Search");
80          waitAndClickLinkContainingText(number);
81          gotoLightBox();
82          waitForElementPresentByXpath("//button[contains(text(),'download attachment')]");
83          waitAndClickButtonByExactText("download attachment");
84      }
85  
86      protected String createDataObjectAttacmentDocument() throws Exception {
87          waitAndClickByLinkText("Create Travel Attachment");
88          String desc = getDescriptionUnique();
89          String attachmentId = "attachment" + uniqueString;
90          waitAndTypeByName("document.documentHeader.documentDescription", desc);
91          waitAndTypeByName("document.newMaintainableObject.dataObject.id",attachmentId);
92          waitAndTypeByName("document.newMaintainableObject.dataObject.travelAttachmentGroupNumber","123");
93          waitAndTypeByName("document.newMaintainableObject.dataObject.description", desc);
94          fileUploadSetUp();
95          fileIngester();
96          waitAndClickButtonByText("Submit");
97          waitAndClickByXpath("//div[@data-parent='ConfirmSubmitDialog']/button[contains(text(),'OK')]");
98          waitForElementPresentByXpath("//button[contains(text(),'download attachment')]");
99          return attachmentId;
100     }
101 
102     protected String createDataObjectAttacmentCollectionDocument() throws Exception {
103         waitAndClickByLinkText("Create Travel Attachment Collection");
104         String desc = getDescriptionUnique();
105         waitAndTypeByName("document.documentHeader.documentDescription", desc);
106         String number = "Num" + uniqueString.substring(6, 13); // number length is not validated, max length is 10
107         waitAndTypeByName("document.newMaintainableObject.dataObject.number", number);
108         waitAndTypeByName("document.newMaintainableObject.dataObject.name", "Name" + uniqueString);
109         fileUploadSetUp();
110 
111         if (fileUploadList != null && fileUploadList.size() > 0) {
112             for (File file : fileUploadList) {
113                 waitAndTypeByName("newCollectionLines['document.newMaintainableObject.dataObject.attachments'].id", "attachment" + uniqueString);
114                 String path = file.getAbsolutePath().toString();
115                 waitIsVisibleByXpath("//div[@data-label='Attached File']/fieldset/div/div/input[@type='file']");
116                 driver.findElement(By.xpath("//div[@data-label='Attached File']/fieldset/div/div/input[@type='file']"))
117                         .sendKeys(path);
118                 System.out.println("In for " + path);
119                 waitAndClickButtonByExactText("Add");
120                 waitForTextPresent("Adding Line...");
121             }
122         }
123 
124         waitForElementPresentByXpath("//button[contains(text(),'download attachment')]");
125         waitAndClickButtonByText("Submit");
126         waitForProgressLoading();
127         waitAndClickByXpath("//div[@data-parent='ConfirmSubmitDialog']/button[contains(text(),'OK')]");
128         waitForElementPresentByXpath("//button[contains(text(),'download attachment')]");
129         waitForTextPresent("Document was successfully submitted.", WebDriverUtils.configuredImplicityWait() * 2);
130         return number;
131     }
132 
133     private void fileUploadSetUp() throws Exception {
134     	setUpResourceDir("general");
135     }
136 
137     protected void setUpResourceDir(String resourceDir) {
138         try {
139             setUpFiles("src/test/resources/" + resourceDir);
140             System.out.println("Try for setUpResourceDir");
141         } catch (Exception e) {
142             System.out.println("Problem loading files from filesystem ( " + e.getMessage() + "). If running from "
143                     + "Intellij make sure working directory is rice-framework/krad-sampleapp/web attempt "
144                     + "to load as resource.");
145                 // Example setUpFiles("rice-framework/krad-sampleapp/web/src/test/resources/" + resourceDir);
146             try {
147                 setUpResourceFiles(resourceDir);
148             } catch (Exception e1) {
149                 e1.printStackTrace();
150                 jiraAwareFail("Problems loading files as resources " + e1.getMessage());
151             }
152             System.out.println("Catch for setUpResourceDir");
153         }
154     }
155 
156     protected void setUpResourceFiles(String resourceDir) throws Exception {
157         System.out.println("In for setUpResourceFiles");
158         String[] resources = getResourceListing(getClass(), resourceDir);
159         fileUploadList = new ArrayList<File>();
160 
161         for (String resource : resources) {
162             InputStream inputStream = getClass().getResourceAsStream(resource);
163             File file = new File(System.getProperty("java.io.tmpdir") + File.separator + resource);
164             OutputStream outputStream = new FileOutputStream(file);
165             IOUtils.copy(inputStream, outputStream);
166             outputStream.close();
167             fileUploadList.add(file);
168             System.out.println("For for setUpResourceFiles");
169         }
170 
171         Collections.sort(fileUploadList);
172     }
173 
174     protected String[] getResourceListing(Class clazz, String pathStartsWith) throws Exception {
175         System.out.println("In for getResourceListing");
176         String classPath = clazz.getName().replace(".", "/")+".class";
177         URL dirUrl = clazz.getClassLoader().getResource(classPath);
178 
179         if (!"jar".equals(dirUrl.getProtocol())) {
180             throw new UnsupportedOperationException("Cannot list files for URL " + dirUrl);
181         }
182 
183         String jarPath = dirUrl.getPath().substring(5, dirUrl.getPath().indexOf("!")); //strip out only the JAR file
184         JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
185         Enumeration<JarEntry> entries = jar.entries();
186         Set<String> result = new HashSet<String>();
187 
188         while(entries.hasMoreElements()) {
189             String entry = entries.nextElement().getName();
190             if (entry.startsWith(pathStartsWith) && !entry.endsWith("/")) { //filter according to the pathStartsWith skipping directories
191                 result.add(entry);
192             }
193         }
194 
195         return result.toArray(new String[result.size()]);
196     }
197 
198     protected void setUpFiles(String path) throws Exception {
199         System.out.println("In for setUpFiles");
200         fileUploadList = new ArrayList<File>();
201 
202         File dir = new File(path);
203 
204         if (dir != null && dir.listFiles().length > 0) {
205             Integer i = 1;
206 
207             for (File file : dir.listFiles()) {
208                 if (file.getName().endsWith(".txt")) {
209                     fileUploadList.add(file);
210                 }
211 
212                 i++;
213             }
214 
215             Collections.sort(fileUploadList);
216         } else {
217             throw new Exception("----Resources not found----");
218         }
219     }
220 
221     /**
222      * Performs Ingesting files to fileupload component and asserts successful ingestion.
223      *
224      */
225     private void fileIngester() throws Exception {
226     	System.out.println("In for fileIngester");
227         if(fileUploadList!=null && fileUploadList.size()>0)
228         {
229 	        for (File file : fileUploadList) {
230 	            String path = file.getAbsolutePath().toString();
231 	            driver.findElement(By.name("document.newMaintainableObject.dataObject.attachmentFile")).sendKeys(path);
232 	            System.out.println("In for -------");
233 	        }
234         }
235     }
236 
237     @Test
238     public void testInquiryforDataObjectWithAttachmentBookmark() throws Exception {
239         testInquiryforDataObjectWithAttachment();
240         passed();
241     }
242 
243     @Test
244     public void testInquiryforDataObjectWithAttachmentNav() throws Exception {
245         testInquiryforDataObjectWithAttachment();
246         passed();
247     }
248 
249     @Test
250     public void testInquiryforDataObjectWithAttachmentCollectionBookmark() throws Exception {
251         testInquiryforDataObjectWithAttachmentCollection();
252         passed();
253     }
254 
255     @Test
256     public void testInquiryforDataObjectWithAttachmentCollectionNav() throws Exception {
257         testInquiryforDataObjectWithAttachmentCollection();
258         passed();
259     }
260 }