001    /**
002     * Copyright 2005-2014 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.sampleu.admin;
017    
018    import freemarker.cache.ClassTemplateLoader;
019    import freemarker.template.Configuration;
020    import freemarker.template.Template;
021    import freemarker.template.TemplateException;
022    import org.apache.commons.io.FileUtils;
023    import org.apache.log4j.Logger;
024    import org.junit.Ignore;
025    import org.junit.Rule;
026    import org.junit.Test;
027    import org.junit.rules.TemporaryFolder;
028    import org.kuali.rice.testtools.selenium.AutomatedFunctionalTestUtils;
029    import org.openqa.selenium.By;
030    import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
031    
032    import java.io.File;
033    import java.io.FileInputStream;
034    import java.io.IOException;
035    import java.io.InputStream;
036    import java.util.ArrayList;
037    import java.util.Enumeration;
038    import java.util.List;
039    import java.util.Properties;
040    
041    /**
042     * tests uploads of new users and group
043     *
044     * <pre>mvn -f rice-middleware/sampleapp/pom.xml -Pstests failsafe:integration-test -Dremote.public.url=env7.rice.kuali.org -Dit.test=XMLIngester -DXMLIngester.groupId=2008 -DXMLIngester.userIncludeDTSinPrefix=false -DXMLIngester.userCntBegin=0 -DXMLIngester.userCnt=600  -DXMLIngester.userPrefix=loadtester -Dremote.driver.dontTearDown=y</pre>
045     *
046     * @deprecated XMLIngesterSTJUnitNavGen
047     * @author Kuali Rice Team (rice.collab@kuali.org)
048     */
049    public class XmlIngester extends AdminTmplMthdAftNavBase {
050    
051        protected final Logger LOG = Logger.getLogger(getClass());
052    
053        // File generation
054        private Configuration cfg;
055        private String PROPS_LOCATION = System.getProperty("xmlingester.props.location", null);
056        private String DEFAULT_PROPS_LOCATION = "XML/xmlingester.properties";
057    
058        // Templates for File Generation
059        private static final String DIR_TMPL = "/XML/";
060        private static final String TMPL_USER_CONTENT = "SimpleUserContent.ftl";
061        private static final String TMPL_GROUP_CONTENT = "SimpleGroupContent.ftl";
062    
063        @Rule
064        public TemporaryFolder folder= new TemporaryFolder();
065    
066        @Ignore // empty test, XML Ingester does not have Create New Cancel functionality
067        @Override
068        public void testCreateNewCancel() throws Exception {}
069    
070        @Ignore // empty test, XML Ingester does not have Edit Cancel functionality
071        @Override
072        public void testEditCancel() throws Exception {}
073    
074        @Override
075        protected String getBookmarkUrl() {
076            return null; // no bookmark test yet
077        }
078    
079        @Override
080        protected String getLinkLocator() {
081            return "XML Ingester";
082        }
083    
084        @Override
085        public String getUserName() {
086            return "admin"; // xml ingestion requires admin permissions
087        }
088    
089        @Override
090        public void testSetUp() {
091            super.testSetUp();
092            // generated load users and group resources
093            cfg = new Configuration();
094            cfg.setTemplateLoader(new ClassTemplateLoader(getClass().getClassLoader().getClass(), DIR_TMPL));
095        }
096    
097        private List<File> buildFileUploadList() throws Exception {
098            List<File> fileUploadList = new ArrayList<File>();
099            try {
100                // update properties with timestamp value if includeDTSinPrefix is true
101                Properties props = loadProperties(PROPS_LOCATION, DEFAULT_PROPS_LOCATION);
102                if(props.get("userIncludeDTSinPrefix") != null
103                        && "true".equalsIgnoreCase((String) props.get("userIncludeDTSinPrefix"))) {
104                    props.setProperty("userPrefix", "" + props.get("userPrefix") + AutomatedFunctionalTestUtils.DTS);
105                }
106                systemPropertiesOverride(props);
107    
108                // build files and add to array
109                fileUploadList.add(
110                        writeTemplateToFile(
111                                folder.newFile("loadtest-users.xml"), cfg.getTemplate(TMPL_USER_CONTENT), props));
112                fileUploadList.add(
113                        writeTemplateToFile(
114                                folder.newFile("loadtest-group.xml"), cfg.getTemplate(TMPL_GROUP_CONTENT), props));
115            } catch( Exception e) {
116                throw new Exception("Unable to generate files for upload", e);
117            }
118            return fileUploadList;
119        }
120    
121        /**
122         * -DXMLIngester.userCnt=176 will override the userCnt in property files.
123         * @param props
124         */
125        private void systemPropertiesOverride(Properties props) {
126            Enumeration<?> names = props.propertyNames();
127            Object nameObject;
128            String name;
129            while (names.hasMoreElements()) {
130                nameObject = names.nextElement();
131                if (nameObject instanceof String) {
132                    name = (String)nameObject;
133                    props.setProperty(name, System.getProperty("XMLIngester." + name, props.getProperty(name)));
134                }
135            }
136        }
137    
138        /**
139         * Based on load user and groups manual tests; dynamically generates user and group file
140         * and loads into the xml ingester screen
141         *
142         */
143        @Test
144        public void testXMLIngesterSuccessfulFileUploadNav() throws Exception {
145            List<File> fileUploadList = buildFileUploadList();
146            navigate();
147            int cnt = 0;
148            for(File file : fileUploadList) {
149                String path = file.getAbsolutePath().toString();
150                driver.findElement(By.name("file[" + cnt + "]")).sendKeys(path);
151                cnt++;
152            }
153            waitAndClickByXpath("//*[@id='imageField']");
154    
155            // confirm all files were uploaded successfully
156            for(File file: fileUploadList) {
157                assertTextPresent("Ingested xml doc: " + file.getName());
158            }
159            passed();
160        }
161    
162        /**
163         * Loads properties from user defined properties file, if not available uses resource file
164         *
165         * @return
166         * @throws IOException
167         */
168        private Properties loadProperties(String fileLocation, String resourceLocation) throws IOException {
169            Properties props = new Properties();
170            InputStream in = null;
171            if(fileLocation != null) {
172                in = new FileInputStream(fileLocation);
173            } else {
174                in = getClass().getClassLoader().getResourceAsStream(resourceLocation);
175            }
176            if(in != null) {
177                props.load(in);
178                in.close();
179            }
180            return props;
181        }
182    
183        /**
184         * writes processed template  to file
185         *
186         * @param file
187         * @param template
188         * @param props
189         * @return
190         * @throws IOException
191         * @throws TemplateException
192         */
193        private File writeTemplateToFile(File file, Template template, Properties props) throws IOException, TemplateException {
194            String output = FreeMarkerTemplateUtils.processTemplateIntoString(template, props);
195            LOG.debug("Generated File Output: " + output);
196            FileUtils.writeStringToFile(file, output);
197            return file;
198        }
199    }