IGNITE-3262 Refactored tests.
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/701417a6 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/701417a6 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/701417a6 Branch: refs/heads/ignite-3262 Commit: 701417a6d2b1f41e9199af0edd00dcb52fd68649 Parents: ffdf3d9 Author: Maxim Afanasiev <[email protected]> Authored: Thu Jun 30 12:58:21 2016 +0700 Committer: Maxim Afanasiev <[email protected]> Committed: Thu Jun 30 12:58:21 2016 +0700 ---------------------------------------------------------------------- .../js/serve/errors/DuplicateKeyException.js | 2 +- .../src/main/js/serve/middlewares/api.js | 2 +- modules/web-console/src/main/js/serve/mongo.js | 14 ++++++++--- .../src/main/js/serve/services/cache.js | 26 ++++++-------------- .../js/test/backend/unit/CacheService.test.js | 13 ++++++++-- 5 files changed, 32 insertions(+), 25 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/701417a6/modules/web-console/src/main/js/serve/errors/DuplicateKeyException.js ---------------------------------------------------------------------- diff --git a/modules/web-console/src/main/js/serve/errors/DuplicateKeyException.js b/modules/web-console/src/main/js/serve/errors/DuplicateKeyException.js index 80b0147..b228d0c 100644 --- a/modules/web-console/src/main/js/serve/errors/DuplicateKeyException.js +++ b/modules/web-console/src/main/js/serve/errors/DuplicateKeyException.js @@ -17,7 +17,7 @@ 'use strict'; -import AppErrorException from './AppError'; +import AppErrorException from './AppErrorException'; class DuplicateKeyException extends AppErrorException { constructor(message) { http://git-wip-us.apache.org/repos/asf/ignite/blob/701417a6/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 2607b97..9e6ce8f 100644 --- a/modules/web-console/src/main/js/serve/middlewares/api.js +++ b/modules/web-console/src/main/js/serve/middlewares/api.js @@ -22,7 +22,7 @@ function sendServerError(err) { err.httpCode = 500; - this.api.error(err) + this.api.error(err); } function sendError(err) { http://git-wip-us.apache.org/repos/asf/ignite/blob/701417a6/modules/web-console/src/main/js/serve/mongo.js ---------------------------------------------------------------------- diff --git a/modules/web-console/src/main/js/serve/mongo.js b/modules/web-console/src/main/js/serve/mongo.js index 50aca10..8c7cfaa 100644 --- a/modules/web-console/src/main/js/serve/mongo.js +++ b/modules/web-console/src/main/js/serve/mongo.js @@ -78,6 +78,10 @@ module.exports.factory = function(deepPopulatePlugin, passportMongo, settings, p } }); + result.errCodes = { + DUPLICATE_KEY_ERROR: 11000, + DUPLICATE_KEY_UPDATE_ERROR: 11001 + }; // Define Account model. result.Account = mongoose.model('Account', AccountSchema); @@ -129,8 +133,8 @@ module.exports.factory = function(deepPopulatePlugin, passportMongo, settings, p // Define Cache schema. const CacheSchema = new Schema({ - space: {type: ObjectId, ref: 'Space', index: true}, - name: String, + space: {type: ObjectId, ref: 'Space', index: true, required: true}, + name: {type: String, required: true}, clusters: [{type: ObjectId, ref: 'Cluster'}], domains: [{type: ObjectId, ref: 'DomainModel'}], cacheMode: {type: String, enum: ['PARTITIONED', 'REPLICATED', 'LOCAL']}, @@ -212,7 +216,7 @@ module.exports.factory = function(deepPopulatePlugin, passportMongo, settings, p writeBehindFlushFrequency: Number, writeBehindFlushThreadCount: Number, - invalidate: Boolean, + // invalidate: Boolean, defaultLockTimeout: Number, atomicWriteOrderMode: {type: String, enum: ['CLOCK', 'PRIMARY']}, writeSynchronizationMode: {type: String, enum: ['FULL_SYNC', 'FULL_ASYNC', 'PRIMARY_SYNC']}, @@ -253,6 +257,8 @@ module.exports.factory = function(deepPopulatePlugin, passportMongo, settings, p demo: Boolean }); + CacheSchema.index({name: 1, space: 1}, {unique: true}); + // Install deep populate plugin. CacheSchema.plugin(deepPopulate, { whitelist: ['domains'] @@ -641,6 +647,8 @@ module.exports.factory = function(deepPopulatePlugin, passportMongo, settings, p res.status(err.code || 500).send(err.message); }; + + // TODO Remove after refactoring spaceService. /** * Query for user spaces. * http://git-wip-us.apache.org/repos/asf/ignite/blob/701417a6/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 c097f78..c4aa39a 100644 --- a/modules/web-console/src/main/js/serve/services/cache.js +++ b/modules/web-console/src/main/js/serve/services/cache.js @@ -63,6 +63,10 @@ module.exports.factory = (_, mongo, errors) => { .then(() => mongo.DomainModel.update({_id: {$in: createdCache.domains}}, {$addToSet: {caches: createdCache._id}}, {multi: true}).exec()) .then(() => createdCache._id) ) + .catch((err) => { + if (err.code === mongo.errCodes.DUPLICATE_KEY_ERROR) + throw new errors.DuplicateKeyException('Cache with name: "' + cache.name + '" already exist.'); + }); }; /** @@ -78,14 +82,8 @@ module.exports.factory = (_, mongo, errors) => { }; /** - * Load cache by its name and space. - * @param {Object} cache - The cache object for load. - * @returns {Promise.<mongo.Cache>|null} that resolves cache if its exits, or null if not exitst. + * Service for manipulate Cache entities. */ - const loadExistingCache = (cache) => { - return mongo.Cache.findOne({space: cache.space, name: cache.name}).exec(); - }; - class CacheService { /** * Create or update cache. @@ -93,18 +91,10 @@ module.exports.factory = (_, mongo, errors) => { * @returns {Promise.<Integer>} that resolves cache id of merge operation. */ static merge(cache) { - return loadExistingCache(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 (cache._id) + return update(cache); - if (cacheId) - return update(cache); - - return create(cache); - }); + return create(cache); } /** http://git-wip-us.apache.org/repos/asf/ignite/blob/701417a6/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 88f463a..b597ff0 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 @@ -26,7 +26,7 @@ let cacheService; let mongo; let errors; -suite('SpaceService', () => { +suite('CacheService', () => { const injectSpaceInCaches = (spaceId) => { return testCaches.map((cache) => ({...cache, space: spaceId})); @@ -64,12 +64,21 @@ suite('SpaceService', () => { .catch(done); }); - test('double save same cache', (done) => { + test('Try to save same cache twice.', (done) => { return cacheService.merge(testCaches[0]) .then(() => cacheService.merge(testCaches[0])) .catch((err) => { assert.instanceOf(err, errors.DuplicateKeyException); + done(); + }) + .then(done); + }); + test('Try to save null cache.', (done) => { + return cacheService.merge({}) + .then(done) + .catch((err) => { + assert.instanceOf(err, errors.DuplicateKeyException); done(); }); });
