Skip to content
This repository has been archived by the owner on Mar 8, 2020. It is now read-only.

Commit

Permalink
[0.19.x] cache enableHistorian flag (#4574)
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Kelsey <d_kelsey@uk.ibm.com>
  • Loading branch information
Dave Kelsey authored and nklincoln committed Jan 31, 2019
1 parent a828b05 commit 2fea08d
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 34 deletions.
7 changes: 1 addition & 6 deletions packages/composer-runtime/lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class Context {
* @param {InstalledBusinessNetwork} installedBusinessNetwork Information associated with the installed business network
*/
constructor(engine, installedBusinessNetwork) {
const method = 'constructor';
if (!installedBusinessNetwork) {
throw new Error('No business network specified');
}
Expand All @@ -50,11 +49,7 @@ class Context {
this.installedBusinessNetwork = installedBusinessNetwork;
this.eventNumber = 0;
this.contextId = uuid.v4();
this.historianEnabled = true;
if (installedBusinessNetwork.getDefinition().getMetadata().getPackageJson().disableHistorian === true) {
LOG.debug(method, 'Historian disabled');
this.historianEnabled = false;
}
this.historianEnabled = installedBusinessNetwork.historianEnabled;
}

/**
Expand Down
9 changes: 9 additions & 0 deletions packages/composer-runtime/lib/installedbusinessnetwork.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
const AclCompiler = require('./aclcompiler');
const QueryCompiler = require('./querycompiler');
const ScriptCompiler = require('./scriptcompiler');
const Logger = require('composer-common').Logger;

const LOG = Logger.getLog('InstalledBusinessNetwork');

/**
* Data associated with the currently installed business network, used by Context.
Expand Down Expand Up @@ -53,11 +56,17 @@ class InstalledBusinessNetwork {
* @private
*/
constructor(networkInfo) {
const method = 'constructor';
this.definition = networkInfo.definition;
this.compiledScriptBundle = networkInfo.compiledScriptBundle;
this.compiledQueryBundle = networkInfo.compiledQueryBundle;
this.compiledAclBundle = networkInfo.compiledAclBundle;
this.archive = networkInfo.archive;
this.historianEnabled = true;
if (this.definition.getMetadata().getPackageJson().disableHistorian === true) {
LOG.debug(method, 'Historian disabled');
this.historianEnabled = false;
}
}

/**
Expand Down
32 changes: 4 additions & 28 deletions packages/composer-runtime/test/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const AccessController = require('../lib/accesscontroller');
const AclManager = require('composer-common').AclManager;
const Api = require('../lib/api');
const BusinessNetworkDefinition = require('composer-common').BusinessNetworkDefinition;
const BusinessNetworkMetadata = require('composer-common').BusinessNetworkMetadata;
const CompiledAclBundle = require('../lib/compiledaclbundle');
const CompiledQueryBundle = require('../lib/compiledquerybundle');
const CompiledScriptBundle = require('../lib/compiledscriptbundle');
Expand Down Expand Up @@ -77,39 +76,16 @@ describe('Context', () => {
}).should.throw(/No business network/i);
});

it('should enable historian if no request to disable is present', () => {
context.historianEnabled.should.be.true;
});

it('should disable historian if disableHistorian set to true', () => {
it('should disable historian if InstalledBusinessNetwork says disabled', () => {
const mockInstalledBusinessNetwork = sinon.createStubInstance(InstalledBusinessNetwork);
const mockBusinessNetworkDefinition = sinon.createStubInstance(BusinessNetworkDefinition);
mockInstalledBusinessNetwork.getDefinition.returns(mockBusinessNetworkDefinition);
const mockBusinessNetworkMetadata = sinon.createStubInstance(BusinessNetworkMetadata);
mockBusinessNetworkDefinition.getMetadata.returns(mockBusinessNetworkMetadata);
mockBusinessNetworkMetadata.getPackageJson.returns({'disableHistorian': true});
mockInstalledBusinessNetwork.historianEnabled = false;
context = new Context(mockEngine, mockInstalledBusinessNetwork);
context.historianEnabled.should.be.false;
});

it('should enable historian if disableHistorian set to false', () => {
const mockInstalledBusinessNetwork = sinon.createStubInstance(InstalledBusinessNetwork);
const mockBusinessNetworkDefinition = sinon.createStubInstance(BusinessNetworkDefinition);
mockInstalledBusinessNetwork.getDefinition.returns(mockBusinessNetworkDefinition);
const mockBusinessNetworkMetadata = sinon.createStubInstance(BusinessNetworkMetadata);
mockBusinessNetworkDefinition.getMetadata.returns(mockBusinessNetworkMetadata);
mockBusinessNetworkMetadata.getPackageJson.returns({'disableHistorian': false});
context = new Context(mockEngine, mockInstalledBusinessNetwork);
context.historianEnabled.should.be.true;
});

it('should disable historian if disableHistorian set to a non boolean', () => {
it('should enable historian if InstalledBusinessNetwork says enabled', () => {
const mockInstalledBusinessNetwork = sinon.createStubInstance(InstalledBusinessNetwork);
const mockBusinessNetworkDefinition = sinon.createStubInstance(BusinessNetworkDefinition);
mockInstalledBusinessNetwork.getDefinition.returns(mockBusinessNetworkDefinition);
const mockBusinessNetworkMetadata = sinon.createStubInstance(BusinessNetworkMetadata);
mockBusinessNetworkDefinition.getMetadata.returns(mockBusinessNetworkMetadata);
mockBusinessNetworkMetadata.getPackageJson.returns({'disableHistorian': 1});
mockInstalledBusinessNetwork.historianEnabled = true;
context = new Context(mockEngine, mockInstalledBusinessNetwork);
context.historianEnabled.should.be.true;
});
Expand Down
69 changes: 69 additions & 0 deletions packages/composer-runtime/test/installedbusinessnetwork.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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.
*/

'use strict';

const InstalledBusinessNetwork = require('../lib/installedbusinessnetwork');
const BusinessNetworkDefinition = require('composer-common').BusinessNetworkDefinition;
const BusinessNetworkMetadata = require('composer-common').BusinessNetworkMetadata;

const chai = require('chai');
chai.use(require('chai-as-promised'));
const sinon = require('sinon');


describe('InstalledBusinessNetwork', () => {
describe('#constructor', () => {

let mockBusinessNetworkDefinition;
let mockBusinessNetworkMetadata;
let mockNetworkInfo;
beforeEach(() => {
mockBusinessNetworkDefinition = sinon.createStubInstance(BusinessNetworkDefinition);
mockBusinessNetworkMetadata = sinon.createStubInstance(BusinessNetworkMetadata);
mockBusinessNetworkDefinition.getMetadata.returns(mockBusinessNetworkMetadata);
mockNetworkInfo = {
definition: mockBusinessNetworkDefinition,
compiledScriptBundle: 'scriptBundle',
compiledQueryBundle: 'queryBundle',
compiledAclBundle: 'aclBundle',
archive: 'archive'
};
});

it('should enable historian if no request to disable is present', () => {
mockBusinessNetworkMetadata.getPackageJson.returns({'something': 'text'});
const installedBusinessNetwork = new InstalledBusinessNetwork(mockNetworkInfo);
installedBusinessNetwork.historianEnabled.should.be.true;
});

it('should disable historian if disableHistorian set to true', () => {
mockBusinessNetworkMetadata.getPackageJson.returns({'disableHistorian': true});
const installedBusinessNetwork = new InstalledBusinessNetwork(mockNetworkInfo);
installedBusinessNetwork.historianEnabled.should.be.false;
});

it('should enable historian if disableHistorian set to false', () => {
mockBusinessNetworkMetadata.getPackageJson.returns({'disableHistorian': false});
const installedBusinessNetwork = new InstalledBusinessNetwork(mockNetworkInfo);
installedBusinessNetwork.historianEnabled.should.be.true;
});

it('should disable historian if disableHistorian set to a non boolean', () => {
mockBusinessNetworkMetadata.getPackageJson.returns({'disableHistorian': 1});
const installedBusinessNetwork = new InstalledBusinessNetwork(mockNetworkInfo);
installedBusinessNetwork.historianEnabled.should.be.true;
});
});
});

0 comments on commit 2fea08d

Please sign in to comment.