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.maintenance;
17  
18  import java.io.File;
19  import java.io.FileOutputStream;
20  import java.io.InputStream;
21  import java.io.OutputStream;
22  import java.net.URL;
23  import java.net.URLDecoder;
24  import java.util.ArrayList;
25  import java.util.Collections;
26  import java.util.Enumeration;
27  import java.util.HashSet;
28  import java.util.List;
29  import java.util.Set;
30  import java.util.jar.JarEntry;
31  import java.util.jar.JarFile;
32  
33  import org.apache.commons.io.IOUtils;
34  import org.apache.commons.lang.StringUtils;
35  import org.junit.Test;
36  import org.kuali.rice.testtools.selenium.AutomatedFunctionalTestUtils;
37  import org.openqa.selenium.By;
38  
39  /**
40   * @author Kuali Rice Team (rice.collab@kuali.org)
41   */
42  public class LabsMaintenanceBOAttachmentAft extends LabsMaintenanceBase {
43  
44      /**
45       * /kr-krad/kradsampleapp?viewId=KradMaintenanceSample-PageR4C2
46       */
47      public static final String BOOKMARK_URL = "/kr-krad/kradsampleapp?viewId=KradMaintenanceSample-PageR4C2";
48      
49      // values set by default for repeatable testing; left as configurable for load tests
50      protected List<File> fileUploadList;
51  
52      @Override
53      protected String getBookmarkUrl() {
54          return BOOKMARK_URL;
55      }
56  
57      @Override
58      public String getUserName() {
59          return "admin"; // must have blanket approve rights
60      }
61  
62      @Override
63      protected void navigate() throws Exception {
64      	navigateToMaintenance("Maintenance Sample - Data Object Attachment");
65      }
66  
67      protected void testMaintenanceBOAttachment() throws Exception {
68          waitAndClickByLinkText("Create Travel Attachment");
69          waitAndTypeByName("document.documentHeader.documentDescription","Test for Data Object Attachment");
70          String random = AutomatedFunctionalTestUtils.createUniqueDtsPlusTwoRandomCharsNot9Digits();
71          waitAndTypeByName("document.newMaintainableObject.dataObject.id","attachment" + random);
72          waitAndTypeByName("document.newMaintainableObject.dataObject.travelAttachmentGroupNumber","123");
73          waitAndTypeByName("document.newMaintainableObject.dataObject.description", "Desc for attachment" + random);
74          fileUploadSetUp();
75          fileIngester();
76          waitAndClickSubmitByText();
77          waitAndClickConfirmSubmitOk();
78          waitForElementPresentByXpath("//button[contains(text(),'download attachment')]");
79      }
80  
81      protected void testMaintenanceBOAttachmentCollection() throws Exception {
82          waitAndClickByLinkText("Create Travel Attachment Collection");
83          waitAndTypeByName("document.documentHeader.documentDescription", "Test for Data Object Attachment Collection");
84          String randomGroup = StringUtils.substring(
85                  AutomatedFunctionalTestUtils.createUniqueDtsPlusTwoRandomCharsNot9Digits(), 2, 8);
86          waitAndTypeByName("document.newMaintainableObject.dataObject.number", "Num" + randomGroup);
87          waitAndTypeByName("document.newMaintainableObject.dataObject.name", "Name" + randomGroup);
88          fileUploadSetUp();
89  
90          if (fileUploadList != null && fileUploadList.size() > 0) {
91              for (File file : fileUploadList) {
92                  String random = AutomatedFunctionalTestUtils.createUniqueDtsPlusTwoRandomCharsNot9Digits();
93                  waitAndTypeByXpath("//div[@data-label='ID']/input", "attachment" + random);
94                  String path = file.getAbsolutePath().toString();
95                  driver.findElement(By.xpath("//div[@data-label='Attached File']/fieldset/div/div/input[@type='file']"))
96                          .sendKeys(path);
97                  System.out.println("In for " + path);
98                  waitAndClickButtonByExactText("Add");
99                  waitForTextPresent("Adding Line...");
100             }
101         }
102 
103         waitForElementPresentByXpath("//button[contains(text(),'download attachment')]");
104         waitAndClickSubmitByText();
105         waitAndClickConfirmSubmitOk();
106         waitForElementPresentByXpath("//button[contains(text(),'download attachment')]");
107     }
108 
109     private void fileUploadSetUp() throws Exception {
110         setUpResourceDir("general");
111     }
112 
113     protected void setUpResourceDir(String resourceDir) {
114         try {
115             setUpFiles("src/test/resources/" + resourceDir);
116             System.out.println("Try for setUpResourceDir");
117         } catch (Exception e) {
118             System.out.println("Problem loading files from filesystem ( " + e.getMessage() + "). If running from "
119                     + "Intellij make sure working directory is rice-framework/krad-sampleapp/web attempt "
120                     + "to load as resource.");
121             // Example setUpFiles("rice-framework/krad-sampleapp/web/src/test/resources/" + resourceDir);
122             try {
123                 setUpResourceFiles(resourceDir);
124             } catch (Exception e1) {
125                 e1.printStackTrace();
126                 jiraAwareFail("Problems loading files as resources " + e1.getMessage());
127             }
128             System.out.println("Catch for setUpResourceDir");
129         }
130     }
131 
132     protected void setUpResourceFiles(String resourceDir) throws Exception {
133         System.out.println("In for setUpResourceFiles");
134         String[] resources = getResourceListing(getClass(), resourceDir);
135         fileUploadList = new ArrayList<File>();
136 
137         for (String resource : resources) {
138             InputStream inputStream = getClass().getResourceAsStream(resource);
139             File file = new File(System.getProperty("java.io.tmpdir") + File.separator + resource);
140             OutputStream outputStream = new FileOutputStream(file);
141             IOUtils.copy(inputStream, outputStream);
142             outputStream.close();
143             fileUploadList.add(file);
144             System.out.println("For for setUpResourceFiles");
145         }
146         Collections.sort(fileUploadList);
147     }
148 
149     protected String[] getResourceListing(Class clazz, String pathStartsWith) throws Exception {
150         System.out.println("In for getResourceListing");
151         String classPath = clazz.getName().replace(".", "/")+".class";
152         URL dirUrl = clazz.getClassLoader().getResource(classPath);
153 
154         if (!"jar".equals(dirUrl.getProtocol())) {
155             throw new UnsupportedOperationException("Cannot list files for URL " + dirUrl);
156         }
157 
158         String jarPath = dirUrl.getPath().substring(5, dirUrl.getPath().indexOf("!")); //strip out only the JAR file
159         JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
160         Enumeration<JarEntry> entries = jar.entries();
161         Set<String> result = new HashSet<String>();
162 
163         while(entries.hasMoreElements()) {
164             String entry = entries.nextElement().getName();
165             if (entry.startsWith(pathStartsWith) && !entry.endsWith("/")) { //filter according to the pathStartsWith skipping directories
166                 result.add(entry);
167             }
168         }
169 
170         return result.toArray(new String[result.size()]);
171     }
172 
173     protected void setUpFiles(String path) throws Exception {
174         System.out.println("In for setUpFiles");
175         fileUploadList = new ArrayList<File>();
176 
177         File dir = new File(path);
178 
179         if (dir != null && dir.listFiles().length > 0) {
180             Integer i = 1;
181 
182             for (File file : dir.listFiles()) {
183                 if (file.getName().endsWith(".txt")) {
184                     fileUploadList.add(file);
185                 }
186 
187                 i++;
188             }
189 
190             Collections.sort(fileUploadList);
191         } else {
192             throw new Exception("----Resources not found----");
193         }
194     }
195 
196     /**
197      * Performs Ingesting files to fileupload component and asserts successful ingestion.
198      *
199      */
200     private void fileIngester() throws Exception {
201         System.out.println("In for fileIngester");
202         if(fileUploadList!=null && fileUploadList.size()>0)
203         {
204             for (File file : fileUploadList) {
205                 String path = file.getAbsolutePath().toString();
206                 driver.findElement(By.name("document.newMaintainableObject.dataObject.attachmentFile")).sendKeys(path);
207                 System.out.println("In for -------");
208             }
209         }
210     }
211 
212     @Test
213     public void testMaintenanceBOAttachmentBookmark() throws Exception {
214         testMaintenanceBOAttachment();
215         passed();
216     }
217 
218     @Test
219     public void testMaintenanceBOAttachmentNav() throws Exception {
220         testMaintenanceBOAttachment();
221         passed();
222     }
223 
224     @Test
225     public void testMaintenanceBOAttachmentCollectionBookmark() throws Exception {
226         testMaintenanceBOAttachmentCollection();
227         passed();
228     }
229 
230     @Test
231     public void testMaintenanceBOAttachmentCollectionNav() throws Exception {
232         testMaintenanceBOAttachmentCollection();
233         passed();
234     }
235 }