-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathinterfacemanager.test.js
199 lines (171 loc) · 6.25 KB
/
interfacemanager.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
var assert = require( 'assert' );
var InterfaceManager = require( '../lib-cov/interfacemanager' );
var interfaceManager;
describe( 'InterfaceManager', function() {
it( 'can not be initalized by error path', function() {
assert.throws( function() {
new InterfaceManager( 'error/path' );
}, function( err ) {
return err.toString()
.indexOf( 'Fail to load interface profiles.Error' ) !== -1;
} );
interfaceManager = new InterfaceManager( '../tests/interface_test.json' );
assert.equal( interfaceManager instanceof InterfaceManager, true );
} );
it( 'can not be initalized when no status is specified', function() {
assert.throws( function() {
new InterfaceManager({});
}, function( err ) {
return err.toString()
.indexOf( 'There is no status specified' ) !== -1;
} );
} );
it( 'should throw error if the interface configuration is not a json ', function() {
assert.throws( function() {
new InterfaceManager( '../tests/README.md' );
}, function( err ) {
return err.toString()
.indexOf( 'syntax error' ) !== -1;
} );
} );
} );
describe( 'interfaceManager', function() {
var ifmgr = new InterfaceManager( {
status: 'online'
} );
describe( '#_addProfile()', function() {
it( 'can not be added without id', function() {
assert.equal( ifmgr._addProfile( {
urls: {
online: 'http://url1'
}
} ), false );
} );
it( 'can not be added when the interface id does not match ^((\\w+\\.)*\\w+)$', function() {
assert.equal( ifmgr._addProfile( {
id: 'Abc-methodName',
urls: {
online: 'http://url1'
}
} ), false );
assert.equal( ifmgr._addProfile( {
id: 'Abc.methodName.',
urls: {
online: 'http://url1'
}
} ), false );
assert.equal( ifmgr._addProfile( {
id: 'Abc.methodName',
urls: {
online: 'http://url1'
}
} ), true );
} );
it( 'can not add duplicated interface id', function() {
assert.equal( ifmgr._addProfile( {
id: 'Abc.methodName',
urls: {
online: 'htpp://url1'
}
} ), false );
assert.equal( ifmgr._addProfile( {
id: 'Abc.method1',
urls: {
online: 'htpp://url1'
}
} ), true );
} );
it( 'must have at least one url in urls or its rulefile is available', function() {
ifmgr._rulebase = '.';
assert.equal( ifmgr._addProfile( {
id: 'Abc.method2',
urls: {}
} ), false );
assert.equal( ifmgr._addProfile( {
id: 'Abc.method2',
ruleFile: 'unavailable.rule.json'
} ), false );
} );
} );
describe( '#getInterfaceIdsByPrefix()', function() {
it( 'should have nothing matched when the prefix is not proper', function() {
assert.equal( interfaceManager.getInterfaceIdsByPrefix( 'Prefix' ).length, 0 );
} );
it ( 'should return an array of interface when the prefix is right', function() {
assert.notEqual( interfaceManager.getInterfaceIdsByPrefix( 'Search.' ).length, 0 );
} );
} );
describe( '#isProfileExisted()', function() {
it( 'should return false when the interface id does not exist', function() {
assert.equal( interfaceManager.isProfileExisted( 'Search.getItem' ), false );
} );
it( 'should return true when the interface id exists', function() {
assert.equal( interfaceManager.isProfileExisted( 'Search.getNav' ), true );
} );
} );
describe( '#_isUrlsValid()', function() {
it( 'should return false when the urls is null or an empty object', function() {
assert.equal( interfaceManager._isUrlsValid( null ), false );
assert.equal( interfaceManager._isUrlsValid( {} ), false );
} );
it( 'should return true when the urls is not null and has at least one url', function() {
assert.equal( interfaceManager._isUrlsValid( { daily: 'http://url1' } ), true );
assert.equal( interfaceManager._isUrlsValid( { daily: 'http://url1', online: 'http://url2' } ), true );
} );
} );
describe( '#getProfile()', function() {
it( 'should return undefined if the given id is not existed', function() {
assert.strictEqual( interfaceManager.getProfile( 'Search.item' ), undefined );
} );
it( 'should renturn the profile if the given interface id is existed', function() {
assert.equal( typeof interfaceManager.getProfile( 'Cart.getMyCart' ), 'object' );
} );
} );
describe( '#getRule()', function() {
it( 'should return rule of the given id', function() {
assert.equal( typeof interfaceManager.getRule( 'Search.list' ), 'object' );
} );
it( 'should throw error when the rule file does not exist', function() {
assert.throws( function() {
interfaceManager.getRule( 'Test.post' );
}, function( err ) {
return err.toString()
.indexOf( 'The rule file is not existed.' ) !== -1;
} );
} );
it( 'should throw error when the rule file is not a json', function() {
assert.throws( function() {
interfaceManager.getRule( 'Search.suggest' );
}, function( err ) {
return err.toString()
.indexOf( 'Rule file has syntax error' ) !== -1;
} );
} );
it( 'should throw error when the interface id is not found', function() {
assert.throws( function() {
interfaceManager.getRule( 'Error.id' );
}, function( err ) {
return err.toString()
.indexOf( 'is not found' ) !== -1;
} );
} );
} );
describe( '#getEngine()', function() {
it( 'should return mockjs', function() {
assert.strictEqual( interfaceManager.getEngine(), 'mockjs' );
} );
} );
describe( '#getStatus()', function() {
it( 'should return online', function() {
assert.strictEqual( interfaceManager.getStatus(), 'online' );
} );
} );
it( 'clientInterfaces should be initalized after the object of interfaceManager is created', function() {
var clientInterfaces = interfaceManager.getClientInterfaces();
console.log( clientInterfaces );
assert.notEqual( clientInterfaces, null );
var cnt = 0;
for ( var i in clientInterfaces ) cnt++;
assert.equal( cnt, 7 );
} );
} );