001    /**
002     * Copyright 2005-2011 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.AdminMenuLegacyITBase;
019    import edu.samplu.common.ITUtil;
020    import freemarker.cache.ClassTemplateLoader;
021    import freemarker.template.Configuration;
022    import freemarker.template.Template;
023    import freemarker.template.TemplateException;
024    import org.apache.commons.io.FileUtils;
025    import org.apache.log4j.Logger;
026    import org.junit.Ignore;
027    import org.junit.Rule;
028    import org.junit.Test;
029    import org.junit.rules.TemporaryFolder;
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.List;
039    import java.util.Properties;
040    
041    /**
042     * tests uploads of new users and group
043     *
044     * @author Kuali Rice Team (rice.collab@kuali.org)
045     */
046    public class XMLIngesterLegacyIT extends AdminMenuLegacyITBase {
047    
048        protected final Logger LOG = Logger.getLogger(getClass());
049    
050        // File generation
051        private Configuration cfg;
052        private String PROPS_LOCATION = System.getProperty("xmlingester.props.location", null);
053        private String DEFAULT_PROPS_LOCATION = "XML/xmlingester.properties";
054    
055        // Templates for File Generation
056        private static final String DIR_TMPL = "/XML/";
057        private static final String TMPL_USER_CONTENT = "SimpleUserContent.ftl";
058        private static final String TMPL_GROUP_CONTENT = "SimpleGroupContent.ftl";
059    
060        @Rule
061        public TemporaryFolder folder= new TemporaryFolder();
062    
063        @Ignore
064        @Override
065        public void testCreateNewCancel() throws Exception {}
066    
067        @Ignore
068        @Override
069        public void testEditCancel() throws Exception {}
070    
071    
072        @Override
073        protected String getLinkLocator() {
074            return "XML Ingester";
075        }
076    
077        @Override
078        public String getUserName() {
079            return "admin"; // xml ingestion requires admin permissions
080        }
081    
082        @Override
083        public void setUp() throws Exception {
084            super.setUp();
085            // generated load users and group resources
086            cfg = new Configuration();
087            cfg.setTemplateLoader(new ClassTemplateLoader(getClass().getClassLoader().getClass(), DIR_TMPL));
088        }
089    
090        private List<File> buildFileUploadList() throws Exception {
091            List<File> fileUploadList = new ArrayList<File>();
092            try {
093                // update properties with timestamp value if includeDTSinPrefix is true
094                Properties props = loadProperties(PROPS_LOCATION, DEFAULT_PROPS_LOCATION);
095                if(props.get("userIncludeDTSinPrefix") != null
096                        && "true".equalsIgnoreCase((String) props.get("userIncludeDTSinPrefix"))) {
097                    props.setProperty("userPrefix", "" + props.get("userPrefix") + ITUtil.DTS);
098                }
099    
100                // build files and add to array
101                fileUploadList.add(
102                        writeTemplateToFile(
103                                folder.newFile("loadtest-users.xml"), cfg.getTemplate(TMPL_USER_CONTENT), props));
104                fileUploadList.add(
105                        writeTemplateToFile(
106                                folder.newFile("loadtest-group.xml"), cfg.getTemplate(TMPL_GROUP_CONTENT), props));
107            } catch( Exception e) {
108                throw new Exception("Unable to generate files for upload", e);
109            }
110            return fileUploadList;
111        }
112    
113    
114        /**
115         * Based on load user and groups manual tests; dynamically generates user and group file
116         * and loads into the xml ingester screen
117         *
118         */
119        @Test
120        public void testXMLIngesterSuccessfulFileUpload() throws Exception {
121            List<File> fileUploadList = buildFileUploadList();
122            gotoMenuLinkLocator();
123            int cnt = 0;
124            for(File file : fileUploadList) {
125                String path = file.getAbsolutePath().toString();
126                driver.findElement(By.name("file[" + cnt + "]")).sendKeys(path);
127                cnt++;
128            }
129            waitAndClickByXpath("//*[@id='imageField']");
130    
131            // confirm all files were uploaded successfully
132            for(File file: fileUploadList) {
133                assertTextPresent("Ingested xml doc: " + file.getName());
134            }
135            passed();
136        }
137    
138        /**
139         * Loads properties from user defined properties file, if not available uses resource file
140         *
141         * @return
142         * @throws IOException
143         */
144        private Properties loadProperties(String fileLocation, String resourceLocation) throws IOException {
145            Properties props = new Properties();
146            InputStream in = null;
147            if(fileLocation != null) {
148                in = new FileInputStream(fileLocation);
149            } else {
150                in = getClass().getClassLoader().getResourceAsStream(resourceLocation);
151            }
152            if(in != null) {
153                props.load(in);
154                in.close();
155            }
156            return props;
157        }
158    
159        /**
160         * writes processed template  to file
161         *
162         * @param file
163         * @param template
164         * @param props
165         * @return
166         * @throws IOException
167         * @throws TemplateException
168         */
169        private File writeTemplateToFile(File file, Template template, Properties props) throws IOException, TemplateException {
170            String output = FreeMarkerTemplateUtils.processTemplateIntoString(template, props);
171            LOG.debug("Generated File Output: " + output);
172            FileUtils.writeStringToFile(file, output);
173            return file;
174        }
175    }