it("graph__list$", function(d) {
    var graph_list_value = [ 
      { id: 'graph_001',
        name: 'mygraph001',
        app: 'app1',
        path: B.path.join(appbundle_path, '/app1/graphs/graph_001.json') 
      },
      { id: 'graph_002',
        name: 'mygraph002',
        app: 'app1',
        path: B.path.join(appbundle_path, '/app1/graphs/graph_002.json') 
      },
      { id: 'graph_003',
        name: 'mygraph003',
        app: 'app2',
        path: B.path.join(appbundle_path, '/app2/graphs/graph_003.json') 
      },
      { id: 'graph_004',
        name: 'mygraph004',
        app: 'app2',
        path: B.path.join(appbundle_path, '/app2/graphs/graph_004.json') 
      }];

      var max_length = 2;
      if (max_length > graph_list_value.length) {
        max_length = graph_list_value.length;
      }
      em.graph__list$(max_length).then(function(v) {
        assert.equal(_.isEqual(v, graph_list_value.slice(0, max_length)), true);
        d();
    }).done();
  });
  it("ui__list$", function(d) {
    var ui_list_value = [ 
      { id: 'ui_001',
        name: 'myui001',
        app: 'app1',
        path: B.path.join(appbundle_path, '/app1/uis/ui_001.json') 
      },
      { id: 'ui_002',
        name: 'myui002',
        app: 'app1',
        path: B.path.join(appbundle_path, '/app1/uis/ui_002.json') 
      },
      { id: 'ui_003',
        name: 'myui003',
        app: 'app2',
        path: B.path.join(appbundle_path, '/app2/uis/ui_003.json') 
      },
      { id: 'ui_004',
        name: 'myui004',
        app: 'app2',
        path: B.path.join(appbundle_path, '/app2/uis/ui_004.json') 
      }];

      var max_length = 2;
      if (max_length > ui_list_value.length) {
        max_length = ui_list_value.length;
      }
      em.ui__list$(max_length).then(function(v) {
        assert.equal(_.isEqual(v, ui_list_value.slice(0, max_length)), true);
        d();
    }).done();
  });
  it("ui__get_app$", function(d) {
    var ui_id = ["ui_001", "ui_002", "ui_003", "ui_004"];
    var count = 0;
    var app_values = [
      { id: 'app1',
        name: 'myapp1',
        //description: undefined,
        path: B.path.join(appbundle_path, '/app1'),
        //main_ui: undefined,
        graphs: [ 'graph_001', 'graph_002' ],
        uis: [ 'ui_001', 'ui_002' ],
        is_builtin: false 
      },
      { id: 'app1',
        name: 'myapp1',
        //description: undefined,
        path: B.path.join(appbundle_path, '/app1'),
        uis: [ 'ui_001', 'ui_002' ],
        //main_ui: undefined,
        graphs: [ 'graph_001', 'graph_002' ],
        is_builtin: false 
      },
      { id: 'app2',
        name: 'myapp2',
        //description: undefined,
        path: B.path.join(appbundle_path, '/app2'),
        //main_ui: undefined,
        graphs: [ 'graph_003', 'graph_004' ],
        uis: [ 'ui_003', 'ui_004' ],
        is_builtin: false
      },
      { id: 'app2',
        name: 'myapp2',
        //description: undefined,
        path: B.path.join(appbundle_path, '/app2'),
        //main_ui: undefined,
        graphs: [ 'graph_003', 'graph_004' ],
        uis: [ 'ui_003', 'ui_004' ],
        is_builtin: false
      }
    ];

    for (var i = 0; i < ui_id.length; i++) {
      (function(index) {
        em.ui__get_app$(ui_id[index]).then(function(v) {
          count++;
          assert.equal(_.isEqual(v, app_values[index]), true);
          if (count === ui_id.length) {
            d();
          }
        }).done();
      })(i);
    }
  });
Example #4
0
/**
 * transform the drivers.json and driver_docs.json into a new object whose key is classname.
 * we use its side effect to change the global var: grove_doc
 * @param  {String} rootpath the path which contains the drivers.json and driver_docs.json
 */
function _create_grove_doc (rootpath) {
  var driver_path = B.path.join(rootpath, "drivers.json");
  var drivers = require(driver_path);
  var driver_docs_path = B.path.join(rootpath, "driver_docs.json");
  var driver_docs = require(driver_docs_path);
  B.check(drivers.length == driver_docs.length, "entity/grove_thing", "array length is not equal");
  grove_doc = {};
  for (var i = 0; i < drivers.length; i++) {
    grove_doc[drivers[i].ClassName] = {
      driver:drivers[i],
      doc:driver_docs[i]
    };
  }
}
  it("app__update$", function(d) {
    var app = {
      id: "new_app_1",
      name: "newname!",
      description: "add the description!!"
    };

    var app_new_value = {
     new_app_1: 
      { id: 'new_app_1',
        name: 'newname!',
        path: B.path.join(appbundle_path, '/new_app_1'),
        //main_ui: undefined,
        graphs: [],
        uis: [],
        is_builtin: false,
        description: 'add the description!!' 
      } 
     };

    em.app__update$(app).then(function() {
      assert.equal(_.isEqual(em.app_store.store.db, app_new_value), true);
      d();
    }).done();
  });
  it("app__list_graphs$", function(d) {

    var app1_graphs = [ 
    { id: 'graph_001',
      name: 'mygraph001',
      app: 'app1',
      path: B.path.join(appbundle_path, '/app1/graphs/graph_001.json') 
    },
    { id: 'graph_002',
      name: 'mygraph002',
      app: 'app1',
      path: B.path.join(appbundle_path, '/app1/graphs/graph_002.json') 
    } ];

    var app2_graphs = [ 
    { id: 'graph_003',
      name: 'mygraph003',
      app: 'app2',
      path: B.path.join(appbundle_path, '/app2/graphs/graph_003.json') 
    },
    { id: 'graph_004',
      name: 'mygraph004',
      app: 'app2',
      path: B.path.join(appbundle_path, '/app2/graphs/graph_004.json') 
    }];

    var app_graphs_arr = [app1_graphs, app2_graphs];


    var graphs_of_app_id = ["app1", "app2"];
    var count = 0;
    for (var i = 0; i < graphs_of_app_id.length; i++) {
      (function(index) {
        em.app__list_graphs$(graphs_of_app_id[index]).then(function(v) {
          count++;
          assert.equal(_.isEqual(v, app_graphs_arr[index]), true);
          if (count === graphs_of_app_id.length)
            d();
        }).done(); 
      })(i);
    }
  });
  it("app__list_uis$", function(d) {
    var app1_uis = [ 
    { id: 'ui_001',
      name: 'myui001',
      path: B.path.join(appbundle_path, '/app1/uis/ui_001.json'),
      app: 'app1' 
    },
    { id: 'ui_002',
      name: 'myui002',
      path: B.path.join(appbundle_path, '/app1/uis/ui_002.json'),
      app: 'app1' 
    }];

    var app2_uis = [ 
    { id: 'ui_003',
      name: 'myui003',
      path: B.path.join(appbundle_path, '/app2/uis/ui_003.json'),
      app: 'app2' 
    },
    { id: 'ui_004',
      name: 'myui004',
      path: B.path.join(appbundle_path, '/app2/uis/ui_004.json'),
      app: 'app2' 
    }];

    var app_uis_arr = [app1_uis, app2_uis];
    var uis_of_app_id = ["app1", "app2"];

    var count = 0;
    for (var i = 0; i < uis_of_app_id.length; i++) {
      (function(index) {
        em.app__list_uis$(uis_of_app_id[index]).then(function(v) {
          count++;
          assert.equal(_.isEqual(v, app_uis_arr[index]), true);
          if (count === uis_of_app_id.length)
            d();
        }).done();
      })(i);
    }
  });
  it("ui__get$", function(d) {
    var ui_arr = [
      { id: 'ui_001',
        name: 'myui001',
        app: 'app1',
        path: B.path.join(appbundle_path, '/app1/uis/ui_001.json')
      },
      { id: 'ui_002',
        name: 'myui002',
        app: 'app1',
        path: B.path.join(appbundle_path, '/app1/uis/ui_002.json')
      },
      { id: 'ui_003',
        name: 'myui003',
        app: 'app2',
        path: B.path.join(appbundle_path, '/app2/uis/ui_003.json') 
      },
      { id: 'ui_004',
        name: 'myui004',
        app: 'app2',
        path: B.path.join(appbundle_path, '/app2/uis/ui_004.json') 
      }
    ];

    var uis_id = ["ui_001", "ui_002", "ui_003", "ui_004"];
    var count = 0;
    for (var i = 0; i < uis_id.length; i++) {
      (function(index) {
        em.ui__get$(uis_id[index]).then(function(v) {
          count++;
          assert.equal(_.isEqual(v, ui_arr[index]), true);
          if (count === uis_id.length) 
            d();
        }).done();
      })(i);
    }
  });
