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/ecl2.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 edu.samplu.common.Failable;
019    import edu.samplu.common.FreemarkerSTBase;
020    import edu.samplu.common.ITUtil;
021    import edu.samplu.common.PropertiesUtils;
022    import org.openqa.selenium.By;
023    
024    import java.io.File;
025    import java.io.IOException;
026    import java.util.ArrayList;
027    import java.util.List;
028    import java.util.Properties;
029    
030    /**
031     * Tests uploads of new users and group.
032     *
033     * @author Kuali Rice Team (rice.collab@kuali.org)
034     */
035    
036    public abstract class XMLIngesterAbstractSmokeTestBase extends FreemarkerSTBase {
037    
038        /**
039         * http://env12.rice.kuali.org/portal.do?channelTitle=XML%20Ingester&channelUrl=http://env12.rice.kuali.org/kew/../core/Ingester.do
040         */
041        public static final String BOOKMARK_URL = ITUtil.PORTAL + "?channelTitle=XML%20Ingester&channelUrl="
042                + ITUtil.getBaseUrlString() + "/kew/../core/Ingester.do";
043    
044        // File generation
045        private String PROPS_LOCATION = System.getProperty("xmlingester.props.location", null);
046        private static final String DEFAULT_PROPS_LOCATION = "XML/xmlingester.properties";
047    
048        // Templates for File Generation
049        private static final String DIR_TMPL = "/XML/";
050        private static final String TMPL_USER_CONTENT = "SimpleUserContent.ftl";
051        private static final String TMPL_GROUP_CONTENT = "SimpleGroupContent.ftl";
052    
053        /**
054         *
055         * @param name of temp file to create
056         * @return tempFile
057         * @throws IOException
058         */
059        protected abstract File newTempFile(String name) throws IOException;
060    
061        /**
062         * {@inheritDoc}
063         * {@link #DIR_TMPL}
064         * @return
065         */
066        @Override
067        protected String getTemplateDir() {
068            return DIR_TMPL;
069        }
070    
071        /**
072         * Nav tests start at {@link ITUtil#PORTAL}.  Bookmark Tests should override and return {@link XMLIngesterAbstractSmokeTestBase#BOOKMARK_URL}
073         * {@inheritDoc}
074         * @return
075         */
076        @Override
077        public String getTestUrl() {
078            return ITUtil.PORTAL;
079        }
080    
081        /**
082         * "admin" xml ingestion requires admin permissions.
083         * {@inheritDoc}
084         * @return
085         */
086        @Override
087        public String getUserName() {
088            return "admin";
089        }
090    
091        /**
092         * go to the getMenuLinkLocator() Menu and click the getLinkLocator()
093         */
094        protected void navigate(Failable failable) throws Exception {
095            selectTopFrame();
096            waitAndClickAdministration(failable);
097            waitForTitleToEqualKualiPortalIndex();
098            waitAndClickXMLIngester(failable);
099            selectFrameIframePortlet();
100            checkForIncidentReport("XML Ingester", failable, "");
101        }
102    
103        /**
104         * Navigate to the page under test and call {@link #testIngestion}
105         *
106         * @param failable {@link edu.samplu.common.Failable}
107         * @throws Exception
108         */
109        protected void testIngestionNav(Failable failable) throws Exception {
110            navigate(failable);
111            testIngestion(failable);
112            passed();
113        }
114    
115        protected void testIngestionBookmark(Failable failable) throws Exception {
116            testIngestion(failable);
117            passed();
118        }
119    
120        /**
121         * Based on load user and groups manual tests; dynamically generates user and group file
122         * and loads into the xml ingester screen.
123         * This test should suffice for both KRAD and KNS versions of the ingester screen.
124         *
125         *
126         */
127        protected void testIngestion(Failable failable) throws Exception {
128            selectFrameIframePortlet();
129            List<File> fileUploadList = buildFileUploadList();
130            int cnt = 0;
131    
132            for(File file : fileUploadList) {
133                String path = file.getAbsolutePath().toString();
134                if (isKrad()){
135                    driver.findElement(By.name("files[" + cnt + "]")).sendKeys(path);
136                } else {
137                    driver.findElement(By.name("file[" + cnt + "]")).sendKeys(path);
138                }
139                cnt++;
140            }
141    
142            // Click the Upload Button
143            if (isKrad()){
144                waitAndClickByXpath("//button");
145            } else {
146                waitAndClickByXpath("//*[@id='imageField']");
147            }
148    
149            // confirm all files were uploaded successfully
150            Thread.sleep(1000);
151            for(File file: fileUploadList) {
152                assertTextPresent("Ingested xml doc: " + file.getName());
153            }
154        }
155    
156        protected List<File> buildFileUploadList() throws Exception {
157            List<File> fileUploadList = new ArrayList<File>();
158            try {
159                // update properties with timestamp value if includeDTSinPrefix is true
160                Properties props = loadProperties(PROPS_LOCATION, DEFAULT_PROPS_LOCATION);
161                if(props.get("userIncludeDTSinPrefix") != null
162                        && "true".equalsIgnoreCase((String) props.get("userIncludeDTSinPrefix"))) {
163                    props.setProperty("userPrefix", "" + props.get("userPrefix") + ITUtil.DTS);
164                }
165                PropertiesUtils.systemPropertiesOverride(props, "XMLIngester");
166    
167                // build files and add to array
168                fileUploadList.add(
169                        writeTemplateToFile(newTempFile("loadtest-users.xml"), cfg.getTemplate(TMPL_USER_CONTENT), props));
170                fileUploadList.add(
171                        writeTemplateToFile(newTempFile("loadtest-group.xml"), cfg.getTemplate(TMPL_GROUP_CONTENT), props));
172            } catch( Exception e) {
173                throw new Exception("Unable to generate files for upload", e);
174            }
175    
176            return fileUploadList;
177        }
178    }