Asterisk - The Open Source Telephony Project GIT-master-f36a736
test_res_stasis.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2013, Digium, Inc.
5 *
6 * David M. Lee, II <dlee@digium.com>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19/*!
20 * \file
21 * \brief Test Stasis Application API.
22 * \author\verbatim David M. Lee, II <dlee@digium.com> \endverbatim
23 *
24 * \ingroup tests
25 */
26
27/*** MODULEINFO
28 <depend>TEST_FRAMEWORK</depend>
29 <depend>res_stasis</depend>
30 <support_level>core</support_level>
31 ***/
32
33#include "asterisk.h"
34
35#include "asterisk/module.h"
36#include "asterisk/test.h"
37#include "asterisk/stasis_app.h"
38
39static const char *test_category = "/stasis/res/";
40
41AST_TEST_DEFINE(app_invoke_dne)
42{
43 int res;
44
45 switch (cmd) {
46 case TEST_INIT:
47 info->name = __func__;
48 info->category = test_category;
49 info->summary = "Test stasis app invocation.";
50 info->description = "Test stasis app invocation.";
51 return AST_TEST_NOT_RUN;
52 case TEST_EXECUTE:
53 break;
54 }
55
56 res = stasis_app_send("i-am-not-an-app", ast_json_null());
57 ast_test_validate(test, -1 == res);
58
59 return AST_TEST_PASS;
60}
61
62struct app_data {
65};
66
67static void app_data_dtor(void *obj)
68{
69 struct app_data *actual = obj;
70
71 ast_json_unref(actual->messages);
72 actual->messages = NULL;
73}
74
75static struct app_data *app_data_create(void)
76{
77 struct app_data *res = ao2_alloc(sizeof(struct app_data), app_data_dtor);
78
79 if (!res) {
80 return NULL;
81 }
82
84 return res;
85}
86
87static void test_handler(void *data, const char *app_name, struct ast_json *message)
88{
89 struct app_data *actual = data;
90 int res;
91 ++(actual->invocations);
93 ast_assert(res == 0);
94}
95
96AST_TEST_DEFINE(app_invoke_one)
97{
100 RAII_VAR(struct ast_json *, expected_message, NULL, ast_json_unref);
102 int res;
103
104 switch (cmd) {
105 case TEST_INIT:
106 info->name = __func__;
107 info->category = test_category;
108 info->summary = "Test stasis app invocation.";
109 info->description = "Test stasis app invocation.";
110 return AST_TEST_NOT_RUN;
111 case TEST_EXECUTE:
112 break;
113 }
114
115 app_name = "test-handler";
116
118
120 message = ast_json_pack("{ s: o }", "test-message", ast_json_null());
121 expected_message = ast_json_pack("[o]", ast_json_ref(message));
122
124 ast_test_validate(test, 0 == res);
125 ast_test_validate(test, 1 == app_data->invocations);
126 ast_test_validate(test, ast_json_equal(expected_message, app_data->messages));
127
128 return AST_TEST_PASS;
129}
130
131AST_TEST_DEFINE(app_replaced)
132{
133 RAII_VAR(struct app_data *, app_data1, NULL, ao2_cleanup);
134 RAII_VAR(struct app_data *, app_data2, NULL, ao2_cleanup);
136 RAII_VAR(struct ast_json *, expected_message1, NULL, ast_json_unref);
138 RAII_VAR(struct ast_json *, expected_message2, NULL, ast_json_unref);
139 char eid[20];
140 int res;
141
142 switch (cmd) {
143 case TEST_INIT:
144 info->name = __func__;
145 info->category = test_category;
146 info->summary = "Test stasis app invocation.";
147 info->description = "Test stasis app invocation.";
148 return AST_TEST_NOT_RUN;
149 case TEST_EXECUTE:
150 break;
151 }
152
153 app_name = "test-handler";
154
155 app_data1 = app_data_create();
156 app_data2 = app_data_create();
157
160 expected_message1 = ast_json_pack("[{s: s, s: s, s: s}]",
161 "type", "ApplicationReplaced",
162 "application", app_name,
163 "asterisk_id", ast_eid_to_str(eid, sizeof(eid), &ast_eid_default));
164 message = ast_json_pack("{ s: o }", "test-message", ast_json_null());
165 expected_message2 = ast_json_pack("[o]", ast_json_ref(message));
166
168 ast_test_validate(test, 0 == res);
169 ast_test_validate(test, 1 == app_data1->invocations);
170 ast_test_validate(test, ast_json_object_get(ast_json_array_get(app_data1->messages, 0), "timestamp")? 1: 0);
171 ast_json_object_del(ast_json_array_get(app_data1->messages, 0), "timestamp");
172 ast_test_validate(test, ast_json_equal(expected_message1, app_data1->messages));
173
174 ast_test_validate(test, 1 == app_data2->invocations);
175 ast_test_validate(test, ast_json_equal(expected_message2, app_data2->messages));
176
177 return AST_TEST_PASS;
178}
179
180static int unload_module(void)
181{
182 AST_TEST_UNREGISTER(app_invoke_dne);
183 AST_TEST_UNREGISTER(app_invoke_one);
184 AST_TEST_UNREGISTER(app_replaced);
185 return 0;
186}
187
188static int load_module(void)
189{
190 AST_TEST_REGISTER(app_replaced);
191 AST_TEST_REGISTER(app_invoke_one);
192 AST_TEST_REGISTER(app_invoke_dne);
194}
195
197 .support_level = AST_MODULE_SUPPORT_CORE,
198 .load = load_module,
199 .unload = unload_module,
200 .requires = "res_stasis",
Asterisk main include file. File version handling, generic pbx functions.
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:409
struct ast_json * ast_json_null(void)
Get the JSON null value.
Definition: json.c:248
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
int ast_json_array_append(struct ast_json *array, struct ast_json *value)
Append to an array.
Definition: json.c:378
int ast_json_equal(const struct ast_json *lhs, const struct ast_json *rhs)
Compare two JSON objects.
Definition: json.c:357
struct ast_json * ast_json_array_get(const struct ast_json *array, size_t index)
Get an element from an array.
Definition: json.c:370
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:612
struct ast_json * ast_json_array_create(void)
Create a empty JSON array.
Definition: json.c:362
int ast_json_object_del(struct ast_json *object, const char *key)
Delete a field from a JSON object.
Definition: json.c:418
struct ast_json * ast_json_ref(struct ast_json *value)
Increase refcount on value.
Definition: json.c:67
struct ast_json * ast_json_object_get(struct ast_json *object, const char *key)
Get a field from a JSON object.
Definition: json.c:407
struct ast_json * ast_json_copy(const struct ast_json *value)
Copy a JSON value, but not its children.
Definition: json.c:637
Asterisk module definitions.
@ AST_MODFLAG_DEFAULT
Definition: module.h:329
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:557
@ AST_MODULE_SUPPORT_CORE
Definition: module.h:121
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
def info(msg)
const char * app_name(struct ast_app *app)
Definition: pbx_app.c:463
#define NULL
Definition: resample.c:96
Stasis Application API. See Stasis Application API for detailed documentation.
int stasis_app_send(const char *app_name, struct ast_json *message)
Send a message to the given Stasis application.
Definition: res_stasis.c:1659
int stasis_app_register(const char *app_name, stasis_app_cb handler, void *data)
Register a new Stasis application.
Definition: res_stasis.c:1780
void stasis_app_unregister(const char *app_name)
Unregister a Stasis application.
Definition: res_stasis.c:1790
struct ast_json * messages
Abstract JSON element (object, array, string, int, ...).
Test Framework API.
@ TEST_INIT
Definition: test.h:200
@ TEST_EXECUTE
Definition: test.h:201
#define AST_TEST_REGISTER(cb)
Definition: test.h:127
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128
@ AST_TEST_PASS
Definition: test.h:195
@ AST_TEST_NOT_RUN
Definition: test.h:194
static struct app_data * app_data_create(void)
static void test_handler(void *data, const char *app_name, struct ast_json *message)
AST_TEST_DEFINE(app_invoke_dne)
static int load_module(void)
static int unload_module(void)
static void app_data_dtor(void *obj)
static const char * test_category
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941
#define ast_assert(a)
Definition: utils.h:739
char * ast_eid_to_str(char *s, int maxlen, struct ast_eid *eid)
Convert an EID to a string.
Definition: utils.c:2839
struct ast_eid ast_eid_default
Global EID.
Definition: options.c:93