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.sampleu.admin;
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 AdminTmplMthdAftNavBase {
030        // values set by default for repeatable testing; left as configurable for load tests
031        protected List<File> fileUploadList;
032    
033        @Override
034        protected String getBookmarkUrl() {
035            return null; // no bookmark test yet
036        }
037    
038        /**
039         * This overridden method ...
040         *
041         * @see edu.sampleu.common.NavTemplateMethodAftBase#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 setUpResourceDir(String resourceDir) {
079            try {
080                setUpFiles("src/it/resources/" + resourceDir);
081            } catch (Exception e) {
082                jiraAwareFail("Files not found. If running from Intellij make sure working directory is rice-middleware/sampleapp");
083            }
084        }
085    
086        protected void setUpFiles(String path) throws Exception {
087            fileUploadList = new ArrayList<File>();
088            // Load the directory as a resource
089            // Turn the resource into a File object
090    
091            File dir = new File(path);
092    
093            if (dir != null && dir.listFiles().length > 0) {
094                Integer i = 1;
095                for (File file : dir.listFiles()) {
096                    if (file.getName().endsWith(".xml")) {
097                        if (!file.getName().equalsIgnoreCase("sample-app-config.xml"))
098                            fileUploadList.add(file);
099                    }
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    }