IGNITE-3262 Added new tests.

Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/7699bc90
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/7699bc90
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/7699bc90

Branch: refs/heads/ignite-3262
Commit: 7699bc90db226f9f2f4682d7135a3258829d874b
Parents: 695096a
Author: Maxim Afanasiev <[email protected]>
Authored: Mon Jun 27 15:08:39 2016 +0700
Committer: Maxim Afanasiev <[email protected]>
Committed: Mon Jun 27 15:08:39 2016 +0700

----------------------------------------------------------------------
 modules/web-console/src/main/js/package.json    |   4 +-
 .../src/main/js/serve/middlewares/api.js        |  17 ++-
 .../src/main/js/serve/routes/caches.js          |  54 ++------
 .../src/main/js/serve/services/cache.js         | 126 ++++++++++++++-----
 .../js/test/backend/unit/CacheService.test.js   |  53 ++++++--
 5 files changed, 160 insertions(+), 94 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/7699bc90/modules/web-console/src/main/js/package.json
----------------------------------------------------------------------
diff --git a/modules/web-console/src/main/js/package.json 
b/modules/web-console/src/main/js/package.json
index a1f395f..4e815fb 100644
--- a/modules/web-console/src/main/js/package.json
+++ b/modules/web-console/src/main/js/package.json
@@ -7,8 +7,8 @@
     "dev": "cross-env NODE_ENV=development gulp watch",
     "build": "cross-env NODE_ENV=production gulp build",
     "test": "karma start karma.conf.js",
