summaryrefslogtreecommitdiffstats
path: root/documentapi/src/tests/messages/messages52test.cpp
blob: 1da48d4aa418e1c292de35e8dfa1acdb071928eb (plain) (blame)
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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// @author Vegard Sjonfjell


#include "messages52test.h"
#include <vespa/documentapi/documentapi.h>
#include <vespa/document/repo/documenttyperepo.h>
#include <vespa/document/update/documentupdate.h>
#include <vespa/document/update/fieldpathupdates.h>
#include <vespa/document/datatype/documenttype.h>

using document::DocumentTypeRepo;

namespace {

document::Document::SP
createDoc(const DocumentTypeRepo &repo, const string &type_name, const string &id)
{
    return document::Document::SP(new document::Document(
                    *repo.getDocumentType(type_name),
                    document::DocumentId(id)));
}

}

static constexpr int MESSAGE_BASE_LENGTH = 5;

Messages52Test::Messages52Test()
{
    // This list MUST mirror the list of routable factories from the DocumentProtocol constructor that support
    // version 5.2. When adding tests to this list, please KEEP THEM ORDERED alphabetically like they are now.

    putTest(DocumentProtocol::MESSAGE_PUTDOCUMENT, TEST_METHOD(Messages52Test::testPutDocumentMessage));
    putTest(DocumentProtocol::MESSAGE_REMOVEDOCUMENT, TEST_METHOD(Messages52Test::testRemoveDocumentMessage));
    putTest(DocumentProtocol::MESSAGE_UPDATEDOCUMENT, TEST_METHOD(Messages52Test::testUpdateDocumentMessage));
}

bool
Messages52Test::testPutDocumentMessage()
{
    auto doc = createDoc(getTypeRepo(), "testdoc", "doc:scheme:");
    PutDocumentMessage msg(doc);

    msg.setTimestamp(666);
    msg.setCondition(TestAndSetCondition("There's just one condition"));

    EXPECT_EQUAL(MESSAGE_BASE_LENGTH +
                 41u +
                 serializedLength(msg.getCondition().getSelection()),
                 serialize("PutDocumentMessage", msg));

    for (uint32_t lang = 0; lang < NUM_LANGUAGES; ++lang) {
        auto routableUp = deserialize("PutDocumentMessage", DocumentProtocol::MESSAGE_PUTDOCUMENT, lang);
        if (EXPECT_TRUE(routableUp.get() != nullptr)) {
            auto & deserializedMsg = static_cast<PutDocumentMessage &>(*routableUp);

            EXPECT_EQUAL(msg.getDocument().getType().getName(), deserializedMsg.getDocument().getType().getName());
            EXPECT_EQUAL(msg.getDocument().getId().toString(), deserializedMsg.getDocument().getId().toString());
            EXPECT_EQUAL(msg.getTimestamp(), deserializedMsg.getTimestamp());
            EXPECT_EQUAL(67u, deserializedMsg.getApproxSize());
            EXPECT_EQUAL(msg.getCondition().getSelection(), deserializedMsg.getCondition().getSelection());
        }
    }

    return true;
}

bool
Messages52Test::testRemoveDocumentMessage()
{
    RemoveDocumentMessage msg(document::DocumentId("doc:scheme:"));

    msg.setCondition(TestAndSetCondition("There's just one condition"));

    EXPECT_EQUAL(MESSAGE_BASE_LENGTH + size_t(16) + serializedLength(msg.getCondition().getSelection()), serialize("RemoveDocumentMessage", msg));

    for (uint32_t lang = 0; lang < NUM_LANGUAGES; ++lang) {
        auto routablePtr = deserialize("RemoveDocumentMessage", DocumentProtocol::MESSAGE_REMOVEDOCUMENT, lang);

        if (EXPECT_TRUE(routablePtr.get() != nullptr)) {
            auto & ref = static_cast<RemoveDocumentMessage &>(*routablePtr);
            EXPECT_EQUAL(string("doc:scheme:"), ref.getDocumentId().toString());
            EXPECT_EQUAL(msg.getCondition().getSelection(), ref.getCondition().getSelection());
        }
    }
    return true;
}

bool
Messages52Test::testUpdateDocumentMessage()
{
    const DocumentTypeRepo & repo = getTypeRepo();
    const document::DocumentType & docType = *repo.getDocumentType("testdoc");

    auto docUpdate = std::make_shared<document::DocumentUpdate>(repo, docType, document::DocumentId("doc:scheme:"));

    docUpdate->addFieldPathUpdate(document::FieldPathUpdate::CP(
        new document::RemoveFieldPathUpdate("intfield", "testdoc.intfield > 0")));

    UpdateDocumentMessage msg(docUpdate);
    msg.setOldTimestamp(666u);
    msg.setNewTimestamp(777u);
    msg.setCondition(TestAndSetCondition("There's just one condition"));

    EXPECT_EQUAL(MESSAGE_BASE_LENGTH + 89u + serializedLength(msg.getCondition().getSelection()), serialize("UpdateDocumentMessage", msg));

    for (uint32_t lang = 0; lang < NUM_LANGUAGES; ++lang) {
        auto routableUp = deserialize("UpdateDocumentMessage", DocumentProtocol::MESSAGE_UPDATEDOCUMENT, lang);

        if (EXPECT_TRUE(routableUp.get() != nullptr)) {
            auto & deserializedMsg = static_cast<UpdateDocumentMessage &>(*routableUp);
            EXPECT_EQUAL(msg.getDocumentUpdate(), deserializedMsg.getDocumentUpdate());
            EXPECT_EQUAL(msg.getOldTimestamp(), deserializedMsg.getOldTimestamp());
            EXPECT_EQUAL(msg.getNewTimestamp(), deserializedMsg.getNewTimestamp());
            EXPECT_EQUAL(115u, deserializedMsg.getApproxSize());
            EXPECT_EQUAL(msg.getCondition().getSelection(), deserializedMsg.getCondition().getSelection());
        }
    }
    return true;
}