001/**
002 * Copyright 2005-2015 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.sampleu.admin;
017
018import freemarker.cache.ClassTemplateLoader;
019import freemarker.template.Configuration;
020import freemarker.template.Template;
021import freemarker.template.TemplateException;
022import org.apache.commons.io.FileUtils;
023import org.apache.log4j.Logger;
024import org.junit.Ignore;
025import org.junit.Rule;
026import org.junit.Test;
027import org.junit.rules.TemporaryFolder;
028import org.kuali.rice.testtools.selenium.AutomatedFunctionalTestUtils;
029import org.openqa.selenium.By;
030import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
031
032import java.io.File;
033import java.io.FileInputStream;
034import java.io.IOException;
035import java.io.InputStream;
036import java.util.ArrayList;
037import java.util.Enumeration;
038import java.util.List;
039import 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 */
049public 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 String testCreateNewCancel() throws Exception {
069        return null;
070    }
071
072    @Ignore // empty test, XML Ingester does not have Edit Cancel functionality
073    @Override
074    public void testEditCancel() throws Exception {}
075
076    @Override
077    protected String getBookmarkUrl() {
078        return null; // no bookmark test yet
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") + AutomatedFunctionalTestUtils.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 testXMLIngesterSuccessfulFileUploadNav() 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}