aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/tests/struct_anno/struct_anno_test.cpp
blob: 57b643a1d27e224e3dbfcbc15bdea91f21308710 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/document/base/testdocrepo.h>
#include <vespa/document/annotation/alternatespanlist.h>
#include <vespa/document/annotation/annotation.h>
#include <vespa/document/annotation/spantree.h>
#include <vespa/document/fieldvalue/document.h>
#include <vespa/document/fieldvalue/stringfieldvalue.h>
#include <vespa/document/serialization/annotationdeserializer.h>
#include <vespa/document/serialization/annotationserializer.h>
#include <vespa/document/serialization/vespadocumentdeserializer.h>
#include <vespa/document/serialization/vespadocumentserializer.h>
#include <vespa/document/repo/documenttyperepo.h>
#include <vespa/vespalib/objects/nbostream.h>
#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/fastos/file.h>

using std::ostringstream;
using std::string;
using std::vector;
using vespalib::nbostream;
using namespace document;

namespace {

class Test : public vespalib::TestApp {
    void requireThatStructFieldsCanContainAnnotations();

public:
    int Main() override;
};

int Test::Main() {
    if (getenv("TEST_SUBSET") != 0) { return 0; }
    TEST_INIT("struct_anno_test");
    TEST_DO(requireThatStructFieldsCanContainAnnotations());
    TEST_DONE();
}

template <typename T, int N> int arraysize(const T (&)[N]) { return N; }

void Test::requireThatStructFieldsCanContainAnnotations() {
    DocumentTypeRepo repo(readDocumenttypesConfig(TEST_PATH("documenttypes.cfg")));

    FastOS_File file(TEST_PATH("document.dat").c_str());
    ASSERT_TRUE(file.OpenReadOnlyExisting());
    char buffer[1024];
    ssize_t size = file.Read(buffer, arraysize(buffer));
    ASSERT_TRUE(size != -1);

    nbostream stream(buffer, size);
    VespaDocumentDeserializer deserializer(repo, stream, 8);
    Document doc;
    deserializer.read(doc);

    FieldValue::UP urlRef = doc.getValue("my_url");
    ASSERT_TRUE(urlRef.get() != NULL);
    const StructFieldValue *url = dynamic_cast<const StructFieldValue*>(urlRef.get());
    ASSERT_TRUE(url != NULL);

    FieldValue::UP strRef = url->getValue("scheme");
    const StringFieldValue *str = dynamic_cast<const StringFieldValue*>(strRef.get());
    ASSERT_TRUE(str != NULL);

    SpanTree::UP tree = std::move(str->getSpanTrees().front());

    EXPECT_EQUAL("my_tree", tree->getName());
    const SimpleSpanList *root = dynamic_cast<const SimpleSpanList*>(&tree->getRoot());
    ASSERT_TRUE(root != NULL);
    EXPECT_EQUAL(1u, root->size());
    SimpleSpanList::const_iterator it = root->begin();
    EXPECT_EQUAL(Span(0, 6), (*it++));
    EXPECT_TRUE(it == root->end());

    EXPECT_EQUAL(1u, tree->numAnnotations());
}

}  // namespace

TEST_APPHOOK(Test);