View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package edu.sampleu.admin;
17  
18  import edu.sampleu.common.FreemarkerAftBase;
19  import freemarker.template.DefaultObjectWrapper;
20  import org.junit.Rule;
21  import org.junit.rules.TemporaryFolder;
22  import org.kuali.rice.testtools.common.JiraAwareFailable;
23  import org.kuali.rice.testtools.common.PropertiesUtils;
24  import org.kuali.rice.testtools.selenium.AutomatedFunctionalTestUtils;
25  import org.kuali.rice.testtools.selenium.WebDriverUtils;
26  import org.openqa.selenium.By;
27  
28  import java.io.File;
29  import java.io.IOException;
30  import java.util.ArrayList;
31  import java.util.LinkedList;
32  import java.util.List;
33  import java.util.Properties;
34  import java.util.StringTokenizer;
35  
36  /**
37   * Tests uploads of new users and group.
38   *
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   */
41  
42  public abstract class XmlIngesterAftBase extends FreemarkerAftBase {
43  
44      /**
45       * http://env12.rice.kuali.org/portal.do?channelTitle=XML%20Ingester&channelUrl=http://env12.rice.kuali.org/kew/../core/Ingester.do
46       */
47      public static final String BOOKMARK_URL = AutomatedFunctionalTestUtils.PORTAL + "?channelTitle=XML%20Ingester&channelUrl="
48              + WebDriverUtils.getBaseUrlString() + "/kew/../core/Ingester.do";
49  
50      // File generation
51      private String PROPS_LOCATION = System.getProperty("xmlingester.props.location", null);
52      private static final String DEFAULT_PROPS_LOCATION = "XML/xmlingester.properties";
53  
54      // Templates for File Generation
55      private static final String DIR_TMPL = "/XML/";
56      private static final String TMPL_USER_CONTENT = "SimpleUserContent.ftl";
57      private static final String TMPL_GROUP_CONTENT = "SimpleGroupContent.ftl";
58  
59      @Rule
60      public TemporaryFolder folder= new TemporaryFolder();
61  
62      protected File newTempFile(String name) throws IOException {
63          return folder.newFile(name);
64      }
65  
66      /**
67       * {@inheritDoc}
68       * {@link #DIR_TMPL}
69       * @return
70       */
71      @Override
72      protected String getTemplateDir() {
73          return DIR_TMPL;
74      }
75  
76      @Override
77      protected String getBookmarkUrl() {
78          return BOOKMARK_URL;
79      }
80  
81      /**
82       * "admin" xml ingestion requires admin permissions.
83       * {@inheritDoc}
84       * @return
85       */
86      @Override
87      public String getUserName() {
88          return "admin";
89      }
90  
91      /**
92       * go to the getMenuLinkLocator() Menu and click the getLinkLocator()
93       */
94      @Override
95      protected void navigate() throws Exception {
96          selectTopFrame();
97          waitAndClickAdministration();
98          waitForTitleToEqualKualiPortalIndex();
99          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