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.*;

public class sql 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("sql");
set = st.executeQuery("SELECT id,number FROM app.extable  WHERE id IN (2,3,4)  ");
set.next();
sqlVariables.put("idColumn",set.getString("id"));
sqlVariables.put("numberColumn",set.getString("number"));
logFile.write(("Test step " + i++ + " finished with result: SUCCESS " + ConfigurationService.newline()).getBytes());
st.execute("INSERT INTO app.extable(id,text) VALUES (20,'cislo')");
logFile.write(("Test step " + i++ + " finished with result: SUCCESS " + ConfigurationService.newline()).getBytes());
st.execute("INSERT INTO app.extable(id,text) VALUES (21,'neco')");
logFile.write(("Test step " + i++ + " finished with result: SUCCESS " + ConfigurationService.newline()).getBytes());
st.execute("INSERT INTO app.extable(id,number,text) VALUES (22,25,'hocico')");
logFile.write(("Test step " + i++ + " finished with result: SUCCESS " + ConfigurationService.newline()).getBytes());
st.execute("DELETE FROM app.extable  WHERE id = 21  ");
logFile.write(("Test step " + i++ + " finished with result: SUCCESS " + ConfigurationService.newline()).getBytes());
st.executeUpdate("UPDATE app.extable SET number=100  WHERE id = 22  ");
logFile.write(("Test step " + i++ + " finished with result: SUCCESS " + 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";
}

}
