Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Source File Upload Tests
Browse files Browse the repository at this point in the history
Add Uploaded source files testing. Handles success and error case.
Exception located in error cases, these tests are expect fail.
Added a 'Critical Error' check to BasePage and some hard defaults
for the document storage dir.
Added an action to delete the stored files on database reset.
Slight modification of ids in Source Documents pages.
  • Loading branch information
djansen-redhat committed Aug 2, 2013
1 parent 5fec7d8 commit 357ce0a
Show file tree
Hide file tree
Showing 11 changed files with 297 additions and 7 deletions.
2 changes: 2 additions & 0 deletions functional-test/pom.xml
Expand Up @@ -63,6 +63,8 @@
<cargo.debug.jvm.args> -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8787 -Xnoagent -Djava.compiler=NONE</cargo.debug.jvm.args>
<!-- this property can be used to control what test needs to be run by failsafe -->
<include.test.patterns>**/AggregateTestSuite.java</include.test.patterns>
<document.storage.directory>${project.build.directory}/zanata-documents</document.storage.directory>
<zanata.testdata.directory>${basedir}/testdata</zanata.testdata.directory>
</properties>

<dependencies>
Expand Down
9 changes: 9 additions & 0 deletions functional-test/src/main/java/org/zanata/page/BasePage.java
Expand Up @@ -229,4 +229,13 @@ public WebElement apply(WebDriver driver)
return getErrors();
}

public void assertNoCriticalErrors()
{
List<WebElement> errors = getDriver().findElements(By.id("errorMessage"));
if (errors.size() > 0)
{
throw new RuntimeException("Critical error: \n"+errors.get(0).getText());
}
}

}
@@ -0,0 +1,102 @@
/*
* Copyright 2010, Red Hat, Inc. and individual contributors as indicated by the
* @author tags. See the copyright.txt file in the distribution for a full
* listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this software; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
* site: http://www.fsf.org.
*/
package org.zanata.page.projects;

import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.zanata.page.BasePage;
import org.zanata.page.webtrans.DocumentsViewPage;

import java.util.List;

@Slf4j
public class ProjectSourceDocumentsPage extends BasePage
{
@FindBy(id = "iterationDocumentsForm:data_table:tb")
private WebElement documentTableTBody;

@FindBy(id = "uploadSidebar:uploadDocumentButton")
private WebElement uploadDocumentButton;

@FindBy(id = "uploadDocForm:uploadFilename")
private WebElement uploadFilenameInput;

@FindBy(id = "uploadDocForm:uploadButton")
private WebElement uploadButton;

@FindBy(id = "uploadDocForm:cancelUploadButton")
private WebElement cancelUploadButton;

public ProjectSourceDocumentsPage(final WebDriver driver)
{
super(driver);
}

public ProjectSourceDocumentsPage pressUploadFileButton()
{
uploadDocumentButton.click();
return new ProjectSourceDocumentsPage(getDriver());
}

public ProjectSourceDocumentsPage enterFilePath(String filePath)
{
uploadFilenameInput.sendKeys(filePath);
return new ProjectSourceDocumentsPage(getDriver());
}

public ProjectSourceDocumentsPage cancelUpload()
{
cancelUploadButton.click();
return new ProjectSourceDocumentsPage(getDriver());
}

public ProjectSourceDocumentsPage submitUpload()
{
uploadButton.click();
return new ProjectSourceDocumentsPage(getDriver());
}

private List<WebElement> getDocumentTableRows()
{
return documentTableTBody.findElements(By.tagName("tr"));
}


public boolean sourceDocumentsContains(String document)
{
List<WebElement> documentTableRows = getDocumentTableRows();
for (WebElement tableRow : documentTableRows)
{
if (tableRow.findElements(By.tagName("td")).get(1).getText().equals(document))
{
return true;
}
}
return false;
}

}
Expand Up @@ -39,6 +39,9 @@ public class ProjectVersionPage extends BasePage
@FindBy(id = "iterationLanguageForm:data_table:tb")
private WebElement localeTableTBody;

@FindBy(linkText = "Source Documents")
private WebElement sourceDocumentsButton;

public ProjectVersionPage(final WebDriver driver)
{
super(driver);
Expand Down Expand Up @@ -106,4 +109,10 @@ public DocumentsViewPage translate(String locale)
}
throw new IllegalArgumentException("can not translate locale: " + locale);
}

