summaryrefslogtreecommitdiffstats
path: root/src/lib/cc/tests/user_context_unittests.cc
blob: e88b421fec845dfa6f6d86eeeed2c2c69dcaa131 (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
// Copyright (C) 2018-2020 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>
#include <cc/user_context.h>
#include <gtest/gtest.h>

using namespace isc::data;

namespace {

ElementPtr gen() {
    std::string content = "{ \"foo\": 1, \"bar\": \"xyz\" }";
    return (Element::fromJSON(content));
}

TEST(UserContext, setget) {
    UserContext parent;
    EXPECT_FALSE(parent.getContext());
    ConstElementPtr map = gen();
    parent.setContext(map);
    ConstElementPtr ctx = parent.getContext();
    EXPECT_EQ(*ctx, *map);
}

TEST(UserContext, null) {
    UserContext parent;
    ElementPtr map = gen();
    parent.contextToElement(map);
    ElementPtr expected = gen();
    EXPECT_EQ(*expected, *map);
}

TEST(UserContext, notMap) {
    UserContext parent;
    ConstElementPtr ctx = Element::create("foo");
    parent.setContext(ctx);
    ElementPtr map = gen();
    parent.contextToElement(map);
    ElementPtr expected = gen();
    expected->set("user-context", ctx);
    EXPECT_EQ(*expected, *map);
}

TEST(UserContext, empty) {
    UserContext parent;
    ConstElementPtr ctx = Element::createMap();
    parent.setContext(ctx);
    ElementPtr map = gen();
    parent.contextToElement(map);
    ElementPtr expected = gen();
    expected->set("user-context", ctx);
    EXPECT_EQ(*expected, *map);
}

TEST(UserContext, noComment) {
    UserContext parent;
    ConstElementPtr ctx = Element::fromJSON("{ \"version\": 1 }");
    parent.setContext(ctx);
    ElementPtr map = gen();
    parent.contextToElement(map);
    ElementPtr expected = gen();
    expected->set("user-context", ctx);
    EXPECT_EQ(*expected, *map);
};

TEST(UserContext, onlyComment) {
    UserContext parent;
    ConstElementPtr ctx = Element::fromJSON("{ \"comment\": \"foobar\" }");
    parent.setContext(ctx);
    ElementPtr map = gen();
    parent.contextToElement(map);
    ElementPtr expected = gen();
    expected->set("user-context", ctx);
    EXPECT_EQ(*expected, *map);
}

TEST(UserContext, both) {
    UserContext parent;
    ConstElementPtr ctx =
        Element::fromJSON("{ \"comment\": \"foobar\", \"version\": 1 }");
    parent.setContext(ctx);
    ElementPtr map = gen();
    parent.contextToElement(map);
    ElementPtr expected = gen();
    expected->set("user-context", ctx);
    EXPECT_EQ(*expected, *map);
}

TEST(toElement, notMap) {
    ConstElementPtr arg = Element::create("foo");
    ConstElementPtr result = UserContext::toElement(arg);
    EXPECT_EQ(*result, *arg);
}

TEST(toElement, empty) {
    ElementPtr map = gen();
    ConstElementPtr ctx = Element::createMap();
    map->set("user-context", ctx);
    ConstElementPtr result = UserContext::toElement(map);
    EXPECT_EQ(*result, *map);
}

TEST(toElement, noComment) {
    ElementPtr map = gen();
    ConstElementPtr ctx = Element::fromJSON("{ \"version\": 1 }");
    map->set("user-context", ctx);
    ConstElementPtr result = UserContext::toElement(map);
    EXPECT_EQ(*result, *map);
}

TEST(toElement, onlyComment) {
    ElementPtr map = gen();
    ConstElementPtr ctx = Element::fromJSON("{ \"comment\": \"foobar\" }");
    map->set("user-context", ctx);
    ConstElementPtr result = UserContext::toElement(map);
    EXPECT_EQ(*result, *map);
}

TEST(toElement, both) {
    ElementPtr map = gen();
    ConstElementPtr ctx =
        Element::fromJSON("{ \"comment\": \"foobar\", \"version\": 1 }");
    map->set("user-context", ctx);
    ConstElementPtr result = UserContext::toElement(map);
    EXPECT_EQ(*result, *map);
}

}