2015-06-11 21:22:18 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* 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.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2015-09-23 19:43:03 +00:00
|
|
|
/*global define,describe,beforeEach,it,jasmine,expect,spyOn */
|
2015-06-11 18:58:53 +00:00
|
|
|
|
|
|
|
define(
|
|
|
|
[
|
|
|
|
'../../src/services/LinkService',
|
2015-08-06 21:55:50 +00:00
|
|
|
'../DomainObjectFactory',
|
|
|
|
'../ControlledPromise'
|
2015-06-11 18:58:53 +00:00
|
|
|
],
|
2015-08-06 22:05:42 +00:00
|
|
|
function (LinkService, domainObjectFactory, ControlledPromise) {
|
2015-06-11 18:58:53 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
describe("LinkService", function () {
|
|
|
|
|
|
|
|
var linkService,
|
|
|
|
mockPolicyService;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
mockPolicyService = jasmine.createSpyObj(
|
|
|
|
'policyService',
|
|
|
|
['allow']
|
|
|
|
);
|
2015-09-22 22:42:39 +00:00
|
|
|
mockPolicyService.allow.andReturn(true);
|
2015-06-11 18:58:53 +00:00
|
|
|
linkService = new LinkService(mockPolicyService);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("validate", function () {
|
|
|
|
|
|
|
|
var object,
|
|
|
|
parentCandidate,
|
|
|
|
validate;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
object = domainObjectFactory({
|
|
|
|
name: 'object'
|
|
|
|
});
|
|
|
|
parentCandidate = domainObjectFactory({
|
2015-09-02 16:37:26 +00:00
|
|
|
name: 'parentCandidate',
|
|
|
|
capabilities: {
|
|
|
|
composition: jasmine.createSpyObj(
|
|
|
|
'composition',
|
|
|
|
['invoke', 'add']
|
|
|
|
)
|
|
|
|
}
|
2015-06-11 18:58:53 +00:00
|
|
|
});
|
|
|
|
validate = function () {
|
|
|
|
return linkService.validate(object, parentCandidate);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
it("does not allow invalid parentCandidate", function () {
|
|
|
|
parentCandidate = undefined;
|
|
|
|
expect(validate()).toBe(false);
|
|
|
|
parentCandidate = {};
|
|
|
|
expect(validate()).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("does not allow parent to be object", function () {
|
|
|
|
parentCandidate.id = object.id = 'abc';
|
|
|
|
expect(validate()).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("does not allow parent that contains object", function () {
|
|
|
|
object.id = 'abc';
|
|
|
|
parentCandidate.id = 'xyz';
|
|
|
|
parentCandidate.model.composition = ['abc'];
|
|
|
|
expect(validate()).toBe(false);
|
|
|
|
});
|
|
|
|
|
2015-09-02 16:37:26 +00:00
|
|
|
it("does not allow parents without composition", function () {
|
|
|
|
parentCandidate = domainObjectFactory({
|
|
|
|
name: 'parentCandidate'
|
|
|
|
});
|
|
|
|
object.id = 'abc';
|
|
|
|
parentCandidate.id = 'xyz';
|
2015-09-22 21:07:46 +00:00
|
|
|
parentCandidate.hasCapability.andCallFake(function (c) {
|
|
|
|
return c !== 'composition';
|
|
|
|
});
|
2015-09-02 16:37:26 +00:00
|
|
|
expect(validate()).toBe(false);
|
|
|
|
});
|
|
|
|
|
2015-06-11 18:58:53 +00:00
|
|
|
describe("defers to policyService", function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
object.id = 'abc';
|
2015-06-12 16:39:24 +00:00
|
|
|
object.capabilities.type = { type: 'object' };
|
2015-06-11 18:58:53 +00:00
|
|
|
parentCandidate.id = 'xyz';
|
2015-06-12 16:39:24 +00:00
|
|
|
parentCandidate.capabilities.type = {
|
|
|
|
type: 'parentCandidate'
|
|
|
|
};
|
2015-06-11 18:58:53 +00:00
|
|
|
parentCandidate.model.composition = [];
|
|
|
|
});
|
|
|
|
|
2015-06-12 16:39:24 +00:00
|
|
|
it("calls policy service with correct args", function () {
|
|
|
|
validate();
|
|
|
|
expect(mockPolicyService.allow).toHaveBeenCalledWith(
|
|
|
|
"composition",
|
|
|
|
parentCandidate.capabilities.type,
|
|
|
|
object.capabilities.type
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-06-11 18:58:53 +00:00
|
|
|
it("and returns false", function () {
|
|
|
|
mockPolicyService.allow.andReturn(true);
|
|
|
|
expect(validate()).toBe(true);
|
|
|
|
expect(mockPolicyService.allow).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("and returns true", function () {
|
|
|
|
mockPolicyService.allow.andReturn(false);
|
|
|
|
expect(validate()).toBe(false);
|
|
|
|
expect(mockPolicyService.allow).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("perform", function () {
|
|
|
|
|
|
|
|
var object,
|
2015-08-06 22:05:42 +00:00
|
|
|
linkedObject,
|
2015-06-11 18:58:53 +00:00
|
|
|
parentModel,
|
|
|
|
parentObject,
|
2015-08-06 22:05:42 +00:00
|
|
|
compositionPromise,
|
|
|
|
persistencePromise,
|
2015-09-02 16:37:26 +00:00
|
|
|
addPromise,
|
2015-08-06 22:05:42 +00:00
|
|
|
compositionCapability,
|
2015-06-11 18:58:53 +00:00
|
|
|
persistenceCapability;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
2015-08-06 22:05:42 +00:00
|
|
|
compositionPromise = new ControlledPromise();
|
|
|
|
persistencePromise = new ControlledPromise();
|
2015-09-02 16:37:26 +00:00
|
|
|
addPromise = new ControlledPromise();
|
2015-06-11 18:58:53 +00:00
|
|
|
persistenceCapability = jasmine.createSpyObj(
|
|
|
|
'persistenceCapability',
|
|
|
|
['persist']
|
|
|
|
);
|
2015-08-06 22:05:42 +00:00
|
|
|
persistenceCapability.persist.andReturn(persistencePromise);
|
|
|
|
compositionCapability = jasmine.createSpyObj(
|
|
|
|
'compositionCapability',
|
2015-09-02 16:37:26 +00:00
|
|
|
['invoke', 'add']
|
2015-08-06 22:05:42 +00:00
|
|
|
);
|
|
|
|
compositionCapability.invoke.andReturn(compositionPromise);
|
2015-09-02 16:37:26 +00:00
|
|
|
compositionCapability.add.andReturn(addPromise);
|
2015-06-11 18:58:53 +00:00
|
|
|
parentModel = {
|
|
|
|
composition: []
|
|
|
|
};
|
|
|
|
parentObject = domainObjectFactory({
|
|
|
|
name: 'parentObject',
|
|
|
|
model: parentModel,
|
|
|
|
capabilities: {
|
|
|
|
mutation: {
|
|
|
|
invoke: function (mutator) {
|
|
|
|
mutator(parentModel);
|
2015-09-02 16:37:26 +00:00
|
|
|
return new ControlledPromise();
|
2015-06-11 18:58:53 +00:00
|
|
|
}
|
|
|
|
},
|
2015-08-06 22:05:42 +00:00
|
|
|
persistence: persistenceCapability,
|
|
|
|
composition: compositionCapability
|
2015-06-11 18:58:53 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
object = domainObjectFactory({
|
|
|
|
name: 'object',
|
|
|
|
id: 'xyz'
|
|
|
|
});
|
|
|
|
|
2015-08-06 22:05:42 +00:00
|
|
|
linkedObject = domainObjectFactory({
|
|
|
|
name: 'object-link',
|
|
|
|
id: 'xyz'
|
|
|
|
});
|
|
|
|
|
2015-06-11 18:58:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2015-09-02 16:37:26 +00:00
|
|
|
it("adds to the parent's composition", function () {
|
|
|
|
expect(compositionCapability.add).not.toHaveBeenCalled();
|
2015-06-11 18:58:53 +00:00
|
|
|
linkService.perform(object, parentObject);
|
2015-09-02 16:37:26 +00:00
|
|
|
expect(compositionCapability.add)
|
|
|
|
.toHaveBeenCalledWith(object);
|
2015-06-11 18:58:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("persists parent", function () {
|
|
|
|
linkService.perform(object, parentObject);
|
2015-09-02 16:37:26 +00:00
|
|
|
expect(addPromise.then).toHaveBeenCalled();
|
2015-09-02 18:45:52 +00:00
|
|
|
addPromise.resolve(linkedObject);
|
2015-06-11 18:58:53 +00:00
|
|
|
expect(parentObject.getCapability)
|
|
|
|
.toHaveBeenCalledWith('persistence');
|
|
|
|
expect(persistenceCapability.persist).toHaveBeenCalled();
|
|
|
|
});
|
2015-08-06 21:55:50 +00:00
|
|
|
|
|
|
|
it("returns object representing new link", function () {
|
2015-08-06 22:05:42 +00:00
|
|
|
var returnPromise, whenComplete;
|
|
|
|
returnPromise = linkService.perform(object, parentObject);
|
|
|
|
whenComplete = jasmine.createSpy('whenComplete');
|
|
|
|
returnPromise.then(whenComplete);
|
|
|
|
|
2015-09-02 18:45:52 +00:00
|
|
|
addPromise.resolve(linkedObject);
|
2015-08-06 22:05:42 +00:00
|
|
|
persistencePromise.resolve();
|
|
|
|
compositionPromise.resolve([linkedObject]);
|
2015-08-06 22:16:16 +00:00
|
|
|
expect(whenComplete).toHaveBeenCalledWith(linkedObject);
|
2015-08-06 21:55:50 +00:00
|
|
|
});
|
2015-09-22 22:49:22 +00:00
|
|
|
|
2015-09-23 19:43:03 +00:00
|
|
|
it("throws an error when performed on invalid inputs", function () {
|
2015-09-22 22:49:22 +00:00
|
|
|
function perform() {
|
|
|
|
linkService.perform(object, parentObject);
|
|
|
|
}
|
|
|
|
|
2015-09-23 19:43:03 +00:00
|
|
|
spyOn(linkService, 'validate');
|
|
|
|
linkService.validate.andReturn(true);
|
2015-09-22 22:49:22 +00:00
|
|
|
expect(perform).not.toThrow();
|
2015-09-23 19:43:03 +00:00
|
|
|
linkService.validate.andReturn(false);
|
2015-09-22 22:49:22 +00:00
|
|
|
expect(perform).toThrow();
|
|
|
|
});
|
2015-06-11 18:58:53 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|