public ProjectSourceDocumentsPage goToSourceDocuments()
{
sourceDocumentsButton.click();
return new ProjectSourceDocumentsPage(getDriver());
}
}
@@ -0,0 +1,136 @@
/*
* Copyright 2013, Red Hat, Inc. and individual contributors as indicated by the
* @author tags. See the copyright.txt file in the distribution for a full
* listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this software; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
* site: http://www.fsf.org.
*/
package org.zanata.feature.document;

import org.hamcrest.Matchers;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.zanata.feature.DetailedTest;
import org.zanata.page.projects.ProjectSourceDocumentsPage;
import org.zanata.util.PropertiesHolder;
import org.zanata.util.ResetDatabaseRule;
import org.zanata.workflow.LoginWorkFlow;

import java.io.File;
import java.io.IOException;

import static org.hamcrest.MatcherAssert.assertThat;

/**
* @author Damian Jansen <a href="mailto:djansen@redhat.com">djansen@redhat.com</a>
*/
@Category(DetailedTest.class)
public class UploadTest
{
@ClassRule
public static ResetDatabaseRule resetDatabaseRule = new ResetDatabaseRule(ResetDatabaseRule.Config.WithData);

@Test
public void uploadedDocumentIsInFilesystem()
{
String successfullyUploaded = "Document file uploadedDocumentIsInFilesystem.txt uploaded.";
String testFileName = "uploadedDocumentIsInFilesystem.txt";
String filePath = PropertiesHolder.getProperty("zanata.testdata.directory")
.concat(File.separator)
.concat(testFileName);

assertThat("Data file "+testFileName+" exists", new File(filePath).exists());

ProjectSourceDocumentsPage projectSourceDocumentsPage = new LoginWorkFlow().signIn("admin", "admin")
.goToProjects()
.goToProject("about fedora")
.goToVersion("master")
.goToSourceDocuments()
.pressUploadFileButton()
.enterFilePath(filePath)
.submitUpload();
assertThat("Document uploaded notification shows",
projectSourceDocumentsPage.getNotificationMessage(), Matchers.equalTo(successfullyUploaded));
assertThat("Document shows in table", projectSourceDocumentsPage.sourceDocumentsContains(testFileName));
}

@Test
public void cancelFileUpload()
{
String testFileName = "cancelFileUpload.txt";
String filePath = PropertiesHolder.getProperty("zanata.testdata.directory")
.concat(File.separator)
.concat(testFileName);

assertThat("Data file "+testFileName+" exists", new File(filePath).exists());
ProjectSourceDocumentsPage projectSourceDocumentsPage = new LoginWorkFlow().signIn("admin", "admin")
.goToProjects()
.goToProject("about fedora")
.goToVersion("master")
.goToSourceDocuments()
.pressUploadFileButton()
.enterFilePath(filePath)
.cancelUpload();
assertThat("Document does not show in table",
!projectSourceDocumentsPage.sourceDocumentsContains(testFileName));
}

@Test(expected = RuntimeException.class)
public void emptyFilenameUpload()
{
ProjectSourceDocumentsPage projectSourceDocumentsPage = new LoginWorkFlow().signIn("admin", "admin")
.goToProjects()
.goToProject("about fedora")
.goToVersion("master")
.goToSourceDocuments()
.pressUploadFileButton()
.submitUpload();

// RHBZ-990373
projectSourceDocumentsPage.assertNoCriticalErrors();
// TODO: Verify graceful handling of scenario
}

@Test(expected = RuntimeException.class)
public void failOnInvalidFileUpload()
{
File noFile;
try {
noFile = File.createTempFile("thereIsNoSpoon", "txt");
} catch(IOException ioException)
{
throw new RuntimeException("failOnInvalidFileUpload:Unable to create temporary file");
}
assertThat("Data file "+noFile.getName()+" exists", noFile.exists());

ProjectSourceDocumentsPage projectSourceDocumentsPage = new LoginWorkFlow().signIn("admin", "admin")
.goToProjects()
.goToProject("about fedora")
.goToVersion("master")
.goToSourceDocuments()
.pressUploadFileButton()
.enterFilePath(noFile.getAbsolutePath());

noFile.delete();
assertThat("Data file " + noFile.getName() + " does not exists", !noFile.exists());
projectSourceDocumentsPage = projectSourceDocumentsPage.submitUpload();

// RHBZ-990373
projectSourceDocumentsPage.assertNoCriticalErrors();
// TODO: Verify graceful handling of scenario
}
}
21 changes: 21 additions & 0 deletions functional-test/src/test/java/org/zanata/util/DatabaseHelper.java
Expand Up @@ -8,11 +8,16 @@
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Nullable;

