Mantid
Loading...
Searching...
No Matches
ONCatHelper.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2012 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
8
9#include <exception>
10
11using Mantid::Catalog::OAuth::IOAuthTokenStore;
12using Mantid::Catalog::OAuth::IOAuthTokenStore_uptr;
13using Mantid::Catalog::OAuth::OAuthToken;
16
18
20 : Mantid::Kernel::InternetHelper(), m_responseMap(responseMap), m_responseCallCounts() {
21 for (const auto &mapping : responseMap) {
22 m_responseCallCounts[mapping.first] = 0;
23 }
24}
25
27
29 return std::all_of(m_responseCallCounts.cbegin(), m_responseCallCounts.cend(),
30 [](const MockResponseCallMapping &mapping) { return mapping.second == 1; });
31}
32
34 return std::all_of(m_responseCallCounts.cbegin(), m_responseCallCounts.cend(),
35 [](const MockResponseCallMapping &mapping) { return mapping.second >= 1; });
36}
37
38InternetHelper::HTTPStatus MockONCatAPI::sendHTTPRequest(const std::string &url, std::ostream &responseStream) {
39 return sendHTTPSRequest(url, responseStream);
40}
41
42InternetHelper::HTTPStatus MockONCatAPI::sendHTTPSRequest(const std::string &url, std::ostream &responseStream) {
43 const auto mockResponse = m_responseMap.find(url);
44
45 if (mockResponse == m_responseMap.end()) {
46 // If the test that is using this has not set up a corresponding URL
47 // then throw an exception rather than segfault.
48 throw NotImplementedError(url + " has not been assigned a corresponding response.");
49 }
50
51 m_responseCallCounts[url] += 1;
52
53 const auto statusCode = mockResponse->second.first;
54 const auto responseBody = mockResponse->second.second;
55
56 // Approximate the behaviour of the actual helper class when a non-OK
57 // response is observed.
58 if (statusCode != InternetHelper::HTTPStatus::OK) {
59 throw InternetError(responseBody, static_cast<int>(statusCode));
60 }
61
62 responseStream << responseBody;
63 return statusCode;
64}
65
67std::shared_ptr<MockONCatAPI> make_mock_oncat_api(const MockResponseMap &responseMap) {
68 return std::make_shared<MockONCatAPI>(responseMap);
69}
71
72std::unique_ptr<ONCat> make_oncat_with_mock_api(const std::shared_ptr<MockONCatAPI> &mockAPI) {
73 auto oncat = ONCat::fromMantidSettings();
74 oncat->setInternetHelper(mockAPI);
75 return oncat;
76}
77
79
80void MockTokenStore::setToken(const boost::optional<OAuthToken> &token) { m_token = token; }
81
82boost::optional<OAuthToken> MockTokenStore::getToken() { return m_token; }
83
84IOAuthTokenStore_uptr make_mock_token_store() { return std::make_unique<MockTokenStore>(); }
85
87 auto tokenStore = std::make_unique<MockTokenStore>();
88 tokenStore->setToken(OAuthToken("Bearer", 3600, "2KSL5aEnLvIudMHIjc7LcBWBCfxOHZ", "api:read data:read settings:read",
89 boost::make_optional<std::string>("eZEiz7LbgFrkL5ZHv7R4ck9gOzXexb")));
90 return tokenStore;
91}
92
93} // namespace Mantid::FrameworkTestHelpers
InternetHelper::HTTPStatus sendHTTPSRequest(const std::string &url, std::ostream &responseStream) override
Performs a request using https.
Definition: ONCatHelper.cpp:42
MockResponseCallCounts m_responseCallCounts
Definition: ONCatHelper.h:45
InternetHelper::HTTPStatus sendHTTPRequest(const std::string &url, std::ostream &responseStream) override
Performs a request using http.
Definition: ONCatHelper.cpp:38
void setToken(const boost::optional< OAuthToken > &token) override
Definition: ONCatHelper.cpp:80
boost::optional< OAuthToken > m_token
Definition: ONCatHelper.h:60
boost::optional< OAuthToken > getToken() override
Definition: ONCatHelper.cpp:82
Exception thrown when error occurs accessing an internet resource.
Definition: Exception.h:321
Marks code as not implemented yet.
Definition: Exception.h:138
InternetHelper : A helper class for supporting access to resources through HTTP and HTTPS.
std::shared_ptr< MockONCatAPI > make_mock_oncat_api(const MockResponseMap &responseMap)
std::map< std::string, std::pair< InternetHelper::HTTPStatus, std::string > > MockResponseMap
Definition: ONCatHelper.h:26
IOAuthTokenStore_uptr make_mock_token_store()
Definition: ONCatHelper.cpp:84
std::unique_ptr< ONCat > make_oncat_with_mock_api(const std::shared_ptr< MockONCatAPI > &mockAPI)
Definition: ONCatHelper.cpp:72
std::pair< const std::basic_string< char >, unsigned int > MockResponseCallMapping
Definition: ONCatHelper.h:28
IOAuthTokenStore_uptr make_mock_token_store_already_logged_in()
Definition: ONCatHelper.cpp:86
Helper class which provides the Collimation Length for SANS instruments.
Definition: NDArray.h:49