2 // Created by jlr on 4/9/18.
11 #include <mon_internal.h>
14 * @return reconfigure command
16 static MON_REQ *reconfigure()
18 MON_REQ *req = mon_req_new(NULL, MON_CMD_RECONFIGURE);
24 * @return show command with no options
26 static MON_REQ *show_plain()
28 MON_REQ *req = mon_req_new(NULL, MON_CMD_SHOW);
34 * @param opts array of option types, terminated with OPT_TYPE_UNKNOWN
35 * @return show command with the requested options, excluding the terminator
37 static MON_REQ *show_options(const MON_OPT_TYPE *opts)
39 MON_REQ *req = mon_req_new(NULL, MON_CMD_SHOW);
42 while (*opts != OPT_TYPE_UNKNOWN) {
43 assert(MON_SUCCESS == mon_req_add_option(req, *opts));
50 * @return show command with every option
52 static MON_REQ *show_all_options()
54 MON_OPT_TYPE opts[] = {
55 OPT_TYPE_SHOW_CONFIG_FILES,
56 OPT_TYPE_SHOW_VERSION,
58 OPT_TYPE_SHOW_TID_REQ_COUNT,
59 OPT_TYPE_SHOW_TID_REQ_PENDING,
61 OPT_TYPE_SHOW_COMMUNITIES,
62 OPT_TYPE_UNKNOWN // terminator
65 return show_options(opts);
68 static char *read_file(const char *filename)
70 FILE *f = fopen(filename, "r");
73 ssize_t n = getline(&s, &nn, f);
77 if( (n > 0) && (s[n-1] == '\n'))
83 static int equal(MON_REQ *r1, MON_REQ *r2)
87 if (r1->command != r2->command)
90 if (mon_req_opt_count(r1) != mon_req_opt_count(r2))
93 for (ii=0; ii < mon_req_opt_count(r1); ii++) {
94 if (mon_req_opt_index(r1, ii)->type != mon_req_opt_index(r2, ii)->type)
101 static int run_test(const char *filename, MON_REQ *(generator)())
104 MON_REQ *expected = NULL;
105 char *req_json_str = NULL;
107 expected = generator();
110 req_json_str = read_file(filename);
111 assert(req_json_str);
113 req = mon_req_parse(NULL, req_json_str);
115 assert(equal(req, expected));
119 mon_req_free(expected);
127 // Test reconfigure command
128 assert(run_test("req_reconfigure.test", reconfigure));
130 // Test show command with no options
131 assert(run_test("req_show_no_options.test", show_plain));
133 // Test show command with all the options
134 assert(run_test("req_show_all_options.test", show_all_options));