michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: "use strict"; michael@0: michael@0: Cu.import("resource://services-common/bagheeraclient.js"); michael@0: Cu.import("resource://services-common/rest.js"); michael@0: Cu.import("resource://testing-common/services-common/bagheeraserver.js"); michael@0: Cu.import("resource://gre/modules/Promise.jsm"); michael@0: Cu.import("resource://gre/modules/Task.jsm"); michael@0: michael@0: function getClientAndServer() { michael@0: let server = new BagheeraServer(); michael@0: server.start(); michael@0: michael@0: let client = new BagheeraClient(server.serverURI); michael@0: michael@0: return [client, server]; michael@0: } michael@0: michael@0: function run_test() { michael@0: initTestLogging("Trace"); michael@0: run_next_test(); michael@0: } michael@0: michael@0: add_test(function test_constructor() { michael@0: let client = new BagheeraClient("http://localhost:1234/"); michael@0: michael@0: run_next_test(); michael@0: }); michael@0: michael@0: add_test(function test_post_json_transport_failure() { michael@0: let client = new BagheeraClient("http://localhost:1234/"); michael@0: michael@0: client.uploadJSON("foo", "bar", {}).then(function onResult(result) { michael@0: do_check_false(result.transportSuccess); michael@0: michael@0: run_next_test(); michael@0: }); michael@0: }); michael@0: michael@0: add_test(function test_post_json_simple() { michael@0: let [client, server] = getClientAndServer(); michael@0: michael@0: server.createNamespace("foo"); michael@0: let promise = client.uploadJSON("foo", "bar", {foo: "bar", biz: "baz"}); michael@0: michael@0: promise.then(function onSuccess(result) { michael@0: do_check_true(result instanceof BagheeraClientRequestResult); michael@0: do_check_true(result.request instanceof RESTRequest); michael@0: do_check_true(result.transportSuccess); michael@0: do_check_true(result.serverSuccess); michael@0: michael@0: server.stop(run_next_test); michael@0: }, do_check_null); michael@0: }); michael@0: michael@0: add_test(function test_post_json_bad_data() { michael@0: let [client, server] = getClientAndServer(); michael@0: michael@0: server.createNamespace("foo"); michael@0: michael@0: client.uploadJSON("foo", "bar", "{this is invalid json}").then( michael@0: function onResult(result) { michael@0: do_check_true(result.transportSuccess); michael@0: do_check_false(result.serverSuccess); michael@0: michael@0: server.stop(run_next_test); michael@0: }); michael@0: }); michael@0: michael@0: add_task(function test_post_delete_multiple_obsolete_documents () { michael@0: let [client, server] = getClientAndServer(); michael@0: let namespace = "foo"; michael@0: let documents = [ michael@0: [namespace, "one", "{v:1}"], michael@0: [namespace, "two", "{v:2}"], michael@0: [namespace, "three", "{v:3}"], michael@0: [namespace, "four", "{v:4}"], michael@0: ]; michael@0: michael@0: try { michael@0: // create initial documents michael@0: server.createNamespace(namespace); michael@0: for (let [ns, id, payload] of documents) { michael@0: server.setDocument(ns, id, payload); michael@0: do_check_true(server.hasDocument(ns, id)); michael@0: } michael@0: michael@0: // Test uploading with deleting some documents. michael@0: let deleteIDs = [0, 1].map((no) => { return documents[no][1]; }); michael@0: let result = yield client.uploadJSON(namespace, "new-1", {foo: "bar"}, {deleteIDs: deleteIDs}); michael@0: do_check_true(result.transportSuccess); michael@0: do_check_true(result.serverSuccess); michael@0: do_check_true(server.hasDocument(namespace, "new-1")); michael@0: for (let id of deleteIDs) { michael@0: do_check_false(server.hasDocument(namespace, id)); michael@0: } michael@0: // Check if the documents that were not staged for deletion are still there. michael@0: for (let [,id,] of documents) { michael@0: if (deleteIDs.indexOf(id) == -1) { michael@0: do_check_true(server.hasDocument(namespace, id)); michael@0: } michael@0: } michael@0: michael@0: // Test upload without deleting documents. michael@0: let ids = Object.keys(server.namespaces[namespace]); michael@0: result = yield client.uploadJSON(namespace, "new-2", {foo: "bar"}); michael@0: do_check_true(result.transportSuccess); michael@0: do_check_true(result.serverSuccess); michael@0: do_check_true(server.hasDocument(namespace, "new-2")); michael@0: // Check to see if all the original documents are still there. michael@0: for (let id of ids) { michael@0: do_check_true(deleteIDs.indexOf(id) !== -1 || server.hasDocument(namespace, id)); michael@0: } michael@0: } finally { michael@0: let deferred = Promise.defer(); michael@0: server.stop(deferred.resolve.bind(deferred)); michael@0: yield deferred.promise; michael@0: } michael@0: }); michael@0: michael@0: add_test(function test_delete_document() { michael@0: let [client, server] = getClientAndServer(); michael@0: michael@0: server.createNamespace("foo"); michael@0: server.setDocument("foo", "bar", "{}"); michael@0: michael@0: client.deleteDocument("foo", "bar").then(function onResult(result) { michael@0: do_check_true(result.transportSuccess); michael@0: do_check_true(result.serverSuccess); michael@0: michael@0: do_check_null(server.getDocument("foo", "bar")); michael@0: michael@0: server.stop(run_next_test); michael@0: }); michael@0: });