src/utils_format_json.[ch]: Implement format_json_notification().
[collectd.git] / src / utils_format_json_test.c
1 /**
2  * collectd - src/utils_format_json_test.c
3  * Copyright (C) 2015       Florian octo Forster
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Florian octo Forster <octo at collectd.org>
25  */
26
27 #include "testing.h"
28 #include "collectd.h"
29 #include "utils_format_json.h"
30 #include "common.h" /* for STATIC_ARRAY_SIZE */
31
32 #include <yajl/yajl_parse.h>
33
34 struct label_s
35 {
36   char *key;
37   char *value;
38 };
39 typedef struct label_s label_t;
40
41 struct test_case_s
42 {
43   label_t *expected_labels;
44   size_t   expected_labels_num;
45
46   label_t *current_label;
47 };
48 typedef struct test_case_s test_case_t;
49
50 static int test_map_key (void *ctx, unsigned char const *key, size_t key_len)
51 {
52   test_case_t *c = ctx;
53   size_t i;
54
55   c->current_label = NULL;
56   for (i = 0; i < c->expected_labels_num; i++)
57   {
58     label_t *l = c->expected_labels + i;
59     if ((strlen (l->key) == key_len)
60         && (strncmp (l->key, (char const *) key, key_len) == 0))
61     {
62       c->current_label = l;
63       break;
64     }
65   }
66
67   return 1; /* continue */
68 }
69
70 static int expect_label (char const *name, char const *got, char const *want)
71 {
72   _Bool ok = (strcmp (got, want) == 0);
73   char msg[1024];
74
75   if (ok)
76     snprintf (msg, sizeof (msg), "label[\"%s\"] = \"%s\"", name, got);
77   else
78     snprintf (msg, sizeof (msg), "label[\"%s\"] = \"%s\", want \"%s\"", name, got, want);
79
80   OK1 (ok, msg);
81   return 0;
82 }
83
84 static int test_string (void *ctx, unsigned char const *value, size_t value_len)
85 {
86   test_case_t *c = ctx;
87
88   if (c->current_label != NULL)
89   {
90     label_t *l = c->current_label;
91     char *got;
92     int status;
93
94     got = malloc (value_len + 1);
95     memmove (got, value, value_len);
96     got[value_len] = 0;
97
98     status = expect_label (l->key, got, l->value);
99
100     free (got);
101
102     if (status != 0)
103       return 0; /* abort */
104   }
105
106   return 1; /* continue */
107 }
108
109 static int expect_json_labels (char *json, label_t *labels, size_t labels_num)
110 {
111   yajl_callbacks funcs = {
112     .yajl_string = test_string,
113     .yajl_map_key = test_map_key,
114   };
115
116   test_case_t c = { labels, labels_num, NULL };
117
118   yajl_handle hndl;
119   CHECK_NOT_NULL (hndl = yajl_alloc (&funcs, NULL, &c));
120   OK (yajl_parse (hndl, (unsigned char *) json, strlen (json)) == yajl_status_ok);
121
122   yajl_free (hndl);
123   return 0;
124 }
125
126 DEF_TEST(notification)
127 {
128   label_t labels[] = {
129     {"summary", "this is a message"},
130     {"alertname", "collectd_unit_test"},
131     {"instance", "example.com"},
132     {"service", "collectd"},
133     {"unit", "case"},
134   };
135
136   /* 1448284606.125 ^= 1555083754651779072 */
137   notification_t n = { NOTIF_WARNING, 1555083754651779072, "this is a message",
138     "example.com", "unit", "", "test", "case", NULL };
139
140   char got[1024];
141   CHECK_ZERO (format_json_notification (got, sizeof (got), &n));
142   // printf ("got = \"%s\";\n", got);
143
144   return expect_json_labels (got, labels, STATIC_ARRAY_SIZE (labels));
145 }
146
147 int main (void)
148 {
149   RUN_TEST(notification);
150
151   END_TEST;
152 }