Example #9
0
FrontEnd.prototype.api_user__register = function(data) {
  B.check(_.isObject(data) && data.name && _.isString(data.passwd),
    "frontend/user_register", "Failed to register for", data);
  var id = B.unique_id("USER_");
  var self = this;

  data.id = id;
  data.appbundle_path = B.path.join(self.center.appbundle_path, id);

  return self.center.em.user__find$(data.name, null).then(function(user) {
    if (user) {
      throw new Error("User name already exists");
    }
    return self.center.em.user__add$(data);
  });
};
function prepare_require(base_path) {
  var fs = require("fs");
  var fp = B.path.join(base_path, "__temp.js");
  var code = "exports.myrequire = require;";
  fs.writeFileSync(fp, code);
  var ret = require(fp);
  fs.unlinkSync(fp);
  function require_in_sandbox(p) {
    var g = {};
    save_global_prop(g);
    var r = ret.myrequire(p);
    recover_global_prop(g);
    return r;
  }
  return require_in_sandbox;
}
Example #11
0
 .then(function() {
   var bundle_json_path = B.path.join(bundle_path, "spec_bundle.json");
   check(B.fs.file_exists(bundle_json_path), "entity/spec", "the bundle json file not exsit", bundle_json_path);
   var bundle_json = B.fs.read_json(bundle_json_path);
   var bundle = _create_local_bundle(bundle_json, bundle_path);
   var spec_path_array = B.fs.find_files(bundle_path, "*.json");
   var tasks = [];
   spec_path_array.forEach(function(p) {
     if (p === bundle_json_path) {
       return;
     }
     log("load_spec", p);
     var spec_json = B.fs.read_json(p);
     var spec = _create_local_spec(spec_json, p, bundle_path, bundle.id);
     bundle.specs.push(spec.id);
     tasks.push(em.spec_store.set$(spec.id, spec, changed_list));
   });
   tasks.push(em.specbundle_store.set$(bundle.id, bundle, changed_list));
   return Promise.all(tasks);
 });
Example #12
0
    service_list.forEach(function(item) {
      var service;
      if (item.service_path) {
        var json_path = B.path.join(item.service_path, "service.json");
        service = B.fs.read_json(json_path);
      } else {
        service = item.servicejson;
      }
      B.check(!_.isUndefined(service), "entity/grove_thing", "service json should not be undefined", item);
      var spec = service.spec;
      spec.id = "spec_" + item.class_name + item.service_name;
      spec.specbundle = specbundle_obj.id;
      if (item.service_path) {
        spec.config[0].default = thing_id;
        spec.config[1].default = item.port_name;
      }
      specbundle_obj.specs.push(spec);
      log("prepare spec:", spec);

      service.spec = spec.id;
      service.type = "grove_service";
      service.id = "groveservice_" + item.class_name + item.port_name + item.service_name + thing_id;
      if (item.service_path) {
        service.path = item.service_path;
        service.type = "grove_semiauto_service";
      } else {
        service.type = "grove_auto_service";
        B.check(!_.isUndefined(item.kerneljs), "entity/grove_thing", "grove_auto_service must contain kerneljs", item);
        service.js = {
          kernel: item.kerneljs
        };
      }
      service.is_connect = true;
      service.own_spec = true;
      service.thing = grovething_obj.id;
      grovething_obj.services.push(service);
      log("prepare service:", service);
    });
  it("ui__update$", function(d) {
    var ui = {
      id: "new_ui_1",
      name: "name_modified",
      description: "add description now",
      app: "new_app_1"
    };

    var ui_new_value =  { 
      new_ui_1: 
        { id: 'new_ui_1',
          name: 'name_modified',
          description: 'add description now',
          app: 'new_app_1',
          path: B.path.join(appbundle_path, '/new_app_1/uis/new_ui_1.json') 
        } 
      };

    em.ui__update$(ui).then(function() {
      assert.equal(_.isEqual(em.ui_store.store.db, ui_new_value), true);
      d();
  }).done();
  });
