summaryrefslogtreecommitdiffstats
path: root/tests/auto/qjsondbwatcher/testqjsondbwatcher.cpp
blob: 44e6e2425b042679c16b78dadce9d8bcc090e221 (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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
/****************************************************************************
 **
 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
 ** Contact: http://www.qt-project.org/
 **
 ** This file is part of the QtAddOn.JsonDb module of the Qt Toolkit.
 **
 ** $QT_BEGIN_LICENSE:LGPL$
 ** GNU Lesser General Public License Usage
 ** This file may be used under the terms of the GNU Lesser General Public
 ** License version 2.1 as published by the Free Software Foundation and
 ** appearing in the file LICENSE.LGPL included in the packaging of this
 ** file. Please review the following information to ensure the GNU Lesser
 ** General Public License version 2.1 requirements will be met:
 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 **
 ** In addition, as a special exception, Nokia gives you certain additional
 ** rights. These rights are described in the Nokia Qt LGPL Exception
 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 **
 ** GNU General Public License Usage
 ** Alternatively, this file may be used under the terms of the GNU General
 ** Public License version 3.0 as published by the Free Software Foundation
 ** and appearing in the file LICENSE.GPL included in the packaging of this
 ** file. Please review the following information to ensure the GNU General
 ** Public License version 3.0 requirements will be met:
 ** http://www.gnu.org/copyleft/gpl.html.
 **
 ** Other Usage
 ** Alternatively, this file may be used in accordance with the terms and
 ** conditions contained in a signed written agreement between you and Nokia.
 **
 **
 **
 **
 **
 **
 ** $QT_END_LICENSE$
 **
 ****************************************************************************/

#include <QCoreApplication>
#include <QFile>
#include <QList>
#include <QTest>
#include <QProcess>
#include <QEventLoop>
#include <QDebug>
#include <QTimer>
#include <QUuid>

#include "qjsonobject.h"
#include "qjsonvalue.h"
#include "qjsonarray.h"
#include "qjsondocument.h"

#include "qjsondbconnection.h"
#include "qjsondbobject.h"
#include "qjsondbreadrequest.h"
#include "qjsondbwatcher.h"
#include "qjsondbwriterequest.h"
#include "private/qjsondbstrings_p.h"
#include "private/qjsondbstandardpaths_p.h"

#include "testhelper.h"

QT_USE_NAMESPACE_JSONDB

// #define EXTRA_DEBUG

class TestQJsonDbWatcher: public TestHelper
{
    Q_OBJECT
    public:
    TestQJsonDbWatcher();
    ~TestQJsonDbWatcher();

private slots:
    void initTestCase();
    void cleanupTestCase();
    void init();
    void cleanup();

    void createAndRemove_data();
    void createAndRemove();
    void indexValue_data();
    void indexValue();
    void history();
    void currentState();
    void notificationTriggersView();
    void notificationTriggersMapReduce();
    void notificationTriggersMultiViews();
    void typeChangeEagerViewSource();
    void invalid();
    void privatePartition();
    void addAndRemove();
    void removeWatcherStatus();
    void uuidQuery();
};

TestQJsonDbWatcher::TestQJsonDbWatcher()
{
}

TestQJsonDbWatcher::~TestQJsonDbWatcher()
{
}

void TestQJsonDbWatcher::initTestCase()
{
    QJsonDbStandardPaths::setAutotestMode(true);
    removeDbFiles();

    QStringList arg_list = QStringList() << "-validate-schemas";
    launchJsonDbDaemon(arg_list, __FILE__);
}

void TestQJsonDbWatcher::cleanupTestCase()
{
    removeDbFiles();
    stopDaemon();
}

void TestQJsonDbWatcher::init()
{
    clearHelperData();
    connectToServer();
}

void TestQJsonDbWatcher::cleanup()
{
    disconnectFromServer();
}

void TestQJsonDbWatcher::createAndRemove_data()
{
    QTest::addColumn<QString>("partition");

    QTest::newRow("persistent") << "";
    QTest::newRow("ephemeral") << "Ephemeral";
}

/*
 * Watch for an item creation
 */

void TestQJsonDbWatcher::createAndRemove()
{
    QVERIFY(mConnection);
    QFETCH(QString, partition);

    // create a watcher
    QJsonDbWatcher watcher;
    watcher.setWatchedActions(QJsonDbWatcher::All);
    watcher.setQuery(QLatin1String("[?_type=%type]"));
    watcher.bindValue(QLatin1String("type"), QLatin1String("com.test.qjsondbwatcher-test"));
    watcher.setPartition(partition);
    mConnection->addWatcher(&watcher);
    QVERIFY(waitForStatus(&watcher, QJsonDbWatcher::Active));

    QJsonObject item;
    item.insert(JsonDbStrings::Property::type(), QLatin1String("com.test.qjsondbwatcher-test"));
    item.insert(QLatin1String("create-test"), 22);

    // Create an item
    QJsonDbCreateRequest request(item);
    request.setPartition(partition);
    mConnection->send(&request);
    QVERIFY(waitForResponseAndNotifications(&request, &watcher, 1));

    QList<QJsonObject> results = request.takeResults();
    QCOMPARE(results.size(), 1);
    QJsonObject info = results.at(0);
    item.insert(JsonDbStrings::Property::uuid(), info.value(JsonDbStrings::Property::uuid()));
    item.insert(JsonDbStrings::Property::version(), info.value(JsonDbStrings::Property::version()));

    QList<QJsonDbNotification> notifications = watcher.takeNotifications();
    QCOMPARE(notifications.size(), 1);

    // remove the object
    item.remove(QLatin1String("create-test"));
    QJsonDbRemoveRequest remove(item);
    remove.setPartition(partition);
    mConnection->send(&remove);
    QVERIFY(waitForResponseAndNotifications(&remove, &watcher, 1));

    notifications = watcher.takeNotifications();
    QCOMPARE(notifications.size(), 1);
    QJsonDbNotification n = notifications[0];
    QJsonObject o = n.object();

    // make sure we got notified on the right object
    QCOMPARE(o.value(JsonDbStrings::Property::uuid()), info.value(JsonDbStrings::Property::uuid()));
    QCOMPARE(o.value(QLatin1String("create-test")).toDouble(), 22.);

    // we do now expect a tombstone
    QVERIFY(o.contains(JsonDbStrings::Property::deleted()));

    mConnection->removeWatcher(&watcher);
}

void TestQJsonDbWatcher::indexValue_data()
{
    QTest::addColumn<QString>("partition");

    QTest::newRow("persistent") << "";
    QTest::newRow("ephemeral") << "Ephemeral";
}

void TestQJsonDbWatcher::indexValue()
{
    QVERIFY(mConnection);
    QFETCH(QString, partition);

    // create an index
    QJsonObject index;
    index.insert(JsonDbStrings::Property::type(), QLatin1String("Index"));
    index.insert(QLatin1String("name"), QLatin1String("create-test"));
    index.insert(QLatin1String("propertyName"), QLatin1String("create-test"));
    index.insert(QLatin1String("propertyType"), QLatin1String("string"));
    index.insert(QLatin1String("objectType"), QLatin1String("com.test.qjsondbwatcher-test"));
    QJsonDbCreateRequest create(index);
    mConnection->send(&create);
    QVERIFY(waitForResponse(&create));
    QList<QJsonObject> toDelete = create.takeResults();

    // create a watcher
    QJsonDbWatcher watcher;
    watcher.setWatchedActions(QJsonDbWatcher::All);
    watcher.setQuery(QLatin1String("[?_type=\"com.test.qjsondbwatcher-test\"][/create-test]"));
    watcher.setPartition(partition);
    mConnection->addWatcher(&watcher);
    QVERIFY(waitForStatus(&watcher, QJsonDbWatcher::Active));

    QJsonObject item;
    item.insert(JsonDbStrings::Property::type(), QLatin1String("com.test.qjsondbwatcher-test"));
    item.insert(QLatin1String("create-test"), 22);

    // Create an item
    QJsonDbCreateRequest request(item);
    request.setPartition(partition);
    mConnection->send(&request);
    QVERIFY(waitForResponseAndNotifications(&request, &watcher, 1));

    QList<QJsonObject> results = request.takeResults();
    QCOMPARE(results.size(), 1);
    QJsonObject info = results.at(0);
    item.insert(JsonDbStrings::Property::uuid(), info.value(JsonDbStrings::Property::uuid()));
    item.insert(JsonDbStrings::Property::version(), info.value(JsonDbStrings::Property::version()));

    QList<QJsonDbNotification> notifications = watcher.takeNotifications();
    QCOMPARE(notifications.size(), 1);
    QJsonDbNotification n = notifications[0];
    QJsonObject o = n.object();
    // verify _indexValue was set unless partition was Ephemeral
    if (partition != QLatin1String("Ephemeral")) {
        QVERIFY(o.contains(QLatin1String("_indexValue")));
        QCOMPARE(o.value(QLatin1String("_indexValue")), item.value(QLatin1String("create-test")));
    }

    // remove the object
    QJsonDbRemoveRequest remove(item);
    remove.setPartition(partition);
    mConnection->send(&remove);
    QVERIFY(waitForResponseAndNotifications(&remove, &watcher, 1));

    notifications = watcher.takeNotifications();
    QCOMPARE(notifications.size(), 1);
    n = notifications[0];
    o = n.object();

    // make sure we got notified on the right object
    QCOMPARE(o.value(JsonDbStrings::Property::uuid()), info.value(JsonDbStrings::Property::uuid()));
    QCOMPARE(o.value(QLatin1String("create-test")).toDouble(), 22.);

    // verify the index value was set unless partition was Ephemeral
    if (partition != QLatin1String("Ephemeral")) {
        QVERIFY(o.contains(QLatin1String("_indexValue")));
        QCOMPARE(o.value(QLatin1String("_indexValue")), item.value(QLatin1String("create-test")));
    }

    // we do now expect a tombstone
    QVERIFY(o.contains(JsonDbStrings::Property::deleted()));

    mConnection->removeWatcher(&watcher);
}

void TestQJsonDbWatcher::history()
{
    QVERIFY(mConnection);

    QFile dataFile(":/partition/json/largeContactsTest.json");
    QVERIFY(dataFile.exists());
    dataFile.open(QIODevice::ReadOnly);
    QByteArray json = dataFile.readAll();
    QVERIFY(json.size());
    dataFile.close();
    QJsonDocument doc(QJsonDocument::fromJson(json));
    QVERIFY(doc.isArray());
    QJsonArray array = doc.array();
    // make a request and connect it
    quint32 firstStateNumber = 0;

    // pass the empty object list to make the constructor happy
    QList<QJsonObject> objects;
    QJsonDbCreateRequest request(objects);

    for (int i = 0; i < qMin(100, array.size()); i++) {
        QJsonObject item = array.at(i).toObject();

        item.insert(JsonDbStrings::Property::uuid(), QUuid::createUuid().toString());
        item.insert(JsonDbStrings::Property::type(), QLatin1String("com.test.qjsondbwatcher-test"));
        item.insert(QLatin1String("i"), i);

        QJsonObject otherItem(item);
        otherItem.insert(JsonDbStrings::Property::uuid(), QUuid::createUuid().toString());
        otherItem.insert(JsonDbStrings::Property::type(), QLatin1String("com.test.qjsondbwatcher-test-other"));
        item.insert(QLatin1String("i"), i);

        request.setObjects(QList<QJsonObject>() << item << otherItem);

        // Create the items
        mConnection->send(&request);
        QVERIFY(waitForResponse(&request));
        if (!firstStateNumber)
            firstStateNumber = request.stateNumber();

        objects.append(request.takeResults());
    }
    QVERIFY(firstStateNumber);

    // create a historical watcher
    QJsonDbWatcher watcher;
    watcher.setWatchedActions(QJsonDbWatcher::All);
    watcher.setQuery(QLatin1String("[?_type=\"com.test.qjsondbwatcher-test\"]"));

    // set the starting state
    watcher.setInitialStateNumber(firstStateNumber-1);
    mConnection->addWatcher(&watcher);

    // expecting one notification per com.test.qjsondbwatcher-test object
    QVERIFY(waitForResponseAndNotifications(0, &watcher, objects.size() / 2));
    QVERIFY(waitForStatus(&watcher, QJsonDbWatcher::Active));

    QList<QJsonDbNotification> notifications = watcher.takeNotifications();
    QCOMPARE(notifications.size(), objects.size() / 2);
    // we received one Create notification per object
    foreach (const QJsonDbNotification n, notifications) {
        QCOMPARE(n.action(), QJsonDbWatcher::Created);
        QVERIFY(n.stateNumber() >= firstStateNumber);
    }

    mConnection->removeWatcher(&watcher);

    {
        // create a historical watcher with an "in" query matching one test type
        QJsonDbWatcher watcherIn;
        watcherIn.setWatchedActions(QJsonDbWatcher::All);
        watcherIn.setQuery(QLatin1String("[?_type in [\"com.test.qjsondbwatcher-test\", \"com.test.qjsondbwatcher-test2\"]]"));

        // set the starting state
        watcherIn.setInitialStateNumber(firstStateNumber-1);
        mConnection->addWatcher(&watcherIn);

        // expecting one notification per com.test.qjsondbwatcher-test object
        QVERIFY(waitForResponseAndNotifications(0, &watcherIn, objects.size() / 2));
        QVERIFY(waitForStatus(&watcherIn, QJsonDbWatcher::Active));

        notifications = watcherIn.takeNotifications();
        QCOMPARE(notifications.size(), objects.size() / 2);
        // we received one Create notification per object
        foreach (const QJsonDbNotification n, notifications) {
            QCOMPARE(n.action(), QJsonDbWatcher::Created);
            QVERIFY(n.stateNumber() >= firstStateNumber);
        }

        mConnection->removeWatcher(&watcherIn);
    }

    {
        // create a historical watcher with an "in" query matching both test types
        QJsonDbWatcher watcherIn;
        watcherIn.setWatchedActions(QJsonDbWatcher::All);
        watcherIn.setQuery(QLatin1String("[?_type in [\"com.test.qjsondbwatcher-test\", \"com.test.qjsondbwatcher-test-other\"]]"));

        // set the starting state
        watcherIn.setInitialStateNumber(firstStateNumber-1);
        mConnection->addWatcher(&watcherIn);

        // expecting two notification per original object
        QVERIFY(waitForResponseAndNotifications(0, &watcherIn, objects.size()));
        QVERIFY(waitForStatus(&watcherIn, QJsonDbWatcher::Active));

        notifications = watcherIn.takeNotifications();
        QCOMPARE(notifications.size(), objects.size());
        // we received one Create notification per object
        foreach (const QJsonDbNotification n, notifications) {
            QCOMPARE(n.action(), QJsonDbWatcher::Created);
            QVERIFY(n.stateNumber() >= firstStateNumber);
        }

        mConnection->removeWatcher(&watcherIn);
    }

    {
        // create a historical watcher with an "in" query matching both test types but with constraint [?i < 50]
        QJsonDbWatcher watcherIn;
        watcherIn.setWatchedActions(QJsonDbWatcher::All);
        watcherIn.setQuery(QLatin1String("[?_type in [\"com.test.qjsondbwatcher-test\", \"com.test.qjsondbwatcher-test-other\"]][?i < 50]"));

        // set the starting state
        watcherIn.setInitialStateNumber(firstStateNumber-1);
        mConnection->addWatcher(&watcherIn);

        // expecting two notification per original object
        QVERIFY(waitForResponseAndNotifications(0, &watcherIn, objects.size() / 2));
        QVERIFY(waitForStatus(&watcherIn, QJsonDbWatcher::Active));

        notifications = watcherIn.takeNotifications();
        QCOMPARE(notifications.size(), objects.size() / 2);
        // we received one Create notification per object
        foreach (const QJsonDbNotification n, notifications) {
            QCOMPARE(n.action(), QJsonDbWatcher::Created);
            QVERIFY(n.stateNumber() >= firstStateNumber);
            QVERIFY(n.object().value(QLatin1String("i")).toDouble() < 50);
        }

        mConnection->removeWatcher(&watcherIn);
    }

    // create a new historical watcher that should retrieve all the changes
    QJsonDbWatcher watcher2;
    watcher2.setWatchedActions(QJsonDbWatcher::All);
    watcher2.setQuery(QLatin1String("[?_type=\"com.test.qjsondbwatcher-test\"]"));
    watcher2.setInitialStateNumber(0);

    mConnection->addWatcher(&watcher2);
    QVERIFY(waitForResponseAndNotifications(0, &watcher2, objects.size() / 2));

    QList<QJsonDbNotification> notifications2 = watcher2.takeNotifications();
    QCOMPARE(notifications2.size(), objects.size() / 2);
    // we received one Create notification per object
    foreach (const QJsonDbNotification n, notifications2)
        QCOMPARE(n.action(), QJsonDbWatcher::Created);

    // create another one
    quint32 stateNumberBeforeLastCreate = watcher2.lastStateNumber();

    {
        QJsonObject item;
        item.insert(JsonDbStrings::Property::type(), QLatin1String("com.test.qjsondbwatcher-test"));
        item.insert(JsonDbStrings::Property::uuid(), QUuid::createUuid().toString());
        item.insert("anotherone", QLatin1String("abc"));
        QJsonDbCreateRequest request(item);
        mConnection->send(&request);
        // wait for response from request, one create notification
        QVERIFY(waitForResponseAndNotifications(&request, &watcher2, 1));

        QList<QJsonObject> results = request.takeResults();
        QCOMPARE(results.size(), 1);
        watcher2.takeNotifications();

        QJsonObject item2;
        item2.insert(JsonDbStrings::Property::type(), QLatin1String("com.test.qjsondbwatcher-test"));
        item2.insert(QLatin1String("_uuid"), results.at(0).value(QLatin1String("_uuid")));
        item2.insert(QLatin1String("_version"), results.at(0).value(QLatin1String("_version")));
        item2.insert("anotherone", QLatin1String("def"));
        item2.insert("field42", QLatin1String("HoG"));

        QJsonDbUpdateRequest update(item2);
        mConnection->send(&update);
        // wait for response from request, one create notification
        QVERIFY(waitForResponseAndNotifications(&update, &watcher2, 1));
        QList<QJsonDbNotification> notifications = watcher2.takeNotifications();
    }
    mConnection->removeWatcher(&watcher2);

    {
        // now verify that queries are being tested for stateNumber > 0
        QJsonDbWatcher watcher22;
        watcher22.setWatchedActions(QJsonDbWatcher::All);
        watcher22.setQuery(QLatin1String("[?_type=\"com.test.qjsondbwatcher-test\"][?field42 = \"HoG\"]"));
        watcher22.setInitialStateNumber(stateNumberBeforeLastCreate+1);

        mConnection->addWatcher(&watcher22);
        QVERIFY(waitForResponseAndNotifications(0, &watcher22, 1));

        QList<QJsonDbNotification> notifications22 = watcher22.takeNotifications();
        QCOMPARE(notifications22.size(), 1);
    }

    {
        // now verify that queries are being tested for stateNumber > 0
        // and that changes are being summarized properly
        QJsonDbWatcher watcher22;
        watcher22.setWatchedActions(QJsonDbWatcher::All);
        watcher22.setQuery(QLatin1String("[?_type=\"com.test.qjsondbwatcher-test\"][?anotherone = \"def\"]"));
        watcher22.setInitialStateNumber(stateNumberBeforeLastCreate);

        mConnection->addWatcher(&watcher22);
        QVERIFY(waitForResponseAndNotifications(0, &watcher22, 1));

        QList<QJsonDbNotification> notifications22 = watcher22.takeNotifications();
        QCOMPARE(notifications22.size(), 1);
        QCOMPARE(notifications22[0].action(), QJsonDbWatcher::Created);
    }

    QJsonDbRemoveRequest remove(objects);
    mConnection->send(&remove);
    QVERIFY(waitForResponse(&remove));
}


void TestQJsonDbWatcher::currentState()
{
    QJsonDbWatcher watcher;
    watcher.setWatchedActions(QJsonDbWatcher::All);
    watcher.setQuery(QLatin1String("[?_type=\"com.test.qjsondbwatcher-test\"]"));

    // set the starting state to 0 to get the current state
    watcher.setInitialStateNumber(0);
    mConnection->addWatcher(&watcher);

    // expecting one notification per create
    QVERIFY(waitForResponseAndNotifications(0, &watcher, 1, 1));
    watcher.takeNotifications();

    // now create another object
    QJsonObject item;
    item.insert(JsonDbStrings::Property::type(), QLatin1String("com.test.qjsondbwatcher-test"));
    item.insert("create-test", 22);

    // Create an item
    QJsonDbCreateRequest request(item);
    mConnection->send(&request);
    QVERIFY(waitForResponseAndNotifications(&request, &watcher, 1));

    QList<QJsonDbNotification> notifications = watcher.takeNotifications();
    QCOMPARE(notifications.size(), 1);

    mConnection->removeWatcher(&watcher);

    QJsonDbRemoveRequest remove(request.takeResults());
    mConnection->send(&remove);
    QVERIFY(waitForResponse(&remove));
}

void TestQJsonDbWatcher::notificationTriggersView()
{
    QVERIFY(mConnection);

    QLatin1String query("[?_type=\"com.test.TestView\"]");
    QJsonArray array(readJsonFile(":/partition/json/map-array-conversion.json").array());

    QList<QJsonObject> objects;
    foreach (const QJsonValue v, array)
        objects.append(v.toObject());
    QJsonObject testObject;
    testObject.insert(JsonDbStrings::Property::type(), QLatin1String("com.test.Test"));
    objects.append(testObject);

    // create the objects
    QJsonDbCreateRequest request(objects);
    mConnection->send(&request);
    QVERIFY(waitForResponse(&request));
    QList<QJsonObject> toDelete;
    foreach (const QJsonObject result, request.takeResults())
        toDelete.prepend(result);

    {
        QJsonDbReadRequest read(query);
        mConnection->send(&read);
        QVERIFY(waitForResponse(&read));
        QList<QJsonObject> objects = read.takeResults();
        int numObjects = objects.size();
        QCOMPARE(numObjects, 1);
    }

    // create a watcher
    QJsonDbWatcher watcher;
    watcher.setWatchedActions(QJsonDbWatcher::All);
    watcher.setQuery(QLatin1String(query));
    mConnection->addWatcher(&watcher);
    QVERIFY(waitForStatus(&watcher, QJsonDbWatcher::Active));

    {
        QJsonObject item;
        item.insert(JsonDbStrings::Property::type(), QLatin1String("com.test.Test"));
        QJsonDbCreateRequest request(item);
        mConnection->send(&request);
        QVERIFY(waitForResponseAndNotifications(&request, &watcher, 1));

        QList<QJsonDbNotification> notifications = watcher.takeNotifications();
        QCOMPARE(notifications.size(), 1);
        QJsonDbNotification n = notifications[0];
        QJsonObject o = n.object();
        // make sure we got notified on the right object
        //QCOMPARE(o.value(JsonDbStrings::Property::uuid()), info.value(JsonDbStrings::Property::uuid()));
    }
    mConnection->removeWatcher(&watcher);

    foreach (const QJsonObject &object, request.takeResults())
        toDelete.prepend(object);

    QJsonDbRemoveRequest remove(toDelete);
    mConnection->send(&remove);
    QVERIFY(waitForResponse(&remove));
}

void TestQJsonDbWatcher::notificationTriggersMapReduce()
{
    QVERIFY(mConnection);

    QJsonParseError error;
    QJsonArray array(readJsonFile(":/partition/json/map-reduce.json", &error).array());
    QVERIFY(error.error == QJsonParseError::NoError);
    QList<QJsonObject> objects;
    foreach (const QJsonValue v, array)
        objects.append(v.toObject());

    // create the objects
    QJsonDbCreateRequest request(objects);
    mConnection->send(&request);
    QVERIFY(waitForResponse(&request));
    QList<QJsonObject> toDelete;
    foreach (const QJsonObject result, request.takeResults())
        toDelete.prepend(result);

    QString query = QLatin1String("[?_type=\"Phone\"]");

    {
        QJsonDbReadRequest read(query);
        mConnection->send(&read);
        QVERIFY(waitForResponse(&read));
        int numObjects = read.takeResults().size();
        QCOMPARE(numObjects, 5);
    }

    query = QLatin1String("[?_type=\"PhoneCount\"]");

    {
        QJsonDbReadRequest read(query);
        mConnection->send(&read);
        QVERIFY(waitForResponse(&read));
        int numObjects = read.takeResults().size();
        QCOMPARE(numObjects, 5);
    }

    {
        const char json[] = "{\"_type\":\"Contact\",\"displayName\":\"Will Robinson\",\"phoneNumbers\":[{\"type\":\"satellite\",\"number\":\"+614159\"}]}";
        QJsonObject object(QJsonDocument::fromJson(json).object());

        QJsonDbCreateRequest write(object);
        mConnection->send(&write);
        QVERIFY(waitForResponse(&write));
        toDelete.append(write.takeResults());
    }

    // create a watcher
    QJsonDbWatcher watcher;
    watcher.setWatchedActions(QJsonDbWatcher::All);
    watcher.setQuery(query);
    mConnection->addWatcher(&watcher);

    QVERIFY(waitForResponseAndNotifications(0, &watcher, 1));
    int numNotifications = watcher.takeNotifications().size();
    QCOMPARE(numNotifications, 1);

    {
        QJsonDbReadRequest read(query);
        mConnection->send(&read);
        QVERIFY(waitForResponse(&read));

        QList<QJsonObject> results = read.takeResults();
        QCOMPARE(results.size(), 6);
    }

    // now write another one
    {
        const char json[] = "{\"_type\":\"Contact\",\"displayName\":\"Jeffrey Goines\",\"phoneNumbers\":[{\"type\":\"satellite\",\"number\":\"+2718281828\"}]}";
        QJsonObject object(QJsonDocument::fromJson(json).object());

        QJsonDbCreateRequest write(object);
        mConnection->send(&write);
        QVERIFY(waitForResponseAndNotifications(&write, &watcher, 1));
        toDelete.append(write.takeResults());

        int numNotifications = watcher.takeNotifications().size();
        QCOMPARE(numNotifications, 1);
    }

    QJsonDbRemoveRequest remove(toDelete);
    mConnection->send(&remove);
    QVERIFY(waitForResponse(&remove));
}

void TestQJsonDbWatcher::notificationTriggersMultiViews()
{
    QVERIFY(mConnection);

    QLatin1String query1("[?_type=\"MultiMapView1\"]");
    QLatin1String query2("[?_type=\"MultiMapView2\"]");
    QJsonArray array(readJsonFile(":/partition/json/multi-map.json").array());

    QList<QJsonObject> objects;
    foreach (const QJsonValue v, array)
        objects.append(v.toObject());

    // create the objects
    QJsonDbCreateRequest request(objects);
    mConnection->send(&request);
    QVERIFY(waitForResponse(&request));
    QList<QJsonObject> toDelete;
    foreach (const QJsonObject result, request.takeResults())
        toDelete.prepend(result);

    {
        QJsonDbReadRequest read(query1);
        mConnection->send(&read);
        QVERIFY(waitForResponse(&read));
        QList<QJsonObject> objects = read.takeResults();
        int numObjects = objects.size();
        QCOMPARE(numObjects, 0);
    }

    // create two watchers
    QJsonDbWatcher watcher1;
    watcher1.setWatchedActions(QJsonDbWatcher::All);
    watcher1.setQuery(QLatin1String(query1));
    mConnection->addWatcher(&watcher1);
    QVERIFY(waitForStatus(&watcher1, QJsonDbWatcher::Active));

    QJsonDbWatcher watcher2;
    watcher2.setWatchedActions(QJsonDbWatcher::All);
    watcher2.setQuery(QLatin1String(query2));
    mConnection->addWatcher(&watcher2);
    QVERIFY(waitForStatus(&watcher2, QJsonDbWatcher::Active));

    {
        QJsonObject item;
        item.insert(JsonDbStrings::Property::type(), QLatin1String("MultiMapSourceType"));
        QJsonDbCreateRequest request(item);
        mConnection->send(&request);
        QVERIFY(waitForResponseAndNotifications(&request, &watcher1, 1));

        QList<QJsonDbNotification> notifications = watcher1.takeNotifications();
        QCOMPARE(notifications.size(), 1);
        QJsonDbNotification n = notifications[0];
        QJsonObject o = n.object();
        // make sure we got notified on the right object
        //QCOMPARE(o.value(JsonDbStrings::Property::uuid()), info.value(JsonDbStrings::Property::uuid()));

        // make sure watcher2 got one also
        notifications = watcher2.takeNotifications();
        QCOMPARE(notifications.size(), 1);

    }
    mConnection->removeWatcher(&watcher1);
    mConnection->removeWatcher(&watcher2);

    foreach (const QJsonObject &object, request.takeResults())
        toDelete.prepend(object);

    QJsonDbRemoveRequest remove(toDelete);
    mConnection->send(&remove);
    QVERIFY(waitForResponse(&remove));
}

void TestQJsonDbWatcher::typeChangeEagerViewSource()
{
    QVERIFY(mConnection);

    QJsonParseError error;
    QJsonArray array(readJsonFile(":/partition/json/map-reduce.json", &error).array());
    QVERIFY(error.error == QJsonParseError::NoError);
    QList<QJsonObject> objects;
    foreach (const QJsonValue v, array)
        objects.append(v.toObject());

    // create the objects
    QJsonDbCreateRequest request(objects);
    mConnection->send(&request);
    QVERIFY(waitForResponse(&request));
    QList<QJsonObject> toDelete;
    foreach (const QJsonObject result, request.takeResults())
        toDelete.prepend(result);

    QString query = QLatin1String("[?_type=\"PhoneCount\"]");

    // verify that we get what we expect
    {
        QJsonDbReadRequest read(query);
        mConnection->send(&read);
        QVERIFY(waitForResponse(&read));
        int numObjects = read.takeResults().size();
        QCOMPARE(numObjects, 5);
    }

    // create an object that's not of the source type of the view
    const char json[] = "{\"_type\":\"not.a.Contact\",\"displayName\":\"Will Robinson\",\"phoneNumbers\":[{\"type\":\"satellite\",\"number\":\"+614159\"}]}";
    QJsonDbObject object(QJsonDocument::fromJson(json).object());
    object.setUuid(QJsonDbObject::createUuidFromString("typeChangeEagerViewSource"));

    QJsonDbWriteRequest write;
    write.setObjects(QList<QJsonObject>() << object);
    mConnection->send(&write);
    QVERIFY(waitForResponse(&write));
    object.insert(QStringLiteral("_version"), write.takeResults().at(0).value(QStringLiteral("_version")).toString());

    // verify that the view didn't change
    {
        QJsonDbReadRequest read(query);
        mConnection->send(&read);
        QVERIFY(waitForResponse(&read));
        int numObjects = read.takeResults().size();
        QCOMPARE(numObjects, 5);
    }

    // create a watcher
    QJsonDbWatcher watcher;
    watcher.setWatchedActions(QJsonDbWatcher::All);
    watcher.setQuery(query);
    mConnection->addWatcher(&watcher);
    QVERIFY(waitForStatus(&watcher, QJsonDbWatcher::Active));

    // change the object so that it's now a source type of the view
    object.insert(QLatin1String("_type"), QLatin1String("Contact"));
    write.setObjects(QList<QJsonObject>() << object);
    mConnection->send(&write);
    QVERIFY(waitForResponseAndNotifications(&write, &watcher, 1));
    object.insert(QStringLiteral("_version"), write.takeResults().at(0).value(QStringLiteral("_version")).toString());
    QList<QJsonDbNotification> notifications = watcher.takeNotifications();
    QCOMPARE(notifications.count(), 1);
    QCOMPARE(notifications[0].action(), QJsonDbWatcher::Created);

    // change it back, which should result in a remove notification
    object.insert(QLatin1String("_type"), QLatin1String("not.a.Contact"));
    write.setObjects(QList<QJsonObject>() << object);
    mConnection->send(&write);
    QVERIFY(waitForResponseAndNotifications(&write, &watcher, 1));
    notifications = watcher.takeNotifications();
    QCOMPARE(notifications.count(), 1);
    QCOMPARE(notifications[0].action(), QJsonDbWatcher::Removed);

    mConnection->removeWatcher(&watcher);
    QJsonDbRemoveRequest remove(toDelete);
    mConnection->send(&remove);
    QVERIFY(waitForResponse(&remove));
}

void TestQJsonDbWatcher::invalid()
{
    // missing query
    QJsonDbWatcher watcher;
    watcher.setQuery(QStringLiteral(""));
    mConnection->addWatcher(&watcher);
    QVERIFY(waitForError(&watcher, QJsonDbWatcher::MissingQuery));
    mConnection->removeWatcher(&watcher);

    // garbage query
    QJsonDbWatcher watcher2;
    watcher2.setQuery(QStringLiteral("not a valid query"));
    mConnection->addWatcher(&watcher2);
    QVERIFY(waitForError(&watcher2, QJsonDbWatcher::MissingQuery));
    mConnection->removeWatcher(&watcher2);

    // non-existent partition
    QJsonDbWatcher watcher3;
    watcher3.setQuery(QStringLiteral("[?_type=\"Contact\"]"));
    watcher3.setPartition(QStringLiteral("this.partition.does.not.exist"));
    mConnection->addWatcher(&watcher3);
    QVERIFY(waitForError(&watcher3, QJsonDbWatcher::InvalidPartition));
    mConnection->removeWatcher(&watcher3);

    // invalid initial state number
    QJsonDbWatcher watcher4;
    watcher4.setQuery(QStringLiteral("[?_type=\"Contact\"]"));
    watcher4.setInitialStateNumber(1234567);
    mConnection->addWatcher(&watcher4);
    QVERIFY(waitForError(&watcher4, QJsonDbWatcher::InvalidStateNumber));
    mConnection->removeWatcher(&watcher4);

    // unavailable partition
    QJsonDbWatcher watcher5;
    watcher5.setQuery(QStringLiteral("[?_type=\"Contact\"]"));
    watcher5.setPartition(QStringLiteral("com.qt-project.removable"));
    mConnection->addWatcher(&watcher5);
    QVERIFY(waitForError(&watcher5, QJsonDbWatcher::InvalidPartition));
    mConnection->removeWatcher(&watcher5);

    // watcher over multiple object tables
    {
        static const char schemaJson[] =
"{ \
    \"_type\": \"_schemaType\", \
    \"name\": \"invalidMapType\", \
    \"schema\": { \
        \"type\": \"object\", \
        \"extends\": {\"$ref\": \"View\"} \
     } \
}";
        QJsonDbObject schema = QJsonDocument::fromJson(QByteArray(schemaJson)).object();

        QJsonDbObject map;
        map.insert(QLatin1String("_type"), QLatin1String("Map"));
        map.insert(QLatin1String("targetType"), QLatin1String("invalidMapType"));
        QJsonObject submap;
        submap.insert(QLatin1String("com.test.indextest"), QLatin1String("function (o) { jsondb.emit(o); }"));
        map.insert(QLatin1String("join"), submap);

        QJsonDbCreateRequest request(QList<QJsonObject>() << schema << map);
        QVERIFY(mConnection->send(&request));
        QVERIFY(waitForResponse(&request));
        QJsonDbWatcher watcher6;
        watcher6.setQuery(QStringLiteral("[?_type=\"Contact\" | _type = \"invalidMapType\"]"));
        mConnection->addWatcher(&watcher6);
        QVERIFY(waitForError(&watcher6, QJsonDbWatcher::MissingQuery));
        mConnection->removeWatcher(&watcher6);
    }

    // watcher over multiple types in the same object table
    QJsonDbWatcher watcher7;
    watcher7.setQuery(QStringLiteral("[?_type in [\"Contact\", \"AnotherContact\"]]"));
    mConnection->addWatcher(&watcher7);
    QVERIFY(waitForStatus(&watcher7, QJsonDbWatcher::Active));
    mConnection->removeWatcher(&watcher7);
}

void TestQJsonDbWatcher::privatePartition()
{
    // watchers are not supported on private partitions, so it should fail
    QJsonDbWatcher privateWatcher;
    privateWatcher.setQuery("[?_type=\"foo\"]");
    privateWatcher.setPartition(QString::fromLatin1("%1.Private").arg(QJsonDbStandardPaths::currentUser()));
    QVERIFY(!mConnection->addWatcher(&privateWatcher));
}

void TestQJsonDbWatcher::addAndRemove()
{
    QJsonDbWatcher watcher;
    watcher.setQuery("[?_type=\"Foo\"]");
    QVERIFY(mConnection->addWatcher(&watcher));
    QVERIFY(mConnection->removeWatcher(&watcher));

    // a dummy request so that we can safely wait for the (internal) watcher
    // requests to be processed.
    QJsonDbReadRequest read(QStringLiteral("[?_type=\"Foo\"]"));
    QVERIFY(mConnection->send(&read));
    QVERIFY(waitForResponse(&read));
}

void TestQJsonDbWatcher::removeWatcherStatus()
{
    {
        QJsonDbWatcher watcher;
        watcher.setQuery("[?_type=\"Foo\"]");
        QVERIFY(mConnection->addWatcher(&watcher));
        QVERIFY(waitForStatus(&watcher, QJsonDbWatcher::Active));
        QVERIFY(mConnection->removeWatcher(&watcher));
        QVERIFY(waitForStatus(&watcher, QJsonDbWatcher::Inactive));
    }

    {
        QJsonDbWatcher watcher;
        watcher.setQuery("[?_type=\"Foo\"]");
        QVERIFY(mConnection->addWatcher(&watcher));
        QVERIFY(mConnection->removeWatcher(&watcher));
        QVERIFY(waitForStatus(&watcher, QJsonDbWatcher::Inactive));
    }
}

void TestQJsonDbWatcher::uuidQuery()
{
    QUuid uuid = QUuid::createUuid();
    QJsonDbWatcher watcher;
    watcher.setQuery(QStringLiteral("[?_uuid=%uuid]"));
    watcher.bindValue(QStringLiteral("uuid"), uuid.toString());
    QVERIFY(mConnection->addWatcher(&watcher));
    QVERIFY(waitForStatus(&watcher, QJsonDbWatcher::Active));

    // create the object
    QJsonObject object;
    object.insert(QLatin1String("_uuid"), uuid.toString());
    object.insert(QLatin1String("_type"), QLatin1String("uuidQuery"));
    QJsonDbWriteRequest request;
    request.setObject(object);
    QVERIFY(mConnection->send(&request));
    QVERIFY(waitForResponseAndNotifications(&request, &watcher, 1));
    QList<QJsonObject> results = request.takeResults();
    QCOMPARE(results.size(), 1);
    object.insert(QLatin1String("_version"), results.at(0).value(QLatin1String("_version")));

    // update the object
    object.insert(QLatin1String("foo"), QLatin1String("bar"));
    request.setObject(object);
    QVERIFY(mConnection->send(&request));
    QVERIFY(waitForResponseAndNotifications(&request, &watcher, 1));

    QVERIFY(mConnection->removeWatcher(&watcher));
    QVERIFY(waitForStatus(&watcher, QJsonDbWatcher::Inactive));
}

QTEST_MAIN(TestQJsonDbWatcher)

#include "testqjsondbwatcher.moc"