[asterisk-commits] file: trunk r384857 - in /trunk: res/ tests/
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Sat Apr 6 11:00:25 CDT 2013
Author: file
Date: Sat Apr 6 11:00:20 2013
New Revision: 384857
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=384857
Log:
Add a res_sorcery_astdb module which uses the astdb to persist objects.
Review: https://reviewboard.asterisk.org/r/2420/
Added:
trunk/res/res_sorcery_astdb.c (with props)
trunk/tests/test_sorcery_astdb.c (with props)
Added: trunk/res/res_sorcery_astdb.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_sorcery_astdb.c?view=auto&rev=384857
==============================================================================
--- trunk/res/res_sorcery_astdb.c (added)
+++ trunk/res/res_sorcery_astdb.c Sat Apr 6 11:00:20 2013
@@ -1,0 +1,327 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Digium, Inc.
+ *
+ * Joshua Colp <jcolp at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+ * \file
+ *
+ * \brief Sorcery Astdb Object Wizard
+ *
+ * \author Joshua Colp <jcolp at digium.com>
+ */
+
+/*** MODULEINFO
+ <support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include <regex.h>
+
+#include "asterisk/module.h"
+#include "asterisk/sorcery.h"
+#include "asterisk/astdb.h"
+#include "asterisk/json.h"
+
+static void *sorcery_astdb_open(const char *data);
+static int sorcery_astdb_create(const struct ast_sorcery *sorcery, void *data, void *object);
+static void *sorcery_astdb_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id);
+static void *sorcery_astdb_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields);
+static void sorcery_astdb_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects,
+ const struct ast_variable *fields);
+static void sorcery_astdb_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex);
+static int sorcery_astdb_update(const struct ast_sorcery *sorcery, void *data, void *object);
+static int sorcery_astdb_delete(const struct ast_sorcery *sorcery, void *data, void *object);
+static void sorcery_astdb_close(void *data);
+
+static struct ast_sorcery_wizard astdb_object_wizard = {
+ .name = "astdb",
+ .open = sorcery_astdb_open,
+ .create = sorcery_astdb_create,
+ .retrieve_id = sorcery_astdb_retrieve_id,
+ .retrieve_fields = sorcery_astdb_retrieve_fields,
+ .retrieve_multiple = sorcery_astdb_retrieve_multiple,
+ .retrieve_regex = sorcery_astdb_retrieve_regex,
+ .update = sorcery_astdb_update,
+ .delete = sorcery_astdb_delete,
+ .close = sorcery_astdb_close,
+};
+
+/*! \brief Helper function which converts from a sorcery object set to a json object */
+static struct ast_json *sorcery_objectset_to_json(const struct ast_variable *objectset)
+{
+ struct ast_json *json = ast_json_object_create();
+ const struct ast_variable *field;
+
+ for (field = objectset; field; field = field->next) {
+ struct ast_json *value = ast_json_string_create(field->value);
+
+ if (!value) {
+ ast_json_unref(json);
+ return NULL;
+ } else if (ast_json_object_set(json, field->name, value)) {
+ ast_json_unref(value);
+ ast_json_unref(json);
+ return NULL;
+ }
+ }
+
+ return json;
+}
+
+/*! \brief Helper function which converts a json object to a sorcery object set */
+static struct ast_variable *sorcery_json_to_objectset(struct ast_json *json)
+{
+ struct ast_json_iter *field;
+ struct ast_variable *objset = NULL;
+
+ for (field = ast_json_object_iter(json); field; field = ast_json_object_iter_next(json, field)) {
+ struct ast_json *value = ast_json_object_iter_value(field);
+ struct ast_variable *variable = ast_variable_new(ast_json_object_iter_key(field), ast_json_string_get(value), "");
+
+ if (!variable) {
+ ast_variables_destroy(objset);
+ return NULL;
+ }
+
+ variable->next = objset;
+ objset = variable;
+ }
+
+ return objset;
+}
+
+/*! \brief Helper function which compares two json objects and sees if they are equal, but only looks at the criteria provided */
+static int sorcery_json_equal(struct ast_json *object, struct ast_json *criteria)
+{
+ struct ast_json_iter *field;
+
+ for (field = ast_json_object_iter(criteria); field; field = ast_json_object_iter_next(criteria, field)) {
+ struct ast_json *object_field = ast_json_object_get(object, ast_json_object_iter_key(field));
+
+ if (!object_field || !ast_json_equal(object_field, ast_json_object_iter_value(field))) {
+ return 0;
+ }
+ }
+
+ return 1;
+}
+
+static int sorcery_astdb_create(const struct ast_sorcery *sorcery, void *data, void *object)
+{
+ RAII_VAR(struct ast_variable *, objset, ast_sorcery_objectset_create(sorcery, object), ast_variables_destroy);
+ RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+ RAII_VAR(char *, value, NULL, ast_free_ptr);
+ const char *prefix = data;
+ char family[strlen(prefix) + strlen(ast_sorcery_object_get_type(object)) + 2];
+
+ if (!objset || !(json = sorcery_objectset_to_json(objset)) || !(value = ast_json_dump_string(json))) {
+ return -1;
+ }
+
+ snprintf(family, sizeof(family), "%s/%s", prefix, ast_sorcery_object_get_type(object));
+
+ return ast_db_put(family, ast_sorcery_object_get_id(object), value);
+}
+
+/*! \brief Internal helper function which retrieves an object, or multiple objects, using fields for criteria */
+static void *sorcery_astdb_retrieve_fields_common(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields, struct ao2_container *objects)
+{
+ const char *prefix = data;
+ char family[strlen(prefix) + strlen(type) + 2];
+ RAII_VAR(struct ast_db_entry *, entries, NULL, ast_db_freetree);
+ RAII_VAR(struct ast_json *, criteria, NULL, ast_json_unref);
+ struct ast_db_entry *entry;
+
+ snprintf(family, sizeof(family), "%s/%s", prefix, type);
+
+ if (!(entries = ast_db_gettree(family, NULL)) || (fields && !(criteria = sorcery_objectset_to_json(fields)))) {
+ return NULL;
+ }
+
+ for (entry = entries; entry; entry = entry->next) {
+ const char *key = entry->key + strlen(family) + 2;
+ RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+ struct ast_json_error error;
+ RAII_VAR(struct ast_variable *, objset, NULL, ast_variables_destroy);
+ void *object = NULL;
+
+ if (!(json = ast_json_load_string(entry->data, &error))) {
+ return NULL;
+ } else if (criteria && !sorcery_json_equal(json, criteria)) {
+ continue;
+ } else if (!(objset = sorcery_json_to_objectset(json)) ||
+ !(object = ast_sorcery_alloc(sorcery, type, key)) ||
+ ast_sorcery_objectset_apply(sorcery, object, objset)) {
+ ao2_cleanup(object);
+ return NULL;
+ }
+
+ if (!objects) {
+ return object;
+ }
+
+ ao2_link(objects, object);
+ ao2_cleanup(object);
+ }
+
+ return NULL;
+}
+
+static void *sorcery_astdb_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields)
+{
+ return sorcery_astdb_retrieve_fields_common(sorcery, data, type, fields, NULL);
+}
+
+static void *sorcery_astdb_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id)
+{
+ const char *prefix = data;
+ char family[strlen(prefix) + strlen(type) + 2];
+ RAII_VAR(char *, value, NULL, ast_free_ptr);
+ RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+ struct ast_json_error error;
+ RAII_VAR(struct ast_variable *, objset, NULL, ast_variables_destroy);
+ void *object = NULL;
+
+ snprintf(family, sizeof(family), "%s/%s", prefix, type);
+
+ if (ast_db_get_allocated(family, id, &value) || !(json = ast_json_load_string(value, &error)) ||
+ !(objset = sorcery_json_to_objectset(json)) || !(object = ast_sorcery_alloc(sorcery, type, id)) ||
+ ast_sorcery_objectset_apply(sorcery, object, objset)) {
+ ao2_cleanup(object);
+ return NULL;
+ }
+
+ return object;
+}
+
+static void sorcery_astdb_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const struct ast_variable *fields)
+{
+ sorcery_astdb_retrieve_fields_common(sorcery, data, type, fields, objects);
+}
+
+static void sorcery_astdb_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex)
+{
+ const char *prefix = data;
+ char family[strlen(prefix) + strlen(type) + 2], tree[strlen(regex) + 1];
+ RAII_VAR(struct ast_db_entry *, entries, NULL, ast_db_freetree);
+ regex_t expression;
+ struct ast_db_entry *entry;
+
+ snprintf(family, sizeof(family), "%s/%s", prefix, type);
+
+ if (regex[0] == '^') {
+ snprintf(tree, sizeof(tree), "%s%%", regex + 1);
+ } else {
+ tree[0] = '\0';
+ }
+
+ if (!(entries = ast_db_gettree(family, tree)) || regcomp(&expression, regex, REG_EXTENDED | REG_NOSUB)) {
+ return;
+ }
+
+ for (entry = entries; entry; entry = entry->next) {
+ /* The key in the entry includes the family, so we need to strip it out for regex purposes */
+ const char *key = entry->key + strlen(family) + 2;
+ RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+ struct ast_json_error error;
+ RAII_VAR(void *, object, NULL, ao2_cleanup);
+ RAII_VAR(struct ast_variable *, objset, NULL, ast_variables_destroy);
+
+ if (regexec(&expression, key, 0, NULL, 0)) {
+ continue;
+ } else if (!(json = ast_json_load_string(entry->data, &error)) ||
+ !(objset = sorcery_json_to_objectset(json)) ||
+ !(object = ast_sorcery_alloc(sorcery, type, key)) ||
+ ast_sorcery_objectset_apply(sorcery, object, objset)) {
+ return;
+ }
+
+ ao2_link(objects, object);
+ }
+
+ regfree(&expression);
+}
+
+static int sorcery_astdb_update(const struct ast_sorcery *sorcery, void *data, void *object)
+{
+ const char *prefix = data;
+ char family[strlen(prefix) + strlen(ast_sorcery_object_get_type(object)) + 2], value[2];
+
+ snprintf(family, sizeof(family), "%s/%s", prefix, ast_sorcery_object_get_type(object));
+
+ /* It is okay for the value to be truncated, we are only checking that it exists */
+ if (ast_db_get(family, ast_sorcery_object_get_id(object), value, sizeof(value))) {
+ return -1;
+ }
+
+ /* The only difference between update and create is that for update the object must already exist */
+ return sorcery_astdb_create(sorcery, data, object);
+}
+
+static int sorcery_astdb_delete(const struct ast_sorcery *sorcery, void *data, void *object)
+{
+ const char *prefix = data;
+ char family[strlen(prefix) + strlen(ast_sorcery_object_get_type(object)) + 2];
+ char value[2];
+
+ snprintf(family, sizeof(family), "%s/%s", prefix, ast_sorcery_object_get_type(object));
+
+ if (ast_db_get(family, ast_sorcery_object_get_id(object), value, sizeof(value))) {
+ return -1;
+ }
+
+ return ast_db_del(family, ast_sorcery_object_get_id(object));
+}
+
+static void *sorcery_astdb_open(const char *data)
+{
+ /* We require a prefix for family string generation, or else stuff could mix together */
+ if (ast_strlen_zero(data)) {
+ return NULL;
+ }
+
+ return ast_strdup(data);
+}
+
+static void sorcery_astdb_close(void *data)
+{
+ ast_free(data);
+}
+
+static int load_module(void)
+{
+ if (ast_sorcery_wizard_register(&astdb_object_wizard)) {
+ return AST_MODULE_LOAD_DECLINE;
+ }
+
+ return AST_MODULE_LOAD_SUCCESS;
+}
+
+static int unload_module(void)
+{
+ ast_sorcery_wizard_unregister(&astdb_object_wizard);
+ return 0;
+}
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Sorcery Astdb Object Wizard",
+ .load = load_module,
+ .unload = unload_module,
+ .load_pri = AST_MODPRI_REALTIME_DRIVER,
+);
Propchange: trunk/res/res_sorcery_astdb.c
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: trunk/res/res_sorcery_astdb.c
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Propchange: trunk/res/res_sorcery_astdb.c
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: trunk/tests/test_sorcery_astdb.c
URL: http://svnview.digium.com/svn/asterisk/trunk/tests/test_sorcery_astdb.c?view=auto&rev=384857
==============================================================================
--- trunk/tests/test_sorcery_astdb.c (added)
+++ trunk/tests/test_sorcery_astdb.c Sat Apr 6 11:00:20 2013
@@ -1,0 +1,638 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Digium, Inc.
+ *
+ * Joshua Colp <jcolp at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+ * \file
+ * \brief Sorcery Unit Tests
+ *
+ * \author Joshua Colp <jcolp at digium.com>
+ *
+ */
+
+/*** MODULEINFO
+ <depend>TEST_FRAMEWORK</depend>
+ <support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "")
+
+#include "asterisk/test.h"
+#include "asterisk/module.h"
+#include "asterisk/sorcery.h"
+#include "asterisk/astdb.h"
+#include "asterisk/logger.h"
+
+/*! \brief Dummy sorcery object */
+struct test_sorcery_object {
+ SORCERY_OBJECT(details);
+ unsigned int bob;
+ unsigned int joe;
+};
+
+/*! \brief Internal function to allocate a test object */
+static void *test_sorcery_object_alloc(const char *id)
+{
+ return ao2_alloc(sizeof(struct test_sorcery_object), NULL);
+}
+
+static struct ast_sorcery *alloc_and_initialize_sorcery(void)
+{
+ struct ast_sorcery *sorcery;
+
+ if (!(sorcery = ast_sorcery_open())) {
+ return NULL;
+ }
+
+ if (ast_sorcery_apply_default(sorcery, "test", "astdb", "test") ||
+ ast_sorcery_object_register(sorcery, "test", test_sorcery_object_alloc, NULL, NULL)) {
+ ast_sorcery_unref(sorcery);
+ return NULL;
+ }
+
+ ast_sorcery_object_field_register(sorcery, "test", "bob", "5", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, bob));
+ ast_sorcery_object_field_register(sorcery, "test", "joe", "10", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, joe));
+
+ return sorcery;
+}
+
+static void deinitialize_sorcery(struct ast_sorcery *sorcery)
+{
+ ast_db_deltree("test/test", NULL);
+ ast_sorcery_unref(sorcery);
+}
+
+AST_TEST_DEFINE(object_create)
+{
+ RAII_VAR(struct ast_sorcery *, sorcery, NULL, deinitialize_sorcery);
+ RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
+ char value[2];
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "object_create";
+ info->category = "/res/sorcery_astdb/";
+ info->summary = "sorcery astdb object creation unit test";
+ info->description =
+ "Test object creation in sorcery using astdb wizard";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ if (!(sorcery = alloc_and_initialize_sorcery())) {
+ ast_test_status_update(test, "Failed to open sorcery structure\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
+ ast_test_status_update(test, "Failed to allocate a known object type\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (ast_sorcery_create(sorcery, obj)) {
+ ast_test_status_update(test, "Failed to create object using astdb wizard\n");
+ return AST_TEST_FAIL;
+ } else if (ast_db_get("test/test", "blah", value, sizeof(value))) {
+ ast_test_status_update(test, "Object was apparently created but does not actually exist in astdb\n");
+ return AST_TEST_FAIL;
+ }
+
+ return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(object_retrieve_id)
+{
+ RAII_VAR(struct ast_sorcery *, sorcery, NULL, deinitialize_sorcery);
+ RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "object_retrieve_id";
+ info->category = "/res/sorcery_astdb/";
+ info->summary = "sorcery object retrieval using id unit test";
+ info->description =
+ "Test object retrieval using id in sorcery with astdb wizard";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ if (!(sorcery = alloc_and_initialize_sorcery())) {
+ ast_test_status_update(test, "Failed to open sorcery structure\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
+ ast_test_status_update(test, "Failed to allocate a known object type\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (ast_sorcery_create(sorcery, obj)) {
+ ast_test_status_update(test, "Failed to create object using astdb wizard\n");
+ return AST_TEST_FAIL;
+ }
+
+ ao2_cleanup(obj);
+
+ if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah2"))) {
+ ast_test_status_update(test, "Failed to allocate second instance of a known object type\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (ast_sorcery_create(sorcery, obj)) {
+ ast_test_status_update(test, "Failed to create second object using astdb wizard\n");
+ return AST_TEST_FAIL;
+ }
+
+ ao2_cleanup(obj);
+
+ if (!(obj = ast_sorcery_retrieve_by_id(sorcery, "test", "blah"))) {
+ ast_test_status_update(test, "Failed to retrieve properly created object using id of 'blah'\n");
+ return AST_TEST_FAIL;
+ } else if (strcmp(ast_sorcery_object_get_id(obj), "blah")) {
+ ast_test_status_update(test, "Retrieved object does not have correct id\n");
+ return AST_TEST_FAIL;
+ }
+
+ return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(object_retrieve_field)
+{
+ RAII_VAR(struct ast_sorcery *, sorcery, NULL, deinitialize_sorcery);
+ RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
+ RAII_VAR(struct ast_variable *, fields, ast_variable_new("joe", "42", ""), ast_variables_destroy);
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "object_retrieve_field";
+ info->category = "/res/sorcery_astdb/";
+ info->summary = "sorcery object retrieval using a specific field unit test";
+ info->description =
+ "Test object retrieval using a specific field in sorcery with astdb wizard";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ if (!fields) {
+ ast_test_status_update(test, "Failed to create fields for object retrieval attempt\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (!(sorcery = alloc_and_initialize_sorcery())) {
+ ast_test_status_update(test, "Failed to open sorcery structure\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
+ ast_test_status_update(test, "Failed to allocate a known object type\n");
+ return AST_TEST_FAIL;
+ }
+
+ obj->joe = 42;
+
+ if (ast_sorcery_create(sorcery, obj)) {
+ ast_test_status_update(test, "Failed to create object using astdb wizard\n");
+ return AST_TEST_FAIL;
+ }
+
+ ao2_cleanup(obj);
+
+ if (!(obj = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_DEFAULT, fields))) {
+ ast_test_status_update(test, "Failed to retrieve properly created object using 'joe' field\n");
+ return AST_TEST_FAIL;
+ }
+
+ ao2_cleanup(obj);
+ ast_variables_destroy(fields);
+
+ if (!(fields = ast_variable_new("joe", "49", ""))) {
+ ast_test_status_update(test, "Failed to create fields for object retrieval attempt\n");
+ return AST_TEST_FAIL;
+ }
+
+ if ((obj = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_DEFAULT, fields))) {
+ ast_test_status_update(test, "Retrieved an object using a field with an in-correct value... that should not happen\n");
+ return AST_TEST_FAIL;
+ }
+
+ return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(object_retrieve_multiple_all)
+{
+ RAII_VAR(struct ast_sorcery *, sorcery, NULL, deinitialize_sorcery);
+ RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
+ RAII_VAR(struct ao2_container *, objects, NULL, ao2_cleanup);
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "object_retrieve_multiple_all";
+ info->category = "/res/sorcery_astdb/";
+ info->summary = "sorcery multiple object retrieval unit test";
+ info->description =
+ "Test multiple object retrieval in sorcery using astdb wizard";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ if (!(sorcery = alloc_and_initialize_sorcery())) {
+ ast_test_status_update(test, "Failed to open sorcery structure\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
+ ast_test_status_update(test, "Failed to allocate a known object type\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (ast_sorcery_create(sorcery, obj)) {
+ ast_test_status_update(test, "Failed to create object using astdb wizard\n");
+ return AST_TEST_FAIL;
+ }
+
+ ao2_cleanup(obj);
+
+ if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah2"))) {
+ ast_test_status_update(test, "Failed to allocate second instance of a known object type\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (ast_sorcery_create(sorcery, obj)) {
+ ast_test_status_update(test, "Failed to create second object using astdb wizard\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (!(objects = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL))) {
+ ast_test_status_update(test, "Failed to retrieve a container of all objects\n");
+ return AST_TEST_FAIL;
+ } else if (ao2_container_count(objects) != 2) {
+ ast_test_status_update(test, "Received a container with no objects in it when there should be some\n");
+ return AST_TEST_FAIL;
+ }
+
+ return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(object_retrieve_multiple_field)
+{
+ RAII_VAR(struct ast_sorcery *, sorcery, NULL, deinitialize_sorcery);
+ RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
+ RAII_VAR(struct ao2_container *, objects, NULL, ao2_cleanup);
+ RAII_VAR(struct ast_variable *, fields, ast_variable_new("joe", "6", ""), ast_variables_destroy);
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "object_retrieve_multiple_field";
+ info->category = "/res/sorcery_astdb/";
+ info->summary = "sorcery multiple object retrieval unit test";
+ info->description =
+ "Test multiple object retrieval in sorcery using fields using astdb wizard";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ if (!fields) {
+ ast_test_status_update(test, "Failed to create fields for multiple retrieve\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (!(sorcery = alloc_and_initialize_sorcery())) {
+ ast_test_status_update(test, "Failed to open sorcery structure\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
+ ast_test_status_update(test, "Failed to allocate a known object type\n");
+ return AST_TEST_FAIL;
+ }
+
+ obj->joe = 6;
+
+ if (ast_sorcery_create(sorcery, obj)) {
+ ast_test_status_update(test, "Failed to create object using astdb wizard\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (!(objects = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_MULTIPLE, fields))) {
+ ast_test_status_update(test, "Failed to retrieve a container of all objects\n");
+ return AST_TEST_FAIL;
+ } else if (ao2_container_count(objects) != 1) {
+ ast_test_status_update(test, "Received a container with no objects in it when there should be some\n");
+ return AST_TEST_FAIL;
+ }
+
+ ao2_cleanup(objects);
+ ast_variables_destroy(fields);
+
+ if (!(fields = ast_variable_new("joe", "7", ""))) {
+ ast_test_status_update(test, "Failed to create fields for multiple retrieval\n");
+ return AST_TEST_FAIL;
+ } else if (!(objects = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_MULTIPLE, fields))) {
+ ast_test_status_update(test, "Failed to retrieve an empty container when retrieving multiple\n");
+ return AST_TEST_FAIL;
+ } else if (ao2_container_count(objects)) {
+ ast_test_status_update(test, "Received a container with objects when there should be none in it\n");
+ return AST_TEST_FAIL;
+ }
+
+ return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(object_retrieve_regex)
+{
+ RAII_VAR(struct ast_sorcery *, sorcery, NULL, deinitialize_sorcery);
+ RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
+ RAII_VAR(struct ao2_container *, objects, NULL, ao2_cleanup);
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "object_retrieve_regex";
+ info->category = "/res/sorcery_astdb/";
+ info->summary = "sorcery multiple object retrieval using regex unit test";
+ info->description =
+ "Test multiple object retrieval in sorcery using regular expression for matching using astdb wizard";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ if (!(sorcery = alloc_and_initialize_sorcery())) {
+ ast_test_status_update(test, "Failed to open sorcery structure\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah-98joe"))) {
+ ast_test_status_update(test, "Failed to allocate a known object type\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (ast_sorcery_create(sorcery, obj)) {
+ ast_test_status_update(test, "Failed to create object using astdb wizard\n");
+ return AST_TEST_FAIL;
+ }
+
+ ao2_cleanup(obj);
+
+ if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah-93joe"))) {
+ ast_test_status_update(test, "Failed to allocate second instance of a known object type\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (ast_sorcery_create(sorcery, obj)) {
+ ast_test_status_update(test, "Failed to create second object using astdb wizard\n");
+ return AST_TEST_FAIL;
+ }
+
+ ao2_cleanup(obj);
+
+ if (!(obj = ast_sorcery_alloc(sorcery, "test", "neener-93joe"))) {
+ ast_test_status_update(test, "Failed to allocate third instance of a known object type\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (ast_sorcery_create(sorcery, obj)) {
+ ast_test_status_update(test, "Failed to create third object using astdb wizard\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (!(objects = ast_sorcery_retrieve_by_regex(sorcery, "test", "^blah-"))) {
+ ast_test_status_update(test, "Failed to retrieve a container of objects\n");
+ return AST_TEST_FAIL;
+ } else if (ao2_container_count(objects) != 2) {
+ ast_test_status_update(test, "Received a container with incorrect number of objects in it\n");
+ return AST_TEST_FAIL;
+ }
+
+ return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(object_update)
+{
+ RAII_VAR(struct ast_sorcery *, sorcery, NULL, deinitialize_sorcery);
+ RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
+ RAII_VAR(struct test_sorcery_object *, obj2, NULL, ao2_cleanup);
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "object_update";
+ info->category = "/res/sorcery_astdb/";
+ info->summary = "sorcery object update unit test";
+ info->description =
+ "Test object updating in sorcery using astdb wizard";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ if (!(sorcery = alloc_and_initialize_sorcery())) {
+ ast_test_status_update(test, "Failed to open sorcery structure\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
+ ast_test_status_update(test, "Failed to allocate a known object type\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (ast_sorcery_create(sorcery, obj)) {
+ ast_test_status_update(test, "Failed to create object using astdb wizard\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (!(obj2 = ast_sorcery_copy(sorcery, obj))) {
+ ast_test_status_update(test, "Failed to allocate a known object type for updating\n");
+ return AST_TEST_FAIL;
+ }
+
+ ao2_cleanup(obj);
+
+ obj2->bob = 1000;
+ obj2->joe = 2000;
+
+ if (ast_sorcery_update(sorcery, obj2)) {
+ ast_test_status_update(test, "Failed to update sorcery with new object\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (!(obj = ast_sorcery_retrieve_by_id(sorcery, "test", "blah"))) {
+ ast_test_status_update(test, "Failed to retrieve properly updated object\n");
+ return AST_TEST_FAIL;
+ } else if ((obj->bob != obj2->bob) || (obj->joe != obj2->joe)) {
+ ast_test_status_update(test, "Object retrieved is not the updated object\n");
+ return AST_TEST_FAIL;
+ }
+
+ return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(object_update_uncreated)
+{
+ RAII_VAR(struct ast_sorcery *, sorcery, NULL, deinitialize_sorcery);
+ RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "object_update_uncreated";
+ info->category = "/res/sorcery_astdb/";
+ info->summary = "sorcery object update unit test";
+ info->description =
+ "Test updating of an uncreated object in sorcery using astdb wizard";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ if (!(sorcery = alloc_and_initialize_sorcery())) {
+ ast_test_status_update(test, "Failed to open sorcery structure\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
+ ast_test_status_update(test, "Failed to allocate a known object type\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (!ast_sorcery_update(sorcery, obj)) {
+ ast_test_status_update(test, "Successfully updated an object which has not been created yet\n");
+ return AST_TEST_FAIL;
+ }
+
+ return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(object_delete)
+{
+ RAII_VAR(struct ast_sorcery *, sorcery, NULL, deinitialize_sorcery);
+ RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "object_delete";
+ info->category = "/res/sorcery_astdb/";
+ info->summary = "sorcery object deletion unit test";
+ info->description =
+ "Test object deletion in sorcery using astdb wizard";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ if (!(sorcery = alloc_and_initialize_sorcery())) {
+ ast_test_status_update(test, "Failed to open sorcery structure\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
+ ast_test_status_update(test, "Failed to allocate a known object type\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (ast_sorcery_create(sorcery, obj)) {
+ ast_test_status_update(test, "Failed to create object using astdb wizard\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (ast_sorcery_delete(sorcery, obj)) {
+ ast_test_status_update(test, "Failed to delete object using astdb wizard\n");
+ return AST_TEST_FAIL;
+ }
+
+ ao2_cleanup(obj);
+
+ if ((obj = ast_sorcery_retrieve_by_id(sorcery, "test", "blah"))) {
+ ast_test_status_update(test, "Retrieved deleted object that should not be there\n");
+ return AST_TEST_FAIL;
+ }
+
+ return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(object_delete_uncreated)
+{
+ RAII_VAR(struct ast_sorcery *, sorcery, NULL, deinitialize_sorcery);
+ RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "object_delete_uncreated";
+ info->category = "/res/sorcery_astdb/";
+ info->summary = "sorcery object deletion unit test";
+ info->description =
+ "Test object deletion of an uncreated object in sorcery using astdb wizard";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ if (!(sorcery = alloc_and_initialize_sorcery())) {
+ ast_test_status_update(test, "Failed to open sorcery structure\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
+ ast_test_status_update(test, "Failed to allocate a known object type\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (!ast_sorcery_delete(sorcery, obj)) {
+ ast_test_status_update(test, "Successfully deleted an object which was never created\n");
+ return AST_TEST_FAIL;
+ }
+
+ return AST_TEST_PASS;
+}
+
+static int unload_module(void)
+{
+ AST_TEST_UNREGISTER(object_create);
+ AST_TEST_UNREGISTER(object_retrieve_id);
+ AST_TEST_UNREGISTER(object_retrieve_field);
+ AST_TEST_UNREGISTER(object_retrieve_multiple_all);
+ AST_TEST_UNREGISTER(object_retrieve_multiple_field);
+ AST_TEST_UNREGISTER(object_retrieve_regex);
+ AST_TEST_UNREGISTER(object_update);
+ AST_TEST_UNREGISTER(object_update_uncreated);
+ AST_TEST_UNREGISTER(object_delete);
+ AST_TEST_UNREGISTER(object_delete_uncreated);
+
+ return 0;
+}
+
+static int load_module(void)
+{
+ AST_TEST_REGISTER(object_create);
+ AST_TEST_REGISTER(object_retrieve_id);
+ AST_TEST_REGISTER(object_retrieve_field);
+ AST_TEST_REGISTER(object_retrieve_multiple_all);
+ AST_TEST_REGISTER(object_retrieve_multiple_field);
+ AST_TEST_REGISTER(object_retrieve_regex);
+ AST_TEST_REGISTER(object_update);
+ AST_TEST_REGISTER(object_update_uncreated);
+ AST_TEST_REGISTER(object_delete);
+ AST_TEST_REGISTER(object_delete_uncreated);
+
+ return AST_MODULE_LOAD_SUCCESS;
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Sorcery astdb Wizard test module");
Propchange: trunk/tests/test_sorcery_astdb.c
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: trunk/tests/test_sorcery_astdb.c
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Propchange: trunk/tests/test_sorcery_astdb.c
------------------------------------------------------------------------------
svn:mime-type = text/plain
More information about the asterisk-commits
mailing list