Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Commit c405e32

Browse files
author
Gabriel Schulhof
committed
Tests: Add resource presence test
1 parent b88e8ef commit c405e32

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed

tests/Presence - Resource/client.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2016 Intel Corporation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// The file names for client and server are reversed in this test, because the test suite uses the
16+
// file name to decide which to run first ("server.js"). In the case of presence, however, the
17+
// client must run first, because it must catch the announcement from the server when it comes up.
18+
19+
var ocf = require( process.argv[ 3 ] );
20+
21+
console.log( JSON.stringify( { assertionCount: 0 } ) );
22+
23+
ocf.device.name = "test-device-" + process.argv[ 2 ];
24+
25+
Promise.all( [
26+
ocf.server.register( {
27+
resourcePath: "/target-resource",
28+
resourceTypes: [ "core.light" ],
29+
interfaces: [ "oic.if.baseline" ],
30+
discoverable: true
31+
} ),
32+
ocf.server.register( {
33+
resourcePath: "/a/" + process.argv[ 2 ],
34+
resourceTypes: [ "core.light" ],
35+
interfaces: [ "oic.if.baseline" ],
36+
discoverable: true
37+
} )
38+
] ).then( function( resources ) {
39+
resources[ 0 ].onupdate( function( request ) {
40+
if ( request.data.uuid === process.argv[ 2 ] ) {
41+
resources[ 1 ].unregister().then( function() {
42+
return request.respond();
43+
} ).catch( function( error ) {
44+
console.log( JSON.stringify( { assertion: "ok", arguments: [
45+
"Server: Failed to respond to update() event: " + ( "" + error ) + "\n" +
46+
JSON.stringify( error, null, 4 )
47+
] } ) );
48+
} );
49+
}
50+
} );
51+
} ).catch( function( error ) {
52+
console.log( JSON.stringify( { assertion: "ok", arguments: [
53+
false, "Server: Unexpected error: " + ( "" + error ) + "\n" +
54+
JSON.stringify( error, null, 4 )
55+
] } ) );
56+
} );

tests/Presence - Resource/server.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright 2016 Intel Corporation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// The file names for client and server are reversed in this test, because the test suite uses the
16+
// file name to decide which to run first ("server.js"). In the case of presence, however, the
17+
// client must run first, because it must catch the announcement from the server when it comes up.
18+
19+
var client = require( process.argv[ 3 ] ).client;
20+
21+
console.log( JSON.stringify( { assertionCount: 1 } ) );
22+
23+
var interesting = {};
24+
25+
var devices = [];
26+
var resources = [];
27+
28+
// Collect the information we need for setting up the "delete" listener
29+
function testSetUp() {
30+
var device, resource;
31+
32+
while ( devices.length > 0 && !interesting.device ) {
33+
device = devices.shift();
34+
if ( device.name === "test-device-" + process.argv[ 2 ] ) {
35+
interesting.device = device;
36+
}
37+
}
38+
39+
while ( resources.length > 0 && interesting.device &&
40+
!( interesting.object && interesting.control ) ) {
41+
resource = resources.shift();
42+
if ( resource.deviceId === interesting.device.uuid ) {
43+
if ( resource.resourcePath === "/a/" + process.argv[ 2 ] ) {
44+
interesting.object = resource;
45+
} else if ( resource.resourcePath === "/target-resource" ) {
46+
interesting.control = resource;
47+
}
48+
}
49+
}
50+
51+
if ( interesting.device && interesting.control && interesting.object ) {
52+
client
53+
.removeListener( "resourcefound", resourcefound )
54+
.removeListener( "devicefound", devicefound );
55+
interesting.object.on( "delete", function() {
56+
console.log( JSON.stringify( { assertion: "ok", arguments: [
57+
true, "Client: Received 'delete' event"
58+
] } ) );
59+
console.log( JSON.stringify( { finished: 0 } ) );
60+
} );
61+
interesting.control.properties.uuid = process.argv[ 2 ];
62+
client.update( interesting.control ).catch( function( error ) {
63+
console.log( JSON.stringify( { assertion: "ok", arguments: [
64+
false, "Client: Failed to update() control resource: " + ( "" + error ) + "\n" +
65+
JSON.stringify( error, null, 4 )
66+
] } ) );
67+
} );
68+
}
69+
}
70+
71+
function resourcefound( resource ) {
72+
resources.push( resource );
73+
testSetUp();
74+
}
75+
76+
function devicefound( device ) {
77+
devices.push( device );
78+
testSetUp();
79+
}
80+
81+
client
82+
.on( "resourcefound", resourcefound )
83+
.on( "devicefound", devicefound );
84+
85+
console.log( JSON.stringify( { ready: true } ) );

0 commit comments

Comments
 (0)