Skip to content

Commit a269429

Browse files
author
ehennum
committed
negative proxy generation tests #534
1 parent 69325cc commit a269429

File tree

2 files changed

+197
-0
lines changed

2 files changed

+197
-0
lines changed

lib/proxy-generator.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,25 @@ function generateSessionFactoryDoc(className, sessionFactoryName) {
593593
}
594594

595595
function generateModuleSource(moduleName, servicedef, endpointdefs) {
596+
if (moduleName === void 0 || moduleName === null) {
597+
throw new Error(`missing module name`);
598+
} else if (servicedef === void 0 || servicedef === null) {
599+
throw new Error(`missing service.json declaration`);
600+
} else if (servicedef.endpointDirectory === void 0 || servicedef.endpointDirectory === null) {
601+
throw new Error(`service.json declaration without endpointDirectory property`);
602+
} else if (!Array.isArray(endpointdefs) || endpointdefs.length === 0) {
603+
throw new Error(`no endpoint pairs of *.api declaration and main module`);
604+
} else {
605+
endpointdefs.forEach(endpoint => {
606+
if (endpoint.moduleExtension === void 0 || endpoint.moduleExtension === null) {
607+
throw new Error(`endpoint without moduleExtension property`);
608+
} else if (endpoint.declaration === void 0 || endpoint.declaration === null) {
609+
throw new Error(`endpoint without declaration`);
610+
} else if (endpoint.declaration.functionName === void 0 || endpoint.declaration.functionName === null) {
611+
throw new Error(`endpoint declaration without functionName property`);
612+
}
613+
});
614+
}
596615
const proxyAST = buildModule(moduleName, servicedef, endpointdefs);
597616
return generate(proxyAST, {comments: true});
598617
}
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/*
2+
* Copyright 2019 MarkLogic Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
'use strict';
17+
18+
const expect = require('chai').expect;
19+
20+
const proxy = require('../../../lib/proxy-generator.js');
21+
22+
describe('negative generation', function() {
23+
it('without module name', function(done) {
24+
try {
25+
const serviceDecl = {"endpointDirectory" : "/directory/"};
26+
const endpoints = [{moduleExtension: ".sjs", declaration:{"functionName" : "funcname"}}];
27+
const proxySrc = proxy.generateSource(null, serviceDecl, endpoints);
28+
expect.fail('expected exception instead of generated proxy source');
29+
} catch(err) {
30+
expect(err.toString()).to.have.string('missing module name');
31+
done();
32+
}
33+
});
34+
it('without service declaration', function(done) {
35+
try {
36+
const endpoints = [{moduleExtension: ".sjs", declaration:{"functionName" : "funcname"}}];
37+
const proxySrc = proxy.generateSource('module.js', null, endpoints);
38+
expect.fail('expected exception instead of generated proxy source');
39+
} catch(err) {
40+
expect(err.toString()).to.have.string('missing service.json declaration');
41+
done();
42+
}
43+
});
44+
it('with service declaration lacking endpointDirectory', function(done) {
45+
try {
46+
const serviceDecl = {"incorrectProperty" : "/directory/"};
47+
const endpoints = [{moduleExtension: ".sjs", declaration:{"functionName" : "funcname"}}];
48+
const proxySrc = proxy.generateSource('module.js', serviceDecl, endpoints);
49+
expect.fail('expected exception instead of generated proxy source');
50+
} catch(err) {
51+
expect(err.toString()).to.have.string('service.json declaration without endpointDirectory property');
52+
done();
53+
}
54+
});
55+
it('without endpoints', function(done) {
56+
try {
57+
const serviceDecl = {"endpointDirectory" : "/directory/"};
58+
const proxySrc = proxy.generateSource('module.js', serviceDecl, null);
59+
expect.fail('expected exception instead of generated proxy source');
60+
} catch(err) {
61+
expect(err.toString()).to.have.string('no endpoint pairs of *.api declaration and main module');
62+
done();
63+
}
64+
});
65+
it('with empty endpoints', function(done) {
66+
try {
67+
const serviceDecl = {"endpointDirectory" : "/directory/"};
68+
const proxySrc = proxy.generateSource('module.js', serviceDecl, []);
69+
expect.fail('expected exception instead of generated proxy source');
70+
} catch(err) {
71+
expect(err.toString()).to.have.string('no endpoint pairs of *.api declaration and main module');
72+
done();
73+
}
74+
});
75+
it('with endpoint lacking moduleExtension', function(done) {
76+
try {
77+
const serviceDecl = {"endpointDirectory" : "/directory/"};
78+
const endpoints = [{incorrectProperty: ".sjs", declaration:{"functionName" : "funcname"}}];
79+
const proxySrc = proxy.generateSource('module.js', serviceDecl, endpoints);
80+
expect.fail('expected exception instead of generated proxy source');
81+
} catch(err) {
82+
expect(err.toString()).to.have.string('endpoint without moduleExtension property');
83+
done();
84+
}
85+
});
86+
it('with endpoint lacking function declaration', function(done) {
87+
try {
88+
const serviceDecl = {"endpointDirectory" : "/directory/"};
89+
const endpoints = [{moduleExtension: ".sjs", incorrectProperty:{"functionName" : "funcname"}}];
90+
const proxySrc = proxy.generateSource('module.js', serviceDecl, endpoints);
91+
expect.fail('expected exception instead of generated proxy source');
92+
} catch(err) {
93+
expect(err.toString()).to.have.string('endpoint without declaration');
94+
done();
95+
}
96+
});
97+
it('with endpoint lacking function name declaration', function(done) {
98+
try {
99+
const serviceDecl = {"endpointDirectory" : "/directory/"};
100+
const endpoints = [{moduleExtension: ".sjs", declaration:{"incorrectProperty" : "funcname"}}];
101+
const proxySrc = proxy.generateSource('module.js', serviceDecl, endpoints);
102+
expect.fail('expected exception instead of generated proxy source');
103+
} catch(err) {
104+
expect(err.toString()).to.have.string('endpoint declaration without functionName property');
105+
done();
106+
}
107+
});
108+
it('with endpoint mapping boolean return value to number', function(done) {
109+
try {
110+
const serviceDecl = {"endpointDirectory" : "/directory/"};
111+
const endpoints = [{moduleExtension: ".sjs", declaration:{
112+
"functionName" : "funcname",
113+
"return" : {"datatype" : "boolean", "$jsType" : "number"}
114+
}}];
115+
const proxySrc = proxy.generateSource('module.js', serviceDecl, endpoints);
116+
expect.fail('expected exception instead of generated proxy source');
117+
} catch(err) {
118+
expect(err.toString()).to.have.string('optional $jsType number can only be "boolean" or "string" for datatype boolean return value');
119+
done();
120+
}
121+
});
122+
it('with endpoint mapping float return value to boolean', function(done) {
123+
try {
124+
const serviceDecl = {"endpointDirectory" : "/directory/"};
125+
const endpoints = [{moduleExtension: ".sjs", declaration:{
126+
"functionName" : "funcname",
127+
"return" : {"datatype" : "float", "$jsType" : "boolean"}
128+
}}];
129+
const proxySrc = proxy.generateSource('module.js', serviceDecl, endpoints);
130+
expect.fail('expected exception instead of generated proxy source');
131+
} catch(err) {
132+
expect(err.toString()).to.have.string('optional $jsType boolean can only be "number" or "string" for datatype float return value');
133+
done();
134+
}
135+
});
136+
it('with endpoint mapping int return value to boolean', function(done) {
137+
try {
138+
const serviceDecl = {"endpointDirectory" : "/directory/"};
139+
const endpoints = [{moduleExtension: ".sjs", declaration:{
140+
"functionName" : "funcname",
141+
"return" : {"datatype" : "int", "$jsType" : "Date"}
142+
}}];
143+
const proxySrc = proxy.generateSource('module.js', serviceDecl, endpoints);
144+
expect.fail('expected exception instead of generated proxy source');
145+
} catch(err) {
146+
expect(err.toString()).to.have.string('optional $jsType Date can only be "number" or "string" for datatype int return value');
147+
done();
148+
}
149+
});
150+
it('with endpoint mapping long return value to number', function(done) {
151+
try {
152+
const serviceDecl = {"endpointDirectory" : "/directory/"};
153+
const endpoints = [{moduleExtension: ".sjs", declaration:{
154+
"functionName" : "funcname",
155+
"return" : {"datatype" : "long", "$jsType" : "number"}
156+
}}];
157+
const proxySrc = proxy.generateSource('module.js', serviceDecl, endpoints);
158+
expect.fail('expected exception instead of generated proxy source');
159+
} catch(err) {
160+
expect(err.toString()).to.have.string('optional $jsType number can only be "string" for datatype long return value');
161+
done();
162+
}
163+
});
164+
it('with endpoint mapping time return value to invalid data type', function(done) {
165+
try {
166+
const serviceDecl = {"endpointDirectory" : "/directory/"};
167+
const endpoints = [{moduleExtension: ".sjs", declaration:{
168+
"functionName" : "funcname",
169+
"return" : {"datatype" : "time", "$jsType" : "invalid"}
170+
}}];
171+
const proxySrc = proxy.generateSource('module.js', serviceDecl, endpoints);
172+
expect.fail('expected exception instead of generated proxy source');
173+
} catch(err) {
174+
expect(err.toString()).to.have.string('optional $jsType invalid can only be "string" for datatype time return value');
175+
done();
176+
}
177+
});
178+
});

0 commit comments

Comments
 (0)