Skip to content

Commit

Permalink
Drop support for old xml connection configs
Browse files Browse the repository at this point in the history
  • Loading branch information
uglide committed Sep 24, 2018
1 parent 06bd25a commit 19fea52
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 221 deletions.
3 changes: 0 additions & 3 deletions src/app/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,6 @@ void Application::initConnectionsManager()
{
//connection manager
ConfigManager confManager(m_settingsDir);
if (confManager.migrateOldConfig("connections.xml", "connections.json")) {
LOG(INFO) << "Migrate connections.xml to connections.json";
}

QString config = confManager.getApplicationConfigPath("connections.json");

Expand Down
115 changes: 0 additions & 115 deletions src/app/models/configmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,121 +34,6 @@ QString ConfigManager::getApplicationConfigPath(const QString &configFile, bool
return configPath;
}

/**
* @brief ConfigManager::migrateOldConfig
* @param oldFileName - config.xml
* @param newFileName - config.json
* @return true or false
*/
bool ConfigManager::migrateOldConfig(const QString &oldFileName, const QString &newFileName)
{
QString jsonConfigPath = getApplicationConfigPath(newFileName, false);

if (QFile::exists(jsonConfigPath)) {
qDebug() << "New config already exist. Ignore old configs.";
return false;
}

// Move config from 0.7.5 or older to appropriate directory
QString homeConfig = QString("%1/%2").arg(m_basePath).arg(oldFileName);

QString xmlConfigPath = getApplicationConfigPath(oldFileName, false);

if (xmlConfigPath.isEmpty())
return false;

if (QFile::exists(homeConfig)) {
qDebug() << "Config migration: 0.7.5 config detected.";
QFile::remove(xmlConfigPath);
if (QFile::copy(homeConfig, xmlConfigPath)
&& QFile::remove(homeConfig)) {
qDebug() << "Old config moved to new dir";
}
}

if (!QFile::exists(xmlConfigPath))
return false;

QJsonArray newConfig = xmlConfigToJsonArray(xmlConfigPath);

QFile::rename(xmlConfigPath, QString("%1.backup").arg(xmlConfigPath));

if (newConfig.size() == 0)
return false;

return saveJsonArrayToFile(newConfig, jsonConfigPath);
}

/**
* @brief ConfigManager::xmlConfigToJsonArray - Migrate XML config to JSON array
* @param xmlConfigPath
* @return
*/
QJsonArray ConfigManager::xmlConfigToJsonArray(const QString &xmlConfigPath)
{
LOG(WARNING) << "XML support is deprecated and will be removed in RDM 1.0";
QJsonArray newConfig;

QFile* xmlConfigfile = new QFile(xmlConfigPath);
if (!xmlConfigfile->open(QIODevice::ReadOnly | QIODevice::Text))
return newConfig;

QXmlStreamReader xml(xmlConfigfile);

QHash<QString, QString> attrMapping ({
{"sshHost", "ssh_host"},
{"sshUser", "ssh_user"},
{"sshPassword", "ssh_password"},
{"sshPort", "ssh_port"},
{"sshPrivateKey", "ssh_private_key_path"},
{"namespaceSeparator", "namespace_separator"},
{"connectionTimeout", "timeout_connect"},
{"executeTimeout", "timeout_execute"},
});

while (!xml.atEnd() && !xml.hasError())
{
QXmlStreamReader::TokenType token = xml.readNext();
if (token == QXmlStreamReader::StartDocument)
continue;
if (token == QXmlStreamReader::StartElement)
{
if (xml.name() == "connections")
continue;
if (xml.name() == "connection") {
QXmlStreamAttributes attributes = xml.attributes();

QString name, value;
QVariantHash connection;

for (QXmlStreamAttribute attr : attributes) {
name = attr.name().toString();
if (attrMapping.contains(name)) {
name = attrMapping[name];
}

value = attr.value().toString();

if (name.contains("port") || name.contains("timeout")) {
connection.insert(name, value.toInt());
} else {
connection.insert(name, value);
}
}

// NOTE(u_glide): We should add NS separator to new config
if (!connection.contains("namespace_separator")) {
connection.insert("namespace_separator",
QString(ServerConfig::DEFAULT_NAMESPACE_SEPARATOR));
}

newConfig.append(QJsonObjectFromVariantHash(connection));
}
}
}
return newConfig;
}

bool ConfigManager::chechPath(const QString &configPath)
{
QFile testConfig(configPath);
Expand Down
6 changes: 2 additions & 4 deletions src/app/models/configmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ class ConfigManager
{
public:
ConfigManager(const QString& basePath = QDir::homePath());
QString getApplicationConfigPath(const QString &, bool checkPath=true);
bool migrateOldConfig(const QString &oldFileName, const QString &newFileName);
QString getApplicationConfigPath(const QString &, bool checkPath=true);
public:
static QString getConfigPath(QString basePath = QDir::homePath());
static QJsonArray xmlConfigToJsonArray(const QString &xmlConfigPath);
static QString getConfigPath(QString basePath = QDir::homePath());

private:
static bool chechPath(const QString&);
Expand Down
29 changes: 13 additions & 16 deletions src/app/models/connectionsmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,29 +100,26 @@ bool ConnectionsManager::loadConnectionsConfigFromFile(const QString& config, bo
{
QJsonArray connections;

if (config.endsWith(".xml")) {
connections = ConfigManager::xmlConfigToJsonArray(config);
} else {
QFile conf(config);
QFile conf(config);

if (!conf.open(QIODevice::ReadOnly))
return false;

QByteArray data = conf.readAll();
conf.close();
if (!conf.open(QIODevice::ReadOnly))
return false;

QJsonDocument jsonConfig = QJsonDocument::fromJson(data);
QByteArray data = conf.readAll();
conf.close();

if (jsonConfig.isEmpty())
return true;
QJsonDocument jsonConfig = QJsonDocument::fromJson(data);

if (!jsonConfig.isArray()) {
return false;
}
if (jsonConfig.isEmpty())
return true;

connections = jsonConfig.array();
if (!jsonConfig.isArray()) {
return false;
}

connections = jsonConfig.array();


for (QJsonValue connection : connections) {
if (!connection.isObject())
continue;
Expand Down
80 changes: 0 additions & 80 deletions tests/unit_tests/testcases/app/test_configmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,83 +29,3 @@ void TestConfigManager::testGetApplicationConfigPath()
actual_result);
QCOMPARE(check_path, QFile::exists(actual_result));
}

void TestConfigManager::testMigrateOldConfig()
{
#ifdef Q_OS_MACX
QSKIP("SKIP ON OSX");
#endif
#ifdef Q_OS_WIN
QSKIP("SKIP ON Windows");
#endif
// Given
QTemporaryDir tmpDir;
tmpDir.setAutoRemove(true);
QString basePath = tmpDir.path();
ConfigManager manager(basePath);
qDebug() << "Base path:" << basePath;

// When
// Case 1 - Migrate from old versions (<= 0.7.6)
// cp connections.xml to temp dir
QString targetConfigPath = QString("%1/connections.xml").arg(basePath);
QVERIFY(QFile::copy("./unit_tests/testcases/app/connections.xml", targetConfigPath));

bool actual_result = manager.migrateOldConfig("connections.xml", "connections.json");

// Then
QCOMPARE(true, actual_result);
// check that connections.xml doesn't exist in base dir
QCOMPARE(false, QFile::exists(targetConfigPath));
// check that .rdm dir exist in base path
QDir configDir(QString("%1/.rdm").arg(basePath));
QCOMPARE(true, configDir.exists());
// check that .rdm/connections.json exists
QFile newConfig(QString("%1/.rdm/connections.json").arg(basePath));
QCOMPARE(true, newConfig.exists());
// check that .rdm/connections.xml.backup exists
QFile oldConfigBackup(QString("%1/.rdm/connections.xml.backup").arg(basePath));
QCOMPARE(true, oldConfigBackup.exists());
// check that .rdm/connections.xml doesn't exist
QCOMPARE(false, QFile::exists(QString("%1/.rdm/connections.xml").arg(basePath)));
// check that backup and new config is not empty
QCOMPARE(true, newConfig.size() > 0);
QCOMPARE(true, oldConfigBackup.size() > 0);

// Clean run - Run on (>= 0.8.0) when connections.xml doesn't exist
// Check that connections.xml not created
actual_result = manager.migrateOldConfig("connections.xml", "connections.json");
QCOMPARE(false, QFile::exists(targetConfigPath));
QCOMPARE(false, QFile::exists(QString("%1/.rdm/connections.xml").arg(basePath)));

// Negative run
// connections.xml, .rdm/connections.xml and .rdm/connections.json exist
QVERIFY(QFile::copy("./unit_tests/testcases/app/connections.xml", targetConfigPath));
QVERIFY(QFile::copy("./unit_tests/testcases/app/connections.xml",
QString("%1/.rdm/connections.xml").arg(basePath)));
QFileInfo configInfo(newConfig);
QDateTime currentModificationTime = configInfo.lastModified();

actual_result = manager.migrateOldConfig("connections.xml", "connections.json");
// Check that old files are untouched and connections.json is not updated
QCOMPARE(false, actual_result);
QCOMPARE(true, QFile::exists(targetConfigPath));
QCOMPARE(true, QFile::exists(QString("%1/.rdm/connections.xml").arg(basePath)));
configInfo.refresh();
QCOMPARE(currentModificationTime, configInfo.lastModified());
}

void TestConfigManager::testXmlConfigToJsonArray()
{
// Given
QString pathToXmlConfig("./unit_tests/testcases/app/connections.xml");

// When
QJsonArray actual_result = ConfigManager::xmlConfigToJsonArray(pathToXmlConfig);

// Then
// Check that resulting array contains namespace separator if it missing in original XML
for (auto connection : actual_result) {
QCOMPARE(QString(":"), connection.toObject()["namespace_separator"].toString());
}
}
4 changes: 1 addition & 3 deletions tests/unit_tests/testcases/app/test_configmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ class TestConfigManager : public BaseTestCase
{
Q_OBJECT
private slots:
void testGetApplicationConfigPath();
void testMigrateOldConfig();
void testXmlConfigToJsonArray();
void testGetApplicationConfigPath();
};

0 comments on commit 19fea52

Please sign in to comment.