#338 Fixed failing tests after refactor

This commit is contained in:
Henry 2015-12-03 12:49:54 -08:00
parent 3b427c31a2
commit 734e979c94
4 changed files with 135 additions and 108 deletions

View File

@ -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",

View File

@ -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.policyService, 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 {

View File

@ -33,21 +33,16 @@ 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, policyService, $q, now){ function CopyTask (domainObject, parent, policyService, $q){
this.domainObject = domainObject; this.domainObject = domainObject;
this.parent = parent; this.parent = parent;
this.$q = $q; this.$q = $q;
this.deferred = undefined; this.deferred = undefined;
this.persistenceService = persistenceService;
this.policyService = policyService; this.policyService = policyService;
this.persistenceSpace = parent.getCapability("persistence") && parent.getCapability("persistence").getSpace();
this.persisted = 0; this.persisted = 0;
this.now = now;
this.clones = []; this.clones = [];
} }
@ -154,7 +149,7 @@ define(
// creation capability of the targetParent to create the // creation capability of the targetParent to create the
// new clone. This will ensure that the correct persistence // new clone. This will ensure that the correct persistence
// space is used. // space is used.
clone = this.parent.hasCapability("instantiation") && originalParent.useCapability("instantiation", cloneObjectModel(originalObject.getModel())); clone = this.parent.hasCapability("instantiation") && this.parent.useCapability("instantiation", cloneObjectModel(originalObject.getModel()));
//Iterate through child tree //Iterate through child tree
return this.$q.when(originalObject.useCapability('composition')).then(function(composees){ return this.$q.when(originalObject.useCapability('composition')).then(function(composees){
@ -193,7 +188,7 @@ 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(); domainObjectClone.getModel().location = self.parent.getId();
return self; return self;
}); });
}; };
@ -207,7 +202,7 @@ define(
this.deferred = this.$q.defer(); this.deferred = this.$q.defer();
if (!this.parent.hasCapability('composition')){ if (!this.parent.hasCapability('composition')){
return self.$q.reject(); return this.$q.reject();
} }
this.buildCopyPlan() this.buildCopyPlan()

View File

@ -63,7 +63,6 @@ define(
beforeEach(function () { beforeEach(function () {
copyService = new CopyService( copyService = new CopyService(
null,
null, null,
policyService policyService
); );
@ -130,48 +129,52 @@ 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);
parentPersistenceCapability = jasmine.createSpyObj( instantiationCapability = jasmine.createSpyObj(
"persistence", "instantiation",
[ "invoke" ]
);
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.reject.andCallFake(function(){});
mockDeferred.resolve.andCallFake(function(value){resolvedValue = value;}); mockDeferred.resolve.andCallFake(function(value){resolvedValue = value;});
mockDeferred.promise = { mockDeferred.promise = {
then: function(callback){ then: function(callback){
@ -179,7 +182,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 +201,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 +210,9 @@ define(
composition: [] composition: []
}, },
capabilities: { capabilities: {
persistence: parentPersistenceCapability instantiation: instantiationCapability,
persistence: persistenceCapability,
composition: compositionCapability
} }
}); });
@ -210,31 +221,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 +275,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 +336,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 +344,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,24 +374,13 @@ define(
copyResult.then(copyFinished); 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 () { it("returns a promise", function () {
expect(copyResult.then).toBeDefined(); expect(copyResult.then).toBeDefined();
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());
}); });
}); });
@ -368,7 +404,7 @@ define(
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);