Newer
Older
#ifndef MANTID_DATAHANDLING_DOWNLOADINSTRUMENTTEST_H_
#define MANTID_DATAHANDLING_DOWNLOADINSTRUMENTTEST_H_
#include <cxxtest/TestSuite.h>
#include "MantidDataHandling/DownloadInstrument.h"
#include <Poco/Net/HTTPResponse.h>
#include <Poco/Glob.h>
#include <Poco/File.h>
#include <Poco/Path.h>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
using Mantid::DataHandling::DownloadInstrument;
using namespace Mantid::DataHandling;
using namespace Mantid::API;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
namespace
{
/**
* Mock out the internet calls of this algorithm
*/
class MockedDownloadInstrument : public DownloadInstrument
{
public:
MockedDownloadInstrument() : DownloadInstrument()
{
}
private:
virtual int doDownloadFile(const std::string& urlFile,
const std::string& localFilePath = "",
const String2StringMap& headers = String2StringMap())
{
std::string dateTime;
auto it = headers.find("if-modified-since");
if(it != headers.end())
dateTime = it->second;
std::string outputString;
if (urlFile.find("api.github.com") != std::string::npos)
{
outputString = "[\n"
" {\n"
" \"name\": \"NewFile.xml\",\n"
" \"path\": \"Code/Mantid/instrument/NewFile.xml\",\n"
" \"sha\": \"Xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
" \"size\": 60,\n"
" \"url\": \"https://www.mantidproject.org/invalid\",\n"
" \"html_url\": \"https://www.mantidproject.org/NewFile.xml\",\n"
" \"git_url\": \"https://www.mantidproject.org/invalid\",\n"
" \"type\": \"file\",\n"
" \"_links\": {\n"
" \"self\": \"https://www.mantidproject.org/invalid\",\n"
" \"git\": \"https://www.mantidproject.org/invalid\",\n"
" \"html\": \"https://www.mantidproject.org/invalid\"\n"
" }\n"
" },\n"
" {\n"
" \"name\": \"UpdatableFile.xml\",\n"
" \"path\": \"Code/Mantid/instrument/UpdatableFile.xml\",\n"
" \"sha\": \"d66ba0a04290093d83d41901048068d495d41764\",\n"
" \"size\": 106141,\n"
" \"url\": \"https://www.mantidproject.org/invalid\",\n"
" \"html_url\": \"https://www.mantidproject.org/UpdatableFile.xml\",\n"
" \"git_url\": \"https://www.mantidproject.org/invalid\",\n"
" \"type\": \"file\",\n"
" \"_links\": {\n"
" \"self\": \"https://www.mantidproject.org/invalid\",\n"
" \"git\": \"https://www.mantidproject.org/invalid\",\n"
" \"html\": \"https://www.mantidproject.org/invalid\"\n"
" }\n"
" }\n"
"]";
}
else if (urlFile.find("https://www.mantidproject.org/NewFile.xml") != std::string::npos)
{
outputString = "Here is some sample text for NewFile.xml";
}
else if (urlFile.find("https://www.mantidproject.org/UpdatableFile.xml") != std::string::npos)
{
outputString = "Here is some sample text for WISH_Definition.xml";
}
std::ofstream file;
file.open (localFilePath);
file << outputString;
file.close();
return Poco::Net::HTTPResponse::HTTP_FOUND;
}
};
}
class DownloadInstrumentTest : public CxxTest::TestSuite
{
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static DownloadInstrumentTest *createSuite() { return new DownloadInstrumentTest(); }
static void destroySuite( DownloadInstrumentTest *suite ) { delete suite; }
void test_Init()
{
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
}
void test_exec()
{
std::string localInstDir = Mantid::Kernel::ConfigService::Instance().getInstrumentDirectories()[0];
cleanupDiretory(localInstDir);
TSM_ASSERT_EQUALS("The expected number of files downloaded was wrong.", runDownloadInstrument(), 2);
cleanupDiretory(localInstDir);
}
void test_execTwoTimesInARow()
{
std::string localInstDir = Mantid::Kernel::ConfigService::Instance().getInstrumentDirectories()[0];
cleanupDiretory(localInstDir);
TSM_ASSERT_EQUALS("The expected number of files downloaded the first time was wrong.", runDownloadInstrument(), 2);
TSM_ASSERT_EQUALS("The expected number of files downloaded the second time was wrong.", runDownloadInstrument(), 1);
cleanupDiretory(localInstDir);
}
int runDownloadInstrument()
{
// Name of the output workspace.
std::string outWSName("DownloadInstrumentTest_OutputWS");
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
TS_ASSERT_THROWS_NOTHING( alg.execute(); );
TS_ASSERT( alg.isExecuted() );
return alg.getProperty("FileDownloadCount");
void cleanupDiretory(std::string dir)
{
deleteFiles(dir, "*.xml");
deleteFiles(dir, "*.json");
}
size_t deleteFiles(std::string filePath, std::string pattern)
Poco::Path path(filePath);
path.makeDirectory();
path.setFileName(pattern);
std::set<std::string> files;
Poco::Glob::glob(path.toString(),files);
for (auto it = files.begin(); it != files.end(); ++it)
{
Poco::File file(*it);
file.remove();
}
return files.size();
}
};
#endif /* MANTID_DATAHANDLING_DOWNLOADINSTRUMENTTEST_H_ */