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 edu.sampleu.common.FreemarkerAftBase;
019import freemarker.template.DefaultObjectWrapper;
020import org.junit.Rule;
021import org.junit.rules.TemporaryFolder;
022import org.kuali.rice.testtools.common.JiraAwareFailable;
023import org.kuali.rice.testtools.common.PropertiesUtils;
024import org.kuali.rice.testtools.selenium.AutomatedFunctionalTestUtils;
025import org.kuali.rice.testtools.selenium.WebDriverUtils;
026import org.openqa.selenium.By;
027
028import java.io.File;
029import java.io.IOException;
030import java.util.ArrayList;
031import java.util.LinkedList;
032import java.util.List;
033import java.util.Properties;
034import java.util.StringTokenizer;
035
036/**
037 * Tests uploads of new users and group.
038 *
039 * @author Kuali Rice Team (rice.collab@kuali.org)
040 */
041
042public abstract class XmlIngesterAftBase extends FreemarkerAftBase {
043
044    /**
045     * http://env12.rice.kuali.org/portal.do?channelTitle=XML%20Ingester&channelUrl=http://env12.rice.kuali.org/kew/../core/Ingester.do
046     */
047    public static final String BOOKMARK_URL = AutomatedFunctionalTestUtils.PORTAL + "?channelTitle=XML%20Ingester&channelUrl="
048            + WebDriverUtils.getBaseUrlString() + "/kew/../core/Ingester.do";
049
050    // File generation
051    private String PROPS_LOCATION = System.getProperty("xmlingester.props.location", null);
052    private static final String DEFAULT_PROPS_LOCATION = "XML/xmlingester.properties";
053
054    // Templates for File Generation
055    private static final String DIR_TMPL = "/XML/";
056    private static final String TMPL_USER_CONTENT = "SimpleUserContent.ftl";
057    private static final String TMPL_GROUP_CONTENT = "SimpleGroupContent.ftl";
058
059    @Rule
060    public TemporaryFolder folder= new TemporaryFolder();
061
062    protected File newTempFile(String name) throws IOException {
063        return folder.newFile(name);
064    }
065
066    /**
067     * {@inheritDoc}
068     * {@link #DIR_TMPL}
069     * @return
070     */
071    @Override
072    protected String getTemplateDir() {
073        return DIR_TMPL;
074    }
075
076    @Override
077    protected String getBookmarkUrl() {
078        return BOOKMARK_URL;
079    }
080
081    /**
082     * "admin" xml ingestion requires admin permissions.
083     * {@inheritDoc}
084     * @return
085     */
086    @Override
087    public String getUserName() {
088        return "admin";
089    }
090
091    /**
092     * go to the getMenuLinkLocator() Menu and click the getLinkLocator()
093     */
094    @Override
095    protected void navigate() throws Exception {
096        selectTopFrame();
097        waitAndClickAdministration();
098        waitForTitleToEqualKualiPortalIndex();
099        waitAndClickXMLIngester(this);
100        selectFrameIframePortlet();
101        checkForIncidentReport("XML Ingester", this, "");
102    }
103
104    public void testXmlIngesterUserList() throws Exception {
105        List<File> fileUploadList = buildUserFileUploadList();
106        testIngestion(this, fileUploadList);
107    }
108
109    /**
110     * Navigate to the page under test and call {@link #testIngestion}
111     *
112     * @param failable {@link org.kuali.rice.testtools.common.JiraAwareFailable}
113     * @throws Exception
114     */
115    protected void testIngestionNav(JiraAwareFailable failable) throws Exception {
116        List<File> fileUploadList = buildFileUploadList();
117        testIngestion(failable, fileUploadList);
118        passed();
119    }
120
121    protected void testIngestionBookmark(JiraAwareFailable failable) throws Exception {
122        List<File> fileUploadList = buildFileUploadList();
123        testIngestion(failable, fileUploadList);
124        passed();
125    }
126
127    /**
128     * Based on load user and groups manual tests; dynamically generates user and group file
129     * and loads into the xml ingester screen.
130     * This test should suffice for both KRAD and KNS versions of the ingester screen.
131     *
132     *
133     */
134    protected void testIngestion(JiraAwareFailable failable, List<File> fileUploadList) throws Exception {
135        selectFrameIframePortlet();
136        int cnt = 0;
137
138        for(File file : fileUploadList) {
139            String path = file.getAbsolutePath().toString();
140            if (isKrad()){
141                driver.findElement(By.name("files[" + cnt + "]")).sendKeys(path);
142            } else {
143                driver.findElement(By.name("file[" + cnt + "]")).sendKeys(path);
144            }
145            cnt++;
146        }
147
148        // Click the Upload Button
149        if (isKrad()){
150            waitAndClickByXpath("//button");
151        } else {
152            waitAndClickByXpath("//*[@id='imageField']");
153        }
154
155        // confirm all files were uploaded successfully
156        Thread.sleep(1000);
157        for(File file: fileUploadList) {
158            waitForTextPresent("Ingested xml doc: " + file.getName(), 360);
159        }
160    }
161
162    protected List<File> buildFileUploadList() throws Exception {
163        List<File> fileUploadList = new ArrayList<File>();
164        try {
165            // update properties with timestamp value if includeDTSinPrefix is true
166            Properties props = loadProperties(PROPS_LOCATION, DEFAULT_PROPS_LOCATION);
167            if(props.get("userIncludeDTSinPrefix") != null
168                    && "true".equalsIgnoreCase((String) props.get("userIncludeDTSinPrefix"))) {
169                props.setProperty("userPrefix", "" + props.get("userPrefix") + AutomatedFunctionalTestUtils.DTS);
170            }
171            props = new PropertiesUtils().systemPropertiesOverride(props, "XMLIngester");
172
173            // build files and add to array
174            fileUploadList.add(
175                    writeTemplateToFile(newTempFile("loadtest-users.xml"), cfg.getTemplate(TMPL_USER_CONTENT), props));
176            fileUploadList.add(
177                    writeTemplateToFile(newTempFile("loadtest-group.xml"), cfg.getTemplate(TMPL_GROUP_CONTENT), props));
178        } catch( Exception e) {
179            throw new Exception("Unable to generate files for upload " + e.getMessage(), e);
180        }
181
182        return fileUploadList;
183    }
184
185    protected List<File> buildUserFileUploadList() throws Exception {
186        List<File> fileUploadList = new ArrayList<File>();
187        try {
188            Properties props = loadProperties(PROPS_LOCATION, DEFAULT_PROPS_LOCATION);
189
190            String usersArg = System.getProperty("xmlingester.user.list").replace(".", "").replace("-", "").toLowerCase();
191            List<XmlIngesterUser> xmlIngesterUsers = new LinkedList<XmlIngesterUser>();
192            StringTokenizer token = new StringTokenizer(usersArg, ",");
193            while (token.hasMoreTokens()) {
194                xmlIngesterUsers.add(new XmlIngesterUser(token.nextToken()));
195            }
196
197            props.put("xmlIngesterUsers", xmlIngesterUsers);
198
199            cfg.setObjectWrapper(new DefaultObjectWrapper());
200
201            // build files and add to array
202            fileUploadList.add(
203                    writeTemplateToFile(newTempFile("userlist-users.xml"), cfg.getTemplate("UserListIngestion.ftl"), props));
204
205        } catch( Exception e) {
206            throw new Exception("Unable to generate files for upload " + e.getMessage(), e);
207        }
208
209        return fileUploadList;
210    }
211}
212