package test;

import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class SubmissionFileTest {
  private WebDriver driver;
  private String baseUrl;
  private String uploadFile;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Before
  public void setUp() throws Exception {
    driver = new HtmlUnitDriver();
    baseUrl = "https://datadryad.org/";
    uploadFile = "/opt/dryad/config/workflow.xml";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testSubmission() throws Exception {
    // Root page
    driver.get(baseUrl + "/");

    // Click Submit and login
    driver.findElement(By.linkText("Submit data now")).click();
    driver.findElement(By.id("aspect_eperson_PasswordLogin_field_login_email")).clear();
    driver.findElement(By.id("aspect_eperson_PasswordLogin_field_login_email")).sendKeys("seleniumtest@datadryad.org");
    driver.findElement(By.id("aspect_eperson_PasswordLogin_field_login_password")).clear();
    driver.findElement(By.id("aspect_eperson_PasswordLogin_field_login_password")).sendKeys("seleniumtest");
    driver.findElement(By.cssSelector("#aspect_eperson_PasswordLogin_item_loginsubmit-item > div.ds-form-content > #aspect_eperson_PasswordLogin_field_submit")).click();

    // 0. Select Journal
    driver.findElement(By.id("xmlui_submit_publication_article_status_accepted")).click();
    driver.findElement(By.id("aspect_submission_StepTransformer_field_prism_publicationName")).clear();
    driver.findElement(By.id("aspect_submission_StepTransformer_field_prism_publicationName")).sendKeys("Automated Test Submission Journal");
    driver.findElement(By.name("manu_accepted-cb")).click();
    driver.findElement(By.name("license_accept")).click();
    driver.findElement(By.id("aspect_submission_StepTransformer_field_submit_next")).click();
    
    // 1. Describe Publication
    driver.findElement(By.id("aspect_submission_StepTransformer_field_dc_title")).clear();
    driver.findElement(By.id("aspect_submission_StepTransformer_field_dc_title")).sendKeys("IGNORE: Automated Test Submission");
    driver.findElement(By.id("aspect_submission_StepTransformer_field_dc_contributor_author_last")).clear();
    driver.findElement(By.id("aspect_submission_StepTransformer_field_dc_contributor_author_last")).sendKeys("Test");
    driver.findElement(By.id("aspect_submission_StepTransformer_field_dc_contributor_author_first")).clear();
    driver.findElement(By.id("aspect_submission_StepTransformer_field_dc_contributor_author_first")).sendKeys("Selenium");
    driver.findElement(By.id("aspect_submission_StepTransformer_field_submit_next")).click();
    
    // 2.1 Upload Data File 1/3
    driver.findElement(By.id("aspect_submission_StepTransformer_field_dataset-file")).clear();
    driver.findElement(By.id("aspect_submission_StepTransformer_field_dataset-file")).sendKeys(uploadFile);
    driver.findElement(By.id("aspect_submission_StepTransformer_field_dc_title")).clear();
    driver.findElement(By.id("aspect_submission_StepTransformer_field_dc_title")).sendKeys("IGNORE: Automated Test Submission File");
    driver.findElement(By.id("aspect_submission_StepTransformer_field_submit_next")).click();
    // 3. Delete submission - can't get this working with selenium
    // 4. Logout
    driver.findElement(By.linkText("Logout")).click();
  }

  @After
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private boolean isAlertPresent() {
    try {
      driver.switchTo().alert();
      return true;
    } catch (NoAlertPresentException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}

@@ -3170,32 +3170,93 @@
 tle%22)).clear();%0A
+    String title = %22IGNORE: Automated Test Submission File%22;%0A
     driver.findE
@@ -3334,145 +3334,554 @@
 eys(
-%22IGNORE: Automated Test Submission File%22);%0A    driver.findElement(By.id(%22aspect_submission_StepTransformer_field_submit_next%22)).click(
+title);%0A    driver.findElement(By.id(%22aspect_submission_StepTransformer_field_submit_next%22)).click();%0A%0A    // Assert that the page is reached and contains Submission overview%0A    assertTrue(%22Submission overview after file upload%22, driver.findElement(By.cssSelector(%22h1.ds-div-head%22)).getText().contains(%22Submission overview%22));%0A%0A    // Assert that the uploaded file is listed on the page%0A    assertTrue(%22Uploaded file appears on page%22, driver.findElement(By.id(%22aspect_submission_submit_OverviewStep_list_datasets%22)).getText().contains(title)
 );%0A
+%0A
     

Add assertions to confirm file is uploaded