import cz.fi.muni.automation.tester.test.AbstractTest;
import cz.fi.muni.automation.core.configuration.*;
import cz.fi.muni.automation.converter.entity.*;
import cz.fi.muni.automation.tester.db.*;
import java.io.*;
import java.util.Calendar;
import java.util.Date;
import java.text.SimpleDateFormat;
import org.junit.Assert;
import java.util.List;
import java.util.ArrayList;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.io.*;
import org.openqa.selenium.support.ui.*;
import org.openqa.selenium.*;

public class all extends AbstractTest {

@Override
public String runTest() throws InterruptedException {
usingDatabase = true;
if (usingDatabase && DbName.NO_DATABASE.toString().equals(ConfigurationService.getValueFromConfiguration(ConfigurationConstants.CONF_DBNAME).toUpperCase())) {
return "Test needs database and no database is configured";
}
try {
int i = 1;
List<Action.Path> list = new ArrayList<>();
Action.Path p = new Action.Path();
ResultSet set;
try {
super.before();
super.prepareLogFileForTest("all");
driver.get("http://localhost:8080/ctds");
logFile.write(("Test step " + i++ + " finished with result: SUCCESS " + ConfigurationService.newline()).getBytes());
list = new ArrayList<>();p = new Action.Path(); p.setType(PathType.ID); p.setValue("name"); list.add(p);
super.getElement(list).sendKeys("uswissgridhe1");
logFile.write(("Test step " + i++ + " finished with result: SUCCESS " + ConfigurationService.newline()).getBytes());
list = new ArrayList<>();p = new Action.Path(); p.setType(PathType.ID); p.setValue("password"); list.add(p);
super.getElement(list).sendKeys("test");
logFile.write(("Test step " + i++ + " finished with result: SUCCESS " + ConfigurationService.newline()).getBytes());
list = new ArrayList<>();p = new Action.Path(); p.setType(PathType.ID); p.setValue("submitButton"); list.add(p);
super.getElement(list).click();
logFile.write(("Test step " + i++ + " finished with result: SUCCESS (login)" + ConfigurationService.newline()).getBytes());
Thread.sleep(5000);
logFile.write(("Test step " + i++ + " finished with result: SUCCESS " + ConfigurationService.newline()).getBytes());
driver.get("http://localhost:8080/ctds/SECURITY_MONITOR/startSecurityMonitor.seam");
logFile.write(("Test step " + i++ + " finished with result: SUCCESS (open security monitor)" + ConfigurationService.newline()).getBytes());
Thread.sleep(3000);
logFile.write(("Test step " + i++ + " finished with result: SUCCESS (wait after opening security monitor)" + ConfigurationService.newline()).getBytes());
list = new ArrayList<>();p = new Action.Path(); p.setType(PathType.XPATH); p.setValue(".//*[@id='angAppSecurityMonitor']/div/div/div[2]/div[1]/div[4]/div[1]/input"); list.add(p);
super.getElement(list).click();
Thread.sleep(2000);
logFile.write(("Test step " + i++ + " finished with result: SUCCESS (open filter management)" + ConfigurationService.newline()).getBytes());
list = new ArrayList<>();p = new Action.Path(); p.setType(PathType.XPATH); p.setValue(".//*[@id='angAppSecurityMonitor']/div/div/input[2]"); list.add(p);
super.getElement(list).click();
Thread.sleep(2000);
logFile.write(("Test step " + i++ + " finished with result: SUCCESS (open creating new filter)" + ConfigurationService.newline()).getBytes());
list = new ArrayList<>();p = new Action.Path(); p.setType(PathType.XPATH); p.setValue(".//*[@id='angAppSecurityMonitor']/div/div/div[2]/div[1]/input[1]"); list.add(p);
super.getElement(list).sendKeys("filter auto test");
logFile.write(("Test step " + i++ + " finished with result: SUCCESS (set filter name)" + ConfigurationService.newline()).getBytes());
list = new ArrayList<>();p = new Action.Path(); p.setType(PathType.XPATH); p.setValue(".//*[@id='angAppSecurityMonitor']/div/div/div[3]/div/select[2]"); list.add(p);
new Select(super.getElement(list)).selectByVisibleText("N-1 limit [%]");
logFile.write(("Test step " + i++ + " finished with result: SUCCESS (set filter name)" + ConfigurationService.newline()).getBytes());
list = new ArrayList<>();p = new Action.Path(); p.setType(PathType.XPATH); p.setValue(".//*[@id='angAppSecurityMonitor']/div/div/div[3]/div/span/span/input[1]"); list.add(p);
super.getElement(list).sendKeys("80");
logFile.write(("Test step " + i++ + " finished with result: SUCCESS (set filter condition)" + ConfigurationService.newline()).getBytes());
list = new ArrayList<>();p = new Action.Path(); p.setType(PathType.XPATH); p.setValue(".//*[@id='angAppSecurityMonitor']/div/div/div[5]/input[3]"); list.add(p);
super.getElement(list).click();
Thread.sleep(10000);
logFile.write(("Test step " + i++ + " finished with result: SUCCESS (save new filter)" + ConfigurationService.newline()).getBytes());
set = st.executeQuery("SELECT id FROM gm_operational.sm_filter  WHERE name = 'filter auto test'  ");
set.next();
sqlVariables.put("filterId",set.getString("id"));
logFile.write(("Test step " + i++ + " finished with result: SUCCESS (select id from filter by name)" + ConfigurationService.newline()).getBytes());
set = st.executeQuery("SELECT id FROM ctds.db_export_observer  WHERE export_id = " + sqlVariables.get("filterId") + "  ");
set.next();
sqlVariables.put("exportId",set.getString("id"));
logFile.write(("Test step " + i++ + " finished with result: SUCCESS (check if was created record in export table)" + ConfigurationService.newline()).getBytes());
list = new ArrayList<>();p = new Action.Path(); p.setType(PathType.XPATH); p.setValue(".//*[@id='angAppSecurityMonitor']/div/div/div[2]/table/tbody/tr/td[7]/a[2]/img"); list.add(p);
super.getElement(list).click();
Thread.sleep(2000);
logFile.write(("Test step " + i++ + " finished with result: SUCCESS (cleaning - remove filter)" + ConfigurationService.newline()).getBytes());
} catch (final org.openqa.selenium.WebDriverException ex) {
logFile.write(("Test step " + i++ + " finished with result: FAIL" + ConfigurationService.newline()).getBytes());
logFile.write((ex.getMessage() + ConfigurationService.newline()).getBytes());
} catch (final SQLException ex) {
logFile.write(("Test step " + i++ + " finished with result: FAIL" + ConfigurationService.newline()).getBytes());
logFile.write((ex.getMessage() + ConfigurationService.newline()).getBytes());
} catch (final Exception ex) {
logFile.write(("Test step " + i++ + " finished with result: FAIL" + ConfigurationService.newline()).getBytes());
logFile.write((ex.getMessage() + ConfigurationService.newline()).getBytes());
} finally {
super.after();
logFile.flush();
logFile.close();
}
} catch (final IOException ex) {
System.err.println(ex.toString());
}
return "Test finished";
}

}
