[asterisk-commits] file: branch file/bucket r395602 - in /team/file/bucket: main/ tests/
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Sat Jul 27 11:35:41 CDT 2013
Author: file
Date: Sat Jul 27 11:35:39 2013
New Revision: 395602
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=395602
Log:
Finish up JSON test, make retrieval test work, tweak code some.
Modified:
team/file/bucket/main/bucket.c
team/file/bucket/tests/test_bucket.c
Modified: team/file/bucket/main/bucket.c
URL: http://svnview.digium.com/svn/asterisk/team/file/bucket/main/bucket.c?view=diff&rev=395602&r1=395601&r2=395602
==============================================================================
--- team/file/bucket/main/bucket.c (original)
+++ team/file/bucket/main/bucket.c Sat Jul 27 11:35:39 2013
@@ -84,7 +84,7 @@
static void *bucket_wizard_retrieve(const struct ast_sorcery *sorcery, void *data, const char *type,
const char *id)
{
- char *full_uri, *scheme_name = DEFAULT_UNSPECIFIED_SCHEME, *path, *tmp;
+ char *full_uri, *scheme_name = DEFAULT_UNSPECIFIED_SCHEME, *tmp;
RAII_VAR(struct bucket_scheme *, scheme, NULL, ao2_cleanup);
if (!(full_uri = ast_strdupa(id))) {
@@ -92,14 +92,17 @@
}
/* Assume no scheme until proven otherwise */
- path = full_uri;
if ((tmp = strstr(full_uri, "://"))) {
scheme_name = full_uri;
*tmp++ = '\0';
- path = tmp;
- }
-
- if (ast_strlen_zero(scheme_name) || ast_strlen_zero(path)) {
+
+ /* If only a scheme has been specified this URI is invalid */
+ if (ast_strlen_zero(tmp)) {
+ return NULL;
+ }
+ }
+
+ if (ast_strlen_zero(scheme_name)) {
return NULL;
}
@@ -108,7 +111,7 @@
return NULL;
}
- return scheme->bucket->retrieve_id(sorcery, data, type, path);
+ return scheme->bucket->retrieve_id(sorcery, data, type, id);
}
/*! \brief Callback function for deleting a bucket */
@@ -143,7 +146,8 @@
SCOPED_AO2WRLOCK(lock, schemes);
struct bucket_scheme *scheme;
- if (ast_strlen_zero(name) || !bucket || !file) {
+ if (ast_strlen_zero(name) || !bucket || !file ||
+ !bucket->create || !bucket->delete || !bucket->retrieve_id) {
return -1;
}
@@ -344,27 +348,48 @@
struct ast_json *ast_bucket_json(const struct ast_bucket *bucket)
{
RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
- struct ast_json *files, *buckets;
+ struct ast_json *id, *files, *buckets;
struct ao2_iterator i;
char *uri;
+ int res = 0;
json = ast_sorcery_objectset_json_create(bucket_sorcery, bucket);
if (!json) {
return NULL;
}
+ id = ast_json_string_create(ast_sorcery_object_get_id(bucket));
+ if (!id) {
+ return NULL;
+ }
+
+ if (ast_json_object_set(json, "id", id)) {
+ ast_json_unref(id);
+ return NULL;
+ }
+
buckets = ast_json_array_create();
if (!buckets) {
return NULL;
}
- /* TODO: Below needs better error checking */
-
i = ao2_iterator_init(bucket->buckets, 0);
for (; (uri = ao2_iterator_next(&i)); ao2_ref(uri, -1)) {
- ast_json_array_append(buckets, ast_json_string_create(uri));
+ struct ast_json *bucket_uri = ast_json_string_create(uri);
+
+ if (!bucket_uri) {
+ res = -1;
+ break;
+ }
+
+ ast_json_array_append(buckets, bucket_uri);
}
ao2_iterator_destroy(&i);
+
+ if (res) {
+ ast_json_unref(buckets);
+ return NULL;
+ }
ast_json_object_set(json, "buckets", buckets);
@@ -375,9 +400,21 @@
i = ao2_iterator_init(bucket->files, 0);
for (; (uri = ao2_iterator_next(&i)); ao2_ref(uri, -1)) {
- ast_json_array_append(files, ast_json_string_create(uri));
+ struct ast_json *file_uri = ast_json_string_create(uri);
+
+ if (!file_uri) {
+ res = -1;
+ break;
+ }
+
+ ast_json_array_append(files, file_uri);
}
ao2_iterator_destroy(&i);
+
+ if (res) {
+ ast_json_unref(files);
+ return NULL;
+ }
ast_json_object_set(json, "files", files);
@@ -507,4 +544,4 @@
failure:
bucket_cleanup();
return -1;
-}
+}
Modified: team/file/bucket/tests/test_bucket.c
URL: http://svnview.digium.com/svn/asterisk/team/file/bucket/tests/test_bucket.c?view=diff&rev=395602&r1=395601&r2=395602
==============================================================================
--- team/file/bucket/tests/test_bucket.c (original)
+++ team/file/bucket/tests/test_bucket.c Sat Jul 27 11:35:39 2013
@@ -71,6 +71,16 @@
return 0;
}
+static void *bucket_test_wizard_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type,
+ const char *id)
+{
+ if (!strcmp(type, "bucket")) {
+ return ast_bucket_alloc(id);
+ } else {
+ return NULL;
+ }
+}
+
static int bucket_test_wizard_delete(const struct ast_sorcery *sorcery, void *data, void *object)
{
if (bucket_test_wizard_state.deleted) {
@@ -85,12 +95,14 @@
static struct ast_sorcery_wizard bucket_test_wizard = {
.name = "test",
.create = bucket_test_wizard_create,
+ .retrieve_id = bucket_test_wizard_retrieve_id,
.delete = bucket_test_wizard_delete,
};
static struct ast_sorcery_wizard bucket_file_test_wizard = {
.name = "test",
.create = bucket_test_wizard_create,
+ .retrieve_id = bucket_test_wizard_retrieve_id,
.delete = bucket_test_wizard_delete,
};
@@ -293,6 +305,7 @@
{
RAII_VAR(struct ast_bucket *, bucket, NULL, ao2_cleanup);
RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+ struct ast_json_iter *field;
switch (cmd) {
case TEST_INIT:
@@ -320,7 +333,73 @@
return AST_TEST_FAIL;
}
- /* TODO: Check contents of JSON against expected */
+ 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);
+
+ if (!strcmp(ast_json_object_iter_key(field), "id")) {
+ if (strcmp(ast_json_string_get(value), "bob")) {
+ ast_test_status_update(test, "Expected id of 'bob' in bucket JSON but got '%s'\n",
+ ast_json_string_get(value));
+ return AST_TEST_FAIL;
+ }
+ } else if (!strcmp(ast_json_object_iter_key(field), "modified")) {
+ if (strcmp(ast_json_string_get(value), "0")) {
+ ast_test_status_update(test, "Expected modified time of '0' in bucket JSON but got '%s'\n",
+ ast_json_string_get(value));
+ return AST_TEST_FAIL;
+ }
+ } else if (!strcmp(ast_json_object_iter_key(field), "created")) {
+ if (strcmp(ast_json_string_get(value), "0")) {
+ ast_test_status_update(test, "Expected created time of '0' in bucket JSON but got '%s'\n",
+ ast_json_string_get(value));
+ return AST_TEST_FAIL;
+ }
+ } else if (!strcmp(ast_json_object_iter_key(field), "scheme")) {
+ if (strcmp(ast_json_string_get(value), "test")) {
+ ast_test_status_update(test, "Expected scheme of 'test' in bucket JSON but got '%s'\n",
+ ast_json_string_get(value));
+ return AST_TEST_FAIL;
+ }
+ } else if (!strcmp(ast_json_object_iter_key(field), "uri")) {
+ if (strcmp(ast_json_string_get(value), "test:///tmp/bob")) {
+ ast_test_status_update(test, "Expected URI of 'test:///tmp/bob' in bucket JSON but got '%s'\n",
+ ast_json_string_get(value));
+ return AST_TEST_FAIL;
+ }
+ } else if (!strcmp(ast_json_object_iter_key(field), "buckets")) {
+ struct ast_json *uri;
+
+ if (ast_json_array_size(value) != 1) {
+ ast_test_status_update(test, "Expected buckets array size of '1' in bucket JSON but got '%zd'\n",
+ ast_json_array_size(value));
+ return AST_TEST_FAIL;
+ }
+
+ uri = ast_json_array_get(value, 0);
+
+ if (strcmp(ast_json_string_get(uri), "test:///tmp/bob/joe")) {
+ ast_test_status_update(test, "Expected URI of 'test:///tmp/bob/joe' in buckets array but got '%s'\n",
+ ast_json_string_get(value));
+ return AST_TEST_FAIL;
+ }
+ } else if (!strcmp(ast_json_object_iter_key(field), "files")) {
+ struct ast_json *uri;
+
+ if (ast_json_array_size(value) != 1) {
+ ast_test_status_update(test, "Expected files array size of '1' in bucket JSON but got '%zd'\n",
+ ast_json_array_size(value));
+ return AST_TEST_FAIL;
+ }
+
+ uri = ast_json_array_get(value, 0);
+
+ if (strcmp(ast_json_string_get(uri), "test:///tmp/bob/recording.wav")) {
+ ast_test_status_update(test, "Expected URI of 'test:///tmp/bob/recording.wav' in buckets array but got '%s'\n",
+ ast_json_string_get(value));
+ return AST_TEST_FAIL;
+ }
+ }
+ }
return AST_TEST_PASS;
}
More information about the asterisk-commits
mailing list