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