describe("spec", function() {
	//create spec_store, specbundle_store
	var specstore;
	var specbundlestore;
	var em;

	before("create_entity_manager", function(d) {
		ES.create_specstore$("memory").then(function(obj1) {
			specstore = obj1;
			return ES.create_specbundlestore$("memory");
		}).then(function(obj2) {
			specbundlestore = obj2;
			em = E.create_entity_manager({
				spec_store:specstore, 
				specbundle_store:specbundlestore
			});
			d();
		}).done();
	});


	var specbundle_path = B.path.join(__dirname, "./spec_bundle");


	var spec1 = 
	{ 
		id: 'spec1',
		name: 'spec1 in myspec1',
		path: '/home/myuser/hope/entity/test/spec_bundle/myspec1/spec1.json',
		specbundle: 'specbundle01' 
	};

	var spec2 = 
	{ 
		id: 'spec2',
		name: 'spec2 in myspec1',
		path: '/home/myuser/hope/entity/test/spec_bundle/myspec1/spec2.json',
		specbundle: 'specbundle01' 
	};

	var spec3 = 
	{
		id: 'spec3',
		name: 'spec3 in myspec2',
		path: '/home/myuser/hope/entity/test/spec_bundle/myspec2/spec3.json',
		specbundle: 'specbundle01'
	};

	var spec4 = 
	{
		id: 'spec4',
		name: 'spec4 in myspec2',
		path: '/home/myuser/hope/entity/test/spec_bundle/myspec2/spec4.json',
		specbundle: 'specbundle01' 
	};

	var specbundle01 = 
	{ 
		id: 'specbundle01',
		name: 'spec_bundle',
		specs: [ 'spec1', 'spec2', 'spec3', 'spec4' ],
		path: '/home/myuser/hope/entity/test/spec_bundle' 
	};

	it("spec__load_from_localbundle$", function(d) {
		var specbundle_in_store = { 
			specbundle01: specbundle01
		};

		
		var spec_in_store = { 
			spec1:  spec1,
			spec2:  spec2,
			spec3:  spec3,
			spec4:  spec4	
		};


		em.spec__load_from_localbundle$(specbundle_path).then(function() {
			assert.equal(_.isEqual(em.specbundle_store.store.db, specbundle_in_store), true);
			assert.equal(_.isEqual(em.spec_store.store.db, spec_in_store), true);
			d();
		}).done();
	});

	it("spec__get$", function(d) {
		var count = 0;
		var spec_arr = ["spec1", "spec2", "spec3", "spec4"];
		var spec_arr_value = [spec1, spec2, spec3, spec4];

		for (var i = 0; i < spec_arr.length; i++) {
			(function(index) {
				em.spec__get$(spec_arr[index]).then(function(v) {
					count++;
					assert.equal(_.isEqual(v, spec_arr_value[index]), true);
					if (count == spec_arr.length)
						d();
				}).done();
			})(i);
		}
	});

	it("spec__list$", function(d) {
		var max_length = 2;
		var spec_list_value = ["spec1", "spec2", "spec3", "spec4"];
		if (max_length > spec_list_value.length)
			max_length = spec_list_value.length;

		em.spec__list$(max_length).then(function(v) {
			assert.equal(_.isEqual(v, spec_list_value.slice(0, max_length)), true);
			d();
		}).done();
	});

	it("spec__get_specbundle$", function(d) {
		em.spec__get_specbundle$("spec1").then(function(v) {
			assert.equal(_.isEqual(v, specbundle01), true);
			d();
		}).done();

	});

	it("specbundle__get$", function(d) {
		var specbundle_id_arr = ["specbundle01"];
		var specbundle_arr = [specbundle01];
		var count = 0;

		for (var i = 0; i < specbundle_id_arr.length; i++) {
			(function(index) {
				em.specbundle__get$(specbundle_id_arr[index]).then(function(v) {
					count++;
					assert.equal(_.isEqual(v, specbundle_arr[index]), true);
					if (count == specbundle_id_arr.length)
						d();
				}).done();
			})(i);
		}
	});

	it("specbundle__list$", function(d) {
		var max_length = 2;
		var specbundle_list_value = [specbundle01];
		if (max_length > specbundle_list_value.length)
			max_length = specbundle_list_value.length;
		em.specbundle__list$(max_length).then(function(v) {
			assert.equal(_.isEqual(v, specbundle_list_value.slice(0, max_length)), true);
			d();
		}).done();
	});

	it("specbundle__list_specs$", function(d) {
		var specbundle_id_arr = ["specbundle01"];
		var specbundle_list_specs_value = [[spec1, spec2, spec3, spec4]];
		var count = 0;

		for (var i = 0; i < specbundle_id_arr.length; i++) {
			(function(index) {
				em.specbundle__list_specs$(specbundle_id_arr[index]).then(function(v) {
					count++;
					assert.equal(_.isEqual(v, specbundle_list_specs_value[index]), true);
					if (count == specbundle_id_arr.length)
						d();
				}).done();
			})(i);
		}
	});
});
/*eslint no-console:0*/
var B = require("hope-base");
var E = require("../index");
var ES = require("../../entity-store");


var appstore = ES.create_appstore("memory");
var graphstore = ES.create_graphstore("memory");
var uistore = ES.create_uistore("memory");
var em = E.create_entity_manager({
  app_store:appstore,
  graph_store:graphstore,
  ui_store:uistore
});

var appbundlepath = B.path.join(__dirname, "./appbundle");

em.app__load_from_bundle$(appbundlepath)
.then(function(list) {
  console.log("==== load bundle ===");
  console.log("[changed_list]", list);
  console.log("[app_store]:", em.app_store.store.db);
  console.log("[graph_store]:", em.graph_store.store.db);
  console.log("[ui_store]:", em.ui_store.store.db);
  console.log("=============\n\n");
}).done(); // 2 apps and 4 graphs

