~michihenning/storage-framework/check-server-side-metadata

« back to all changes in this revision

Viewing changes to tests/remote-client/MockProvider.cpp

  • Committer: Tarmac
  • Author(s): Michi Henning
  • Date: 2016-08-23 11:43:47 UTC
  • mfrom: (53.1.7 remote-client-coverage)
  • Revision ID: tarmac-20160823114347-bwuvliesmauo2v0n
Increased remote client coverage.

Approved by James Henstridge, unity-api-1-bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2016 Canonical Ltd
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 *
 
16
 * Authors: Michi Henning <michi.henning@canonical.com>
 
17
 */
 
18
 
 
19
#include "MockProvider.h"
 
20
 
 
21
#include <unity/storage/provider/Exceptions.h>
 
22
#include <unity/storage/provider/metadata_keys.h>
 
23
 
 
24
#include <boost/thread.hpp>
 
25
#include <boost/thread/future.hpp>
 
26
 
 
27
#include <chrono>
 
28
#include <thread>
 
29
#include <inttypes.h>
 
30
 
 
31
using namespace unity::storage;
 
32
using namespace unity::storage::provider;
 
33
using namespace std;
 
34
 
 
35
using boost::make_exceptional_future;
 
36
using boost::make_ready_future;
 
37
 
 
38
MockProvider::MockProvider()
 
39
{
 
40
}
 
41
 
 
42
MockProvider::MockProvider(string const& cmd)
 
43
    : cmd_(cmd)
 
44
{
 
45
}
 
46
 
 
47
boost::future<ItemList> MockProvider::roots(Context const&)
 
48
{
 
49
    ItemList roots =
 
50
    {
 
51
        {"root_id", {}, "Root", "etag", ItemType::root, {}}
 
52
    };
 
53
    return make_ready_future<ItemList>(roots);
 
54
}
 
55
 
 
56
boost::future<tuple<ItemList,string>> MockProvider::list(
 
57
    string const& item_id, string const& page_token,
 
58
    Context const&)
 
59
{
 
60
    if (item_id != "root_id")
 
61
    {
 
62
        string msg = string("Item::list(): no such item: \"") + item_id + "\"";
 
63
        return make_exceptional_future<tuple<ItemList,string>>(NotExistsException(msg, item_id));
 
64
    }
 
65
    if (page_token != "")
 
66
    {
 
67
        string msg = string("Item::list(): invalid page token: \"") + page_token + "\"";
 
68
        return make_exceptional_future<tuple<ItemList,string>>(LogicException("invalid page token"));
 
69
    }
 
70
    ItemList children =
 
71
    {
 
72
        {
 
73
            "child_id", { "root_id" }, "Child", "etag", ItemType::file,
 
74
            { { SIZE_IN_BYTES, 0 }, { LAST_MODIFIED_TIME, "2007-04-05T14:30Z" } }
 
75
        }
 
76
    };
 
77
    boost::promise<tuple<ItemList,string>> p;
 
78
    p.set_value(make_tuple(children, string()));
 
79
    return p.get_future();
 
80
}
 
81
 
 
82
boost::future<ItemList> MockProvider::lookup(
 
83
    string const& parent_id, string const& name, Context const&)
 
84
{
 
85
    if (parent_id != "root_id")
 
86
    {
 
87
        string msg = string("Folder::lookup(): no such item: \"") + parent_id + "\"";
 
88
        return make_exceptional_future<ItemList>(NotExistsException(msg, parent_id));
 
89
    }
 
90
    if (name != "Child")
 
91
    {
 
92
        string msg = string("Folder::lookup(): no such item: \"") + name + "\"";
 
93
        return make_exceptional_future<ItemList>(NotExistsException(msg, name));
 
94
    }
 
95
    ItemList children =
 
96
    {
 
97
        { "child_id", { "root_id" }, "Child", "etag", ItemType::file,
 
98
          { { SIZE_IN_BYTES, 0 }, { LAST_MODIFIED_TIME, "2007-04-05T14:30Z" } } }
 
99
    };
 
100
    return make_ready_future<ItemList>(children);
 
101
}
 
102
 
 
103
boost::future<Item> MockProvider::metadata(string const& item_id, Context const&)
 
