|
| 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