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

Commit 2fea08d

Browse files
Dave Kelseynklincoln
Dave Kelsey
authored andcommitted
[0.19.x] cache enableHistorian flag (#4574)
Signed-off-by: Dave Kelsey <d_kelsey@uk.ibm.com>
1 parent a828b05 commit 2fea08d

File tree

4 files changed

+83
-34
lines changed

4 files changed

+83
-34
lines changed

packages/composer-runtime/lib/context.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ class Context {
4141
* @param {InstalledBusinessNetwork} installedBusinessNetwork Information associated with the installed business network
4242
*/
4343
constructor(engine, installedBusinessNetwork) {
44-
const method = 'constructor';
4544
if (!installedBusinessNetwork) {
4645
throw new Error('No business network specified');
4746
}
@@ -50,11 +49,7 @@ class Context {
5049
this.installedBusinessNetwork = installedBusinessNetwork;
5150
this.eventNumber = 0;
5251
this.contextId = uuid.v4();
53-
this.historianEnabled = true;
54-
if (installedBusinessNetwork.getDefinition().getMetadata().getPackageJson().disableHistorian === true) {
55-
LOG.debug(method, 'Historian disabled');
56-
this.historianEnabled = false;
57-
}
52+
this.historianEnabled = installedBusinessNetwork.historianEnabled;
5853
}
5954

6055
/**

packages/composer-runtime/lib/installedbusinessnetwork.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
const AclCompiler = require('./aclcompiler');
1818
const QueryCompiler = require('./querycompiler');
1919
const ScriptCompiler = require('./scriptcompiler');
20+
const Logger = require('composer-common').Logger;
21+
22+
const LOG = Logger.getLog('InstalledBusinessNetwork');
2023

2124
/**
2225
* Data associated with the currently installed business network, used by Context.
@@ -53,11 +56,17 @@ class InstalledBusinessNetwork {
5356
* @private
5457
*/
5558
constructor(networkInfo) {
59+
const method = 'constructor';
5660
this.definition = networkInfo.definition;
5761
this.compiledScriptBundle = networkInfo.compiledScriptBundle;
5862
this.compiledQueryBundle = networkInfo.compiledQueryBundle;
5963
this.compiledAclBundle = networkInfo.compiledAclBundle;
6064
this.archive = networkInfo.archive;
65+
this.historianEnabled = true;
66+
if (this.definition.getMetadata().getPackageJson().disableHistorian === true) {
67+
LOG.debug(method, 'Historian disabled');
68+
this.historianEnabled = false;
69+
}
6170
}
6271

6372
/**

packages/composer-runtime/test/context.js

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ const AccessController = require('../lib/accesscontroller');
1919
const AclManager = require('composer-common').AclManager;
2020
const Api = require('../lib/api');
2121
const BusinessNetworkDefinition = require('composer-common').BusinessNetworkDefinition;
22-
const BusinessNetworkMetadata = require('composer-common').BusinessNetworkMetadata;
2322
const CompiledAclBundle = require('../lib/compiledaclbundle');
2423
const CompiledQueryBundle = require('../lib/compiledquerybundle');
2524
const CompiledScriptBundle = require('../lib/compiledscriptbundle');
@@ -77,39 +76,16 @@ describe('Context', () => {
7776
}).should.throw(/No business network/i);
7877
});
7978

80-
it('should enable historian if no request to disable is present', () => {
81-
context.historianEnabled.should.be.true;
82-
});
83-
84-
it('should disable historian if disableHistorian set to true', () => {
79+
it('should disable historian if InstalledBusinessNetwork says disabled', () => {
8580
const mockInstalledBusinessNetwork = sinon.createStubInstance(InstalledBusinessNetwork);
86-
const mockBusinessNetworkDefinition = sinon.createStubInstance(BusinessNetworkDefinition);
87-
mockInstalledBusinessNetwork.getDefinition.returns(mockBusinessNetworkDefinition);
88-
const mockBusinessNetworkMetadata = sinon.createStubInstance(BusinessNetworkMetadata);
89-
mockBusinessNetworkDefinition.getMetadata.returns(mockBusinessNetworkMetadata);
90-
mockBusinessNetworkMetadata.getPackageJson.returns({'disableHistorian': true});
81+
mockInstalledBusinessNetwork.historianEnabled = false;
9182
context = new Context(mockEngine, mockInstalledBusinessNetwork);
9283
context.historianEnabled.should.be.false;
9384
});
9485

95-
it('should enable historian if disableHistorian set to false', () => {
96-
const mockInstalledBusinessNetwork = sinon.createStubInstance(InstalledBusinessNetwork);
97-
const mockBusinessNetworkDefinition = sinon.createStubInstance(BusinessNetworkDefinition);
98-
mockInstalledBusinessNetwork.getDefinition.returns(mockBusinessNetworkDefinition);
99-
const mockBusinessNetworkMetadata = sinon.createStubInstance(BusinessNetworkMetadata);
100-
mockBusinessNetworkDefinition.getMetadata.returns(mockBusinessNetworkMetadata);
101-
mockBusinessNetworkMetadata.getPackageJson.returns({'disableHistorian': false});
102-
context = new Context(mockEngine, mockInstalledBusinessNetwork);
103-
context.historianEnabled.should.be.true;
104-
});
105-
106-
it('should disable historian if disableHistorian set to a non boolean', () => {
86+
it('should enable historian if InstalledBusinessNetwork says enabled', () => {
10787
const mockInstalledBusinessNetwork = sinon.createStubInstance(InstalledBusinessNetwork);
108-
const mockBusinessNetworkDefinition = sinon.createStubInstance(BusinessNetworkDefinition);
109-
mockInstalledBusinessNetwork.getDefinition.returns(mockBusinessNetworkDefinition);
110-
const mockBusinessNetworkMetadata = sinon.createStubInstance(BusinessNetworkMetadata);
111-
mockBusinessNetworkDefinition.getMetadata.returns(mockBusinessNetworkMetadata);
112-
mockBusinessNetworkMetadata.getPackageJson.returns({'disableHistorian': 1});
88+
mockInstalledBusinessNetwork.historianEnabled = true;
11389
context = new Context(mockEngine, mockInstalledBusinessNetwork);
11490
context.historianEnabled.should.be.true;
11591
});
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
'use strict';
16+
17+
const InstalledBusinessNetwork = require('../lib/installedbusinessnetwork');
18+
const BusinessNetworkDefinition = require('composer-common').BusinessNetworkDefinition;
19+
const BusinessNetworkMetadata = require('composer-common').BusinessNetworkMetadata;
20+
21+
const chai = require('chai');
22+
chai.use(require('chai-as-promised'));
23+
const sinon = require('sinon');
24+
25+
26+
describe('InstalledBusinessNetwork', () => {
27+
describe('#constructor', () => {
28+
29+
let mockBusinessNetworkDefinition;
30+
let mockBusinessNetworkMetadata;
31+
let mockNetworkInfo;
32+
beforeEach(() => {
33+
mockBusinessNetworkDefinition = sinon.createStubInstance(BusinessNetworkDefinition);
34+
mockBusinessNetworkMetadata = sinon.createStubInstance(BusinessNetworkMetadata);
35+
mockBusinessNetworkDefinition.getMetadata.returns(mockBusinessNetworkMetadata);
36+
mockNetworkInfo = {
37+
definition: mockBusinessNetworkDefinition,
38+
compiledScriptBundle: 'scriptBundle',
39+
compiledQueryBundle: 'queryBundle',
40+
compiledAclBundle: 'aclBundle',
41+
archive: 'archive'
42+
};
43+
});
44+
45+
it('should enable historian if no request to disable is present', () => {
46+
mockBusinessNetworkMetadata.getPackageJson.returns({'something': 'text'});
47+
const installedBusinessNetwork = new InstalledBusinessNetwork(mockNetworkInfo);
48+
installedBusinessNetwork.historianEnabled.should.be.true;
49+
});
50+
51+
it('should disable historian if disableHistorian set to true', () => {
52+
mockBusinessNetworkMetadata.getPackageJson.returns({'disableHistorian': true});
53+
const installedBusinessNetwork = new InstalledBusinessNetwork(mockNetworkInfo);
54+
installedBusinessNetwork.historianEnabled.should.be.false;
55+
});
56+
57+
it('should enable historian if disableHistorian set to false', () => {
58+
mockBusinessNetworkMetadata.getPackageJson.returns({'disableHistorian': false});
59+
const installedBusinessNetwork = new InstalledBusinessNetwork(mockNetworkInfo);
60+
installedBusinessNetwork.historianEnabled.should.be.true;
61+
});
62+
63+
it('should disable historian if disableHistorian set to a non boolean', () => {
64+
mockBusinessNetworkMetadata.getPackageJson.returns({'disableHistorian': 1});
65+
const installedBusinessNetwork = new InstalledBusinessNetwork(mockNetworkInfo);
66+
installedBusinessNetwork.historianEnabled.should.be.true;
67+
});
68+
});
69+
});

0 commit comments

Comments
 (0)