104
{
 
105
    if (item_id == "root_id")
 
106
    {
 
107
        Item metadata{"root_id", {}, "Root", "etag", ItemType::root, {}};
 
108
        return make_ready_future<Item>(metadata);
 
109
    }
 
110
    else if (item_id == "child_id")
 
111
    {
 
112
        Item metadata
 
113
        {
 
114
            "child_id", { "root_id" }, "Child", "etag", ItemType::file,
 
115
            { { SIZE_IN_BYTES, 0 }, { LAST_MODIFIED_TIME, "2007-04-05T14:30Z" } }
 
116
        };
 
117
        return make_ready_future<Item>(metadata);
 
118
    }
 
119
    else if (item_id == "child_folder_id")
 
120
    {
 
121
        Item metadata{"child_folder_id", { "root_id" }, "Child_Folder", "etag", ItemType::folder, {}};
 
122
        return make_ready_future<Item>(metadata);
 
123
    }
 
124
    return make_exceptional_future<Item>(NotExistsException("metadata(): no such item: " + item_id, item_id));
 
125
}
 
126
 
 
127
boost::future<Item> MockProvider::create_folder(
 
128
    string const& parent_id, string const& name,
 
129
    Context const&)
 
130
{
 
131
    Item metadata{"new_folder_id", { parent_id }, name, "etag", ItemType::folder, {}};
 
132
    return make_ready_future<Item>(metadata);
 
133
}
 
134
 
 
135
string make_job_id()
 
136
{
 
137
    static int last_job_id = 0;
 
138
    return to_string(++last_job_id);
 
139
}
 
140
 
 
141
boost::future<unique_ptr<UploadJob>> MockProvider::create_file(
 
142
    string const&, string const&,
 
143
    int64_t, string const&, bool, Context const&)
 
144
{
 
145
    return make_ready_future<unique_ptr<UploadJob>>(new MockUploadJob(make_job_id()));
 
146
}
 
147
 
 
148
boost::future<unique_ptr<UploadJob>> MockProvider::update(
 
149
    string const&, int64_t, string const&, Context const&)
 
150
{
 
151
    return make_ready_future<unique_ptr<UploadJob>>(new MockUploadJob(make_job_id()));
 
152
}
 
153
 
 
154
boost::future<unique_ptr<DownloadJob>> MockProvider::download(
 
155
    string const&, Context const&)
 
156
{
 
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))
 
160
    {
 
161
        ResourceException e("download(): write failed", errno);
 
162
        job->report_error(make_exception_ptr(e));
 
163
        return make_exceptional_future<unique_ptr<DownloadJob>>(e);
 
164
    }
 
165
    job->report_complete();
 
166
    return make_ready_future(std::move(job));
 
167
}
 
168
 
 
169
boost::future<void> MockProvider::delete_item(
 
170
    string const&, Context const&)
 
171
{
 
172
    return make_ready_future();
 
173
}
 
174
 
 
175
boost::future<Item> MockProvider::move(
 
176
    string const& item_id, string const& new_parent_id,
 
177
    string const& new_name, Context const&)
 
178
{
 
179
    Item metadata{item_id, { new_parent_id }, new_name, "etag", ItemType::file, {}};
 
180
    return make_ready_future(metadata);
 
181
}
 
182
 
 
183
boost::future<Item> MockProvider::copy(
 
184
    string const&, string const& new_parent_id,
 
185
    string const& new_name, Context const&)
 
186
{
 
187
    Item metadata{"new_item_id", { new_parent_id }, new_name, "etag", ItemType::file, {}};
 
188
    return make_ready_future(metadata);
 
189
}
 
190
 
 
191
MockUploadJob::MockUploadJob()
 
192
    : UploadJob("some_id")
 
193
{
 
194
}
 
195
 
 
196
MockUploadJob::MockUploadJob(string const& cmd)
 
197
    : UploadJob("some_id")
 
198
    , cmd_(cmd)
 
199
{
 
200
}
 
201
 
 
202
boost::future<void> MockUploadJob::cancel()
 
203
{
 
204
    return make_ready_future();
 
205
}
 
206
 
 
207
boost::future<Item> MockUploadJob::finish()
 
208
{
 
209
    Item metadata
 
210
    {
 
211
        "some_id", { "root_id" }, "some_upload", "etag", ItemType::file,
 
212
        { { SIZE_IN_BYTES, 10 }, { LAST_MODIFIED_TIME, "2011-04-05T14:30:10.005Z" } }
 
213
    };
 
214
    return make_ready_future(metadata);
 
215
}
 
216
 
 
217
MockDownloadJob::MockDownloadJob()
 
218
    : DownloadJob("some_id")
 
219
{
 
220
}
 
221
 
 
222
MockDownloadJob::MockDownloadJob(string const& cmd)
 
223
    : DownloadJob("some_id")
 
224
    , cmd_(cmd)
 
225
{
 
226
}
 
227
 
 
228
boost::future<void> MockDownloadJob::cancel()
 
229
{
 
230
    return make_ready_future();
 
231
}
 
232
 
 
233
boost::future<void> MockDownloadJob::finish()
 
234
{
 
235
    return make_ready_future();
 
236
}