Skip to content

Commit

Permalink
Merge pull request telefonicaid#1461 from jason-fox/feature/fix-ngsi-ld
Browse files Browse the repository at this point in the history
Fix NGSI-LD test
  • Loading branch information
AlvaroVega authored Sep 21, 2023
2 parents 1791bcc + 7f97fae commit 0579bff
Show file tree
Hide file tree
Showing 11 changed files with 303 additions and 570 deletions.
Original file line number Diff line number Diff line change
@@ -1,49 +1,18 @@
[
{
"@context": "http://context.json-ld",
"id": "urn:ngsi-ld:SensorMachine:TheFirstLight",
"id": "urn:ngsi-ld:SensorMachine:Light1",
"type": "SensorMachine",
"hardcodedAttr": {
"status": {
"type": "Property",
"value": {
"@type": "hardcodedType",
"@value": "hardcodedValue"
}
"value": "STARTING"
},
"bootstrapServer": {
"type": "Property",
"value": {
"@type": "Address",
"@value": "127.0.0.1"
}
},
"commandAttr_status": {
"type": "Property",
"value": {
"@type": "commandStatus",
"@value": "UNKNOWN"
}
},
"commandAttr_info": {
"type": "Property",
"value": {
"@type": "commandResult",
"@value": " "
}
},
"wheel1_status": {
"type": "Property",
"value": {
"@type": "commandStatus",
"@value": "UNKNOWN"
}
},
"wheel1_info": {
"type": "Property",
"value": {
"@type": "commandResult",
"@value": " "
}
}
}
]
154 changes: 67 additions & 87 deletions test/unit/ngsi-ld/general/contextBrokerOAuthSecurityAccess-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,16 +289,6 @@ describe('NGSI-LD - Secured access to the Context Broker with OAuth2 provider',
)
.reply(201, null, { Location: '/ngsi-ld/v1/csourceRegistrations//6319a7f5254b05844116584d' });

contextBrokerMock
.post(
'/ngsi-ld/v1/entityOperations/upsert/',
utils.readExampleFile(
'./test/unit/ngsi-ld/examples/' +
'contextRequests/createProvisionedDeviceWithGroupAndStatic3.json'
)
)
.reply(204);

contextBrokerMock
.post(
'/ngsi-ld/v1/subscriptions/',
Expand All @@ -316,38 +306,38 @@ describe('NGSI-LD - Secured access to the Context Broker with OAuth2 provider',
});
});
});
// FIXME: disabled test by #1421
// it('subscribe requests use auth header', function (done) {
// iotAgentLib.getDevice('Light1', null, 'smartgondor', 'electricity', function (error, device) {
// iotAgentLib.subscribe(device, ['dimming'], null, function (error) {
// should.not.exist(error);

// contextBrokerMock.done();

// done();
// });
// });
// });
// FIXME: disabled test by #1421
// it('unsubscribe requests use auth header', function (done) {
// oauth2Mock
// .post(
// '/auth/realms/default/protocol/openid-connect/token',
// utils.readExampleFile('./test/unit/examples/oauthRequests/getTokenFromTrust.json', true)
// )
// .reply(201, utils.readExampleFile('./test/unit/examples/oauthResponses/tokenFromTrust.json'), {});

// contextBrokerMock.delete('/ngsi-ld/v1/subscriptions/51c0ac9ed714fb3b37d7d5a8', '').reply(204);

// iotAgentLib.getDevice('Light1', null, 'smartgondor', 'electricity', function (error, device) {
// iotAgentLib.subscribe(device, ['dimming'], null, function (error) {
// iotAgentLib.unsubscribe(device, '51c0ac9ed714fb3b37d7d5a8', function (error) {
// contextBrokerMock.done();
// done();
// });
// });
// });
// });

it('subscribe requests use auth header', function (done) {
iotAgentLib.getDevice('Light1', null, 'smartgondor', 'electricity', function (error, device) {
iotAgentLib.subscribe(device, ['dimming'], null, function (error) {
should.not.exist(error);

contextBrokerMock.done();

done();
});
});
});

