2015-05-13 23:42:35 +00:00
|
|
|
/*****************************************************************************
|
2018-05-14 22:46:17 +00:00
|
|
|
* Open MCT, Copyright (c) 2014-2018, 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/TelemetryAggregator"],
|
|
|
|
function (TelemetryAggregator) {
|
|
|
|
|
|
|
|
describe("The telemetry aggregator", function () {
|
|
|
|
var mockQ,
|
|
|
|
mockProviders,
|
2014-12-30 22:00:20 +00:00
|
|
|
mockUnsubscribes,
|
2014-11-29 04:29:30 +00:00
|
|
|
aggregator;
|
|
|
|
|
|
|
|
function mockPromise(value) {
|
|
|
|
return {
|
|
|
|
then: function (callback) {
|
|
|
|
return mockPromise(callback(value));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-05-20 18:39:49 +00:00
|
|
|
function makeMockProvider(key, index) {
|
2014-11-29 04:29:30 +00:00
|
|
|
var provider = jasmine.createSpyObj(
|
2014-12-30 22:00:20 +00:00
|
|
|
"provider" + index,
|
2016-05-19 18:29:13 +00:00
|
|
|
["requestTelemetry", "subscribe"]
|
2014-12-30 22:00:20 +00:00
|
|
|
),
|
|
|
|
unsubscribe = jasmine.createSpy("unsubscribe" + index);
|
2018-06-30 00:32:59 +00:00
|
|
|
provider.requestTelemetry.and.returnValue({ someKey: key });
|
|
|
|
provider.subscribe.and.returnValue(unsubscribe);
|
2014-12-30 22:00:20 +00:00
|
|
|
|
|
|
|
// Store to verify interactions later
|
|
|
|
mockUnsubscribes[index] = unsubscribe;
|
2014-11-29 04:29:30 +00:00
|
|
|
return provider;
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(function () {
|
2016-05-19 18:29:13 +00:00
|
|
|
mockQ = jasmine.createSpyObj("$q", ["all"]);
|
2018-06-30 00:32:59 +00:00
|
|
|
mockQ.all.and.returnValue(mockPromise([]));
|
2014-11-29 04:29:30 +00:00
|
|
|
|
2014-12-30 22:00:20 +00:00
|
|
|
mockUnsubscribes = [];
|
2016-05-20 18:39:49 +00:00
|
|
|
mockProviders = ["a", "b", "c"].map(makeMockProvider);
|
2014-11-29 04:29:30 +00:00
|
|
|
|
|
|
|
aggregator = new TelemetryAggregator(mockQ, mockProviders);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("passes requests to aggregated providers", function () {
|
|
|
|
var requests = [
|
|
|
|
{ someKey: "some value" },
|
|
|
|
{ someKey: "some other value" }
|
|
|
|
];
|
|
|
|
|
|
|
|
aggregator.requestTelemetry(requests);
|
|
|
|
|
|
|
|
mockProviders.forEach(function (mockProvider) {
|
|
|
|
expect(mockProvider.requestTelemetry)
|
|
|
|
.toHaveBeenCalledWith(requests);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("merges results from all providers", function () {
|
|
|
|
var capture = jasmine.createSpy("capture");
|
|
|
|
|
2018-06-30 00:32:59 +00:00
|
|
|
mockQ.all.and.returnValue(mockPromise([
|
2014-11-29 04:29:30 +00:00
|
|
|
{ someKey: "some value" },
|
|
|
|
{ someOtherKey: "some other value" }
|
|
|
|
]));
|
|
|
|
|
|
|
|
aggregator.requestTelemetry().then(capture);
|
|
|
|
|
|
|
|
// Verify that aggregator results were run through
|
|
|
|
// $q.all
|
|
|
|
expect(mockQ.all).toHaveBeenCalledWith([
|
|
|
|
{ someKey: 'a' },
|
|
|
|
{ someKey: 'b' },
|
|
|
|
{ someKey: 'c' }
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(capture).toHaveBeenCalledWith({
|
|
|
|
someKey: "some value",
|
|
|
|
someOtherKey: "some other value"
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-12-30 22:00:20 +00:00
|
|
|
it("broadcasts subscriptions from all providers", function () {
|
|
|
|
var mockCallback = jasmine.createSpy("callback"),
|
|
|
|
subscription = aggregator.subscribe(mockCallback);
|
|
|
|
|
|
|
|
// Make sure all providers got subscribed to
|
|
|
|
mockProviders.forEach(function (mockProvider) {
|
|
|
|
expect(mockProvider.subscribe).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Verify that unsubscription gets broadcast too
|
|
|
|
mockUnsubscribes.forEach(function (mockUnsubscribe) {
|
|
|
|
expect(mockUnsubscribe).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
subscription(); // unsubscribe
|
|
|
|
mockUnsubscribes.forEach(function (mockUnsubscribe) {
|
|
|
|
expect(mockUnsubscribe).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2014-11-29 04:29:30 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
}
|
2016-05-19 18:29:13 +00:00
|
|
|
);
|