summaryrefslogtreecommitdiffstats
path: root/tests/teststore.cpp
blob: 902014cd2976ba0fb978453ef6f7fbaea6342fbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
    Copyright (c) 2018 Christian Mollekopf <mollekopf@kolabsys.com>

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library 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 Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/
#include "teststore.h"

#include <sink/store.h>
#include <sink/resourcecontrol.h>
#include <sink/secretstore.h>

#include <kmime/kmime_message.h>

#include <KCalCore/Event>
#include <KCalCore/ICalFormat>

#include <QDebug>
#include <QUuid>
#include <QVariant>

#include "framework/src/domain/mime/mailtemplates.h"

using namespace Kube;

static void iterateOverObjects(const QVariantList &list, std::function<void(const QVariantMap &)> callback)
{
    for (const auto &entry : list) {
        auto object = entry.toMap();
        callback(object);
    }
}

static QStringList toStringList(const QVariantList &list)
{
    QStringList s;
    for (const auto &e : list) {
        s << e.toString();
    }
    return s;
}

static QByteArrayList toByteArrayList(const QVariantList &list)
{
    QByteArrayList s;
    for (const auto &e : list) {
        s << e.toByteArray();
    }
    return s;
}

static void createMail(const QVariantMap &object, const QByteArray &folder = {})
{
    using namespace Sink::ApplicationDomain;

    auto toAddresses = toStringList(object["to"].toList());
    auto ccAddresses = toStringList(object["cc"].toList());
    auto bccAddresses = toStringList(object["bcc"].toList());

    QList<Attachment> attachments = {};
    if (object.contains("attachments")) {
        auto attachmentSpecs = object["attachments"].toList();
        for (int i = 0; i < attachmentSpecs.size(); ++i) {
            auto const &spec = attachmentSpecs.at(i).toMap();
            attachments << Attachment{spec["name"].toString(),
                spec["name"].toString(),
                spec["mimeType"].toByteArray(),
                false,
                spec["data"].toByteArray()};
        }
    }

    KMime::Types::Mailbox mb;
    mb.fromUnicodeString("identity@example.org");
    auto msg = MailTemplates::createMessage({},
            toAddresses,
            ccAddresses,
            bccAddresses,
            mb,
            object["subject"].toString(),
            object["body"].toString(),
            object["bodyIsHtml"].toBool(),
            attachments,
            {},
            {});
    if (object.contains("messageId")) {
        msg->messageID(true)->from7BitString(object["messageId"].toByteArray());
    }
    if (object.contains("inReplyTo")) {
        msg->inReplyTo(true)->from7BitString(object["inReplyTo"].toByteArray());
    }
    if (object.contains("date")) {
        msg->date(true)->setDateTime(QDateTime::fromString(object["date"].toString(), Qt::ISODate));
    }

    msg->assemble();

    auto mail = ApplicationDomainType::createEntity<Mail>(object["resource"].toByteArray());
    mail.setMimeMessage(msg->encodedContent(true));
    mail.setUnread(object["unread"].toBool());
    if (!folder.isEmpty()) {
        mail.setFolder(folder);
    }
    Sink::Store::create(mail).exec().waitForFinished();
}

static void createFolder(const QVariantMap &object)
{
    using namespace Sink::ApplicationDomain;
    auto folder = ApplicationDomainType::createEntity<Folder>(object["resource"].toByteArray());
    folder.setName(object["name"].toString());
    folder.setSpecialPurpose(toByteArrayList(object["specialpurpose"].toList()));
    Sink::Store::create(folder).exec().waitForFinished();

    iterateOverObjects(object.value("mails").toList(), [=](const QVariantMap &object) {
        createMail(object, folder.identifier());
    });
}

static void createEvent(const QVariantMap &object, const QByteArray &calendarId = {})
{
    using Sink::ApplicationDomain::ApplicationDomainType;
    using Sink::ApplicationDomain::Event;

    auto sinkEvent = ApplicationDomainType::createEntity<Event>(object["resource"].toByteArray());

    auto calcoreEvent = QSharedPointer<KCalCore::Event>::create();

    if (object.contains("uid")) {
        auto uid = object["uid"].toString();
        sinkEvent.setUid(uid);
        calcoreEvent->setUid(uid);
    } else {
        auto uid = QUuid::createUuid().toString();
        sinkEvent.setUid(uid);
        calcoreEvent->setUid(uid);
    }

    auto summary = object["summary"].toString();
    sinkEvent.setSummary(summary);
    calcoreEvent->setSummary(summary);

    if (object.contains("description")) {
        auto description = object["description"].toString();
        sinkEvent.setDescription(description);
        calcoreEvent->setDescription(description);
    }

    auto startTime = object["starts"].toDateTime();
    auto endTime = object["ends"].toDateTime();
    sinkEvent.setStartTime(startTime);
    sinkEvent.setEndTime(endTime);

    calcoreEvent->setDtStart(startTime);
    calcoreEvent->setDtEnd(endTime);

    auto ical = KCalCore::ICalFormat().toRawString(static_cast<QSharedPointer<KCalCore::Incidence>>(calcoreEvent));
    sinkEvent.setIcal(ical);

    sinkEvent.setCalendar(calendarId);

    Sink::Store::create(sinkEvent).exec().waitForFinished();
}

