services/common/tests/unit/test_bagheera_client.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/services/common/tests/unit/test_bagheera_client.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,137 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +"use strict";
     1.8 +
     1.9 +Cu.import("resource://services-common/bagheeraclient.js");
    1.10 +Cu.import("resource://services-common/rest.js");
    1.11 +Cu.import("resource://testing-common/services-common/bagheeraserver.js");
    1.12 +Cu.import("resource://gre/modules/Promise.jsm");
    1.13 +Cu.import("resource://gre/modules/Task.jsm");
    1.14 +
    1.15 +function getClientAndServer() {
    1.16 +  let server = new BagheeraServer();
    1.17 +  server.start();
    1.18 +
    1.19 +  let client = new BagheeraClient(server.serverURI);
    1.20 +
    1.21 +  return [client, server];
    1.22 +}
    1.23 +
    1.24 +function run_test() {
    1.25 +  initTestLogging("Trace");
    1.26 +  run_next_test();
    1.27 +}
    1.28 +
    1.29 +add_test(function test_constructor() {
    1.30 +  let client = new BagheeraClient("http://localhost:1234/");
    1.31 +
    1.32 +  run_next_test();
    1.33 +});
    1.34 +
    1.35 +add_test(function test_post_json_transport_failure() {
    1.36 +  let client = new BagheeraClient("http://localhost:1234/");
    1.37 +
    1.38 +  client.uploadJSON("foo", "bar", {}).then(function onResult(result) {
    1.39 +    do_check_false(result.transportSuccess);
    1.40 +
    1.41 +    run_next_test();
    1.42 +  });
    1.43 +});
    1.44 +
    1.45 +add_test(function test_post_json_simple() {
    1.46 +  let [client, server] = getClientAndServer();
    1.47 +
    1.48 +  server.createNamespace("foo");
    1.49 +  let promise = client.uploadJSON("foo", "bar", {foo: "bar", biz: "baz"});
    1.50 +
    1.51 +  promise.then(function onSuccess(result) {
    1.52 +    do_check_true(result instanceof BagheeraClientRequestResult);
    1.53 +    do_check_true(result.request instanceof RESTRequest);
    1.54 +    do_check_true(result.transportSuccess);
    1.55 +    do_check_true(result.serverSuccess);
    1.56 +
    1.57 +    server.stop(run_next_test);
    1.58 +  }, do_check_null);
    1.59 +});
    1.60 +
    1.61 +add_test(function test_post_json_bad_data() {
    1.62 +  let [client, server] = getClientAndServer();
    1.63 +
    1.64 +  server.createNamespace("foo");
    1.65 +
    1.66 +  client.uploadJSON("foo", "bar", "{this is invalid json}").then(
    1.67 +    function onResult(result) {
    1.68 +    do_check_true(result.transportSuccess);
    1.69 +    do_check_false(result.serverSuccess);
    1.70 +
    1.71 +    server.stop(run_next_test);
    1.72 +  });
    1.73 +});
    1.74 +
    1.75 +add_task(function test_post_delete_multiple_obsolete_documents () {
    1.76 +  let [client, server] = getClientAndServer();
    1.77 +  let namespace = "foo";
    1.78 +  let documents = [
    1.79 +    [namespace, "one", "{v:1}"],
    1.80 +    [namespace, "two", "{v:2}"],
    1.81 +    [namespace, "three", "{v:3}"],
    1.82 +    [namespace, "four", "{v:4}"],
    1.83 +  ];
    1.84 +
    1.85 +  try {
    1.86 +    // create initial documents
    1.87 +    server.createNamespace(namespace);
    1.88 +    for (let [ns, id, payload] of documents) {
    1.89 +      server.setDocument(ns, id, payload);
    1.90 +      do_check_true(server.hasDocument(ns, id));
    1.91 +    }
    1.92 +
    1.93 +    // Test uploading with deleting some documents.
    1.94 +    let deleteIDs = [0, 1].map((no) => { return documents[no][1]; });
    1.95 +    let result = yield client.uploadJSON(namespace, "new-1", {foo: "bar"}, {deleteIDs: deleteIDs});
    1.96 +    do_check_true(result.transportSuccess);
    1.97 +    do_check_true(result.serverSuccess);
    1.98 +    do_check_true(server.hasDocument(namespace, "new-1"));
    1.99 +    for (let id of deleteIDs) {
   1.100 +      do_check_false(server.hasDocument(namespace, id));
   1.101 +    }
   1.102 +    // Check if the documents that were not staged for deletion are still there.
   1.103 +    for (let [,id,] of documents) {
   1.104 +      if (deleteIDs.indexOf(id) == -1) {
   1.105 +        do_check_true(server.hasDocument(namespace, id));
   1.106 +      }
   1.107 +    }
   1.108 +
   1.109 +    // Test upload without deleting documents.
   1.110 +    let ids = Object.keys(server.namespaces[namespace]);
   1.111 +    result = yield client.uploadJSON(namespace, "new-2", {foo: "bar"});
   1.112 +    do_check_true(result.transportSuccess);
   1.113 +    do_check_true(result.serverSuccess);
   1.114 +    do_check_true(server.hasDocument(namespace, "new-2"));
   1.115 +    // Check to see if all the original documents are still there.
   1.116 +    for (let id of ids) {
   1.117 +      do_check_true(deleteIDs.indexOf(id) !== -1 || server.hasDocument(namespace, id));
   1.118 +    }
   1.119 +  } finally {
   1.120 +    let deferred = Promise.defer();
   1.121 +    server.stop(deferred.resolve.bind(deferred));
   1.122 +    yield deferred.promise;
   1.123 +  }
   1.124 +});
   1.125 +
   1.126 +add_test(function test_delete_document() {
   1.127 +  let [client, server] = getClientAndServer();
   1.128 +
   1.129 +  server.createNamespace("foo");
   1.130 +  server.setDocument("foo", "bar", "{}");
   1.131 +
   1.132 +  client.deleteDocument("foo", "bar").then(function onResult(result) {
   1.133 +    do_check_true(result.transportSuccess);
   1.134 +    do_check_true(result.serverSuccess);
   1.135 +
   1.136 +    do_check_null(server.getDocument("foo", "bar"));
   1.137 +
   1.138 +    server.stop(run_next_test);
   1.139 +  });
   1.140 +});

mercurial