|
@@ -21,6 +21,7 @@
|
|
|
#include "ctkXnatSessionTest.h"
|
|
|
|
|
|
#include <QCoreApplication>
|
|
|
+#include <QCryptographicHash>
|
|
|
#include <QDebug>
|
|
|
#include <QDir>
|
|
|
#include <QSignalSpy>
|
|
@@ -32,11 +33,14 @@
|
|
|
#include <QUuid>
|
|
|
|
|
|
#include <ctkXnatDataModel.h>
|
|
|
+#include <ctkXnatException.h>
|
|
|
+#include <ctkXnatFile.h>
|
|
|
#include <ctkXnatLoginProfile.h>
|
|
|
-#include <ctkXnatSession.h>
|
|
|
#include <ctkXnatProject.h>
|
|
|
+#include <ctkXnatResource.h>
|
|
|
+#include <ctkXnatResourceFolder.h>
|
|
|
+#include <ctkXnatSession.h>
|
|
|
#include <ctkXnatSubject.h>
|
|
|
-#include <ctkXnatException.h>
|
|
|
|
|
|
class ctkXnatSessionTestCasePrivate
|
|
|
{
|
|
@@ -74,6 +78,7 @@ void ctkXnatSessionTestCase::initTestCase()
|
|
|
d->LoginProfile.setPassword("ctk-xnat2015");
|
|
|
}
|
|
|
|
|
|
+// --------------------------------------------------------------------------
|
|
|
void ctkXnatSessionTestCase::init()
|
|
|
{
|
|
|
Q_D(ctkXnatSessionTestCase);
|
|
@@ -83,10 +88,12 @@ void ctkXnatSessionTestCase::init()
|
|
|
d->Session->open();
|
|
|
}
|
|
|
|
|
|
+// --------------------------------------------------------------------------
|
|
|
void ctkXnatSessionTestCase::cleanupTestCase()
|
|
|
{
|
|
|
}
|
|
|
|
|
|
+// --------------------------------------------------------------------------
|
|
|
void ctkXnatSessionTestCase::cleanup()
|
|
|
{
|
|
|
Q_D(ctkXnatSessionTestCase);
|
|
@@ -95,6 +102,7 @@ void ctkXnatSessionTestCase::cleanup()
|
|
|
d->Session = NULL;
|
|
|
}
|
|
|
|
|
|
+// --------------------------------------------------------------------------
|
|
|
void ctkXnatSessionTestCase::testProjectList()
|
|
|
{
|
|
|
Q_D(ctkXnatSessionTestCase);
|
|
@@ -107,6 +115,7 @@ void ctkXnatSessionTestCase::testProjectList()
|
|
|
QVERIFY(projects.size() > 0);
|
|
|
}
|
|
|
|
|
|
+// --------------------------------------------------------------------------
|
|
|
void ctkXnatSessionTestCase::testResourceUri()
|
|
|
{
|
|
|
Q_D(ctkXnatSessionTestCase);
|
|
@@ -116,6 +125,7 @@ void ctkXnatSessionTestCase::testResourceUri()
|
|
|
QVERIFY(dataModel->resourceUri().isEmpty());
|
|
|
}
|
|
|
|
|
|
+// --------------------------------------------------------------------------
|
|
|
void ctkXnatSessionTestCase::testParentChild()
|
|
|
{
|
|
|
Q_D(ctkXnatSessionTestCase);
|
|
@@ -153,6 +163,7 @@ void ctkXnatSessionTestCase::testParentChild()
|
|
|
delete project;
|
|
|
}
|
|
|
|
|
|
+// --------------------------------------------------------------------------
|
|
|
void ctkXnatSessionTestCase::testSession()
|
|
|
{
|
|
|
Q_D(ctkXnatSessionTestCase);
|
|
@@ -188,6 +199,7 @@ void ctkXnatSessionTestCase::testSession()
|
|
|
{}
|
|
|
}
|
|
|
|
|
|
+// --------------------------------------------------------------------------
|
|
|
void ctkXnatSessionTestCase::testAuthenticationError()
|
|
|
{
|
|
|
ctkXnatLoginProfile loginProfile;
|
|
@@ -206,6 +218,7 @@ void ctkXnatSessionTestCase::testAuthenticationError()
|
|
|
{}
|
|
|
}
|
|
|
|
|
|
+// --------------------------------------------------------------------------
|
|
|
void ctkXnatSessionTestCase::testCreateProject()
|
|
|
{
|
|
|
Q_D(ctkXnatSessionTestCase);
|
|
@@ -223,7 +236,7 @@ void ctkXnatSessionTestCase::testCreateProject()
|
|
|
bool exists = d->Session->exists(project);
|
|
|
QVERIFY(!exists);
|
|
|
|
|
|
- d->Session->save(project);
|
|
|
+ project->save();
|
|
|
|
|
|
exists = d->Session->exists(project);
|
|
|
QVERIFY(exists);
|
|
@@ -234,6 +247,7 @@ void ctkXnatSessionTestCase::testCreateProject()
|
|
|
QVERIFY(!exists);
|
|
|
}
|
|
|
|
|
|
+// --------------------------------------------------------------------------
|
|
|
void ctkXnatSessionTestCase::testCreateSubject()
|
|
|
{
|
|
|
Q_D(ctkXnatSessionTestCase);
|
|
@@ -274,6 +288,159 @@ void ctkXnatSessionTestCase::testCreateSubject()
|
|
|
}
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
+void ctkXnatSessionTestCase::testAddResourceFolder()
|
|
|
+{
|
|
|
+ Q_D(ctkXnatSessionTestCase);
|
|
|
+
|
|
|
+ ctkXnatDataModel* dataModel = d->Session->dataModel();
|
|
|
+
|
|
|
+ QString projectId = QString("CTK_") + QUuid::createUuid().toString().mid(1, 8);
|
|
|
+ d->Project = projectId;
|
|
|
+
|
|
|
+ ctkXnatProject* project = new ctkXnatProject(dataModel);
|
|
|
+ project->setId(projectId);
|
|
|
+ project->setName(projectId);
|
|
|
+ project->setDescription("CTK_test_project");
|
|
|
+
|
|
|
+ QVERIFY(!project->exists());
|
|
|
+
|
|
|
+ project->save();
|
|
|
+
|
|
|
+ QVERIFY(project->exists());
|
|
|
+
|
|
|
+ ctkXnatResource* resource = project->addResourceFolder("TestResource", "testFormat", "testContent", "testTag1,testTag2");
|
|
|
+ QVERIFY(resource->exists());
|
|
|
+ QVERIFY(resource->name() == "TestResource");
|
|
|
+ QVERIFY(resource->format() == "testFormat");
|
|
|
+ QVERIFY(resource->content() == "testContent");
|
|
|
+ QVERIFY(resource->tags() == "testTag1,testTag2");
|
|
|
+
|
|
|
+ ctkXnatResourceFolder* folder = dynamic_cast<ctkXnatResourceFolder*>(resource->parent());
|
|
|
+ QVERIFY(folder->exists());
|
|
|
+ QVERIFY(folder != 0);
|
|
|
+ QVERIFY(folder->name() == "Resources");
|
|
|
+
|
|
|
+ project->erase();
|
|
|
+ QVERIFY(!project->exists());
|
|
|
+ QVERIFY(!folder->exists());
|
|
|
+ QVERIFY(!resource->exists());
|
|
|
+}
|
|
|
+
|
|
|
+// --------------------------------------------------------------------------
|
|
|
+void ctkXnatSessionTestCase::testUploadAndDownloadFile()
|
|
|
+{
|
|
|
+ Q_D(ctkXnatSessionTestCase);
|
|
|
+
|
|
|
+ ctkXnatDataModel* dataModel = d->Session->dataModel();
|
|
|
+
|
|
|
+ QString projectId = QString("CTK_") + QUuid::createUuid().toString().mid(1, 8);
|
|
|
+ d->Project = projectId;
|
|
|
+
|
|
|
+ ctkXnatProject* project = new ctkXnatProject(dataModel);
|
|
|
+ project->setId(projectId);
|
|
|
+ project->setName(projectId);
|
|
|
+ project->setDescription("CTK_test_project");
|
|
|
+
|
|
|
+ QVERIFY(!project->exists());
|
|
|
+
|
|
|
+ project->save();
|
|
|
+
|
|
|
+ QVERIFY(project->exists());
|
|
|
+
|
|
|
+ ctkXnatResource* resource = project->addResourceFolder("TestResourceContainingData");
|
|
|
+ QVERIFY(resource->exists());
|
|
|
+ QVERIFY(resource->name() == "TestResourceContainingData");
|
|
|
+ QVERIFY(resource->format() == "");
|
|
|
+ QVERIFY(resource->content() == "");
|
|
|
+ QVERIFY(resource->tags() == "");
|
|
|
+
|
|
|
+ QString tempDirPath = QDir::tempPath() + QUuid::createUuid().toString().mid(1, 8);
|
|
|
+ QString uploadFileName = tempDirPath + "/ctk_xnat_upload_" + QUuid::createUuid().toString().mid(1, 8) + ".txt";
|
|
|
+ QString downloadFileName = tempDirPath + "/ctk_xnat_download_" + QUuid::createUuid().toString().mid(1, 8) + ".txt";
|
|
|
+
|
|
|
+ QDir tempDir;
|
|
|
+ if (tempDir.mkdir(tempDirPath))
|
|
|
+ {
|
|
|
+ QFile uploadedFile(uploadFileName);
|
|
|
+
|
|
|
+ if (uploadedFile.open(QFile::ReadWrite))
|
|
|
+ {
|
|
|
+ QTextStream stream( &uploadedFile );
|
|
|
+ stream << "Hi, I am a CTK test file! ;-)" << endl;
|
|
|
+
|
|
|
+ QFileInfo fileInfo;
|
|
|
+ fileInfo.setFile(uploadFileName);
|
|
|
+ // Create xnatFile object
|
|
|
+ ctkXnatFile* xnatFile = new ctkXnatFile(resource);
|
|
|
+ xnatFile->setLocalFilePath(fileInfo.filePath());
|
|
|
+ xnatFile->setName(fileInfo.fileName());
|
|
|
+ xnatFile->setFileFormat("some format");
|
|
|
+ xnatFile->setFileContent("some content");
|
|
|
+ xnatFile->setFileTags("some, tags");
|
|
|
+ resource->add(xnatFile);
|
|
|
+
|
|
|
+ // Actual file upload
|
|
|
+ xnatFile->save();
|
|
|
+
|
|
|
+ QVERIFY(xnatFile->exists());
|
|
|
+
|
|
|
+ xnatFile->download(downloadFileName);
|
|
|
+
|
|
|
+ QFile downloadedFile(downloadFileName);
|
|
|
+
|
|
|
+ QVERIFY(downloadedFile.exists());
|
|
|
+
|
|
|
+ uploadedFile.close();
|
|
|
+ if (downloadedFile.open(QFile::ReadOnly) && uploadedFile.open(QFile::ReadOnly))
|
|
|
+ {
|
|
|
+ QCryptographicHash hashUploaded(QCryptographicHash::Md5);
|
|
|
+ QCryptographicHash hashDownloaded(QCryptographicHash::Md5);
|
|
|
+
|
|
|
+#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
|
|
|
+ hashUploaded.addData(&uploadedFile);
|
|
|
+ hashDownloaded.addData(&downloadedFile);
|
|
|
+#else
|
|
|
+ hashUploaded.addData(uploadedFile.readAll());
|
|
|
+ hashDownloaded.addData(downloadedFile.readAll());
|
|
|
+#endif
|
|
|
+
|
|
|
+ QString md5ChecksumUploaded(hashUploaded.result().toHex());
|
|
|
+ QString md5ChecksumDownloaded(hashDownloaded.result().toHex());
|
|
|
+
|
|
|
+ QVERIFY (md5ChecksumDownloaded == md5ChecksumUploaded);
|
|
|
+
|
|
|
+ // Remove the data from XNAT
|
|
|
+ project->erase();
|
|
|
+ QVERIFY(!project->exists());
|
|
|
+ QVERIFY(!resource->exists());
|
|
|
+ QVERIFY(!xnatFile->exists());
|
|
|
+
|
|
|
+ // Remove the local data
|
|
|
+ uploadedFile.close();
|
|
|
+ downloadedFile.close();
|
|
|
+ uploadedFile.remove();
|
|
|
+ downloadedFile.remove();
|
|
|
+ tempDir.cdUp();
|
|
|
+ tempDir.rmdir(tempDirPath);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ qWarning()<<"Could not open files for validation! Could not finish test!";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ qWarning()<<"Could not create temporary file for upload! Could not finish test!";
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ qWarning()<<"Could not create temporary directory! Could not finish test!";
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// --------------------------------------------------------------------------
|
|
|
int ctkXnatSessionTest(int argc, char* argv[])
|
|
|
{
|
|
|
QCoreApplication app(argc, argv);
|