Asterisk - The Open Source Telephony Project  18.5.0
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 <[email protected]>
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 <[email protected]> \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 
39 static const char *test_category = "/stasis/res/";
40 
41 AST_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 
62 struct app_data {
64  struct ast_json *messages;
65 };
66 
67 static 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 
75 static 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 
87 static 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);
92  res = ast_json_array_append(actual->messages, ast_json_copy(message));
93  ast_assert(res == 0);
94 }
95 
96 AST_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 
131 AST_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 
180 static 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 
188 static 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",
201 );
struct ast_json * ast_json_ref(struct ast_json *value)
Increase refcount on value.
Definition: json.c:67
Asterisk main include file. File version handling, generic pbx functions.
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:591
AST_TEST_DEFINE(app_invoke_dne)
char * ast_eid_to_str(char *s, int maxlen, struct ast_eid *eid)
Convert an EID to a string.
Definition: main/utils.c:2587
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_object_del(struct ast_json *object, const char *key)
Delete a field from a JSON object.
Definition: json.c:408
static void app_data_dtor(void *obj)
struct ast_json * messages
Test Framework API.
#define AST_TEST_REGISTER(cb)
Definition: test.h:127
#define ast_assert(a)
Definition: utils.h:695
#define NULL
Definition: resample.c:96
static int unload_module(void)
struct ast_json * ast_json_null(void)
Get the JSON null value.
Definition: json.c:248
static const char * test_category
void stasis_app_unregister(const char *app_name)
Unregister a Stasis application.
Definition: res_stasis.c:1787
#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:911
struct ast_json * ast_json_array_create(void)
Create a empty JSON array.
Definition: json.c:352
int ast_json_array_append(struct ast_json *array, struct ast_json *value)
Append to an array.
Definition: json.c:368
static void test_handler(void *data, const char *app_name, struct ast_json *message)
static int load_module(void)
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128
def info(msg)
const char * app_name(struct ast_app *app)
Definition: pbx_app.c:463
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:411
int stasis_app_register(const char *app_name, stasis_app_cb handler, void *data)
Register a new Stasis application.
Definition: res_stasis.c:1777
static struct app_data * app_data_create(void)
int stasis_app_send(const char *app_name, struct ast_json *message)
Send a message to the given Stasis application.
Definition: res_stasis.c:1656
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
struct ast_eid ast_eid_default
Global EID.
Definition: options.c:93
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
struct ast_json * ast_json_object_get(struct ast_json *object, const char *key)
Get a field from a JSON object.
Definition: json.c:397
int ast_json_equal(const struct ast_json *lhs, const struct ast_json *rhs)
Compare two JSON objects.
Definition: json.c:347
Abstract JSON element (object, array, string, int, ...).
Stasis Application API. See Stasis Application API for detailed documentation.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
struct ast_json * ast_json_array_get(const struct ast_json *array, size_t index)
Get an element from an array.
Definition: json.c:360
struct ast_json * ast_json_copy(const struct ast_json *value)
Copy a JSON value, but not its children.
Definition: json.c:616