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 setUpResourceDir(String resourceDir) {
79          try {
80              setUpFiles("src/it/resources/" + resourceDir);
81          } catch (Exception e) {
82              failableFail("Files not found. If running from Intellij make sure working directory is rice-middleware/sampleapp");
83          }
84      }
85  
86      protected void setUpFiles(String path) throws Exception {
87          fileUploadList = new ArrayList<File>();
88          // Load the directory as a resource
89          // Turn the resource into a File object
90  
91          File dir = new File(path);
92  
93          if (dir != null && dir.listFiles().length > 0) {
94              Integer i = 1;
95              for (File file : dir.listFiles()) {
96                  if (file.getName().endsWith(".xml")) {
97                      if (!file.getName().equalsIgnoreCase("sample-app-config.xml"))
98                          fileUploadList.add(file);
99                  }
100                 i++;
101             }
102             Collections.sort(fileUploadList);
103         } else {
104             throw new Exception("----Resources not found----");
105         }
106     }
107 
108     protected void testEdocLiteIngestion() throws Exception {
109         testXMLIngesterSuccessfulFileUpload();
110 
111         Thread.sleep(2000);
112         driver.switchTo().defaultContent();
113         waitAndClickByLinkText("Main Menu");
114         waitAndClickByLinkText("eDoc Lite");
115 
116         selectFrameIframePortlet();
117         waitIsVisible(By.cssSelector("input.tinybutton:nth-child(1)")); // why name methodToCall.search fails?
118         waitAndClick(By.cssSelector("input.tinybutton:nth-child(1)"));
119         Thread.sleep(2000);
120         driver.switchTo().defaultContent();
121         Thread.sleep(1000);
122         waitIsVisible(By.className("exportlinks"));
123         selectFrameIframePortlet();
124     }
125 
126     /**
127      * Uploads file available from fileUploadList through XML Ingester.
128      * Uploads each sublist from main fileUploadList if size greater than 10.
129      *
130      */
131     public void testXMLIngesterSuccessfulFileUpload() throws Exception {
132         if (fileUploadList == null && fileUploadList.isEmpty()) {
133             return;
134         }
135         if (fileUploadList.size() > 10) {
136             List<List<File>> subLists = getSubListsForFile(fileUploadList, 10);
137             for (List<File> fileSet : subLists) {
138                 fileIngester(fileSet);
139                 for (File file : fileSet) {
140                     checkMessages(file);
141                 }
142             }
143         } else {
144             fileIngester(fileUploadList);
145         }
146     }
147 
148     private void checkMessages(File file) throws InterruptedException {
149         waitIsVisible(By.className("error")); // messages appear in error too.
150         if (!isTextPresent("without allowOverwrite set")) { // docs should still be present
151             // from previous run, if not we'll fail when we assert they exist.
152             assertTextPresent("Ingested xml doc: " + file.getName());
153         }
154     }
155 }