2 * collectd - src/tests/utils_parse_json_test.c
3 * Copyright (C) 2018 Florian Forster
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * Florian Forster <octo at collectd.org>
29 #include "utils_parse_json.h"
32 DEF_TEST(single_value) /* {{{ */
37 "{\"host\":\"example.com\",\"plugin\":\"test\",\"type\":\"answer\","
38 "\"time\":1434357493.398,\"interval\":10.000,"
39 "\"dstypes\":[\"gauge\"],\"values\":[42.0]},"
41 "{\"host\":\"example.com\",\"plugin\":\"test\",\"type\":\"gauge\","
42 "\"time\":1434357493.398,\"interval\":10.000,"
43 "\"dstypes\":[\"gauge\"],\"values\":[null]},"
45 "{\"host\":\"example.com\",\"plugin\":\"test\",\"type\":\"total_things\","
46 "\"plugin_instance\":\"foo\",\"type_instance\":\"bar\","
47 "\"time\":1434357493.398,\"interval\":10.000,"
48 "\"dstypes\":[\"derive\"],\"values\":[31337]}"
51 value_list_t **vls = NULL;
54 CHECK_ZERO(parse_json(json, &vls, &vls_num));
59 value_list_t *vl = vls[0];
60 EXPECT_EQ_STR("example.com", vl->host);
61 EXPECT_EQ_STR("test", vl->plugin);
62 EXPECT_EQ_STR("", vl->plugin_instance);
63 EXPECT_EQ_STR("answer", vl->type);
64 EXPECT_EQ_STR("", vl->type_instance);
65 OK(vl->time == 1540129631229236480);
66 OK(vl->interval == 10737418240);
67 OK(vl->values_len == 1);
68 OK(vl->values[0].gauge == 42.0);
71 OK(vl->values_len == 1);
72 OK(isnan(vl->values[0].gauge));
75 EXPECT_EQ_STR("example.com", vl->host);
76 EXPECT_EQ_STR("test", vl->plugin);
77 EXPECT_EQ_STR("foo", vl->plugin_instance);
78 EXPECT_EQ_STR("total_things", vl->type);
79 EXPECT_EQ_STR("bar", vl->type_instance);
80 OK(vl->time == 1540129631229236480);
81 OK(vl->interval == 10737418240);
82 OK(vl->values_len == 1);
83 OK(vl->values[0].derive == 31337);
91 } /* }}} single_value */
93 DEF_TEST(multi_value) /* {{{ */
97 "{\"host\":\"example.com\",\"plugin\":\"load\",\"type\":\"load\","
98 "\"time\":1434357493,\"interval\":10,"
99 "\"dstypes\":[\"gauge\",\"gauge\",\"gauge\"],\"values\":[1.12,0.56,0.64]}"
102 value_list_t **vls = NULL;
105 CHECK_ZERO(parse_json(json, &vls, &vls_num));
108 value_list_t *vl = vls[0];
109 EXPECT_EQ_STR("example.com", vl->host);
110 EXPECT_EQ_STR("load", vl->plugin);
111 EXPECT_EQ_STR("load", vl->type);
112 OK(vl->time == 1540129630801887232);
113 OK(vl->interval == 10737418240);
115 OK(vl->values_len == 3);
116 OK(vl->values[0].gauge == 1.12);
117 OK(vl->values[1].gauge == 0.56);
118 OK(vl->values[2].gauge == 0.64);
124 } /* }}} multi_value */
126 DEF_TEST(failures) /* {{{ */
131 "{\"plugin\":\"test\",\"type\":\"answer\","
132 "\"time\":1434357493.398,\"interval\":10.000,"
133 "\"dstypes\":[\"gauge\"],\"values\":[42.0]},"
135 "{\"host\":\"example.com\",\"type\":\"answer\","
136 "\"time\":1434357493.398,\"interval\":10.000,"
137 "\"dstypes\":[\"gauge\"],\"values\":[42.0]},"
139 "{\"host\":\"example.com\",\"plugin\":\"test\","
140 "\"time\":1434357493.398,\"interval\":10.000,"
141 "\"dstypes\":[\"gauge\"],\"values\":[42.0]},"
143 "{\"host\":\"example.com\",\"plugin\":\"test\",\"type\":\"answer\","
144 "\"interval\":10.000,"
145 "\"dstypes\":[\"gauge\"],\"values\":[42.0]},"
146 /* interval missing */
147 "{\"host\":\"example.com\",\"plugin\":\"test\",\"type\":\"answer\","
148 "\"time\":1434357493.398,"
149 "\"dstypes\":[\"gauge\"],\"values\":[42.0]},"
150 /* derive -> floating point mismatch */
151 "{\"host\":\"example.com\",\"plugin\":\"test\",\"type\":\"answer\","
152 "\"time\":1434357493.398,\"interval\":10.000,"
153 "\"dstypes\":[\"derive\"],\"values\":[42.0]},"
154 /* len(dstypes) != len(values) */
155 "{\"host\":\"example.com\",\"plugin\":\"test\",\"type\":\"answer\","
156 "\"time\":1434357493.398,\"interval\":10.000,"
157 "\"dstypes\":[\"gauge\"],\"values\":[42.0, 23.0]},"
158 /* type mismatch: got boolean, want string */
159 "{\"host\":true,\"plugin\":\"test\",\"type\":\"answer\","
160 "\"time\":1434357493.398,\"interval\":10.000,"
161 "\"dstypes\":[\"gauge\"],\"values\":[42.0]},"
162 /* type mismatch: got boolean, want number */
163 "{\"host\":\"example.com\",\"plugin\":\"test\",\"type\":\"answer\","
164 "\"time\":true,\"interval\":10.000,"
165 "\"dstypes\":[\"gauge\"],\"values\":[42.0]},"
166 /* type mismatch: got string/number, want array/array */
167 "{\"host\":\"example.com\",\"plugin\":\"test\",\"type\":\"answer\","
168 "\"time\":1434357493.398,\"interval\":10.000,"
169 "\"dstypes\":\"gauge\",\"values\":42.0}"
172 value_list_t **vls = NULL;
175 CHECK_ZERO(parse_json(json, &vls, &vls_num));
181 int main(int argc, char **argv) /* {{{ */
183 RUN_TEST(single_value);
184 RUN_TEST(multi_value);
190 /* vim: set sw=2 sts=2 et fdm=marker : */