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:
Victor Woeltjen 2015-12-04 09:46:24 -08:00
commit cce415fc51
10 changed files with 364 additions and 156 deletions

View File

@ -102,6 +102,12 @@
"implementation": "navigation/NavigationService.js"
}
],
"policies": [
{
"implementation": "creation/CreationPolicy.js",
"category": "creation"
}
],
"actions": [
{
"key": "navigate",

View File

@ -69,7 +69,7 @@ define(
// Introduce one create action per type
return this.typeService.listTypes().filter(function (type) {
return type.hasFeature("creation");
return self.policyService.allow("creation", type);
}).map(function (type) {
return new CreateAction(
type,

View 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;
}
);

View File

@ -33,6 +33,9 @@ define(
var mockTypeService,
mockDialogService,
mockCreationService,
mockPolicyService,
mockCreationPolicy,
mockPolicyMap = {},
mockTypes,
provider;
@ -67,14 +70,32 @@ define(
"creationService",
[ "createObject" ]
);
mockPolicyService = jasmine.createSpyObj(
"policyService",
[ "allow" ]
);
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);
provider = new CreateActionProvider(
mockTypeService,
mockDialogService,
mockCreationService
mockCreationService,
mockPolicyService
);
});
@ -94,15 +115,15 @@ define(
it("does not expose non-creatable types", function () {
// 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.
expect(provider.getActions({
key: "create",
domainObject: {}
}).length).toEqual(2);
// Make sure it was creation which was used to check
expect(mockTypes[1].hasFeature)
.toHaveBeenCalledWith("creation");
expect(mockPolicyService.allow)
.toHaveBeenCalledWith("creation", mockTypes[0]);
});
});
}

View 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();
});
});
}
);

View File

@ -8,6 +8,7 @@
"creation/CreateMenuController",
"creation/CreateWizard",
"creation/CreationService",
"creation/CreationPolicy",
"creation/LocatorController",
"navigation/NavigateAction",
"navigation/NavigationService",

View File

