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