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     */
016    package edu.samplu.admin.test;
017    
018    import java.io.File;
019    import java.util.ArrayList;
020    
021    import org.junit.Assert;
022    import org.junit.Test;
023    import org.openqa.selenium.By;
024    
025    import java.util.Collections;
026    import java.util.List;
027    
028    /**
029     * @author Kuali Rice Team (rice.collab@kuali.org)
030     */
031    public class OAAeDocLiteXMLIngestNavIT extends AdminTmplMthdSTNavBase {
032        // values set by default for repeatable testing; left as configurable for load tests
033        private List<File> fileUploadList;
034    
035        private void setUpFiles() throws Exception {
036            fileUploadList = new ArrayList<File>();
037            // Load the directory as a resource
038            // Turn the resource into a File object
039    
040            File dir = new File("src/it/resources/OAA");
041    
042            if (dir != null && dir.listFiles().length > 0) {
043                Integer i = 1;
044                for (File file : dir.listFiles()) {
045                    if (file.getName().endsWith(".xml")) {
046                        if (!file.getName().equalsIgnoreCase("sample-app-config.xml"))
047                            fileUploadList.add(file);
048                    }
049                    i++;
050                }
051                Collections.sort(fileUploadList);
052            } else {
053                throw new Exception("----Resources not found----");
054            }
055    
056        }
057    
058        @Test
059        public void test() throws Exception {
060            try {
061                setUpFiles();
062            } catch (Exception e) {
063                Assert.fail("Resources not found. Test will be skipped");
064            }
065            testXMLIngesterSuccessfulFileUpload();
066    
067            Thread.sleep(2000);
068            driver.switchTo().defaultContent();
069            waitAndClickByLinkText("Main Menu");
070            waitAndClickByLinkText("eDoc Lite");
071    
072            driver.switchTo().frame("iframeportlet");
073            waitAndClick(By.cssSelector("td.infoline > input[name=\"methodToCall.search\"]"));
074            Thread.sleep(2000);
075    
076            /*
077            assertEquals("eDoc.Example1Doctype", driver.findElement(By.xpath("//table[@id='row']/tbody/tr/td[3]"))
078                    .getText());
079            assertEquals("InterviewRequest", driver.findElement(By.xpath("//table[@id='row']/tbody/tr[2]/td[3]")).getText());
080            assertEquals("OfferRequest", driver.findElement(By.xpath("//table[@id='row']/tbody/tr[3]/td[3]")).getText());
081            assertEquals("SearchStatus", driver.findElement(By.xpath("//table[@id='row']/tbody/tr[4]/td[3]")).getText());
082            assertEquals("VacancyNotice", driver.findElement(By.xpath("//table[@id='row']/tbody/tr[5]/td[3]")).getText());
083            assertEquals("WaiverRequest", driver.findElement(By.xpath("//table[@id='row']/tbody/tr[6]/td[3]")).getText());
084           */
085            driver.switchTo().defaultContent();
086    
087            waitAndClickLogout();
088            passed();
089        }
090    
091        /**
092         * Uploads file available from fileUploadList through XML Ingester.
093         * Uploads each sublist from main fileUploadList if size greater than 10. 
094         * 
095         */
096        public void testXMLIngesterSuccessfulFileUpload() throws Exception {
097            gotoMenuLinkLocator();
098            if (fileUploadList != null && !fileUploadList.isEmpty()) {
099    
100                if (fileUploadList.size() > 10) {
101                    List<List<File>> subLists = getSubListsForFile(fileUploadList, 10);
102                    for (List<File> fileSet : subLists) {
103                        fileIngester(fileSet);
104                    }
105                } else {
106                    fileIngester(fileUploadList);
107                }
108            }
109        }
110    
111        @Override
112        protected String getLinkLocator() {
113    
114            return "XML Ingester";
115        }
116    
117        /**
118         * Performs Ingesting files to fileupload component and asserts succesful ingestion.
119         * 
120         */
121        private void fileIngester(List<File> fileToUpload) throws Exception {
122            int cnt = 0;
123            for (File file : fileToUpload) {
124                String path = file.getAbsolutePath().toString();
125                driver.findElement(By.name("file[" + cnt + "]")).sendKeys(path);
126                cnt++;
127            }
128            waitAndClickByXpath("//*[@id='imageField']");
129            Thread.sleep(2000);
130            // confirm all files were uploaded successfully
131            for (File file : fileToUpload) {
132                assertTextPresent("Ingested xml doc: " + file.getName());
133    
134            }
135        }
136    
137        /**
138         * Divides fileUploadList from resources into sublists to match the maximum number of file
139         * upload components available on XML Ingester Screen
140         * 
141         */
142        private List<List<File>> getSubListsForFile(List<File> fileList, final int L) {
143            List<List<File>> subLists = new ArrayList<List<File>>();
144            final int N = fileList.size();
145            for (int i = 0; i < N; i += L) {
146                subLists.add(new ArrayList<File>(fileList.subList(i, Math.min(N, i + L))));
147            }
148            return subLists;
149        }
150    }