@ -89,8 +89,7 @@
"name": "Copy Service",
"description": "Provides a service for copying objects",
"implementation": "services/CopyService.js",
"depends": ["$q", "creationService", "policyService",
"persistenceService", "now"]
"depends": ["$q", "policyService", "now"]
},
{
"key": "locationService",

View File

@ -38,12 +38,9 @@ define(
* @memberof platform/entanglement
* @implements {platform/entanglement.AbstractComposeService}
*/
function CopyService($q, creationService, policyService, persistenceService, now) {
function CopyService($q, policyService) {
this.$q = $q;
this.creationService = creationService;
this.policyService = policyService;
this.persistenceService = persistenceService;
this.now = now;
}
CopyService.prototype.validate = function (object, parentCandidate) {
@ -71,7 +68,7 @@ define(
*/
CopyService.prototype.perform = function (domainObject, parent) {
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)) {
return copyTask.perform();
} else {

View File

@ -23,8 +23,8 @@
/*global define */
define(
["uuid"],
function (uuid) {
[],
function () {
"use strict";
/**
@ -33,36 +33,48 @@ define(
*
* @param domainObject The object to copy
* @param parent The new location of the cloned object tree
* @param persistenceService
* @param $q
* @param now
* @constructor
*/
function CopyTask (domainObject, parent, persistenceService, $q, now){
function CopyTask (domainObject, parent, policyService, $q){
this.domainObject = domainObject;
this.parent = parent;
this.firstClone = undefined;
this.$q = $q;
this.deferred = undefined;
this.persistenceService = persistenceService;
this.policyService = policyService;
this.persisted = 0;
this.now = now;
this.clones = [];
}
function composeChild(child, parent) {
function composeChild(child, parent, setLocation) {
//Once copied, associate each cloned
// composee with its parent clone
child.model.location = parent.id;
parent.model.composition = parent.model.composition || [];
return parent.model.composition.push(child.id);
parent.getModel().composition.push(child.getId());
//If a location is not specified, set it.
if (setLocation && child.getModel().location === undefined) {
child.getModel().location = parent.getId();
}
}
function cloneObjectModel(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.modified;
delete clone.location;
return clone;
}
@ -73,13 +85,10 @@ define(
* result in automatic request batching by the browser.
*/
function persistObjects(self) {
return self.$q.all(self.clones.map(function(clone){
clone.model.persisted = self.now();
return self.persistenceService.createObject(clone.persistenceSpace, clone.id, clone.model)
.then(function(){
self.deferred.notify({phase: "copying", totalObjects: self.clones.length, processed: ++self.persisted});
});
return clone.getCapability("persistence").persist().then(function(){
self.deferred.notify({phase: "copying", totalObjects: self.clones.length, processed: ++self.persisted});
});
})).then(function(){
return self;
});
@ -89,18 +98,10 @@ define(
* Will add a list of clones to the specified parent's composition
*/
function addClonesToParent(self) {
var parentClone = self.clones[self.clones.length-1];
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);})
return self.firstClone.getCapability("persistence").persist()
.then(function(){self.parent.getCapability("composition").add(self.firstClone.getId());})
.then(function(){return self.parent.getCapability("persistence").persist();})
.then(function(){return parentClone;});
// Ensure the clone of the original domainObject is returned
.then(function(){return self.firstClone;});
}
/**
@ -112,13 +113,16 @@ define(
CopyTask.prototype.copyComposees = function(composees, clonedParent, originalParent){
var self = this;
return (composees || []).reduce(function(promise, composee){
return (composees || []).reduce(function(promise, originalComposee){
//If the composee is composed of other
// objects, chain a promise..
return promise.then(function(){
// ...to recursively copy it (and its children)
return self.copy(composee, originalParent).then(function(composee){
composeChild(composee, clonedParent);
return self.copy(originalComposee, originalParent).then(function(clonedComposee){
//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)
);
@ -131,29 +135,43 @@ define(
* cloning objects, and composing them with their child clones
* as it goes
* @private
* @param originalObject
* @param originalParent
* @returns {*}
* @returns {DomainObject} If the type of the original object allows for
* duplication, then a duplicate of the object, otherwise the object
* itself (to allow linking to non duplicatable objects).
*/
CopyTask.prototype.copy = function(originalObject, originalParent) {
CopyTask.prototype.copy = function(originalObject) {
var self = this,
modelClone = {
id: uuid(),
model: cloneObjectModel(originalObject.getModel()),
persistenceSpace: originalParent.hasCapability('persistence') && originalParent.getCapability('persistence').getSpace()
};
clone;
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.
return self.copyComposees(composees, modelClone, originalObject).then(function (){
//Add the clone to the list of clones that will
//be returned by this function
self.clones.push(modelClone);
return modelClone;
//Check if the type of the object being copied allows for
// creation of new instances. If it does not, then a link to the
// original will be created instead.
if (this.policyService.allow("creation", originalObject.getCapability("type"))){
//create a new clone of the original object. Use the
// creation capability of the targetParent to create the
// new clone. This will ensure that the correct persistence
// space is used.
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;
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;
});
};

View File

@ -63,7 +63,6 @@ define(
beforeEach(function () {
copyService = new CopyService(
null,
null,
policyService
);
@ -130,47 +129,50 @@ define(
creationService,
createObjectPromise,
copyService,
mockPersistenceService,
mockNow,
object,
newParent,
copyResult,
copyFinished,
persistObjectPromise,
parentPersistenceCapability,
persistenceCapability,
instantiationCapability,
compositionCapability,
locationCapability,
resolvedValue;
beforeEach(function () {
creationService = jasmine.createSpyObj(
'creationService',
['createObject']
);
createObjectPromise = synchronousPromise(undefined);
creationService.createObject.andReturn(createObjectPromise);
policyService.allow.andReturn(true);
mockPersistenceService = jasmine.createSpyObj(
'persistenceService',
['createObject', 'updateObject']
);
persistObjectPromise = synchronousPromise(undefined);
mockPersistenceService.createObject.andReturn(persistObjectPromise);
mockPersistenceService.updateObject.andReturn(persistObjectPromise);
parentPersistenceCapability = jasmine.createSpyObj(
"persistence",
instantiationCapability = jasmine.createSpyObj(
"instantiation",
[ "invoke" ]
);
persistenceCapability = jasmine.createSpyObj(
"persistenceCapability",
[ "persist", "getSpace" ]
);
persistenceCapability.persist.andReturn(persistObjectPromise);
parentPersistenceCapability.persist.andReturn(persistObjectPromise);
parentPersistenceCapability.getSpace.andReturn("testSpace");
compositionCapability = jasmine.createSpyObj(
'compositionCapability',
['invoke', 'add']
);
mockNow = jasmine.createSpyObj("mockNow", ["now"]);
mockNow.now.andCallFake(function(){
return 1234;
});
locationCapability = jasmine.createSpyObj(
'locationCapability',
['isLink']
);
locationCapability.isLink.andReturn(false);
mockDeferred = jasmine.createSpyObj('mockDeferred', ['notify', 'resolve']);
mockDeferred = jasmine.createSpyObj(
'mockDeferred',
['notify', 'resolve', 'reject']
);
mockDeferred.notify.andCallFake(function(notification){});
mockDeferred.resolve.andCallFake(function(value){resolvedValue = value;});
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.all.andCallFake(function (promises) {
var result = {};
@ -194,6 +200,8 @@ define(
describe("on domain object without composition", function () {
beforeEach(function () {
var objectCopy;
newParent = domainObjectFactory({
name: 'newParent',
id: '456',
@ -201,7 +209,9 @@ define(
composition: []
},
capabilities: {
persistence: parentPersistenceCapability
instantiation: instantiationCapability,
persistence: persistenceCapability,
composition: compositionCapability
}
});
@ -210,31 +220,46 @@ define(
id: 'abc',
model: {
name: 'some object',
location: newParent.id,
persisted: mockNow.now()
location: '456',
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);
copyFinished = jasmine.createSpy('copyFinished');
copyResult.then(copyFinished);
});
it("uses persistence service", function () {
expect(mockPersistenceService.createObject)
.toHaveBeenCalledWith(parentPersistenceCapability.getSpace(), jasmine.any(String), object.getModel());
expect(persistObjectPromise.then)
.toHaveBeenCalledWith(jasmine.any(Function));
});
it("uses persistence capability", function () {
expect(persistenceCapability.persist)
.toHaveBeenCalled();
});
it("deep clones object model", function () {
//var newModel = creationService
var newModel = mockPersistenceService
.createObject
.mostRecentCall
.args[2];
var newModel = copyFinished.calls[0].args[0].getModel();
expect(newModel).toEqual(object.model);
expect(newModel).not.toBe(object.model);
});
@ -249,27 +274,57 @@ define(
describe("on domainObject with composition", function () {
var newObject,
childObject,
compositionCapability,
locationCapability,
objectClone,
childObjectClone,
compositionPromise;
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']);
locationCapability.isLink.andReturn(true);
newParent = domainObjectFactory({
name: 'newParent',
id: '456',
model: {
composition: []
},
capabilities: {
instantiation: instantiationCapability,
persistence: persistenceCapability,
composition: compositionCapability
}
});
childObject = domainObjectFactory({
name: 'childObject',
id: 'def',
model: {
name: 'a child object'
name: 'a child object',
location: 'abc'
},
capabilities: {
persistence: persistenceCapability,
location: locationCapability
}
});
compositionCapability = jasmine.createSpyObj(
'compositionCapability',
['invoke', 'add']
);
childObjectClone = domainObjectFactory({
name: 'childObject',
id: 'def.clone',
capabilities: {
persistence: persistenceCapability,
location: locationCapability
}
});
compositionPromise = jasmine.createSpyObj(
'compositionPromise',
['then']
@ -280,7 +335,7 @@ define(
.andReturn(synchronousPromise([childObject]));
object = domainObjectFactory({
name: 'object',
name: 'some object',
id: 'abc',
model: {
name: 'some object',
@ -288,36 +343,27 @@ define(
location: 'testLocation'
},
capabilities: {
instantiation: instantiationCapability,
composition: compositionCapability,
location: locationCapability
}
});
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
location: locationCapability,
persistence: persistenceCapability
}
});
createObjectPromise = synchronousPromise(newObject);
creationService.createObject.andReturn(createObjectPromise);
copyService = new CopyService(mockQ, creationService, policyService, mockPersistenceService, mockNow.now);
objectClone = domainObjectFactory({
name: 'some object',
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(){
@ -327,10 +373,9 @@ define(
copyResult.then(copyFinished);
});
it("copies object and children in a bottom-up" +
" fashion", function () {
expect(mockPersistenceService.createObject.calls[0].args[2].name).toEqual(childObject.model.name);
expect(mockPersistenceService.createObject.calls[1].args[2].name).toEqual(object.model.name);
it("returns a promise", function () {
expect(copyResult.then).toBeDefined();
expect(copyFinished).toHaveBeenCalled();
});
it("returns a promise", function () {
@ -338,15 +383,27 @@ define(
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() {
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({
name: 'object',
capabilities: {
type: { type: 'object' }
type: { type: 'object' },
location: locationCapability,
persistence: persistenceCapability
}
});
newParent = domainObjectFactory({
name: 'parentCandidate',
capabilities: {
type: { type: 'parentCandidate' }
type: { type: 'parentCandidate' },
instantiation: instantiationCapability,
composition: compositionCapability,
persistence: persistenceCapability
}
});
instantiationCapability.invoke.andReturn(object);
});
it("throws an error", function () {
var copyService =
new CopyService(mockQ, creationService, policyService, mockPersistenceService, mockNow.now);
new CopyService(mockQ, policyService);
function perform() {
copyService.perform(object, newParent);