Skip to content

Commit a8ed947

Browse files
author
Chris Wiechmann
committed
Making MockElasticsearchMethod available
to external clients to run tests.
1 parent 07b88a2 commit a8ed947

3 files changed

Lines changed: 40 additions & 5 deletions

File tree

api-builder-plugin-fn-elasticsearch/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## [1.0.14] 2020-12-09
8+
### Added
9+
- New method: mockElasticsearchMethod to mock requests to Elasticsearch
10+
711
## [1.0.13] 2020-12-09
812
### Changed
913
- Test & Logging of Elasticsearch connection improved

api-builder-plugin-fn-elasticsearch/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@axway-api-builder-ext/api-builder-plugin-fn-elasticsearch",
3-
"version": "1.0.13",
3+
"version": "1.0.14",
44
"description": "Integrate Elasticsearch into your API-Builder flow to combine search data for instance with other data available in your flow.",
55
"author": "Chris Wiechmann <cwiechmann@axway.com> (http://www.axway.com)",
66
"license": "Apache-2.0",
@@ -32,16 +32,16 @@
3232
"dependencies": {
3333
"@axway/api-builder-sdk": "^1.0.8",
3434
"@elastic/elasticsearch": "^7.10.0",
35-
"deep-equal": "^2.0.4"
35+
"deep-equal": "^2.0.4",
36+
"simple-mock": "^0.8.0"
3637
},
3738
"peerDependencies": {
3839
"@axway/api-builder-runtime": "^4.41.1"
3940
},
4041
"devDependencies": {
4142
"@axway/api-builder-test-utils": "^1.1.4",
4243
"chai": "^4.2.0",
43-
"mocha": "^7.2.0",
44-
"simple-mock": "^0.8.0"
44+
"mocha": "^7.2.0"
4545
},
4646
"scripts": {
4747
"test": "mocha ./test --recursive -R spec",

api-builder-plugin-fn-elasticsearch/src/actions/ElasticsearchClient.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const { Client } = require('@elastic/elasticsearch');
2+
const simple = require('simple-mock');
3+
const fs = require('fs');
24

35
class ElasticsearchClient {
46

@@ -17,4 +19,33 @@ class ElasticsearchClient {
1719
}
1820
}
1921

20-
module.exports = { ElasticsearchClient }
22+
function mockElasticsearchMethod(client, methodName, responeFilename, shouldError) {
23+
var mockedFn = simple.spy(function (params, options, callback) {
24+
const resonse = JSON.parse(fs.readFileSync(responeFilename), null);
25+
if(callback != undefined) {
26+
if(shouldError) {
27+
// Return the given response as an error
28+
callback(resonse, null);
29+
} else {
30+
// Otherwise return the response as expected
31+
callback(null, resonse);
32+
}
33+
} else {
34+
return new Promise((resolve, reject) => {
35+
if(shouldError) {
36+
reject(resonse);
37+
} else {
38+
resolve(resonse);
39+
}
40+
});
41+
}
42+
});
43+
// Use the extend functionality of the ES-Client to register the mocked method
44+
client.extend(methodName, { force: true }, ({ makeRequest }) => {
45+
return mockedFn;
46+
});
47+
// Return the mocked function to perform assertions
48+
return mockedFn;
49+
}
50+
51+
module.exports = { ElasticsearchClient, mockElasticsearchMethod }

0 commit comments

Comments
 (0)