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