diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/mailtransportresource/mailtransportresource.cpp | 70 | ||||
-rw-r--r-- | examples/mailtransportresource/tests/mailtransporttest.cpp | 20 |
2 files changed, 49 insertions, 41 deletions
diff --git a/examples/mailtransportresource/mailtransportresource.cpp b/examples/mailtransportresource/mailtransportresource.cpp index e8a15ee..51e4976 100644 --- a/examples/mailtransportresource/mailtransportresource.cpp +++ b/examples/mailtransportresource/mailtransportresource.cpp | |||
@@ -34,6 +34,7 @@ | |||
34 | #include "resultprovider.h" | 34 | #include "resultprovider.h" |
35 | #include "mailtransport.h" | 35 | #include "mailtransport.h" |
36 | #include "mail_generated.h" | 36 | #include "mail_generated.h" |
37 | #include "inspection.h" | ||
37 | #include <synchronizer.h> | 38 | #include <synchronizer.h> |
38 | #include <log.h> | 39 | #include <log.h> |
39 | #include <resourceconfig.h> | 40 | #include <resourceconfig.h> |
@@ -91,24 +92,6 @@ public: | |||
91 | QByteArray mResourceInstanceIdentifier; | 92 | QByteArray mResourceInstanceIdentifier; |
92 | }; | 93 | }; |
93 | 94 | ||
94 | static KAsync::Job<void>send(const ApplicationDomain::Mail &mail, const MailtransportResource::Settings &settings) | ||
95 | { | ||
96 | const auto data = mail.getMimeMessage(); | ||
97 | auto msg = KMime::Message::Ptr::create(); | ||
98 | msg->setHead(KMime::CRLFtoLF(data)); | ||
99 | msg->parse(); | ||
100 | if (settings.testMode) { | ||
101 | Log() << "I would totally send that mail, but I'm in test mode."; | ||
102 | } else { | ||
103 | if (MailTransport::sendMessage(msg, settings.server.toUtf8(), settings.username.toUtf8(), settings.password.toUtf8(), settings.cacert.toUtf8())) { | ||
104 | Log() << "Sent message successfully"; | ||
105 | } else { | ||
106 | Log() << "Failed to send message"; | ||
107 | return KAsync::error<void>(1, "Failed to send the message."); | ||
108 | } | ||
109 | } | ||
110 | return KAsync::null<void>(); | ||
111 | } | ||
112 | 95 | ||
113 | //TODO fold into synchronizer | 96 | //TODO fold into synchronizer |
114 | class MailtransportWriteback : public Sink::SourceWriteBack | 97 | class MailtransportWriteback : public Sink::SourceWriteBack |
@@ -137,11 +120,39 @@ public: | |||
137 | class MailtransportSynchronizer : public Sink::Synchronizer { | 120 | class MailtransportSynchronizer : public Sink::Synchronizer { |
138 | public: | 121 | public: |
139 | MailtransportSynchronizer(const QByteArray &resourceType, const QByteArray &resourceInstanceIdentifier) | 122 | MailtransportSynchronizer(const QByteArray &resourceType, const QByteArray &resourceInstanceIdentifier) |
140 | : Sink::Synchronizer(resourceType, resourceInstanceIdentifier) | 123 | : Sink::Synchronizer(resourceType, resourceInstanceIdentifier), |
124 | mResourceInstanceIdentifier(resourceInstanceIdentifier) | ||
141 | { | 125 | { |
142 | 126 | ||
143 | } | 127 | } |
144 | 128 | ||
129 | KAsync::Job<void>send(const ApplicationDomain::Mail &mail, const MailtransportResource::Settings &settings) | ||
130 | { | ||
131 | const auto data = mail.getMimeMessage(); | ||
132 | auto msg = KMime::Message::Ptr::create(); | ||
133 | msg->setHead(KMime::CRLFtoLF(data)); | ||
134 | msg->parse(); | ||
135 | if (settings.testMode) { | ||
136 | Log() << "I would totally send that mail, but I'm in test mode." << mail.identifier(); | ||
137 | auto path = resourceStorageLocation(mResourceInstanceIdentifier) + "/test/"; | ||
138 | Trace() << path; | ||
139 | QDir dir; | ||
140 | dir.mkpath(path); | ||
141 | QFile f(path+ mail.identifier()); | ||
142 | f.open(QIODevice::ReadWrite); | ||
143 | f.write("foo"); | ||
144 | f.close(); | ||
145 | } else { | ||
146 | if (MailTransport::sendMessage(msg, settings.server.toUtf8(), settings.username.toUtf8(), settings.password.toUtf8(), settings.cacert.toUtf8())) { | ||
147 | Log() << "Sent message successfully"; | ||
148 | } else { | ||
149 | Log() << "Failed to send message"; | ||
150 | return KAsync::error<void>(1, "Failed to send the message."); | ||
151 | } | ||
152 | } | ||
153 | return KAsync::null<void>(); | ||
154 | } | ||
155 | |||
145 | KAsync::Job<void> synchronizeWithSource() Q_DECL_OVERRIDE | 156 | KAsync::Job<void> synchronizeWithSource() Q_DECL_OVERRIDE |
146 | { | 157 | { |
147 | Log() << " Synchronizing"; | 158 | Log() << " Synchronizing"; |
@@ -151,16 +162,18 @@ public: | |||
151 | Log() << " Looking for mail"; | 162 | Log() << " Looking for mail"; |
152 | store().reader<ApplicationDomain::Mail>().query(query, [&](const ApplicationDomain::Mail &mail) -> bool { | 163 | store().reader<ApplicationDomain::Mail>().query(query, [&](const ApplicationDomain::Mail &mail) -> bool { |
153 | Trace() << "Found mail: " << mail.identifier(); | 164 | Trace() << "Found mail: " << mail.identifier(); |
154 | // if (!mail.isSent()) { | 165 | if (!mail.getSent()) { |
155 | toSend << mail; | 166 | toSend << mail; |
156 | // } | 167 | } |
157 | return true; | 168 | return true; |
158 | }); | 169 | }); |
159 | auto job = KAsync::null<void>(); | 170 | auto job = KAsync::null<void>(); |
160 | for (const auto &m : toSend) { | 171 | for (const auto &m : toSend) { |
161 | job = job.then(send(m, mSettings)).then<void>([]() { | 172 | job = job.then(send(m, mSettings)).then<void>([this, m]() { |
162 | //on success, mark the mail as sent and move it to a separate place | 173 | auto modifiedMail = ApplicationDomain::Mail(mResourceInstanceIdentifier, m.identifier(), m.revision(), QSharedPointer<Sink::ApplicationDomain::MemoryBufferAdaptor>::create()); |
163 | //TODO | 174 | modifiedMail.setSent(true); |
175 | modify(modifiedMail); | ||
176 | //TODO copy to a sent mail folder as well | ||
164 | }); | 177 | }); |
165 | } | 178 | } |
166 | job = job.then<void>([&future]() { | 179 | job = job.then<void>([&future]() { |
@@ -208,6 +221,15 @@ void MailtransportResource::removeFromDisk(const QByteArray &instanceIdentifier) | |||
208 | 221 | ||
209 | KAsync::Job<void> MailtransportResource::inspect(int inspectionType, const QByteArray &inspectionId, const QByteArray &domainType, const QByteArray &entityId, const QByteArray &property, const QVariant &expectedValue) | 222 | KAsync::Job<void> MailtransportResource::inspect(int inspectionType, const QByteArray &inspectionId, const QByteArray &domainType, const QByteArray &entityId, const QByteArray &property, const QVariant &expectedValue) |
210 | { | 223 | { |
224 | if (domainType == ENTITY_TYPE_MAIL) { | ||
225 | if (inspectionType == Sink::ResourceControl::Inspection::ExistenceInspectionType) { | ||
226 | auto path = resourceStorageLocation(mResourceInstanceIdentifier) + "/test/" + entityId; | ||
227 | if (QFileInfo::exists(path)) { | ||
228 | return KAsync::null<void>(); | ||
229 | } | ||
230 | return KAsync::error<void>(1, "Couldn't find message: " + path); | ||
231 | } | ||
232 | } | ||
211 | return KAsync::null<void>(); | 233 | return KAsync::null<void>(); |
212 | } | 234 | } |
213 | 235 | ||
diff --git a/examples/mailtransportresource/tests/mailtransporttest.cpp b/examples/mailtransportresource/tests/mailtransporttest.cpp index 564b4cb..f1190a7 100644 --- a/examples/mailtransportresource/tests/mailtransporttest.cpp +++ b/examples/mailtransportresource/tests/mailtransporttest.cpp | |||
@@ -26,11 +26,6 @@ class MailtransportTest : public QObject | |||
26 | resource.setProperty("testmode", true); | 26 | resource.setProperty("testmode", true); |
27 | return resource; | 27 | return resource; |
28 | } | 28 | } |
29 | |||
30 | void removeResourceFromDisk(const QByteArray &identifier) | ||
31 | { | ||
32 | // ::MailtransportResource::removeFromDisk(identifier); | ||
33 | } | ||
34 | QByteArray mResourceInstanceIdentifier; | 29 | QByteArray mResourceInstanceIdentifier; |
35 | 30 | ||
36 | private slots: | 31 | private slots: |
@@ -39,13 +34,10 @@ private slots: | |||
39 | { | 34 | { |
40 | Test::initTest(); | 35 | Test::initTest(); |
41 | Log::setDebugOutputLevel(Sink::Log::Trace); | 36 | Log::setDebugOutputLevel(Sink::Log::Trace); |
42 | // resetTestEnvironment(); | ||
43 | auto resource = createResource(); | 37 | auto resource = createResource(); |
44 | QVERIFY(!resource.identifier().isEmpty()); | 38 | QVERIFY(!resource.identifier().isEmpty()); |
45 | VERIFYEXEC(Store::create(resource)); | 39 | VERIFYEXEC(Store::create(resource)); |
46 | mResourceInstanceIdentifier = resource.identifier(); | 40 | mResourceInstanceIdentifier = resource.identifier(); |
47 | // mCapabilities = resource.getProperty("capabilities").value<QByteArrayList>(); | ||
48 | qWarning() << "FOooooooooooooooooooo"; | ||
49 | } | 41 | } |
50 | 42 | ||
51 | void cleanup() | 43 | void cleanup() |
@@ -56,9 +48,6 @@ private slots: | |||
56 | 48 | ||
57 | void init() | 49 | void init() |
58 | { | 50 | { |
59 | // qDebug(); | ||
60 | // qDebug() << "-----------------------------------------"; | ||
61 | // qDebug(); | ||
62 | // VERIFYEXEC(ResourceControl::start(mResourceInstanceIdentifier)); | 51 | // VERIFYEXEC(ResourceControl::start(mResourceInstanceIdentifier)); |
63 | } | 52 | } |
64 | 53 | ||
@@ -74,13 +63,10 @@ private slots: | |||
74 | VERIFYEXEC(Store::create(mail)); | 63 | VERIFYEXEC(Store::create(mail)); |
75 | VERIFYEXEC(ResourceControl::flushMessageQueue(QByteArrayList() << mResourceInstanceIdentifier)); | 64 | VERIFYEXEC(ResourceControl::flushMessageQueue(QByteArrayList() << mResourceInstanceIdentifier)); |
76 | VERIFYEXEC(Store::synchronize(Query::ResourceFilter(mResourceInstanceIdentifier))); | 65 | VERIFYEXEC(Store::synchronize(Query::ResourceFilter(mResourceInstanceIdentifier))); |
66 | VERIFYEXEC(ResourceControl::inspect<ApplicationDomain::Mail>(ResourceControl::Inspection::ExistenceInspection(mail, true))); | ||
77 | 67 | ||
78 | //TODO verify the mail has been sent | 68 | auto sentMail = Store::readOne<ApplicationDomain::Mail>(Query::IdentityFilter(mail).request<Mail::Sent>()); |
79 | 69 | QVERIFY(sentMail.getSent()); | |
80 | // auto modifiedMail = Store::readOne<ApplicationDomain::Mail>(Query::IdentityFilter(mail)); | ||
81 | // VERIFYEXEC(Store::modify(modifiedMail)); | ||
82 | // VERIFYEXEC(ResourceControl::flushMessageQueue(QByteArrayList() << mResourceInstanceIdentifier)); | ||
83 | // VERIFYEXEC(ResourceControl::flushReplayQueue(QByteArrayList() << mResourceInstanceIdentifier)); | ||
84 | 70 | ||
85 | } | 71 | } |
86 | 72 | ||