2 * collectd - src/curl_json.c
3 * Copyright (C) 2017 Florian octo Forster
5 * Licensed under the same terms and conditions as src/curl_json.c.
8 * Florian octo Forster <octo at collectd.org>
11 #include "curl_json.c"
15 static void test_submit(cj_t *db, cj_key_t *key, value_t *value) {
16 /* hack: we repurpose db->curl to store received values. */
17 c_avl_tree_t *values = (void *)db->curl;
19 value_t *value_copy = calloc(1, sizeof(*value_copy));
20 memmove(value_copy, value, sizeof(*value_copy));
22 assert(c_avl_insert(values, key->path, value_copy) == 0);
25 static derive_t test_metric(cj_t *db, char const *path) {
26 c_avl_tree_t *values = (void *)db->curl;
29 if (c_avl_get(values, path, (void *)&ret) == 0) {
36 static cj_t *test_setup(char *json, char *key_path) {
37 cj_t *db = calloc(1, sizeof(*db));
38 db->yajl = yajl_alloc(&ycallbacks,
40 /* alloc funcs = */ NULL,
42 /* alloc funcs = */ NULL, NULL,
44 /* context = */ (void *)db);
46 /* hack; see above. */
47 db->curl = (void *)cj_avl_create();
49 cj_key_t *key = calloc(1, sizeof(*key));
50 key->magic = CJ_KEY_MAGIC;
51 key->path = strdup(key_path);
52 key->type = strdup("MAGIC");
54 assert(cj_append_key(db, key) == 0);
56 db->state[0].tree = db->tree;
58 cj_curl_callback(json, strlen(json), 1, db);
60 yajl_complete_parse(db->yajl);
62 yajl_parse_complete(db->yajl);
68 static void test_teardown(cj_t *db) {
69 c_avl_tree_t *values = (void *)db->curl;
74 while (c_avl_pick(values, &key, &value) == 0) {
75 /* key will be freed by cj_free. */
78 c_avl_destroy(values);
92 {"{\"foo\":42,\"bar\":23}", "foo", 42},
93 {"{\"foo\":42,\"bar\":23}", "bar", 23},
94 {"{\"a\":{\"b\":{\"c\":123}}", "a/b/c", 123},
95 {"{\"x\":{\"y\":{\"z\":789}}", "x/*/z", 789},
96 // {"[10,11,12,13]", "0", 10},
97 // {"{\"a\":[[10,11,12,13,14]]}", "a/0/0", 10},
98 // {"{\"a\":[[10,11,12,13,14]]}", "a/0/1", 11},
99 // {"{\"a\":[[10,11,12,13,14]]}", "a/0/2", 12},
100 // {"{\"a\":[[10,11,12,13,14]]}", "a/0/3", 13},
101 // {"{\"a\":[[10,11,12,13,14]]}", "a/0/4", 14},
104 for (size_t i = 0; i < STATIC_ARRAY_SIZE(cases); i++) {
105 cj_t *db = test_setup(cases[i].json, cases[i].key_path);
107 EXPECT_EQ_INT(cases[i].want, test_metric(db, cases[i].key_path));
115 int main(int argc, char **argv) {
116 cj_submit = test_submit;