Skip to content

Commit

Permalink
fixup! test: add tests for exploreDirectory method
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Dec 22, 2023
1 parent 5eda27d commit 1fd39b2
Showing 1 changed file with 61 additions and 5 deletions.
66 changes: 61 additions & 5 deletions test/core/discovery_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,30 @@ final invalidTestDiscoveryUri =
Uri.parse('$testUriScheme://[::2]/.well-known/wot');
final directoryTestUri1 = Uri.parse('$testUriScheme://[::3]/.well-known/wot');
final directoryTestThingsUri1 = Uri.parse('$testUriScheme://[::3]/things');
final directoryTestUri2 = Uri.parse('$testUriScheme://[::4]/.well-known/wot');
final directoryTestThingsUri2 = Uri.parse('$testUriScheme://[::4]/things');

const validTestTitle = 'Test TD';
const validTestTitle1 = 'Test TD 1';
const validTestThingDescription = '''
{
"@context": "https://www.w3.org/2022/wot/td/v1.1",
"title": "$validTestTitle",
"title": "$validTestTitle1",
"security": "nosec_sc",
"securityDefinitions": {
"nosec_sc": {"scheme": "nosec"}
}
}
''';

const validDirectoryTestTitle1 = 'Test TD 2';
final directoryThingDescription1 = '''
{
"@context": [
"https://www.w3.org/2022/wot/td/v1.1",
"https://www.w3.org/2022/wot/discovery"
],
"@type": "ThingDirectory",
"title": "$validTestTitle",
"title": "$validDirectoryTestTitle1",
"security": "nosec_sc",
"securityDefinitions": {
"nosec_sc": {"scheme": "nosec"}
Expand All @@ -55,6 +58,31 @@ final directoryThingDescription1 = '''
}
''';

const validDirectoryTestTitle2 = 'Test TD 2';
final directoryThingDescription2 = '''
{
"@context": [
"https://www.w3.org/2022/wot/td/v1.1",
"https://www.w3.org/2022/wot/discovery"
],
"@type": "ThingDirectory",
"title": "$validDirectoryTestTitle2",
"security": "nosec_sc",
"securityDefinitions": {
"nosec_sc": {"scheme": "nosec"}
},
"properties": {
"things": {
"forms": [
{
"href": "$directoryTestThingsUri2"
}
]
}
}
}
''';

const invalidTestThingDescription = '"Hi there!"';

void main() {
Expand All @@ -70,7 +98,7 @@ void main() {
final thingDescription =
await wot.requestThingDescription(validTestDiscoveryUri);

expect(thingDescription.title, validTestTitle);
expect(thingDescription.title, validTestTitle1);
});

test(
Expand Down Expand Up @@ -107,11 +135,31 @@ void main() {
var counter = 0;
await for (final thingDescription in thingDiscoveryProcess) {
counter++;
expect(thingDescription.title, validTestTitle);
expect(thingDescription.title, validTestTitle1);
}
expect(counter, 1);
expect(thingDiscoveryProcess.done, true);
});

test('should be able to handle invalid TDs during discovery', () async {
final servient = Servient(
clientFactories: [
_MockedProtocolClientFactory(),
],
);

final wot = await servient.start();
final thingDiscoveryProcess =
await wot.exploreDirectory(directoryTestUri2);

var counter = 0;
await for (final _ in thingDiscoveryProcess) {
counter++;
}
expect(counter, 0);
expect(thingDiscoveryProcess.done, true);
// expect(thingDiscoveryProcess.error, isNotNull);
});
});
}

Expand All @@ -135,6 +183,10 @@ class _MockedProtocolClient implements ProtocolClient {
return '[$validTestThingDescription]'.toContent('application/td+json');
}

if (href == directoryTestThingsUri2) {
return '[$invalidTestThingDescription]'.toContent('application/td+json');
}

throw StateError('Encountered an unknown URI $href.');
}

Expand All @@ -152,6 +204,10 @@ class _MockedProtocolClient implements ProtocolClient {
return directoryThingDescription1.toDiscoveryContent(url);
}

if (url == directoryTestUri2) {
return directoryThingDescription2.toDiscoveryContent(url);
}

throw StateError('Encountered invalid URL.');
}

Expand Down

0 comments on commit 1fd39b2

Please sign in to comment.