mirror of
https://github.com/nasa/openmct.git
synced 2025-05-11 04:52:57 +00:00
Merge pull request #371 from nasa/open338
#338 [Copy] Copying an object with telemetry from the data dictionary results in a number of unknown objects in copied tree
This commit is contained in:
commit
cce415fc51
@ -102,6 +102,12 @@
|
|||||||
"implementation": "navigation/NavigationService.js"
|
"implementation": "navigation/NavigationService.js"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"policies": [
|
||||||
|
{
|
||||||
|
"implementation": "creation/CreationPolicy.js",
|
||||||
|
"category": "creation"
|
||||||
|
}
|
||||||
|
],
|
||||||
"actions": [
|
"actions": [
|
||||||
{
|
{
|
||||||
"key": "navigate",
|
"key": "navigate",
|
||||||
|
@ -69,7 +69,7 @@ define(
|
|||||||
|
|
||||||
// Introduce one create action per type
|
// Introduce one create action per type
|
||||||
return this.typeService.listTypes().filter(function (type) {
|
return this.typeService.listTypes().filter(function (type) {
|
||||||
return type.hasFeature("creation");
|
return self.policyService.allow("creation", type);
|
||||||
}).map(function (type) {
|
}).map(function (type) {
|
||||||
return new CreateAction(
|
return new CreateAction(
|
||||||
type,
|
type,
|
||||||
|
45
platform/commonUI/browse/src/creation/CreationPolicy.js
Normal file
45
platform/commonUI/browse/src/creation/CreationPolicy.js
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
/*****************************************************************************
|
||||||
|
* Open MCT Web, Copyright (c) 2014-2015, United States Government
|
||||||
|
* as represented by the Administrator of the National Aeronautics and Space
|
||||||
|
* Administration. All rights reserved.
|
||||||
|
*
|
||||||
|
* Open MCT Web is licensed under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0.
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
* License for the specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*
|
||||||
|
* Open MCT Web includes source code licensed under additional open source
|
||||||
|
* licenses. See the Open Source Licenses file (LICENSES.md) included with
|
||||||
|
* this source code distribution or the Licensing information page available
|
||||||
|
* at runtime from the About dialog for additional information.
|
||||||
|
*****************************************************************************/
|
||||||
|
/*global define*/
|
||||||
|
|
||||||
|
define(
|
||||||
|
[],
|
||||||
|
function () {
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A policy for determining whether objects of a given type can be
|
||||||
|
* created.
|
||||||
|
* @constructor
|
||||||
|
* @implements {Policy}
|
||||||
|
* @memberof platform/commonUI/browse
|
||||||
|
*/
|
||||||
|
function CreationPolicy() {
|
||||||
|
}
|
||||||
|
|
||||||
|
CreationPolicy.prototype.allow = function (type) {
|
||||||
|
return type.hasFeature("creation");
|
||||||
|
};
|
||||||
|
|
||||||
|
return CreationPolicy;
|
||||||
|
}
|
||||||
|
);
|
@ -33,6 +33,9 @@ define(
|
|||||||
var mockTypeService,
|
var mockTypeService,
|
||||||
mockDialogService,
|
mockDialogService,
|
||||||
mockCreationService,
|
mockCreationService,
|
||||||
|
mockPolicyService,
|
||||||
|
mockCreationPolicy,
|
||||||
|
mockPolicyMap = {},
|
||||||
mockTypes,
|
mockTypes,
|
||||||
provider;
|
provider;
|
||||||
|
|
||||||
@ -67,14 +70,32 @@ define(
|
|||||||
"creationService",
|
"creationService",
|
||||||
[ "createObject" ]
|
[ "createObject" ]
|
||||||
);
|
);
|
||||||
|
mockPolicyService = jasmine.createSpyObj(
|
||||||
|
"policyService",
|
||||||
|
[ "allow" ]
|
||||||
|
);
|
||||||
|
|
||||||
mockTypes = [ "A", "B", "C" ].map(createMockType);
|
mockTypes = [ "A", "B", "C" ].map(createMockType);
|
||||||
|
|
||||||
|
mockTypes.forEach(function(type){
|
||||||
|
mockPolicyMap[type.getName()] = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
mockCreationPolicy = function(type){
|
||||||
|
return mockPolicyMap[type.getName()];
|
||||||
|
};
|
||||||
|
|
||||||
|
mockPolicyService.allow.andCallFake(function(category, type){
|
||||||
|
return category === "creation" && mockCreationPolicy(type) ? true : false;
|
||||||
|
});
|
||||||
|
|
||||||
mockTypeService.listTypes.andReturn(mockTypes);
|
mockTypeService.listTypes.andReturn(mockTypes);
|
||||||
|
|
||||||
provider = new CreateActionProvider(
|
provider = new CreateActionProvider(
|
||||||
mockTypeService,
|
mockTypeService,
|
||||||
mockDialogService,
|
mockDialogService,
|
||||||
mockCreationService
|
mockCreationService,
|
||||||
|
mockPolicyService
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -94,15 +115,15 @@ define(
|
|||||||
|
|
||||||
it("does not expose non-creatable types", function () {
|
it("does not expose non-creatable types", function () {
|
||||||
// One of the types won't have the creation feature...
|
// One of the types won't have the creation feature...
|
||||||
mockTypes[1].hasFeature.andReturn(false);
|
mockPolicyMap[mockTypes[0].getName()] = false;
|
||||||
// ...so it should have been filtered out.
|
// ...so it should have been filtered out.
|
||||||
expect(provider.getActions({
|
expect(provider.getActions({
|
||||||
key: "create",
|
key: "create",
|
||||||
domainObject: {}
|
domainObject: {}
|
||||||
}).length).toEqual(2);
|
}).length).toEqual(2);
|
||||||
// Make sure it was creation which was used to check
|
// Make sure it was creation which was used to check
|
||||||
expect(mockTypes[1].hasFeature)
|
expect(mockPolicyService.allow)
|
||||||
.toHaveBeenCalledWith("creation");
|
.toHaveBeenCalledWith("creation", mockTypes[0]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
53
platform/commonUI/browse/test/creation/CreationPolicySpec.js
Normal file
53
platform/commonUI/browse/test/creation/CreationPolicySpec.js
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
/*****************************************************************************
|
||||||
|
* Open MCT Web, Copyright (c) 2014-2015, United States Government
|
||||||
|
* as represented by the Administrator of the National Aeronautics and Space
|
||||||
|
* Administration. All rights reserved.
|
||||||
|
*
|
||||||
|
* Open MCT Web is licensed under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0.
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
* License for the specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*
|
||||||
|
* Open MCT Web includes source code licensed under additional open source
|
||||||
|
* licenses. See the Open Source Licenses file (LICENSES.md) included with
|
||||||
|
* this source code distribution or the Licensing information page available
|
||||||
|
* at runtime from the About dialog for additional information.
|
||||||
|
*****************************************************************************/
|
||||||
|
/*global define,describe,it,expect,beforeEach,jasmine*/
|
||||||
|
|
||||||
|
define(
|
||||||
|
["../../src/creation/CreationPolicy"],
|
||||||
|
function (CreationPolicy) {
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
describe("The creation policy", function () {
|
||||||
|
var mockType,
|
||||||
|
policy;
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
mockType = jasmine.createSpyObj(
|
||||||
|
'type',
|
||||||
|
['hasFeature']
|
||||||
|
);
|
||||||
|
|
||||||
|
policy = new CreationPolicy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("allows creation of types with the creation feature", function () {
|
||||||
|
mockType.hasFeature.andReturn(true);
|
||||||
|
expect(policy.allow(mockType)).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("disallows creation of types without the creation feature", function () {
|
||||||
|
mockType.hasFeature.andReturn(false);
|
||||||
|
expect(policy.allow(mockType)).toBeFalsy();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
);
|
@ -8,6 +8,7 @@
|
|||||||
"creation/CreateMenuController",
|
"creation/CreateMenuController",
|
||||||
"creation/CreateWizard",
|
"creation/CreateWizard",
|
||||||
"creation/CreationService",
|
"creation/CreationService",
|
||||||
|
"creation/CreationPolicy",
|
||||||
"creation/LocatorController",
|
"creation/LocatorController",
|
||||||
"navigation/NavigateAction",
|
"navigation/NavigateAction",
|
||||||
"navigation/NavigationService",
|
"navigation/NavigationService",
|
||||||
|
@ -89,8 +89,7 @@
|
|||||||
"name": "Copy Service",
|
"name": "Copy Service",
|
||||||
"description": "Provides a service for copying objects",
|
"description": "Provides a service for copying objects",
|
||||||
"implementation": "services/CopyService.js",
|
"implementation": "services/CopyService.js",
|
||||||
"depends": ["$q", "creationService", "policyService",
|
"depends": ["$q", "policyService", "now"]
|
||||||
"persistenceService", "now"]
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"key": "locationService",
|
"key": "locationService",
|
||||||
|
@ -38,12 +38,9 @@ define(
|
|||||||
* @memberof platform/entanglement
|
* @memberof platform/entanglement
|
||||||
* @implements {platform/entanglement.AbstractComposeService}
|
* @implements {platform/entanglement.AbstractComposeService}
|
||||||
*/
|
*/
|
||||||
function CopyService($q, creationService, policyService, persistenceService, now) {
|
function CopyService($q, policyService) {
|
||||||
this.$q = $q;
|
this.$q = $q;
|
||||||
this.creationService = creationService;
|
|
||||||
this.policyService = policyService;
|
this.policyService = policyService;
|
||||||
this.persistenceService = persistenceService;
|
|
||||||
this.now = now;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CopyService.prototype.validate = function (object, parentCandidate) {
|
CopyService.prototype.validate = function (object, parentCandidate) {
|
||||||
@ -71,7 +68,7 @@ define(
|
|||||||
*/
|
*/
|
||||||
CopyService.prototype.perform = function (domainObject, parent) {
|
CopyService.prototype.perform = function (domainObject, parent) {
|
||||||
var $q = this.$q,
|
var $q = this.$q,
|
||||||
copyTask = new CopyTask(domainObject, parent, this.persistenceService, this.$q, this.now);
|
copyTask = new CopyTask(domainObject, parent, this.policyService, this.$q);
|
||||||
if (this.validate(domainObject, parent)) {
|
if (this.validate(domainObject, parent)) {
|
||||||
return copyTask.perform();
|
return copyTask.perform();
|
||||||
} else {
|
} else {
|
||||||
|
@ -23,8 +23,8 @@
|
|||||||
/*global define */
|
/*global define */
|
||||||
|
|
||||||
define(
|
define(
|
||||||
["uuid"],
|
[],
|
||||||
function (uuid) {
|
function () {
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -33,36 +33,48 @@ define(
|
|||||||
*
|
*
|
||||||
* @param domainObject The object to copy
|
* @param domainObject The object to copy
|
||||||
* @param parent The new location of the cloned object tree
|
* @param parent The new location of the cloned object tree
|
||||||
* @param persistenceService
|
|
||||||
* @param $q
|
* @param $q
|
||||||
* @param now
|
|
||||||
* @constructor
|
* @constructor
|
||||||
*/
|
*/
|
||||||
function CopyTask (domainObject, parent, persistenceService, $q, now){
|
function CopyTask (domainObject, parent, policyService, $q){
|
||||||
this.domainObject = domainObject;
|
this.domainObject = domainObject;
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
|
this.firstClone = undefined;
|
||||||
this.$q = $q;
|
this.$q = $q;
|
||||||
this.deferred = undefined;
|
this.deferred = undefined;
|
||||||
this.persistenceService = persistenceService;
|
this.policyService = policyService;
|
||||||
this.persisted = 0;
|
this.persisted = 0;
|
||||||
this.now = now;
|
|
||||||
this.clones = [];
|
this.clones = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
function composeChild(child, parent) {
|
function composeChild(child, parent, setLocation) {
|
||||||
//Once copied, associate each cloned
|
//Once copied, associate each cloned
|
||||||
// composee with its parent clone
|
// composee with its parent clone
|
||||||
child.model.location = parent.id;
|
|
||||||
parent.model.composition = parent.model.composition || [];
|
parent.getModel().composition.push(child.getId());
|
||||||
return parent.model.composition.push(child.id);
|
|
||||||
|
//If a location is not specified, set it.
|
||||||
|
if (setLocation && child.getModel().location === undefined) {
|
||||||
|
child.getModel().location = parent.getId();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function cloneObjectModel(objectModel) {
|
function cloneObjectModel(objectModel) {
|
||||||
var clone = JSON.parse(JSON.stringify(objectModel));
|
var clone = JSON.parse(JSON.stringify(objectModel));
|
||||||
|
|
||||||
delete clone.composition;
|
/**
|
||||||
|
* Reset certain fields.
|
||||||
|
*/
|
||||||
|
//If has a composition, set it to an empty array. Will be
|
||||||
|
// recomposed later with the ids of its cloned children.
|
||||||
|
if (clone.composition) {
|
||||||
|
//Important to set it to an empty array here, otherwise
|
||||||
|
// hasCapability("composition") returns false;
|
||||||
|
clone.composition = [];
|
||||||
|
}
|
||||||
delete clone.persisted;
|
delete clone.persisted;
|
||||||
delete clone.modified;
|
delete clone.modified;
|
||||||
|
delete clone.location;
|
||||||
|
|
||||||
return clone;
|
return clone;
|
||||||
}
|
}
|
||||||
@ -73,13 +85,10 @@ define(
|
|||||||
* result in automatic request batching by the browser.
|
* result in automatic request batching by the browser.
|
||||||
*/
|
*/
|
||||||
function persistObjects(self) {
|
function persistObjects(self) {
|
||||||
|
|
||||||
return self.$q.all(self.clones.map(function(clone){
|
return self.$q.all(self.clones.map(function(clone){
|
||||||
clone.model.persisted = self.now();
|
return clone.getCapability("persistence").persist().then(function(){
|
||||||
return self.persistenceService.createObject(clone.persistenceSpace, clone.id, clone.model)
|
self.deferred.notify({phase: "copying", totalObjects: self.clones.length, processed: ++self.persisted});
|
||||||
.then(function(){
|
});
|
||||||
self.deferred.notify({phase: "copying", totalObjects: self.clones.length, processed: ++self.persisted});
|
|
||||||
});
|
|
||||||
})).then(function(){
|
})).then(function(){
|
||||||
return self;
|
return self;
|
||||||
});
|
});
|
||||||
@ -89,18 +98,10 @@ define(
|
|||||||
* Will add a list of clones to the specified parent's composition
|
* Will add a list of clones to the specified parent's composition
|
||||||
*/
|
*/
|
||||||
function addClonesToParent(self) {
|
function addClonesToParent(self) {
|
||||||
var parentClone = self.clones[self.clones.length-1];
|
return self.firstClone.getCapability("persistence").persist()
|
||||||
|
.then(function(){self.parent.getCapability("composition").add(self.firstClone.getId());})
|
||||||
if (!self.parent.hasCapability('composition')){
|
|
||||||
return self.$q.reject();
|
|
||||||
}
|
|
||||||
|
|
||||||
return self.persistenceService
|
|
||||||
.updateObject(parentClone.persistenceSpace, parentClone.id, parentClone.model)
|
|
||||||
.then(function(){return self.parent.getCapability("composition").add(parentClone.id);})
|
|
||||||
.then(function(){return self.parent.getCapability("persistence").persist();})
|
.then(function(){return self.parent.getCapability("persistence").persist();})
|
||||||
.then(function(){return parentClone;});
|
.then(function(){return self.firstClone;});
|
||||||
// Ensure the clone of the original domainObject is returned
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -112,13 +113,16 @@ define(
|
|||||||
CopyTask.prototype.copyComposees = function(composees, clonedParent, originalParent){
|
CopyTask.prototype.copyComposees = function(composees, clonedParent, originalParent){
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
return (composees || []).reduce(function(promise, composee){
|
return (composees || []).reduce(function(promise, originalComposee){
|
||||||
//If the composee is composed of other
|
//If the composee is composed of other
|
||||||
// objects, chain a promise..
|
// objects, chain a promise..
|
||||||
return promise.then(function(){
|
return promise.then(function(){
|
||||||
// ...to recursively copy it (and its children)
|
// ...to recursively copy it (and its children)
|
||||||
return self.copy(composee, originalParent).then(function(composee){
|
return self.copy(originalComposee, originalParent).then(function(clonedComposee){
|
||||||
composeChild(composee, clonedParent);
|
//Compose the child within its parent. Cloned
|
||||||
|
// objects will need to also have their location
|
||||||
|
// set, however linked objects will not.
|
||||||
|
return composeChild(clonedComposee, clonedParent, clonedComposee !== originalComposee);
|
||||||
});
|
});
|
||||||
});}, self.$q.when(undefined)
|
});}, self.$q.when(undefined)
|
||||||
);
|
);
|
||||||
@ -131,29 +135,43 @@ define(
|
|||||||
* cloning objects, and composing them with their child clones
|
* cloning objects, and composing them with their child clones
|
||||||
* as it goes
|
* as it goes
|
||||||
* @private
|
* @private
|
||||||
* @param originalObject
|
* @returns {DomainObject} If the type of the original object allows for
|
||||||
* @param originalParent
|
* duplication, then a duplicate of the object, otherwise the object
|
||||||
* @returns {*}
|
* itself (to allow linking to non duplicatable objects).
|
||||||
*/
|
*/
|
||||||
CopyTask.prototype.copy = function(originalObject, originalParent) {
|
CopyTask.prototype.copy = function(originalObject) {
|
||||||
var self = this,
|
var self = this,
|
||||||
modelClone = {
|
clone;
|
||||||
id: uuid(),
|
|
||||||
model: cloneObjectModel(originalObject.getModel()),
|
|
||||||
persistenceSpace: originalParent.hasCapability('persistence') && originalParent.getCapability('persistence').getSpace()
|
|
||||||
};
|
|
||||||
|
|
||||||
return this.$q.when(originalObject.useCapability('composition')).then(function(composees){
|
//Check if the type of the object being copied allows for
|
||||||
self.deferred.notify({phase: "preparing"});
|
// creation of new instances. If it does not, then a link to the
|
||||||
//Duplicate the object's children, and their children, and
|
// original will be created instead.
|
||||||
// so on down to the leaf nodes of the tree.
|
if (this.policyService.allow("creation", originalObject.getCapability("type"))){
|
||||||
return self.copyComposees(composees, modelClone, originalObject).then(function (){
|
//create a new clone of the original object. Use the
|
||||||
//Add the clone to the list of clones that will
|
// creation capability of the targetParent to create the
|
||||||
//be returned by this function
|
// new clone. This will ensure that the correct persistence
|
||||||
self.clones.push(modelClone);
|
// space is used.
|
||||||
return modelClone;
|
clone = this.parent.useCapability("instantiation", cloneObjectModel(originalObject.getModel()));
|
||||||
|
|
||||||
|
//Iterate through child tree
|
||||||
|
return this.$q.when(originalObject.useCapability('composition')).then(function(composees){
|
||||||
|
self.deferred.notify({phase: "preparing"});
|
||||||
|
//Duplicate the object's children, and their children, and
|
||||||
|
// so on down to the leaf nodes of the tree.
|
||||||
|
//If it is a link, don't both with children
|
||||||
|
return self.copyComposees(composees, clone, originalObject).then(function (){
|
||||||
|
//Add the clone to the list of clones that will
|
||||||
|
//be returned by this function
|
||||||
|
self.clones.push(clone);
|
||||||
|
return clone;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
} else {
|
||||||
|
//Creating a link, no need to iterate children
|
||||||
|
return self.$q.when(originalObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -172,7 +190,10 @@ define(
|
|||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
return this.copy(self.domainObject, self.parent).then(function(domainObjectClone){
|
return this.copy(self.domainObject, self.parent).then(function(domainObjectClone){
|
||||||
domainObjectClone.model.location = self.parent.getId();
|
if (domainObjectClone !== self.domainObject) {
|
||||||
|
domainObjectClone.getModel().location = self.parent.getId();
|
||||||
|
}
|
||||||
|
self.firstClone = domainObjectClone;
|
||||||
return self;
|
return self;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -63,7 +63,6 @@ define(
|
|||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
copyService = new CopyService(
|
copyService = new CopyService(
|
||||||
null,
|
|
||||||
null,
|
null,
|
||||||
policyService
|
policyService
|
||||||
);
|
);
|
||||||
@ -130,47 +129,50 @@ define(
|
|||||||
creationService,
|
creationService,
|
||||||
createObjectPromise,
|
createObjectPromise,
|
||||||
copyService,
|
copyService,
|
||||||
mockPersistenceService,
|
|
||||||
mockNow,
|
mockNow,
|
||||||
object,
|
object,
|
||||||
newParent,
|
newParent,
|
||||||
copyResult,
|
copyResult,
|
||||||
copyFinished,
|
copyFinished,
|
||||||
persistObjectPromise,
|
persistObjectPromise,
|
||||||
parentPersistenceCapability,
|
persistenceCapability,
|
||||||
|
instantiationCapability,
|
||||||
|
compositionCapability,
|
||||||
|
locationCapability,
|
||||||
resolvedValue;
|
resolvedValue;
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
creationService = jasmine.createSpyObj(
|
|
||||||
'creationService',
|
|
||||||
['createObject']
|
|
||||||
);
|
|
||||||
createObjectPromise = synchronousPromise(undefined);
|
createObjectPromise = synchronousPromise(undefined);
|
||||||
creationService.createObject.andReturn(createObjectPromise);
|
|
||||||
policyService.allow.andReturn(true);
|
policyService.allow.andReturn(true);
|
||||||
|
|
||||||
mockPersistenceService = jasmine.createSpyObj(
|
|
||||||
'persistenceService',
|
|
||||||
['createObject', 'updateObject']
|
|
||||||
);
|
|
||||||
persistObjectPromise = synchronousPromise(undefined);
|
persistObjectPromise = synchronousPromise(undefined);
|
||||||
mockPersistenceService.createObject.andReturn(persistObjectPromise);
|
|
||||||
mockPersistenceService.updateObject.andReturn(persistObjectPromise);
|
instantiationCapability = jasmine.createSpyObj(
|
||||||
|
"instantiation",
|
||||||
parentPersistenceCapability = jasmine.createSpyObj(
|
[ "invoke" ]
|
||||||
"persistence",
|
);
|
||||||
|
|
||||||
|
persistenceCapability = jasmine.createSpyObj(
|
||||||
|
"persistenceCapability",
|
||||||
[ "persist", "getSpace" ]
|
[ "persist", "getSpace" ]
|
||||||
);
|
);
|
||||||
|
persistenceCapability.persist.andReturn(persistObjectPromise);
|
||||||
|
|
||||||
parentPersistenceCapability.persist.andReturn(persistObjectPromise);
|
compositionCapability = jasmine.createSpyObj(
|
||||||
parentPersistenceCapability.getSpace.andReturn("testSpace");
|
'compositionCapability',
|
||||||
|
['invoke', 'add']
|
||||||
|
);
|
||||||
|
|
||||||
mockNow = jasmine.createSpyObj("mockNow", ["now"]);
|
locationCapability = jasmine.createSpyObj(
|
||||||
mockNow.now.andCallFake(function(){
|
'locationCapability',
|
||||||
return 1234;
|
['isLink']
|
||||||
});
|
);
|
||||||
|
locationCapability.isLink.andReturn(false);
|
||||||
|
|
||||||
mockDeferred = jasmine.createSpyObj('mockDeferred', ['notify', 'resolve']);
|
mockDeferred = jasmine.createSpyObj(
|
||||||
|
'mockDeferred',
|
||||||
|
['notify', 'resolve', 'reject']
|
||||||
|
);
|
||||||
mockDeferred.notify.andCallFake(function(notification){});
|
mockDeferred.notify.andCallFake(function(notification){});
|
||||||
mockDeferred.resolve.andCallFake(function(value){resolvedValue = value;});
|
mockDeferred.resolve.andCallFake(function(value){resolvedValue = value;});
|
||||||
mockDeferred.promise = {
|
mockDeferred.promise = {
|
||||||
@ -179,7 +181,11 @@ define(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
mockQ = jasmine.createSpyObj('mockQ', ['when', 'all', 'reject', 'defer']);
|
mockQ = jasmine.createSpyObj(
|
||||||
|
'mockQ',
|
||||||
|
['when', 'all', 'reject', 'defer']
|
||||||
|
);
|
||||||
|
mockQ.reject.andReturn(synchronousPromise(undefined));
|
||||||
mockQ.when.andCallFake(synchronousPromise);
|
mockQ.when.andCallFake(synchronousPromise);
|
||||||
mockQ.all.andCallFake(function (promises) {
|
mockQ.all.andCallFake(function (promises) {
|
||||||
var result = {};
|
var result = {};
|
||||||
@ -194,6 +200,8 @@ define(
|
|||||||
|
|
||||||
describe("on domain object without composition", function () {
|
describe("on domain object without composition", function () {
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
|
var objectCopy;
|
||||||
|
|
||||||
newParent = domainObjectFactory({
|
newParent = domainObjectFactory({
|
||||||
name: 'newParent',
|
name: 'newParent',
|
||||||
id: '456',
|
id: '456',
|
||||||
@ -201,7 +209,9 @@ define(
|
|||||||
composition: []
|
composition: []
|
||||||
},
|
},
|
||||||
capabilities: {
|
capabilities: {
|
||||||
persistence: parentPersistenceCapability
|
instantiation: instantiationCapability,
|
||||||
|
persistence: persistenceCapability,
|
||||||
|
composition: compositionCapability
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -210,31 +220,46 @@ define(
|
|||||||
id: 'abc',
|
id: 'abc',
|
||||||
model: {
|
model: {
|
||||||
name: 'some object',
|
name: 'some object',
|
||||||
location: newParent.id,
|
location: '456',
|
||||||
persisted: mockNow.now()
|
someOtherAttribute: 'some other value',
|
||||||
|
embeddedObjectAttribute: {
|
||||||
|
name: 'Some embedded object'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
capabilities: {
|
||||||
|
persistence: persistenceCapability
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
copyService = new CopyService(mockQ, creationService, policyService, mockPersistenceService, mockNow.now);
|
objectCopy = domainObjectFactory({
|
||||||
|
name: 'object',
|
||||||
|
id: 'abc.copy.fdgdfgdf',
|
||||||
|
capabilities: {
|
||||||
|
persistence: persistenceCapability,
|
||||||
|
location: locationCapability
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
instantiationCapability.invoke.andCallFake(
|
||||||
|
function(model){
|
||||||
|
objectCopy.model = model;
|
||||||
|
return objectCopy;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
copyService = new CopyService(mockQ, policyService);
|
||||||
copyResult = copyService.perform(object, newParent);
|
copyResult = copyService.perform(object, newParent);
|
||||||
copyFinished = jasmine.createSpy('copyFinished');
|
copyFinished = jasmine.createSpy('copyFinished');
|
||||||
copyResult.then(copyFinished);
|
copyResult.then(copyFinished);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("uses persistence service", function () {
|
it("uses persistence capability", function () {
|
||||||
expect(mockPersistenceService.createObject)
|
expect(persistenceCapability.persist)
|
||||||
.toHaveBeenCalledWith(parentPersistenceCapability.getSpace(), jasmine.any(String), object.getModel());
|
.toHaveBeenCalled();
|
||||||
|
});
|
||||||
expect(persistObjectPromise.then)
|
|
||||||
.toHaveBeenCalledWith(jasmine.any(Function));
|
|
||||||
});
|
|
||||||
|
|
||||||
it("deep clones object model", function () {
|
it("deep clones object model", function () {
|
||||||
//var newModel = creationService
|
var newModel = copyFinished.calls[0].args[0].getModel();
|
||||||
var newModel = mockPersistenceService
|
|
||||||
.createObject
|
|
||||||
.mostRecentCall
|
|
||||||
.args[2];
|
|
||||||
expect(newModel).toEqual(object.model);
|
expect(newModel).toEqual(object.model);
|
||||||
expect(newModel).not.toBe(object.model);
|
expect(newModel).not.toBe(object.model);
|
||||||
});
|
});
|
||||||
@ -249,27 +274,57 @@ define(
|
|||||||
describe("on domainObject with composition", function () {
|
describe("on domainObject with composition", function () {
|
||||||
var newObject,
|
var newObject,
|
||||||
childObject,
|
childObject,
|
||||||
compositionCapability,
|
objectClone,
|
||||||
locationCapability,
|
childObjectClone,
|
||||||
compositionPromise;
|
compositionPromise;
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
|
var invocationCount = 0,
|
||||||
|
objectClones;
|
||||||
|
|
||||||
|
instantiationCapability.invoke.andCallFake(
|
||||||
|
function(model){
|
||||||
|
var cloneToReturn = objectClones[invocationCount++];
|
||||||
|
cloneToReturn.model = model;
|
||||||
|
return cloneToReturn;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
locationCapability = jasmine.createSpyObj('locationCapability', ['isLink']);
|
newParent = domainObjectFactory({
|
||||||
locationCapability.isLink.andReturn(true);
|
name: 'newParent',
|
||||||
|
id: '456',
|
||||||
|
model: {
|
||||||
|
composition: []
|
||||||
|
},
|
||||||
|
capabilities: {
|
||||||
|
instantiation: instantiationCapability,
|
||||||
|
persistence: persistenceCapability,
|
||||||
|
composition: compositionCapability
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
childObject = domainObjectFactory({
|
childObject = domainObjectFactory({
|
||||||
name: 'childObject',
|
name: 'childObject',
|
||||||
id: 'def',
|
id: 'def',
|
||||||
model: {
|
model: {
|
||||||
name: 'a child object'
|
name: 'a child object',
|
||||||
|
location: 'abc'
|
||||||
|
},
|
||||||
|
capabilities: {
|
||||||
|
persistence: persistenceCapability,
|
||||||
|
location: locationCapability
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
compositionCapability = jasmine.createSpyObj(
|
|
||||||
'compositionCapability',
|
childObjectClone = domainObjectFactory({
|
||||||
['invoke', 'add']
|
name: 'childObject',
|
||||||
);
|
id: 'def.clone',
|
||||||
|
capabilities: {
|
||||||
|
persistence: persistenceCapability,
|
||||||
|
location: locationCapability
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
compositionPromise = jasmine.createSpyObj(
|
compositionPromise = jasmine.createSpyObj(
|
||||||
'compositionPromise',
|
'compositionPromise',
|
||||||
['then']
|
['then']
|
||||||
@ -280,7 +335,7 @@ define(
|
|||||||
.andReturn(synchronousPromise([childObject]));
|
.andReturn(synchronousPromise([childObject]));
|
||||||
|
|
||||||
object = domainObjectFactory({
|
object = domainObjectFactory({
|
||||||
name: 'object',
|
name: 'some object',
|
||||||
id: 'abc',
|
id: 'abc',
|
||||||
model: {
|
model: {
|
||||||
name: 'some object',
|
name: 'some object',
|
||||||
@ -288,36 +343,27 @@ define(
|
|||||||
location: 'testLocation'
|
location: 'testLocation'
|
||||||
},
|
},
|
||||||
capabilities: {
|
capabilities: {
|
||||||
|
instantiation: instantiationCapability,
|
||||||
composition: compositionCapability,
|
composition: compositionCapability,
|
||||||
location: locationCapability
|
location: locationCapability,
|
||||||
}
|
persistence: persistenceCapability
|
||||||
});
|
|
||||||
newObject = domainObjectFactory({
|
|
||||||
name: 'object',
|
|
||||||
id: 'abc2',
|
|
||||||
model: {
|
|
||||||
name: 'some object',
|
|
||||||
composition: []
|
|
||||||
},
|
|
||||||
capabilities: {
|
|
||||||
composition: compositionCapability
|
|
||||||
}
|
|
||||||
});
|
|
||||||
newParent = domainObjectFactory({
|
|
||||||
name: 'newParent',
|
|
||||||
id: '456',
|
|
||||||
model: {
|
|
||||||
composition: []
|
|
||||||
},
|
|
||||||
capabilities: {
|
|
||||||
composition: compositionCapability,
|
|
||||||
persistence: parentPersistenceCapability
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
createObjectPromise = synchronousPromise(newObject);
|
objectClone = domainObjectFactory({
|
||||||
creationService.createObject.andReturn(createObjectPromise);
|
name: 'some object',
|
||||||
copyService = new CopyService(mockQ, creationService, policyService, mockPersistenceService, mockNow.now);
|
id: 'abc.clone',
|
||||||
|
capabilities: {
|
||||||
|
instantiation: instantiationCapability,
|
||||||
|
composition: compositionCapability,
|
||||||
|
location: locationCapability,
|
||||||
|
persistence: persistenceCapability
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
objectClones = [objectClone, childObjectClone];
|
||||||
|
|
||||||
|
copyService = new CopyService(mockQ, policyService);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("the cloning process", function(){
|
describe("the cloning process", function(){
|
||||||
@ -327,10 +373,9 @@ define(
|
|||||||
copyResult.then(copyFinished);
|
copyResult.then(copyFinished);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("copies object and children in a bottom-up" +
|
it("returns a promise", function () {
|
||||||
" fashion", function () {
|
expect(copyResult.then).toBeDefined();
|
||||||
expect(mockPersistenceService.createObject.calls[0].args[2].name).toEqual(childObject.model.name);
|
expect(copyFinished).toHaveBeenCalled();
|
||||||
expect(mockPersistenceService.createObject.calls[1].args[2].name).toEqual(object.model.name);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("returns a promise", function () {
|
it("returns a promise", function () {
|
||||||
@ -338,15 +383,27 @@ define(
|
|||||||
expect(copyFinished).toHaveBeenCalled();
|
expect(copyFinished).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("clears modified and sets persisted", function () {
|
|
||||||
expect(copyFinished.mostRecentCall.args[0].model.modified).toBeUndefined();
|
|
||||||
expect(copyFinished.mostRecentCall.args[0].model.persisted).toBe(mockNow.now());
|
|
||||||
});
|
|
||||||
|
|
||||||
it ("correctly locates cloned objects", function() {
|
it ("correctly locates cloned objects", function() {
|
||||||
expect(mockPersistenceService.createObject.calls[0].args[2].location).toEqual(mockPersistenceService.createObject.calls[1].args[1]);
|
expect(childObjectClone.getModel().location).toEqual(objectClone.getId());
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
describe("when cloning non-creatable objects", function() {
|
||||||
|
beforeEach(function () {
|
||||||
|
policyService.allow.andCallFake(function(category){
|
||||||
|
//Return false for 'creation' policy
|
||||||
|
return category !== 'creation';
|
||||||
|
});
|
||||||
|
|
||||||
|
copyResult = copyService.perform(object, newParent);
|
||||||
|
copyFinished = jasmine.createSpy('copyFinished');
|
||||||
|
copyResult.then(copyFinished);
|
||||||
|
});
|
||||||
|
it ("creates link instead of clone", function() {
|
||||||
|
var copiedObject = copyFinished.calls[0].args[0];
|
||||||
|
expect(copiedObject).toBe(object);
|
||||||
|
expect(compositionCapability.add).toHaveBeenCalledWith(copiedObject.getId());
|
||||||
|
//expect(newParent.getModel().composition).toContain(copiedObject.getId());
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -355,20 +412,28 @@ define(
|
|||||||
object = domainObjectFactory({
|
object = domainObjectFactory({
|
||||||
name: 'object',
|
name: 'object',
|
||||||
capabilities: {
|
capabilities: {
|
||||||
type: { type: 'object' }
|
type: { type: 'object' },
|
||||||
|
location: locationCapability,
|
||||||
|
persistence: persistenceCapability
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
newParent = domainObjectFactory({
|
newParent = domainObjectFactory({
|
||||||
name: 'parentCandidate',
|
name: 'parentCandidate',
|
||||||
capabilities: {
|
capabilities: {
|
||||||
type: { type: 'parentCandidate' }
|
type: { type: 'parentCandidate' },
|
||||||
|
instantiation: instantiationCapability,
|
||||||
|
composition: compositionCapability,
|
||||||
|
persistence: persistenceCapability
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
instantiationCapability.invoke.andReturn(object);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("throws an error", function () {
|
it("throws an error", function () {
|
||||||
var copyService =
|
var copyService =
|
||||||
new CopyService(mockQ, creationService, policyService, mockPersistenceService, mockNow.now);
|
new CopyService(mockQ, policyService);
|
||||||
|
|
||||||
function perform() {
|
function perform() {
|
||||||
copyService.perform(object, newParent);
|
copyService.perform(object, newParent);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user