static void createCalendar(const QVariantMap &object)
{
    using Sink::ApplicationDomain::Calendar;
    using Sink::ApplicationDomain::ApplicationDomainType;

    auto calendar = ApplicationDomainType::createEntity<Calendar>(object["resource"].toByteArray());
    calendar.setName(object["name"].toString());
    Sink::Store::create(calendar).exec().waitForFinished();

    auto calendarId = calendar.identifier();
    iterateOverObjects(object.value("events").toList(),
        [calendarId](const QVariantMap &object) { createEvent(object, calendarId); });
}

void TestStore::setup(const QVariantMap &map)
{
    using namespace Sink::ApplicationDomain;
    iterateOverObjects(map.value("accounts").toList(), [&] (const QVariantMap &object) {
        auto account = ApplicationDomainType::createEntity<SinkAccount>("", object["id"].toByteArray());
        account.setName(object["name"].toString());
        Sink::Store::create(account).exec().waitForFinished();
    });
    QByteArrayList resources;
    iterateOverObjects(map.value("resources").toList(), [&] (const QVariantMap &object) {
        resources << object["id"].toByteArray();
        auto resource = [&] {
            using namespace Sink::ApplicationDomain;
            auto resource = ApplicationDomainType::createEntity<SinkResource>("", object["id"].toByteArray());
            if (object["type"] == "dummy") {
                resource.setResourceType("sink.dummy");
            } else if (object["type"] == "mailtransport") {
                resource.setResourceType("sink.mailtransport");
                resource.setProperty("testmode", true);
            } else if (object["type"] == "caldav") {
                resource.setResourceType("sink.caldav");
                resource.setProperty("testmode", true);
            } else {
                Q_ASSERT(false);
            }
            return resource;
        }();
        resource.setAccount(object["account"].toByteArray());
        Sink::Store::create(resource).exec().waitForFinished();
        Sink::SecretStore::instance().insert(resource.identifier(), "secret");
    });

    iterateOverObjects(map.value("identities").toList(), [] (const QVariantMap &object) {
        auto identity = Sink::ApplicationDomain::Identity{};
        identity.setAccount(object["account"].toByteArray());
        identity.setAddress(object["address"].toString());
        identity.setName(object["name"].toString());
        Sink::Store::create(identity).exec().waitForFinished();
    });

    iterateOverObjects(map.value("folders").toList(), createFolder);
    iterateOverObjects(map.value("mails").toList(), [] (const QVariantMap &map) {
        createMail(map);
    });

    iterateOverObjects(map.value("calendars").toList(), createCalendar);

    Sink::ResourceControl::flushMessageQueue(resources).exec().waitForFinished();
}

QVariant TestStore::load(const QByteArray &type, const QVariantMap &filter)
{
    using namespace Sink::ApplicationDomain;
    const auto list = loadList(type, filter);
    if (!list.isEmpty()) {
        if (list.size() > 1) {
            qWarning() << "While loading" << type << "with filter" << filter
                       << "; got multiple elements, but returning the first one.";
        }
        return list.first();
    }
    return {};
}
template <typename T>
QVariantList toVariantList(const QList<T> &list)
{
    QVariantList result;
    std::transform(list.constBegin(), list.constEnd(), std::back_inserter(result), [] (const T &m) {
        return QVariant::fromValue(T::Ptr::create(m));
    });
    Q_ASSERT(list.size() == result.size());
    return result;
}

QVariantList TestStore::loadList(const QByteArray &type, const QVariantMap &filter)
{
    using namespace Sink::ApplicationDomain;
    Sink::Query query;
    if (filter.contains("resource")) {
        query.resourceFilter(filter.value("resource").toByteArray());
    }

    for (QVariantMap::const_iterator it = filter.begin(); it != filter.end(); ++it) {
        if (it.key() == "messageId") {
            query.filter<Mail::MessageId>(it.value());
        } else if (it.key() == "draft") {
            query.filter<Mail::Draft>(it.value());
        } else if (it.key() == "subject") {
            query.filter<Mail::Subject>(it.value());
        }
    }

    if (type == "mail") {
        return toVariantList(Sink::Store::read<Mail>(query));
    }
    if (type == "folder") {
        return toVariantList(Sink::Store::read<Folder>(query));
    }
    if (type == "resource") {
        return toVariantList(Sink::Store::read<SinkResource>(query));
    }
    if (type == "account") {
        return toVariantList(Sink::Store::read<SinkAccount>(query));
    }

    Q_ASSERT(false);
    return {};
}

QVariantMap TestStore::read(const QVariant &object)
{
    using namespace Sink::ApplicationDomain;
    QVariantMap map;
    if (auto mail = object.value<Mail::Ptr>()) {
        map.insert("uid", mail->identifier());
        map.insert("subject", mail->getSubject());
        map.insert("draft", mail->getDraft());
        return map;
    }
    Q_ASSERT(false);
    return {};
}