[asterisk-commits] file: branch file/bucket r395618 - in /team/file/bucket: main/ tests/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sun Jul 28 17:08:44 CDT 2013


Author: file
Date: Sun Jul 28 17:08:42 2013
New Revision: 395618

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=395618
Log:
Add file JSON function and test.

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=395618&r1=395617&r2=395618
==============================================================================
--- team/file/bucket/main/bucket.c (original)
+++ team/file/bucket/main/bucket.c Sun Jul 28 17:08:42 2013
@@ -625,6 +625,58 @@
 	return ast_sorcery_delete(bucket_sorcery, file);
 }
 
+struct ast_json *ast_bucket_file_json(const struct ast_bucket_file *file)
+{
+	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+	struct ast_json *id, *metadata;
+	struct ao2_iterator i;
+	struct ast_bucket_metadata *attribute;
+	int res = 0;
+
+	json = ast_sorcery_objectset_json_create(bucket_sorcery, file);
+	if (!json) {
+		return NULL;
+	}
+
+	id = ast_json_string_create(ast_sorcery_object_get_id(file));
+	if (!id) {
+		return NULL;
+	}
+
+	if (ast_json_object_set(json, "id", id)) {
+		ast_json_unref(id);
+		return NULL;
+	}
+
+	metadata = ast_json_object_create();
+	if (!metadata) {
+		return NULL;
+	}
+
+	i = ao2_iterator_init(file->metadata, 0);
+	for (; (attribute = ao2_iterator_next(&i)); ao2_ref(attribute, -1)) {
+		struct ast_json *value = ast_json_string_create(attribute->value);
+
+		if (!value) {
+			res = -1;
+			break;
+		}
+
+		ast_json_object_set(metadata, attribute->name, value);
+	}
+	ao2_iterator_destroy(&i);
+
+	if (res) {
+		ast_json_unref(metadata);
+		return NULL;
+	}
+
+	ast_json_object_set(json, "metadata", metadata);
+
+	ast_json_ref(json);
+	return json;
+}
+
 /*! \brief Hashing function for scheme container */
 static int bucket_scheme_hash(const void *obj, const int flags)
 {

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=395618&r1=395617&r2=395618
==============================================================================
--- team/file/bucket/tests/test_bucket.c (original)
+++ team/file/bucket/tests/test_bucket.c Sun Jul 28 17:08:42 2013
@@ -793,6 +793,92 @@
 	return AST_TEST_PASS;
 }
 
+AST_TEST_DEFINE(bucket_file_json)
+{
+	RAII_VAR(struct ast_bucket_file *, file, NULL, ao2_cleanup);
+	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+	struct ast_json_iter *field;
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = "bucket_file_json";
+		info->category = "/main/bucket/";
+		info->summary = "file json unit test";
+		info->description =
+			"Test creation of JSON for a file";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	if (!(file = ast_bucket_file_alloc("test:///tmp/bob"))) {
+		ast_test_status_update(test, "Failed to allocate bucket\n");
+		return AST_TEST_FAIL;
+	}
+
+	if (ast_bucket_file_metadata_set(file, "bob", "joe")) {
+		ast_test_status_update(test, "Failed to set metadata 'bob' to 'joe' on newly allocated file\n");
+		return AST_TEST_FAIL;
+	}
+
+	json = ast_bucket_file_json(file);
+	if (!json) {
+		ast_test_status_update(test, "Could not produce JSON for a valid file\n");
+		return AST_TEST_FAIL;
+	}
+
+	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 file 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 file 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 file 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 file 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 file JSON but got '%s'\n",
+					ast_json_string_get(value));
+				return AST_TEST_FAIL;
+			}
+		} else if (!strcmp(ast_json_object_iter_key(field), "metadata")) {
+			struct ast_json *bob = ast_json_object_get(value, "bob");
+
+			if (!bob) {
+				ast_test_status_update(test, "Failed to get 'bob' metadata in JSON\n");
+				return AST_TEST_FAIL;
+			}
+
+			if (strcmp(ast_json_string_get(bob), "joe")) {
+				ast_test_status_update(test, "Retrieved 'bob' metadata has value '%s' instead of 'joe'\n",
+					ast_json_string_get(bob));
+				return AST_TEST_FAIL;
+			}
+		}
+	}
+
+	return AST_TEST_PASS;
+}
+
 static int unload_module(void)
 {
 	AST_TEST_UNREGISTER(bucket_scheme_register_unregister);
@@ -809,6 +895,7 @@
 	AST_TEST_UNREGISTER(bucket_file_metadata_set);
 	AST_TEST_UNREGISTER(bucket_file_metadata_unset);
 	AST_TEST_UNREGISTER(bucket_file_metadata_get);
+	AST_TEST_UNREGISTER(bucket_file_json);
 	return 0;
 }
 
@@ -828,6 +915,7 @@
 	AST_TEST_REGISTER(bucket_file_metadata_set);
 	AST_TEST_REGISTER(bucket_file_metadata_unset);
 	AST_TEST_REGISTER(bucket_file_metadata_get);
+	AST_TEST_REGISTER(bucket_file_json);
 	return AST_MODULE_LOAD_SUCCESS;
 }
 




More information about the asterisk-commits mailing list