import org.apache.commons.io.FileUtils;
import org.hamcrest.Matchers;
import org.openqa.selenium.support.ui.FluentWait;
import com.google.common.base.Predicate;
Expand Down Expand Up @@ -189,6 +194,22 @@ public void resetData()
waitUntil("select count(*) from HProjectIteration", 0);
}

public void resetFileData()
{
File path = new File(PropertiesHolder.getProperty("document.storage.directory"));
if(path.exists())
{
try {
FileUtils.deleteDirectory(path);
}catch (IOException e)
{
log.error("Failed to delete", path, e);
throw new RuntimeException("error");
}
}

}

private void waitUntil(final String sql, final int expectedResultCount)
{
wait.until(new Predicate<Statement>()
Expand Down
Expand Up @@ -28,6 +28,8 @@ protected void before() throws Throwable
return;
}

DatabaseHelper.database().resetFileData();

if (configSet.contains(Config.WithData))
{
DatabaseHelper.database().resetDatabaseWithData();
Expand Down
5 changes: 4 additions & 1 deletion functional-test/src/test/resources/setup.properties
Expand Up @@ -15,4 +15,7 @@ zanata.apikey=${zanata.apikey}
zanata.database.backup=${zanata.database.backup}

#this can be used to disable database reset rule
disable.database.reset=${disable.database.reset}
disable.database.reset=${disable.database.reset}

document.storage.directory=${document.storage.directory}
zanata.testdata.directory=${zanata.testdata.directory}
1 change: 1 addition & 0 deletions functional-test/testdata/cancelFileUpload.txt
@@ -0,0 +1 @@
This is a cancel upload test file
@@ -0,0 +1 @@
This is a test file
16 changes: 10 additions & 6 deletions zanata-war/src/main/webapp/iteration/source_files.xhtml
Expand Up @@ -42,7 +42,7 @@

<ui:define name="page_title">#{projectIterationFilesAction.projectSlug}:#{projectIterationFilesAction.iterationSlug} - #{messages['jsf.SourceDocuments']}</ui:define>
<ui:define name="center_content">
<h:form onkeydown="if(event.keyCode==13) { return false;}">
<h:form id="iterationDocumentsForm" onkeydown="if(event.keyCode==13) { return false;}">
<s:token allowMultiplePosts="true"/>
<s:div style="text-align:center">
<rich:dataScroller id="data_table_scroller" for="data_table"
Expand Down Expand Up @@ -208,9 +208,10 @@

<rich:panel id="loggedIn" rendered="#{identity.loggedIn}">
<f:facet name="header">#{messages['jsf.Actions']}</f:facet>
<h:form>
<h:form id="uploadSidebar">

<a4j:commandLink styleClass="action_link"
<a4j:commandLink id="uploadDocumentButton"
styleClass="action_link"
rendered="#{projectIterationFilesAction.documentUploadAllowed}"
value="#{messages['jsf.iteration.files.UploadDocument']}"
title="#{messages['jsf.SupportedUploadFormats']}"
Expand Down Expand Up @@ -274,7 +275,8 @@
<h:outputText value="#{messages['jsf.iteration.files.UploadDocument']}"/>
</f:facet>
<h:form id="uploadDocForm" enctype="multipart/form-data">
<s:fileUpload accept="*"
<s:fileUpload id="uploadFilename"
accept="*"
data="#{projectIterationFilesAction.documentFileUpload.fileContents}"
fileName="#{projectIterationFilesAction.documentFileUpload.fileName}"/>
<br/>
Expand Down Expand Up @@ -306,9 +308,11 @@
</a>
</rich:collapsiblePanel>
<div align="right">
<h:commandButton value="#{messages['jsf.Upload']}"
<h:commandButton id="uploadButton"
value="#{messages['jsf.Upload']}"
action="#{projectIterationFilesAction.uploadDocumentFile}"/>
<h:commandButton value="#{messages['jsf.Cancel']}"
<h:commandButton id="cancelUploadButton"
value="#{messages['jsf.Cancel']}"
onclick="return hideDocUploadPanel();"/>
</div>
</h:form>
Expand Down

0 comments on commit 357ce0a

Please sign in to comment.