Skip to content

Commit

Permalink
re #8924, merge in #10549 and #10550
Browse files Browse the repository at this point in the history
  • Loading branch information
NickDraper committed Nov 24, 2014
2 parents 36a2cfe + 8450787 commit a34f1ef
Show file tree
Hide file tree
Showing 14 changed files with 1,074 additions and 3 deletions.
9 changes: 9 additions & 0 deletions Code/Mantid/Framework/DataHandling/CMakeLists.txt
Expand Up @@ -11,6 +11,7 @@ set ( SRC_FILES
src/DeleteTableRows.cpp
src/DetermineChunking.cpp
src/DownloadInstrument.cpp
src/DownloadFile.cpp
src/ExtractMonitorWorkspace.cpp
src/FilterEventsByLogValuePreNexus.cpp
src/FindDetectorsInShape.cpp
Expand Down Expand Up @@ -157,7 +158,11 @@ set ( INC_FILES
inc/MantidDataHandling/DefineGaugeVolume.h
inc/MantidDataHandling/DeleteTableRows.h
inc/MantidDataHandling/DetermineChunking.h
<<<<<<< HEAD
inc/MantidDataHandling/DownloadInstrument.h
=======
inc/MantidDataHandling/DownloadFile.h
>>>>>>> 8450787b233cb81fee1cb7776982d04dc4e0b2a4
inc/MantidDataHandling/ExtractMonitorWorkspace.h
inc/MantidDataHandling/FilterEventsByLogValuePreNexus.h
inc/MantidDataHandling/FindDetectorsInShape.h
Expand Down Expand Up @@ -303,7 +308,11 @@ set ( TEST_FILES
DefineGaugeVolumeTest.h
DeleteTableRowsTest.h
DetermineChunkingTest.h
<<<<<<< HEAD
DownloadInstrumentTest.h
=======
DownloadFileTest.h
>>>>>>> 8450787b233cb81fee1cb7776982d04dc4e0b2a4
ExtractMonitorWorkspaceTest.h
FilterEventsByLogValuePreNexusTest.h
FindDetectorsInShapeTest.h
Expand Down
@@ -0,0 +1,67 @@
#ifndef MANTID_DATAHANDLING_DOWNLOADFILE_H_
#define MANTID_DATAHANDLING_DOWNLOADFILE_H_

#include "MantidKernel/System.h"
#include "MantidAPI/Algorithm.h"

namespace Mantid
{

namespace Kernel
{
//forward Declaration
class InternetHelper;
}

namespace DataHandling
{

/** DownloadFile : Downloads a file from a url to the file system
Copyright &copy; 2014 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport DownloadFile : public API::Algorithm
{
public:
DownloadFile();
virtual ~DownloadFile();

virtual const std::string name() const;
virtual int version() const;
virtual const std::string category() const;
virtual const std::string summary() const;

protected:
Kernel::InternetHelper* m_internetHelper;

private:
void init();
void exec();



};


} // namespace DataHandling
} // namespace Mantid

#endif /* MANTID_DATAHANDLING_DOWNLOADFILE_H_ */
93 changes: 93 additions & 0 deletions Code/Mantid/Framework/DataHandling/src/DownloadFile.cpp
@@ -0,0 +1,93 @@
#include "MantidDataHandling/DownloadFile.h"
#include "MantidKernel/InternetHelper.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/MandatoryValidator.h"
#include "MantidAPI/FileProperty.h"

#include "Poco/URI.h"

#include "boost/make_shared.hpp"
#include "boost/algorithm/string/predicate.hpp"

#include <string>
#include <stdexcept>

namespace Mantid
{
namespace DataHandling
{

using Mantid::Kernel::Direction;
using Mantid::Kernel::MandatoryValidator;
using Mantid::Kernel::StringListValidator;
using Mantid::API::WorkspaceProperty;
using Mantid::API::FileProperty;

// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(DownloadFile)



//----------------------------------------------------------------------------------------------
/** Constructor
*/
DownloadFile::DownloadFile():m_internetHelper(new Kernel::InternetHelper())
{
}

//----------------------------------------------------------------------------------------------
/** Destructor
*/
DownloadFile::~DownloadFile()
{
delete m_internetHelper;
}


//----------------------------------------------------------------------------------------------

/// Algorithms name for identification. @see Algorithm::name
const std::string DownloadFile::name() const { return "DownloadFile"; }

/// Algorithm's version for identification. @see Algorithm::version
int DownloadFile::version() const { return 1;}

/// Algorithm's category for identification. @see Algorithm::category
const std::string DownloadFile::category() const { return "DataHandling";}

/// Algorithm's summary for use in the GUI and help. @see Algorithm::summary
const std::string DownloadFile::summary() const { return "Downloads a file from a url to the file system";}


//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void DownloadFile::init()
{
declareProperty("Address", "", boost::make_shared<MandatoryValidator<std::string> >(),
"The address of the network resource to download. This should start http:// or https:// .", Direction::InOut);
declareProperty(new FileProperty("Filename", "", FileProperty::Save),
"The filename to save the download to.");
}

//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void DownloadFile::exec()
{
std::string address = getProperty("Address");
if ((!boost::starts_with(address,"http://")) && (!boost::starts_with(address,"https://")))
{
address = "http://" + address;
g_log.information("Address must start http:// or https://, http has been assumed to continue: " + address);
}
std::string filename = getProperty("Filename");

Poco::URI url(address);
m_internetHelper->downloadFile(url.toString(),filename);
setProperty("Address",address);
}


} // namespace DataHandling
} // namespace Mantid
146 changes: 146 additions & 0 deletions Code/Mantid/Framework/DataHandling/test/DownloadFileTest.h
@@ -0,0 +1,146 @@
#ifndef MANTID_DATAHANDLING_DOWNLOADFILETEST_H_
#define MANTID_DATAHANDLING_DOWNLOADFILETEST_H_

#include <cxxtest/TestSuite.h>

#include "MantidDataHandling/DownloadFile.h"
#include "MantidKernel/InternetHelper.h"

#include <Poco/TemporaryFile.h>


#include <fstream>
#include <sstream>

using Mantid::Kernel::InternetHelper;
using Mantid::DataHandling::DownloadFile;
using namespace Mantid::Kernel;
using namespace Mantid::API;


namespace
{
/**
* Mock out the internet calls of this helper
*/
class MockedInternetHelper : public InternetHelper
{
protected:
virtual int sendHTTPSRequest(const std::string& url,
std::ostream& responseStream,
const StringToStringMap& headers = StringToStringMap())
{
UNUSED_ARG(url);
UNUSED_ARG(headers);
responseStream << "HTTPS request succeeded";
return 200;
}
virtual int sendHTTPRequest(const std::string& url,
std::ostream& responseStream,
const StringToStringMap& headers = StringToStringMap())
{
UNUSED_ARG(url);
UNUSED_ARG(headers);
responseStream << "HTTP request succeeded";
return 200;
}
};

/**
* Mock out the internet calls of this algorithm
*/
class MockedDownloadFile : public DownloadFile
{
public:
MockedDownloadFile()
{
delete m_internetHelper;
m_internetHelper = new MockedInternetHelper();
}
};
}


class DownloadFileTest : 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 DownloadFileTest *createSuite() { return new DownloadFileTest(); }
static void destroySuite( DownloadFileTest *suite ) { delete suite; }


void test_Init()
{
DownloadFile alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
}

void test_Bad_Address()
{
MockedInternetHelper internetHelper;
std::string url = "www.google.com";
Poco::TemporaryFile tmpFile;
exec_alg(url,tmpFile.path(), "http://" + url);
}

void exec_alg(std::string address, std::string filename, std::string newAddress = "")
{
MockedDownloadFile alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("Address", address) );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("Filename", filename) );
TS_ASSERT_THROWS_NOTHING( alg.execute(); );
TS_ASSERT( alg.isExecuted() );
if (newAddress.size() > 0)
{
TS_ASSERT_EQUALS(newAddress, alg.getPropertyValue("Address") );
}
}

void test_DownloadFile_HTTP()
{
MockedInternetHelper internetHelper;
std::string url = "http://www.google.com";
Poco::TemporaryFile tmpFile;
exec_alg(url,tmpFile.path());
TSM_ASSERT("File has not been created.",tmpFile.exists());
TSM_ASSERT("File is not a file.",tmpFile.isFile());
std::fstream fs;
TS_ASSERT_THROWS_NOTHING (fs.open (tmpFile.path().c_str(), std::fstream::in ));

TSM_ASSERT("Cannot open file.",fs.is_open());

std::stringstream ss;
ss << fs.rdbuf();//read the file
fs.close();

TS_ASSERT_EQUALS ("HTTP request succeeded", ss.str());
}

void test_DownloadFile_HTTPS()
{
MockedInternetHelper internetHelper;
std::string httpsUrl = "https://api.github.com/repos/mantidproject/mantid/contents";
Poco::TemporaryFile tmpFile;
exec_alg(httpsUrl,tmpFile.path());
TSM_ASSERT("File has not been created.",tmpFile.exists());
TSM_ASSERT("File is not a file.",tmpFile.isFile());
std::fstream fs;
TS_ASSERT_THROWS_NOTHING (fs.open (tmpFile.path().c_str(), std::fstream::in ));

TSM_ASSERT("Cannot open file.",fs.is_open());

std::stringstream ss;
ss << fs.rdbuf();//read the file
fs.close();

TS_ASSERT_EQUALS ("HTTPS request succeeded", ss.str());
}

};


#endif /* MANTID_DATAHANDLING_DOWNLOADFILETEST_H_ */
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/Kernel/CMakeLists.txt
Expand Up @@ -33,6 +33,7 @@ set ( SRC_FILES
src/IPropertyManager.cpp
src/ISaveable.cpp
src/InstrumentInfo.cpp
src/InternetHelper.cpp
src/Interpolation.cpp
src/LibraryManager.cpp
src/LibraryWrapper.cpp
Expand Down Expand Up @@ -162,6 +163,7 @@ set ( INC_FILES
inc/MantidKernel/IValidator.h
inc/MantidKernel/Instantiator.h
inc/MantidKernel/InstrumentInfo.h
inc/MantidKernel/InternetHelper.h
inc/MantidKernel/Interpolation.h
inc/MantidKernel/LibraryManager.h
inc/MantidKernel/LibraryWrapper.h
Expand Down Expand Up @@ -287,6 +289,7 @@ set ( TEST_FILES
ISaveableTest.h
IValidatorTest.h
InstrumentInfoTest.h
InternetHelperTest.h
InterpolationTest.h
ListValidatorTest.h
LogFilterTest.h
Expand Down

0 comments on commit a34f1ef

Please sign in to comment.