Skip to content

Commit b17d249

Browse files
committed
Endpoint Caller unit tests.
1 parent 08b22f3 commit b17d249

File tree

3 files changed

+129
-1
lines changed

3 files changed

+129
-1
lines changed

lib/endpoint-proxy.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,7 @@ class EndpointCaller {
835835
const copyEndpointDeclaration = mlutil.copyProperties(endpointDeclaration);
836836

837837
copyEndpointDeclaration.functionName = endpointFunctionName;
838+
expandFunctionDeclaration(copyEndpointDeclaration);
838839

839840
this.$mlProxy = client.createProxy(serviceDeclaration).withFunction(copyEndpointDeclaration, endpointExtension);
840841

test-basic/endpoint-caller.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright (c) 2021 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+
17+
var marklogic = require('../');
18+
var fs = require('fs');
19+
var testconfig = require('../etc/test-config.js');
20+
var expect = require('chai').expect;
21+
var dbWriter = marklogic.createDatabaseClient(testconfig.restWriterConnection);
22+
23+
describe('Endpoint caller', function() {
24+
25+
it('moduleInitSjs endpoint', function(done) {
26+
const serviceDeclaration = JSON.parse(fs.readFileSync('test-basic-proxy/ml-modules/generated/moduleInitSjs/service.json', {encoding: 'utf8'}));
27+
const endpointDeclaration = JSON.parse(fs.readFileSync('test-basic-proxy/ml-modules/generated/moduleInitSjs/initializer.api', {encoding: 'utf8'}));
28+
endpointDeclaration.endpoint = serviceDeclaration.endpointDirectory+'initializer'+'.sjs';
29+
const endpointCaller = dbWriter.endpointCaller(endpointDeclaration);
30+
const params = {param1:true, param2: '1.2', param3: [1.2, 3.4], param4:[5, 6] };
31+
32+
endpointCaller.call(params)
33+
.then(output => {
34+
expect(output).to.eql(true);
35+
done();
36+
})
37+
.catch(err => {
38+
expect.fail(err.toString());
39+
done();
40+
});
41+
});
42+
43+
it('moduleInitXqy endpoint', function(done) {
44+
const serviceDeclaration = JSON.parse(fs.readFileSync('test-basic-proxy/ml-modules/generated/moduleInitXqy/service.json', {encoding: 'utf8'}));
45+
const endpointDeclaration = JSON.parse(fs.readFileSync('test-basic-proxy/ml-modules/generated/moduleInitXqy/initializer.api', {encoding: 'utf8'}));
46+
endpointDeclaration.endpoint = serviceDeclaration.endpointDirectory+'initializer'+'.xqy';
47+
const endpointCaller = dbWriter.endpointCaller(endpointDeclaration);
48+
const params = {param1:true, param2: '1.2', param3: [1.2, 3.4], param4:[5, 6] };
49+
50+
endpointCaller.call(params)
51+
.then(output => {
52+
expect(output).to.eql(true);
53+
done();
54+
})
55+
.catch(err => {
56+
expect.fail(err.toString());
57+
done();
58+
});
59+
});
60+
61+
it('postOfMultipartForDocumentArray1 endpoint', function(done) {
62+
const serviceDeclaration = JSON.parse(fs.readFileSync('test-basic-proxy/ml-modules/generated/postOfMultipartForDocument/service.json',
63+
{encoding: 'utf8'}));
64+
const endpointDeclaration = JSON.parse(fs.readFileSync('test-basic-proxy/ml-modules/generated/postOfMultipartForDocument/postOfMultipartForDocumentArray1.api',
65+
{encoding: 'utf8'}));
66+
endpointDeclaration.endpoint = serviceDeclaration.endpointDirectory+'postOfMultipartForDocumentArray1'+'.sjs';
67+
const endpointCaller = dbWriter.endpointCaller(endpointDeclaration);
68+
const params = {param1:[{"root":{"child":"text1"}}, {"root":{"child":"text2"}}]};
69+
70+
endpointCaller.call(params)
71+
.then(output => {
72+
expect(output).to.eql(["text1", 1]);
73+
done();
74+
})
75+
.catch(err => {
76+
expect.fail(err.toString());
77+
done();
78+
});
79+
});
80+
81+
it('postOfUrlencodedForDocumentArray1 endpoint', function(done) {
82+
const serviceDeclaration = JSON.parse(fs.readFileSync('test-basic-proxy/ml-modules/generated/postOfUrlencodedForDocument/service.json',
83+
{encoding: 'utf8'}));
84+
const endpointDeclaration = JSON.parse(fs.readFileSync('test-basic-proxy/ml-modules/generated/postOfUrlencodedForDocument/postOfUrlencodedForDocumentArray1.api',
85+
{encoding: 'utf8'}));
86+
endpointDeclaration.endpoint = serviceDeclaration.endpointDirectory+'postOfUrlencodedForDocumentArray1'+'.mjs';
87+
const endpointCaller = dbWriter.endpointCaller(endpointDeclaration);
88+
const params = {param1:[1.2, 3.4]};
89+
90+
endpointCaller.call(params)
91+
.then(output => {
92+
expect(output).to.eql(["text1", 1]);
93+
done();
94+
})
95+
.catch(err => {
96+
expect.fail(err.toString());
97+
done();
98+
});
99+
});
100+
});

test-basic/service-caller.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('Service caller', function() {
2525
it('moduleInitSjs endpoint', function(done) {
2626
const serviceDeclaration = JSON.parse(fs.readFileSync('test-basic-proxy/ml-modules/generated/moduleInitSjs/service.json', {encoding: 'utf8'}));
2727
serviceDeclaration.endpointExtension = '.sjs';
28-
const endpointDeclaration = JSON.parse(fs.readFileSync('test-basic-proxy/ml-modules/generated/moduleInitMjs/initializer.api', {encoding: 'utf8'}));
28+
const endpointDeclaration = JSON.parse(fs.readFileSync('test-basic-proxy/ml-modules/generated/moduleInitSjs/initializer.api', {encoding: 'utf8'}));
2929
const serviceCaller = dbWriter.serviceCaller(serviceDeclaration, [endpointDeclaration]);
3030
const params = {param1:true, param2: '1.2', param3: [1.2, 3.4], param4:[5, 6] };
3131

@@ -97,4 +97,31 @@ describe('Service caller', function() {
9797
done();
9898
});
9999
});
100+
101+
it('verify support for multiple endpoint declarations', function(done) {
102+
const serviceDeclaration = JSON.parse(fs.readFileSync('test-basic-proxy/ml-modules/generated/postOfMultipartForDocument/service.json',
103+
{encoding: 'utf8'}));
104+
serviceDeclaration.endpointExtension = '.sjs';
105+
const endpointDeclaration1 = JSON.parse(fs.readFileSync('test-basic-proxy/ml-modules/generated/postOfMultipartForDocument/postOfMultipartForDocumentArray1.api',
106+
{encoding: 'utf8'}));
107+
const endpointDeclaration2 = JSON.parse(fs.readFileSync('test-basic-proxy/ml-modules/generated/postOfMultipartForDocument/postOfMultipartForDocumentTextDocument1.api',
108+
{encoding: 'utf8'}));
109+
const serviceCaller = dbWriter.serviceCaller(serviceDeclaration, [endpointDeclaration1, endpointDeclaration2]);
110+
const params1 = {param1:[1.2, 3.4]};
111+
const params2 = {param1:[{"root":{"child":"text1"}}, {"root":{"child":"text2"}}]};
112+
113+
serviceCaller.call('postOfMultipartForDocumentArray1', params1)
114+
.then(output => {
115+
expect(output).to.eql(["text1", 1]);
116+
return serviceCaller.call('postOfMultipartForDocumentTextDocument1', params2);
117+
})
118+
.then(output => {
119+
expect(output).to.eql('abc');
120+
done();
121+
})
122+
.catch(err => {
123+
expect.fail(err.toString());
124+
done();
125+
});
126+
});
100127
});

0 commit comments

Comments
 (0)