-    "ci-test-backend": "cross-env NODE_ENV=test mocha -u tdd --require 
babel-core/register --reporter mocha-teamcity-reporter --recursive 
./test/backend/unit",
-    "test-backend": "cross-env NODE_ENV=test mocha -u tdd --require 
babel-core/register  --recursive ./test/backend/unit"
+    "ci-test-backend": "cross-env NODE_ENV=test 
CONFIG_PATH='./test/backend/config/settings.json' mocha -u tdd --require 
babel-core/register --reporter mocha-teamcity-reporter --recursive 
./test/backend/unit",
+    "test-backend": "cross-env NODE_ENV=test 
CONFIG_PATH='./test/backend/config/settings.json' mocha -u tdd --require 
babel-core/register  --recursive ./test/backend/unit"
   },
   "author": "",
   "contributors": [

http://git-wip-us.apache.org/repos/asf/ignite/blob/7699bc90/modules/web-console/src/main/js/serve/middlewares/api.js
----------------------------------------------------------------------
diff --git a/modules/web-console/src/main/js/serve/middlewares/api.js 
b/modules/web-console/src/main/js/serve/middlewares/api.js
index 9d19d3b..d8565e4 100644
--- a/modules/web-console/src/main/js/serve/middlewares/api.js
+++ b/modules/web-console/src/main/js/serve/middlewares/api.js
@@ -19,26 +19,25 @@
 
 // Fire me up!
 
-function sendServerError(error) {
-    error.httpCode = 500;
+const sendServerError = (err) => {
+    err.httpCode = 500;
 
-    this.api.error(error)
-}
+    this.api.error(err)
+};
 
-function sendError(err) {
+const sendError = (err) => {
     // TODO: removed code from error
     this.status(err.httpCode || err.code || 500).send(err.message);
-}
+};
 
-function sendOk(data) {
+const sendOk = (data) => {
     this.status(200).json(data);
-}
+};
 
 module.exports = {
     implements: 'middlewares/api',
     factory: () => {
         return (req, res, next) => {
-            //Only prepare send functions
             res.api = {
                 error: sendError.bind(res),
                 ok: sendOk.bind(res),

http://git-wip-us.apache.org/repos/asf/ignite/blob/7699bc90/modules/web-console/src/main/js/serve/routes/caches.js
----------------------------------------------------------------------
diff --git a/modules/web-console/src/main/js/serve/routes/caches.js 
b/modules/web-console/src/main/js/serve/routes/caches.js
index 39a30a7..784d872 100644
--- a/modules/web-console/src/main/js/serve/routes/caches.js
+++ b/modules/web-console/src/main/js/serve/routes/caches.js
@@ -35,33 +35,9 @@ module.exports.factory = function(_, express, mongo, 
cacheService) {
          * @param res Response.
          */
         router.post('/list', (req, res) => {
-            const result = {};
-            let spaceIds = [];
-
-            // Get owned space and all accessed space.
-            mongo.spaces(req.currentUserId(), req.header('IgniteDemoMode'))
-                .then((spaces) => {
-                    result.spaces = spaces;
-                    spaceIds = spaces.map((space) => space._id);
-
-                    return mongo.Cluster.find({space: {$in: 
spaceIds}}).sort('name').lean().exec();
-                })
-                .then((clusters) => {
-                    result.clusters = clusters;
-
-                    return mongo.DomainModel.find({space: {$in: 
spaceIds}}).sort('name').lean().exec();
-                })
-                .then((domains) => {
-                    result.domains = domains;
-
-                    return mongo.Cache.find({space: {$in: 
spaceIds}}).sort('name').lean().exec();
-                })
-                .then((caches) => {
-                    result.caches = caches;
-
-                    res.json(result);
-                })
-                .catch((err) => mongo.handleError(res, err));
+            cacheService.listByUser(req.currentUserId(), 
req.header('IgniteDemoMode'))
+                .then(res.api.ok)
+                .catch(req.api.error);
         });
 
         /**
@@ -70,7 +46,7 @@ module.exports.factory = function(_, express, mongo, 
cacheService) {
         router.post('/save', (req, res) => {
             const cache = req.body;
 
-            cacheService.save(cache)
+            cacheService.merge(cache)
                 .then(res.api.ok)
                 .catch(res.api.error);
         });
@@ -79,28 +55,20 @@ module.exports.factory = function(_, express, mongo, 
cacheService) {
          * Remove cache by ._id.
          */
         router.post('/remove', (req, res) => {
-            const params = req.body;
-            const cacheId = params._id;
+            const cache = req.body;
 
-            mongo.Cluster.update({caches: {$in: [cacheId]}}, {$pull: {caches: 
cacheId}}, {multi: true}).exec()
-                .then(() => mongo.DomainModel.update({caches: {$in: 
[cacheId]}}, {$pull: {caches: cacheId}}, {multi: true}).exec())
-                .then(() => mongo.Cache.remove(params).exec())
-                .then(() => res.sendStatus(200))
-                .catch((err) => mongo.handleError(res, err));
+            cacheService.remove(cache)
+                .then(res.api.ok)
+                .catch(res.api.error);
         });
 
         /**
          * Remove all caches.
          */
         router.post('/remove/all', (req, res) => {
-            mongo.spaceIds(req.currentUserId(), req.header('IgniteDemoMode'))
-                .then((spaceIds) =>
-                    mongo.Cluster.update({space: {$in: spaceIds}}, {caches: 
[]}, {multi: true}).exec()
-                        .then(() => mongo.DomainModel.update({space: {$in: 
spaceIds}}, {caches: []}, {multi: true}).exec())
-                        .then(() => mongo.Cache.remove({space: {$in: 
spaceIds}}).exec())
-                )
-                .then(() => res.sendStatus(200))
-                .catch((err) => mongo.handleError(res, err));
+            cacheService.removeAll(req.currentUserId(), 
req.header('IgniteDemoMode'))
+                .then(res.api.ok)
+                .catch(res.api.error);
         });
 
         factoryResolve(router);

http://git-wip-us.apache.org/repos/asf/ignite/blob/7699bc90/modules/web-console/src/main/js/serve/services/cache.js
----------------------------------------------------------------------
diff --git a/modules/web-console/src/main/js/serve/services/cache.js 
b/modules/web-console/src/main/js/serve/services/cache.js
index fdccacb..ff867ff 100644
--- a/modules/web-console/src/main/js/serve/services/cache.js
+++ b/modules/web-console/src/main/js/serve/services/cache.js
@@ -27,35 +27,99 @@ module.exports = {
         'errors']
 };
 
-module.exports.factory = function (_, express, mongo, errors) {
-    const save = (cache) => {
-        return mongo.Cache.findOne({space: cache.space, name: 
cache.name}).exec()
-            .then((existingCache) => {
-                const cacheId = cache._id;
-
-                if (existingCache && cacheId !== existingCache._id.toString())
-                    throw new errors.DuplicateKeyException('Cache with name: 
"' + existingCache.name + '" already exist.');
-
-                if (cacheId) {
-                    return mongo.Cache.update({_id: cacheId}, cache, {upsert: 
true}).exec()
-                        .then(() => mongo.Cluster.update({_id: {$in: 
cache.clusters}}, {$addToSet: {caches: cacheId}}, {multi: true}).exec())
-                        .then(() => mongo.Cluster.update({_id: {$nin: 
cache.clusters}}, {$pull: {caches: cacheId}}, {multi: true}).exec())
-                        .then(() => mongo.DomainModel.update({_id: {$in: 
cache.domains}}, {$addToSet: {caches: cacheId}}, {multi: true}).exec())
-                        .then(() => mongo.DomainModel.update({_id: {$nin: 
cache.domains}}, {$pull: {caches: cacheId}}, {multi: true}).exec())
-                        .then(() => cacheId);
-                }
-
-                // TODO: Replace to mongo.Cache.create()
-                return (new mongo.Cache(cache)).save()
-                    .then((cache) =>
-                        mongo.Cluster.update({_id: {$in: cache.clusters}}, 
{$addToSet: {caches: cache._id}}, {multi: true}).exec()
-                            .then(() => mongo.DomainModel.update({_id: {$in: 
cache.domains}}, {$addToSet: {caches: cache._id}}, {multi: true}).exec())
-                            .then(() => cache._id)
-                    );
-            });
-    };
-
-    return {
-        save
-    };
+module.exports.factory = (_, express, mongo, errors) => {
+
+    class CacheService {
+                
+        static update(cache) {
+            const cacheId = cache._id;
+
+            return mongo.Cache.update({_id: cacheId}, cache, {upsert: 
true}).exec()
+                .then(() => mongo.Cluster.update({_id: {$in: cache.clusters}}, 
{$addToSet: {caches: cacheId}}, {multi: true}).exec())
+                .then(() => mongo.Cluster.update({_id: {$nin: 
cache.clusters}}, {$pull: {caches: cacheId}}, {multi: true}).exec())
+                .then(() => mongo.DomainModel.update({_id: {$in: 
cache.domains}}, {$addToSet: {caches: cacheId}}, {multi: true}).exec())
+                .then(() => mongo.DomainModel.update({_id: {$nin: 
cache.domains}}, {$pull: {caches: cacheId}}, {multi: true}).exec())
+                .then(() => cacheId);
+        }
+
+        static create(cache) {
+            // TODO: Replace to mongo.Cache.create()
+            return (new mongo.Cache(cache)).save()
+                .then((cache) =>
+                    mongo.Cluster.update({_id: {$in: cache.clusters}}, 
{$addToSet: {caches: cache._id}}, {multi: true}).exec()
+                        .then(() => mongo.DomainModel.update({_id: {$in: 
cache.domains}}, {$addToSet: {caches: cache._id}}, {multi: true}).exec())
+                        .then(() => cache._id)
+                )
+        }
+
+        static merge(cache) {
+            return this.loadByName(cache)
+                .then((existingCache) => {
+                    const cacheId = cache._id;
+
+                    if (existingCache && cacheId !== 
existingCache._id.toString())
+                        throw new errors.DuplicateKeyException('Cache with 
name: "' + existingCache.name + '" already exist.');
+
+                    if (cacheId) {
+                        return this.update(cache);
+                    }
+
+                    return this.create(cache);
+                });
+        }
+
+        static loadByName(cache) {
+            return mongo.Cache.findOne({space: cache.space, name: 
cache.name}).exec();
+        };
+
+        static listByUser(userId, demo) {
+            const result = {};
+            let spaceIds = [];
+
+            // Get owned space and all accessed space.
+            return mongo.spaces(userId, demo)
+                .then((spaces) => {
+                    result.spaces = spaces;
+                    spaceIds = spaces.map((space) => space._id);
+
+                    return mongo.Cluster.find({space: {$in: 
spaceIds}}).sort('name').lean().exec();
+                })
+                .then((clusters) => {
+                    result.clusters = clusters;
+
+                    return mongo.DomainModel.find({space: {$in: 
spaceIds}}).sort('name').lean().exec();
+                })
+                .then((domains) => {
+                    result.domains = domains;
+
+                    return mongo.Cache.find({space: {$in: 
spaceIds}}).sort('name').lean().exec();
+                })
+                .then((caches) => {
+                    result.caches = caches;
+
+                    return result;
+                })
+        }
+
+        static remove(cache) {
+            const cacheId = cache._id;
+
+            return mongo.Cluster.update({caches: {$in: [cacheId]}}, {$pull: 
{caches: cacheId}}, {multi: true}).exec()
+                .then(() => mongo.DomainModel.update({caches: {$in: 
[cacheId]}}, {$pull: {caches: cacheId}}, {multi: true}).exec())
+                .then(() => mongo.Cache.remove(cache).exec())
+                .then(() => ({}))
+        };
+
+        static removeAll(user, demo) {
+            return mongo.spaceIds(user, demo)
+                .then((spaceIds) =>
+                    mongo.Cluster.update({space: {$in: spaceIds}}, {caches: 
[]}, {multi: true}).exec()
+                        .then(() => mongo.DomainModel.update({space: {$in: 
spaceIds}}, {caches: []}, {multi: true}).exec())
+                        .then(() => mongo.Cache.remove({space: {$in: 
spaceIds}}).exec())
+                        .then(() => ({}))
+                );
+        };
+    }
+
+    return CacheService;
 };

http://git-wip-us.apache.org/repos/asf/ignite/blob/7699bc90/modules/web-console/src/main/js/test/backend/unit/CacheService.test.js
----------------------------------------------------------------------
diff --git 
a/modules/web-console/src/main/js/test/backend/unit/CacheService.test.js 
b/modules/web-console/src/main/js/test/backend/unit/CacheService.test.js
index ffa416f..8d9f059 100644
--- a/modules/web-console/src/main/js/test/backend/unit/CacheService.test.js
+++ b/modules/web-console/src/main/js/test/backend/unit/CacheService.test.js
@@ -29,16 +29,17 @@ suite('CacheService', () => {
                 mongo = _mongo;
                 cacheService = _cacheService;
                 errors = _errors;
-            })
-            .then(() => {
-                return Promise.all([
-                    mongo.Cache.remove().exec()
-                ])
             });
     });
 
-    test('Cache save', (done) => {
-        return cacheService.save(testCaches[0])
+    setup(() => {
+        return Promise.all([
+            mongo.Cache.remove().exec()
+        ]);
+    });
+
+    test('Cache merge', (done) => {
+        return cacheService.merge(testCaches[0])
             .then((cacheId)=> {
                 assert.isNotNull(cacheId);
 
@@ -55,12 +56,46 @@ suite('CacheService', () => {
     });
 
     test('double save same cache', (done) => {
-        return cacheService.save(testCaches[0])
-            .then(() => cacheService.save(testCaches[0]))
+        return cacheService.merge(testCaches[0])
+            .then(() => cacheService.merge(testCaches[0]))
             .catch((err) => {
                 assert.instanceOf(err, errors.DuplicateKeyException);
 
                 done()
             })
     });
+
+    test('Remove cache', (done)=> {
+        return cacheService.merge(testCaches[0])
+            .then((cacheId)=> {
+
+                assert.isNotNull(cacheId);
+
+                return mongo.Cache.findById(cacheId)
+                    .then((cache)=> {
+                        assert.isNotNull(cache);
+
+                        return cache;
+                    })
+                    .then(cacheService.remove)
+                    .then(() => {
+                        return mongo.Cache.findById(cacheId)
+                            .then((notFoundCache) => {
+                                assert.isNull(notFoundCache);
+                            })
+                    })
+            })
+            .then(done)
+            .catch(done);
+    });
+
+    test('Remove all caches for user', (done) => {
+        return Promise.all([cacheService.merge(testCaches[0]), 
cacheService.merge(testCaches[1])])
+            .then((results) => {
+
+            })
+            .then(done)
+            .catch(done);
+    });
+
 });
\ No newline at end of file

Reply via email to