it('unsubscribe requests use auth header', function (done) {
oauth2Mock
.post(
'/auth/realms/default/protocol/openid-connect/token',
utils.readExampleFile('./test/unit/examples/oauthRequests/getTokenFromTrust.json', true)
)
.reply(201, utils.readExampleFile('./test/unit/examples/oauthResponses/tokenFromTrust.json'), {});

contextBrokerMock.delete('/ngsi-ld/v1/subscriptions/51c0ac9ed714fb3b37d7d5a8', '').reply(204);

iotAgentLib.getDevice('Light1', null, 'smartgondor', 'electricity', function (error, device) {
iotAgentLib.subscribe(device, ['dimming'], null, function (error) {
iotAgentLib.unsubscribe(device, '51c0ac9ed714fb3b37d7d5a8', function (error) {
contextBrokerMock.done();
done();
});
});
});
});
});
});

Expand Down Expand Up @@ -745,23 +735,14 @@ describe(
.matchHeader('fiware-service', 'testservice')
.matchHeader('authorization', 'Bearer bea752e377680acd1349a3ed59db855a1db07zxc')
.post(
'/ngsi-ld/v1/entityOperations/upsert/',
'/ngsi-ld/v1/entityOperations/upsert/?options=update',
utils.readExampleFile(
'./test/unit/ngsi-ld/examples/' +
'contextRequests/createProvisionedDeviceWithGroupAndStatic2.json'
)
)
.reply(204);

contextBrokerMock3 = nock('http://unexistentHost:1026')
.matchHeader('fiware-service', 'testservice')
.matchHeader('authorization', 'Bearer zzz752e377680acd1349a3ed59db855a1db07bbb')
.post(
'/ngsi-ld/v1/entityOperations/upsert/?options=update',
utils.readExampleFile('./test/unit/ngsi-ld/examples/contextRequests/updateContext5.json')
)
.reply(204);

iotAgentConfig.authentication.tokenPath = '/oauth2/token';
iotAgentLib.activate(iotAgentConfig, function () {
done();
Expand All @@ -773,24 +754,23 @@ describe(

done();
});
// FIXME: disabled test by #1421
// it('should not raise any error', function (done) {
// request(deviceCreation, function (error, response, body) {
// should.not.exist(error);
// response.statusCode.should.equal(201);
// contextBrokerMock.done();
// contextBrokerMock2.done();
// done();
// });
// });
// FIXME: disabled test by #1421
// it('should send the mixed data to the Context Broker', function (done) {
// iotAgentLib.update('Light1', 'SensorMachine', '', values, function (error) {
// should.not.exist(error);
// contextBrokerMock3.done();
// done();
// });
// });

it('should not raise any error', function (done) {
request(deviceCreation, function (error, response, body) {
should.not.exist(error);
response.statusCode.should.equal(201);
contextBrokerMock.done();
done();
});
});

it('should send the mixed data to the Context Broker', function (done) {
iotAgentLib.update('Light1', 'SensorMachine', '', values, function (error) {
should.not.exist(error);
contextBrokerMock2.done();
done();
});
});
});
}
);
Expand Down Expand Up @@ -833,7 +813,7 @@ describe(

contextBrokerMock = nock('http://unexistentHost:1026')
.matchHeader('fiware-service', 'testservice')
.matchHeader('Authorization', 'Bearer 999210dacf913772606c95dd0b895d5506cbc988')
.matchHeader('Authorization', 'Bearer 000210dacf913772606c95dd0b895d5506cbc700')
.post(
'/ngsi-ld/v1/entityOperations/upsert/?options=update',
utils.readExampleFile(
Expand All @@ -849,22 +829,22 @@ describe(
});
});
});
// FIXME: disabled test by #1421
// it('should send the permanent token in the auth header', function (done) {
// iotAgentLib.update('machine1', 'SensorMachine', '', values, function (error) {
// should.not.exist(error);
// contextBrokerMock.done();
// done();
// });
// });
// FIXME: disabled test by #1421
// it('should use the permanent trust token in the following requests', function (done) {
// iotAgentLib.update('machine1', 'SensorMachine', '', values, function (error) {
// should.not.exist(error);
// contextBrokerMock.done();
// done();
// });
// });

it('should send the permanent token in the auth header', function (done) {
iotAgentLib.update('machine1', 'SensorMachine', '', values, function (error) {
should.not.exist(error);
contextBrokerMock.done();
done();
});
});

