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.FreemarkerUtil;
021    import edu.samplu.common.ITUtil;
022    import freemarker.cache.ClassTemplateLoader;
023    import freemarker.template.Configuration;
024    import org.openqa.selenium.By;
025    
026    import java.io.File;
027    import java.io.IOException;
028    import java.util.ArrayList;
029    import java.util.List;
030    import java.util.Properties;
031    
032    /**
033     * Tests uploads of new users and group.
034     *
035     * @author Kuali Rice Team (rice.collab@kuali.org)
036     */
037    
038    public abstract class XMLIngesterAbstractSmokeTestBase extends FreemarkerSTBase {
039    
040        /**
041         * http://env12.rice.kuali.org/portal.do?channelTitle=XML%20Ingester&channelUrl=http://env12.rice.kuali.org/kew/../core/Ingester.do
042         */
043        public static final String BOOKMARK_URL = ITUtil.PORTAL + "?channelTitle=XML%20Ingester&channelUrl="
044                + ITUtil.getBaseUrlString() + "/kew/../core/Ingester.do";
045    
046        // File generation
047        private String PROPS_LOCATION = System.getProperty("xmlingester.props.location", null);
048        private static final String DEFAULT_PROPS_LOCATION = "XML/xmlingester.properties";
049    
050        // Templates for File Generation
051        private static final String DIR_TMPL = "/XML/";
052        private static final String TMPL_USER_CONTENT = "SimpleUserContent.ftl";
053        private static final String TMPL_GROUP_CONTENT = "SimpleGroupContent.ftl";
054    
055        /**
056         *
057         * @param name of temp file to create
058         * @return tempFile
059         * @throws IOException
060         */
061        protected abstract File newTempFile(String name) throws IOException;
062    
063        /**
064         * Nav tests start at {@link ITUtil#PORTAL}.  Bookmark Tests should override and return {@link XMLIngesterAbstractSmokeTestBase#BOOKMARK_URL}
065         * {@inheritDoc}
066         * @return
067         */
068        @Override
069        public String getTestUrl() {
070            return ITUtil.PORTAL;
071        }
072    
073        /**
074         * "admin" xml ingestion requires admin permissions.
075         * {@inheritDoc}
076         * @return
077         */
078        @Override
079        public String getUserName() {
080            return "admin";
081        }
082    
083        /**
084         * go to the getMenuLinkLocator() Menu and click the getLinkLocator()
085         */
086        protected void navigate(Failable failable) throws Exception {
087            selectTopFrame();
088            waitAndClickAdministration(failable);
089            waitForTitleToEqualKualiPortalIndex();
090            waitAndClickXMLIngester(failable);
091            selectFrameIframePortlet();
092            checkForIncidentReport("XML Ingester", failable, "");
093        }
094    
095        @Override
096        public void setUp() throws Exception {
097            super.setUp();
098            // generated load users and group resources
099            cfg = new Configuration();
100            cfg.setTemplateLoader(new ClassTemplateLoader(getClass().getClassLoader().getClass(), DIR_TMPL));
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         *
124         */
125        protected void testIngestion(Failable failable) throws Exception {
126            List<File> fileUploadList = buildFileUploadList();
127            int cnt = 0;
128    
129            for(File file : fileUploadList) {
130                String path = file.getAbsolutePath().toString();
131                driver.findElement(By.name("file[" + cnt + "]")).sendKeys(path);
132                cnt++;
133            }
134    
135            waitAndClickByXpath("//*[@id='imageField']");
136    
137            // confirm all files were uploaded successfully
138            for(File file: fileUploadList) {
139                assertTextPresent("Ingested xml doc: " + file.getName());
140            }
141        }
142    
143        protected List<File> buildFileUploadList() throws Exception {
144            List<File> fileUploadList = new ArrayList<File>();
145            try {
146                // update properties with timestamp value if includeDTSinPrefix is true
147                Properties props = loadProperties(PROPS_LOCATION, DEFAULT_PROPS_LOCATION);
148                if(props.get("userIncludeDTSinPrefix") != null
149                        && "true".equalsIgnoreCase((String) props.get("userIncludeDTSinPrefix"))) {
150                    props.setProperty("userPrefix", "" + props.get("userPrefix") + ITUtil.DTS);
151                }
152                systemPropertiesOverride(props, "XMLIngester");
153    
154                // build files and add to array
155                fileUploadList.add(
156                        writeTemplateToFile(newTempFile("loadtest-users.xml"), cfg.getTemplate(TMPL_USER_CONTENT), props));
157                fileUploadList.add(
158                        writeTemplateToFile(newTempFile("loadtest-group.xml"), cfg.getTemplate(TMPL_GROUP_CONTENT), props));
159            } catch( Exception e) {
160                throw new Exception("Unable to generate files for upload", e);
161            }
162    
163            return fileUploadList;
164        }
165    }