setTimeout(function add_graph() {
  var graph = {
    id: "new_graph_1",
    name: "newgraph1",
  ui_store:uistore
});

var hub = {
  id: "hub1",
  name: "hub in sample",
  mnode: "no need",
  things: []
};

var specbundle = {
  name: "public specbundle in hub1",
  id: "public_specbundle1"
};

var thingbundle_path = B.path.join(__dirname, "./thingbundle");

em.hub_store.set$(hub.id, hub)
.then(function() {
  return em.thing__load_from_bundle$(thingbundle_path, specbundle, "hub1");
})
.then(function(list) {
  console.log("========= load from bundle =======");
  console.log("[changed_list]", list);
  console.log("[specbundle]:", em.specbundle_store.store.db);
  console.log("[spec]:", em.spec_store.store.db);
  console.log("[hub]:", em.hub_store.store.db);
  console.log("[thing]:", em.thing_store.store.db);
  console.log("[service]:", em.service_store.store.db);
}).done();
function prepare_script(service_cache_obj, action) {
  var context;
  var func_string;
  var func_script;
  switch (service_cache_obj.type) {
    case "hope_service":
    case "beihai_service":
    case "oic_service":
    case "ui_service":
    case "grove_semiauto_service":
      var filepath = B.path.join(service_cache_obj.path, action + ".js");

      //1. read the content of the file
      if (B.fs.file_exists(filepath)) {
        context = fs.readFileSync(filepath);
      }
      else if (action === "kernel" || action === "after_resume") {
        context = "";
      }
      else {
        context = "done()";
      }

      //2 wrap the function
      if (action === "kernel") {
        func_string = "(function(IN, CONFIG){\n" + context + "\n})";
      }
      else if (action === "service_init" || action === "service_destroy") {
        func_string = "(function(CONFIG){\n" + context + "\n})";
      }
      else {
        func_string = "(function(CONFIG){\n" + context + "\n})";
      }

      //3 new vm.Script
      func_script = new vm.Script(func_string,
        {filename: service_cache_obj.path + "__" + action});
      return func_script;
    case "grove_auto_service":

      //1. read the content of the prop in service_cache_obj
      if (!_.isUndefined(service_cache_obj.js[action])) {
        context = service_cache_obj.js[action];
      }
      else if (action === "kernel" || action === "after_resume") {
        context = "";
      }
      else {
        context = "done()";
      }

      //2 wrap the function
      if (action === "kernel") {
        func_string = "(function(IN, CONFIG){\n" + context + "\n})";
      }
      else if (action === "service_init" || action === "service_destroy") {
        func_string = "(function(CONFIG){\n" + context + "\n})";
      }
      else {
        func_string = "(function(CONFIG){\n" + context + "\n})";
      }

      //3 new vm.Script
      func_script = new vm.Script(func_string,
        {filename: service_cache_obj.path + "__" + action});
      return func_script;
    case "nodered_service":
      if (action === "after_resume") {
        context = "if (CONFIG._nr.category !== 'config') {\n" +
                  " var f = require('" + service_cache_obj.path.replace(/\\/g, "\\\\") + "');\n" +
                  " __nodered.sendOUT = sendOUT;\n" +
                  " __nodered.sendERR = sendERR;\n" +
                  " __nodered.id = CONFIG._nr.id;\n" +
                  " f(__nodered.RED);\n" +
                  " __nodered.node = new __nodered['Type_' + CONFIG._nr.type](CONFIG);\n" +
                  " hub_shared.__nodered_nodes[CONFIG._nr.id] = __nodered.node;\n" +
                  "};";
        func_string = "(function(CONFIG){\n" + context + "\n})";
      } else if (action === "resume") {
        context = "if (CONFIG._nr.category === 'config') {\n" +
                  " var f = require('" + service_cache_obj.path.replace(/\\/g, "\\\\") + "');\n" +
                  " __nodered.sendOUT = function(){};\n" +
                  " __nodered.sendERR = function(){};\n" +
                  " __nodered.id = CONFIG._nr.id;\n" +
                  " f(__nodered.RED);\n" +
                  " __nodered.node = new __nodered['Type_' + CONFIG._nr.type](CONFIG);\n" +
                  " hub_shared.__nodered_nodes[CONFIG._nr.id] = __nodered.node;\n" +
                  "};\n" +
                  "done();";
        func_string = "(function(CONFIG){\n" + context + "\n})";
      } else if (action === "kernel") {
        func_string = "(function(IN, CONFIG){\n" +
                          "__nodered.sendOUT = sendOUT;\n" +
                          "__nodered.sendERR = sendERR;\n" +
                          "__nodered.node.receive(IN.in1);\n" +
                      "})";
      } else if (action === "pause") {
        func_string = "(function(CONFIG){ Promise.resolve(__nodered.node.close()).then(function(){__nodered.node.afterclose();done();});})";
      } else {
        func_string = "(function(CONFIG){ done() })";
      }
      func_script = new vm.Script(func_string,
        {filename: service_cache_obj.path + "__" + action});
      return func_script;
    default:
      check(false, "sm", "not support the service type:", service_cache_obj.type);
  }
}
Example #18
0
  /**
   * parse the one item of the wellknown api
   * @param  {string} apiname such as "GET /v1/node/GroveRotaryAngleA0/angle -> int angle"
   * @return {object}         object:
   *                           {
   *                             class_name: GroveRotaryAngle
   *                             service_name: read_angle, the prefix is decided by get or post  
   *                             port_name: A0 
   *                             service_path: the service template folder
   *                           }
   *                           if the return is null, this items is not service, we should ignore it 
   *                           in the following process.
   */
  function parse_apiname(apiname) {
    var splits = apiname.split(" ");
    var prefix;
    var method_type; // for generate_grove_service_now 
    if (splits[0] === "GET") {
      prefix = "read_";
      method_type = "Reads";
    }
    else if (splits[0] === "POST") {
      prefix = "write_";
      method_type = "Writes";
    }
    else {
      //find HASEVENT API, ignore it
      return null;
    }

    var service_name = prefix + splits[1].split("/")[4];
    var fullname = splits[1].split("/")[3];
    var port_name;
    var class_name;

    if (_.endsWith(fullname, "D0")) {
      port_name = "D0";
      class_name = fullname.slice(0, -2);
    } else if (_.endsWith(fullname, 'D1')) {
      port_name = "D1";
      class_name = fullname.slice(0, -2);     
    } else if (_.endsWith(fullname, 'D2')) {
      port_name = "D2";
      class_name = fullname.slice(0, -2);     
    } else if (_.endsWith(fullname, 'A0')) {
      port_name = "A0";
      class_name = fullname.slice(0, -2);     
    } else if (_.endsWith(fullname, 'I2C0')) {
      port_name = "I2C0";
      class_name = fullname.slice(0, -4);     
    } else if (_.endsWith(fullname, 'UART0')) {
      port_name = "UART0";
      class_name = fullname.slice(0, -5);     
    } else {
      check("false", "entity/grove_thing", "unkown class_name and port_name", fullname);
    }
    var service_path = B.path.join(grove_bundle_path, class_name, service_name);
    var service_ret;
    //if the service template doesnt exsit, create the service right now, set prop:kerneljs servicejson
    if (!is_service_path(service_path)) {
      log("the service folder doesn't exsit, create now", service_name);
      service_ret = generate_grove_service_now( grove_bundle_path, 
                                                class_name, 
                                                service_name, 
                                                method_type, 
                                                port_name, 
                                                thing_id, 
                                                thing_name, 
                                                base_url);
    } else {
      service_ret = {
        class_name: class_name,
        service_name : service_name,
        service_path : service_path,
        port_name : port_name
      };
    }
    log("parse the wellknown api done:", apiname, service_ret);
    return service_ret;
  } // end of function parse_apiname(apiname)
describe("app/graph/ui update", function() {
  var appstore;
  var graphstore;
  var uistore;
  var em;

  before("create_entity_manager",function(d) {
    ES.create_appstore$("memory").then(function(obj1) {
      appstore = obj1;
      return ES.create_graphstore$("memory");
    }).then(function(obj2) {
      graphstore = obj2;
      return ES.create_uistore$("memory");
    }).then(function(obj3) {
      uistore = obj3;
      em = E.create_entity_manager({
        app_store : appstore,
        graph_store : graphstore,
        ui_store : uistore
      });
      d();
    }).done();
  });

  var appbundle_path = B.path.join(__dirname, "./app_bundle");


   //app__update$ test
  it("app add", function(d) {
    var app = {
      id: "new_app_1",
      name: "newapp1"
    };
    em.app__add$(app, appbundle_path).then(function() {
      d();
    }).done();
  });

  it("app__update$", function(d) {
    var app = {
      id: "new_app_1",
      name: "newname!",
      description: "add the description!!"
    };

    var app_new_value = {
     new_app_1: 
      { id: 'new_app_1',
        name: 'newname!',
        path: B.path.join(appbundle_path, '/new_app_1'),
        //main_ui: undefined,
        graphs: [],
        uis: [],
        is_builtin: false,
        description: 'add the description!!' 
      } 
     };

    em.app__update$(app).then(function() {
      assert.equal(_.isEqual(em.app_store.store.db, app_new_value), true);
      d();
    }).done();
  });


  ////graph__update$ test
  it("add graph", function(d) {
    var graph = {
      id: "new_graph_1",
      name: "newgraph1",
      app: "new_app_1"
    };
    em.graph__add$(graph).then(function() {
      d();
    }).done();
  });


  it("graph__update$", function(d) {
    var graph = {
      id: "new_graph_1",
      name: "name_modified",
      description: "add description now",
      app: "new_app_1"
    };

    var graph_new_value =  { 
      new_graph_1: 
        { id: 'new_graph_1',
          name: 'name_modified',
          description: 'add description now',
          app: 'new_app_1',
          path: B.path.join(appbundle_path, '/new_app_1/graphs/new_graph_1.json') 
        } 
      };

    em.graph__update$(graph).then(function() {
      assert.equal(_.isEqual(em.graph_store.store.db, graph_new_value), true);
      d();
  }).done();
  });


  it("graph__remove", function(d) {
    em.graph__remove$("new_graph_1").then(function() {
    d();
  }).done();
  });


  ////ui__update$ test
  it("ui add", function(d) {
    var ui = {
      id: "new_ui_1",
      name: "newui1",
      app: "new_app_1"
    };
    em.ui__add$(ui).then(function() {
      d();
    }).done();
  });
  

  it("ui__update$", function(d) {
    var ui = {
      id: "new_ui_1",
      name: "name_modified",
      description: "add description now",
      app: "new_app_1"
    };

    var ui_new_value =  { 
      new_ui_1: 
        { id: 'new_ui_1',
          name: 'name_modified',
          description: 'add description now',
          app: 'new_app_1',
          path: B.path.join(appbundle_path, '/new_app_1/uis/new_ui_1.json') 
        } 
      };

    em.ui__update$(ui).then(function() {
      assert.equal(_.isEqual(em.ui_store.store.db, ui_new_value), true);
      d();
  }).done();
  });

  it("ui__remove$", function(d) {
    em.ui__remove$("new_ui_1").then(function() {
      d();
    }).done();
  });


  it("app__remove$", function(d) {
    var app_id = "new_app_1";
    em.app__remove$(app_id).then(function() {
      d();
    }).done();
  });
});
Example #20
0
function is_service_path(service_path) {
  return B.fs.dir_exists(service_path) && 
  B.fs.file_exists(B.path.join(service_path, "service.json"));
}
  it("app_load_from_bundle$", function(d) {
    
    app_in_store = { 
    app1: 
    {
      id: 'app1',
      name: 'myapp1',
      //description: undefined,
      path: B.path.join(appbundle_path, '/app1'),
      //main_ui: undefined,
      graphs: [ 'graph_001', 'graph_002' ],
      uis: [ 'ui_001', 'ui_002' ],
      is_builtin: false 
    },
    app2: 
    {
      id: 'app2',
      name: 'myapp2',
      //description: undefined,
      path: B.path.join(appbundle_path, '/app2'),
      //main_ui: undefined,
      graphs: [ 'graph_003', 'graph_004' ],
      uis: [ 'ui_003', 'ui_004' ],
      is_builtin: false 
    }
  };

   graph_in_store = { graph_001: 
   { id: 'graph_001',
     name: 'mygraph001',
     app: 'app1',
     path: B.path.join(appbundle_path, '/app1/graphs/graph_001.json') },
  graph_002: 
   { id: 'graph_002',
     name: 'mygraph002',
     app: 'app1',
     path: B.path.join(appbundle_path, '/app1/graphs/graph_002.json') },
  graph_003: 
   { id: 'graph_003',
     name: 'mygraph003',
     app: 'app2',
     path: B.path.join(appbundle_path, '/app2/graphs/graph_003.json') },
  graph_004: 
   { id: 'graph_004',
     name: 'mygraph004',
     app: 'app2',
     path: B.path.join(appbundle_path, '/app2/graphs/graph_004.json') } 
  };

  ui_in_store = { 
  ui_001: 
   { id: 'ui_001',
     name: 'myui001',
     path: B.path.join(appbundle_path, '/app1/uis/ui_001.json'),
     app: 'app1' },
  ui_002: 
   { id: 'ui_002',
     name: 'myui002',
     path: B.path.join(appbundle_path, '/app1/uis/ui_002.json'),
     app: 'app1' },
  ui_003: 
   { id: 'ui_003',
     name: 'myui003',
     path: B.path.join(appbundle_path, '/app2/uis/ui_003.json'),
     app: 'app2' },
  ui_004: 
   { id: 'ui_004',
     name: 'myui004',
     path: B.path.join(appbundle_path, '/app2/uis/ui_004.json'),
     app: 'app2' }
   };

    em.app__load_from_bundle$(appbundle_path, null).then(function() {
      assert.equal(_.isEqual(em.app_store.store.db, app_in_store), true);
      assert.equal(_.isEqual(em.graph_store.store.db, graph_in_store), true);
      assert.equal(_.isEqual(em.ui_store.store.db, ui_in_store), true);
      d();
    }).done();
  });
describe("app/graph/ui add/remove/list/get", function() {
  //create app_store,graph_store,ui_store
  var appstore;
  var graphstore;
  var uistore;
  var em;

  before("create_entity_manager", function(d) {
    ES.create_appstore$("memory").then(function(obj1) {
      appstore = obj1;
      return ES.create_graphstore$("memory");
    }).then(function(obj2) {
      graphstore = obj2;
      return ES.create_uistore$("memory");
    }).then(function(obj3) {
      uistore = obj3;
      em = E.create_entity_manager({
        app_store : appstore,
        graph_store : graphstore,
        ui_store : uistore
      });
      d();
    }).done();
  });

  var appbundle_path = B.path.join(__dirname, "./app_bundle");


  //app_add and app_remove
  var app_add_array = {};

  var new_app1 = {
    id: 'new_app_1',
    name: 'newapp1',
    //description: undefined,
    path: B.path.join(appbundle_path, '/new_app_1'),
   
   // main_ui: undefined,
    graphs: [],
    uis: [],
    is_builtin: false
  };

  var new_app2 = 
  {
    id: 'new_app_2',
    name: 'newapp1',
    //description: undefined,
    path: B.path.join(appbundle_path, '/new_app_2'),
    //main_ui: undefined,
    graphs: [],
    uis: [],
    is_builtin: false 
  };

  var new_app3 =
  {
    id: 'new_app_3',
    name: 'newapp1',
    //description: undefined,
    path: B.path.join(appbundle_path, '/new_app_3'),
    //main_ui: undefined,
    graphs: [],
    uis: [],
    is_builtin: false
  };

  var app_in_store_0 = 
  {
    new_app_1: new_app1
  };

  var app_in_store_1 = 
  {
    new_app_1: new_app1, 
    new_app_2: new_app2
  };


  var app_in_store_2 = 
  {
    new_app_1: new_app1,
    new_app_2: new_app2,
    new_app_3: new_app3
  };

  var app_in_store_array = [app_in_store_0, app_in_store_1, app_in_store_2];
  var app_in_store_array1 = [new_app1, new_app2, new_app3];

  it("app__add$", function(d) {
      app_add_array = [
      {
        id: "new_app_1",
        name: "newapp1"
      },
      {
        id: "new_app_2",
        name: "newapp1"
      },
      {
        id: "new_app_3",
        name: "newapp1"
      }
    ];

    var count = 0;
    for (var i = 0; i < app_add_array.length; i++) {
      (function(index) {
        em.app__add$(app_add_array[index], appbundle_path).then(function() {
          em.app__get$(app_add_array[index].id).then(function(v) {
            assert.equal(_.isEqual(v, app_in_store_array1[index]), true);
          });
          assert.equal(_.isEqual(em.app_store.store.db, app_in_store_array[index]), true);
          assert.equal(_.isEqual(em.graph_store.store.db, {}), true);
          assert.equal(_.isEqual(em.ui_store.store.db, {}), true);
          count++;
          if (count === app_add_array.length)
            d();
        }).done();
      })(i);
    }
  });


  it("app_remove$", function(d) {
    
    var count = 0;
    for (var i = app_add_array.length - 1; i >= 0; i--) {
      (function(index) {
        em.app__remove$(app_add_array[index].id).then(function() { 
          count++;
          if (index !== 0) {
            assert.equal(_.isEqual(em.app_store.store.db, app_in_store_array[index - 1]), true);
            
          }
          else {
            assert.equal(_.isEqual(em.app_store.store.db, {}), true);
          }
          assert.equal(_.isEqual(em.graph_store.store.db, {}), true);
          assert.equal(_.isEqual(em.ui_store.store.db, {}), true);
          if (count === app_add_array.length)
            d();
        }).done();
      })(i);
    }
  });


  var graph_in_store = {};
  var ui_in_store = {};
  var app_in_store = {};
  it("app_load_from_bundle$", function(d) {
    
    app_in_store = { 
    app1: 
    {
      id: 'app1',
      name: 'myapp1',
      //description: undefined,
      path: B.path.join(appbundle_path, '/app1'),
      //main_ui: undefined,
      graphs: [ 'graph_001', 'graph_002' ],
      uis: [ 'ui_001', 'ui_002' ],
      is_builtin: false 
    },
    app2: 
    {
      id: 'app2',
      name: 'myapp2',
      //description: undefined,
      path: B.path.join(appbundle_path, '/app2'),
      //main_ui: undefined,
      graphs: [ 'graph_003', 'graph_004' ],
      uis: [ 'ui_003', 'ui_004' ],
      is_builtin: false 
    }
  };

   graph_in_store = { graph_001: 
   { id: 'graph_001',
     name: 'mygraph001',
     app: 'app1',
     path: B.path.join(appbundle_path, '/app1/graphs/graph_001.json') },
  graph_002: 
   { id: 'graph_002',
     name: 'mygraph002',
     app: 'app1',
     path: B.path.join(appbundle_path, '/app1/graphs/graph_002.json') },
  graph_003: 
   { id: 'graph_003',
     name: 'mygraph003',
     app: 'app2',
     path: B.path.join(appbundle_path, '/app2/graphs/graph_003.json') },
  graph_004: 
   { id: 'graph_004',
     name: 'mygraph004',
     app: 'app2',
     path: B.path.join(appbundle_path, '/app2/graphs/graph_004.json') } 
  };

  ui_in_store = { 
  ui_001: 
   { id: 'ui_001',
     name: 'myui001',
     path: B.path.join(appbundle_path, '/app1/uis/ui_001.json'),
     app: 'app1' },
  ui_002: 
   { id: 'ui_002',
     name: 'myui002',
     path: B.path.join(appbundle_path, '/app1/uis/ui_002.json'),
     app: 'app1' },
  ui_003: 
   { id: 'ui_003',
     name: 'myui003',
     path: B.path.join(appbundle_path, '/app2/uis/ui_003.json'),
     app: 'app2' },
  ui_004: 
   { id: 'ui_004',
     name: 'myui004',
     path: B.path.join(appbundle_path, '/app2/uis/ui_004.json'),
     app: 'app2' }
   };

    em.app__load_from_bundle$(appbundle_path, null).then(function() {
      assert.equal(_.isEqual(em.app_store.store.db, app_in_store), true);
      assert.equal(_.isEqual(em.graph_store.store.db, graph_in_store), true);
      assert.equal(_.isEqual(em.ui_store.store.db, ui_in_store), true);
      d();
    }).done();
  });

  
  //app__get$ test
  it("app_get app_ids is not array", function(d) {
    var app_ids = ["app1", "app2"];
    var count = 0;
    for (var i = 0; i < app_ids.length; i++) {
      (function(index) {
        em.app__get$(app_ids[index]).then(function(v) {
          count++;
          assert.equal(_.isEqual(v, app_in_store["app" + (index + 1)]), true);
          if (count === app_ids.length) {
            d();
          }
        }).done();
      })(i);
    }
  });

  it("app__get$ app_ids is array", function(d) {
    var app_get_value = [];
    app_get_value.push(app_in_store.app1);
    app_get_value.push(app_in_store.app2);
    var app_id_arr = ["app1", "app2"];
    em.app__get$(app_id_arr).then(function(v) {
      assert.equal(_.isEqual(v, app_get_value), true);
      d();
    }).done();
  });


  //app__list$ test
  it("app__list$", function(d) {
    var max_length = 1;
    if (max_length > 2)
      max_length = 2;
    var app_list_value = [];
    for (var i = 0; i < max_length; i++) {
      app_list_value.push(app_in_store["app" + (i + 1)]);
    }
    em.app__list$(max_length).then(function(v) {
      assert.equal(_.isEqual(v, app_list_value), true);
      d();
    }).done();
  });

  //app_list_graphs$ test
  it("app__list_graphs$", function(d) {

    var app1_graphs = [ 
    { id: 'graph_001',
      name: 'mygraph001',
      app: 'app1',
      path: B.path.join(appbundle_path, '/app1/graphs/graph_001.json') 
    },
    { id: 'graph_002',
      name: 'mygraph002',
      app: 'app1',
      path: B.path.join(appbundle_path, '/app1/graphs/graph_002.json') 
    } ];

    var app2_graphs = [ 
    { id: 'graph_003',
      name: 'mygraph003',
      app: 'app2',
      path: B.path.join(appbundle_path, '/app2/graphs/graph_003.json') 
    },
    { id: 'graph_004',
      name: 'mygraph004',
      app: 'app2',
      path: B.path.join(appbundle_path, '/app2/graphs/graph_004.json') 
    }];

    var app_graphs_arr = [app1_graphs, app2_graphs];


    var graphs_of_app_id = ["app1", "app2"];
    var count = 0;
    for (var i = 0; i < graphs_of_app_id.length; i++) {
      (function(index) {
        em.app__list_graphs$(graphs_of_app_id[index]).then(function(v) {
          count++;
          assert.equal(_.isEqual(v, app_graphs_arr[index]), true);
          if (count === graphs_of_app_id.length)
            d();
        }).done(); 
      })(i);
    }
  });
  
  //app__list_uis$ test
  it("app__list_uis$", function(d) {
    var app1_uis = [ 
    { id: 'ui_001',
      name: 'myui001',
      path: B.path.join(appbundle_path, '/app1/uis/ui_001.json'),
      app: 'app1' 
    },
    { id: 'ui_002',
      name: 'myui002',
      path: B.path.join(appbundle_path, '/app1/uis/ui_002.json'),
      app: 'app1' 
    }];

    var app2_uis = [ 
    { id: 'ui_003',
      name: 'myui003',
      path: B.path.join(appbundle_path, '/app2/uis/ui_003.json'),
      app: 'app2' 
    },
    { id: 'ui_004',
      name: 'myui004',
      path: B.path.join(appbundle_path, '/app2/uis/ui_004.json'),
      app: 'app2' 
    }];

    var app_uis_arr = [app1_uis, app2_uis];
    var uis_of_app_id = ["app1", "app2"];

    var count = 0;
    for (var i = 0; i < uis_of_app_id.length; i++) {
      (function(index) {
        em.app__list_uis$(uis_of_app_id[index]).then(function(v) {
          count++;
          assert.equal(_.isEqual(v, app_uis_arr[index]), true);
          if (count === uis_of_app_id.length)
            d();
        }).done();
      })(i);
    }
  });



  //graph add and remove test
  var graph_add_arr = [
    {
      id: "new_graph_1",
      name: "newgraph1",
      app: "app1"
    },
    {
      id: "new_graph_2",
      name: "newgraph2",
      app: "app1"
    },
    {
      id: "new_graph_3",
      name: "newgraph3",
      app: "app2"
    }
  ];

  var graph_add_0 = {
      id: "new_graph_1",
      name: "newgraph1",
      app: "app1",
      path: B.path.join(appbundle_path, '/app1/graphs/new_graph_1.json')
    };

  var graph_add_1 = {
    id: 'new_graph_2',
    name: 'newgraph2',
    app: 'app1',
     path: B.path.join(appbundle_path, '/app1/graphs/new_graph_2.json') 
   };
  var graph_add_2 = {
    id: 'new_graph_3',
    name: 'newgraph3',
    app: 'app2',
    path: B.path.join(appbundle_path, '/app2/graphs/new_graph_3.json')
  };

  var graph_add_parray = [graph_add_0, graph_add_1, graph_add_2];



  it("graph__add$", function(d) {
    var count = 0;
    var graph_in_store_ch = _.clone(graph_in_store);
    
    for (var i = 0; i < graph_add_arr.length; i++) {
      (function(index) {
        em.graph__add$(graph_add_arr[index]).then(function() {
        count++;

        graph_in_store_ch[graph_add_arr[index].id] = _.clone(graph_add_parray[index]);
        assert.equal(_.isEqual(em.graph_store.store.db, graph_in_store_ch), true);
        if (count === graph_add_arr.length)
          d();
        }).done();
      })(i);
    }
  });

  it("graph__remove$", function(d) {
    var count = 0;
    for (var i = graph_add_arr.length - 1; i >= 0; i--) {
      (function(index) {
        em.graph__remove$(graph_add_arr[index].id).then(function() {
          count++;
          var graph_in_store_ch = _.clone(graph_in_store);
          for (var j = 0; j < index; j++) {
            graph_in_store_ch[graph_add_arr[j].id] = _.clone(graph_add_parray[j]);
          }
          assert.equal(_.isEqual(em.graph_store.store.db, graph_in_store_ch), true);         
          if (count === graph_add_arr.length)
            d();
        }).done();
      })(i);
    }
  });




  //graph__get$ test
  it("graph__get$", function(d) {
    var graphs_arr = [
      { id: 'graph_001',
        name: 'mygraph001',
        app: 'app1',
        path: B.path.join(appbundle_path, '/app1/graphs/graph_001.json')
      },
      { id: 'graph_002',
        name: 'mygraph002',
        app: 'app1',
        path: B.path.join(appbundle_path, '/app1/graphs/graph_002.json')
      },
      { id: 'graph_003',
        name: 'mygraph003',
        app: 'app2',
        path: B.path.join(appbundle_path, '/app2/graphs/graph_003.json') 
      },
      { id: 'graph_004',
        name: 'mygraph004',
        app: 'app2',
        path: B.path.join(appbundle_path, '/app2/graphs/graph_004.json') 
      }
    ];

    var graphs_id = ["graph_001", "graph_002", "graph_003", "graph_004"];
    var count = 0;
    for (var i = 0; i < graphs_id.length; i++) {
      (function(index) {
        em.graph__get$(graphs_id[index]).then(function(v) {
          count++;
          assert.equal(_.isEqual(v, graphs_arr[index]), true);
          if (count === graphs_id.length) 
            d();
        }).done();
      })(i);
    }
  });

  //graph__list$ test
  it("graph__list$", function(d) {
    var graph_list_value = [ 
      { id: 'graph_001',
        name: 'mygraph001',
        app: 'app1',
        path: B.path.join(appbundle_path, '/app1/graphs/graph_001.json') 
      },
      { id: 'graph_002',
        name: 'mygraph002',
        app: 'app1',
        path: B.path.join(appbundle_path, '/app1/graphs/graph_002.json') 
      },
      { id: 'graph_003',
        name: 'mygraph003',
        app: 'app2',
        path: B.path.join(appbundle_path, '/app2/graphs/graph_003.json') 
      },
      { id: 'graph_004',
        name: 'mygraph004',
        app: 'app2',
        path: B.path.join(appbundle_path, '/app2/graphs/graph_004.json') 
      }];

      var max_length = 2;
      if (max_length > graph_list_value.length) {
        max_length = graph_list_value.length;
      }
      em.graph__list$(max_length).then(function(v) {
        assert.equal(_.isEqual(v, graph_list_value.slice(0, max_length)), true);
        d();
    }).done();
  });

  //graph__get_app$ test
  it("graph__get_app$", function(d) {
    var graphs_id = ["graph_001", "graph_002", "graph_003", "graph_004"];
    var count = 0;
    var app_values = [
      { id: 'app1',
        name: 'myapp1',
        //description: undefined,
        path: B.path.join(appbundle_path, '/app1'),
       // main_ui: undefined,
        graphs: [ 'graph_001', 'graph_002' ],
        uis: [ 'ui_001', 'ui_002' ],
        is_builtin: false 
      },
      { id: 'app1',
        name: 'myapp1',
        //description: undefined,
        path: B.path.join(appbundle_path, '/app1'),
        //main_ui: undefined,
        graphs: [ 'graph_001', 'graph_002' ],
        uis: [ 'ui_001', 'ui_002' ],
        is_builtin: false 
      },
      { id: 'app2',
        name: 'myapp2',
        //description: undefined,
        path: B.path.join(appbundle_path, '/app2'),
        //main_ui: undefined,
        graphs: [ 'graph_003', 'graph_004' ],
        uis: [ 'ui_003', 'ui_004' ],
        is_builtin: false
      },
      { id: 'app2',
        name: 'myapp2',
        //description: undefined,
        path: B.path.join(appbundle_path, '/app2'),
        //main_ui: undefined,
        graphs: [ 'graph_003', 'graph_004' ],
        uis: [ 'ui_003', 'ui_004' ],
        is_builtin: false
      }
    ];

    for (var i = 0; i < graphs_id.length; i++) {
      (function(index) {
        em.graph__get_app$(graphs_id[index]).then(function(v) {
          count++;
          assert.equal(_.isEqual(v, app_values[index]), true);
          if (count === graphs_id.length) {
            d();
          }
        }).done();
      })(i);
    }
  });


  
  //ui add and remove test
  var ui_add_arr = [
    {
      id: "new_ui_1",
      name: "newui1",
      app: "app1"
    },
    {
      id: "new_ui_2",
      name: "newui2",
      app: "app1"
    },
    {
      id: "new_ui_3",
      name: "newui3",
      app: "app2"
    }
  ];

  var ui_add_0 = {
      id: "new_ui_1",
      name: "newui1",
      app: "app1",
      path: B.path.join(appbundle_path, '/app1/uis/new_ui_1.json')
    };

  var ui_add_1 = {
    id: 'new_ui_2',
    name: 'newui2',
    app: 'app1',
     path: B.path.join(appbundle_path, '/app1/uis/new_ui_2.json') 
   };
  var ui_add_2 = {
    id: 'new_ui_3',
    name: 'newui3',
    app: 'app2',
    path: B.path.join(appbundle_path, '/app2/uis/new_ui_3.json')
  };

  var ui_add_parray = [ui_add_0, ui_add_1, ui_add_2];

  it("ui__add$", function(d) {
    var count = 0;
    var ui_in_store_ch = _.clone(ui_in_store);
    
    for (var i = 0; i < ui_add_arr.length; i++) {
      (function(index) {
        em.ui__add$(ui_add_arr[index]).then(function() {
        count++;

        ui_in_store_ch[ui_add_arr[index].id] = _.clone(ui_add_parray[index]);
        assert.equal(_.isEqual(em.ui_store.store.db, ui_in_store_ch), true);
        if (count === ui_add_arr.length)
          d();
        }).done();
      })(i);
    }
  });

  it("ui__remove$", function(d) {
    var count = 0;
    for (var i = ui_add_arr.length - 1; i >= 0; i--) {
      (function(index) {
        em.ui__remove$(ui_add_arr[index].id).then(function() {
          count++;
          var ui_in_store_ch = _.clone(ui_in_store);
          for (var j = 0; j < index; j++) {
            ui_in_store_ch[ui_add_arr[j].id] = _.clone(ui_add_parray[j]);
          }
          assert.equal(_.isEqual(em.ui_store.store.db, ui_in_store_ch), true);         
          if (count === ui_add_arr.length)
            d();
        }).done();
      })(i);
    }
  });

  //ui__get$ test
  it("ui__get$", function(d) {
    var ui_arr = [
      { id: 'ui_001',
        name: 'myui001',
        app: 'app1',
        path: B.path.join(appbundle_path, '/app1/uis/ui_001.json')
      },
      { id: 'ui_002',
        name: 'myui002',
        app: 'app1',
        path: B.path.join(appbundle_path, '/app1/uis/ui_002.json')
      },
      { id: 'ui_003',
        name: 'myui003',
        app: 'app2',
        path: B.path.join(appbundle_path, '/app2/uis/ui_003.json') 
      },
      { id: 'ui_004',
        name: 'myui004',
        app: 'app2',
        path: B.path.join(appbundle_path, '/app2/uis/ui_004.json') 
      }
    ];

    var uis_id = ["ui_001", "ui_002", "ui_003", "ui_004"];
    var count = 0;
    for (var i = 0; i < uis_id.length; i++) {
      (function(index) {
        em.ui__get$(uis_id[index]).then(function(v) {
          count++;
          assert.equal(_.isEqual(v, ui_arr[index]), true);
          if (count === uis_id.length) 
            d();
        }).done();
      })(i);
    }
  });

  //ui__list$ test
  it("ui__list$", function(d) {
    var ui_list_value = [ 
      { id: 'ui_001',
        name: 'myui001',
        app: 'app1',
        path: B.path.join(appbundle_path, '/app1/uis/ui_001.json') 
      },
      { id: 'ui_002',
        name: 'myui002',
        app: 'app1',
        path: B.path.join(appbundle_path, '/app1/uis/ui_002.json') 
      },
      { id: 'ui_003',
        name: 'myui003',
        app: 'app2',
        path: B.path.join(appbundle_path, '/app2/uis/ui_003.json') 
      },
      { id: 'ui_004',
        name: 'myui004',
        app: 'app2',
        path: B.path.join(appbundle_path, '/app2/uis/ui_004.json') 
      }];

      var max_length = 2;
      if (max_length > ui_list_value.length) {
        max_length = ui_list_value.length;
      }
      em.ui__list$(max_length).then(function(v) {
        assert.equal(_.isEqual(v, ui_list_value.slice(0, max_length)), true);
        d();
    }).done();
  });

  //ui__get_app$ test
  it("ui__get_app$", function(d) {
    var ui_id = ["ui_001", "ui_002", "ui_003", "ui_004"];
    var count = 0;
    var app_values = [
      { id: 'app1',
        name: 'myapp1',
        //description: undefined,
        path: B.path.join(appbundle_path, '/app1'),
        //main_ui: undefined,
        graphs: [ 'graph_001', 'graph_002' ],
        uis: [ 'ui_001', 'ui_002' ],
        is_builtin: false 
      },
      { id: 'app1',
        name: 'myapp1',
        //description: undefined,
        path: B.path.join(appbundle_path, '/app1'),
        uis: [ 'ui_001', 'ui_002' ],
        //main_ui: undefined,
        graphs: [ 'graph_001', 'graph_002' ],
        is_builtin: false 
      },
      { id: 'app2',
        name: 'myapp2',
        //description: undefined,
        path: B.path.join(appbundle_path, '/app2'),
        //main_ui: undefined,
        graphs: [ 'graph_003', 'graph_004' ],
        uis: [ 'ui_003', 'ui_004' ],
        is_builtin: false
      },
      { id: 'app2',
        name: 'myapp2',
        //description: undefined,
        path: B.path.join(appbundle_path, '/app2'),
        //main_ui: undefined,
        graphs: [ 'graph_003', 'graph_004' ],
        uis: [ 'ui_003', 'ui_004' ],
        is_builtin: false
      }
    ];

    for (var i = 0; i < ui_id.length; i++) {
      (function(index) {
        em.ui__get_app$(ui_id[index]).then(function(v) {
          count++;
          assert.equal(_.isEqual(v, app_values[index]), true);
          if (count === ui_id.length) {
            d();
          }
        }).done();
      })(i);
    }
  });
});