it('should use the permanent trust token in the following requests', function (done) {
iotAgentLib.update('machine1', 'SensorMachine', '', values, function (error) {
should.not.exist(error);
contextBrokerMock.done();
done();
});
});
});
}
);
20 changes: 7 additions & 13 deletions test/unit/ngsi-ld/lazyAndCommands/command-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,6 @@ describe('NGSI-LD - Command functionalities', function () {
)
.reply(201, null, { Location: '/ngsi-ld/v1/csourceRegistrations/6319a7f5254b05844116584d' });

contextBrokerMock
.matchHeader('fiware-service', 'smartgondor')
.post('/ngsi-ld/v1/entityOperations/upsert/')
.reply(204);

iotAgentLib.activate(iotAgentConfig, done);
});

Expand All @@ -155,14 +150,13 @@ describe('NGSI-LD - Command functionalities', function () {
});

describe('When a device is preregistered with commands', function () {
// FIXME: disabled test by #1421
// it('should register as Context Provider of the commands', function (done) {
// iotAgentLib.register(device3, function (error) {
// should.not.exist(error);
// contextBrokerMock.done();
// done();
// });
// });
it('should register as Context Provider of the commands', function (done) {
iotAgentLib.register(device3, function (error) {
should.not.exist(error);
contextBrokerMock.done();
done();
});
});
});

describe('When multiple command updates via PATCH /attrs arrive to the IoT Agent as Context Provider', function () {
Expand Down
86 changes: 43 additions & 43 deletions test/unit/ngsi-ld/lazyAndCommands/merge-patch-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,7 @@ const iotAgentConfig = {
types: {
Robot: {
internalAttributes: [],
commands: [
{
name: 'position',
object_id: 'pos',
type: 'Object'
},
{
name: 'orientation',
type: 'Object'
}
],
commands: [],
lazy: [
{
name: 'batteryLevel',
Expand All @@ -82,7 +72,18 @@ const iotAgentConfig = {
const device3 = {
id: 'r2d2',
type: 'Robot',
service: 'smartgondor'
service: 'smartgondor',
commands: [
{
name: 'position',
object_id: 'pos',
type: 'Object'
},
{
name: 'orientation',
type: 'Object'
}
]
};

describe('NGSI-LD - Merge-Patch functionalities', function () {
Expand Down Expand Up @@ -154,37 +155,36 @@ describe('NGSI-LD - Merge-Patch functionalities', function () {
});
});

// FIXME: after (PR#1396)
// it('should call the client handler once', function (done) {
// let handlerCalled = 0;

// iotAgentLib.setMergePatchHandler(function (id, type, service, subservice, attributes, callback) {
// id.should.equal('urn:ngsi-ld:' + device3.type + ':' + device3.id);
// type.should.equal(device3.type);
// attributes[0].name.should.equal('position');
// attributes[1].name.should.equal('orientation');
// should.equal(attributes[1].value, null);
// handlerCalled++;
// callback(null, {
// id,
// type,
// attributes: [
// {
// name: 'position',
// type: 'Array',
// value: '[28, -104, 23]'
// }
// ]
// });
// });

// request(options, function (error, response, body) {
// should.not.exist(error);
// response.statusCode.should.equal(200);
// handlerCalled.should.equal(1);
// done();
// });
// });
it('should call the client handler once', function (done) {
let handlerCalled = 0;

iotAgentLib.setMergePatchHandler(function (id, type, service, subservice, attributes, callback) {
id.should.equal('urn:ngsi-ld:' + device3.type + ':' + device3.id);
type.should.equal(device3.type);
attributes[0].name.should.equal('position');
attributes[1].name.should.equal('orientation');
should.equal(attributes[1].value, null);
handlerCalled++;
callback(null, {
id,
type,
attributes: [
{
name: 'position',
type: 'Array',
value: '[28, -104, 23]'
}
]
});
});

request(options, function (error, response, body) {
should.not.exist(error);
response.statusCode.should.equal(200);
handlerCalled.should.equal(1);
done();
});
});
});

xdescribe('When a partial update PATCH with an NGSI-LD Null arrives to the IoT Agent as Context Provider', function () {
Expand Down
Loading

0 comments on commit 0579bff

Please sign in to comment.