From b744467f215cc0d0cb39d2cc482c2f6ec946cd9d Mon Sep 17 00:00:00 2001 From: Joshi Date: Tue, 25 Feb 2020 12:29:47 -0800 Subject: [PATCH] Bug fixes: Ensures that the default condition emits a condition result update Fixes remove condition --- src/plugins/condition/Condition.js | 6 +- src/plugins/condition/ConditionManager.js | 39 ++++--- src/plugins/condition/ConditionManagerSpec.js | 103 +++++++++++------- .../components/ConditionCollection.vue | 7 +- .../condition/criterion/TelemetryCriterion.js | 35 +++--- 5 files changed, 110 insertions(+), 80 deletions(-) diff --git a/src/plugins/condition/Condition.js b/src/plugins/condition/Condition.js index 9e0a26e041..7110e04811 100644 --- a/src/plugins/condition/Condition.js +++ b/src/plugins/condition/Condition.js @@ -58,11 +58,11 @@ export default class ConditionClass extends EventEmitter { this.id = this.openmct.objects.makeKeyString(conditionConfiguration.identifier); this.criteria = []; this.criteriaResults = {}; + this.result = null; if (conditionConfiguration.configuration.criteria) { this.createCriteria(conditionConfiguration.configuration.criteria); } this.trigger = conditionConfiguration.configuration.trigger; - this.result = null; this.openmct.objects.get(this.id).then(obj => this.observeForChanges(obj)); } @@ -199,9 +199,7 @@ export default class ConditionClass extends EventEmitter { subscribe() { this.criteria.forEach((criterion) => { - if (criterion.isValid()) { - criterion.subscribe(); - } + criterion.subscribe(); }) } diff --git a/src/plugins/condition/ConditionManager.js b/src/plugins/condition/ConditionManager.js index 66fd392fc2..d9c0a61638 100644 --- a/src/plugins/condition/ConditionManager.js +++ b/src/plugins/condition/ConditionManager.js @@ -91,6 +91,11 @@ export default class ConditionManager extends EventEmitter { } else { this.conditionCollection.unshift(condition); } + //There are no criteria for a default condition and hence no subscriptions. + //Hence the conditionResult must be manually triggered for it. + if (conditionConfiguration.isDefault) { + this.handleConditionResult(); + } } createConditionDomainObject(isDefault, conditionConfiguration) { @@ -160,7 +165,7 @@ export default class ConditionManager extends EventEmitter { if (found) { let index = found.index; let condition = this.conditionCollection[index]; - let conditionIdAsString = this.openmct.objects.makeKeyString(condition.identifier); + let conditionIdAsString = condition.id; condition.destroyCriteria(); condition.off('conditionResultUpdated', this.handleConditionResult.bind(this)); this.conditionCollection.splice(index, 1); @@ -169,6 +174,7 @@ export default class ConditionManager extends EventEmitter { delete this.conditionResults[conditionIdAsString]; } this.persist(); + this.handleConditionResult(); } } @@ -201,28 +207,33 @@ export default class ConditionManager extends EventEmitter { this.persist(); } - handleConditionResult(args) { - let idAsString = this.openmct.objects.makeKeyString(args.id); - let found = this.findConditionById(idAsString); + handleConditionResult(resultObj) { let conditionCollection = this.domainObject.configuration.conditionCollection; let currentConditionIdentifier = conditionCollection[conditionCollection.length-1]; - if (found) { - this.conditionResults[idAsString] = args.data.result; - for (let i = 0, ii = conditionCollection.length - 1; i < ii; i++) { - let conditionIdAsString = this.openmct.objects.makeKeyString(conditionCollection[i]); - if (this.conditionResults[conditionIdAsString]) { - //first condition to be true wins - currentConditionIdentifier = conditionCollection[i]; - break; - } + if (resultObj) { + let idAsString = this.openmct.objects.makeKeyString(resultObj.id); + let found = this.findConditionById(idAsString); + if (found) { + this.conditionResults[idAsString] = resultObj.data.result; + } + } + + for (let i = 0, ii = conditionCollection.length - 1; i < ii; i++) { + let conditionIdAsString = this.openmct.objects.makeKeyString(conditionCollection[i]); + if (this.conditionResults[conditionIdAsString]) { + //first condition to be true wins + currentConditionIdentifier = conditionCollection[i]; + break; } } this.openmct.objects.get(currentConditionIdentifier).then((obj) => { + console.log(obj.configuration.output); this.emit('conditionSetResultUpdated', { id: this.domainObject.identifier, - output: obj.configuration.output + output: obj.configuration.output, + conditionId: currentConditionIdentifier }) }); } diff --git a/src/plugins/condition/ConditionManagerSpec.js b/src/plugins/condition/ConditionManagerSpec.js index 6cea3710bf..c5e50e9694 100644 --- a/src/plugins/condition/ConditionManagerSpec.js +++ b/src/plugins/condition/ConditionManagerSpec.js @@ -25,55 +25,74 @@ import ConditionManager from './ConditionManager'; describe('ConditionManager', () => { let conditionMgr; - let testTelemetryObject; + let mockListener; let openmct = {}; - let parentDomainObject; - let composition; + let conditionSetDomainObject = { + identifier: { + namespace: "", + key: "600a7372-8d48-4dc4-98b6-548611b1ff7e" + }, + type: "conditionSet", + location: "mine", + configuration: { + conditionCollection: [] + } + }; + let mockConditionDomainObject = { + isDefault: true, + type: 'condition', + identifier: { + namespace: '', + key: '1234-5678' + } + }; - beforeAll(function () { - testTelemetryObject = { - identifier:{ namespace: "", key: "test-object"}, - type: "test-object", - name: "Test Object", - telemetry: { - values: [{ - key: "some-key", - name: "Some attribute", - hints: { - domain: 1 - } - }, { - key: "some-other-key", - name: "Another attribute", - hints: { - range: 1 - } - }] + function mockAngularComponents() { + let mockInjector = jasmine.createSpyObj('$injector', ['get']); + + let mockInstantiate = jasmine.createSpy('mockInstantiate'); + mockInstantiate.and.returnValue(mockInstantiate); + + let mockDomainObject = { + useCapability: function () { + return mockConditionDomainObject; } }; - openmct.objects = jasmine.createSpyObj('objects', ['get', 'makeKeyString']); - openmct.objects.get.and.returnValue(testTelemetryObject); - openmct.objects.makeKeyString.and.returnValue(testTelemetryObject.identifier.key); - openmct.telemetry = jasmine.createSpyObj('telemetry', ['isTelemetryObject']); - conditionMgr = new ConditionManager(openmct); - parentDomainObject = {}; - composition = {}; + mockInstantiate.and.callFake(function () { + return mockDomainObject; + }); + mockInjector.get.and.callFake(function (service) { + return { + 'instantiate': mockInstantiate + }[service]; + }); + + openmct.$injector = mockInjector; + } + + beforeAll(function () { + + mockAngularComponents(); + + openmct.objects = jasmine.createSpyObj('objects', ['get', 'makeKeyString', 'observe', 'mutate']); + openmct.objects.get.and.returnValues(new Promise(function (resolve, reject) { + resolve(conditionSetDomainObject); + }), new Promise(function (resolve, reject) { + resolve(mockConditionDomainObject); + })); + openmct.objects.makeKeyString.and.returnValue(conditionSetDomainObject.identifier.key); + openmct.objects.observe.and.returnValue(function () {}); + openmct.objects.mutate.and.returnValue(function () {}); + conditionMgr = new ConditionManager(conditionSetDomainObject, openmct); + mockListener = jasmine.createSpy('mockListener'); + + conditionMgr.on('conditionSetResultUpdated', mockListener); }); it('creates a conditionCollection with a default condition', function () { - - }); - - it('adds a condition to the collection with one criterion', function () { - - }); - - it('updates a condition\'s criteria to something valid', function () { - - }); - - it('removes a condition from the conditionCollection', function () { - + expect(conditionMgr.domainObject.configuration.conditionCollection.length).toEqual(1); + let defaultConditionIdentifier = conditionMgr.domainObject.configuration.conditionCollection[0]; + expect(defaultConditionIdentifier).toEqual(mockConditionDomainObject.identifier); }); }); diff --git a/src/plugins/condition/components/ConditionCollection.vue b/src/plugins/condition/components/ConditionCollection.vue index be38bebfb4..1c1c509498 100644 --- a/src/plugins/condition/components/ConditionCollection.vue +++ b/src/plugins/condition/components/ConditionCollection.vue @@ -200,12 +200,7 @@ export default { this.currentConditionIdentifier = identifier; }, removeCondition(identifier) { - let index = _.findIndex(this.conditionCollection, (condition) => { - let conditionId = this.openmct.objects.makeKeyString(condition); - let id = this.openmct.objects.makeKeyString(identifier); - return conditionId === id; - }); - this.conditionManager.removeCondition(null, index); + this.conditionManager.removeCondition(identifier); }, reorder(reorderPlan) { this.conditionManager.reorderConditions(reorderPlan); diff --git a/src/plugins/condition/criterion/TelemetryCriterion.js b/src/plugins/condition/criterion/TelemetryCriterion.js index 1ac7a2b150..b36c990cb2 100644 --- a/src/plugins/condition/criterion/TelemetryCriterion.js +++ b/src/plugins/condition/criterion/TelemetryCriterion.js @@ -72,17 +72,19 @@ export default class TelemetryCriterion extends EventEmitter { } computeResult(data) { - let comparator = this.findOperation(this.operation); - let params = []; let result = false; - params.push(data[this.metadata]); - if (this.input instanceof Array && this.input.length) { - params.push(this.input[0]); - } else if (this.input) { - params.push(this.input); - } - if (typeof comparator === 'function') { - result = comparator(params); + if (data) { + let comparator = this.findOperation(this.operation); + let params = []; + params.push(data[this.metadata]); + if (this.input instanceof Array && this.input.length) { + params.push(this.input[0]); + } else if (this.input) { + params.push(this.input); + } + if (typeof comparator === 'function') { + result = comparator(params); + } } return result; } @@ -100,12 +102,17 @@ export default class TelemetryCriterion extends EventEmitter { /** * Subscribes to the telemetry object and returns an unsubscribe function + * If the telemetry is not valid, returns nothing */ subscribe() { - this.unsubscribe(); - this.subscription = this.telemetryAPI.subscribe(this.telemetryObject, (datum) => { - this.handleSubscription(datum); - }); + if (this.isValid()) { + this.unsubscribe(); + this.subscription = this.telemetryAPI.subscribe(this.telemetryObject, (datum) => { + this.handleSubscription(datum); + }); + } else { + this.handleSubscription(); + } } /**