2015-05-13 23:42:35 +00:00
|
|
|
/*****************************************************************************
|
2017-04-05 21:35:12 +00:00
|
|
|
* Open MCT, Copyright (c) 2014-2017, United States Government
|
2015-05-13 23:42:35 +00:00
|
|
|
* as represented by the Administrator of the National Aeronautics and Space
|
|
|
|
* Administration. All rights reserved.
|
|
|
|
*
|
2016-07-12 23:21:58 +00:00
|
|
|
* Open MCT is licensed under the Apache License, Version 2.0 (the
|
2015-05-13 23:42:35 +00:00
|
|
|
* "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.
|
|
|
|
*
|
2016-07-12 23:21:58 +00:00
|
|
|
* Open MCT includes source code licensed under additional open source
|
2015-05-13 23:42:35 +00:00
|
|
|
* 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.
|
|
|
|
*****************************************************************************/
|
2014-11-29 04:29:30 +00:00
|
|
|
|
|
|
|
define(
|
|
|
|
["../src/TelemetryCapability"],
|
|
|
|
function (TelemetryCapability) {
|
|
|
|
|
|
|
|
describe("The telemetry capability", function () {
|
|
|
|
var mockInjector,
|
|
|
|
mockQ,
|
|
|
|
mockLog,
|
|
|
|
mockDomainObject,
|
|
|
|
mockTelemetryService,
|
|
|
|
mockReject,
|
2014-12-30 22:00:20 +00:00
|
|
|
mockUnsubscribe,
|
2017-02-22 03:04:28 +00:00
|
|
|
telemetry,
|
2017-02-22 03:53:56 +00:00
|
|
|
mockTelemetryAPI,
|
|
|
|
mockAPI;
|
2014-11-29 04:29:30 +00:00
|
|
|
|
|
|
|
function mockPromise(value) {
|
|
|
|
return {
|
|
|
|
then: function (callback) {
|
|
|
|
return mockPromise(callback(value));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-02-22 03:53:56 +00:00
|
|
|
function noop() {
|
|
|
|
}
|
|
|
|
|
2014-11-29 04:29:30 +00:00
|
|
|
beforeEach(function () {
|
|
|
|
mockInjector = jasmine.createSpyObj("$injector", ["get"]);
|
|
|
|
mockQ = jasmine.createSpyObj("$q", ["when", "reject"]);
|
|
|
|
mockLog = jasmine.createSpyObj("$log", ["warn", "info", "debug"]);
|
|
|
|
mockDomainObject = jasmine.createSpyObj(
|
|
|
|
"domainObject",
|
2016-05-19 18:29:13 +00:00
|
|
|
["getId", "getCapability", "getModel"]
|
2014-11-29 04:29:30 +00:00
|
|
|
);
|
|
|
|
mockTelemetryService = jasmine.createSpyObj(
|
|
|
|
"telemetryService",
|
2016-05-19 18:29:13 +00:00
|
|
|
["requestTelemetry", "subscribe"]
|
2014-11-29 04:29:30 +00:00
|
|
|
);
|
|
|
|
mockReject = jasmine.createSpyObj("reject", ["then"]);
|
2014-12-30 22:00:20 +00:00
|
|
|
mockUnsubscribe = jasmine.createSpy("unsubscribe");
|
2014-11-29 04:29:30 +00:00
|
|
|
|
|
|
|
mockInjector.get.andReturn(mockTelemetryService);
|
|
|
|
|
|
|
|
mockQ.when.andCallFake(mockPromise);
|
|
|
|
mockQ.reject.andReturn(mockReject);
|
|
|
|
|
|
|
|
mockDomainObject.getId.andReturn("testId");
|
|
|
|
mockDomainObject.getModel.andReturn({
|
|
|
|
telemetry: {
|
|
|
|
source: "testSource",
|
|
|
|
key: "testKey"
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-12-30 21:45:42 +00:00
|
|
|
mockTelemetryService.requestTelemetry
|
|
|
|
.andReturn(mockPromise({}));
|
2014-12-30 22:00:20 +00:00
|
|
|
mockTelemetryService.subscribe
|
|
|
|
.andReturn(mockUnsubscribe);
|
2014-12-30 21:45:42 +00:00
|
|
|
|
2014-11-29 04:29:30 +00:00
|
|
|
// Bubble up...
|
|
|
|
mockReject.then.andReturn(mockReject);
|
|
|
|
|
2017-02-22 03:53:56 +00:00
|
|
|
mockTelemetryAPI = jasmine.createSpyObj("telemetryAPI", [
|
|
|
|
"getMetadata",
|
|
|
|
"subscribe",
|
|
|
|
"request",
|
|
|
|
"findRequestProvider",
|
|
|
|
"findSubscriptionProvider"
|
|
|
|
]);
|
|
|
|
mockTelemetryAPI.getMetadata.andReturn({
|
|
|
|
valuesForHints: function () {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
});
|
2017-02-22 03:04:28 +00:00
|
|
|
|
|
|
|
mockAPI = {
|
2017-02-28 00:07:56 +00:00
|
|
|
telemetry: mockTelemetryAPI,
|
|
|
|
conductor: {
|
|
|
|
bounds: function () {
|
|
|
|
return {
|
|
|
|
start: 0,
|
|
|
|
end: 1
|
|
|
|
};
|
2017-03-27 21:53:41 +00:00
|
|
|
},
|
|
|
|
timeSystem: function () {
|
|
|
|
return {
|
|
|
|
metadata: {
|
|
|
|
key: 'mockTimeSystem'
|
|
|
|
}
|
|
|
|
};
|
2017-02-28 00:07:56 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-22 03:04:28 +00:00
|
|
|
};
|
|
|
|
|
2014-11-29 04:29:30 +00:00
|
|
|
telemetry = new TelemetryCapability(
|
2017-02-22 03:04:28 +00:00
|
|
|
mockAPI,
|
2014-11-29 04:29:30 +00:00
|
|
|
mockInjector,
|
|
|
|
mockQ,
|
|
|
|
mockLog,
|
|
|
|
mockDomainObject
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("applies only to objects with telemetry sources", function () {
|
|
|
|
expect(TelemetryCapability.appliesTo({
|
|
|
|
telemetry: { source: "testSource" }
|
|
|
|
})).toBeTruthy();
|
|
|
|
expect(TelemetryCapability.appliesTo({
|
|
|
|
xtelemetry: { source: "testSource" }
|
|
|
|
})).toBeFalsy();
|
|
|
|
expect(TelemetryCapability.appliesTo({})).toBeFalsy();
|
|
|
|
expect(TelemetryCapability.appliesTo()).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("gets a telemetry service from the injector", function () {
|
|
|
|
telemetry.requestData();
|
|
|
|
expect(mockInjector.get)
|
|
|
|
.toHaveBeenCalledWith("telemetryService");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("applies request arguments", function () {
|
|
|
|
telemetry.requestData({ start: 42 });
|
|
|
|
expect(mockTelemetryService.requestTelemetry)
|
|
|
|
.toHaveBeenCalledWith([{
|
|
|
|
id: "testId", // from domain object
|
|
|
|
source: "testSource", // from model
|
|
|
|
key: "testKey", // from model
|
2017-03-27 21:53:41 +00:00
|
|
|
start: 42, // from argument
|
|
|
|
domain: 'mockTimeSystem'
|
2014-11-29 04:29:30 +00:00
|
|
|
}]);
|
|
|
|
});
|
|
|
|
|
2015-07-14 22:51:13 +00:00
|
|
|
it("provides an empty series when telemetry is missing", function () {
|
|
|
|
var series;
|
|
|
|
mockTelemetryService.requestTelemetry.andReturn(mockPromise({}));
|
2016-05-19 18:29:13 +00:00
|
|
|
telemetry.requestData({}).then(function (s) {
|
|
|
|
series = s;
|
|
|
|
});
|
2015-07-14 22:51:13 +00:00
|
|
|
expect(series.getPointCount()).toEqual(0);
|
|
|
|
});
|
|
|
|
|
2014-11-29 04:29:30 +00:00
|
|
|
it("provides telemetry metadata", function () {
|
|
|
|
expect(telemetry.getMetadata()).toEqual({
|
|
|
|
id: "testId", // from domain object
|
|
|
|
source: "testSource",
|
2017-02-28 00:07:56 +00:00
|
|
|
key: "testKey",
|
|
|
|
start: 0,
|
2017-03-27 21:53:41 +00:00
|
|
|
end: 1,
|
|
|
|
domain: 'mockTimeSystem'
|
2014-11-29 04:29:30 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("uses domain object as a key if needed", function () {
|
|
|
|
// Don't include key in telemetry
|
|
|
|
mockDomainObject.getModel.andReturn({
|
|
|
|
telemetry: { source: "testSource" }
|
|
|
|
});
|
|
|
|
|
|
|
|
// Should have used the domain object's ID
|
|
|
|
expect(telemetry.getMetadata()).toEqual({
|
|
|
|
id: "testId", // from domain object
|
|
|
|
source: "testSource", // from model
|
2017-02-28 00:07:56 +00:00
|
|
|
key: "testId", // from domain object
|
|
|
|
start: 0,
|
2017-03-27 21:53:41 +00:00
|
|
|
end: 1,
|
|
|
|
domain: 'mockTimeSystem'
|
2014-11-29 04:29:30 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it("warns if no telemetry service can be injected", function () {
|
2016-05-19 18:29:13 +00:00
|
|
|
mockInjector.get.andCallFake(function () {
|
|
|
|
throw "";
|
|
|
|
});
|
2014-11-29 04:29:30 +00:00
|
|
|
|
|
|
|
// Verify precondition
|
|
|
|
expect(mockLog.warn).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
telemetry.requestData();
|
|
|
|
|
|
|
|
expect(mockLog.warn).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2017-02-22 03:53:56 +00:00
|
|
|
it("if a new style telemetry source is available, use it", function () {
|
|
|
|
var mockProvider = {};
|
|
|
|
mockTelemetryAPI.findSubscriptionProvider.andReturn(mockProvider);
|
|
|
|
telemetry.subscribe(noop, {});
|
|
|
|
expect(mockTelemetryService.subscribe).not.toHaveBeenCalled();
|
|
|
|
expect(mockTelemetryAPI.subscribe).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("if a new style telemetry source is not available, revert to old API", function () {
|
|
|
|
mockTelemetryAPI.findSubscriptionProvider.andReturn(undefined);
|
|
|
|
telemetry.subscribe(noop, {});
|
|
|
|
expect(mockTelemetryAPI.subscribe).not.toHaveBeenCalled();
|
|
|
|
expect(mockTelemetryService.subscribe).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Wraps telemetry returned from the new API as a telemetry series", function () {
|
|
|
|
var returnedTelemetry;
|
|
|
|
var mockTelemetry = [{
|
|
|
|
prop1: "val1",
|
|
|
|
prop2: "val2",
|
|
|
|
prop3: "val3"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
prop1: "val4",
|
|
|
|
prop2: "val5",
|
|
|
|
prop3: "val6"
|
|
|
|
}];
|
|
|
|
var mockProvider = {};
|
|
|
|
var dunzo = false;
|
|
|
|
|
|
|
|
mockTelemetryAPI.findRequestProvider.andReturn(mockProvider);
|
|
|
|
mockTelemetryAPI.request.andReturn(Promise.resolve(mockTelemetry));
|
|
|
|
|
|
|
|
telemetry.requestData({}).then(function (data) {
|
|
|
|
returnedTelemetry = data;
|
|
|
|
dunzo = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
waitsFor(function () {
|
|
|
|
return dunzo;
|
|
|
|
});
|
|
|
|
|
|
|
|
runs(function () {
|
|
|
|
expect(returnedTelemetry.getPointCount).toBeDefined();
|
|
|
|
expect(returnedTelemetry.getDomainValue).toBeDefined();
|
|
|
|
expect(returnedTelemetry.getRangeValue).toBeDefined();
|
|
|
|
expect(returnedTelemetry.getPointCount()).toBe(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-12-30 22:00:20 +00:00
|
|
|
it("allows subscriptions to updates", function () {
|
|
|
|
var mockCallback = jasmine.createSpy("callback"),
|
|
|
|
subscription = telemetry.subscribe(mockCallback);
|
|
|
|
|
|
|
|
// Verify subscription to the appropriate object
|
|
|
|
expect(mockTelemetryService.subscribe).toHaveBeenCalledWith(
|
|
|
|
jasmine.any(Function),
|
|
|
|
[{
|
|
|
|
id: "testId", // from domain object
|
|
|
|
source: "testSource",
|
2017-02-28 00:07:56 +00:00
|
|
|
key: "testKey",
|
|
|
|
start: 0,
|
2017-03-27 21:53:41 +00:00
|
|
|
end: 1,
|
|
|
|
domain: 'mockTimeSystem'
|
2014-12-30 22:00:20 +00:00
|
|
|
}]
|
|
|
|
);
|
|
|
|
|
|
|
|
// Check that the callback gets invoked
|
|
|
|
expect(mockCallback).not.toHaveBeenCalled();
|
|
|
|
mockTelemetryService.subscribe.mostRecentCall.args[0]({
|
|
|
|
testSource: { testKey: { someKey: "some value" } }
|
|
|
|
});
|
|
|
|
expect(mockCallback).toHaveBeenCalledWith(
|
|
|
|
{ someKey: "some value" }
|
|
|
|
);
|
|
|
|
|
|
|
|
// Finally, unsubscribe
|
|
|
|
expect(mockUnsubscribe).not.toHaveBeenCalled();
|
|
|
|
subscription(); // should be an unsubscribe function
|
|
|
|
expect(mockUnsubscribe).toHaveBeenCalled();
|
2017-03-27 21:53:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("applies time conductor bounds if request bounds not defined", function () {
|
|
|
|
var fullRequest = telemetry.buildRequest({});
|
|
|
|
var mockBounds = mockAPI.conductor.bounds();
|
|
|
|
|
|
|
|
expect(fullRequest.start).toBe(mockBounds.start);
|
|
|
|
expect(fullRequest.end).toBe(mockBounds.end);
|
|
|
|
|
|
|
|
fullRequest = telemetry.buildRequest({start: 10, end: 20});
|
|
|
|
|
|
|
|
expect(fullRequest.start).toBe(10);
|
|
|
|
expect(fullRequest.end).toBe(20);
|
|
|
|
});
|
2014-12-30 22:00:20 +00:00
|
|
|
|
2017-03-27 21:53:41 +00:00
|
|
|
it("applies domain from time system if none defined", function () {
|
|
|
|
var fullRequest = telemetry.buildRequest({});
|
|
|
|
var mockTimeSystem = mockAPI.conductor.timeSystem();
|
|
|
|
expect(fullRequest.domain).toBe(mockTimeSystem.metadata.key);
|
2014-12-30 22:00:20 +00:00
|
|
|
|
2017-03-27 21:53:41 +00:00
|
|
|
fullRequest = telemetry.buildRequest({domain: 'someOtherDomain'});
|
|
|
|
expect(fullRequest.domain).toBe('someOtherDomain');
|
2014-12-30 22:00:20 +00:00
|
|
|
});
|
2014-11-29 04:29:30 +00:00
|
|
|
});
|
|
|
|
}
|
2015-07-14 22:51:13 +00:00
|
|
|
);
|