aboutsummaryrefslogtreecommitdiffstats
path: root/documentapi/src/tests/messages/testbase.h
blob: a2cd5ee5649c8b3a67f03996135d583c6570c492 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <vespa/documentapi/messagebus/documentprotocol.h>
#include <vespa/messagebus/routable.h>
#include <vespa/vespalib/component/version.h>
#include <vespa/vespalib/testkit/testapp.h>
#include <map>

using namespace documentapi;

/**
 * Declare the signature of the test method.
 */
class TestBase;
typedef bool (TestBase::*TEST_METHOD_PT)();
#define TEST_METHOD(pt) ((TEST_METHOD_PT)&pt)

/**
 * This is the test base itself. It offers a set of utility functions that reflect on the version returned by
 * the pure virtual getVersion() function. You need to inherit this and assign a version and a set of message
 * tests to it.
 */
class TestBase : public vespalib::TestApp {
    std::shared_ptr<const document::DocumentTypeRepo> _repo;
protected:
    const string                       _dataPath;
    DocumentProtocol                   _protocol;
    std::map<uint32_t, TEST_METHOD_PT> _tests;

    // Declares what languages share serialization.
    enum {
        LANG_CPP = 0,
        LANG_JAVA,
        NUM_LANGUAGES
    };

    TestBase();
    virtual ~TestBase() { /* empty */ }
    virtual const vespalib::Version getVersion() const = 0;
    virtual bool shouldTestCoverage() const = 0;
    TestBase &putTest(uint32_t type, TEST_METHOD_PT test);
    int Main() override;

public:
    using Tamper = std::function<mbus::Blob(mbus::Blob)>;
    static mbus::Blob truncate(mbus::Blob data, size_t bytes);
    static mbus::Blob pad(mbus::Blob data, size_t bytes);

    const document::DocumentTypeRepo &getTypeRepo() { return *_repo; }
    std::shared_ptr<const document::DocumentTypeRepo> &getTypeRepoSp() { return _repo; }

    bool testCoverage(const std::vector<uint32_t> &expected, const std::vector<uint32_t> &actual, bool report = false) const;
    bool writeFile(const string &filename, const mbus::Blob& blob) const;
    mbus::Blob readFile(const string &filename) const;
    uint32_t serialize(const string &filename, const mbus::Routable &routable, Tamper tamper);
    uint32_t serialize(const string &filename, const mbus::Routable &routable) {
        return serialize(filename, routable, [](auto x)noexcept{ return x; });
    }
    mbus::Routable::UP deserialize(const string &filename, uint32_t classId, uint32_t lang);
    void dump(const mbus::Blob &blob) const;

    string getPath(const string &filename) const { return _dataPath + "/" + filename; }
    mbus::Blob encode(const mbus::Routable &obj) const { return _protocol.encode(getVersion(), obj); }
    mbus::Routable::UP decode(mbus::BlobRef data) const { return _protocol.decode(getVersion(), data); }
private:
    bool file_content_is_unchanged(const string& filename, const mbus::Blob& data_to_write) const;
};