View Javadoc

1   /*
2    * Copyright 2005-2013 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/ecl1.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 edu.samplu.admin.test;
17  
18  import java.io.File;
19  import java.util.ArrayList;
20  
21  import org.junit.Assert;
22  import org.junit.Test;
23  import org.openqa.selenium.By;
24  
25  import java.util.Collections;
26  import java.util.List;
27  
28  /**
29   * @author Kuali Rice Team (rice.collab@kuali.org)
30   */
31  public class IUeDocLiteXMLIngestNavIT extends AdminTmplMthdSTNavBase {
32      // values set by default for repeatable testing; left as configurable for load tests
33      private List<File> fileUploadList;
34  
35      private void setUpFiles() throws Exception {
36          fileUploadList = new ArrayList<File>();
37          // Load the directory as a resource
38          // Turn the resource into a File object
39  
40          File dir = new File("src/it/resources/IU");
41  
42          if (dir != null && dir.listFiles().length > 0) {
43              Integer i = 1;
44              for (File file : dir.listFiles()) {
45                  if (file.getName().endsWith(".xml")) {
46                      if (!file.getName().equalsIgnoreCase("sample-app-config.xml"))
47                          fileUploadList.add(file);
48                  }
49                  i++;
50              }
51              Collections.sort(fileUploadList);
52          } else {
53              throw new Exception("----Resources not found----");
54          }
55      }
56  
57      @Test
58      public void test() throws Exception {
59          try {
60              setUpFiles();
61          } catch (Exception e) {
62              Assert.fail("Resources not found. Test will be skipped");
63          }
64          testXMLIngesterSuccessfulFileUpload();
65  
66          Thread.sleep(2000);
67          driver.switchTo().defaultContent();
68          waitAndClickByLinkText("Main Menu");
69          waitAndClickByLinkText("eDoc Lite");
70  
71          driver.switchTo().frame("iframeportlet");
72          waitAndClick(By.cssSelector("td.infoline > input[name=\"methodToCall.search\"]"));
73          Thread.sleep(2000);
74         
75          driver.switchTo().defaultContent();
76  
77          waitAndClickLogout();
78          passed();
79      }
80  
81      /**
82       * Uploads file available from fileUploadList through XML Ingester.
83       * Uploads each sublist from main fileUploadList if size greater than 10. 
84       * 
85       */
86      public void testXMLIngesterSuccessfulFileUpload() throws Exception {
87          gotoMenuLinkLocator();
88          if (fileUploadList != null && !fileUploadList.isEmpty()) {
89  
90              if (fileUploadList.size() > 10) {
91                  List<List<File>> subLists = getSubListsForFile(fileUploadList, 10);
92                  for (List<File> fileSet : subLists) {
93                      fileIngester(fileSet);
94                      for (File file : fileSet) {
95                          assertTextPresent("Ingested xml doc: " + file.getName());
96                      }
97                  }
98              } else {
99                  fileIngester(fileUploadList);
100             }
101         }
102     }
103 
104     /**
105      * This overridden method ...
106      * 
107      * @see edu.samplu.common.NavTemplateMethodSTBase#getLinkLocator()
108      */
109     @Override
110     protected String getLinkLocator() {
111 
112         return "XML Ingester";
113     }
114 
115     /**
116      * Performs Ingesting files to fileupload component and asserts succesful ingestion.
117      * 
118      */
119     private void fileIngester(List<File> fileToUpload) throws Exception {
120         int cnt = 0;
121         for (File file : fileToUpload) {
122             String path = file.getAbsolutePath().toString();
123             driver.findElement(By.name("file[" + cnt + "]")).sendKeys(path);
124             cnt++;
125         }
126         waitAndClickByXpath("//*[@id='imageField']");
127         Thread.sleep(1000);
128         // confirm all files were uploaded successfully
129     }
130 
131     /**
132      * Divides fileUploadList from resources into sublists to match the maximum number of file
133      * upload components available on XML Ingester Screen
134      * 
135      */
136     private List<List<File>> getSubListsForFile(List<File> fileList, final int L) {
137         List<List<File>> subLists = new ArrayList<List<File>>();
138         final int N = fileList.size();
139         for (int i = 0; i < N; i += L) {
140             subLists.add(new ArrayList<File>(fileList.subList(i, Math.min(N, i + L))));
141         }
142         return subLists;
143     }
144 }