From bc9b711df1fae1112d21c974df44e86156761f71 Mon Sep 17 00:00:00 2001
From: Robert Goldmann <deadlocker@gmx.de>
Date: Sun, 5 May 2019 14:17:10 +0200
Subject: [PATCH] #432 - finished test for uploading a database

---
 pom.xml                                       |  6 +++
 .../de/deadlocker8/budgetmaster/Main.java     |  2 +-
 .../resources/templates/helpers/header.ftl    |  4 +-
 .../resources/templates/helpers/navbar.ftl    |  2 +-
 .../resources/templates/settings/import.ftl   |  4 +-
 .../budgetmaster/integration/ImportTest.java  | 54 ++++++++++++++++---
 .../SeleniumTestExecutionListener.java        |  4 +-
 7 files changed, 60 insertions(+), 16 deletions(-)

diff --git a/pom.xml b/pom.xml
index 1ee44018f..9c2e817b1 100644
--- a/pom.xml
+++ b/pom.xml
@@ -195,6 +195,12 @@
             <version>${selenium.version}</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.seleniumhq.selenium</groupId>
+            <artifactId>selenium-support</artifactId>
+            <version>${selenium.version}</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>
diff --git a/src/main/java/de/deadlocker8/budgetmaster/Main.java b/src/main/java/de/deadlocker8/budgetmaster/Main.java
index 09258d734..da74dae2b 100644
--- a/src/main/java/de/deadlocker8/budgetmaster/Main.java
+++ b/src/main/java/de/deadlocker8/budgetmaster/Main.java
@@ -83,7 +83,7 @@ public class Main extends SpringBootServletInitializer implements ApplicationRun
 
 	public static Path getApplicationSupportFolder()
 	{
-		if(System.getProperties().contains("test"))
+		if(System.getProperties().containsKey("testProfile"))
 		{
 			RunMode.currentRunMode = RunMode.TEST;
 		}
diff --git a/src/main/resources/templates/helpers/header.ftl b/src/main/resources/templates/helpers/header.ftl
index dd70a0759..fe828c51d 100644
--- a/src/main/resources/templates/helpers/header.ftl
+++ b/src/main/resources/templates/helpers/header.ftl
@@ -1,9 +1,9 @@
 <#macro style name>
         <#import "/spring.ftl" as s>
         <#if helpers.getSettings().isUseDarkTheme()>
-                <link type="text/css" rel="stylesheet" href=<@s.url '${"/css/dark/" + name + ".css"}'/>/>
+                <link type="text/css" rel="stylesheet" href="<@s.url '${"/css/dark/" + name + ".css"}'/>"/>
         <#else>
-                <link type="text/css" rel="stylesheet" href=<@s.url '${"/css/" + name + ".css"}'/>/>
+                <link type="text/css" rel="stylesheet" href="<@s.url '${"/css/" + name + ".css"}'/>"/>
         </#if>
 </#macro>
 
diff --git a/src/main/resources/templates/helpers/navbar.ftl b/src/main/resources/templates/helpers/navbar.ftl
index 6f4c845f3..0ab247ecf 100644
--- a/src/main/resources/templates/helpers/navbar.ftl
+++ b/src/main/resources/templates/helpers/navbar.ftl
@@ -164,7 +164,7 @@
                 <p>${locale.getString("info.text.backup.reminder")}</p>
             </div>
             <div class="modal-footer background-color">
-                <a href="<@s.url '/backupReminder/cancel'/>" class="modal-action modal-close waves-effect waves-light red btn-flat white-text">${locale.getString("cancel")}</a>
+                <a href="<@s.url '/backupReminder/cancel'/>" id="buttonCloseReminder" class="modal-action modal-close waves-effect waves-light red btn-flat white-text">${locale.getString("cancel")}</a>
                 <a href="<@s.url '/backupReminder/settings'/>" class="modal-action modal-close waves-effectwaves-light green btn-flat white-text">${locale.getString("info.button.backup.reminder")}</a>
             </div>
         </div>
diff --git a/src/main/resources/templates/settings/import.ftl b/src/main/resources/templates/settings/import.ftl
index 53e2568e2..119e10474 100644
--- a/src/main/resources/templates/settings/import.ftl
+++ b/src/main/resources/templates/settings/import.ftl
@@ -41,7 +41,7 @@
                                     </td>
                                     <td class="import-text">${locale.getString("info.database.import.or")}</td>
                                     <td>
-                                        <a href="<@s.url '/accounts/newAccount'/>" class="btn waves-effect waves-light budgetmaster-blue"><i class="material-icons left">add</i>${locale.getString("title.account.new")}</a>
+                                        <a href="<@s.url '/accounts/newAccount'/>" class="btn waves-effect waves-light budgetmaster-blue button-new-account"><i class="material-icons left">add</i>${locale.getString("title.account.new")}</a>
                                     </td>
                                 </tr>
                             </#list>
@@ -61,7 +61,7 @@
                             </div>
 
                             <div class="col m6 l4 left-align">
-                                <button class="btn waves-effect waves-light budgetmaster-blue" type="submit" name="action">
+                                <button class="btn waves-effect waves-light budgetmaster-blue" type="submit" name="action" id="buttonImport">
                                     <i class="material-icons left">unarchive</i>${locale.getString("settings.database.import")}
                                 </button>
                             </div>
diff --git a/src/test/java/de/deadlocker8/budgetmaster/integration/ImportTest.java b/src/test/java/de/deadlocker8/budgetmaster/integration/ImportTest.java
index 80310361d..560f5ea0e 100644
--- a/src/test/java/de/deadlocker8/budgetmaster/integration/ImportTest.java
+++ b/src/test/java/de/deadlocker8/budgetmaster/integration/ImportTest.java
@@ -7,29 +7,33 @@ import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.openqa.selenium.By;
+import org.openqa.selenium.NoSuchElementException;
 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.WebElement;
+import org.openqa.selenium.support.ui.ExpectedConditions;
+import org.openqa.selenium.support.ui.WebDriverWait;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.context.embedded.LocalServerPort;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.transaction.annotation.Transactional;
 
 import java.io.File;
-import java.net.URISyntaxException;
 import java.util.List;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 
 @RunWith(SpringJUnit4ClassRunner.class)
-@SpringBootTest(classes = Main.class,webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
+@SpringBootTest(classes = Main.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
 @SeleniumTest
+@Transactional
 public class ImportTest
 {
 	@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
 	@Autowired
 	private WebDriver driver;
-	private final static String BASE_URL = "http://localhost:";
+	private final static String BASE_URL = "https://localhost:";
 	private String url;
 
 	@LocalServerPort
@@ -44,7 +48,7 @@ public class ImportTest
 	@Test
 	public void requestImport() throws InterruptedException
 	{
-		driver.get(url + "/settings/database/requestImport");
+		driver.get(url);
 
 		// login
 		WebElement inputPassword = driver.findElement(By.id("login-password"));
@@ -52,6 +56,18 @@ public class ImportTest
 		WebElement buttonLogin = driver.findElement(By.tagName("button"));
 		buttonLogin.click();
 
+		// hide backup reminder if present
+		try
+		{
+			WebElement buttonCloseReminder = driver.findElement(By.cssSelector("#modalBackupReminder #buttonCloseReminder"));
+			buttonCloseReminder.click();
+		}
+		catch(NoSuchElementException ignored)
+		{
+		}
+
+		driver.get(url + "/settings/database/requestImport");
+
 		// upload database
 		WebElement input = driver.findElement(By.id("inputDatabaseImport"));
 		assertNotNull(input);
@@ -66,6 +82,18 @@ public class ImportTest
 		WebElement buttonUpload = driver.findElement(By.id("button-confirm-database-import"));
 		buttonUpload.click();
 
+		// create new account
+		driver.findElement(By.className("button-new-account")).click();
+		WebElement inputAccountName = driver.findElement(By.id("account-name"));
+		inputAccountName.sendKeys("DefaultAccount0815");
+		driver.findElement(By.tagName("button")).click();
+
+		// create second account
+		driver.findElement(By.className("button-new-account")).click();
+		inputAccountName = driver.findElement(By.id("account-name"));
+		inputAccountName.sendKeys("Account2");
+		driver.findElement(By.tagName("button")).click();
+
 		// account matching
 		WebElement headlineImport = driver.findElement(By.className("headline"));
 		assertEquals(Localization.getString("info.title.database.import.dialog"), IntegrationTestHelper.getTextNode(headlineImport));
@@ -77,9 +105,19 @@ public class ImportTest
 		WebElement sourceAccount1 = row1.findElement(By.className("account-source"));
 		assertEquals("DefaultAccount0815", IntegrationTestHelper.getTextNode(sourceAccount1));
 
-		List<WebElement> destinationAccounts = row1.findElements(By.cssSelector(".account-destination option"));
-		assertEquals(1, destinationAccounts.size());
-		System.out.println(destinationAccounts.get(0).getText());
-//		assertEquals("Default Account", IntegrationTestHelper.getTextNode(destinationAccounts.get(0)));
+		// match first account
+		row1.findElement(By.className("select-dropdown")).click();
+		WebElement accountToSelect = row1.findElement(By.xpath("//form/table/tbody/tr[1]/td[5]/div/div/ul/li/span[text()='DefaultAccount0815']"));
+		accountToSelect.click();
+
+		// match second account
+		tableRows.get(1).findElement(By.className("select-dropdown")).click();
+		accountToSelect = row1.findElement(By.xpath("//form/table/tbody/tr[2]/td[5]/div/div/ul/li/span[text()='Account2']"));
+		accountToSelect.click();
+
+		// confirm import
+		driver.findElement(By.id("buttonImport")).click();
+
+		assertEquals(Localization.getString("menu.settings"), IntegrationTestHelper.getTextNode(driver.findElement(By.className("headline"))));
 	}
 }
\ No newline at end of file
diff --git a/src/test/java/de/deadlocker8/budgetmaster/integration/SeleniumTestExecutionListener.java b/src/test/java/de/deadlocker8/budgetmaster/integration/SeleniumTestExecutionListener.java
index cc782ddbe..5dfe13519 100644
--- a/src/test/java/de/deadlocker8/budgetmaster/integration/SeleniumTestExecutionListener.java
+++ b/src/test/java/de/deadlocker8/budgetmaster/integration/SeleniumTestExecutionListener.java
@@ -31,9 +31,9 @@ public class SeleniumTestExecutionListener extends AbstractTestExecutionListener
 	@Override
 	public void prepareTestInstance(TestContext testContext)
 	{
-		if(!System.getProperties().contains("test"))
+		if(!System.getProperties().containsKey("testProfile"))
 		{
-			throw new RuntimeException("Test profile not activated. Skipping tests. (Set -Dtest=true in your VM arguments)");
+			throw new RuntimeException("Test profile not activated. Skipping tests. (Set -DtestProfile=true in your VM arguments)");
 		}
 
 		if(webDriver != null)
-- 
GitLab