Skip to content

Commit

Permalink
Merge pull request #117 from fisuda/feature/check_element
Browse files Browse the repository at this point in the history
ADD feature to check if element is empty
  • Loading branch information
fisuda committed Feb 25, 2023
2 parents dcf94e4 + ea952da commit 341516e
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## node-red-contrib-letsfiware-NGSI v0.12.0-next

- ADD feature to check if element is empty (#116)
- UPDATE Node.js dependencies (#115)
- RENAME ppalette label for nodes (#114)
- UPDATE documentation (#113)
Expand Down
3 changes: 3 additions & 0 deletions src/nodes/NGSI/source/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ const getEntities = async function (param) {
try {
const res = await lib.http(options);
if (res.status === 200) {
if (res.data.length === 0) {
break;
}
param.buffer.send(res.data);
param.config.offset += param.config.limit;
if (totalCount <= 0) {
Expand Down
3 changes: 3 additions & 0 deletions src/nodes/NGSI/types/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ const getTypes = async function (param) {
try {
const res = await lib.http(options);
if (res.status === 200) {
if (res.data.length === 0) {
break;
}
types = types.concat(res.data);
param.config.offset += param.config.limit;
if (totalCount <= 0) {
Expand Down
25 changes: 25 additions & 0 deletions test/unit/source_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,31 @@ describe('source.js', () => {

await getEntities(param);
});
it('total count 0', async () => {
sourceNode.__set__('lib', {
http: async () => Promise.resolve({
status: 200,
headers: { 'fiware-total-count': 0 },
data: [{}]
}),
buildHTTPHeader: () => { return {}; },
buildParams: () => new URLSearchParams(),
});
const getEntities = sourceNode.__get__('getEntities');
const nobuffering = sourceNode.__get__('nobuffering');

const param = {
host: 'http://orion:1026',
pathname: '/v2/entities',
buffer: nobuffering.open({ send: () => { } }),
config: {
offset: 0,
limit: 2,
}
};

await getEntities(param);
});
it('should be 400 Bad Request', async () => {
sourceNode.__set__('lib', {
http: async () => Promise.resolve({ status: 400, statusText: 'Bad Request' }),
Expand Down
28 changes: 28 additions & 0 deletions test/unit/types_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,34 @@ describe('types.js', () => {

assert.deepEqual(actual, expected);
});
it('total count 0', async () => {
typesNode.__set__('lib', {
http: async () => Promise.resolve({
status: 200,
headers: { 'fiware-total-count': 0 },
data: [{}],
}),
buildHTTPHeader: () => { return {}; },
buildParams: () => new URLSearchParams(),
});
const getTypes = typesNode.__get__('getTypes');

const param = {
method: 'post',
host: 'http://orion:1026',
pathname: '/v2/types',
config: {
limit: 0,
offset: 0,
},
};

const actual = await getTypes(param);

const expected = [{}];

assert.deepEqual(actual, expected);
});
it('should be 400 Bad Request', async () => {
typesNode.__set__('lib', {
http: async () => Promise.resolve({ status: 400, statusText: 'Bad Request' }),
Expand Down

0 comments on commit 341516e

Please sign in to comment.