2
* Copyright (C) 2016 Canonical Ltd
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU Lesser General Public License version 3 as
6
* published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Authors: Michi Henning <michi.henning@canonical.com>
19
#include "MockProvider.h"
21
#include <unity/storage/provider/Exceptions.h>
22
#include <unity/storage/provider/metadata_keys.h>
24
#include <boost/thread.hpp>
25
#include <boost/thread/future.hpp>
31
using namespace unity::storage;
32
using namespace unity::storage::provider;
35
using boost::make_exceptional_future;
36
using boost::make_ready_future;
38
MockProvider::MockProvider()
42
MockProvider::MockProvider(string const& cmd)
47
boost::future<ItemList> MockProvider::roots(Context const&)
51
{"root_id", {}, "Root", "etag", ItemType::root, {}}
53
return make_ready_future<ItemList>(roots);
56
boost::future<tuple<ItemList,string>> MockProvider::list(
57
string const& item_id, string const& page_token,
60
if (item_id != "root_id")
62
string msg = string("Item::list(): no such item: \"") + item_id + "\"";
63
return make_exceptional_future<tuple<ItemList,string>>(NotExistsException(msg, item_id));
67
string msg = string("Item::list(): invalid page token: \"") + page_token + "\"";
68
return make_exceptional_future<tuple<ItemList,string>>(LogicException("invalid page token"));
73
"child_id", { "root_id" }, "Child", "etag", ItemType::file,
74
{ { SIZE_IN_BYTES, 0 }, { LAST_MODIFIED_TIME, "2007-04-05T14:30Z" } }
77
boost::promise<tuple<ItemList,string>> p;
78
p.set_value(make_tuple(children, string()));
79
return p.get_future();
82
boost::future<ItemList> MockProvider::lookup(
83
string const& parent_id, string const& name, Context const&)
85
if (parent_id != "root_id")
87
string msg = string("Folder::lookup(): no such item: \"") + parent_id + "\"";
88
return make_exceptional_future<ItemList>(NotExistsException(msg, parent_id));
92
string msg = string("Folder::lookup(): no such item: \"") + name + "\"";
93
return make_exceptional_future<ItemList>(NotExistsException(msg, name));
97
{ "child_id", { "root_id" }, "Child", "etag", ItemType::file,
98
{ { SIZE_IN_BYTES, 0 }, { LAST_MODIFIED_TIME, "2007-04-05T14:30Z" } } }
100
return make_ready_future<ItemList>(children);
103
boost::future<Item> MockProvider::metadata(string const& item_id, Context const&)
105
if (item_id == "root_id")
107
Item metadata{"root_id", {}, "Root", "etag", ItemType::root, {}};
108
return make_ready_future<Item>(metadata);
110
else if (item_id == "child_id")
114
"child_id", { "root_id" }, "Child", "etag", ItemType::file,
115
{ { SIZE_IN_BYTES, 0 }, { LAST_MODIFIED_TIME, "2007-04-05T14:30Z" } }
117
return make_ready_future<Item>(metadata);
119
else if (item_id == "child_folder_id")
121
Item metadata{"child_folder_id", { "root_id" }, "Child_Folder", "etag", ItemType::folder, {}};
122
return make_ready_future<Item>(metadata);
124
return make_exceptional_future<Item>(NotExistsException("metadata(): no such item: " + item_id, item_id));
127
boost::future<Item> MockProvider::create_folder(
128
string const& parent_id, string const& name,
131
Item metadata{"new_folder_id", { parent_id }, name, "etag", ItemType::folder, {}};
132
return make_ready_future<Item>(metadata);
137
static int last_job_id = 0;
138
return to_string(++last_job_id);
141
boost::future<unique_ptr<UploadJob>> MockProvider::create_file(
142
string const&, string const&,
143
int64_t, string const&, bool, Context const&)
145
return make_ready_future<unique_ptr<UploadJob>>(new MockUploadJob(make_job_id()));
148
boost::future<unique_ptr<UploadJob>> MockProvider::update(
149
string const&, int64_t, string const&, Context const&)
151
return make_ready_future<unique_ptr<UploadJob>>(new MockUploadJob(make_job_id()));
154
boost::future<unique_ptr<DownloadJob>> MockProvider::download(
155
string const&, Context const&)
157
unique_ptr<DownloadJob> job(new MockDownloadJob(make_job_id()));
158
const char contents[] = "Hello world";
159
if (write(job->write_socket(), contents, sizeof(contents)) != sizeof(contents))
161
ResourceException e("download(): write failed", errno);
162
job->report_error(make_exception_ptr(e));
163
return make_exceptional_future<unique_ptr<DownloadJob>>(e);
165
job->report_complete();
166
return make_ready_future(std::move(job));
169
boost::future<void> MockProvider::delete_item(
170
string const&, Context const&)
172
return make_ready_future();
175
boost::future<Item> MockProvider::move(
176
string const& item_id, string const& new_parent_id,
177
string const& new_name, Context const&)
179
Item metadata{item_id, { new_parent_id }, new_name, "etag", ItemType::file, {}};
180
return make_ready_future(metadata);
183
boost::future<Item> MockProvider::copy(
184
string const&, string const& new_parent_id,
185
string const& new_name, Context const&)
187
Item metadata{"new_item_id", { new_parent_id }, new_name, "etag", ItemType::file, {}};
188
return make_ready_future(metadata);
191
MockUploadJob::MockUploadJob()
192
: UploadJob("some_id")
196
MockUploadJob::MockUploadJob(string const& cmd)
197
: UploadJob("some_id")
202
boost::future<void> MockUploadJob::cancel()
204
return make_ready_future();
207
boost::future<Item> MockUploadJob::finish()
211
"some_id", { "root_id" }, "some_upload", "etag", ItemType::file,
212
{ { SIZE_IN_BYTES, 10 }, { LAST_MODIFIED_TIME, "2011-04-05T14:30:10.005Z" } }
214
return make_ready_future(metadata);
217
MockDownloadJob::MockDownloadJob()
218
: DownloadJob("some_id")
222
MockDownloadJob::MockDownloadJob(string const& cmd)
223
: DownloadJob("some_id")
228
boost::future<void> MockDownloadJob::cancel()
230
return make_ready_future();
233
boost::future<void> MockDownloadJob::finish()
235
return make_ready_future();