001 /*
002 * Copyright 2005-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl1.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */package edu.samplu.admin.test;
016
017 import org.junit.Assert;
018 import org.openqa.selenium.By;
019
020 import java.io.File;
021 import java.util.ArrayList;
022 import java.util.Collections;
023 import java.util.List;
024
025 /**
026 * @author Kuali Rice Team (rice.collab@kuali.org)
027 */
028
029 public abstract class EdocLiteXmlIngesterBase extends AdminTmplMthdSTNavBase {
030 // values set by default for repeatable testing; left as configurable for load tests
031 protected List<File> fileUploadList;
032
033 @Override
034 public void fail(String message) {
035 Assert.fail(message);
036 }
037
038 /**
039 * This overridden method ...
040 *
041 * @see edu.samplu.common.NavTemplateMethodSTBase#getLinkLocator()
042 */
043 @Override
044 protected String getLinkLocator() {
045 return "XML Ingester";
046 }
047
048 /**
049 * Performs Ingesting files to fileupload component and asserts succesful ingestion.
050 *
051 */
052 private void fileIngester(List<File> fileToUpload) throws Exception {
053 int cnt = 0;
054 for (File file : fileToUpload) {
055 String path = file.getAbsolutePath().toString();
056 driver.findElement(By.name("file[" + cnt + "]")).sendKeys(path);
057 cnt++;
058 }
059 waitAndClickByXpath("//*[@id='imageField']");
060 Thread.sleep(1000);
061 // confirm all files were uploaded successfully
062 }
063
064 /**
065 * Divides fileUploadList from resources into sublists to match the maximum number of file
066 * upload components available on XML Ingester Screen
067 *
068 */
069 private List<List<File>> getSubListsForFile(List<File> fileList, final int L) {
070 List<List<File>> subLists = new ArrayList<List<File>>();
071 final int N = fileList.size();
072 for (int i = 0; i < N; i += L) {
073 subLists.add(new ArrayList<File>(fileList.subList(i, Math.min(N, i + L))));
074 }
075 return subLists;
076 }
077
078 protected void setUpFiles(String path) throws Exception {
079 fileUploadList = new ArrayList<File>();
080 // Load the directory as a resource
081 // Turn the resource into a File object
082
083 File dir = new File(path);
084
085 if (dir != null && dir.listFiles().length > 0) {
086 Integer i = 1;
087 for (File file : dir.listFiles()) {
088 if (file.getName().endsWith(".xml")) {
089 if (!file.getName().equalsIgnoreCase("sample-app-config.xml"))
090 fileUploadList.add(file);
091 }
092 i++;
093 }
094 Collections.sort(fileUploadList);
095 } else {
096 throw new Exception("----Resources not found----");
097 }
098 }
099
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 }